1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Collections;
|
---|
4 | using System.Drawing;
|
---|
5 | using System.Windows.Forms;
|
---|
6 |
|
---|
7 | namespace Netron.Diagramming.Core
|
---|
8 | {
|
---|
9 | // ----------------------------------------------------------------------
|
---|
10 | /// <summary>
|
---|
11 | /// This tool implement the action of hitting an entity on the canvas.
|
---|
12 | /// </summary>
|
---|
13 | // ----------------------------------------------------------------------
|
---|
14 | public class HitTool : AbstractTool, IMouseListener, IKeyboardListener
|
---|
15 | {
|
---|
16 | #region Fields
|
---|
17 |
|
---|
18 | // ------------------------------------------------------------------
|
---|
19 | /// <summary>
|
---|
20 | /// Specifies if one of the multi-select keys are pressed. If this
|
---|
21 | /// is false, then the current Selection is cleared before adding
|
---|
22 | /// any selected entities to the Selection.
|
---|
23 | /// </summary>
|
---|
24 | // ------------------------------------------------------------------
|
---|
25 | bool isMultiSelectKeyPressed = false;
|
---|
26 |
|
---|
27 | // ------------------------------------------------------------------
|
---|
28 | /// <summary>
|
---|
29 | /// The keyboard keys that enable/disable multi-selection.
|
---|
30 | /// </summary>
|
---|
31 | // ------------------------------------------------------------------
|
---|
32 | Keys[] myMultiSelectKeys = new Keys[]
|
---|
33 | {
|
---|
34 | Keys.ShiftKey,
|
---|
35 | Keys.ControlKey
|
---|
36 | };
|
---|
37 |
|
---|
38 | #endregion
|
---|
39 |
|
---|
40 | #region Constructor
|
---|
41 |
|
---|
42 | // ------------------------------------------------------------------
|
---|
43 | /// <summary>
|
---|
44 | /// Initializes a new instance of the <see cref="T:HitTool"/> class.
|
---|
45 | /// </summary>
|
---|
46 | /// <param name="name">The name of the tool.</param>
|
---|
47 | // ------------------------------------------------------------------
|
---|
48 | public HitTool(string name)
|
---|
49 | : base(name)
|
---|
50 | {
|
---|
51 | }
|
---|
52 | #endregion
|
---|
53 |
|
---|
54 | #region Methods
|
---|
55 |
|
---|
56 | // ------------------------------------------------------------------
|
---|
57 | /// <summary>
|
---|
58 | /// Called when the tool is activated.
|
---|
59 | /// </summary>
|
---|
60 | // ------------------------------------------------------------------
|
---|
61 | protected override void OnActivateTool()
|
---|
62 | {
|
---|
63 | }
|
---|
64 |
|
---|
65 | // ------------------------------------------------------------------
|
---|
66 | /// <summary>
|
---|
67 | /// Handles the mouse down event
|
---|
68 | /// </summary>
|
---|
69 | /// <param name="e">The
|
---|
70 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
71 | /// containing the event data.</param>
|
---|
72 | // ------------------------------------------------------------------
|
---|
73 | public virtual bool MouseDown(MouseEventArgs e)
|
---|
74 | {
|
---|
75 | if (e == null)
|
---|
76 | {
|
---|
77 | throw new ArgumentNullException("The argument object is 'null'");
|
---|
78 | }
|
---|
79 |
|
---|
80 | //if(e.Button == MouseButtons.Left && Enabled && !IsSuspended)
|
---|
81 | if (Enabled && !IsSuspended)
|
---|
82 | {
|
---|
83 | // Only if one of the multi-select keys are pressed do we NOT
|
---|
84 | // clear the selection before adding to it the selected entity.
|
---|
85 | bool clearSelectionFirst = true;
|
---|
86 | if (this.isMultiSelectKeyPressed == true)
|
---|
87 | {
|
---|
88 | clearSelectionFirst = false;
|
---|
89 | }
|
---|
90 |
|
---|
91 | // Also don't clear the selection if a group is currently
|
---|
92 | // selected so we can drill-down into it.
|
---|
93 | if (Selection.SelectedItems.Count > 0)
|
---|
94 | {
|
---|
95 | foreach (IDiagramEntity entity in Selection.SelectedItems)
|
---|
96 | {
|
---|
97 | if ( (entity is IGroup) &&
|
---|
98 | (entity.Hit(e.Location) == true) )
|
---|
99 | {
|
---|
100 | clearSelectionFirst = false;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | Selection.CollectEntitiesAt(e.Location, clearSelectionFirst);
|
---|
105 |
|
---|
106 | if (Selection.SelectedItems.Count > 0)
|
---|
107 | {
|
---|
108 | IMouseListener listener =
|
---|
109 | Selection.SelectedItems[0].GetService(
|
---|
110 | typeof(IMouseListener)) as IMouseListener;
|
---|
111 |
|
---|
112 | if (listener != null)
|
---|
113 | {
|
---|
114 | if (listener.MouseDown(e))
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | if ( (Selection.SelectedItems.Count > 0) &&
|
---|
120 | (Selection.SelectedItems[0] is ITextProvider) )
|
---|
121 | {
|
---|
122 | //ActivateTool();
|
---|
123 | ITextProvider textProvider =
|
---|
124 | Selection.SelectedItems[0] as ITextProvider;
|
---|
125 |
|
---|
126 | if ((e.Clicks == textProvider.EditTextClicks) &&
|
---|
127 | (textProvider.AllowTextEditing))
|
---|
128 | {
|
---|
129 | return Controller.ActivateTextEditor(textProvider);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | return false;
|
---|
134 | }
|
---|
135 |
|
---|
136 | // ------------------------------------------------------------------
|
---|
137 | /// <summary>
|
---|
138 | /// Handles the mouse move event.
|
---|
139 | /// </summary>
|
---|
140 | /// <param name="e">The
|
---|
141 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
142 | /// containing the event data.</param>
|
---|
143 | // ------------------------------------------------------------------
|
---|
144 | public virtual void MouseMove(MouseEventArgs e)
|
---|
145 | {
|
---|
146 |
|
---|
147 | }
|
---|
148 |
|
---|
149 | // ------------------------------------------------------------------
|
---|
150 | /// <summary>
|
---|
151 | /// Handles the mouse up event.
|
---|
152 | /// </summary>
|
---|
153 | /// <param name="e">The
|
---|
154 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
155 | /// containing the event data.</param>
|
---|
156 | // ------------------------------------------------------------------
|
---|
157 | public virtual void MouseUp(MouseEventArgs e)
|
---|
158 | {
|
---|
159 | if(IsActive)
|
---|
160 | {
|
---|
161 | DeactivateTool();
|
---|
162 | }
|
---|
163 | }
|
---|
164 | #endregion
|
---|
165 |
|
---|
166 | #region IKeyboardListener Members
|
---|
167 |
|
---|
168 | // ------------------------------------------------------------------
|
---|
169 | /// <summary>
|
---|
170 | /// Handles the key down event. If the key pressed is one of our
|
---|
171 | /// keys defined as a multi-select key, then selected items are
|
---|
172 | /// appended to the Selection (i.e. the Selection is not cleared
|
---|
173 | /// first).
|
---|
174 | /// </summary>
|
---|
175 | /// <param name="e">The
|
---|
176 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
177 | /// containing the event data.</param>
|
---|
178 | // ------------------------------------------------------------------
|
---|
179 | public void KeyDown(KeyEventArgs e)
|
---|
180 | {
|
---|
181 | foreach (Keys k in this.myMultiSelectKeys)
|
---|
182 | {
|
---|
183 | if (e.KeyCode == k)
|
---|
184 | {
|
---|
185 | this.isMultiSelectKeyPressed = true;
|
---|
186 | return;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | // ------------------------------------------------------------------
|
---|
192 | /// <summary>
|
---|
193 | /// Handles the key up event. If the key pressed is one of our
|
---|
194 | /// keys defined as a multi-select key, then multi-select mode is
|
---|
195 | /// disabled.
|
---|
196 | /// </summary>
|
---|
197 | /// <param name="e">The
|
---|
198 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
199 | /// containing the event data.</param>
|
---|
200 | // ------------------------------------------------------------------
|
---|
201 | public void KeyUp(KeyEventArgs e)
|
---|
202 | {
|
---|
203 | foreach (Keys k in this.myMultiSelectKeys)
|
---|
204 | {
|
---|
205 | if (e.KeyCode == k)
|
---|
206 | {
|
---|
207 | this.isMultiSelectKeyPressed = false;
|
---|
208 | return;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | // ------------------------------------------------------------------
|
---|
214 | /// <summary>
|
---|
215 | /// Handles the key press event. Nothing is performed here.
|
---|
216 | /// </summary>
|
---|
217 | /// <param name="e">The
|
---|
218 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
219 | /// containing the event data.</param>
|
---|
220 | // ------------------------------------------------------------------
|
---|
221 | public void KeyPress(KeyPressEventArgs e)
|
---|
222 | {}
|
---|
223 |
|
---|
224 | #endregion
|
---|
225 | }
|
---|
226 |
|
---|
227 | }
|
---|