1
// ----------------------------------------------------------------
4
// Copyright 2008, Irénée HOTTIER,
6
// This is free software licensed under the NUnit license, You may
7
// obtain a copy of the license at http://nunit.org/?p=license&r=2.4
8
// ----------------------------------------------------------------
11
using System.Collections.Generic;
13
using System.Windows.Forms;
14
using System.Diagnostics;
16
namespace NUnit.UiException
19
/// Watch events coming from the mouse over a given control hierarchy and
20
/// proprage up events such as: MouseLeaved or MouseClicked to observers.
21
/// When a MouseLeave event is fired, the class waits a certain amount of time
22
/// to check whether or not a mouseEnter event is coming. If nothing happens
23
/// after this time, the class delivers a mouseLeaved event to registered observers.
24
/// Unlike MouseLeave, the MouseClicked event is fired when it occurs on
25
/// one of the underlying controls.
27
public class MouseWatcher
30
/// Default setting that indicate the amount of time to wait (in milliseconds)
31
/// after a mouseLeave event occured.
33
public const int DEFAULT_TIMER_INTERVAL = 500;
36
/// Fired when MouseWatcher has detected that the mouse completely
37
/// get out of the watched control collection.
39
public event EventHandler MouseLeaved;
42
/// Fired when MouseWatcher has detected a click on one of underlying
43
/// control hierarchy.
45
public event EventHandler MouseClicked;
48
/// The list of controls to be watched.
50
private List<Control> _controls;
53
/// The current active control.
55
private Control _active;
58
/// The timer that waits amount of time explained above.
63
/// Builds a new instance of MouseWatcher.
67
_controls = new List<Control>();
69
_timer.Tick += new EventHandler(_timer_Tick);
70
_timer.Interval = DEFAULT_TIMER_INTERVAL;
76
/// Registers this control in the collection of controls for which
77
/// mouse interactions must be watched.
79
/// <param name="control">Appends this control to the list of control for which
80
/// mouse interactions must be watched.</param>
81
public void Register(Control control)
83
TraceExceptionHelper.CheckNotNull(control, "control");
84
_controls.Add(control);
86
control.MouseEnter += new EventHandler(control_MouseEnter);
87
control.MouseLeave += new EventHandler(control_MouseLeave);
88
control.MouseClick += new MouseEventHandler(control_MouseClick);
90
foreach (Control child in control.Controls)
97
/// Gets the current active control.
101
get { return (_active); }
105
/// Gets the count of controls in the list.
109
get { return (_controls.Count); }
113
/// Gives access to the timer.
115
protected Timer Timer
117
get { return (_timer); }
121
/// Handler called when a tick occurs.
123
protected void HandleTimerTick()
125
//Trace.WriteLine("timer: active=" + _active);
129
_timer.Enabled = false;
133
//Trace.WriteLine("====> LEAVE");
135
_timer.Enabled = false;
137
if (MouseLeaved != null)
138
MouseLeaved(this, new EventArgs());
144
/// Timer event handler.
146
void _timer_Tick(object sender, EventArgs e)
152
/// Handler called when mouse cursor leaved a control.
154
void control_MouseLeave(object sender, EventArgs e)
156
// Trace.WriteLine(sender + " said: mouse leave");
158
if (_active != sender)
162
_timer.Enabled = true;
168
/// Handler called when a click has occured on
169
/// control hierarchy.
171
void control_MouseClick(object sender, MouseEventArgs e)
173
if (MouseClicked != null)
174
MouseClicked(this, e);
180
/// Handler called when mouse cursor entered a control.
182
void control_MouseEnter(object sender, EventArgs e)
184
//Trace.WriteLine(sender + " said mouse enter");
185
_active = (Control)sender;