using System;
using System.Drawing;
using System.Windows.Forms;
namespace Netron.Diagramming.Core {
// ----------------------------------------------------------------------
///
/// This tool implements the standard rectangular selection mechanism.
/// There are two modes (much like Visio).
///
/// - Inclusiveelements are selected if
/// they are contained in the selection rectangle
/// - Touchingelements are selected if the
/// selection rectangle has an overlap with the element
///
///
/// Note that this tool is slightly different than other tools
/// since it activates itself unless it has been suspended by another
/// tool.
///
// ----------------------------------------------------------------------
public class SelectionTool : AbstractTool, IMouseListener {
#region Fields
// ------------------------------------------------------------------
///
/// The location of the mouse when the motion starts.
///
// ------------------------------------------------------------------
private Point initialPoint;
#endregion
#region Constructor
///
/// Initializes a new instance of the class.
///
/// The name of the tool.
public SelectionTool(string name)
: base(name) {
}
#endregion
#region Methods
// ------------------------------------------------------------------
///
/// Called when the tool is activated.
///
// ------------------------------------------------------------------
protected override void OnActivateTool() {
Controller.View.CurrentCursor = CursorPalette.Selection;
}
// ------------------------------------------------------------------
///
/// Handles the mouse down event.
///
/// The
/// instance
/// containing the event data.
// ------------------------------------------------------------------
public bool MouseDown(MouseEventArgs e) {
if (e == null)
throw new ArgumentNullException(
"The argument object is 'null'");
if (e.Button == MouseButtons.Left && Enabled && !IsSuspended) {
if (this.Controller.Model.Selection.SelectedItems.Count == 0 && this.Controller.Model.Selection.Connector == null) {
initialPoint = e.Location;
ActivateTool();
return true;// This tells the tool-loop to stop looking
// for another handler, which keeps the CPU low.
}
}
return false;
}
// ------------------------------------------------------------------
///
/// Handles the mouse move event.
///
/// The
/// instance
/// containing the event data.
// ------------------------------------------------------------------
public void MouseMove(MouseEventArgs e) {
if (e == null)
throw new ArgumentNullException(
"The argument object is 'null'");
IView view = this.Controller.View;
Point point = e.Location;
if (IsActive && !IsSuspended) {
Controller.View.PaintGhostRectangle(initialPoint, point);
Rectangle rec = System.Drawing.Rectangle.Inflate(
Controller.View.Ghost.Rectangle, 20, 20);
Controller.View.Invalidate(rec);
}
}
// ------------------------------------------------------------------
///
/// Handles the mouse up event.
///
/// The
/// instance
/// containing the event data.
// ------------------------------------------------------------------
public void MouseUp(MouseEventArgs e) {
if (IsActive) {
DeactivateTool();
if (Controller.View.Ghost != null) {
this.Controller.Model.Selection.CollectEntitiesInside(
Controller.View.Ghost.Rectangle);//world space
Controller.RaiseOnShowSelectionProperties(
new SelectionEventArgs(
this.Controller.Model.Selection.SelectedItems.ToArray()));
}
Controller.View.ResetGhost();
}
}
#endregion
}
}