123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871 |
- using Resources;
- using SmartCoalApplication.Actions;
- using SmartCoalApplication.Base;
- using SmartCoalApplication.Core;
- using SmartCoalApplication.Resources;
- using SmartCoalApplication.SystemLayer;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace SmartCoalApplication
- {
- internal sealed class MainForm : PdnBaseForm
- {
- public AppWorkspace appWorkspace;
- private FloatingToolForm[] floaters;
- private System.Windows.Forms.Timer floaterOpacityTimer;
- private Timer deferredInitializationTimer;
- private IContainer components;
- private bool killAfterInit = false;
- //private SplashForm splashForm = null;
- private List<string> queuedInstanceMessages = new List<string>();
- private void SingleInstanceManager_InstanceMessageReceived(object sender, EventArgs e)
- {
- BeginInvoke(new Procedure(ProcessQueuedInstanceMessages), null);
- }
- public MainForm() : this(new string[0])
- {
- }
- public const int WM_DEVICECHANGE = 0x219;
- public const int DBT_DEVNODES_CHANGED = 0x0007;
- protected override void WndProc(ref Message m)
- {
- if (m.Msg == WM_DEVICECHANGE) //设备改变消息
- {
- switch (m.WParam.ToInt32())
- {
- case DBT_DEVNODES_CHANGED:
- break;
- }
- }
- base.WndProc(ref m);
- }
- private enum ArgumentAction
- {
- Open,
- OpenUntitled,
- NoOp
- }
- private bool SplitMessage(string message, out ArgumentAction action, out string actionParm)
- {
- if (message.Length == 0)
- {
- action = ArgumentAction.NoOp;
- actionParm = null;
- return false;
- }
- const string untitledPrefix = "untitled:";
- if (message.IndexOf(untitledPrefix) == 0)
- {
- action = ArgumentAction.OpenUntitled;
- actionParm = message.Substring(untitledPrefix.Length);
- return true;
- }
- action = ArgumentAction.Open;
- actionParm = message;
- return true;
- }
- private bool ProcessMessage(string message)
- {
- if (IsDisposed)
- {
- return false;
- }
- ArgumentAction action;
- string actionParm;
- bool result;
- result = SplitMessage(message, out action, out actionParm);
- if (!result)
- {
- return true;
- }
- switch (action)
- {
- case ArgumentAction.NoOp:
- result = true;
- break;
- case ArgumentAction.Open:
- Activate();
- if (IsCurrentModalForm && Enabled)
- {
- result = this.appWorkspace.OpenFileInNewWorkspace(actionParm);
- }
- break;
- case ArgumentAction.OpenUntitled:
- Activate();
- if (!string.IsNullOrEmpty(actionParm) && IsCurrentModalForm && Enabled)
- {
- result = this.appWorkspace.OpenFileInNewWorkspace(actionParm, false);
- if (result)
- {
- this.appWorkspace.ActiveDocumentWorkspace.SetDocumentSaveOptions(null);
- this.appWorkspace.ActiveDocumentWorkspace.Document.Dirty = true;
- }
- }
- break;
- default:
- throw new InvalidEnumArgumentException();
- }
- return result;
- }
- private void ProcessQueuedInstanceMessages()
- {
- if (IsDisposed)
- {
- return;
- }
- if (Program.instance.splashForm != null)
- {
- Program.instance.splashForm.Close();
- Program.instance.splashForm.Dispose();
- Program.instance.splashForm = null;
- }
- }
- private void Application_Idle(object sender, EventArgs e)
- {
- if (!this.IsDisposed && this.queuedInstanceMessages.Count > 0)
- {
- ProcessQueuedInstanceMessages();
- }
- }
- private void PositionFloatingForms()
- {
- this.appWorkspace.ResetFloatingForms();
- try
- {
- SnapManager.Load(Settings.CurrentUser);
- }
- catch
- {
- this.appWorkspace.ResetFloatingForms();
- }
- foreach (FloatingToolForm ftf in floaters)
- {
- this.AddOwnedForm(ftf);
- }
- if (Settings.CurrentUser.GetBoolean(SettingNames.RuleListFormVisible, false))
- {
- this.appWorkspace.Widgets.RuleListForm.Show();
- }
- // If the floating form is off screen somehow, reset it
- // We've been getting a lot of reports where people say their Colors window has disappeared
- Screen[] allScreens = Screen.AllScreens;
- foreach (FloatingToolForm ftf in this.floaters)
- {
- if (!ftf.Visible)
- {
- continue;
- }
- bool reset = false;
- try
- {
- bool foundAScreen = false;
- foreach (Screen screen in allScreens)
- {
- Rectangle intersect = Rectangle.Intersect(screen.Bounds, ftf.Bounds);
- if (intersect.Width > 0 && intersect.Height > 0)
- {
- foundAScreen = true;
- break;
- }
- }
- if (!foundAScreen)
- {
- reset = true;
- }
- }
- catch (Exception)
- {
- reset = true;
- }
- if (reset)
- {
- this.appWorkspace.ResetFloatingForm(ftf);
- }
- }
- this.floaterOpacityTimer.Enabled = true;
- }
- public MainForm(string[] args)
- {
- bool canSetCurrentDir = true;
- this.StartPosition = FormStartPosition.WindowsDefaultLocation;
- //bool splash = false;
- List<string> fileNames = new List<string>();
- // Parse command line arguments
- foreach (string argument in args)
- {
- if (0 == string.Compare(argument, "/dontForceGC"))
- {
- Utility.AllowGCFullCollect = false;
- }
- else if (0 == string.Compare(argument, "/splash", true))
- {
- //splash = true;
- }
- else if (argument.Length > 0 && argument[0] != '/')
- {
- //try
- //{
- // string fullPath = Path.GetFullPath(argument);
- // fileNames.Add(fullPath);
- //}
- //catch (Exception)
- //{
- // fileNames.Add(argument);
- // canSetCurrentDir = false;
- //}
- //splash = true;
- }
- }
- if (canSetCurrentDir)
- {
- try
- {
- Environment.CurrentDirectory = PdnInfo.GetApplicationDir();
- }
- catch (Exception)
- {
- }
- }
- //if (Program.instance.configModel.StartImgStatus == 2)
- //{
- // this.splashForm = new SplashForm(Program.instance.configModel.StartImg);
- // this.splashForm.TopMost = true;
- // this.splashForm.Show();
- // this.splashForm.Update();
- //}
- InitializeComponent();
- this.Icon = PdnInfo.AppIcon;
- LoadSettings();
- if (Program.instance.splashForm != null && !Program.instance.splashForm.IsDisposed)
- Program.instance.splashForm.Hide();
- LoadWindowState();
- deferredInitializationTimer.Enabled = true;
- Application.Idle += new EventHandler(Application_Idle);
- this.Shown += new EventHandler(this.MainForm_Shown);
- }
- private void MainForm_Shown(object sender, EventArgs e)
- {
- this.appWorkspace.ToolBar.test1(false);
- this.appWorkspace.SetTopLeftCheckState();
- if (Program.instance.configModel.autoOpenDialog) {
- using (AutomaticMeasurement.AutomaticMeasurement af = new AutomaticMeasurement.AutomaticMeasurement(this.appWorkspace))
- {
- af.StartPosition = FormStartPosition.CenterScreen;
- af.ShowDialog(this.appWorkspace);
- }
- }
- #region [获取系统内存]
- Task.Factory.StartNew(() =>
- {
- while (true)
- {
- System.Threading.Thread.Sleep(1000 * 60);
- PerformanceCounter ramCounter;
- ramCounter = new PerformanceCounter("Memory", "Available MBytes");
- if (ramCounter.NextValue() <= 100f)
- {
- MessageBox.Show("内存即将不足,请谨慎操作!");
- System.Threading.Thread.Sleep(1000 * 60);
- }
- }
- });
-
- #endregion
- }
- protected override void OnShown(EventArgs e)
- {
- base.OnShown(e);
- }
- private void LoadWindowState()
- {
- try
- {
- FormWindowState fws = (FormWindowState)Enum.Parse(typeof(FormWindowState),
- Settings.CurrentUser.GetString(SettingNames.WindowState, WindowState.ToString()), true);
- if (fws != FormWindowState.Minimized)
- {
- if (fws != FormWindowState.Maximized)
- {
- Rectangle newBounds = Rectangle.Empty;
- newBounds.Width = Settings.CurrentUser.GetInt32(SettingNames.Width, this.Width);
- newBounds.Height = Settings.CurrentUser.GetInt32(SettingNames.Height, this.Height);
- int left = Settings.CurrentUser.GetInt32(SettingNames.Left, this.Left);
- int top = Settings.CurrentUser.GetInt32(SettingNames.Top, this.Top);
- newBounds.Location = new Point(left, top);
- this.Bounds = newBounds;
- }
- this.WindowState = fws;
- }
- }
- catch
- {
- try
- {
- Settings.CurrentUser.Delete(
- new string[]
- {
- SettingNames.Width,
- SettingNames.Height,
- SettingNames.WindowState,
- SettingNames.Top,
- SettingNames.Left
- });
- }
- catch
- {
-
- }
- }
- }
- private void LoadSettings()
- {
- try
- {
- PdnBaseForm.EnableOpacity = Settings.CurrentUser.GetBoolean(SettingNames.TranslucentWindows, true);
- }
- catch (Exception)
- {
- try
- {
- Settings.CurrentUser.Delete(
- new string[]
- {
- SettingNames.TranslucentWindows
- });
- }
- catch
- {
- }
- }
- }
- private void SaveSettings()
- {
- Settings.CurrentUser.SetInt32(SettingNames.Width, this.Width);
- Settings.CurrentUser.SetInt32(SettingNames.Height, this.Height);
- Settings.CurrentUser.SetInt32(SettingNames.Top, this.Top);
- Settings.CurrentUser.SetInt32(SettingNames.Left, this.Left);
- Settings.CurrentUser.SetString(SettingNames.WindowState, this.WindowState.ToString());
- Settings.CurrentUser.SetBoolean(SettingNames.TranslucentWindows, PdnBaseForm.EnableOpacity);
- this.appWorkspace.SaveSettings();
- }
- protected override void OnQueryEndSession(CancelEventArgs e)
- {
- if (IsCurrentModalForm)
- {
- OnClosing(e);
- }
- else
- {
- foreach (Form form in Application.OpenForms)
- {
- PdnBaseForm asPDF = form as PdnBaseForm;
- if (asPDF != null)
- {
- asPDF.Flash();
- }
- }
- e.Cancel = true;
- }
- base.OnQueryEndSession(e);
- }
- protected override void OnClosing(CancelEventArgs e)
- {
- if (!e.Cancel)
- {
- if (this.appWorkspace != null)
- {
- if (this.appWorkspace.DocumentWorkspaces != null && this.appWorkspace.DocumentWorkspaces.Length > 0)
- {
- foreach (DocumentWorkspace document in this.appWorkspace.DocumentWorkspaces)
- {
- CloseWorkspaceAction action = new CloseWorkspaceAction();
- this.appWorkspace.PerformAction(action);
- e.Cancel = action.Cancelled;
- if (e.Cancel) break;
- }
- }
- }
- }
- if (!e.Cancel)
- {
- SaveSettings();
- this.Hide();
- if (this.queuedInstanceMessages != null)
- {
- this.queuedInstanceMessages.Clear();
- }
- }
- base.OnClosing(e);
- }
- protected override void OnClosed(EventArgs e)
- {
- base.OnClosed(e);
- }
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (this.components != null)
- {
- this.components.Dispose();
- this.components = null;
- }
- if (this.floaterOpacityTimer != null)
- {
- this.floaterOpacityTimer.Tick -= new System.EventHandler(this.FloaterOpacityTimer_Tick);
- this.floaterOpacityTimer.Dispose();
- this.floaterOpacityTimer = null;
- }
- }
- try
- {
- base.Dispose(disposing);
- }
- catch (RankException)
- {
- // System.Windows.Forms.PropertyStore
- // Discard error - bug #2746
- }
- }
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.components = new Container();
- this.appWorkspace = new AppWorkspace(this);
- this.floaterOpacityTimer = new System.Windows.Forms.Timer(this.components);
- this.deferredInitializationTimer = new System.Windows.Forms.Timer(this.components);
- this.SuspendLayout();
- //
- // floaterOpacityTimer
- //
- this.floaterOpacityTimer.Enabled = false;
- this.floaterOpacityTimer.Interval = 25;
- this.floaterOpacityTimer.Tick += new System.EventHandler(this.FloaterOpacityTimer_Tick);
- //
- // appWorkspace
- //
- this.appWorkspace.Dock = System.Windows.Forms.DockStyle.Fill;
- this.appWorkspace.Location = new System.Drawing.Point(0, 0);
- this.appWorkspace.Name = "appWorkspace";
- this.appWorkspace.Size = new System.Drawing.Size(752, 648);
- this.appWorkspace.TabIndex = 2;
- this.appWorkspace.ActiveDocumentWorkspaceChanging += new EventHandler(AppWorkspace_ActiveDocumentWorkspaceChanging);
- this.appWorkspace.ActiveDocumentWorkspaceChanged += new EventHandler(AppWorkspace_ActiveDocumentWorkspaceChanged);
- //
- // deferredInitializationTimer
- //
- this.deferredInitializationTimer.Interval = 250;
- this.deferredInitializationTimer.Tick += new EventHandler(DeferredInitialization);
- //
- // MainForm
- //
- try
- {
- this.AllowDrop = true;
- }
- catch (InvalidOperationException)
- {
- // Discard error. See bug #2605.
- }
- this.AutoScaleDimensions = new SizeF(96F, 96F);
- this.AutoScaleMode = AutoScaleMode.Dpi;
- this.ClientSize = new Size(950, 738);
- this.Controls.Add(this.appWorkspace);
- this.Name = "MainForm";
- this.StartPosition = FormStartPosition.WindowsDefaultLocation;
- this.ForceActiveTitleBar = true;
- this.KeyPreview = true;
- this.Controls.SetChildIndex(this.appWorkspace, 0);
- this.ResumeLayout(false);
- this.PerformLayout();
- }
- private void AppWorkspace_ActiveDocumentWorkspaceChanging(object sender, EventArgs e)
- {
- if (this.appWorkspace.ActiveDocumentWorkspace != null)
- {
- this.appWorkspace.ActiveDocumentWorkspace.ScaleFactorChanged -= DocumentWorkspace_ScaleFactorChanged;
- this.appWorkspace.ActiveDocumentWorkspace.DocumentChanged -= DocumentWorkspace_DocumentChanged;
- this.appWorkspace.ActiveDocumentWorkspace.SaveOptionsChanged -= DocumentWorkspace_SaveOptionsChanged;
- }
- }
- private void AppWorkspace_ActiveDocumentWorkspaceChanged(object sender, EventArgs e)
- {
- if (this.appWorkspace.ActiveDocumentWorkspace != null)
- {
- this.appWorkspace.ActiveDocumentWorkspace.ScaleFactorChanged += DocumentWorkspace_ScaleFactorChanged;
- this.appWorkspace.ActiveDocumentWorkspace.DocumentChanged += DocumentWorkspace_DocumentChanged;
- this.appWorkspace.ActiveDocumentWorkspace.SaveOptionsChanged += DocumentWorkspace_SaveOptionsChanged;
- }
- SetTitleText();
- }
- private void DocumentWorkspace_SaveOptionsChanged(object sender, EventArgs e)
- {
- SetTitleText();
- }
- private void FloaterOpacityTimer_Tick(object sender, System.EventArgs e)
- {
- if (this.WindowState == FormWindowState.Minimized ||
- this.floaters == null ||
- !PdnBaseForm.EnableOpacity ||
- this.appWorkspace.ActiveDocumentWorkspace == null)
- {
- return;
- }
- // Here's the behavior we want for our floaters:
- // 1. If the mouse is within a floaters rectangle, it should transition to fully opaque
- // 2. If the mouse is outside the floater's rectangle, it should transition to partially
- // opaque
- // 3. However, if the floater is outside where the document is visible on screen, it
- // should always be fully opaque.
- Rectangle screenDocRect;
- try
- {
- screenDocRect = this.appWorkspace.ActiveDocumentWorkspace.VisibleDocumentBounds;
- }
- catch (ObjectDisposedException)
- {
- return; // do nothing, we are probably in the process of shutting down the app
- }
- for (int i = 0; i < floaters.Length; ++i)
- {
- FloatingToolForm ftf = floaters[i];
- Rectangle intersect = Rectangle.Intersect(screenDocRect, ftf.Bounds);
- double opacity = -1.0;
- try
- {
- if (intersect.Width == 0 ||
- intersect.Height == 0 ||
- (ftf.Bounds.Contains(Control.MousePosition) &&
- !appWorkspace.ActiveDocumentWorkspace.IsMouseCaptured()) ||
- Utility.DoesControlHaveMouseCaptured(ftf))
- {
- opacity = Math.Min(1.0, ftf.Opacity + 0.125);
- }
- else
- {
- opacity = Math.Max(0.75, ftf.Opacity - 0.0625);
- }
- if (opacity != ftf.Opacity)
- {
- ftf.Opacity = opacity;
- }
- }
- catch (System.ComponentModel.Win32Exception)
- {
- // We just eat the exception. Chris Strahl was having some problem where opacity was 0.7
- // and we were trying to set it to 0.7 and it said "the parameter is incorrect"
- // ... which is stupid. Bad NVIDIA drivers for his GeForce Go?
- }
- }
- }
- protected override void OnLoad(EventArgs e)
- {
- EnsureFormIsOnScreen();
- if (killAfterInit)
- {
- Application.Exit();
- }
- this.floaters = new FloatingToolForm[] {
- appWorkspace.Widgets.RuleListForm,
- //appWorkspace.Widgets.PixelTrackingDialog,
- //appWorkspace.Widgets.RunningDialog,
- //appWorkspace.Widgets.HistogramDialog,
- //appWorkspace.Widgets.ScratchTreatmentDialog,
- //appWorkspace.Widgets.SmudgeTreatmentDialog,
- //appWorkspace.Widgets.LabelListDialog,
- //appWorkspace.Widgets.WorkFlowDialog,
- //appWorkspace.Widgets.MeasureListDialog,
- //appWorkspace.Widgets.OpticalDensityDialog,
- //appWorkspace.Widgets.ProjectEngineering
- };
- foreach (FloatingToolForm ftf in floaters)
- {
- ftf.Closing += this.HideInsteadOfCloseHandler;
- }
- PositionFloatingForms();
- base.OnLoad(e);
- }
- protected override void OnResize(EventArgs e)
- {
- if (floaterOpacityTimer != null)
- {
- if (WindowState == FormWindowState.Minimized)
- {
- if (this.floaterOpacityTimer.Enabled)
- {
- this.floaterOpacityTimer.Enabled = false;
- }
- }
- else
- {
- if (!this.floaterOpacityTimer.Enabled)
- {
- this.floaterOpacityTimer.Enabled = true;
- }
- this.FloaterOpacityTimer_Tick(this, EventArgs.Empty);
- }
- }
- base.OnResize(e);
- }
- private void DocumentWorkspace_DocumentChanged(object sender, System.EventArgs e)
- {
- SetTitleText();
- OnResize(EventArgs.Empty);
- }
- private void SetTitleText()
- {
- if (this.appWorkspace == null)
- {
- return;
- }
- this.Text = PdnInfo.GetAppName();
- }
- private void OnMenuDropDownClosed(object sender, System.EventArgs e)
- {
- ToolStripMenuItem menu = (ToolStripMenuItem)sender;
- foreach (ToolStripItem tsi in menu.DropDownItems)
- {
- tsi.Enabled = true;
- }
- }
- private void HideInsteadOfCloseHandler(object sender, CancelEventArgs e)
- {
- e.Cancel = true;
- ((Form)sender).Hide();
- }
- protected override void OnDragEnter(DragEventArgs drgevent)
- {
- if (Enabled && drgevent.Data.GetDataPresent(DataFormats.FileDrop))
- {
- string[] files = (string[])drgevent.Data.GetData(DataFormats.FileDrop);
- foreach (string file in files)
- {
- try
- {
- FileAttributes fa = System.IO.File.GetAttributes(file);
- if ((fa & FileAttributes.Directory) == 0)
- {
- drgevent.Effect = DragDropEffects.Copy;
- }
- }
- catch
- {
- }
- }
- }
- base.OnDragEnter(drgevent);
- }
- private string[] PruneDirectories(string[] fileNames)
- {
- List<string> result = new List<string>();
- foreach (string fileName in fileNames)
- {
- try
- {
- FileAttributes fa = System.IO.File.GetAttributes(fileName);
- if ((fa & FileAttributes.Directory) == 0)
- {
- result.Add(fileName);
- }
- }
- catch
- {
- }
- }
- return result.ToArray();
- }
- protected override void OnDragDrop(DragEventArgs drgevent)
- {
- Activate();
- if (!IsCurrentModalForm || !Enabled)
- {
- // do nothing
- }
- else if (drgevent.Data.GetDataPresent(DataFormats.FileDrop))
- {
- string[] allFiles = (string[])drgevent.Data.GetData(DataFormats.FileDrop);
- if (allFiles == null)
- {
- return;
- }
- string[] files = PruneDirectories(allFiles);
- if (files.Length == 0)
- {
- return;
- }
- this.appWorkspace.OpenFilesInNewWorkspace(files);
- }
- base.OnDragDrop(drgevent);
- }
- private void DocumentWorkspace_ScaleFactorChanged(object sender, EventArgs e)
- {
- SetTitleText();
- }
- protected override void OnSizeChanged(EventArgs e)
- {
- base.OnSizeChanged(e);
- SetTitleText();
- }
- public void DeferredInitialization(object sender, EventArgs e)
- {
- this.deferredInitializationTimer.Enabled = false;
- this.deferredInitializationTimer.Tick -= new EventHandler(DeferredInitialization);
- this.deferredInitializationTimer.Dispose();
- this.deferredInitializationTimer = null;
- }
- /*protected override void OnHelpRequested(HelpEventArgs hevent)
- {
- // F1 is already handled by the Menu->Help menu item. No need to process it twice.
- hevent.Handled = true;
- base.OnHelpRequested(hevent);
- }*/
- }
- }
|