]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/cursor.h
d5d5d3397a7603c6a1f0212c1a88d770a77d61c9
[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 <gtk/gtk.h>
27 #include <uilib/uilib.h>
28
29 #include "debugging/debugging.h"
30
31 typedef struct _GdkCursor GdkCursor;
32
33 GdkCursor* create_blank_cursor();
34 void blank_cursor( ui::Widget widget );
35 void default_cursor( ui::Widget widget );
36 void Sys_GetCursorPos( ui::Window window, int *x, int *y );
37 void Sys_SetCursorPos( ui::Window window, int x, int y );
38
39
40
41 class DeferredMotion
42 {
43 guint m_handler;
44 typedef void ( *MotionFunction )( gdouble x, gdouble y, guint state, void* data );
45 MotionFunction m_function;
46 void* m_data;
47 gdouble m_x;
48 gdouble m_y;
49 guint m_state;
50
51 static gboolean deferred( DeferredMotion* self ){
52         self->m_handler = 0;
53         self->m_function( self->m_x, self->m_y, self->m_state, self->m_data );
54         return FALSE;
55 }
56 public:
57 DeferredMotion( MotionFunction function, void* data ) : m_handler( 0 ), m_function( function ), m_data( data ){
58 }
59 void motion( gdouble x, gdouble y, guint state ){
60         m_x = x;
61         m_y = y;
62         m_state = state;
63         if ( m_handler == 0 ) {
64                 m_handler = g_idle_add( (GSourceFunc)deferred, this );
65         }
66 }
67 static gboolean gtk_motion( ui::Widget widget, GdkEventMotion *event, DeferredMotion* self ){
68         self->motion( event->x, event->y, event->state );
69         return FALSE;
70 }
71 };
72
73 class DeferredMotionDelta
74 {
75 int m_delta_x;
76 int m_delta_y;
77 guint m_motion_handler;
78 typedef void ( *MotionDeltaFunction )( int x, int y, void* data );
79 MotionDeltaFunction m_function;
80 void* m_data;
81
82 static gboolean deferred_motion( gpointer data ){
83         reinterpret_cast<DeferredMotionDelta*>( data )->m_function(
84                 reinterpret_cast<DeferredMotionDelta*>( data )->m_delta_x,
85                 reinterpret_cast<DeferredMotionDelta*>( data )->m_delta_y,
86                 reinterpret_cast<DeferredMotionDelta*>( data )->m_data
87                 );
88         reinterpret_cast<DeferredMotionDelta*>( data )->m_motion_handler = 0;
89         reinterpret_cast<DeferredMotionDelta*>( data )->m_delta_x = 0;
90         reinterpret_cast<DeferredMotionDelta*>( data )->m_delta_y = 0;
91         return FALSE;
92 }
93 public:
94 DeferredMotionDelta( MotionDeltaFunction function, void* data ) : m_delta_x( 0 ), m_delta_y( 0 ), m_motion_handler( 0 ), m_function( function ), m_data( data ){
95 }
96 void flush(){
97         if ( m_motion_handler != 0 ) {
98                 g_source_remove( m_motion_handler );
99                 deferred_motion( this );
100         }
101 }
102 void motion_delta( int x, int y, unsigned int state ){
103         m_delta_x += x;
104         m_delta_y += y;
105         if ( m_motion_handler == 0 ) {
106                 m_motion_handler = g_idle_add( deferred_motion, this );
107         }
108 }
109 };
110
111 class FreezePointer
112 {
113 unsigned int handle_motion;
114 int recorded_x, recorded_y, last_x, last_y;
115 typedef void ( *MotionDeltaFunction )( int x, int y, unsigned int state, void* data );
116 MotionDeltaFunction m_function;
117 void* m_data;
118 public:
119 FreezePointer() : handle_motion( 0 ), m_function( 0 ), m_data( 0 ){
120 }
121 static gboolean motion_delta( ui::Widget widget, GdkEventMotion *event, FreezePointer* self ){
122         int current_x, current_y;
123         Sys_GetCursorPos( ui::Window(GTK_WINDOW( widget )), &current_x, &current_y );
124         int dx = current_x - self->last_x;
125         int dy = current_y - self->last_y;
126         int ddx = current_x - self->recorded_x;
127         int ddy = current_y - self->recorded_y;
128         self->last_x = current_x;
129         self->last_y = current_y;
130         if ( dx != 0 || dy != 0 ) {
131                 //globalOutputStream() << "motion x: " << dx << ", y: " << dy << "\n";
132                 if (ddx < -32 || ddx > 32 || ddy < -32 || ddy > 32) {
133                         Sys_SetCursorPos( ui::Window(GTK_WINDOW( widget )), self->recorded_x, self->recorded_y );
134                         self->last_x = self->recorded_x;
135                         self->last_y = self->recorded_y;
136                 }
137                 self->m_function( dx, dy, event->state, self->m_data );
138         }
139         return FALSE;
140 }
141
142 void freeze_pointer( ui::Window window, MotionDeltaFunction function, void* data ){
143         ASSERT_MESSAGE( m_function == 0, "can't freeze pointer" );
144
145         const GdkEventMask mask = static_cast<GdkEventMask>( GDK_POINTER_MOTION_MASK
146                                                                                                                  | GDK_POINTER_MOTION_HINT_MASK
147                                                                                                                  | GDK_BUTTON_MOTION_MASK
148                                                                                                                  | GDK_BUTTON1_MOTION_MASK
149                                                                                                                  | GDK_BUTTON2_MOTION_MASK
150                                                                                                                  | GDK_BUTTON3_MOTION_MASK
151                                                                                                                  | GDK_BUTTON_PRESS_MASK
152                                                                                                                  | GDK_BUTTON_RELEASE_MASK
153                                                                                                                  | GDK_VISIBILITY_NOTIFY_MASK );
154
155         GdkCursor* cursor = create_blank_cursor();
156         //GdkGrabStatus status =
157         gdk_pointer_grab( gtk_widget_get_window(GTK_WIDGET(window)), TRUE, mask, 0, cursor, GDK_CURRENT_TIME );
158         gdk_cursor_unref( cursor );
159
160         Sys_GetCursorPos( window, &recorded_x, &recorded_y );
161
162         Sys_SetCursorPos( window, recorded_x, recorded_y );
163
164         last_x = recorded_x;
165         last_y = recorded_y;
166
167         m_function = function;
168         m_data = data;
169
170         handle_motion = g_signal_connect( G_OBJECT( window ), "motion_notify_event", G_CALLBACK( motion_delta ), this );
171 }
172
173 void unfreeze_pointer( ui::Window window ){
174         g_signal_handler_disconnect( G_OBJECT( window ), handle_motion );
175
176         m_function = 0;
177         m_data = 0;
178
179         Sys_SetCursorPos( window, recorded_x, recorded_y );
180
181         gdk_pointer_ungrab( GDK_CURRENT_TIME );
182 }
183 };
184
185 #endif