PageRenderTime 71ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/src/AppWorkspace.cs

https://bitbucket.org/tcz001/openpdn
C# | 2447 lines | 1971 code | 392 blank | 84 comment | 288 complexity | b9bd44fe47e89855f6c5b93526aa35c6 MD5 | raw file
Possible License(s): Unlicense
  1. /////////////////////////////////////////////////////////////////////////////////
  2. // Paint.NET //
  3. // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
  4. // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
  5. // See src/Resources/Files/License.txt for full licensing and attribution //
  6. // details. //
  7. // . //
  8. /////////////////////////////////////////////////////////////////////////////////
  9. using PaintDotNet.Actions;
  10. using PaintDotNet.Effects;
  11. using PaintDotNet.HistoryFunctions;
  12. using PaintDotNet.HistoryMementos;
  13. using PaintDotNet.SystemLayer;
  14. using PaintDotNet.Tools;
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Diagnostics;
  20. using System.Drawing;
  21. using System.Drawing.Drawing2D;
  22. using System.Drawing.Imaging;
  23. using System.Globalization;
  24. using System.IO;
  25. using System.Reflection;
  26. using System.Runtime.Serialization;
  27. using System.Runtime.Serialization.Formatters.Binary;
  28. using System.Security;
  29. using System.Text;
  30. using System.Windows.Forms;
  31. namespace PaintDotNet
  32. {
  33. internal class AppWorkspace
  34. : UserControl,
  35. ISnapObstacleHost
  36. {
  37. private readonly string cursorInfoStatusBarFormat = PdnResources.GetString("StatusBar.CursorInfo.Format");
  38. private readonly string imageInfoStatusBarFormat = PdnResources.GetString("StatusBar.Size.Format");
  39. private Type defaultToolTypeChoice;
  40. private Type globalToolTypeChoice = null;
  41. private bool globalRulersChoice = false;
  42. private AppEnvironment appEnvironment;
  43. private DocumentWorkspace activeDocumentWorkspace;
  44. // if a new workspace is added, and this workspace is not dirty, then it will be removed.
  45. // This keeps track of the last workspace added via CreateBlankDocumentInNewWorkspace (if
  46. // true was passed for its 2nd parameter)
  47. private DocumentWorkspace initialWorkspace;
  48. private List<DocumentWorkspace> documentWorkspaces = new List<DocumentWorkspace>();
  49. private WorkspaceWidgets widgets;
  50. private Panel workspacePanel;
  51. private PdnToolBar toolBar;
  52. private PdnStatusBar statusBar;
  53. private ToolsForm mainToolBarForm;
  54. private LayerForm layerForm;
  55. private HistoryForm historyForm;
  56. private ColorsForm colorsForm;
  57. private MostRecentFiles mostRecentFiles = null;
  58. private const int defaultMostRecentFilesMax = 8;
  59. private SnapObstacleController snapObstacle;
  60. private bool addedToSnapManager = false;
  61. private int ignoreUpdateSnapObstacle = 0;
  62. private int suspendThumbnailUpdates = 0;
  63. public void CheckForUpdates()
  64. {
  65. this.toolBar.MainMenu.CheckForUpdates();
  66. }
  67. public IDisposable SuspendThumbnailUpdates()
  68. {
  69. CallbackOnDispose resumeFn = new CallbackOnDispose(ResumeThumbnailUpdates);
  70. ++this.suspendThumbnailUpdates;
  71. if (this.suspendThumbnailUpdates == 1)
  72. {
  73. Widgets.DocumentStrip.SuspendThumbnailUpdates();
  74. Widgets.LayerControl.SuspendLayerPreviewUpdates();
  75. }
  76. return resumeFn;
  77. }
  78. private void ResumeThumbnailUpdates()
  79. {
  80. --this.suspendThumbnailUpdates;
  81. if (this.suspendThumbnailUpdates == 0)
  82. {
  83. Widgets.DocumentStrip.ResumeThumbnailUpdates();
  84. Widgets.LayerControl.ResumeLayerPreviewUpdates();
  85. }
  86. }
  87. public Type DefaultToolType
  88. {
  89. get
  90. {
  91. return this.defaultToolTypeChoice;
  92. }
  93. set
  94. {
  95. this.defaultToolTypeChoice = value;
  96. Settings.CurrentUser.SetString(SettingNames.DefaultToolTypeName, value.Name);
  97. }
  98. }
  99. public Type GlobalToolTypeChoice
  100. {
  101. get
  102. {
  103. return this.globalToolTypeChoice;
  104. }
  105. set
  106. {
  107. this.globalToolTypeChoice = value;
  108. if (ActiveDocumentWorkspace != null)
  109. {
  110. ActiveDocumentWorkspace.SetToolFromType(value);
  111. }
  112. }
  113. }
  114. public DocumentWorkspace InitialWorkspace
  115. {
  116. set
  117. {
  118. this.initialWorkspace = value;
  119. }
  120. }
  121. public event EventHandler RulersEnabledChanged;
  122. protected virtual void OnRulersEnabledChanged()
  123. {
  124. if (RulersEnabledChanged != null)
  125. {
  126. RulersEnabledChanged(this, EventArgs.Empty);
  127. }
  128. }
  129. public bool RulersEnabled
  130. {
  131. get
  132. {
  133. return this.globalRulersChoice;
  134. }
  135. set
  136. {
  137. if (this.globalRulersChoice != value)
  138. {
  139. this.globalRulersChoice = value;
  140. if (ActiveDocumentWorkspace != null)
  141. {
  142. ActiveDocumentWorkspace.RulersEnabled = value;
  143. }
  144. OnRulersEnabledChanged();
  145. }
  146. }
  147. }
  148. private void DocumentWorkspace_DrawGridChanged(object sender, EventArgs e)
  149. {
  150. DrawGrid = this.activeDocumentWorkspace.DrawGrid;
  151. }
  152. private void ViewConfigStrip_DrawGridChanged(object sender, EventArgs e)
  153. {
  154. DrawGrid = ((ViewConfigStrip)sender).DrawGrid;
  155. }
  156. private bool DrawGrid
  157. {
  158. get
  159. {
  160. return this.Widgets.ViewConfigStrip.DrawGrid;
  161. }
  162. set
  163. {
  164. if (this.Widgets.ViewConfigStrip.DrawGrid != value)
  165. {
  166. this.Widgets.ViewConfigStrip.DrawGrid = value;
  167. }
  168. if (this.activeDocumentWorkspace != null && this.activeDocumentWorkspace.DrawGrid != value)
  169. {
  170. this.activeDocumentWorkspace.DrawGrid = value;
  171. }
  172. Settings.CurrentUser.SetBoolean(SettingNames.DrawGrid, this.DrawGrid);
  173. }
  174. }
  175. public event EventHandler UnitsChanged;
  176. protected virtual void OnUnitsChanged()
  177. {
  178. if (UnitsChanged != null)
  179. {
  180. UnitsChanged(this, EventArgs.Empty);
  181. }
  182. }
  183. public MeasurementUnit Units
  184. {
  185. get
  186. {
  187. return this.widgets.ViewConfigStrip.Units;
  188. }
  189. set
  190. {
  191. this.widgets.ViewConfigStrip.Units = value;
  192. }
  193. }
  194. public SnapObstacle SnapObstacle
  195. {
  196. get
  197. {
  198. if (this.snapObstacle == null)
  199. {
  200. // HACK: for some reason retrieving the ClientRectangle can raise a VisibleChanged event
  201. // so we initially pass in Rectangle.Empty for the rectangle bounds
  202. this.snapObstacle = new SnapObstacleController(
  203. this.Name,
  204. Rectangle.Empty,
  205. SnapRegion.Interior,
  206. true);
  207. this.snapObstacle.EnableSave = false;
  208. PdnBaseForm pdbForm = FindForm() as PdnBaseForm;
  209. pdbForm.Moving += new MovingEventHandler(ParentForm_Moving);
  210. pdbForm.Move += new EventHandler(ParentForm_Move);
  211. pdbForm.ResizeEnd += new EventHandler(ParentForm_ResizeEnd);
  212. pdbForm.Layout += new LayoutEventHandler(ParentForm_Layout);
  213. pdbForm.SizeChanged += new EventHandler(ParentForm_SizeChanged);
  214. UpdateSnapObstacle();
  215. }
  216. return this.snapObstacle;
  217. }
  218. }
  219. private void ParentForm_Move(object sender, EventArgs e)
  220. {
  221. UpdateSnapObstacle();
  222. }
  223. private void ParentForm_SizeChanged(object sender, EventArgs e)
  224. {
  225. UpdateSnapObstacle();
  226. }
  227. private void ParentForm_Layout(object sender, LayoutEventArgs e)
  228. {
  229. UpdateSnapObstacle();
  230. }
  231. private void ParentForm_ResizeEnd(object sender, EventArgs e)
  232. {
  233. UpdateSnapObstacle();
  234. }
  235. private void ParentForm_Moving(object sender, MovingEventArgs e)
  236. {
  237. UpdateSnapObstacle();
  238. }
  239. private void SuspendUpdateSnapObstacle()
  240. {
  241. ++this.ignoreUpdateSnapObstacle;
  242. }
  243. private void ResumeUpdateSnapObstacle()
  244. {
  245. --this.ignoreUpdateSnapObstacle;
  246. }
  247. private void UpdateSnapObstacle()
  248. {
  249. if (this.ignoreUpdateSnapObstacle > 0)
  250. {
  251. return;
  252. }
  253. if (this.snapObstacle == null)
  254. {
  255. return;
  256. }
  257. if (!this.addedToSnapManager)
  258. {
  259. SnapManager sm = SnapManager.FindMySnapManager(this);
  260. if (sm != null)
  261. {
  262. SnapObstacle so = this.SnapObstacle;
  263. if (!this.addedToSnapManager)
  264. {
  265. sm.AddSnapObstacle(this.SnapObstacle);
  266. this.addedToSnapManager = true;
  267. FindForm().Shown += new EventHandler(AppWorkspace_Shown);
  268. }
  269. }
  270. }
  271. if (this.snapObstacle != null)
  272. {
  273. Rectangle clientRect;
  274. if (ActiveDocumentWorkspace != null)
  275. {
  276. clientRect = ActiveDocumentWorkspace.VisibleViewRectangle;
  277. }
  278. else
  279. {
  280. clientRect = this.workspacePanel.ClientRectangle;
  281. }
  282. Rectangle screenRect = this.workspacePanel.RectangleToScreen(clientRect);
  283. this.snapObstacle.SetBounds(screenRect);
  284. this.snapObstacle.Enabled = this.Visible && this.Enabled;
  285. }
  286. }
  287. private void AppWorkspace_Shown(object sender, EventArgs e)
  288. {
  289. UpdateSnapObstacle();
  290. }
  291. protected override void OnLayout(LayoutEventArgs levent)
  292. {
  293. UpdateSnapObstacle();
  294. base.OnLayout(levent);
  295. }
  296. protected override void OnLocationChanged(EventArgs e)
  297. {
  298. UpdateSnapObstacle();
  299. base.OnLocationChanged(e);
  300. }
  301. protected override void OnSizeChanged(EventArgs e)
  302. {
  303. UpdateSnapObstacle();
  304. base.OnSizeChanged(e);
  305. }
  306. protected override void OnEnabledChanged(EventArgs e)
  307. {
  308. UpdateSnapObstacle();
  309. base.OnEnabledChanged(e);
  310. }
  311. protected override void OnVisibleChanged(EventArgs e)
  312. {
  313. UpdateSnapObstacle();
  314. base.OnVisibleChanged(e);
  315. }
  316. public void ResetFloatingForms()
  317. {
  318. ResetFloatingForm(Widgets.ToolsForm);
  319. ResetFloatingForm(Widgets.HistoryForm);
  320. ResetFloatingForm(Widgets.LayerForm);
  321. ResetFloatingForm(Widgets.ColorsForm);
  322. }
  323. public void ResetFloatingForm(FloatingToolForm ftf)
  324. {
  325. SnapManager sm = SnapManager.FindMySnapManager(this);
  326. if (ftf == Widgets.ToolsForm)
  327. {
  328. sm.ParkObstacle(Widgets.ToolsForm, this, HorizontalSnapEdge.Top, VerticalSnapEdge.Left);
  329. }
  330. else if (ftf == Widgets.HistoryForm)
  331. {
  332. sm.ParkObstacle(Widgets.HistoryForm, this, HorizontalSnapEdge.Top, VerticalSnapEdge.Right);
  333. }
  334. else if (ftf == Widgets.LayerForm)
  335. {
  336. sm.ParkObstacle(Widgets.LayerForm, this, HorizontalSnapEdge.Bottom, VerticalSnapEdge.Right);
  337. }
  338. else if (ftf == Widgets.ColorsForm)
  339. {
  340. sm.ParkObstacle(Widgets.ColorsForm, this, HorizontalSnapEdge.Bottom, VerticalSnapEdge.Left);
  341. }
  342. else
  343. {
  344. throw new ArgumentException();
  345. }
  346. }
  347. private Set<Triple<Assembly, Type, Exception>> effectLoadErrors = new Set<Triple<Assembly, Type, Exception>>();
  348. public void ReportEffectLoadError(Triple<Assembly, Type, Exception> error)
  349. {
  350. lock (this.effectLoadErrors)
  351. {
  352. if (!this.effectLoadErrors.Contains(error))
  353. {
  354. this.effectLoadErrors.Add(error);
  355. }
  356. }
  357. }
  358. public static string GetLocalizedEffectErrorMessage(Assembly assembly, Type type, Exception exception)
  359. {
  360. IPluginSupportInfo supportInfo;
  361. string typeName;
  362. if (type != null)
  363. {
  364. typeName = type.FullName;
  365. supportInfo = PluginSupportInfo.GetPluginSupportInfo(type);
  366. }
  367. else if (exception is TypeLoadException)
  368. {
  369. TypeLoadException asTlex = exception as TypeLoadException;
  370. typeName = asTlex.TypeName;
  371. supportInfo = PluginSupportInfo.GetPluginSupportInfo(assembly);
  372. }
  373. else
  374. {
  375. supportInfo = PluginSupportInfo.GetPluginSupportInfo(assembly);
  376. typeName = null;
  377. }
  378. return GetLocalizedEffectErrorMessage(assembly, typeName, supportInfo, exception);
  379. }
  380. public static string GetLocalizedEffectErrorMessage(Assembly assembly, string typeName, Exception exception)
  381. {
  382. IPluginSupportInfo supportInfo = PluginSupportInfo.GetPluginSupportInfo(assembly);
  383. return GetLocalizedEffectErrorMessage(assembly, typeName, supportInfo, exception);
  384. }
  385. private static string GetLocalizedEffectErrorMessage(Assembly assembly, string typeName, IPluginSupportInfo supportInfo, Exception exception)
  386. {
  387. string fileName = assembly.Location;
  388. string shortErrorFormat = PdnResources.GetString("EffectErrorMessage.ShortFormat");
  389. string fullErrorFormat = PdnResources.GetString("EffectErrorMessage.FullFormat");
  390. string notSuppliedText = PdnResources.GetString("EffectErrorMessage.InfoNotSupplied");
  391. string errorText;
  392. if (supportInfo == null)
  393. {
  394. errorText = string.Format(
  395. shortErrorFormat,
  396. fileName ?? notSuppliedText,
  397. typeName ?? notSuppliedText,
  398. exception.ToString());
  399. }
  400. else
  401. {
  402. errorText = string.Format(
  403. fullErrorFormat,
  404. fileName ?? notSuppliedText,
  405. typeName ?? supportInfo.DisplayName ?? notSuppliedText,
  406. (supportInfo.Version ?? new Version()).ToString(),
  407. supportInfo.Author ?? notSuppliedText,
  408. supportInfo.Copyright ?? notSuppliedText,
  409. (supportInfo.WebsiteUri == null ? notSuppliedText : supportInfo.WebsiteUri.ToString()),
  410. exception.ToString());
  411. }
  412. return errorText;
  413. }
  414. public IList<Triple<Assembly, Type, Exception>> GetEffectLoadErrors()
  415. {
  416. return this.effectLoadErrors.ToArray();
  417. }
  418. public void RunEffect(Type effectType)
  419. {
  420. // TODO: this is kind of a hack
  421. this.toolBar.MainMenu.RunEffect(effectType);
  422. }
  423. public PdnToolBar ToolBar
  424. {
  425. get
  426. {
  427. return this.toolBar;
  428. }
  429. }
  430. private ImageResource FileNewIcon
  431. {
  432. get
  433. {
  434. return PdnResources.GetImageResource("Icons.MenuFileNewIcon.png");
  435. }
  436. }
  437. private ImageResource ImageFromDiskIcon
  438. {
  439. get
  440. {
  441. return PdnResources.GetImageResource("Icons.ImageFromDiskIcon.png");
  442. }
  443. }
  444. public MostRecentFiles MostRecentFiles
  445. {
  446. get
  447. {
  448. if (this.mostRecentFiles == null)
  449. {
  450. this.mostRecentFiles = new MostRecentFiles(defaultMostRecentFilesMax);
  451. }
  452. return this.mostRecentFiles;
  453. }
  454. }
  455. private void DocumentWorkspace_DocumentChanging(object sender, EventArgs<Document> e)
  456. {
  457. UI.SuspendControlPainting(this);
  458. }
  459. private void DocumentWorkspace_DocumentChanged(object sender, EventArgs e)
  460. {
  461. UpdateDocInfoInStatusBar();
  462. UI.ResumeControlPainting(this);
  463. Invalidate(true);
  464. }
  465. private void CoordinatesToStrings(int x, int y, out string xString, out string yString, out string unitsString)
  466. {
  467. this.activeDocumentWorkspace.Document.CoordinatesToStrings(this.Units, x, y, out xString, out yString, out unitsString);
  468. }
  469. private void UpdateCursorInfoInStatusBar(int cursorX, int cursorY)
  470. {
  471. SuspendLayout();
  472. if (this.activeDocumentWorkspace == null ||
  473. this.activeDocumentWorkspace.Document == null)
  474. {
  475. this.statusBar.CursorInfoText = string.Empty;
  476. }
  477. else
  478. {
  479. string xString;
  480. string yString;
  481. string units;
  482. CoordinatesToStrings(cursorX, cursorY, out xString, out yString, out units);
  483. string cursorText = string.Format(
  484. CultureInfo.InvariantCulture,
  485. this.cursorInfoStatusBarFormat,
  486. xString,
  487. units,
  488. yString,
  489. units);
  490. this.statusBar.CursorInfoText = cursorText;
  491. }
  492. ResumeLayout(false);
  493. }
  494. private void UpdateDocInfoInStatusBar()
  495. {
  496. if (this.activeDocumentWorkspace == null ||
  497. this.activeDocumentWorkspace.Document == null)
  498. {
  499. this.statusBar.ImageInfoStatusText = string.Empty;
  500. }
  501. else if (this.activeDocumentWorkspace != null &&
  502. this.activeDocumentWorkspace.Document != null)
  503. {
  504. string widthString;
  505. string heightString;
  506. string units;
  507. CoordinatesToStrings(
  508. this.activeDocumentWorkspace.Document.Width,
  509. this.activeDocumentWorkspace.Document.Height,
  510. out widthString,
  511. out heightString,
  512. out units);
  513. string imageText = string.Format(
  514. CultureInfo.InvariantCulture,
  515. this.imageInfoStatusBarFormat,
  516. widthString,
  517. units,
  518. heightString,
  519. units);
  520. this.statusBar.ImageInfoStatusText = imageText;
  521. }
  522. }
  523. [Browsable(false)]
  524. public WorkspaceWidgets Widgets
  525. {
  526. get
  527. {
  528. return this.widgets;
  529. }
  530. }
  531. [Browsable(false)]
  532. public AppEnvironment AppEnvironment
  533. {
  534. get
  535. {
  536. return this.appEnvironment;
  537. }
  538. }
  539. [Browsable(false)]
  540. public DocumentWorkspace ActiveDocumentWorkspace
  541. {
  542. get
  543. {
  544. return this.activeDocumentWorkspace;
  545. }
  546. set
  547. {
  548. if (value != this.activeDocumentWorkspace)
  549. {
  550. if (value != null &&
  551. this.documentWorkspaces.IndexOf(value) == -1)
  552. {
  553. throw new ArgumentException("DocumentWorkspace was not created with AddNewDocumentWorkspace");
  554. }
  555. bool focused = false;
  556. if (this.activeDocumentWorkspace != null)
  557. {
  558. focused = this.activeDocumentWorkspace.Focused;
  559. }
  560. UI.SuspendControlPainting(this);
  561. OnActiveDocumentWorkspaceChanging();
  562. this.activeDocumentWorkspace = value;
  563. OnActiveDocumentWorkspaceChanged();
  564. UI.ResumeControlPainting(this);
  565. Refresh();
  566. if (value != null)
  567. {
  568. value.Focus();
  569. }
  570. }
  571. }
  572. }
  573. private void ActiveDocumentWorkspace_FirstInputAfterGotFocus(object sender, EventArgs e)
  574. {
  575. this.toolBar.DocumentStrip.EnsureItemFullyVisible(this.toolBar.DocumentStrip.SelectedDocumentIndex);
  576. }
  577. public DocumentWorkspace[] DocumentWorkspaces
  578. {
  579. get
  580. {
  581. return this.documentWorkspaces.ToArray();
  582. }
  583. }
  584. public DocumentWorkspace AddNewDocumentWorkspace()
  585. {
  586. if (this.initialWorkspace != null)
  587. {
  588. if (this.initialWorkspace.Document == null || !this.initialWorkspace.Document.Dirty)
  589. {
  590. this.globalToolTypeChoice = this.initialWorkspace.GetToolType();
  591. RemoveDocumentWorkspace(this.initialWorkspace);
  592. this.initialWorkspace = null;
  593. }
  594. }
  595. DocumentWorkspace dw = new DocumentWorkspace();
  596. dw.AppWorkspace = this;
  597. this.documentWorkspaces.Add(dw);
  598. this.toolBar.DocumentStrip.AddDocumentWorkspace(dw);
  599. return dw;
  600. }
  601. public Image GetDocumentWorkspaceThumbnail(DocumentWorkspace dw)
  602. {
  603. this.toolBar.DocumentStrip.SyncThumbnails();
  604. Image[] images = this.toolBar.DocumentStrip.DocumentThumbnails;
  605. DocumentWorkspace[] documents = this.toolBar.DocumentStrip.DocumentList;
  606. for (int i = 0; i < documents.Length; ++i)
  607. {
  608. if (documents[i] == dw)
  609. {
  610. return images[i];
  611. }
  612. }
  613. throw new ArgumentException("The requested DocumentWorkspace doesn't exist in this AppWorkspace");
  614. }
  615. public void RemoveDocumentWorkspace(DocumentWorkspace documentWorkspace)
  616. {
  617. int dwIndex = this.documentWorkspaces.IndexOf(documentWorkspace);
  618. if (dwIndex == -1)
  619. {
  620. throw new ArgumentException("DocumentWorkspace was not created with AddNewDocumentWorkspace");
  621. }
  622. bool removingCurrentDW;
  623. if (this.ActiveDocumentWorkspace == documentWorkspace)
  624. {
  625. removingCurrentDW = true;
  626. this.globalToolTypeChoice = documentWorkspace.GetToolType();
  627. }
  628. else
  629. {
  630. removingCurrentDW = false;
  631. }
  632. documentWorkspace.SetTool(null);
  633. // Choose new active DW if removing the current DW
  634. if (removingCurrentDW)
  635. {
  636. if (this.documentWorkspaces.Count == 1)
  637. {
  638. this.ActiveDocumentWorkspace = null;
  639. }
  640. else if (dwIndex == 0)
  641. {
  642. this.ActiveDocumentWorkspace = this.documentWorkspaces[1];
  643. }
  644. else
  645. {
  646. this.ActiveDocumentWorkspace = this.documentWorkspaces[dwIndex - 1];
  647. }
  648. }
  649. this.documentWorkspaces.Remove(documentWorkspace);
  650. this.toolBar.DocumentStrip.RemoveDocumentWorkspace(documentWorkspace);
  651. if (this.initialWorkspace == documentWorkspace)
  652. {
  653. this.initialWorkspace = null;
  654. }
  655. // Clean up the DocumentWorkspace
  656. Document document = documentWorkspace.Document;
  657. documentWorkspace.Document = null;
  658. document.Dispose();
  659. documentWorkspace.Dispose();
  660. documentWorkspace = null;
  661. }
  662. private void UpdateHistoryButtons()
  663. {
  664. if (ActiveDocumentWorkspace == null)
  665. {
  666. widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Undo, false);
  667. widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Redo, false);
  668. }
  669. else
  670. {
  671. if (ActiveDocumentWorkspace.History.UndoStack.Count > 1)
  672. {
  673. widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Undo, true);
  674. }
  675. else
  676. {
  677. widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Undo, false);
  678. }
  679. if (ActiveDocumentWorkspace.History.RedoStack.Count > 0)
  680. {
  681. widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Redo, true);
  682. }
  683. else
  684. {
  685. widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Redo, false);
  686. }
  687. }
  688. }
  689. private void HistoryChangedHandler(object sender, EventArgs e)
  690. {
  691. UpdateHistoryButtons();
  692. // some actions change the document size: make sure we update our status bar panel
  693. // TODO: shouldn't this be handled by our DocumentWorkspace.DocumentChanged handler...?
  694. UpdateDocInfoInStatusBar();
  695. }
  696. public event EventHandler ActiveDocumentWorkspaceChanging;
  697. protected virtual void OnActiveDocumentWorkspaceChanging()
  698. {
  699. SuspendUpdateSnapObstacle();
  700. if (ActiveDocumentWorkspaceChanging != null)
  701. {
  702. ActiveDocumentWorkspaceChanging(this, EventArgs.Empty);
  703. }
  704. if (this.activeDocumentWorkspace != null)
  705. {
  706. this.activeDocumentWorkspace.FirstInputAfterGotFocus +=
  707. ActiveDocumentWorkspace_FirstInputAfterGotFocus;
  708. this.activeDocumentWorkspace.RulersEnabledChanged -= this.DocumentWorkspace_RulersEnabledChanged;
  709. this.activeDocumentWorkspace.DocumentMouseEnter -= this.DocumentMouseEnterHandler;
  710. this.activeDocumentWorkspace.DocumentMouseLeave -= this.DocumentMouseLeaveHandler;
  711. this.activeDocumentWorkspace.DocumentMouseMove -= this.DocumentMouseMoveHandler;
  712. this.activeDocumentWorkspace.DocumentMouseDown -= this.DocumentMouseDownHandler;
  713. this.activeDocumentWorkspace.Scroll -= this.DocumentWorkspace_Scroll;
  714. this.activeDocumentWorkspace.Layout -= this.DocumentWorkspace_Layout;
  715. this.activeDocumentWorkspace.DrawGridChanged -= this.DocumentWorkspace_DrawGridChanged;
  716. this.activeDocumentWorkspace.DocumentClick -= this.DocumentClick;
  717. this.activeDocumentWorkspace.DocumentMouseUp -= this.DocumentMouseUpHandler;
  718. this.activeDocumentWorkspace.DocumentKeyPress -= this.DocumentKeyPress;
  719. this.activeDocumentWorkspace.DocumentKeyUp -= this.DocumenKeyUp;
  720. this.activeDocumentWorkspace.DocumentKeyDown -= this.DocumentKeyDown;
  721. this.activeDocumentWorkspace.History.Changed -= HistoryChangedHandler;
  722. this.activeDocumentWorkspace.StatusChanged -= OnDocumentWorkspaceStatusChanged;
  723. this.activeDocumentWorkspace.DocumentChanging -= DocumentWorkspace_DocumentChanging;
  724. this.activeDocumentWorkspace.DocumentChanged -= DocumentWorkspace_DocumentChanged;
  725. this.activeDocumentWorkspace.Selection.Changing -= SelectedPathChangingHandler;
  726. this.activeDocumentWorkspace.Selection.Changed -= SelectedPathChangedHandler;
  727. this.activeDocumentWorkspace.ScaleFactorChanged -= ZoomChangedHandler;
  728. this.activeDocumentWorkspace.ZoomBasisChanged -= DocumentWorkspace_ZoomBasisChanged;
  729. this.activeDocumentWorkspace.Visible = false;
  730. this.historyForm.HistoryControl.HistoryStack = null;
  731. this.activeDocumentWorkspace.ToolChanging -= this.ToolChangingHandler;
  732. this.activeDocumentWorkspace.ToolChanged -= this.ToolChangedHandler;
  733. if (this.activeDocumentWorkspace.Tool != null)
  734. {
  735. while (this.activeDocumentWorkspace.Tool.IsMouseEntered)
  736. {
  737. this.activeDocumentWorkspace.Tool.PerformMouseLeave();
  738. }
  739. }
  740. Type toolType = this.activeDocumentWorkspace.GetToolType();
  741. if (toolType != null)
  742. {
  743. this.globalToolTypeChoice = this.activeDocumentWorkspace.GetToolType();
  744. }
  745. }
  746. ResumeUpdateSnapObstacle();
  747. UpdateSnapObstacle();
  748. }
  749. public event EventHandler ActiveDocumentWorkspaceChanged;
  750. protected virtual void OnActiveDocumentWorkspaceChanged()
  751. {
  752. SuspendUpdateSnapObstacle();
  753. if (this.activeDocumentWorkspace == null)
  754. {
  755. this.toolBar.CommonActionsStrip.SetButtonEnabled(CommonAction.Print, false);
  756. this.toolBar.CommonActionsStrip.SetButtonEnabled(CommonAction.Save, false);
  757. }
  758. else
  759. {
  760. this.activeDocumentWorkspace.SuspendLayout();
  761. this.toolBar.CommonActionsStrip.SetButtonEnabled(CommonAction.Print, true);
  762. this.toolBar.CommonActionsStrip.SetButtonEnabled(CommonAction.Save, true);
  763. this.activeDocumentWorkspace.BackColor = System.Drawing.SystemColors.ControlDark;
  764. this.activeDocumentWorkspace.Dock = System.Windows.Forms.DockStyle.Fill;
  765. this.activeDocumentWorkspace.DrawGrid = this.DrawGrid;
  766. this.activeDocumentWorkspace.PanelAutoScroll = true;
  767. this.activeDocumentWorkspace.RulersEnabled = this.globalRulersChoice;
  768. this.activeDocumentWorkspace.TabIndex = 0;
  769. this.activeDocumentWorkspace.TabStop = false;
  770. this.activeDocumentWorkspace.RulersEnabledChanged += this.DocumentWorkspace_RulersEnabledChanged;
  771. this.activeDocumentWorkspace.DocumentMouseEnter += this.DocumentMouseEnterHandler;
  772. this.activeDocumentWorkspace.DocumentMouseLeave += this.DocumentMouseLeaveHandler;
  773. this.activeDocumentWorkspace.DocumentMouseMove += this.DocumentMouseMoveHandler;
  774. this.activeDocumentWorkspace.DocumentMouseDown += this.DocumentMouseDownHandler;
  775. this.activeDocumentWorkspace.Scroll += this.DocumentWorkspace_Scroll;
  776. this.activeDocumentWorkspace.DrawGridChanged += this.DocumentWorkspace_DrawGridChanged;
  777. this.activeDocumentWorkspace.DocumentClick += this.DocumentClick;
  778. this.activeDocumentWorkspace.DocumentMouseUp += this.DocumentMouseUpHandler;
  779. this.activeDocumentWorkspace.DocumentKeyPress += this.DocumentKeyPress;
  780. this.activeDocumentWorkspace.DocumentKeyUp += this.DocumenKeyUp;
  781. this.activeDocumentWorkspace.DocumentKeyDown += this.DocumentKeyDown;
  782. if (this.workspacePanel.Controls.Contains(this.activeDocumentWorkspace))
  783. {
  784. this.activeDocumentWorkspace.Visible = true;
  785. }
  786. else
  787. {
  788. this.activeDocumentWorkspace.Dock = DockStyle.Fill;
  789. this.workspacePanel.Controls.Add(this.activeDocumentWorkspace);
  790. }
  791. this.activeDocumentWorkspace.Layout += this.DocumentWorkspace_Layout;
  792. this.toolBar.ViewConfigStrip.ScaleFactor = this.activeDocumentWorkspace.ScaleFactor;
  793. this.toolBar.ViewConfigStrip.ZoomBasis = this.activeDocumentWorkspace.ZoomBasis;
  794. this.activeDocumentWorkspace.AppWorkspace = this;
  795. this.activeDocumentWorkspace.History.Changed += HistoryChangedHandler;
  796. this.activeDocumentWorkspace.StatusChanged += OnDocumentWorkspaceStatusChanged;
  797. this.activeDocumentWorkspace.DocumentChanging += DocumentWorkspace_DocumentChanging;
  798. this.activeDocumentWorkspace.DocumentChanged += DocumentWorkspace_DocumentChanged;
  799. this.activeDocumentWorkspace.Selection.Changing += SelectedPathChangingHandler;
  800. this.activeDocumentWorkspace.Selection.Changed += SelectedPathChangedHandler;
  801. this.activeDocumentWorkspace.ScaleFactorChanged += ZoomChangedHandler;
  802. this.activeDocumentWorkspace.ZoomBasisChanged += DocumentWorkspace_ZoomBasisChanged;
  803. this.activeDocumentWorkspace.Units = this.widgets.ViewConfigStrip.Units;
  804. this.historyForm.HistoryControl.HistoryStack = this.ActiveDocumentWorkspace.History;
  805. this.activeDocumentWorkspace.ToolChanging += this.ToolChangingHandler;
  806. this.activeDocumentWorkspace.ToolChanged += this.ToolChangedHandler;
  807. this.toolBar.ViewConfigStrip.RulersEnabled = this.activeDocumentWorkspace.RulersEnabled;
  808. this.toolBar.DocumentStrip.SelectDocumentWorkspace(this.activeDocumentWorkspace);
  809. this.activeDocumentWorkspace.SetToolFromType(this.globalToolTypeChoice);
  810. UpdateSelectionToolbarButtons();
  811. UpdateHistoryButtons();
  812. UpdateDocInfoInStatusBar();
  813. this.activeDocumentWorkspace.ResumeLayout();
  814. this.activeDocumentWorkspace.PerformLayout();
  815. this.activeDocumentWorkspace.FirstInputAfterGotFocus +=
  816. ActiveDocumentWorkspace_FirstInputAfterGotFocus;
  817. }
  818. if (ActiveDocumentWorkspaceChanged != null)
  819. {
  820. ActiveDocumentWorkspaceChanged(this, EventArgs.Empty);
  821. }
  822. UpdateStatusBarContextStatus();
  823. ResumeUpdateSnapObstacle();
  824. UpdateSnapObstacle();
  825. }
  826. public AppWorkspace()
  827. {
  828. SuspendLayout();
  829. // initialize!
  830. InitializeComponent();
  831. InitializeFloatingForms();
  832. this.mainToolBarForm.ToolsControl.SetTools(DocumentWorkspace.ToolInfos);
  833. this.mainToolBarForm.ToolsControl.ToolClicked += new ToolClickedEventHandler(this.MainToolBar_ToolClicked);
  834. this.toolBar.ToolChooserStrip.SetTools(DocumentWorkspace.ToolInfos);
  835. this.toolBar.ToolChooserStrip.ToolClicked += new ToolClickedEventHandler(this.MainToolBar_ToolClicked);
  836. this.toolBar.AppWorkspace = this;
  837. // init the Widgets container
  838. this.widgets = new WorkspaceWidgets(this);
  839. this.widgets.ViewConfigStrip = this.toolBar.ViewConfigStrip;
  840. this.widgets.CommonActionsStrip = this.toolBar.CommonActionsStrip;
  841. this.widgets.ToolConfigStrip = this.toolBar.ToolConfigStrip;
  842. this.widgets.ToolsForm = this.mainToolBarForm;
  843. this.widgets.LayerForm = this.layerForm;
  844. this.widgets.HistoryForm = this.historyForm;
  845. this.widgets.ColorsForm = this.colorsForm;
  846. this.widgets.StatusBarProgress = this.statusBar;
  847. this.widgets.DocumentStrip = this.toolBar.DocumentStrip;
  848. // Load our settings and initialize the AppEnvironment
  849. LoadSettings();
  850. // hook into Environment *Changed events
  851. AppEnvironment.PrimaryColorChanged += PrimaryColorChangedHandler;
  852. AppEnvironment.SecondaryColorChanged += SecondaryColorChangedHandler;
  853. AppEnvironment.ShapeDrawTypeChanged += ShapeDrawTypeChangedHandler;
  854. AppEnvironment.GradientInfoChanged += GradientInfoChangedHandler;
  855. AppEnvironment.ToleranceChanged += OnEnvironmentToleranceChanged;
  856. AppEnvironment.AlphaBlendingChanged += AlphaBlendingChangedHandler;
  857. AppEnvironment.FontInfo = this.toolBar.ToolConfigStrip.FontInfo;
  858. AppEnvironment.TextAlignment = this.toolBar.ToolConfigStrip.FontAlignment;
  859. AppEnvironment.AntiAliasingChanged += Environment_AntiAliasingChanged;
  860. AppEnvironment.FontInfoChanged += Environment_FontInfoChanged;
  861. AppEnvironment.FontSmoothingChanged += Environment_FontSmoothingChanged;
  862. AppEnvironment.TextAlignmentChanged += Environment_TextAlignmentChanged;
  863. AppEnvironment.PenInfoChanged += Environment_PenInfoChanged;
  864. AppEnvironment.BrushInfoChanged += Environment_BrushInfoChanged;
  865. AppEnvironment.ColorPickerClickBehaviorChanged += Environment_ColorPickerClickBehaviorChanged;
  866. AppEnvironment.ResamplingAlgorithmChanged += Environment_ResamplingAlgorithmChanged;
  867. AppEnvironment.SelectionCombineModeChanged += Environment_SelectionCombineModeChanged;
  868. AppEnvironment.FloodModeChanged += Environment_FloodModeChanged;
  869. AppEnvironment.SelectionDrawModeInfoChanged += Environment_SelectionDrawModeInfoChanged;
  870. this.toolBar.DocumentStrip.RelinquishFocus += RelinquishFocusHandler;
  871. this.toolBar.ToolConfigStrip.ToleranceChanged += OnToolBarToleranceChanged;
  872. this.toolBar.ToolConfigStrip.FontAlignmentChanged += ToolConfigStrip_TextAlignmentChanged;
  873. this.toolBar.ToolConfigStrip.FontInfoChanged += ToolConfigStrip_FontTextChanged;
  874. this.toolBar.ToolConfigStrip.FontSmoothingChanged += ToolConfigStrip_FontSmoothingChanged;
  875. this.toolBar.ToolConfigStrip.RelinquishFocus += RelinquishFocusHandler2;
  876. this.toolBar.CommonActionsStrip.RelinquishFocus += OnToolStripRelinquishFocus;
  877. this.toolBar.CommonActionsStrip.MouseWheel += OnToolStripMouseWheel;
  878. this.toolBar.CommonActionsStrip.ButtonClick += CommonActionsStrip_ButtonClick;
  879. this.toolBar.ViewConfigStrip.DrawGridChanged += ViewConfigStrip_DrawGridChanged;
  880. this.toolBar.ViewConfigStrip.RulersEnabledChanged += ViewConfigStrip_RulersEnabledChanged;
  881. this.toolBar.ViewConfigStrip.ZoomBasisChanged += ViewConfigStrip_ZoomBasisChanged;
  882. this.toolBar.ViewConfigStrip.ZoomScaleChanged += ViewConfigStrip_ZoomScaleChanged;
  883. this.toolBar.ViewConfigStrip.ZoomIn += ViewConfigStrip_ZoomIn;
  884. this.toolBar.ViewConfigStrip.ZoomOut += ViewConfigStrip_ZoomOut;
  885. this.toolBar.ViewConfigStrip.UnitsChanged += ViewConfigStrip_UnitsChanged;
  886. this.toolBar.ViewConfigStrip.RelinquishFocus += OnToolStripRelinquishFocus;
  887. this.toolBar.ViewConfigStrip.MouseWheel += OnToolStripMouseWheel;
  888. this.toolBar.ToolConfigStrip.BrushInfoChanged += DrawConfigStrip_BrushChanged;
  889. this.toolBar.ToolConfigStrip.ShapeDrawTypeChanged += DrawConfigStrip_ShapeDrawTypeChanged;
  890. this.toolBar.ToolConfigStrip.PenInfoChanged += DrawConfigStrip_PenChanged;
  891. this.toolBar.ToolConfigStrip.GradientInfoChanged += ToolConfigStrip_GradientInfoChanged;
  892. this.toolBar.ToolConfigStrip.AlphaBlendingChanged += OnDrawConfigStripAlphaBlendingChanged;
  893. this.toolBar.ToolConfigStrip.AntiAliasingChanged += DrawConfigStrip_AntiAliasingChanged;
  894. this.toolBar.ToolConfigStrip.RelinquishFocus += OnToolStripRelinquishFocus;
  895. this.toolBar.ToolConfigStrip.ColorPickerClickBehaviorChanged += ToolConfigStrip_ColorPickerClickBehaviorChanged;
  896. this.toolBar.ToolConfigStrip.ResamplingAlgorithmChanged += ToolConfigStrip_ResamplingAlgorithmChanged;
  897. this.toolBar.ToolConfigStrip.SelectionCombineModeChanged += ToolConfigStrip_SelectionCombineModeChanged;
  898. this.toolBar.ToolConfigStrip.FloodModeChanged += ToolConfigStrip_FloodModeChanged;
  899. this.toolBar.ToolConfigStrip.SelectionDrawModeInfoChanged += ToolConfigStrip_SelectionDrawModeInfoChanged;
  900. this.toolBar.ToolConfigStrip.SelectionDrawModeUnitsChanging += ToolConfigStrip_SelectionDrawModeUnitsChanging;
  901. this.toolBar.ToolConfigStrip.MouseWheel += OnToolStripMouseWheel;
  902. this.toolBar.DocumentStrip.RelinquishFocus += OnToolStripRelinquishFocus;
  903. this.toolBar.DocumentStrip.DocumentClicked += DocumentStrip_DocumentTabClicked;
  904. this.toolBar.DocumentStrip.DocumentListChanged += DocumentStrip_DocumentListChanged;
  905. // Synchronize
  906. AppEnvironment.PerformAllChanged();
  907. this.globalToolTypeChoice = this.defaultToolTypeChoice;
  908. this.toolBar.ToolConfigStrip.ToolBarConfigItems = ToolBarConfigItems.None;
  909. this.layerForm.LayerControl.AppWorkspace = this;
  910. ResumeLayout();
  911. PerformLayout();
  912. }
  913. private void ToolConfigStrip_ColorPickerClickBehaviorChanged(object sender, EventArgs e)
  914. {
  915. this.appEnvironment.ColorPickerClickBehavior = this.widgets.ToolConfigStrip.ColorPickerClickBehavior;
  916. }
  917. private void Environment_ColorPickerClickBehaviorChanged(object sender, EventArgs e)
  918. {
  919. this.widgets.ToolConfigStrip.ColorPickerClickBehavior = this.appEnvironment.ColorPickerClickBehavior;
  920. }
  921. private void ToolConfigStrip_ResamplingAlgorithmChanged(object sender, EventArgs e)
  922. {
  923. this.appEnvironment.ResamplingAlgorithm = this.widgets.ToolConfigStrip.ResamplingAlgorithm;
  924. }
  925. private void Environment_ResamplingAlgorithmChanged(object sender, EventArgs e)
  926. {
  927. this.widgets.ToolConfigStrip.ResamplingAlgorithm = this.appEnvironment.ResamplingAlgorithm;
  928. }
  929. private void ToolConfigStrip_SelectionCombineModeChanged(object sender, EventArgs e)
  930. {
  931. this.appEnvironment.SelectionCombineMode = this.widgets.ToolConfigStrip.SelectionCombineMode;
  932. }
  933. private void Environment_SelectionCombineModeChanged(object sender, EventArgs e)
  934. {
  935. this.widgets.ToolConfigStrip.SelectionCombineMode = this.appEnvironment.SelectionCombineMode;
  936. }
  937. private void ToolConfigStrip_FloodModeChanged(object sender, EventArgs e)
  938. {
  939. this.appEnvironment.FloodMode = this.widgets.ToolConfigStrip.FloodMode;
  940. }
  941. private void Environment_FloodModeChanged(object sender, EventArgs e)
  942. {
  943. this.widgets.ToolConfigStrip.FloodMode = this.appEnvironment.FloodMode;
  944. }
  945. private void ToolConfigStrip_SelectionDrawModeInfoChanged(object sender, EventArgs e)
  946. {
  947. this.appEnvironment.SelectionDrawModeInfo = this.widgets.ToolConfigStrip.SelectionDrawModeInfo;
  948. }
  949. private void Environment_SelectionDrawModeInfoChanged(object sender, EventArgs e)
  950. {
  951. this.widgets.ToolConfigStrip.SelectionDrawModeInfo = this.appEnvironment.SelectionDrawModeInfo;
  952. }
  953. private sealed class ToolConfigStrip_SelectionDrawModeUnitsChangeHandler
  954. {
  955. private ToolConfigStrip toolConfigStrip;
  956. private Document activeDocument;
  957. private MeasurementUnit oldUnits;
  958. public ToolConfigStrip_SelectionDrawModeUnitsChangeHandler(ToolConfigStrip toolConfigStrip, Document activeDocument)
  959. {
  960. this.toolConfigStrip = toolConfigStrip;
  961. this.activeDocument = activeDocument;
  962. this.oldUnits = toolConfigStrip.SelectionDrawModeInfo.Units;
  963. }
  964. public void Initialize()
  965. {
  966. this.toolConfigStrip.SelectionDrawModeUnitsChanged += ToolConfigStrip_SelectionDrawModeUnitsChanged;
  967. }
  968. public void ToolConfigStrip_SelectionDrawModeUnitsChanged(object sender, EventArgs e)
  969. {
  970. try
  971. {
  972. SelectionDrawModeInfo sdmi = this.toolConfigStrip.SelectionDrawModeInfo;
  973. MeasurementUnit newUnits = sdmi.Units;
  974. double oldWidth = sdmi.Width;
  975. double oldHeight = sdmi.Height;
  976. double newWidth;
  977. double newHeight;
  978. newWidth = Document.ConvertMeasurement(oldWidth, this.oldUnits, this.activeDocument.DpuUnit, this.activeDocument.DpuX, newUnits);
  979. newHeight = Document.ConvertMeasurement(oldHeight, this.oldUnits, this.activeDocument.DpuUnit, this.activeDocument.DpuY, newUnits);
  980. SelectionDrawModeInfo newSdmi = sdmi.CloneWithNewWidthAndHeight(newWidth, newHeight);
  981. this.toolConfigStrip.SelectionDrawModeInfo = newSdmi;
  982. }
  983. finally
  984. {
  985. this.toolConfigStrip.SelectionDrawModeUnitsChanged -= ToolConfigStrip_SelectionDrawModeUnitsChanged;
  986. }
  987. }
  988. }
  989. private void ToolConfigStrip_SelectionDrawModeUnitsChanging(object sender, EventArgs e)
  990. {
  991. if (this.ActiveDocumentWorkspace != null && this.ActiveDocumentWorkspace.Document != null)
  992. {
  993. ToolConfigStrip_SelectionDrawModeUnitsChangeHandler tcsSdmuch = new ToolConfigStrip_SelectionDrawModeUnitsChangeHandler(
  994. this.toolBar.ToolConfigStrip, this.ActiveDocumentWorkspace.Document);
  995. tcsSdmuch.Initialize();
  996. }
  997. }
  998. private void DocumentStrip_DocumentListChanged(object sender, EventArgs e)
  999. {
  1000. bool enableThem = (this.widgets.DocumentStrip.DocumentCount != 0);
  1001. this.widgets.ToolsForm.Enabled = enableThem;
  1002. this.widgets.HistoryForm.Enabled = enableThem;
  1003. this.widgets.LayerForm.Enabled = enableThem;
  1004. this.widgets.ColorsForm.Enabled = enableThem;
  1005. this.widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Paste, enableThem);
  1006. UpdateHistoryButtons();
  1007. UpdateDocInfoInStatusBar();
  1008. UpdateCursorInfoInStatusBar(0, 0);
  1009. }
  1010. public void SaveSettings()
  1011. {
  1012. Settings.CurrentUser.SetBoolean(SettingNames.Rulers, this.globalRulersChoice);
  1013. Settings.CurrentUser.SetBoolean(SettingNames.DrawGrid, this.DrawGrid);
  1014. Settings.CurrentUser.SetString(SettingNames.DefaultToolTypeName, this.defaultToolTypeChoice.Name);
  1015. this.MostRecentFiles.SaveMruList();
  1016. }
  1017. private void LoadDefaultToolType()
  1018. {
  1019. string defaultToolTypeName = Settings.CurrentUser.GetString(SettingNames.DefaultToolTypeName, Tool.DefaultToolType.Name);
  1020. ToolInfo[] tis = DocumentWorkspace.ToolInfos;
  1021. ToolInfo ti = Array.Find(
  1022. tis,
  1023. delegate(ToolInfo check)
  1024. {
  1025. if (string.Compare(defaultToolTypeName, check.ToolType.Name, StringComparison.InvariantCultureIgnoreCase) == 0)
  1026. {
  1027. return true;
  1028. }
  1029. else
  1030. {
  1031. return false;
  1032. }
  1033. });
  1034. if (ti == null)
  1035. {
  1036. this.defaultToolTypeChoice = Tool.DefaultToolType;
  1037. }
  1038. else
  1039. {
  1040. this.defaultToolTypeChoice = ti.ToolType;
  1041. }
  1042. }
  1043. public void LoadSettings()
  1044. {
  1045. try
  1046. {
  1047. LoadDefaultToolType();
  1048. this.globalToolTypeChoice = this.defaultToolTypeChoice;
  1049. this.globalRulersChoice = Settings.CurrentUser.GetBoolean(SettingNames.Rulers, false);
  1050. this.DrawGrid = Settings.CurrentUser.GetBoolean(SettingNames.DrawGrid, false);
  1051. this.appEnvironment = AppEnvironment.GetDefaultAppEnvironment();
  1052. this.widgets.ViewConfigStrip.Units = (MeasurementUnit)Enum.Parse(typeof(MeasurementUnit),
  1053. Settings.CurrentUser.GetString(SettingNames.Units, MeasurementUnit.Pixel.ToString()), true);
  1054. }
  1055. catch (Exception)
  1056. {
  1057. this.appEnvironment = new AppEnvironment();
  1058. this.appEnvironment.SetToDefaults();
  1059. try
  1060. {
  1061. Settings.CurrentUser.Delete(
  1062. new string[]
  1063. {
  1064. SettingNames.Rulers,
  1065. SettingNames.DrawGrid,
  1066. SettingNames.Units,
  1067. SettingNames.DefaultAppEnvironment,
  1068. SettingNames.DefaultToolTypeName,
  1069. });
  1070. }
  1071. catch (Exception)
  1072. {
  1073. }
  1074. }
  1075. try
  1076. {
  1077. this.toolBar.ToolConfigStrip.LoadFromAppEnvironment(this.appEnvironment);
  1078. }
  1079. catch (Exception)
  1080. {
  1081. this.appEnvironment = new AppEnvironment();
  1082. this.appEnvironment.SetToDefaults();
  1083. this.toolBar.ToolConfigStrip.LoadFromAppEnvironment(this.appEnvironment);
  1084. }
  1085. }
  1086. protected override void OnLoad(EventArgs e)
  1087. {
  1088. if (this.ActiveDocumentWorkspace != null)
  1089. {
  1090. this.ActiveDocumentWorkspace.Select();
  1091. }
  1092. UpdateSnapObstacle();
  1093. base.OnLoad(e);
  1094. }
  1095. public void RefreshTool()
  1096. {
  1097. Type toolType = activeDocumentWorkspace.GetToolType();
  1098. Widgets.ToolsControl.SelectTool(toolType);
  1099. }
  1100. private void GradientInfoChangedHandler(object sender, EventArgs e)
  1101. {
  1102. if (widgets.ToolConfigStrip.GradientInfo != AppEnvironment.GradientInfo)
  1103. {
  1104. widgets.ToolConfigStrip.GradientInfo = AppEnvironment.GradientInfo;
  1105. }
  1106. }
  1107. private void ToolConfigStrip_GradientInfoChanged(object sender, EventArgs e)
  1108. {
  1109. if (AppEnvironment.GradientInfo != widgets.ToolConfigStrip.GradientInfo)
  1110. {
  1111. AppEnvironment.GradientInfo = widgets.ToolConfigStrip.GradientInfo;
  1112. }
  1113. }
  1114. /// <summary>
  1115. /// Keeps the Environment's ShapeDrawType and the corresponding widget synchronized
  1116. /// </summary>
  1117. private void ShapeDrawTypeChangedHandler(object sender, EventArgs e)
  1118. {
  1119. if (widgets.ToolConfigStrip.ShapeDrawType != AppEnvironment.ShapeDrawType)
  1120. {
  1121. widgets.ToolConfigStrip.ShapeDrawType = AppEnvironment.ShapeDrawType;
  1122. }
  1123. }
  1124. /// <summary>
  1125. /// Keeps the Environment's alpha blending value and the corresponding widget synchronized
  1126. /// </summary>
  1127. private void AlphaBlendingChangedHandler(object sender, EventArgs e)
  1128. {
  1129. if (widgets.ToolConfigStrip.AlphaBlending != AppEnvironment.AlphaBlending)
  1130. {
  1131. widgets.ToolConfigStrip.AlphaBlending = AppEnvironment.AlphaBlending;
  1132. }
  1133. }
  1134. private void ColorDisplay_UserPrimaryAndSecondaryColorsChanged(object sender, EventArgs e)
  1135. {
  1136. // We need to make sure that we don't change which user color is selected (primary vs. secondary)
  1137. // To do this we choose the ordering based on which one is currently active (primary vs. secondary)
  1138. if (widgets.ColorsForm.WhichUserColor == WhichUserColor.Primary)
  1139. {
  1140. widgets.ColorsForm.SetColorControlsRedraw(false);
  1141. SecondaryColorChangedHandler(sender, e);
  1142. PrimaryColorChangedHandler(sender, e);
  1143. widgets.ColorsForm.SetColorControlsRedraw(true);
  1144. widgets.ColorsForm.WhichUserColor = WhichUserColor.Primary;
  1145. }
  1146. else //if (widgets.ColorsForm.WhichUserColor == WhichUserColor.Background)
  1147. {
  1148. widgets.ColorsForm.SetColorControlsRedraw(false);
  1149. PrimaryColorChangedHandler(sender, e);
  1150. SecondaryColorChangedHandler(sender, e);
  1151. widgets.ColorsForm.SetColorControlsRedraw(true);
  1152. widgets.ColorsForm.WhichUserColor = WhichUserColor.Secondary;
  1153. }
  1154. }
  1155. private void PrimaryColorChangedHandler(object sender, EventArgs e)
  1156. {
  1157. if (sender == appEnvironment)
  1158. {
  1159. widgets.ColorsForm.UserPrimaryColor = AppEnvironment.PrimaryColor;
  1160. }
  1161. }
  1162. private void OnToolBarToleranceChanged(object sender, EventArgs e)
  1163. {
  1164. AppEnvironment.Tolerance = widgets.ToolConfigStrip.Tolerance;
  1165. this.Focus();
  1166. }
  1167. private void OnEnvironmentToleranceChanged(object sender, EventArgs e)
  1168. {
  1169. widgets.ToolConfigStrip.Tolerance = AppEnvironment.Tolerance;
  1170. this.Focus();
  1171. }
  1172. private void SecondaryColorChangedHandler(object sender, EventArgs e)
  1173. {
  1174. if (sender == appEnvironment)
  1175. {
  1176. widgets.ColorsForm.UserSecondaryColor = AppEnvironment.SecondaryColor;
  1177. }
  1178. }
  1179. private void RelinquishFocusHandler(object sender, EventArgs e)
  1180. {
  1181. this.Focus();
  1182. }
  1183. private void RelinquishFocusHandler2(object sender, EventArgs e)
  1184. {
  1185. if (this.activeDocumentWorkspace != null)
  1186. {
  1187. this.activeDocumentWorkspace.Focus();
  1188. }
  1189. }
  1190. private void ColorsForm_UserPrimaryColorChanged(object sender, ColorEventArgs e)
  1191. {
  1192. ColorsForm cf = (ColorsForm)sender;
  1193. AppEnvironment.PrimaryColor = e.Color;
  1194. }
  1195. private void ColorsForm_UserSecondaryColorChanged(object sender, ColorEventArgs e)
  1196. {
  1197. ColorsForm cf = (ColorsForm)sender;
  1198. AppEnvironment.SecondaryColor = e.Color;
  1199. }
  1200. /// <summary>
  1201. /// Handles the SelectedPathChanging event that is raised by the AppEnvironment.
  1202. /// </summary>
  1203. private void SelectedPathChangingHandler(object sender, EventArgs e)
  1204. {
  1205. }
  1206. private void UpdateSelectionToolbarButtons()
  1207. {
  1208. if (ActiveDocumentWorkspace == null || ActiveDocumentWorkspace.Selection.IsEmpty)
  1209. {
  1210. widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Cut, false);
  1211. widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Copy, false);
  1212. widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Deselect, false);
  1213. widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.CropToSelection, false);
  1214. }
  1215. else
  1216. {
  1217. widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Cut, true);
  1218. widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Copy, true);
  1219. widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.Deselect, true);
  1220. widgets.CommonActionsStrip.SetButtonEnabled(CommonAction.CropToSelection, true);
  1221. }
  1222. }
  1223. /// <summary>
  1224. /// Handles the SelectedPathChanged event that is raised by the AppEnvironment.
  1225. /// </summary>
  1226. private void SelectedPathChangedHandler(object sender, EventArgs e)
  1227. {
  1228. UpdateSelectionToolbarButtons();
  1229. }
  1230. private void ZoomChangedHandler(object sender, EventArgs e)
  1231. {
  1232. ScaleFactor sf = this.activeDocumentWorkspace.ScaleFactor;
  1233. this.toolBar.ViewConfigStrip.SuspendEvents();
  1234. this.toolBar.ViewConfigStrip.ZoomBasis = this.activeDocumentWorkspace.ZoomBasis;
  1235. this.toolBar.ViewConfigStrip.ScaleFactor = sf;
  1236. this.toolBar.ViewConfigStrip.ResumeEvents();
  1237. }
  1238. private void InitializeComponent()
  1239. {
  1240. this.toolBar = new PdnToolBar();
  1241. this.statusBar = new PdnStatusBar();
  1242. this.workspacePanel = new Panel();
  1243. this.workspacePanel.SuspendLayout();
  1244. this.statusBar.SuspendLayout();
  1245. this.SuspendLayout();
  1246. //
  1247. // toolBar
  1248. //
  1249. this.toolBar.Name = "toolBar";
  1250. this.toolBar.Dock = DockStyle.Top;
  1251. //
  1252. // statusBar
  1253. //
  1254. this.statusBar.Name = "statusBar";
  1255. //
  1256. // workspacePanel
  1257. //
  1258. this.workspacePanel.Name = "workspacePanel";
  1259. this.workspacePanel.Dock = DockStyle.Fill;
  1260. //
  1261. // AppWorkspace
  1262. //
  1263. this.Controls.Add(this.workspacePanel);
  1264. this.Controls.Add(this.statusBar);
  1265. this.Controls.Add(this.toolBar);
  1266. this.Name = "AppWorkspace";
  1267. this.Size = new System.Drawing.Size(872, 640);
  1268. this.workspacePanel.ResumeLayout(false);
  1269. this.statusBar.ResumeLayout(false);
  1270. this.statusBar.PerformLayout();
  1271. this.ResumeLayout(false);
  1272. }
  1273. private void DocumentStrip_DocumentTabClicked(
  1274. object sender,
  1275. EventArgs<Pair<DocumentWorkspace, DocumentClickAction>> e)
  1276. {
  1277. switch (e.Data.Second)
  1278. {
  1279. case DocumentClickAction.Select:
  1280. this.ActiveDocumentWorkspace = e.Data.First;
  1281. break;
  1282. case DocumentClickAction.Close:
  1283. CloseWorkspaceAction cwa = new CloseWorkspaceAction(e.Data.First);
  1284. PerformAction(cwa);
  1285. break;
  1286. default:
  1287. throw new NotImplementedException("Code for DocumentClickAction." + e.Data.Second.ToString() + " not implemented");
  1288. }
  1289. Update();
  1290. }
  1291. private void OnToolStripMouseWheel(object sender, MouseEventArgs e)
  1292. {
  1293. if (this.activeDocumentWorkspace != null)
  1294. {
  1295. this.activeDocumentWorkspace.PerformMouseWheel((Control)sender, e);
  1296. }
  1297. }
  1298. private void OnToolStripRelinquishFocus(object sender, EventArgs e)
  1299. {
  1300. if (this.activeDocumentWorkspace != null)
  1301. {
  1302. this.activeDocumentWorkspace.Focus();
  1303. }
  1304. }
  1305. // The Document* events are raised by the Document class, handled here,
  1306. // and relayed as necessary. For instance, for the DocumentMouse* events,
  1307. // these are all relayed to the active tool.
  1308. private void DocumentMouseEnterHandler(object sender, EventArgs e)
  1309. {
  1310. if (ActiveDocumentWorkspace.Tool != null)
  1311. {
  1312. ActiveDocumentWorkspace.Tool.PerformMouseEnter();
  1313. }
  1314. }
  1315. private void DocumentMouseLeaveHandler(object sender, EventArgs e)
  1316. {
  1317. if (ActiveDocumentWorkspace.Tool != null)
  1318. {
  1319. ActiveDocumentWorkspace.Tool.PerformMouseLeave();
  1320. }
  1321. }
  1322. private void DocumentMouseUpHandler(object sender, MouseEventArgs e)
  1323. {
  1324. if (ActiveDocumentWorkspace.Tool != null)
  1325. {
  1326. ActiveDocumentWorkspace.Tool.PerformMouseUp(e);
  1327. }
  1328. }
  1329. private void DocumentMouseDownHandler(object sender, MouseEventArgs e)
  1330. {
  1331. if (ActiveDocumentWorkspace.Tool != null)
  1332. {
  1333. ActiveDocumentWorkspace.Tool.PerformMouseDown(e);
  1334. }
  1335. }
  1336. private void DocumentMouseMoveHandler(object sender, MouseEventArgs e)
  1337. {
  1338. if (ActiveDocumentWorkspace.Tool != null)
  1339. {
  1340. ActiveDocumentWorkspace.Tool.PerformMouseMove(e);
  1341. }
  1342. UpdateCursorInfoInStatusBar(e.X, e.Y);
  1343. }
  1344. private void DocumentClick(object sender, EventArgs e)
  1345. {
  1346. if (ActiveDocumentWorkspace.Tool != null)
  1347. {
  1348. ActiveDocumentWorkspace.Tool.PerformClick();
  1349. }
  1350. }
  1351. private void DocumentKeyPress(object sender, KeyPressEventArgs e)
  1352. {
  1353. if (ActiveDocumentWorkspace.Tool != null)
  1354. {
  1355. ActiveDocumentWorkspace.Tool.PerformKeyPress(e);
  1356. }
  1357. }
  1358. private void DocumentKeyDown(object sender, KeyEventArgs e)
  1359. {
  1360. if (ActiveDocumentWorkspace.Tool != null)
  1361. {
  1362. ActiveDocumentWorkspace.Tool.PerformKeyDown(e);
  1363. }
  1364. }
  1365. private void DocumenKeyUp(object sender, KeyEventArgs e)
  1366. {
  1367. if (ActiveDocumentWorkspace.Tool != null)
  1368. {
  1369. ActiveDocumentWorkspace.Tool.PerformKeyUp(e);
  1370. }
  1371. }
  1372. private void InitializeFloatingForms()
  1373. {
  1374. // MainToolBarForm
  1375. mainToolBarForm = new ToolsForm();
  1376. mainToolBarForm.RelinquishFocus += RelinquishFocusHandler;
  1377. mainToolBarForm.ProcessCmdKeyEvent += OnToolFormProcessCmdKeyEvent;
  1378. // LayerForm
  1379. layerForm = new LayerForm();
  1380. layerForm.LayerControl.AppWorkspace = this;
  1381. layerForm.LayerControl.ClickedOnLayer += LayerControl_ClickedOnLayer;
  1382. layerForm.NewLayerButtonClick += LayerForm_NewLayerButtonClicked;
  1383. layerForm.DeleteLayerButtonClick += LayerForm_DeleteLayerButtonClicked;
  1384. layerForm.DuplicateLayerButtonClick += LayerForm_DuplicateLayerButtonClick;
  1385. layerForm.MergeLayerDownClick += new EventHandler(LayerForm_MergeLayerDownClick);
  1386. layerForm.MoveLayerUpButtonClick += LayerForm_MoveLayerUpButtonClicked;
  1387. layerForm.MoveLayerDownButtonClick += LayerForm_MoveLayerDownButtonClicked;
  1388. layerForm.PropertiesButtonClick += LayerForm_PropertiesButtonClick;
  1389. layerForm.RelinquishFocus += RelinquishFocusHandler;
  1390. layerForm.ProcessCmdKeyEvent += OnToolFormProcessCmdKeyEvent;
  1391. // HistoryForm
  1392. historyForm = new HistoryForm();
  1393. historyForm.RewindButtonClicked += HistoryForm_RewindButtonClicked;
  1394. historyForm.UndoButtonClicked += HistoryForm_UndoButtonClicked;
  1395. historyForm.RedoButtonClicked += HistoryForm_RedoButtonClicked;
  1396. historyForm.FastForwardButtonClicked += HistoryForm_FastForwardButtonClicked;
  1397. historyForm.RelinquishFocus += RelinquishFocusHandler;
  1398. historyForm.ProcessCmdKeyEvent += OnToolFormProcessCmdKeyEvent;
  1399. // ColorsForm
  1400. colorsForm = new ColorsForm();
  1401. colorsForm.PaletteCollection = new PaletteCollection();
  1402. colorsForm.WhichUserColor = WhichUserColor.Primary;
  1403. colorsForm.UserPrimaryColorChanged += ColorsForm_UserPrimaryColorChanged;
  1404. colorsForm.UserSecondaryColorChanged += ColorsForm_UserSecondaryColorChanged;
  1405. colorsForm.RelinquishFocus += RelinquishFocusHandler;
  1406. colorsForm.ProcessCmdKeyEvent += OnToolFormProcessCmdKeyEvent;
  1407. }
  1408. // TODO: put at correct scope
  1409. public event CmdKeysEventHandler ProcessCmdKeyEvent;
  1410. private bool OnToolFormProcessCmdKeyEvent(object sender, ref Message msg, Keys keyData)
  1411. {
  1412. if (ProcessCmdKeyEvent != null)
  1413. {
  1414. return ProcessCmdKeyEvent(sender, ref msg, keyData);
  1415. }
  1416. else
  1417. {
  1418. return false;
  1419. }
  1420. }
  1421. public void PerformActionAsync(AppWorkspaceAction performMe)
  1422. {
  1423. this.BeginInvoke(new Procedure<AppWorkspaceAction>(PerformAction), new object[] { performMe });
  1424. }
  1425. public void PerformAction(AppWorkspaceAction performMe)
  1426. {
  1427. Update();
  1428. using (new WaitCursorChanger(this))
  1429. {
  1430. performMe.PerformAction(this);
  1431. }
  1432. Update();
  1433. }
  1434. private void MainToolBar_ToolClicked(object sender, ToolClickedEventArgs e)
  1435. {
  1436. if (ActiveDocumentWorkspace != null)
  1437. {
  1438. ActiveDocumentWorkspace.Focus();
  1439. ActiveDocumentWorkspace.SetToolFromType(e.ToolType);
  1440. }
  1441. }
  1442. private void ToolChangingHandler(object sender, EventArgs e)
  1443. {
  1444. UI.SuspendControlPainting(this.toolBar);
  1445. if (ActiveDocumentWorkspace.Tool != null)
  1446. {
  1447. // unregister for events here (none at this time)
  1448. }
  1449. }
  1450. private void ToolChangedHandler(object sender, EventArgs e)
  1451. {
  1452. if (ActiveDocumentWorkspace.Tool != null)
  1453. {
  1454. this.widgets.ToolsControl.SelectTool(ActiveDocumentWorkspace.GetToolType(), false);
  1455. this.toolBar.ToolChooserStrip.SelectTool(ActiveDocumentWorkspace.GetToolType(), false);
  1456. this.toolBar.ToolConfigStrip.Visible = true; // HACK: see bug #2702
  1457. this.toolBar.ToolConfigStrip.ToolBarConfigItems = ActiveDocumentWorkspace.Tool.ToolBarConfigItems;
  1458. this.globalToolTypeChoice = ActiveDocumentWorkspace.GetToolType();
  1459. }
  1460. UpdateStatusBarContextStatus();
  1461. UI.ResumeControlPainting(this.toolBar);
  1462. this.toolBar.Refresh();
  1463. }
  1464. private void DrawConfigStrip_AntiAliasingChanged(object sender, System.EventArgs e)
  1465. {
  1466. AppEnvironment.AntiAliasing = ((ToolConfigStrip)sender).AntiAliasing;
  1467. }
  1468. private void DrawConfigStrip_PenChanged(object sender, System.EventArgs e)
  1469. {
  1470. AppEnvironment.PenInfo = this.toolBar.ToolConfigStrip.PenInfo;
  1471. }
  1472. private void DrawConfigStrip_BrushChanged(object sender, System.EventArgs e)
  1473. {
  1474. AppEnvironment.BrushInfo = this.toolBar.ToolConfigStrip.BrushInfo;
  1475. }
  1476. private void LayerControl_ClickedOnLayer(object sender, EventArgs<Layer> ce)
  1477. {
  1478. if (ActiveDocumentWorkspace != null)
  1479. {
  1480. if (ce.Data != ActiveDocumentWorkspace.ActiveLayer)
  1481. {
  1482. ActiveDocumentWorkspace.ActiveLayer = ce.Data;
  1483. }
  1484. }
  1485. this.RelinquishFocusHandler(sender, EventArgs.Empty);
  1486. }
  1487. private void LayerForm_NewLayerButtonClicked(object sender, System.EventArgs e)
  1488. {
  1489. if (ActiveDocumentWorkspace != null)
  1490. {
  1491. ActiveDocumentWorkspace.ExecuteFunction(new AddNewBlankLayerFunction());
  1492. }
  1493. }
  1494. private void LayerForm_DeleteLayerButtonClicked(object sender, System.EventArgs e)
  1495. {
  1496. if (ActiveDocumentWorkspace != null && ActiveDocumentWorkspace.Document.Layers.Count > 1)
  1497. {
  1498. ActiveDocumentWorkspace.ExecuteFunction(new DeleteLayerFunction(ActiveDocumentWorkspace.ActiveLayerIndex));
  1499. }
  1500. }
  1501. private void LayerForm_MergeLayerDownClick(object sender, EventArgs e)
  1502. {
  1503. if (ActiveDocumentWorkspace != null)
  1504. {
  1505. if (ActiveDocumentWorkspace != null &&
  1506. ActiveDocumentWorkspace.ActiveLayerIndex > 0)
  1507. {
  1508. // TODO: keep this in sync with LayersMenu. not appropriate to refactor into an Action for a 'dot' release
  1509. int newLayerIndex = Utility.Clamp(
  1510. ActiveDocumentWorkspace.ActiveLayerIndex - 1,
  1511. 0,
  1512. ActiveDocumentWorkspace.Document.Layers.Count - 1);
  1513. ActiveDocumentWorkspace.ExecuteFunction(
  1514. new MergeLayerDownFunction(ActiveDocumentWorkspace.ActiveLayerIndex));
  1515. ActiveDocumentWorkspace.ActiveLayerIndex = newLayerIndex;
  1516. }
  1517. }
  1518. }
  1519. private void LayerForm_DuplicateLayerButtonClick(object sender, System.EventArgs e)
  1520. {
  1521. if (ActiveDocumentWorkspace != null)
  1522. {
  1523. ActiveDocumentWorkspace.ExecuteFunction(new DuplicateLayerFunction(ActiveDocumentWorkspace.ActiveLayerIndex));
  1524. }
  1525. }
  1526. private void LayerForm_MoveLayerUpButtonClicked(object sender, System.EventArgs e)
  1527. {
  1528. if (ActiveDocumentWorkspace != null && ActiveDocumentWorkspace.Document.Layers.Count >= 2)
  1529. {
  1530. ActiveDocumentWorkspace.PerformAction(new MoveActiveLayerUpAction());
  1531. }
  1532. }
  1533. private void LayerForm_MoveLayerDownButtonClicked(object sender, System.EventArgs e)
  1534. {
  1535. if (ActiveDocumentWorkspace != null && ActiveDocumentWorkspace.Document.Layers.Count >= 2)
  1536. {
  1537. ActiveDocumentWorkspace.PerformAction(new MoveActiveLayerDownAction());
  1538. }
  1539. }
  1540. private void DrawConfigStrip_ShapeDrawTypeChanged(object sender, System.EventArgs e)
  1541. {
  1542. if (AppEnvironment.ShapeDrawType != widgets.ToolConfigStrip.ShapeDrawType)
  1543. {
  1544. AppEnvironment.ShapeDrawType = widgets.ToolConfigStrip.ShapeDrawType;
  1545. }
  1546. }
  1547. private void HistoryForm_UndoButtonClicked(object sender, System.EventArgs e)
  1548. {
  1549. if (ActiveDocumentWorkspace != null)
  1550. {
  1551. ActiveDocumentWorkspace.PerformAction(new HistoryUndoAction());
  1552. }
  1553. }
  1554. private void HistoryForm_RedoButtonClicked(object sender, System.EventArgs e)
  1555. {
  1556. if (ActiveDocumentWorkspace != null)
  1557. {
  1558. ActiveDocumentWorkspace.PerformAction(new HistoryRedoAction());
  1559. }
  1560. }
  1561. private void ViewConfigStrip_RulersEnabledChanged(object sender, System.EventArgs e)
  1562. {
  1563. if (ActiveDocumentWorkspace != null)
  1564. {
  1565. ActiveDocumentWorkspace.RulersEnabled = this.toolBar.ViewConfigStrip.RulersEnabled;
  1566. }
  1567. }
  1568. private void HistoryForm_RewindButtonClicked(object sender, EventArgs e)
  1569. {
  1570. if (ActiveDocumentWorkspace != null)
  1571. {
  1572. ActiveDocumentWorkspace.PerformAction(new HistoryRewindAction());
  1573. }
  1574. }
  1575. private void HistoryForm_FastForwardButtonClicked(object sender, EventArgs e)
  1576. {
  1577. if (ActiveDocumentWorkspace != null)
  1578. {
  1579. ActiveDocumentWorkspace.PerformAction(new HistoryFastForwardAction());
  1580. }
  1581. }
  1582. private void LayerForm_PropertiesButtonClick(object sender, EventArgs e)
  1583. {
  1584. if (ActiveDocumentWorkspace != null)
  1585. {
  1586. ActiveDocumentWorkspace.PerformAction(new OpenActiveLayerPropertiesAction());
  1587. }
  1588. }
  1589. private void Environment_FontInfoChanged(object sender, EventArgs e)
  1590. {
  1591. this.widgets.ToolConfigStrip.FontInfo = AppEnvironment.FontInfo;
  1592. }
  1593. private void Environment_FontSmoothingChanged(object sender, EventArgs e)
  1594. {
  1595. this.widgets.ToolConfigStrip.FontSmoothing = AppEnvironment.FontSmoothing;
  1596. }
  1597. private void Environment_TextAlignmentChanged(object sender, EventArgs e)
  1598. {
  1599. this.widgets.ToolConfigStrip.FontAlignment = AppEnvironment.TextAlignment;
  1600. }
  1601. private void Environment_PenInfoChanged(object sender, EventArgs e)
  1602. {
  1603. this.widgets.ToolConfigStrip.PenInfo = AppEnvironment.PenInfo;
  1604. }
  1605. private void Environment_BrushInfoChanged(object sender, EventArgs e)
  1606. {
  1607. }
  1608. private void ToolConfigStrip_TextAlignmentChanged(object sender, EventArgs e)
  1609. {
  1610. AppEnvironment.TextAlignment = widgets.ToolConfigStrip.FontAlignment;
  1611. }
  1612. private void ToolConfigStrip_FontTextChanged(object sender, EventArgs e)
  1613. {
  1614. AppEnvironment.FontInfo = widgets.ToolConfigStrip.FontInfo;
  1615. }
  1616. private void ToolConfigStrip_FontSmoothingChanged(object sender, EventArgs e)
  1617. {
  1618. AppEnvironment.FontSmoothing = widgets.ToolConfigStrip.FontSmoothing;
  1619. }
  1620. protected override void OnResize(EventArgs e)
  1621. {
  1622. UpdateSnapObstacle();
  1623. base.OnResize(e);
  1624. if (ParentForm != null && this.ActiveDocumentWorkspace != null)
  1625. {
  1626. if (ParentForm.WindowState == FormWindowState.Minimized)
  1627. {
  1628. this.ActiveDocumentWorkspace.EnableToolPulse = false;
  1629. }
  1630. else
  1631. {
  1632. this.ActiveDocumentWorkspace.EnableToolPulse = true;
  1633. }
  1634. }
  1635. }
  1636. private void DocumentWorkspace_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
  1637. {
  1638. OnScroll(e);
  1639. }
  1640. private void DocumentWorkspace_Layout(object sender, LayoutEventArgs e)
  1641. {
  1642. UpdateSnapObstacle();
  1643. }
  1644. private void ViewConfigStrip_ZoomBasisChanged(object sender, EventArgs e)
  1645. {
  1646. if (ActiveDocumentWorkspace != null)
  1647. {
  1648. if (ActiveDocumentWorkspace.ZoomBasis != this.toolBar.ViewConfigStrip.ZoomBasis)
  1649. {
  1650. ActiveDocumentWorkspace.ZoomBasis = this.toolBar.ViewConfigStrip.ZoomBasis;
  1651. }
  1652. }
  1653. }
  1654. private void DocumentWorkspace_ZoomBasisChanged(object sender, EventArgs e)
  1655. {
  1656. if (this.toolBar.ViewConfigStrip.ZoomBasis != this.ActiveDocumentWorkspace.ZoomBasis)
  1657. {
  1658. this.toolBar.ViewConfigStrip.ZoomBasis = this.ActiveDocumentWorkspace.ZoomBasis;
  1659. }
  1660. }
  1661. private void ViewConfigStrip_ZoomScaleChanged(object sender, EventArgs e)
  1662. {
  1663. if (ActiveDocumentWorkspace != null)
  1664. {
  1665. if (this.toolBar.ViewConfigStrip.ZoomBasis == ZoomBasis.ScaleFactor)
  1666. {
  1667. this.activeDocumentWorkspace.ScaleFactor = this.toolBar.ViewConfigStrip.ScaleFactor;
  1668. }
  1669. }
  1670. }
  1671. private void DocumentWorkspace_RulersEnabledChanged(object sender, EventArgs e)
  1672. {
  1673. this.toolBar.ViewConfigStrip.RulersEnabled = this.activeDocumentWorkspace.RulersEnabled;
  1674. this.globalRulersChoice = this.activeDocumentWorkspace.RulersEnabled;
  1675. PerformLayout();
  1676. ActiveDocumentWorkspace.UpdateRulerSelectionTinting();
  1677. Settings.CurrentUser.SetBoolean(SettingNames.Rulers, this.activeDocumentWorkspace.RulersEnabled);
  1678. }
  1679. private void Environment_AntiAliasingChanged(object sender, EventArgs e)
  1680. {
  1681. this.toolBar.ToolConfigStrip.AntiAliasing = AppEnvironment.AntiAliasing;
  1682. }
  1683. private void ViewConfigStrip_ZoomIn(object sender, EventArgs e)
  1684. {
  1685. if (this.ActiveDocumentWorkspace != null)
  1686. {
  1687. this.ActiveDocumentWorkspace.ZoomIn();
  1688. }
  1689. }
  1690. private void ViewConfigStrip_ZoomOut(object sender, EventArgs e)
  1691. {
  1692. if (this.ActiveDocumentWorkspace != null)
  1693. {
  1694. this.ActiveDocumentWorkspace.ZoomOut();
  1695. }
  1696. }
  1697. private void ViewConfigStrip_UnitsChanged(object sender, EventArgs e)
  1698. {
  1699. if (this.toolBar.ViewConfigStrip.Units != MeasurementUnit.Pixel)
  1700. {
  1701. Settings.CurrentUser.SetString(SettingNames.LastNonPixelUnits, this.toolBar.ViewConfigStrip.Units.ToString());
  1702. }
  1703. if (this.activeDocumentWorkspace != null)
  1704. {
  1705. this.activeDocumentWorkspace.Units = this.Units;
  1706. }
  1707. Settings.CurrentUser.SetString(SettingNames.Units, this.toolBar.ViewConfigStrip.Units.ToString());
  1708. UpdateDocInfoInStatusBar();
  1709. this.statusBar.CursorInfoText = string.Empty;
  1710. OnUnitsChanged();
  1711. }
  1712. private void OnDrawConfigStripAlphaBlendingChanged(object sender, EventArgs e)
  1713. {
  1714. if (AppEnvironment.AlphaBlending != widgets.ToolConfigStrip.AlphaBlending)
  1715. {
  1716. AppEnvironment.AlphaBlending = widgets.ToolConfigStrip.AlphaBlending;
  1717. }
  1718. }
  1719. public event EventHandler StatusChanged;
  1720. private void OnStatusChanged()
  1721. {
  1722. if (StatusChanged != null)
  1723. {
  1724. StatusChanged(this, EventArgs.Empty);
  1725. }
  1726. }
  1727. private void OnDocumentWorkspaceStatusChanged(object sender, EventArgs e)
  1728. {
  1729. OnStatusChanged();
  1730. UpdateStatusBarContextStatus();
  1731. }
  1732. private void UpdateStatusBarContextStatus()
  1733. {
  1734. if (ActiveDocumentWorkspace != null)
  1735. {
  1736. this.statusBar.ContextStatusText = this.activeDocumentWorkspace.StatusText;
  1737. this.statusBar.ContextStatusImage = this.activeDocumentWorkspace.StatusIcon;
  1738. }
  1739. else
  1740. {
  1741. this.statusBar.ContextStatusText = string.Empty;
  1742. this.statusBar.ContextStatusImage = null;
  1743. }
  1744. }
  1745. private static bool NullGetThumbnailImageAbort()
  1746. {
  1747. return false;
  1748. }
  1749. /// <summary>
  1750. /// Creates a blank document of the given size in a new workspace, and activates that workspace.
  1751. /// </summary>
  1752. /// <remarks>
  1753. /// If isInitial=true, then last workspace added by this method is kept track of, and if it is not modified by
  1754. /// the time the next workspace is added, then it will be removed.
  1755. /// </remarks>
  1756. /// <returns>true if everything was successful, false if there wasn't enough memory</returns>
  1757. public bool CreateBlankDocumentInNewWorkspace(Size size, MeasurementUnit dpuUnit, double dpu, bool isInitial)
  1758. {
  1759. DocumentWorkspace dw1 = this.activeDocumentWorkspace;
  1760. if (dw1 != null)
  1761. {
  1762. dw1.SuspendRefresh();
  1763. }
  1764. try
  1765. {
  1766. Document untitled = new Document(size.Width, size.Height);
  1767. untitled.DpuUnit = dpuUnit;
  1768. untitled.DpuX = dpu;
  1769. untitled.DpuY = dpu;
  1770. BitmapLayer bitmapLayer;
  1771. try
  1772. {
  1773. using (new WaitCursorChanger(this))
  1774. {
  1775. bitmapLayer = Layer.CreateBackgroundLayer(size.Width, size.Height);
  1776. }
  1777. }
  1778. catch (OutOfMemoryException)
  1779. {
  1780. Utility.ErrorBox(this, PdnResources.GetString("NewImageAction.Error.OutOfMemory"));
  1781. return false;
  1782. }
  1783. using (new WaitCursorChanger(this))
  1784. {
  1785. bool focused = false;
  1786. if (this.ActiveDocumentWorkspace != null && this.ActiveDocumentWorkspace.Focused)
  1787. {
  1788. focused = true;
  1789. }
  1790. untitled.Layers.Add(bitmapLayer);
  1791. DocumentWorkspace dw = this.AddNewDocumentWorkspace();
  1792. this.Widgets.DocumentStrip.LockDocumentWorkspaceDirtyValue(dw, false);
  1793. dw.SuspendRefresh();
  1794. try
  1795. {
  1796. dw.Document = untitled;
  1797. }
  1798. catch (OutOfMemoryException)
  1799. {
  1800. Utility.ErrorBox(this, PdnResources.GetString("NewImageAction.Error.OutOfMemory"));
  1801. RemoveDocumentWorkspace(dw);
  1802. untitled.Dispose();
  1803. return false;
  1804. }
  1805. dw.ActiveLayer = (Layer)dw.Document.Layers[0];
  1806. this.ActiveDocumentWorkspace = dw;
  1807. dw.SetDocumentSaveOptions(null, null, null);
  1808. dw.History.ClearAll();
  1809. dw.History.PushNewMemento(
  1810. new NullHistoryMemento(PdnResources.GetString("NewImageAction.Name"),
  1811. this.FileNewIcon));
  1812. dw.Document.Dirty = false;
  1813. dw.ResumeRefresh();
  1814. if (isInitial)
  1815. {
  1816. this.initialWorkspace = dw;
  1817. }
  1818. if (focused)
  1819. {
  1820. this.ActiveDocumentWorkspace.Focus();
  1821. }
  1822. this.Widgets.DocumentStrip.UnlockDocumentWorkspaceDirtyValue(dw);
  1823. }
  1824. }
  1825. finally
  1826. {
  1827. if (dw1 != null)
  1828. {
  1829. dw1.ResumeRefresh();
  1830. }
  1831. }
  1832. return true;
  1833. }
  1834. public bool OpenFilesInNewWorkspace(string[] fileNames)
  1835. {
  1836. if (IsDisposed)
  1837. {
  1838. return false;
  1839. }
  1840. bool result = true;
  1841. foreach (string fileName in fileNames)
  1842. {
  1843. result &= OpenFileInNewWorkspace(fileName);
  1844. if (!result)
  1845. {
  1846. break;
  1847. }
  1848. }
  1849. return result;
  1850. }
  1851. public bool OpenFileInNewWorkspace(string fileName)
  1852. {
  1853. return OpenFileInNewWorkspace(fileName, true);
  1854. }
  1855. public bool OpenFileInNewWorkspace(string fileName, bool addToMruList)
  1856. {
  1857. if (fileName == null)
  1858. {
  1859. throw new ArgumentNullException("fileName");
  1860. }
  1861. if (fileName.Length == 0)
  1862. {
  1863. throw new ArgumentOutOfRangeException("fileName.Length == 0");
  1864. }
  1865. PdnBaseForm.UpdateAllForms();
  1866. FileType fileType;
  1867. Document document;
  1868. this.widgets.StatusBarProgress.ResetProgressStatusBar();
  1869. ProgressEventHandler progressCallback =
  1870. delegate(object sender, ProgressEventArgs e)
  1871. {
  1872. this.widgets.StatusBarProgress.SetProgressStatusBar(e.Percent);
  1873. };
  1874. document = DocumentWorkspace.LoadDocument(this, fileName, out fileType, progressCallback);
  1875. this.widgets.StatusBarProgress.EraseProgressStatusBar();
  1876. if (document == null)
  1877. {
  1878. this.Cursor = Cursors.Default;
  1879. }
  1880. else
  1881. {
  1882. using (new WaitCursorChanger(this))
  1883. {
  1884. DocumentWorkspace dw = AddNewDocumentWorkspace();
  1885. Widgets.DocumentStrip.LockDocumentWorkspaceDirtyValue(dw, false);
  1886. try
  1887. {
  1888. dw.Document = document;
  1889. }
  1890. catch (OutOfMemoryException)
  1891. {
  1892. Utility.ErrorBox(this, PdnResources.GetString("LoadImage.Error.OutOfMemoryException"));
  1893. RemoveDocumentWorkspace(dw);
  1894. document.Dispose();
  1895. return false;
  1896. }
  1897. dw.ActiveLayer = (Layer)document.Layers[0];
  1898. dw.SetDocumentSaveOptions(fileName, fileType, null);
  1899. this.ActiveDocumentWorkspace = dw;
  1900. dw.History.ClearAll();
  1901. dw.History.PushNewMemento(
  1902. new NullHistoryMemento(
  1903. PdnResources.GetString("OpenImageAction.Name"),
  1904. this.ImageFromDiskIcon));
  1905. document.Dirty = false;
  1906. Widgets.DocumentStrip.UnlockDocumentWorkspaceDirtyValue(dw);
  1907. }
  1908. if (document != null)
  1909. {
  1910. ActiveDocumentWorkspace.ZoomBasis = ZoomBasis.FitToWindow;
  1911. }
  1912. // add to MRU list
  1913. if (addToMruList)
  1914. {
  1915. ActiveDocumentWorkspace.AddToMruList();
  1916. }
  1917. this.toolBar.DocumentStrip.SyncThumbnails();
  1918. WarnAboutSavedWithVersion(document.SavedWithVersion);
  1919. }
  1920. if (ActiveDocumentWorkspace != null)
  1921. {
  1922. ActiveDocumentWorkspace.Focus();
  1923. }
  1924. return document != null;
  1925. }
  1926. private void WarnAboutSavedWithVersion(Version savedWith)
  1927. {
  1928. // warn about version?
  1929. // 2.1 Build 1897 signifies when the file format changed and broke backwards compatibility (for saving)
  1930. // 2.1 Build 1921 signifies when MemoryBlock was upgraded to support 64-bits, which broke it again
  1931. // 2.1 Build 1924 upgraded to "unimportant ordering" for MemoryBlock serialization so we can to faster multiproc saves
  1932. // (in v2.5 we always save in order, although that doesn't change the file format's laxness)
  1933. // 2.5 Build 2105 changed the way PropertyItems are serialized
  1934. // 2.6 Build upgrade to .NET 2.0, does not appear to be compatible with 2.5 and earlier files as a result
  1935. if (savedWith < new Version(2, 6, 0))
  1936. {
  1937. Version ourVersion = PdnInfo.GetVersion();
  1938. Version ourVersion2 = new Version(ourVersion.Major, ourVersion.Minor);
  1939. Version ourVersion3 = new Version(ourVersion.Major, ourVersion.Minor, ourVersion.Build);
  1940. int fields;
  1941. if (savedWith < ourVersion2)
  1942. {
  1943. fields = 2;
  1944. }
  1945. else
  1946. {
  1947. fields = 3;
  1948. }
  1949. string format = PdnResources.GetString("SavedWithOlderVersion.Format");
  1950. string text = string.Format(format, savedWith.ToString(fields), ourVersion.ToString(fields));
  1951. // TODO: should we even bother to inform them? It is probably more annoying than not,
  1952. // especially since older versions will say "Hey this file is corrupt OR saved with a newer version"
  1953. //Utility.InfoBox(this, text);
  1954. }
  1955. }
  1956. /// <summary>
  1957. /// Computes what the size of a new document should be. If the screen is in a normal,
  1958. /// wider-than-tall (landscape) mode then it returns 800x600. If the screen is in a
  1959. /// taller-than-wide (portrait) mode then it retusn 600x800. If the screen is square
  1960. /// then it returns 800x600.
  1961. /// </summary>
  1962. public Size GetNewDocumentSize()
  1963. {
  1964. PdnBaseForm findForm = this.FindForm() as PdnBaseForm;
  1965. if (findForm != null && findForm.ScreenAspect < 1.0)
  1966. {
  1967. return new Size(600, 800);
  1968. }
  1969. else
  1970. {
  1971. return new Size(800, 600);
  1972. }
  1973. }
  1974. private void CommonActionsStrip_ButtonClick(object sender, EventArgs<CommonAction> e)
  1975. {
  1976. CommonAction ca = e.Data;
  1977. switch (ca)
  1978. {
  1979. case CommonAction.New:
  1980. PerformAction(new NewImageAction());
  1981. break;
  1982. case CommonAction.Open:
  1983. PerformAction(new OpenFileAction());
  1984. break;
  1985. case CommonAction.Save:
  1986. if (ActiveDocumentWorkspace != null)
  1987. {
  1988. ActiveDocumentWorkspace.DoSave();
  1989. }
  1990. break;
  1991. case CommonAction.Print:
  1992. if (ActiveDocumentWorkspace != null)
  1993. {
  1994. PrintAction pa = new PrintAction();
  1995. ActiveDocumentWorkspace.PerformAction(pa);
  1996. }
  1997. break;
  1998. case CommonAction.Cut:
  1999. if (ActiveDocumentWorkspace != null)
  2000. {
  2001. CutAction cutAction = new CutAction();
  2002. cutAction.PerformAction(ActiveDocumentWorkspace);
  2003. }
  2004. break;
  2005. case CommonAction.Copy:
  2006. if (ActiveDocumentWorkspace != null)
  2007. {
  2008. CopyToClipboardAction ctca = new CopyToClipboardAction(ActiveDocumentWorkspace);
  2009. ctca.PerformAction();
  2010. }
  2011. break;
  2012. case CommonAction.Paste:
  2013. if (ActiveDocumentWorkspace != null)
  2014. {
  2015. PasteAction pa = new PasteAction(ActiveDocumentWorkspace);
  2016. pa.PerformAction();
  2017. }
  2018. break;
  2019. case CommonAction.CropToSelection:
  2020. if (ActiveDocumentWorkspace != null)
  2021. {
  2022. using (new PushNullToolMode(ActiveDocumentWorkspace))
  2023. {
  2024. ActiveDocumentWorkspace.ExecuteFunction(new CropToSelectionFunction());
  2025. }
  2026. }
  2027. break;
  2028. case CommonAction.Deselect:
  2029. if (ActiveDocumentWorkspace != null)
  2030. {
  2031. ActiveDocumentWorkspace.ExecuteFunction(new DeselectFunction());
  2032. }
  2033. break;
  2034. case CommonAction.Undo:
  2035. if (ActiveDocumentWorkspace != null)
  2036. {
  2037. ActiveDocumentWorkspace.PerformAction(new HistoryUndoAction());
  2038. }
  2039. break;
  2040. case CommonAction.Redo:
  2041. if (ActiveDocumentWorkspace != null)
  2042. {
  2043. ActiveDocumentWorkspace.PerformAction(new HistoryRedoAction());
  2044. }
  2045. break;
  2046. default:
  2047. throw new InvalidEnumArgumentException("e.Data");
  2048. }
  2049. if (ActiveDocumentWorkspace != null)
  2050. {
  2051. ActiveDocumentWorkspace.Focus();
  2052. }
  2053. }
  2054. }
  2055. }