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