b3cfe080f8676c10ea8010ce60fb27fa01fa992f
[xonotic/netradiant.git] / libs / gtkutil / cursor.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #if !defined( INCLUDED_GTKUTIL_CURSOR_H )
23 #define INCLUDED_GTKUTIL_CURSOR_H
24
25 #include <glib.h>
26 #include <gdk/gdkevents.h>
27 #include <gtk/gtkwidget.h>
28 #include <gtk/gtkwindow.h>
29
30 #include "debugging/debugging.h"
31
32 typedef struct _GdkCursor GdkCursor;
33 typedef struct _GtkWidget GtkWidget;
34 typedef struct _GtkWindow GtkWindow;
35
36 GdkCursor* create_blank_cursor();
37 void blank_cursor( GtkWidget* widget );
38 void default_cursor( GtkWidget* widget );
39 void Sys_GetCursorPos( GtkWindow* window, int *x, int *y );
40 void Sys_SetCursorPos( GtkWindow* window, int x, int y );
41
42
43
44 class DeferredMotion
45 {
46 guint m_handler;
47 typedef void ( *MotionFunction )( gdouble x, gdouble y, guint state, void* data );
48 MotionFunction m_function;
49 void* m_data;
50 gdouble m_x;
51 gdouble m_y;
52 guint m_state;
53
54 static gboolean deferred( DeferredMotion* self ){
55         self->m_handler = 0;
56         self->m_function( self->m_x, self->m_y, self->m_state, self->m_data );
57         return FALSE;
58 }
59 public:
60 DeferredMotion( MotionFunction function, void* data ) : m_handler( 0 ), m_function( function ), m_data( data ){
61 }
62 void motion( gdouble x, gdouble y, guint state ){
63         m_x = x;
64         m_y = y;
65         m_state = state;
66         if ( m_handler == 0 ) {
67                 m_handler = g_idle_add( (GSourceFunc)deferred, this );
68         }
69 }
70 static gboolean gtk_motion( GtkWidget *widget, GdkEventMotion *event, DeferredMotion* self ){
71         self->motion( event->x, event->y, event->state );
72         return FALSE;
73 }
74 };
75
76 class DeferredMotionDelta
77 {
78 int m_delta_x;
79 int m_delta_y;
80 guint m_motion_handler;
81 typedef void ( *MotionDeltaFunction )( int x, int y, void* data );
82 MotionDeltaFunction m_function;
83 void* m_data;
84
85 static gboolean deferred_motion( gpointer data ){
86         reinterpret_cast<DeferredMotionDelta*>( data )->m_function(
87                 reinterpret_cast<DeferredMotionDelta*>( data )->m_delta_x,
88                 reinterpret_cast<DeferredMotionDelta*>( data )->m_delta_y,
89                 reinterpret_cast<DeferredMotionDelta*>( data )->m_data
90                 );
91         reinterpret_cast<DeferredMotionDelta*>( data )->m_motion_handler = 0;
92         reinterpret_cast<DeferredMotionDelta*>( data )->m_delta_x = 0;
93         reinterpret_cast<DeferredMotionDelta*>( data )->m_delta_y = 0;
94         return FALSE;
95 }
96 public:
97 DeferredMotionDelta( MotionDeltaFunction function, void* data ) : m_delta_x( 0 ), m_delta_y( 0 ), m_motion_handler( 0 ), m_function( function ), m_data( data ){
98 }
99 void flush(){
100         if ( m_motion_handler != 0 ) {
101                 g_source_remove( m_motion_handler );
102                 deferred_motion( this );
103         }
104 }
105 void motion_delta( int x, int y, unsigned int state ){
106         m_delta_x += x;
107         m_delta_y += y;
108         if ( m_motion_handler == 0 ) {
109                 m_motion_handler = g_idle_add( deferred_motion, this );
110         }
111 }
112 };
113
114 class FreezePointer
115 {
116 unsigned int handle_motion;
117 int recorded_x, recorded_y;
118 typedef void ( *MotionDeltaFunction )( int x, int y, unsigned int state, void* data );
119 MotionDeltaFunction m_function;
120 void* m_data;
121 public:
122 FreezePointer() : handle_motion( 0 ), m_function( 0 ), m_data( 0 ){
123 }
124 static gboolean motion_delta( GtkWidget *widget, GdkEventMotion *event, FreezePointer* self ){
125         int current_x, current_y;
126         Sys_GetCursorPos( GTK_WINDOW( widget ), &current_x, &current_y );
127         int dx = current_x - self->recorded_x;
128         int dy = current_y - self->recorded_y;
129         if ( dx != 0 || dy != 0 ) {
130                 //globalOutputStream() << "motion x: " << dx << ", y: " << dy << "\n";
131                 Sys_SetCursorPos( GTK_WINDOW( widget ), self->recorded_x, self->recorded_y );
132                 self->m_function( dx, dy, event->state, self->m_data );
133         }
134         return FALSE;
135 }
136
137 void freeze_pointer( GtkWindow* window, MotionDeltaFunction function, void* data ){
138         ASSERT_MESSAGE( m_function == 0, "can't freeze pointer" );
139
140         const GdkEventMask mask = static_cast<GdkEventMask>( GDK_POINTER_MOTION_MASK
141                                                                                                                  | GDK_POINTER_MOTION_HINT_MASK
142                                                                                                                  | GDK_BUTTON_MOTION_MASK
143                                                                                                                  | GDK_BUTTON1_MOTION_MASK
144                                                                                                                  | GDK_BUTTON2_MOTION_MASK
145                                                                                                                  | GDK_BUTTON3_MOTION_MASK
146                                                                                                                  | GDK_BUTTON_PRESS_MASK
147                                                                                                                  | GDK_BUTTON_RELEASE_MASK
148                                                                                                                  | GDK_VISIBILITY_NOTIFY_MASK );
149
150         GdkCursor* cursor = create_blank_cursor();
151         //GdkGrabStatus status =
152         gdk_pointer_grab( GTK_WIDGET( window )->window, TRUE, mask, 0, cursor, GDK_CURRENT_TIME );
153         gdk_cursor_unref( cursor );
154
155         Sys_GetCursorPos( window, &recorded_x, &recorded_y );
156
157         Sys_SetCursorPos( window, recorded_x, recorded_y );
158
159         m_function = function;
160         m_data = data;
161
162         handle_motion = g_signal_connect( G_OBJECT( window ), "motion_notify_event", G_CALLBACK( motion_delta ), this );
163 }
164
165 void unfreeze_pointer( GtkWindow* window ){
166         g_signal_handler_disconnect( G_OBJECT( window ), handle_motion );
167
168         m_function = 0;
169         m_data = 0;
170
171         Sys_SetCursorPos( window, recorded_x, recorded_y );
172
173         gdk_pointer_ungrab( GDK_CURRENT_TIME );
174 }
175 };
176
177 #endif