Tool.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  1. using PaintDotNet.Measurement.HistoryFunctions;
  2. using PaintDotNet.SystemLayer;
  3. using System;
  4. using System.Collections;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8. namespace PaintDotNet.Measurement
  9. {
  10. /// <summary>
  11. /// Encapsulates the functionality for a tool that goes in the main window's toolbar
  12. /// and that affects the Document.
  13. /// A Tool should only emit a HistoryMemento when it actually modifies the canvas.
  14. /// So, for instance, if the user draws a line but that line doesn't fall within
  15. /// the canvas (like if the seleciton region excludes it), then since the user
  16. /// hasn't really done anything there should be no HistoryMemento emitted.
  17. /// </summary>
  18. /// <remarks>
  19. /// A bit about the eventing model:
  20. /// * Perform[Event]() methods are ALWAYS used to trigger the events. This can be called by
  21. /// either instance methods or outside (client) callers.
  22. /// * [Event]() methods are called first by Perform[Event](). This gives the base Tool class a
  23. /// first chance at handling the event. These methods are private and non-overridable.
  24. /// * On[Event]() methods are then called by [Event]() if necessary, and should be overrided
  25. /// as necessary by derived classes. Always call the base implementation unless the
  26. /// documentation says otherwise. The base implementation gives the Tool a chance to provide
  27. /// default, overridable behavior for an event.
  28. /// </remarks>
  29. public class Tool : IDisposable, IHotKeyTarget
  30. {
  31. public static readonly Type DefaultToolType = typeof(Tools.PaintBrushTool);
  32. private ImageResource toolBarImage;
  33. private Cursor cursor;
  34. private ToolInfo toolInfo;
  35. private int mouseDown = 0; // incremented for every MouseDown, decremented for every MouseUp
  36. private int ignoreMouseMove = 0; // when >0, MouseMove is ignored and then this is decremented
  37. protected Cursor handCursor;
  38. protected Cursor handCursorMouseDown;
  39. protected Cursor handCursorInvalid;
  40. private Cursor panOldCursor;
  41. private Point lastMouseXY;
  42. private Point lastPanMouseXY;
  43. private bool panMode = false; // 'true' when the user is holding down the spacebar
  44. private bool panTracking = false; // 'true' when panMode is true, and when the mouse is down (which is when MouseMove should do panning)
  45. private MoveNubRenderer trackingNub = null; // when we are in pan-tracking mode, we draw this in the center of the screen
  46. private IDocumentWorkspace documentWorkspace;
  47. private bool active = false;
  48. protected bool autoScroll = true;
  49. private Hashtable keysThatAreDown = new Hashtable();
  50. private MouseButtons lastButton = MouseButtons.None;
  51. private Surface scratchSurface;
  52. private PdnRegion saveRegion;
  53. private int mouseEnter; // increments on MouseEnter, decrements on MouseLeave. The MouseLeave event is ONLY raised when this value decrements to 0, and MouseEnter is ONLY raised when this value increments to 1
  54. protected Surface ScratchSurface
  55. {
  56. get
  57. {
  58. return scratchSurface;
  59. }
  60. }
  61. protected Document Document
  62. {
  63. get
  64. {
  65. return DocumentWorkspace.GetDocument();
  66. }
  67. }
  68. public IDocumentWorkspace DocumentWorkspace
  69. {
  70. get
  71. {
  72. return this.documentWorkspace;
  73. }
  74. }
  75. public IAppWorkspace AppWorkspace
  76. {
  77. get
  78. {
  79. return DocumentWorkspace.GetAppWorkspace();
  80. }
  81. }
  82. protected IAppEnvironment AppEnvironment
  83. {
  84. get
  85. {
  86. return AppWorkspace.GetAppEnvironment();
  87. }
  88. }
  89. protected Selection Selection
  90. {
  91. get
  92. {
  93. return DocumentWorkspace.GetSelection();
  94. }
  95. }
  96. protected Layer ActiveLayer
  97. {
  98. get
  99. {
  100. return DocumentWorkspace.GetActiveLayer();
  101. }
  102. }
  103. protected int ActiveLayerIndex
  104. {
  105. get
  106. {
  107. return DocumentWorkspace.GetActiveLayerIndex();
  108. }
  109. set
  110. {
  111. DocumentWorkspace.SetActiveLayerIndex(value);
  112. }
  113. }
  114. public void ClearSavedMemory()
  115. {
  116. this.savedTiles = null;
  117. }
  118. public void ClearSavedRegion()
  119. {
  120. if (this.saveRegion != null)
  121. {
  122. this.saveRegion.Dispose();
  123. this.saveRegion = null;
  124. }
  125. }
  126. public void RestoreRegion(PdnRegion region)
  127. {
  128. if (region != null)
  129. {
  130. BitmapLayer activeLayer = (BitmapLayer)ActiveLayer;
  131. activeLayer.Surface.CopySurface(this.ScratchSurface, region);
  132. activeLayer.Invalidate(region);
  133. }
  134. }
  135. public void RestoreSavedRegion()
  136. {
  137. if (this.saveRegion != null)
  138. {
  139. BitmapLayer activeLayer = (BitmapLayer)ActiveLayer;
  140. activeLayer.Surface.CopySurface(this.ScratchSurface, this.saveRegion);
  141. activeLayer.Invalidate(this.saveRegion);
  142. this.saveRegion.Dispose();
  143. this.saveRegion = null;
  144. }
  145. }
  146. private const int saveTileGranularity = 32;
  147. private BitVector2D savedTiles;
  148. public void SaveRegion(PdnRegion saveMeRegion, Rectangle saveMeBounds)
  149. {
  150. BitmapLayer activeLayer = (BitmapLayer)ActiveLayer;
  151. if (savedTiles == null)
  152. {
  153. savedTiles = new BitVector2D(
  154. (activeLayer.Width + saveTileGranularity - 1) / saveTileGranularity,
  155. (activeLayer.Height + saveTileGranularity - 1) / saveTileGranularity);
  156. savedTiles.Clear(false);
  157. }
  158. Rectangle regionBounds;
  159. if (saveMeRegion == null)
  160. {
  161. regionBounds = saveMeBounds;
  162. }
  163. else
  164. {
  165. regionBounds = saveMeRegion.GetBoundsInt();
  166. }
  167. Rectangle bounds = Rectangle.Union(regionBounds, saveMeBounds);
  168. bounds.Intersect(activeLayer.Bounds);
  169. int leftTile = bounds.Left / saveTileGranularity;
  170. int topTile = bounds.Top / saveTileGranularity;
  171. int rightTile = (bounds.Right - 1) / saveTileGranularity;
  172. int bottomTile = (bounds.Bottom - 1) / saveTileGranularity;
  173. for (int tileY = topTile; tileY <= bottomTile; ++tileY)
  174. {
  175. Rectangle rowAccumBounds = Rectangle.Empty;
  176. for (int tileX = leftTile; tileX <= rightTile; ++tileX)
  177. {
  178. if (!savedTiles.Get(tileX, tileY))
  179. {
  180. Rectangle tileBounds = new Rectangle(tileX * saveTileGranularity, tileY * saveTileGranularity,
  181. saveTileGranularity, saveTileGranularity);
  182. tileBounds.Intersect(activeLayer.Bounds);
  183. if (rowAccumBounds == Rectangle.Empty)
  184. {
  185. rowAccumBounds = tileBounds;
  186. }
  187. else
  188. {
  189. rowAccumBounds = Rectangle.Union(rowAccumBounds, tileBounds);
  190. }
  191. savedTiles.Set(tileX, tileY, true);
  192. }
  193. else
  194. {
  195. if (rowAccumBounds != Rectangle.Empty)
  196. {
  197. using (Surface dst = ScratchSurface.CreateWindow(rowAccumBounds),
  198. src = activeLayer.Surface.CreateWindow(rowAccumBounds))
  199. {
  200. dst.CopySurface(src);
  201. }
  202. rowAccumBounds = Rectangle.Empty;
  203. }
  204. }
  205. }
  206. if (rowAccumBounds != Rectangle.Empty)
  207. {
  208. using (Surface dst = ScratchSurface.CreateWindow(rowAccumBounds),
  209. src = activeLayer.Surface.CreateWindow(rowAccumBounds))
  210. {
  211. dst.CopySurface(src);
  212. }
  213. rowAccumBounds = Rectangle.Empty;
  214. }
  215. }
  216. if (this.saveRegion != null)
  217. {
  218. this.saveRegion.Dispose();
  219. this.saveRegion = null;
  220. }
  221. if (saveMeRegion != null)
  222. {
  223. this.saveRegion = saveMeRegion.Clone();
  224. }
  225. }
  226. private sealed class KeyTimeInfo
  227. {
  228. public DateTime KeyDownTime;
  229. public DateTime LastKeyPressPulse;
  230. private int repeats = 0;
  231. public int Repeats
  232. {
  233. get
  234. {
  235. return repeats;
  236. }
  237. set
  238. {
  239. repeats = value;
  240. }
  241. }
  242. public KeyTimeInfo()
  243. {
  244. KeyDownTime = DateTime.Now;
  245. LastKeyPressPulse = KeyDownTime;
  246. }
  247. }
  248. /// <summary>
  249. /// Tells you whether the tool is "active" or not. If the tool is not active
  250. /// it is not safe to call any other method besides PerformActivate. All
  251. /// properties are safe to get values from.
  252. /// </summary>
  253. public bool Active
  254. {
  255. get
  256. {
  257. return this.active;
  258. }
  259. }
  260. /// <summary>
  261. /// Returns true if the Tool has the input focus, or false if it does not.
  262. /// </summary>
  263. /// <remarks>
  264. /// This is used, for instanced, by the Text Tool so that it doesn't blink the
  265. /// cursor unless it's actually going to do something in response to your
  266. /// keyboard input!
  267. /// </remarks>
  268. public bool Focused
  269. {
  270. get
  271. {
  272. return DocumentWorkspace.Focused();
  273. }
  274. }
  275. public bool IsMouseDown
  276. {
  277. get
  278. {
  279. return this.mouseDown > 0;
  280. }
  281. }
  282. /// <summary>
  283. /// Gets a flag that determines whether the Tool is deactivated while the current
  284. /// layer is changing, and then reactivated afterwards.
  285. /// </summary>
  286. /// <remarks>
  287. /// This property is queried every time the ActiveLayer property of DocumentWorkspace
  288. /// is changed. If false is returned, then the tool is not deactivated during the
  289. /// layer change and must manually maintain coherency.
  290. /// </remarks>
  291. public virtual bool DeactivateOnLayerChange
  292. {
  293. get
  294. {
  295. return true;
  296. }
  297. }
  298. /// <summary>
  299. /// Tells you which keys are pressed
  300. /// </summary>
  301. public Keys ModifierKeys
  302. {
  303. get
  304. {
  305. return Control.ModifierKeys;
  306. }
  307. }
  308. /// <summary>
  309. /// Represents the Image that is displayed in the toolbar.
  310. /// </summary>
  311. public ImageResource Image
  312. {
  313. get
  314. {
  315. return this.toolBarImage;
  316. }
  317. }
  318. public event EventHandler CursorChanging;
  319. protected virtual void OnCursorChanging()
  320. {
  321. if (CursorChanging != null)
  322. {
  323. CursorChanging(this, EventArgs.Empty);
  324. }
  325. }
  326. public event EventHandler CursorChanged;
  327. protected virtual void OnCursorChanged()
  328. {
  329. if (CursorChanged != null)
  330. {
  331. CursorChanged(this, EventArgs.Empty);
  332. }
  333. }
  334. /// <summary>
  335. /// The Cursor that is displayed when this Tool is active and the
  336. /// mouse cursor is inside the document view.
  337. /// </summary>
  338. public Cursor Cursor
  339. {
  340. get
  341. {
  342. return this.cursor;
  343. }
  344. set
  345. {
  346. OnCursorChanging();
  347. this.cursor = value;
  348. OnCursorChanged();
  349. }
  350. }
  351. /// <summary>
  352. /// The name of the Tool. For instance, "Pencil". This name should *not* end in "Tool", e.g. "Pencil Tool"
  353. /// </summary>
  354. public string Name
  355. {
  356. get
  357. {
  358. return this.toolInfo.Name;
  359. }
  360. }
  361. /// <summary>
  362. /// A short description of how to use the tool.
  363. /// </summary>
  364. public string HelpText
  365. {
  366. get
  367. {
  368. return this.toolInfo.HelpText;
  369. }
  370. }
  371. public ToolInfo Info
  372. {
  373. get
  374. {
  375. return this.toolInfo;
  376. }
  377. }
  378. public ToolBarConfigItems ToolBarConfigItems
  379. {
  380. get
  381. {
  382. return this.toolInfo.ToolBarConfigItems;
  383. }
  384. }
  385. /// <summary>
  386. /// Specifies whether or not an inherited tool should take Ink commands
  387. /// </summary>
  388. protected virtual bool SupportsInk
  389. {
  390. get
  391. {
  392. return false;
  393. }
  394. }
  395. public char HotKey
  396. {
  397. get
  398. {
  399. return this.toolInfo.HotKey;
  400. }
  401. }
  402. // Methods to send messages to this class
  403. public void PerformActivate()
  404. {
  405. Activate();
  406. }
  407. public void PerformDeactivate()
  408. {
  409. Deactivate();
  410. }
  411. private bool IsOverflow(MouseEventArgs e)
  412. {
  413. PointF clientPt = DocumentWorkspace.DocumentToClient(new PointF(e.X, e.Y));
  414. return clientPt.X < -16384 || clientPt.Y < -16384;
  415. }
  416. public bool IsMouseEntered
  417. {
  418. get
  419. {
  420. return this.mouseEnter > 0;
  421. }
  422. }
  423. public void PerformMouseEnter()
  424. {
  425. MouseEnter();
  426. }
  427. private void MouseEnter()
  428. {
  429. ++this.mouseEnter;
  430. if (this.mouseEnter == 1)
  431. {
  432. OnMouseEnter();
  433. }
  434. }
  435. protected virtual void OnMouseEnter()
  436. {
  437. }
  438. public void PerformMouseLeave()
  439. {
  440. MouseLeave();
  441. }
  442. private void MouseLeave()
  443. {
  444. if (this.mouseEnter == 1)
  445. {
  446. this.mouseEnter = 0;
  447. OnMouseLeave();
  448. }
  449. else
  450. {
  451. this.mouseEnter = Math.Max(0, this.mouseEnter - 1);
  452. }
  453. }
  454. protected virtual void OnMouseLeave()
  455. {
  456. }
  457. public void PerformMouseMove(MouseEventArgs e)
  458. {
  459. if (IsOverflow(e))
  460. {
  461. return;
  462. }
  463. if (e is StylusEventArgs)
  464. {
  465. if (this.SupportsInk)
  466. {
  467. StylusMove(e as StylusEventArgs);
  468. }
  469. // if the tool does not claim ink support, discard
  470. }
  471. else
  472. {
  473. MouseMove(e);
  474. }
  475. }
  476. public void PerformMouseDown(MouseEventArgs e)
  477. {
  478. if (IsOverflow(e))
  479. {
  480. return;
  481. }
  482. if (e is StylusEventArgs)
  483. {
  484. if (this.SupportsInk)
  485. {
  486. StylusDown(e as StylusEventArgs);
  487. }
  488. // if the tool does not claim ink support, discard
  489. }
  490. else
  491. {
  492. if (this.SupportsInk)
  493. {
  494. DocumentWorkspace.Focus();
  495. }
  496. MouseDown(e);
  497. }
  498. }
  499. public void PerformMouseUp(MouseEventArgs e)
  500. {
  501. if (IsOverflow(e))
  502. {
  503. return;
  504. }
  505. if (e is StylusEventArgs)
  506. {
  507. if (this.SupportsInk)
  508. {
  509. StylusUp(e as StylusEventArgs);
  510. }
  511. // if the tool does not claim ink support, discard
  512. }
  513. else
  514. {
  515. MouseUp(e);
  516. }
  517. }
  518. public void PerformKeyPress(KeyPressEventArgs e)
  519. {
  520. KeyPress(e);
  521. }
  522. public void PerformKeyPress(Keys key)
  523. {
  524. KeyPress(key);
  525. }
  526. public void PerformKeyUp(KeyEventArgs e)
  527. {
  528. KeyUp(e);
  529. }
  530. public void PerformKeyDown(KeyEventArgs e)
  531. {
  532. KeyDown(e);
  533. }
  534. public void PerformClick()
  535. {
  536. Click();
  537. }
  538. public void PerformPulse()
  539. {
  540. Pulse();
  541. }
  542. public void PerformPaste(IDataObject data, out bool handled)
  543. {
  544. Paste(data, out handled);
  545. }
  546. public void PerformPasteQuery(IDataObject data, out bool canHandle)
  547. {
  548. PasteQuery(data, out canHandle);
  549. }
  550. private void Activate()
  551. {
  552. Debug.Assert(this.active != true, "already active!");
  553. this.active = true;
  554. this.handCursor = new Cursor(PdnResources.GetResourceStream("Cursors.PanToolCursor.cur"));
  555. this.handCursorMouseDown = new Cursor(PdnResources.GetResourceStream("Cursors.PanToolCursorMouseDown.cur"));
  556. this.handCursorInvalid = new Cursor(PdnResources.GetResourceStream("Cursors.PanToolCursorInvalid.cur"));
  557. this.panTracking = false;
  558. this.panMode = false;
  559. this.mouseDown = 0;
  560. this.savedTiles = null;
  561. this.saveRegion = null;
  562. this.scratchSurface = DocumentWorkspace.BorrowScratchSurface(this.GetType().Name + ": Tool.Activate()");
  563. Selection.Changing += new EventHandler(SelectionChangingHandler);
  564. Selection.Changed += new EventHandler(SelectionChangedHandler);
  565. this.trackingNub = new MoveNubRenderer(this.RendererList);
  566. this.trackingNub.Visible = false;
  567. this.trackingNub.Size = new SizeF(10, 10);
  568. this.trackingNub.Shape = MoveNubShape.Compass;
  569. this.RendererList.Add(this.trackingNub, false);
  570. OnActivate();
  571. }
  572. void FinishedHistoryStepGroup(object sender, EventArgs e)
  573. {
  574. OnFinishedHistoryStepGroup();
  575. }
  576. protected virtual void OnFinishedHistoryStepGroup()
  577. {
  578. }
  579. /// <summary>
  580. /// This method is called when the tool is being activated; that is, when the
  581. /// user has chosen to use this tool by clicking on it on a toolbar.
  582. /// </summary>
  583. protected virtual void OnActivate()
  584. {
  585. }
  586. private void Deactivate()
  587. {
  588. Debug.Assert(this.active != false, "not active!");
  589. this.active = false;
  590. Selection.Changing -= new EventHandler(SelectionChangingHandler);
  591. Selection.Changed -= new EventHandler(SelectionChangedHandler);
  592. OnDeactivate();
  593. this.RendererList.Remove(this.trackingNub);
  594. this.trackingNub.Dispose();
  595. this.trackingNub = null;
  596. DocumentWorkspace.ReturnScratchSurface(this.scratchSurface);
  597. this.scratchSurface = null;
  598. if (this.saveRegion != null)
  599. {
  600. this.saveRegion.Dispose();
  601. this.saveRegion = null;
  602. }
  603. this.savedTiles = null;
  604. if (this.handCursor != null)
  605. {
  606. this.handCursor.Dispose();
  607. this.handCursor = null;
  608. }
  609. if (this.handCursorMouseDown != null)
  610. {
  611. this.handCursorMouseDown.Dispose();
  612. this.handCursorMouseDown = null;
  613. }
  614. if (this.handCursorInvalid != null)
  615. {
  616. this.handCursorInvalid.Dispose();
  617. this.handCursorInvalid = null;
  618. }
  619. }
  620. /// <summary>
  621. /// This method is called when the tool is being deactivated; that is, when the
  622. /// user has chosen to use another tool by clicking on another tool on a
  623. /// toolbar.
  624. /// </summary>
  625. protected virtual void OnDeactivate()
  626. {
  627. }
  628. private void StylusDown(StylusEventArgs e)
  629. {
  630. if (!this.panMode)
  631. {
  632. OnStylusDown(e);
  633. }
  634. }
  635. protected virtual void OnStylusDown(StylusEventArgs e)
  636. {
  637. }
  638. private void StylusMove(StylusEventArgs e)
  639. {
  640. if (!this.panMode)
  641. {
  642. OnStylusMove(e);
  643. }
  644. }
  645. protected virtual void OnStylusMove(StylusEventArgs e)
  646. {
  647. if (this.mouseDown > 0)
  648. {
  649. ScrollIfNecessary(new PointF(e.X, e.Y));
  650. }
  651. }
  652. private void StylusUp(StylusEventArgs e)
  653. {
  654. if (this.panTracking)
  655. {
  656. this.trackingNub.Visible = false;
  657. this.panTracking = false;
  658. this.Cursor = this.handCursor;
  659. }
  660. else
  661. {
  662. OnStylusUp(e);
  663. }
  664. }
  665. protected virtual void OnStylusUp(StylusEventArgs e)
  666. {
  667. }
  668. private void MouseMove(MouseEventArgs e)
  669. {
  670. if (this.ignoreMouseMove > 0)
  671. {
  672. --this.ignoreMouseMove;
  673. }
  674. else if (this.panTracking && e.Button == MouseButtons.Left)
  675. {
  676. // Pan the document, using Stylus coordinates. This is done in
  677. // MouseMove instead of StylusMove because StylusMove is
  678. // asynchronous, and would not 'feel' right (pan motions would
  679. // stack up)
  680. Point position = new Point(e.X, e.Y);
  681. RectangleF visibleRect = DocumentWorkspace.GetVisibleDocumentRectangleF();
  682. PointF visibleCenterPt = Utility.GetRectangleCenter(visibleRect);
  683. PointF delta = new PointF(e.X - lastPanMouseXY.X, e.Y - lastPanMouseXY.Y);
  684. PointF newScroll = DocumentWorkspace.GetDocumentScrollPositionF();
  685. if (delta.X != 0 || delta.Y != 0)
  686. {
  687. newScroll.X -= delta.X;
  688. newScroll.Y -= delta.Y;
  689. lastPanMouseXY = new Point(e.X, e.Y);
  690. lastPanMouseXY.X -= (int)Math.Truncate(delta.X);
  691. lastPanMouseXY.Y -= (int)Math.Truncate(delta.Y);
  692. ++this.ignoreMouseMove; // setting DocumentScrollPosition incurs a MouseMove event. ignore it prevents 'jittering' at non-integral zoom levels (like, say, 743%)
  693. DocumentWorkspace.SetDocumentScrollPositionF(newScroll);
  694. Update();
  695. }
  696. }
  697. else if (!this.panMode)
  698. {
  699. OnMouseMove(e);
  700. }
  701. this.lastMouseXY = new Point(e.X, e.Y);
  702. this.lastButton = e.Button;
  703. }
  704. /// <summary>
  705. /// This method is called when the Tool is active and the mouse is moving within
  706. /// the document canvas area.
  707. /// </summary>
  708. /// <param name="e">Contains information about where the mouse cursor is, in document coordinates.</param>
  709. protected virtual void OnMouseMove(MouseEventArgs e)
  710. {
  711. if (this.panMode || this.mouseDown > 0)
  712. {
  713. ScrollIfNecessary(new PointF(e.X, e.Y));
  714. }
  715. }
  716. private void MouseDown(MouseEventArgs e)
  717. {
  718. ++this.mouseDown;
  719. if (this.panMode)
  720. {
  721. this.panTracking = true;
  722. this.lastPanMouseXY = new Point(e.X, e.Y);
  723. if (this.CanPan())
  724. {
  725. this.Cursor = this.handCursorMouseDown;
  726. }
  727. }
  728. else
  729. {
  730. OnMouseDown(e);
  731. }
  732. this.lastMouseXY = new Point(e.X, e.Y);
  733. }
  734. /// <summary>
  735. /// This method is called when the Tool is active and a mouse button has been
  736. /// pressed within the document area.
  737. /// </summary>
  738. /// <param name="e">Contains information about where the mouse cursor is, in document coordinates, and which mouse buttons were pressed.</param>
  739. protected virtual void OnMouseDown(MouseEventArgs e)
  740. {
  741. this.lastButton = e.Button;
  742. }
  743. private void MouseUp(MouseEventArgs e)
  744. {
  745. --this.mouseDown;
  746. if (!this.panMode)
  747. {
  748. OnMouseUp(e);
  749. }
  750. this.lastMouseXY = new Point(e.X, e.Y);
  751. }
  752. /// <summary>
  753. /// This method is called when the Tool is active and a mouse button has been
  754. /// released within the document area.
  755. /// </summary>
  756. /// <param name="e">Contains information about where the mouse cursor is, in document coordinates, and which mouse buttons were released.</param>
  757. protected virtual void OnMouseUp(MouseEventArgs e)
  758. {
  759. this.lastButton = e.Button;
  760. }
  761. private void Click()
  762. {
  763. OnClick();
  764. }
  765. /// <summary>
  766. /// This method is called when the Tool is active and a mouse button has been
  767. /// clicked within the document area. If you need more specific information,
  768. /// such as where the mouse was clicked and which button was used, respond to
  769. /// the MouseDown/MouseUp events.
  770. /// </summary>
  771. protected virtual void OnClick()
  772. {
  773. }
  774. private void KeyPress(KeyPressEventArgs e)
  775. {
  776. OnKeyPress(e);
  777. }
  778. private static DateTime lastToolSwitch = DateTime.MinValue;
  779. // if we are pressing 'S' to switch to the selection tools, then consecutive
  780. // presses of 'S' should switch to the next selection tol in the list. however,
  781. // if we wait awhile then pressing 'S' should go to the *first* selection
  782. // tool. 'awhile' is defined by this variable.
  783. private static readonly TimeSpan toolSwitchReset = new TimeSpan(0, 0, 0, 2, 0);
  784. private const char decPenSizeShortcut = '[';
  785. private const char decPenSizeBy5Shortcut = (char)27; // Ctrl [ but must also test that Ctrl is down
  786. private const char incPenSizeShortcut = ']';
  787. private const char incPenSizeBy5Shortcut = (char)29; // Ctrl ] but must also test that Ctrl is down
  788. private const char swapColorsShortcut = 'x';
  789. private const char swapPrimarySecondaryChoice = 'c';
  790. private char[] wildShortcuts = new char[] { ',', '.', '/' };
  791. // Return true if the key is handled, false if not.
  792. protected virtual bool OnWildShortcutKey(int ordinal)
  793. {
  794. return false;
  795. }
  796. /// <summary>
  797. /// This method is called when the tool is active and a keyboard key is pressed
  798. /// and released. If you respond to the keyboard key, set e.Handled to true.
  799. /// </summary>
  800. protected virtual void OnKeyPress(KeyPressEventArgs e)
  801. {
  802. if (!e.Handled && DocumentWorkspace.Focused())
  803. {
  804. int wsIndex = Array.IndexOf<char>(wildShortcuts, e.KeyChar);
  805. if (wsIndex != -1)
  806. {
  807. e.Handled = OnWildShortcutKey(wsIndex);
  808. }
  809. else if (e.KeyChar == swapColorsShortcut)
  810. {
  811. AppWorkspace.SwapUserColors();//Widgets.ColorsForm.
  812. e.Handled = true;
  813. }
  814. else if (e.KeyChar == swapPrimarySecondaryChoice)
  815. {
  816. AppWorkspace.ToggleWhichUserColor();//Widgets.ColorsForm.
  817. e.Handled = true;
  818. }
  819. else if (e.KeyChar == decPenSizeShortcut)
  820. {
  821. AppWorkspace.AddToPenSize(-1.0f);//Widgets.ToolConfigStrip.
  822. e.Handled = true;
  823. }
  824. else if (e.KeyChar == decPenSizeBy5Shortcut && (ModifierKeys & Keys.Control) != 0)
  825. {
  826. AppWorkspace.AddToPenSize(-5.0f);
  827. e.Handled = true;
  828. }
  829. else if (e.KeyChar == incPenSizeShortcut)
  830. {
  831. AppWorkspace.AddToPenSize(+1.0f);
  832. e.Handled = true;
  833. }
  834. else if (e.KeyChar == incPenSizeBy5Shortcut && (ModifierKeys & Keys.Control) != 0)
  835. {
  836. AppWorkspace.AddToPenSize(+5.0f);
  837. e.Handled = true;
  838. }
  839. else
  840. {
  841. ToolInfo[] toolInfos = DocumentWorkspace.GetToolInfos();
  842. Type currentToolType = DocumentWorkspace.GetToolType();
  843. int currentTool = 0;
  844. if (0 != (ModifierKeys & Keys.Shift))
  845. {
  846. Array.Reverse(toolInfos);
  847. }
  848. if (char.ToLower(this.HotKey) != char.ToLower(e.KeyChar) ||
  849. (DateTime.Now - lastToolSwitch) > toolSwitchReset)
  850. {
  851. // If it's been a short time since they pressed a tool switching hotkey,
  852. // we will start from the beginning of this list. This helps to enable two things:
  853. // 1) If multiple tools have the same hotkey, the user may press that hotkey
  854. // to cycle through them
  855. // 2) After a period of time, pressing the hotkey will revert to always
  856. // choosing the first tool in that list of tools which have the same hotkey.
  857. currentTool = -1;
  858. }
  859. else
  860. {
  861. for (int t = 0; t < toolInfos.Length; ++t)
  862. {
  863. if (toolInfos[t].ToolType == currentToolType)
  864. {
  865. currentTool = t;
  866. break;
  867. }
  868. }
  869. }
  870. for (int t = 0; t < toolInfos.Length; ++t)
  871. {
  872. int newTool = (t + currentTool + 1) % toolInfos.Length;
  873. ToolInfo localToolInfo = toolInfos[newTool];
  874. if (localToolInfo.ToolType == DocumentWorkspace.GetToolType() &&
  875. localToolInfo.SkipIfActiveOnHotKey)
  876. {
  877. continue;
  878. }
  879. if (char.ToLower(localToolInfo.HotKey) == char.ToLower(e.KeyChar))
  880. {
  881. if (!this.IsMouseDown)
  882. {
  883. AppWorkspace.SelectTool(localToolInfo.ToolType);//Widgets.ToolsControl.
  884. }
  885. e.Handled = true;
  886. lastToolSwitch = DateTime.Now;
  887. break;
  888. }
  889. }
  890. // If the keypress is still not handled ...
  891. if (!e.Handled)
  892. {
  893. switch (e.KeyChar)
  894. {
  895. // By default, Esc/Enter clear the current selection if there is any
  896. case (char)13: // Enter
  897. case (char)27: // Escape
  898. if (this.mouseDown == 0 && !Selection.IsEmpty)
  899. {
  900. e.Handled = true;
  901. DocumentWorkspace.ExecuteFunction(new DeselectFunction());
  902. }
  903. break;
  904. }
  905. }
  906. }
  907. }
  908. }
  909. private DateTime lastKeyboardMove = DateTime.MinValue;
  910. private Keys lastKey;
  911. private int keyboardMoveSpeed = 1;
  912. private int keyboardMoveRepeats = 0;
  913. private void KeyPress(Keys key)
  914. {
  915. OnKeyPress(key);
  916. }
  917. /// <summary>
  918. /// This method is called when the tool is active and a keyboard key is pressed
  919. /// and released that is not representable with a regular Unicode chararacter.
  920. /// An example would be the arrow keys.
  921. /// </summary>
  922. protected virtual void OnKeyPress(Keys key)
  923. {
  924. Point dir = Point.Empty;
  925. if (key != lastKey)
  926. {
  927. lastKeyboardMove = DateTime.MinValue;
  928. }
  929. lastKey = key;
  930. switch (key)
  931. {
  932. case Keys.Left:
  933. --dir.X;
  934. break;
  935. case Keys.Right:
  936. ++dir.X;
  937. break;
  938. case Keys.Up:
  939. --dir.Y;
  940. break;
  941. case Keys.Down:
  942. ++dir.Y;
  943. break;
  944. }
  945. if (!dir.Equals(Point.Empty))
  946. {
  947. long span = DateTime.Now.Ticks - lastKeyboardMove.Ticks;
  948. if ((span * 4) > TimeSpan.TicksPerSecond)
  949. {
  950. keyboardMoveRepeats = 0;
  951. keyboardMoveSpeed = 1;
  952. }
  953. else
  954. {
  955. keyboardMoveRepeats++;
  956. if (keyboardMoveRepeats > 15 && (keyboardMoveRepeats % 4) == 0)
  957. {
  958. keyboardMoveSpeed++;
  959. }
  960. }
  961. lastKeyboardMove = DateTime.Now;
  962. int offset = (int)(Math.Ceiling(DocumentWorkspace.GetRatio()) * (double)keyboardMoveSpeed);//ScaleFactor.
  963. Cursor.Position = new Point(Cursor.Position.X + offset * dir.X, Cursor.Position.Y + offset * dir.Y);
  964. Point location = DocumentWorkspace.PointToScreenFromTool(Point.Truncate(DocumentWorkspace.DocumentToClient(PointF.Empty)));
  965. PointF stylusLocF = new PointF((float)Cursor.Position.X - (float)location.X, (float)Cursor.Position.Y - (float)location.Y);
  966. Point stylusLoc = new Point(Cursor.Position.X - location.X, Cursor.Position.Y - location.Y);
  967. stylusLoc = DocumentWorkspace.UnscalePoint(stylusLoc);
  968. stylusLocF = DocumentWorkspace.UnscalePoint(stylusLocF);
  969. DocumentWorkspace.PerformDocumentMouseMove(new StylusEventArgs(lastButton, 1, stylusLocF.X, stylusLocF.Y, 0, 1.0f));
  970. DocumentWorkspace.PerformDocumentMouseMove(new MouseEventArgs(lastButton, 1, stylusLoc.X, stylusLoc.Y, 0));
  971. }
  972. }
  973. private bool CanPan()
  974. {
  975. Rectangle vis = Utility.RoundRectangle(DocumentWorkspace.GetVisibleDocumentRectangleF());
  976. vis.Intersect(Document.Bounds);
  977. if (vis == Document.Bounds)
  978. {
  979. return false;
  980. }
  981. else
  982. {
  983. return true;
  984. }
  985. }
  986. private void KeyUp(KeyEventArgs e)
  987. {
  988. if (this.panMode)
  989. {
  990. this.panMode = false;
  991. this.panTracking = false;
  992. this.trackingNub.Visible = false;
  993. this.Cursor = this.panOldCursor;
  994. this.panOldCursor = null;
  995. e.Handled = true;
  996. }
  997. OnKeyUp(e);
  998. }
  999. /// <summary>
  1000. /// This method is called when the tool is active and a keyboard key is pressed.
  1001. /// If you respond to the keyboard key, set e.Handled to true.
  1002. /// </summary>
  1003. protected virtual void OnKeyUp(KeyEventArgs e)
  1004. {
  1005. keysThatAreDown.Clear();
  1006. }
  1007. private void KeyDown(KeyEventArgs e)
  1008. {
  1009. OnKeyDown(e);
  1010. }
  1011. /// <summary>
  1012. /// This method is called when the tool is active and a keyboard key is released
  1013. /// Before responding, check that e.Handled is false, and if you then respond to
  1014. /// the keyboard key, set e.Handled to true.
  1015. /// </summary>
  1016. protected virtual void OnKeyDown(KeyEventArgs e)
  1017. {
  1018. if (!e.Handled)
  1019. {
  1020. if (!keysThatAreDown.Contains(e.KeyData))
  1021. {
  1022. keysThatAreDown.Add(e.KeyData, new KeyTimeInfo());
  1023. }
  1024. if (!this.IsMouseDown &&
  1025. !this.panMode &&
  1026. e.KeyCode == Keys.Space)
  1027. {
  1028. this.panMode = true;
  1029. this.panOldCursor = this.Cursor;
  1030. if (CanPan())
  1031. {
  1032. this.Cursor = this.handCursor;
  1033. }
  1034. else
  1035. {
  1036. this.Cursor = this.handCursorInvalid;
  1037. }
  1038. }
  1039. // arrow keys are processed in another way
  1040. // we get their KeyDown but no KeyUp, so they can not be handled
  1041. // by our normal methods
  1042. OnKeyPress(e.KeyData);
  1043. }
  1044. }
  1045. private void SelectionChanging()
  1046. {
  1047. OnSelectionChanging();
  1048. }
  1049. /// <summary>
  1050. /// This method is called when the Tool is active and the selection area is
  1051. /// about to be changed.
  1052. /// </summary>
  1053. protected virtual void OnSelectionChanging()
  1054. {
  1055. }
  1056. private void SelectionChanged()
  1057. {
  1058. OnSelectionChanged();
  1059. }
  1060. /// <summary>
  1061. /// This method is called when the Tool is active and the selection area has
  1062. /// been changed.
  1063. /// </summary>
  1064. protected virtual void OnSelectionChanged()
  1065. {
  1066. }
  1067. private void ExecutingHistoryMemento(object sender, ExecutingHistoryMementoEventArgs e)
  1068. {
  1069. OnExecutingHistoryMemento(e);
  1070. }
  1071. protected virtual void OnExecutingHistoryMemento(ExecutingHistoryMementoEventArgs e)
  1072. {
  1073. }
  1074. private void ExecutedHistoryMemento(object sender, ExecutedHistoryMementoEventArgs e)
  1075. {
  1076. OnExecutedHistoryMemento(e);
  1077. }
  1078. protected virtual void OnExecutedHistoryMemento(ExecutedHistoryMementoEventArgs e)
  1079. {
  1080. }
  1081. private void PasteQuery(IDataObject data, out bool canHandle)
  1082. {
  1083. OnPasteQuery(data, out canHandle);
  1084. }
  1085. /// <summary>
  1086. /// This method is called when the system is querying a tool as to whether
  1087. /// it can handle a pasted object.
  1088. /// </summary>
  1089. /// <param name="data">
  1090. /// The clipboard data that was pasted by the user that should be inspected.
  1091. /// </param>
  1092. /// <param name="canHandle">
  1093. /// <b>true</b> if the data can be handled by the tool, <b>false</b> if not.
  1094. /// </param>
  1095. /// <remarks>
  1096. /// If you do not set canHandle to <b>true</b> then the tool will not be
  1097. /// able to respond to the Edit menu's Paste item.
  1098. /// </remarks>
  1099. protected virtual void OnPasteQuery(IDataObject data, out bool canHandle)
  1100. {
  1101. canHandle = false;
  1102. }
  1103. private void Paste(IDataObject data, out bool handled)
  1104. {
  1105. OnPaste(data, out handled);
  1106. }
  1107. /// <summary>
  1108. /// This method is called when the user invokes a paste operation. Tools get
  1109. /// the first chance to handle this data.
  1110. /// </summary>
  1111. /// <param name="data">
  1112. /// The data that was pasted by the user.
  1113. /// </param>
  1114. /// <param name="handled">
  1115. /// <b>true</b> if the data was handled and pasted, <b>false</b> if not.
  1116. /// </param>
  1117. /// <remarks>
  1118. /// If you do not set handled to <b>true</b> the event will be passed to the
  1119. /// global paste handler.
  1120. /// </remarks>
  1121. protected virtual void OnPaste(IDataObject data, out bool handled)
  1122. {
  1123. handled = false;
  1124. }
  1125. private void Pulse()
  1126. {
  1127. OnPulse();
  1128. }
  1129. protected bool IsFormActive
  1130. {
  1131. get
  1132. {
  1133. return (object.ReferenceEquals(Form.ActiveForm, DocumentWorkspace.FindForm()));
  1134. }
  1135. }
  1136. /// <summary>
  1137. /// This method is called many times per second, called by the DocumentWorkspace.
  1138. /// </summary>
  1139. protected virtual void OnPulse()
  1140. {
  1141. if (this.panTracking && this.lastButton == MouseButtons.Right)
  1142. {
  1143. Point position = this.lastMouseXY;
  1144. RectangleF visibleRect = DocumentWorkspace.GetVisibleDocumentRectangleF();
  1145. PointF visibleCenterPt = Utility.GetRectangleCenter(visibleRect);
  1146. PointF delta = new PointF(position.X - visibleCenterPt.X, position.Y - visibleCenterPt.Y);
  1147. PointF newScroll = DocumentWorkspace.GetDocumentScrollPositionF();
  1148. this.trackingNub.Visible = true;
  1149. if (delta.X != 0 || delta.Y != 0)
  1150. {
  1151. newScroll.X += delta.X;
  1152. newScroll.Y += delta.Y;
  1153. ++this.ignoreMouseMove; // setting DocumentScrollPosition incurs a MouseMove event. ignore it prevents 'jittering' at non-integral zoom levels (like, say, 743%)
  1154. UI.SuspendControlPainting(DocumentWorkspace.GetThis());
  1155. DocumentWorkspace.SetDocumentScrollPositionF(newScroll);
  1156. this.trackingNub.Visible = true;
  1157. this.trackingNub.Location = Utility.GetRectangleCenter(DocumentWorkspace.GetVisibleDocumentRectangleF());
  1158. UI.ResumeControlPainting(DocumentWorkspace.GetThis());
  1159. DocumentWorkspace.InvalidateFromTool(true);
  1160. Update();
  1161. }
  1162. }
  1163. }
  1164. protected bool ScrollIfNecessary(PointF position)
  1165. {
  1166. if (!autoScroll || !CanPan())
  1167. {
  1168. return false;
  1169. }
  1170. RectangleF visible = DocumentWorkspace.GetVisibleDocumentRectangleF();
  1171. PointF lastScrollPosition = DocumentWorkspace.GetDocumentScrollPositionF();
  1172. PointF delta = PointF.Empty;
  1173. PointF zoomedPoint = PointF.Empty;
  1174. zoomedPoint.X = Utility.Lerp((visible.Left + visible.Right) / 2.0f, position.X, 1.02f);
  1175. zoomedPoint.Y = Utility.Lerp((visible.Top + visible.Bottom) / 2.0f, position.Y, 1.02f);
  1176. if (zoomedPoint.X < visible.Left)
  1177. {
  1178. delta.X = zoomedPoint.X - visible.Left;
  1179. }
  1180. else if (zoomedPoint.X > visible.Right)
  1181. {
  1182. delta.X = zoomedPoint.X - visible.Right;
  1183. }
  1184. if (zoomedPoint.Y < visible.Top)
  1185. {
  1186. delta.Y = zoomedPoint.Y - visible.Top;
  1187. }
  1188. else if (zoomedPoint.Y > visible.Bottom)
  1189. {
  1190. delta.Y = zoomedPoint.Y - visible.Bottom;
  1191. }
  1192. if (!delta.IsEmpty)
  1193. {
  1194. PointF newScrollPosition = new PointF(lastScrollPosition.X + delta.X, lastScrollPosition.Y + delta.Y);
  1195. DocumentWorkspace.SetDocumentScrollPositionF(newScrollPosition);
  1196. Update();
  1197. return true;
  1198. }
  1199. else
  1200. {
  1201. return false;
  1202. }
  1203. }
  1204. private void SelectionChangingHandler(object sender, EventArgs e)
  1205. {
  1206. OnSelectionChanging();
  1207. }
  1208. private void SelectionChangedHandler(object sender, EventArgs e)
  1209. {
  1210. OnSelectionChanged();
  1211. }
  1212. protected void SetStatus(ImageResource statusIcon, string statusText)
  1213. {
  1214. if (statusIcon == null && statusText != null)
  1215. {
  1216. statusIcon = PdnResources.GetImageResource("Icons.MenuHelpHelpTopicsIcon.png");
  1217. }
  1218. DocumentWorkspace.SetStatus(statusText, statusIcon);
  1219. }
  1220. protected SurfaceBoxRendererList RendererList
  1221. {
  1222. get
  1223. {
  1224. return this.DocumentWorkspace.GetRendererList();
  1225. }
  1226. }
  1227. protected void Update()
  1228. {
  1229. DocumentWorkspace.Update();
  1230. }
  1231. protected object GetStaticData()
  1232. {
  1233. return DocumentWorkspace.GetStaticToolData(this.GetType());
  1234. }
  1235. protected void SetStaticData(object data)
  1236. {
  1237. DocumentWorkspace.SetStaticToolData(this.GetType(), data);
  1238. }
  1239. // NOTE: Your constructor must be able to run successfully with a documentWorkspace
  1240. // of null. This is sent in while the DocumentControl static constructor
  1241. // class is building a list of ToolInfo instances, so that it may construct
  1242. // the list without having to also construct a DocumentControl.
  1243. public Tool(IDocumentWorkspace documentWorkspace,
  1244. ImageResource toolBarImage,
  1245. string name,
  1246. string helpText,
  1247. char hotKey,
  1248. bool skipIfActiveOnHotKey,
  1249. ToolBarConfigItems toolBarConfigItems)
  1250. {
  1251. this.documentWorkspace = documentWorkspace;
  1252. this.toolBarImage = toolBarImage;
  1253. this.toolInfo = new ToolInfo(name, helpText, toolBarImage, hotKey, skipIfActiveOnHotKey, toolBarConfigItems, this.GetType());
  1254. if (this.documentWorkspace != null)
  1255. {
  1256. this.documentWorkspace.UpdateStatusBarToToolHelpText(this);
  1257. }
  1258. }
  1259. ~Tool()
  1260. {
  1261. Dispose(false);
  1262. }
  1263. public void Dispose()
  1264. {
  1265. Dispose(true);
  1266. GC.SuppressFinalize(this);
  1267. }
  1268. protected virtual void Dispose(bool disposing)
  1269. {
  1270. Debug.Assert(!this.active, "Tool is still active!");
  1271. if (disposing)
  1272. {
  1273. if (this.saveRegion != null)
  1274. {
  1275. this.saveRegion.Dispose();
  1276. this.saveRegion = null;
  1277. }
  1278. OnDisposed();
  1279. }
  1280. }
  1281. public event EventHandler Disposed;
  1282. private void OnDisposed()
  1283. {
  1284. if (Disposed != null)
  1285. {
  1286. Disposed(this, EventArgs.Empty);
  1287. }
  1288. }
  1289. public Form AssociatedForm
  1290. {
  1291. get
  1292. {
  1293. return AppWorkspace.FindForm();
  1294. }
  1295. }
  1296. }
  1297. }