PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/src/AppWorkspace.cs

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