1 | using System;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.Windows.Forms;
|
---|
4 |
|
---|
5 | namespace Netron.Diagramming.Core {
|
---|
6 | // ----------------------------------------------------------------------
|
---|
7 | /// <summary>
|
---|
8 | /// This tool implements the standard rectangular selection mechanism.
|
---|
9 | /// There are two modes (much like Visio).
|
---|
10 | /// <list type="bullet">
|
---|
11 | /// <item><term>Inclusive</term><description>elements are selected if
|
---|
12 | /// they are contained in the selection rectangle</description></item>
|
---|
13 | /// <item><term>Touching</term><description>elements are selected if the
|
---|
14 | /// selection rectangle has an overlap with the element</description>
|
---|
15 | /// </item>
|
---|
16 | /// </list>
|
---|
17 | /// <para>Note that this tool is slightly different than other tools
|
---|
18 | /// since it activates itself unless it has been suspended by another
|
---|
19 | /// tool. </para>
|
---|
20 | /// </summary>
|
---|
21 | // ----------------------------------------------------------------------
|
---|
22 | public class SelectionTool : AbstractTool, IMouseListener {
|
---|
23 | #region Fields
|
---|
24 |
|
---|
25 | // ------------------------------------------------------------------
|
---|
26 | /// <summary>
|
---|
27 | /// The location of the mouse when the motion starts.
|
---|
28 | /// </summary>
|
---|
29 | // ------------------------------------------------------------------
|
---|
30 | private Point initialPoint;
|
---|
31 |
|
---|
32 | #endregion
|
---|
33 |
|
---|
34 | #region Constructor
|
---|
35 | /// <summary>
|
---|
36 | /// Initializes a new instance of the <see cref="T:SelectionTool"/> class.
|
---|
37 | /// </summary>
|
---|
38 | /// <param name="name">The name of the tool.</param>
|
---|
39 | public SelectionTool(string name)
|
---|
40 | : base(name) {
|
---|
41 | }
|
---|
42 | #endregion
|
---|
43 |
|
---|
44 | #region Methods
|
---|
45 |
|
---|
46 | // ------------------------------------------------------------------
|
---|
47 | /// <summary>
|
---|
48 | /// Called when the tool is activated.
|
---|
49 | /// </summary>
|
---|
50 | // ------------------------------------------------------------------
|
---|
51 | protected override void OnActivateTool() {
|
---|
52 | Controller.View.CurrentCursor = CursorPalette.Selection;
|
---|
53 | }
|
---|
54 |
|
---|
55 | // ------------------------------------------------------------------
|
---|
56 | /// <summary>
|
---|
57 | /// Handles the mouse down event.
|
---|
58 | /// </summary>
|
---|
59 | /// <param name="e">The
|
---|
60 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
61 | /// containing the event data.</param>
|
---|
62 | // ------------------------------------------------------------------
|
---|
63 | public bool MouseDown(MouseEventArgs e) {
|
---|
64 | if (e == null)
|
---|
65 | throw new ArgumentNullException(
|
---|
66 | "The argument object is 'null'");
|
---|
67 |
|
---|
68 | if (e.Button == MouseButtons.Left && Enabled && !IsSuspended) {
|
---|
69 | if (this.Controller.Model.Selection.SelectedItems.Count == 0 && this.Controller.Model.Selection.Connector == null) {
|
---|
70 | initialPoint = e.Location;
|
---|
71 | ActivateTool();
|
---|
72 | return true;// This tells the tool-loop to stop looking
|
---|
73 | // for another handler, which keeps the CPU low.
|
---|
74 | }
|
---|
75 | }
|
---|
76 | return false;
|
---|
77 | }
|
---|
78 |
|
---|
79 | // ------------------------------------------------------------------
|
---|
80 | /// <summary>
|
---|
81 | /// Handles the mouse move event.
|
---|
82 | /// </summary>
|
---|
83 | /// <param name="e">The
|
---|
84 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
85 | /// containing the event data.</param>
|
---|
86 | // ------------------------------------------------------------------
|
---|
87 | public void MouseMove(MouseEventArgs e) {
|
---|
88 | if (e == null)
|
---|
89 | throw new ArgumentNullException(
|
---|
90 | "The argument object is 'null'");
|
---|
91 | IView view = this.Controller.View;
|
---|
92 | Point point = e.Location;
|
---|
93 | if (IsActive && !IsSuspended) {
|
---|
94 | Controller.View.PaintGhostRectangle(initialPoint, point);
|
---|
95 | Rectangle rec = System.Drawing.Rectangle.Inflate(
|
---|
96 | Controller.View.Ghost.Rectangle, 20, 20);
|
---|
97 |
|
---|
98 | Controller.View.Invalidate(rec);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | // ------------------------------------------------------------------
|
---|
103 | /// <summary>
|
---|
104 | /// Handles the mouse up event.
|
---|
105 | /// </summary>
|
---|
106 | /// <param name="e">The
|
---|
107 | /// <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance
|
---|
108 | /// containing the event data.</param>
|
---|
109 | // ------------------------------------------------------------------
|
---|
110 | public void MouseUp(MouseEventArgs e) {
|
---|
111 | if (IsActive) {
|
---|
112 | DeactivateTool();
|
---|
113 | if (Controller.View.Ghost != null) {
|
---|
114 | this.Controller.Model.Selection.CollectEntitiesInside(
|
---|
115 | Controller.View.Ghost.Rectangle);//world space
|
---|
116 |
|
---|
117 | Controller.RaiseOnShowSelectionProperties(
|
---|
118 | new SelectionEventArgs(
|
---|
119 | this.Controller.Model.Selection.SelectedItems.ToArray()));
|
---|
120 | }
|
---|
121 | Controller.View.ResetGhost();
|
---|
122 | }
|
---|
123 | }
|
---|
124 | #endregion
|
---|
125 | }
|
---|
126 |
|
---|
127 | }
|
---|