1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Collections;
|
---|
4 | using System.Drawing;
|
---|
5 | using System.Windows.Forms;
|
---|
6 | using System.IO;
|
---|
7 | using System.Text;
|
---|
8 |
|
---|
9 | namespace Netron.Diagramming.Core
|
---|
10 | {
|
---|
11 | // ----------------------------------------------------------------------
|
---|
12 | /// <summary>
|
---|
13 | /// This tool implements the action of moving shapes on the canvas.
|
---|
14 | /// <para>Note that this tool is slightly different than other tools
|
---|
15 | /// since it activates itself unless it has been suspended by another
|
---|
16 | /// tool. </para>
|
---|
17 | /// </summary>
|
---|
18 | public class DragDropTool :
|
---|
19 | AbstractTool,
|
---|
20 | IMouseListener,
|
---|
21 | IDragDropListener
|
---|
22 | {
|
---|
23 |
|
---|
24 | #region Fields
|
---|
25 | Cursor feedbackCursor;
|
---|
26 | #endregion
|
---|
27 |
|
---|
28 | #region Constructor
|
---|
29 | /// <summary>
|
---|
30 | /// Initializes a new instance of the <see cref="T:DragDropTool"/> class.
|
---|
31 | /// </summary>
|
---|
32 | /// <param name="name">The name of the tool.</param>
|
---|
33 | public DragDropTool(string name) : base(name)
|
---|
34 | {
|
---|
35 | }
|
---|
36 | #endregion
|
---|
37 |
|
---|
38 | #region Methods
|
---|
39 |
|
---|
40 | /// <summary>
|
---|
41 | /// Called when the tool is activated.
|
---|
42 | /// </summary>
|
---|
43 | protected override void OnActivateTool()
|
---|
44 | {
|
---|
45 | Controller.View.CurrentCursor = Cursors.SizeAll;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /// <summary>
|
---|
49 | /// Handles the mouse down event
|
---|
50 | /// </summary>
|
---|
51 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
52 | public bool MouseDown(MouseEventArgs e)
|
---|
53 | {
|
---|
54 | return false; //continue spreading the events
|
---|
55 | }
|
---|
56 |
|
---|
57 | /// <summary>
|
---|
58 | /// Handles the mouse move event
|
---|
59 | /// </summary>
|
---|
60 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
61 | public void MouseMove(MouseEventArgs e)
|
---|
62 | {
|
---|
63 |
|
---|
64 | }
|
---|
65 | /// <summary>
|
---|
66 | /// Handles the mouse up event
|
---|
67 | /// </summary>
|
---|
68 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
69 | public void MouseUp(MouseEventArgs e)
|
---|
70 | {
|
---|
71 |
|
---|
72 | }
|
---|
73 | #endregion
|
---|
74 |
|
---|
75 | /// <summary>
|
---|
76 | /// On dragdrop.
|
---|
77 | /// </summary>
|
---|
78 | /// <param name="e">The <see cref="T:KeyEventArgs"/> instance containing the event data.</param>
|
---|
79 | public bool OnDragDrop(DragEventArgs e)
|
---|
80 | {
|
---|
81 | Control control = (Control)this.Controller.ParentControl;
|
---|
82 | Point p = control.PointToClient(new Point(e.X, e.Y));
|
---|
83 | IShape shape = null;
|
---|
84 | IDataObject iDataObject = e.Data;
|
---|
85 | string text;
|
---|
86 |
|
---|
87 | if (iDataObject.GetDataPresent("IShape"))
|
---|
88 | {
|
---|
89 | shape = (IShape)iDataObject.GetData("IShape");
|
---|
90 | shape.Model = Controller.Model;
|
---|
91 | }
|
---|
92 | else if (iDataObject.GetDataPresent(typeof(string)))
|
---|
93 | {
|
---|
94 | text = iDataObject.GetData(
|
---|
95 | typeof(string)).ToString();
|
---|
96 |
|
---|
97 | //foreach (string shapeType in Enum.GetNames(typeof(ShapeTypes)))
|
---|
98 | //{
|
---|
99 | // if (shapeType.ToString().ToLower() == text.ToLower())
|
---|
100 | // {
|
---|
101 | // shape = ShapeFactory.GetShape(shapeType);
|
---|
102 | // break;
|
---|
103 | // }
|
---|
104 | //}
|
---|
105 |
|
---|
106 | // If our shape is still null, then the text being dragged
|
---|
107 | // onto the canvas is not the name of a shape, so add a
|
---|
108 | // TextOnly shape with the text.
|
---|
109 | //if (shape == null)
|
---|
110 | //{
|
---|
111 | // shape = new TextOnly(Controller.Model);
|
---|
112 | // (shape as TextOnly).Text = text;
|
---|
113 | //}
|
---|
114 |
|
---|
115 | shape = new TextOnly(Controller.Model);
|
---|
116 | (shape as TextOnly).Text = text;
|
---|
117 | }
|
---|
118 | else if (iDataObject.GetDataPresent(DataFormats.FileDrop))
|
---|
119 | {
|
---|
120 | try
|
---|
121 | {
|
---|
122 | string[] files = (string[])iDataObject.GetData(
|
---|
123 | DataFormats.FileDrop);
|
---|
124 |
|
---|
125 | foreach (string fileName in files)
|
---|
126 | {
|
---|
127 | FileInfo info = new FileInfo(fileName);
|
---|
128 |
|
---|
129 | if (info.Exists)
|
---|
130 | {
|
---|
131 | FileShape fileShape = new FileShape(fileName);
|
---|
132 | AddShapeCommand cmd = new AddShapeCommand(
|
---|
133 | this.Controller,
|
---|
134 | fileShape,
|
---|
135 | p);
|
---|
136 | this.Controller.UndoManager.AddUndoCommand(cmd);
|
---|
137 | cmd.Redo();
|
---|
138 | p.Offset(20, 20);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | feedbackCursor = null;
|
---|
143 | }
|
---|
144 | catch
|
---|
145 | {
|
---|
146 | }
|
---|
147 | }
|
---|
148 | else if (iDataObject.GetDataPresent(typeof(Bitmap)))
|
---|
149 | {
|
---|
150 | // Doesn't support dropping images yet - having problems
|
---|
151 | // getting images off the clipboard.
|
---|
152 | shape = new ImageShape(Controller.Model);
|
---|
153 | (shape as ImageShape).Image =
|
---|
154 | (Image)iDataObject.GetData(typeof(Bitmap));
|
---|
155 | return true;
|
---|
156 | }
|
---|
157 |
|
---|
158 | if(shape != null)
|
---|
159 | {
|
---|
160 | shape.MoveBy(new Point(p.X, p.Y));
|
---|
161 |
|
---|
162 |
|
---|
163 | //ComplexRectangle shape = new ComplexRectangle();
|
---|
164 | //shape.Rectangle = new Rectangle(p.X, p.Y, 150, 70);
|
---|
165 | //shape.Text = "Just an example, work in progress.";
|
---|
166 |
|
---|
167 | //TextLabel shape = new TextLabel();
|
---|
168 | //shape.Rectangle = new Rectangle(p.X, p.Y, 150, 70);
|
---|
169 | //shape.Text = "Just an example, work in progress.";
|
---|
170 |
|
---|
171 | AddShapeCommand cmd = new AddShapeCommand(
|
---|
172 | this.Controller,
|
---|
173 | shape,
|
---|
174 | p);
|
---|
175 | this.Controller.UndoManager.AddUndoCommand(cmd);
|
---|
176 | cmd.Redo();
|
---|
177 | feedbackCursor = null;
|
---|
178 | return true;
|
---|
179 | }
|
---|
180 |
|
---|
181 | // The drop event wasn't handled here.
|
---|
182 | return false;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /// <summary>
|
---|
186 | /// On drag leave.
|
---|
187 | /// </summary>
|
---|
188 | /// <param name="e">The <see cref="T:KeyEventArgs"/> instance containing the event data.</param>
|
---|
189 | public bool OnDragLeave(EventArgs e)
|
---|
190 | {
|
---|
191 | return false;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /// <summary>
|
---|
195 | /// On drag over.
|
---|
196 | /// </summary>
|
---|
197 | /// <param name="e">The <see cref="T:KeyPressEventArgs"/> instance containing the event data.</param>
|
---|
198 | public bool OnDragOver(DragEventArgs e)
|
---|
199 | {
|
---|
200 | return AnalyzeData(e);
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | /// <summary>
|
---|
205 | /// On drag enter
|
---|
206 | /// </summary>
|
---|
207 | /// <param name="e"></param>
|
---|
208 | public bool OnDragEnter(DragEventArgs e)
|
---|
209 | {
|
---|
210 | return AnalyzeData(e);
|
---|
211 | }
|
---|
212 |
|
---|
213 | private bool AnalyzeData(DragEventArgs e)
|
---|
214 | {
|
---|
215 | IDataObject iDataObject = e.Data;
|
---|
216 |
|
---|
217 | if (iDataObject.GetDataPresent("IShape"))
|
---|
218 | {
|
---|
219 | feedbackCursor = CursorPalette.DropShape;
|
---|
220 | e.Effect = DragDropEffects.Copy;
|
---|
221 | return true;
|
---|
222 | }
|
---|
223 |
|
---|
224 | if(iDataObject.GetDataPresent(typeof(string)))
|
---|
225 | {
|
---|
226 | // Need to determine if the text on the clipboard is the
|
---|
227 | // name of a shape or if it's just plain old text to add.
|
---|
228 | //foreach(string shapeType in Enum.GetNames(typeof(ShapeTypes)))
|
---|
229 | //{
|
---|
230 | // if(shapeType.ToString().ToLower() ==
|
---|
231 | // iDataObject.GetData(
|
---|
232 | // typeof(string)).ToString().ToLower())
|
---|
233 | // {
|
---|
234 | // feedbackCursor = CursorPalette.DropShape;
|
---|
235 | // e.Effect = DragDropEffects.Copy;
|
---|
236 | // return true;
|
---|
237 | // }
|
---|
238 | //}
|
---|
239 | feedbackCursor = CursorPalette.DropText;
|
---|
240 | e.Effect = DragDropEffects.Copy;
|
---|
241 | return true;
|
---|
242 | }
|
---|
243 |
|
---|
244 | if (iDataObject.GetDataPresent(DataFormats.FileDrop))
|
---|
245 | {
|
---|
246 | feedbackCursor = CursorPalette.Add;
|
---|
247 | e.Effect = DragDropEffects.Copy;
|
---|
248 | return true;
|
---|
249 | }
|
---|
250 |
|
---|
251 | // Having problems getting images off the clipboard.
|
---|
252 | //if(iDataObject.GetDataPresent(typeof(Bitmap)))
|
---|
253 | //{
|
---|
254 | // feedbackCursor = CursorPallet.DropImage;
|
---|
255 | // e.Effect = DragDropEffects.Copy;
|
---|
256 | // return;
|
---|
257 |
|
---|
258 | //}
|
---|
259 | return false;
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | public void GiveFeedback(GiveFeedbackEventArgs e)
|
---|
264 | {
|
---|
265 | e.UseDefaultCursors = false;
|
---|
266 | Cursor.Current = feedbackCursor;
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | }
|
---|