using System.Collections;
using System.Windows.Forms;
namespace Netron.Diagramming.Core {
// ----------------------------------------------------------------------
///
/// This tool allows entities to give feedback (e.g. via a change of
/// cursor) when the mouse is hovering over them.
///
// ----------------------------------------------------------------------
class HoverTool : AbstractTool, IMouseListener {
#region Fields
private IHoverListener currentListener = null;
private IDiagramEntity previousHovered = null;
#endregion
#region Constructor
// ------------------------------------------------------------------
///
/// Initializes a new instance of the class.
///
/// The name of the tool.
// ------------------------------------------------------------------
public HoverTool(string name)
: base(name) {
}
#endregion
#region Methods
// ------------------------------------------------------------------
///
/// Called when the tool is activated.
///
// ------------------------------------------------------------------
protected override void OnActivateTool() {
}
// ------------------------------------------------------------------
///
/// Handles the mouse down event.
///
/// The instance containing
/// the event data.
// ------------------------------------------------------------------
public bool MouseDown(MouseEventArgs e) {
return false;
}
// ------------------------------------------------------------------
///
/// Handles the mouse move event
///
/// The instance containing
/// the event data.
// ------------------------------------------------------------------
public void MouseMove(MouseEventArgs e) {
if (!IsSuspended && this.Enabled) {
IHoverListener listener = null;
CollectionBase paintables =
this.Controller.Model.Paintables;
IDiagramEntity entity;
if (paintables.Count == 0) return;
//going from top to the bottom of the z-order
for (int k = paintables.Count - 1; k >= 0; k--) {
entity = paintables[k];
if (entity.Hit(e.Location)) //we caught an entity
{
//unhover the previous, if any
if (previousHovered != null) {
previousHovered.Hovered = false;
}
//tell the current one it's being hovered
entity.Hovered = true;
//fetch the hovering service, if defined
listener = entity.GetService(
typeof(IHoverListener)) as IHoverListener;
if (listener != null) //the caught entity does listen
{
if (currentListener == listener) //it's the same as the previous time
listener.MouseHover(e);
else //we moved from one entity to another listening entity
{
if (currentListener != null) //tell the previous entity we are leaving
currentListener.MouseLeave(e);
listener.MouseEnter(e); //tell the current one we enter
currentListener = listener;
}
} else //the caught entity does not listen
{
if (currentListener != null) {
currentListener.MouseLeave(e);
currentListener = null;
}
}
previousHovered = entity;//remember, for the next time
return; //if another entity is listening underneath this entity it will not receive the notification
}
}
if (currentListener != null) {
currentListener.MouseLeave(e);
currentListener = null;
}
//unhover the previous, if any
if (previousHovered != null)
previousHovered.Hovered = false;
}
}
// ------------------------------------------------------------------
///
/// Handles the mouse up event.
///
/// The instance containing
/// the event data.
// ------------------------------------------------------------------
public void MouseUp(MouseEventArgs e) {
}
#endregion
}
}