3
// Copyright (C) 2009 GNOME Do
5
// This program is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
10
// This program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// GNU General Public License for more details.
15
// You should have received a copy of the GNU General Public License
16
// along with this program. If not, see <http://www.gnu.org/licenses/>.
20
using System.Collections.Generic;
26
namespace Docky.Interface
28
public class CursorUpdatedArgs : EventArgs
30
public readonly Point OldCursor;
31
public readonly Point NewCursor;
33
public CursorUpdatedArgs (Point newCursor, Point oldCursor)
35
NewCursor = newCursor;
36
OldCursor = oldCursor;
40
public class CursorTracker : IDisposable
45
DateTime last_gtk_update;
49
public event EventHandler<CursorUpdatedArgs> CursorUpdated;
51
public bool Enabled { get; set; }
53
public Gdk.Point Cursor { get; private set; }
54
public Gdk.Point RootCursor { get; private set; }
56
public ModifierType ModifierType { get; private set; }
58
public Cairo.PointD CursorD {
59
get { return new Cairo.PointD (Cursor.X, Cursor.Y); }
62
public Cairo.PointD RootCursorD {
63
get { return new Cairo.PointD (RootCursor.X, RootCursor.Y); }
66
public uint TimerLength {
67
get { return timer_length; }
69
if (timer_length == value)
76
public CursorTracker (Gtk.Window parent, uint timerLength)
81
parent.MotionNotifyEvent += HandleMotionNotifyEvent;
82
parent.AddEvents ((int) EventMask.PointerMotionMask);
84
timer_length = timerLength;
85
timer = GLib.Timeout.Add (timer_length, HandleCursorTimeoutElapsed);
88
bool HandleCursorTimeoutElapsed ()
90
if (!Enabled || (DateTime.UtcNow - last_gtk_update).TotalMilliseconds < timer_length >> 1) {
94
int x, y, xroot, yroot;
98
parent.Display.GetPointer (out screen, out xroot, out yroot, out mod);
100
if (screen == parent.Screen) {
102
if (parent is DockWindow)
103
(parent as DockWindow).GetBufferedPosition (out geo.X, out geo.Y);
105
parent.GetPosition (out geo.X, out geo.Y);
116
if (Cursor.X != x || Cursor.Y != y || ModifierType != mod) {
118
Cursor = new Point (x, y);
119
RootCursor = new Point (xroot, yroot);
122
OnCursorUpdated (last);
128
void HandleMotionNotifyEvent (object o, MotionNotifyEventArgs args)
134
last_gtk_update = DateTime.UtcNow;
135
Cursor = new Point ((int) args.Event.X, (int) args.Event.Y);
136
RootCursor = new Point ((int) args.Event.XRoot, (int) args.Event.YRoot);
137
ModifierType = args.Event.State;
139
OnCursorUpdated (last);
142
void OnCursorUpdated (Point lastCursor)
144
if (CursorUpdated != null)
145
CursorUpdated (this, new CursorUpdatedArgs (Cursor, lastCursor));
150
GLib.Source.Remove (timer);
151
timer = GLib.Timeout.Add (timer_length, HandleCursorTimeoutElapsed);
154
public void Dispose ()
156
GLib.Source.Remove (timer);
157
parent.MotionNotifyEvent -= HandleMotionNotifyEvent;