1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text;
|
---|
4 | using System.Windows.Forms;
|
---|
5 | using System.Drawing;
|
---|
6 |
|
---|
7 | namespace Netron.Diagramming.Core
|
---|
8 | {
|
---|
9 | // ----------------------------------------------------------------------
|
---|
10 | /// <summary>
|
---|
11 | /// Allows the user to zoom in to a user-drawn rectangle area.
|
---|
12 | /// </summary>
|
---|
13 | // ----------------------------------------------------------------------
|
---|
14 | public class ZoomAreaTool : AbstractTool, IMouseListener
|
---|
15 | {
|
---|
16 | #region Fields
|
---|
17 |
|
---|
18 | // ------------------------------------------------------------------
|
---|
19 | /// <summary>
|
---|
20 | /// The location of the mouse when the motion starts.
|
---|
21 | /// </summary>
|
---|
22 | // ------------------------------------------------------------------
|
---|
23 | protected Point initialPoint;
|
---|
24 |
|
---|
25 | // ------------------------------------------------------------------
|
---|
26 | /// <summary>
|
---|
27 | /// Says whether the startingPoint was set, otherwise the ghost will
|
---|
28 | /// appear even before an initial point was set!
|
---|
29 | /// </summary>
|
---|
30 | // ------------------------------------------------------------------
|
---|
31 | protected bool started;
|
---|
32 |
|
---|
33 | #endregion
|
---|
34 |
|
---|
35 | // ------------------------------------------------------------------
|
---|
36 | /// <summary>
|
---|
37 | /// Constructor.
|
---|
38 | /// </summary>
|
---|
39 | /// <param name="toolName">string: The name of the tool.</param>
|
---|
40 | // ------------------------------------------------------------------
|
---|
41 | public ZoomAreaTool(string toolName)
|
---|
42 | : base(toolName)
|
---|
43 | {
|
---|
44 | }
|
---|
45 |
|
---|
46 | #region Methods
|
---|
47 |
|
---|
48 | // ------------------------------------------------------------------
|
---|
49 | /// <summary>
|
---|
50 | /// Called when the tool is activated.
|
---|
51 | /// </summary>
|
---|
52 | // ------------------------------------------------------------------
|
---|
53 | protected override void OnActivateTool()
|
---|
54 | {
|
---|
55 | base.OnActivateTool();
|
---|
56 | Controller.View.CurrentCursor = CursorPalette.Cross;
|
---|
57 | }
|
---|
58 |
|
---|
59 | // ------------------------------------------------------------------
|
---|
60 | /// <summary>
|
---|
61 | /// Handles the mouse down event
|
---|
62 | /// </summary>
|
---|
63 | /// <param name="e">The
|
---|
64 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
65 | /// containing the event data.</param>
|
---|
66 | // ------------------------------------------------------------------
|
---|
67 | public bool MouseDown(MouseEventArgs e)
|
---|
68 | {
|
---|
69 | if (e == null)
|
---|
70 | {
|
---|
71 | throw new ArgumentNullException(
|
---|
72 | "The argument object is 'null'");
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (e.Button == MouseButtons.Left && IsActive && !IsSuspended)
|
---|
76 | {
|
---|
77 | initialPoint = e.Location;
|
---|
78 | started = true;
|
---|
79 | return true; // This tells the tool-loop to stop looking
|
---|
80 | // for another handler, which keeps the CPU low.
|
---|
81 |
|
---|
82 | }
|
---|
83 | return false;
|
---|
84 | }
|
---|
85 |
|
---|
86 | // ------------------------------------------------------------------
|
---|
87 | /// <summary>
|
---|
88 | /// Handles the mouse move event
|
---|
89 | /// </summary>
|
---|
90 | /// <param name="e">The
|
---|
91 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
92 | /// containing the event data.</param>
|
---|
93 | // ------------------------------------------------------------------
|
---|
94 | public void MouseMove(MouseEventArgs e)
|
---|
95 | {
|
---|
96 | if (e == null)
|
---|
97 | {
|
---|
98 | throw new ArgumentNullException(
|
---|
99 | "The argument object is 'null'");
|
---|
100 | }
|
---|
101 | if (IsActive && !IsSuspended && started)
|
---|
102 | {
|
---|
103 | IView view = this.Controller.View;
|
---|
104 | Point point = e.Location;
|
---|
105 |
|
---|
106 | Controller.View.PaintGhostRectangle(initialPoint, point);
|
---|
107 | Rectangle area = System.Drawing.Rectangle.Inflate(
|
---|
108 | Controller.View.Ghost.Rectangle, 20, 20);
|
---|
109 |
|
---|
110 | Controller.View.Invalidate(area);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | // ------------------------------------------------------------------
|
---|
115 | /// <summary>
|
---|
116 | /// Handles the mouse up event.
|
---|
117 | /// </summary>
|
---|
118 | /// <param name="e">The
|
---|
119 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
120 | /// containing the event data.</param>
|
---|
121 | // ------------------------------------------------------------------
|
---|
122 | public void MouseUp(MouseEventArgs e)
|
---|
123 | {
|
---|
124 | if (IsActive)
|
---|
125 | {
|
---|
126 | DeactivateTool();
|
---|
127 | IView view = Controller.View;
|
---|
128 | if (view.Ghost != null)
|
---|
129 | {
|
---|
130 | Rectangle zoomArea = view.Ghost.Rectangle;
|
---|
131 | view.ZoomArea(zoomArea);
|
---|
132 | started = false;
|
---|
133 | }
|
---|
134 | Controller.View.ResetGhost();
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | #endregion
|
---|
139 | }
|
---|
140 | }
|
---|