Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Tools/DragDropTool.cs @ 16565

Last change on this file since 16565 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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