Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Tools/SelectionTool.cs @ 2768

Last change on this file since 2768 was 2768, checked in by mkommend, 14 years ago

added solution folders and sources for the netron library (ticket #867)

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