MainForm.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. using Resources;
  2. using SmartCoalApplication.Actions;
  3. using SmartCoalApplication.Base;
  4. using SmartCoalApplication.Core;
  5. using SmartCoalApplication.Resources;
  6. using SmartCoalApplication.SystemLayer;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace SmartCoalApplication
  16. {
  17. internal sealed class MainForm : PdnBaseForm
  18. {
  19. public AppWorkspace appWorkspace;
  20. private FloatingToolForm[] floaters;
  21. private System.Windows.Forms.Timer floaterOpacityTimer;
  22. private Timer deferredInitializationTimer;
  23. private IContainer components;
  24. private bool killAfterInit = false;
  25. //private SplashForm splashForm = null;
  26. private List<string> queuedInstanceMessages = new List<string>();
  27. private void SingleInstanceManager_InstanceMessageReceived(object sender, EventArgs e)
  28. {
  29. BeginInvoke(new Procedure(ProcessQueuedInstanceMessages), null);
  30. }
  31. public MainForm() : this(new string[0])
  32. {
  33. }
  34. public const int WM_DEVICECHANGE = 0x219;
  35. public const int DBT_DEVNODES_CHANGED = 0x0007;
  36. protected override void WndProc(ref Message m)
  37. {
  38. if (m.Msg == WM_DEVICECHANGE) //设备改变消息
  39. {
  40. switch (m.WParam.ToInt32())
  41. {
  42. case DBT_DEVNODES_CHANGED:
  43. break;
  44. }
  45. }
  46. base.WndProc(ref m);
  47. }
  48. private enum ArgumentAction
  49. {
  50. Open,
  51. OpenUntitled,
  52. NoOp
  53. }
  54. private bool SplitMessage(string message, out ArgumentAction action, out string actionParm)
  55. {
  56. if (message.Length == 0)
  57. {
  58. action = ArgumentAction.NoOp;
  59. actionParm = null;
  60. return false;
  61. }
  62. const string untitledPrefix = "untitled:";
  63. if (message.IndexOf(untitledPrefix) == 0)
  64. {
  65. action = ArgumentAction.OpenUntitled;
  66. actionParm = message.Substring(untitledPrefix.Length);
  67. return true;
  68. }
  69. action = ArgumentAction.Open;
  70. actionParm = message;
  71. return true;
  72. }
  73. private bool ProcessMessage(string message)
  74. {
  75. if (IsDisposed)
  76. {
  77. return false;
  78. }
  79. ArgumentAction action;
  80. string actionParm;
  81. bool result;
  82. result = SplitMessage(message, out action, out actionParm);
  83. if (!result)
  84. {
  85. return true;
  86. }
  87. switch (action)
  88. {
  89. case ArgumentAction.NoOp:
  90. result = true;
  91. break;
  92. case ArgumentAction.Open:
  93. Activate();
  94. if (IsCurrentModalForm && Enabled)
  95. {
  96. result = this.appWorkspace.OpenFileInNewWorkspace(actionParm);
  97. }
  98. break;
  99. case ArgumentAction.OpenUntitled:
  100. Activate();
  101. if (!string.IsNullOrEmpty(actionParm) && IsCurrentModalForm && Enabled)
  102. {
  103. result = this.appWorkspace.OpenFileInNewWorkspace(actionParm, false);
  104. if (result)
  105. {
  106. this.appWorkspace.ActiveDocumentWorkspace.SetDocumentSaveOptions(null);
  107. this.appWorkspace.ActiveDocumentWorkspace.Document.Dirty = true;
  108. }
  109. }
  110. break;
  111. default:
  112. throw new InvalidEnumArgumentException();
  113. }
  114. return result;
  115. }
  116. private void ProcessQueuedInstanceMessages()
  117. {
  118. if (IsDisposed)
  119. {
  120. return;
  121. }
  122. if (Program.instance.splashForm != null)
  123. {
  124. Program.instance.splashForm.Close();
  125. Program.instance.splashForm.Dispose();
  126. Program.instance.splashForm = null;
  127. }
  128. }
  129. private void Application_Idle(object sender, EventArgs e)
  130. {
  131. if (!this.IsDisposed && this.queuedInstanceMessages.Count > 0)
  132. {
  133. ProcessQueuedInstanceMessages();
  134. }
  135. }
  136. private void PositionFloatingForms()
  137. {
  138. this.appWorkspace.ResetFloatingForms();
  139. try
  140. {
  141. SnapManager.Load(Settings.CurrentUser);
  142. }
  143. catch
  144. {
  145. this.appWorkspace.ResetFloatingForms();
  146. }
  147. foreach (FloatingToolForm ftf in floaters)
  148. {
  149. this.AddOwnedForm(ftf);
  150. }
  151. if (Settings.CurrentUser.GetBoolean(SettingNames.RuleListFormVisible, false))
  152. {
  153. this.appWorkspace.Widgets.RuleListForm.Show();
  154. }
  155. // If the floating form is off screen somehow, reset it
  156. // We've been getting a lot of reports where people say their Colors window has disappeared
  157. Screen[] allScreens = Screen.AllScreens;
  158. foreach (FloatingToolForm ftf in this.floaters)
  159. {
  160. if (!ftf.Visible)
  161. {
  162. continue;
  163. }
  164. bool reset = false;
  165. try
  166. {
  167. bool foundAScreen = false;
  168. foreach (Screen screen in allScreens)
  169. {
  170. Rectangle intersect = Rectangle.Intersect(screen.Bounds, ftf.Bounds);
  171. if (intersect.Width > 0 && intersect.Height > 0)
  172. {
  173. foundAScreen = true;
  174. break;
  175. }
  176. }
  177. if (!foundAScreen)
  178. {
  179. reset = true;
  180. }
  181. }
  182. catch (Exception)
  183. {
  184. reset = true;
  185. }
  186. if (reset)
  187. {
  188. this.appWorkspace.ResetFloatingForm(ftf);
  189. }
  190. }
  191. this.floaterOpacityTimer.Enabled = true;
  192. }
  193. public MainForm(string[] args)
  194. {
  195. bool canSetCurrentDir = true;
  196. this.StartPosition = FormStartPosition.WindowsDefaultLocation;
  197. //bool splash = false;
  198. List<string> fileNames = new List<string>();
  199. // Parse command line arguments
  200. foreach (string argument in args)
  201. {
  202. if (0 == string.Compare(argument, "/dontForceGC"))
  203. {
  204. Utility.AllowGCFullCollect = false;
  205. }
  206. else if (0 == string.Compare(argument, "/splash", true))
  207. {
  208. //splash = true;
  209. }
  210. else if (argument.Length > 0 && argument[0] != '/')
  211. {
  212. //try
  213. //{
  214. // string fullPath = Path.GetFullPath(argument);
  215. // fileNames.Add(fullPath);
  216. //}
  217. //catch (Exception)
  218. //{
  219. // fileNames.Add(argument);
  220. // canSetCurrentDir = false;
  221. //}
  222. //splash = true;
  223. }
  224. }
  225. if (canSetCurrentDir)
  226. {
  227. try
  228. {
  229. Environment.CurrentDirectory = PdnInfo.GetApplicationDir();
  230. }
  231. catch (Exception)
  232. {
  233. }
  234. }
  235. //if (Program.instance.configModel.StartImgStatus == 2)
  236. //{
  237. // this.splashForm = new SplashForm(Program.instance.configModel.StartImg);
  238. // this.splashForm.TopMost = true;
  239. // this.splashForm.Show();
  240. // this.splashForm.Update();
  241. //}
  242. InitializeComponent();
  243. this.Icon = PdnInfo.AppIcon;
  244. LoadSettings();
  245. if (Program.instance.splashForm != null && !Program.instance.splashForm.IsDisposed)
  246. Program.instance.splashForm.Hide();
  247. LoadWindowState();
  248. deferredInitializationTimer.Enabled = true;
  249. Application.Idle += new EventHandler(Application_Idle);
  250. this.Shown += new EventHandler(this.MainForm_Shown);
  251. }
  252. private void MainForm_Shown(object sender, EventArgs e)
  253. {
  254. this.appWorkspace.ToolBar.test1(false);
  255. this.appWorkspace.SetTopLeftCheckState();
  256. if (Program.instance.configModel.autoOpenDialog) {
  257. using (AutomaticMeasurement.AutomaticMeasurement af = new AutomaticMeasurement.AutomaticMeasurement(this.appWorkspace))
  258. {
  259. af.StartPosition = FormStartPosition.CenterScreen;
  260. af.ShowDialog(this.appWorkspace);
  261. }
  262. }
  263. #region [获取系统内存]
  264. Task.Factory.StartNew(() =>
  265. {
  266. while (true)
  267. {
  268. System.Threading.Thread.Sleep(1000 * 60);
  269. PerformanceCounter ramCounter;
  270. ramCounter = new PerformanceCounter("Memory", "Available MBytes");
  271. if (ramCounter.NextValue() <= 100f)
  272. {
  273. MessageBox.Show("内存即将不足,请谨慎操作!");
  274. System.Threading.Thread.Sleep(1000 * 60);
  275. }
  276. }
  277. });
  278. #endregion
  279. }
  280. protected override void OnShown(EventArgs e)
  281. {
  282. base.OnShown(e);
  283. }
  284. private void LoadWindowState()
  285. {
  286. try
  287. {
  288. FormWindowState fws = (FormWindowState)Enum.Parse(typeof(FormWindowState),
  289. Settings.CurrentUser.GetString(SettingNames.WindowState, WindowState.ToString()), true);
  290. if (fws != FormWindowState.Minimized)
  291. {
  292. if (fws != FormWindowState.Maximized)
  293. {
  294. Rectangle newBounds = Rectangle.Empty;
  295. newBounds.Width = Settings.CurrentUser.GetInt32(SettingNames.Width, this.Width);
  296. newBounds.Height = Settings.CurrentUser.GetInt32(SettingNames.Height, this.Height);
  297. int left = Settings.CurrentUser.GetInt32(SettingNames.Left, this.Left);
  298. int top = Settings.CurrentUser.GetInt32(SettingNames.Top, this.Top);
  299. newBounds.Location = new Point(left, top);
  300. this.Bounds = newBounds;
  301. }
  302. this.WindowState = fws;
  303. }
  304. }
  305. catch
  306. {
  307. try
  308. {
  309. Settings.CurrentUser.Delete(
  310. new string[]
  311. {
  312. SettingNames.Width,
  313. SettingNames.Height,
  314. SettingNames.WindowState,
  315. SettingNames.Top,
  316. SettingNames.Left
  317. });
  318. }
  319. catch
  320. {
  321. }
  322. }
  323. }
  324. private void LoadSettings()
  325. {
  326. try
  327. {
  328. PdnBaseForm.EnableOpacity = Settings.CurrentUser.GetBoolean(SettingNames.TranslucentWindows, true);
  329. }
  330. catch (Exception)
  331. {
  332. try
  333. {
  334. Settings.CurrentUser.Delete(
  335. new string[]
  336. {
  337. SettingNames.TranslucentWindows
  338. });
  339. }
  340. catch
  341. {
  342. }
  343. }
  344. }
  345. private void SaveSettings()
  346. {
  347. Settings.CurrentUser.SetInt32(SettingNames.Width, this.Width);
  348. Settings.CurrentUser.SetInt32(SettingNames.Height, this.Height);
  349. Settings.CurrentUser.SetInt32(SettingNames.Top, this.Top);
  350. Settings.CurrentUser.SetInt32(SettingNames.Left, this.Left);
  351. Settings.CurrentUser.SetString(SettingNames.WindowState, this.WindowState.ToString());
  352. Settings.CurrentUser.SetBoolean(SettingNames.TranslucentWindows, PdnBaseForm.EnableOpacity);
  353. this.appWorkspace.SaveSettings();
  354. }
  355. protected override void OnQueryEndSession(CancelEventArgs e)
  356. {
  357. if (IsCurrentModalForm)
  358. {
  359. OnClosing(e);
  360. }
  361. else
  362. {
  363. foreach (Form form in Application.OpenForms)
  364. {
  365. PdnBaseForm asPDF = form as PdnBaseForm;
  366. if (asPDF != null)
  367. {
  368. asPDF.Flash();
  369. }
  370. }
  371. e.Cancel = true;
  372. }
  373. base.OnQueryEndSession(e);
  374. }
  375. protected override void OnClosing(CancelEventArgs e)
  376. {
  377. if (!e.Cancel)
  378. {
  379. if (this.appWorkspace != null)
  380. {
  381. if (this.appWorkspace.DocumentWorkspaces != null && this.appWorkspace.DocumentWorkspaces.Length > 0)
  382. {
  383. foreach (DocumentWorkspace document in this.appWorkspace.DocumentWorkspaces)
  384. {
  385. CloseWorkspaceAction action = new CloseWorkspaceAction();
  386. this.appWorkspace.PerformAction(action);
  387. e.Cancel = action.Cancelled;
  388. if (e.Cancel) break;
  389. }
  390. }
  391. }
  392. }
  393. if (!e.Cancel)
  394. {
  395. SaveSettings();
  396. this.Hide();
  397. if (this.queuedInstanceMessages != null)
  398. {
  399. this.queuedInstanceMessages.Clear();
  400. }
  401. }
  402. base.OnClosing(e);
  403. }
  404. protected override void OnClosed(EventArgs e)
  405. {
  406. base.OnClosed(e);
  407. }
  408. /// <summary>
  409. /// Clean up any resources being used.
  410. /// </summary>
  411. protected override void Dispose(bool disposing)
  412. {
  413. if (disposing)
  414. {
  415. if (this.components != null)
  416. {
  417. this.components.Dispose();
  418. this.components = null;
  419. }
  420. if (this.floaterOpacityTimer != null)
  421. {
  422. this.floaterOpacityTimer.Tick -= new System.EventHandler(this.FloaterOpacityTimer_Tick);
  423. this.floaterOpacityTimer.Dispose();
  424. this.floaterOpacityTimer = null;
  425. }
  426. }
  427. try
  428. {
  429. base.Dispose(disposing);
  430. }
  431. catch (RankException)
  432. {
  433. // System.Windows.Forms.PropertyStore
  434. // Discard error - bug #2746
  435. }
  436. }
  437. /// <summary>
  438. /// Required method for Designer support - do not modify
  439. /// the contents of this method with the code editor.
  440. /// </summary>
  441. private void InitializeComponent()
  442. {
  443. this.components = new Container();
  444. this.appWorkspace = new AppWorkspace(this);
  445. this.floaterOpacityTimer = new System.Windows.Forms.Timer(this.components);
  446. this.deferredInitializationTimer = new System.Windows.Forms.Timer(this.components);
  447. this.SuspendLayout();
  448. //
  449. // floaterOpacityTimer
  450. //
  451. this.floaterOpacityTimer.Enabled = false;
  452. this.floaterOpacityTimer.Interval = 25;
  453. this.floaterOpacityTimer.Tick += new System.EventHandler(this.FloaterOpacityTimer_Tick);
  454. //
  455. // appWorkspace
  456. //
  457. this.appWorkspace.Dock = System.Windows.Forms.DockStyle.Fill;
  458. this.appWorkspace.Location = new System.Drawing.Point(0, 0);
  459. this.appWorkspace.Name = "appWorkspace";
  460. this.appWorkspace.Size = new System.Drawing.Size(752, 648);
  461. this.appWorkspace.TabIndex = 2;
  462. this.appWorkspace.ActiveDocumentWorkspaceChanging += new EventHandler(AppWorkspace_ActiveDocumentWorkspaceChanging);
  463. this.appWorkspace.ActiveDocumentWorkspaceChanged += new EventHandler(AppWorkspace_ActiveDocumentWorkspaceChanged);
  464. //
  465. // deferredInitializationTimer
  466. //
  467. this.deferredInitializationTimer.Interval = 250;
  468. this.deferredInitializationTimer.Tick += new EventHandler(DeferredInitialization);
  469. //
  470. // MainForm
  471. //
  472. try
  473. {
  474. this.AllowDrop = true;
  475. }
  476. catch (InvalidOperationException)
  477. {
  478. // Discard error. See bug #2605.
  479. }
  480. this.AutoScaleDimensions = new SizeF(96F, 96F);
  481. this.AutoScaleMode = AutoScaleMode.Dpi;
  482. this.ClientSize = new Size(950, 738);
  483. this.Controls.Add(this.appWorkspace);
  484. this.Name = "MainForm";
  485. this.StartPosition = FormStartPosition.WindowsDefaultLocation;
  486. this.ForceActiveTitleBar = true;
  487. this.KeyPreview = true;
  488. this.Controls.SetChildIndex(this.appWorkspace, 0);
  489. this.ResumeLayout(false);
  490. this.PerformLayout();
  491. }
  492. private void AppWorkspace_ActiveDocumentWorkspaceChanging(object sender, EventArgs e)
  493. {
  494. if (this.appWorkspace.ActiveDocumentWorkspace != null)
  495. {
  496. this.appWorkspace.ActiveDocumentWorkspace.ScaleFactorChanged -= DocumentWorkspace_ScaleFactorChanged;
  497. this.appWorkspace.ActiveDocumentWorkspace.DocumentChanged -= DocumentWorkspace_DocumentChanged;
  498. this.appWorkspace.ActiveDocumentWorkspace.SaveOptionsChanged -= DocumentWorkspace_SaveOptionsChanged;
  499. }
  500. }
  501. private void AppWorkspace_ActiveDocumentWorkspaceChanged(object sender, EventArgs e)
  502. {
  503. if (this.appWorkspace.ActiveDocumentWorkspace != null)
  504. {
  505. this.appWorkspace.ActiveDocumentWorkspace.ScaleFactorChanged += DocumentWorkspace_ScaleFactorChanged;
  506. this.appWorkspace.ActiveDocumentWorkspace.DocumentChanged += DocumentWorkspace_DocumentChanged;
  507. this.appWorkspace.ActiveDocumentWorkspace.SaveOptionsChanged += DocumentWorkspace_SaveOptionsChanged;
  508. }
  509. SetTitleText();
  510. }
  511. private void DocumentWorkspace_SaveOptionsChanged(object sender, EventArgs e)
  512. {
  513. SetTitleText();
  514. }
  515. private void FloaterOpacityTimer_Tick(object sender, System.EventArgs e)
  516. {
  517. if (this.WindowState == FormWindowState.Minimized ||
  518. this.floaters == null ||
  519. !PdnBaseForm.EnableOpacity ||
  520. this.appWorkspace.ActiveDocumentWorkspace == null)
  521. {
  522. return;
  523. }
  524. // Here's the behavior we want for our floaters:
  525. // 1. If the mouse is within a floaters rectangle, it should transition to fully opaque
  526. // 2. If the mouse is outside the floater's rectangle, it should transition to partially
  527. // opaque
  528. // 3. However, if the floater is outside where the document is visible on screen, it
  529. // should always be fully opaque.
  530. Rectangle screenDocRect;
  531. try
  532. {
  533. screenDocRect = this.appWorkspace.ActiveDocumentWorkspace.VisibleDocumentBounds;
  534. }
  535. catch (ObjectDisposedException)
  536. {
  537. return; // do nothing, we are probably in the process of shutting down the app
  538. }
  539. for (int i = 0; i < floaters.Length; ++i)
  540. {
  541. FloatingToolForm ftf = floaters[i];
  542. Rectangle intersect = Rectangle.Intersect(screenDocRect, ftf.Bounds);
  543. double opacity = -1.0;
  544. try
  545. {
  546. if (intersect.Width == 0 ||
  547. intersect.Height == 0 ||
  548. (ftf.Bounds.Contains(Control.MousePosition) &&
  549. !appWorkspace.ActiveDocumentWorkspace.IsMouseCaptured()) ||
  550. Utility.DoesControlHaveMouseCaptured(ftf))
  551. {
  552. opacity = Math.Min(1.0, ftf.Opacity + 0.125);
  553. }
  554. else
  555. {
  556. opacity = Math.Max(0.75, ftf.Opacity - 0.0625);
  557. }
  558. if (opacity != ftf.Opacity)
  559. {
  560. ftf.Opacity = opacity;
  561. }
  562. }
  563. catch (System.ComponentModel.Win32Exception)
  564. {
  565. // We just eat the exception. Chris Strahl was having some problem where opacity was 0.7
  566. // and we were trying to set it to 0.7 and it said "the parameter is incorrect"
  567. // ... which is stupid. Bad NVIDIA drivers for his GeForce Go?
  568. }
  569. }
  570. }
  571. protected override void OnLoad(EventArgs e)
  572. {
  573. EnsureFormIsOnScreen();
  574. if (killAfterInit)
  575. {
  576. Application.Exit();
  577. }
  578. this.floaters = new FloatingToolForm[] {
  579. appWorkspace.Widgets.RuleListForm,
  580. //appWorkspace.Widgets.PixelTrackingDialog,
  581. //appWorkspace.Widgets.RunningDialog,
  582. //appWorkspace.Widgets.HistogramDialog,
  583. //appWorkspace.Widgets.ScratchTreatmentDialog,
  584. //appWorkspace.Widgets.SmudgeTreatmentDialog,
  585. //appWorkspace.Widgets.LabelListDialog,
  586. //appWorkspace.Widgets.WorkFlowDialog,
  587. //appWorkspace.Widgets.MeasureListDialog,
  588. //appWorkspace.Widgets.OpticalDensityDialog,
  589. //appWorkspace.Widgets.ProjectEngineering
  590. };
  591. foreach (FloatingToolForm ftf in floaters)
  592. {
  593. ftf.Closing += this.HideInsteadOfCloseHandler;
  594. }
  595. PositionFloatingForms();
  596. base.OnLoad(e);
  597. }
  598. protected override void OnResize(EventArgs e)
  599. {
  600. if (floaterOpacityTimer != null)
  601. {
  602. if (WindowState == FormWindowState.Minimized)
  603. {
  604. if (this.floaterOpacityTimer.Enabled)
  605. {
  606. this.floaterOpacityTimer.Enabled = false;
  607. }
  608. }
  609. else
  610. {
  611. if (!this.floaterOpacityTimer.Enabled)
  612. {
  613. this.floaterOpacityTimer.Enabled = true;
  614. }
  615. this.FloaterOpacityTimer_Tick(this, EventArgs.Empty);
  616. }
  617. }
  618. base.OnResize(e);
  619. }
  620. private void DocumentWorkspace_DocumentChanged(object sender, System.EventArgs e)
  621. {
  622. SetTitleText();
  623. OnResize(EventArgs.Empty);
  624. }
  625. private void SetTitleText()
  626. {
  627. if (this.appWorkspace == null)
  628. {
  629. return;
  630. }
  631. this.Text = PdnInfo.GetAppName();
  632. }
  633. private void OnMenuDropDownClosed(object sender, System.EventArgs e)
  634. {
  635. ToolStripMenuItem menu = (ToolStripMenuItem)sender;
  636. foreach (ToolStripItem tsi in menu.DropDownItems)
  637. {
  638. tsi.Enabled = true;
  639. }
  640. }
  641. private void HideInsteadOfCloseHandler(object sender, CancelEventArgs e)
  642. {
  643. e.Cancel = true;
  644. ((Form)sender).Hide();
  645. }
  646. protected override void OnDragEnter(DragEventArgs drgevent)
  647. {
  648. if (Enabled && drgevent.Data.GetDataPresent(DataFormats.FileDrop))
  649. {
  650. string[] files = (string[])drgevent.Data.GetData(DataFormats.FileDrop);
  651. foreach (string file in files)
  652. {
  653. try
  654. {
  655. FileAttributes fa = System.IO.File.GetAttributes(file);
  656. if ((fa & FileAttributes.Directory) == 0)
  657. {
  658. drgevent.Effect = DragDropEffects.Copy;
  659. }
  660. }
  661. catch
  662. {
  663. }
  664. }
  665. }
  666. base.OnDragEnter(drgevent);
  667. }
  668. private string[] PruneDirectories(string[] fileNames)
  669. {
  670. List<string> result = new List<string>();
  671. foreach (string fileName in fileNames)
  672. {
  673. try
  674. {
  675. FileAttributes fa = System.IO.File.GetAttributes(fileName);
  676. if ((fa & FileAttributes.Directory) == 0)
  677. {
  678. result.Add(fileName);
  679. }
  680. }
  681. catch
  682. {
  683. }
  684. }
  685. return result.ToArray();
  686. }
  687. protected override void OnDragDrop(DragEventArgs drgevent)
  688. {
  689. Activate();
  690. if (!IsCurrentModalForm || !Enabled)
  691. {
  692. // do nothing
  693. }
  694. else if (drgevent.Data.GetDataPresent(DataFormats.FileDrop))
  695. {
  696. string[] allFiles = (string[])drgevent.Data.GetData(DataFormats.FileDrop);
  697. if (allFiles == null)
  698. {
  699. return;
  700. }
  701. string[] files = PruneDirectories(allFiles);
  702. if (files.Length == 0)
  703. {
  704. return;
  705. }
  706. this.appWorkspace.OpenFilesInNewWorkspace(files);
  707. }
  708. base.OnDragDrop(drgevent);
  709. }
  710. private void DocumentWorkspace_ScaleFactorChanged(object sender, EventArgs e)
  711. {
  712. SetTitleText();
  713. }
  714. protected override void OnSizeChanged(EventArgs e)
  715. {
  716. base.OnSizeChanged(e);
  717. SetTitleText();
  718. }
  719. public void DeferredInitialization(object sender, EventArgs e)
  720. {
  721. this.deferredInitializationTimer.Enabled = false;
  722. this.deferredInitializationTimer.Tick -= new EventHandler(DeferredInitialization);
  723. this.deferredInitializationTimer.Dispose();
  724. this.deferredInitializationTimer = null;
  725. }
  726. /*protected override void OnHelpRequested(HelpEventArgs hevent)
  727. {
  728. // F1 is already handled by the Menu->Help menu item. No need to process it twice.
  729. hevent.Handled = true;
  730. base.OnHelpRequested(hevent);
  731. }*/
  732. }
  733. }