using System;
using System.Drawing;
using System.Windows.Forms;
namespace Netron.Diagramming.Core {
// ----------------------------------------------------------------------
///
/// Allows the user to zoom in to a user-drawn rectangle area.
///
// ----------------------------------------------------------------------
public class ZoomAreaTool : AbstractTool, IMouseListener {
#region Fields
// ------------------------------------------------------------------
///
/// The location of the mouse when the motion starts.
///
// ------------------------------------------------------------------
protected Point initialPoint;
// ------------------------------------------------------------------
///
/// Says whether the startingPoint was set, otherwise the ghost will
/// appear even before an initial point was set!
///
// ------------------------------------------------------------------
protected bool started;
#endregion
// ------------------------------------------------------------------
///
/// Constructor.
///
/// string: The name of the tool.
// ------------------------------------------------------------------
public ZoomAreaTool(string toolName)
: base(toolName) {
}
#region Methods
// ------------------------------------------------------------------
///
/// Called when the tool is activated.
///
// ------------------------------------------------------------------
protected override void OnActivateTool() {
base.OnActivateTool();
Controller.View.CurrentCursor = CursorPalette.Cross;
}
// ------------------------------------------------------------------
///
/// 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 && IsActive && !IsSuspended) {
initialPoint = e.Location;
started = true;
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'");
}
if (IsActive && !IsSuspended && started) {
IView view = this.Controller.View;
Point point = e.Location;
Controller.View.PaintGhostRectangle(initialPoint, point);
Rectangle area = System.Drawing.Rectangle.Inflate(
Controller.View.Ghost.Rectangle, 20, 20);
Controller.View.Invalidate(area);
}
}
// ------------------------------------------------------------------
///
/// Handles the mouse up event.
///
/// The
/// instance
/// containing the event data.
// ------------------------------------------------------------------
public void MouseUp(MouseEventArgs e) {
if (IsActive) {
IView view = Controller.View;
if (view.Ghost != null) {
Rectangle zoomArea = view.Ghost.Rectangle;
view.ZoomArea(zoomArea);
started = false;
}
Controller.View.ResetGhost();
}
}
#endregion
}
}