]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/cursor.h
Q3map2:
[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 #include <gtk/gtkmain.h>
30
31 #include "debugging/debugging.h"
32
33 typedef struct _GdkCursor GdkCursor;
34 typedef struct _GtkWidget GtkWidget;
35 typedef struct _GtkWindow GtkWindow;
36
37 #if 0
38 GdkCursor* create_blank_cursor();
39 void blank_cursor( GtkWidget* widget );
40 void default_cursor( GtkWidget* widget );
41 #endif
42 void Sys_GetCursorPos( GtkWindow* window, int *x, int *y );
43 void Sys_SetCursorPos( GtkWindow* window, int x, int y );
44
45
46
47 class DeferredMotion
48 {
49 guint m_handler;
50 typedef void ( *MotionFunction )( gdouble x, gdouble y, guint state, void* data );
51 MotionFunction m_function;
52 void* m_data;
53 gdouble m_x;
54 gdouble m_y;
55 guint m_state;
56
57 static gboolean deferred( DeferredMotion* self ){
58         self->m_handler = 0;
59         self->m_function( self->m_x, self->m_y, self->m_state, self->m_data );
60         return FALSE;
61 }
62 public:
63 DeferredMotion( MotionFunction function, void* data ) : m_handler( 0 ), m_function( function ), m_data( data ){
64 }
65 void motion( gdouble x, gdouble y, guint state ){
66         m_x = x;
67         m_y = y;
68         m_state = state;
69         if ( m_handler == 0 ) {
70                 m_handler = g_idle_add( (GSourceFunc)deferred, this );
71         }
72 }
73 static gboolean gtk_motion( GtkWidget *widget, GdkEventMotion *event, DeferredMotion* self ){
74         self->motion( event->x, event->y, event->state );
75         return FALSE;
76 }
77 };
78
79 class DeferredMotionDelta
80 {
81 int m_delta_x;
82 int m_delta_y;
83 guint m_motion_handler;
84 typedef void ( *MotionDeltaFunction )( int x, int y, void* data );
85 MotionDeltaFunction m_function;
86 void* m_data;
87
88 static gboolean deferred_motion( gpointer data ){
89         reinterpret_cast<DeferredMotionDelta*>( data )->m_function(
90                 reinterpret_cast<DeferredMotionDelta*>( data )->m_delta_x,
91                 reinterpret_cast<DeferredMotionDelta*>( data )->m_delta_y,
92                 reinterpret_cast<DeferredMotionDelta*>( data )->m_data
93                 );
94         reinterpret_cast<DeferredMotionDelta*>( data )->m_motion_handler = 0;
95         reinterpret_cast<DeferredMotionDelta*>( data )->m_delta_x = 0;
96         reinterpret_cast<DeferredMotionDelta*>( data )->m_delta_y = 0;
97         return FALSE;
98 }
99 public:
100 DeferredMotionDelta( MotionDeltaFunction function, void* data ) : m_delta_x( 0 ), m_delta_y( 0 ), m_motion_handler( 0 ), m_function( function ), m_data( data ){
101 }
102 void flush(){
103         if ( m_motion_handler != 0 ) {
104                 g_source_remove( m_motion_handler );
105                 deferred_motion( this );
106         }
107 }
108 void motion_delta( int x, int y, unsigned int state ){
109         m_delta_x += x;
110         m_delta_y += y;
111         if ( m_motion_handler == 0 ) {
112                 m_motion_handler = g_idle_add( deferred_motion, this );
113         }
114 }
115 };
116
117 class FreezePointer
118 {
119 unsigned int handle_motion;
120 int recorded_x, recorded_y, last_x, last_y, center_x, center_y;
121 GtkWidget* m_weedjet;
122 typedef void ( *MotionDeltaFunction )( int x, int y, unsigned int state, void* data );
123 MotionDeltaFunction m_function;
124 void* m_data;
125 public:
126 FreezePointer() : handle_motion( 0 ), m_function( 0 ), m_data( 0 ){
127 }
128 static gboolean motion_delta( GtkWidget *widget, GdkEventMotion *event, FreezePointer* self ){
129         int current_x, current_y;
130         Sys_GetCursorPos( GTK_WINDOW( widget ), &current_x, &current_y );
131         int dx = current_x - self->last_x;
132         int dy = current_y - self->last_y;
133         int ddx = current_x - self->center_x;
134         int ddy = current_y - self->center_y;
135         self->last_x = current_x;
136         self->last_y = current_y;
137         if ( dx != 0 || dy != 0 ) {
138                 //globalOutputStream() << "motion x: " << dx << ", y: " << dy << "\n";
139                 if (ddx < -32 || ddx > 32 || ddy < -32 || ddy > 32) {
140                         Sys_SetCursorPos( GTK_WINDOW( widget ), self->center_x, self->center_y );
141                         self->last_x = self->center_x;
142                         self->last_y = self->center_y;
143                 }
144                 self->m_function( dx, dy, event->state, self->m_data );
145         }
146         return FALSE;
147 }
148
149 void freeze_pointer( GtkWindow* window, GtkWidget* widget, MotionDeltaFunction function, void* data ){
150         ASSERT_MESSAGE( m_function == 0, "can't freeze pointer" );
151
152         const GdkEventMask mask = static_cast<GdkEventMask>( GDK_POINTER_MOTION_MASK
153                                                                                                                  | GDK_POINTER_MOTION_HINT_MASK
154                                                                                                                  | GDK_BUTTON_MOTION_MASK
155                                                                                                                  | GDK_BUTTON1_MOTION_MASK
156                                                                                                                  | GDK_BUTTON2_MOTION_MASK
157                                                                                                                  | GDK_BUTTON3_MOTION_MASK
158                                                                                                                  | GDK_BUTTON_PRESS_MASK
159                                                                                                                  | GDK_BUTTON_RELEASE_MASK
160                                                                                                                  | GDK_VISIBILITY_NOTIFY_MASK );
161
162         GdkCursor* cursor = gdk_cursor_new( GDK_BLANK_CURSOR );
163         //GdkCursor* cursor = create_blank_cursor();
164         //GdkGrabStatus status =
165         /*      fixes cursor runaways during srsly quick drags in camera
166         drags with pressed buttons have no problem at all w/o this      */
167         gdk_pointer_grab( GTK_WIDGET( window )->window, TRUE, mask, 0, cursor, GDK_CURRENT_TIME );
168         //gdk_window_set_cursor ( GTK_WIDGET( window )->window, cursor );
169         /*      is needed to fix activating neighbour widgets, that happens, if using upper one */
170         gtk_grab_add( widget );
171         m_weedjet = widget;
172
173         gdk_cursor_unref( cursor );
174
175         Sys_GetCursorPos( window, &recorded_x, &recorded_y );
176
177         /*      using center for tracking for max safety        */
178         gdk_window_get_origin( widget->window, &center_x, &center_y );
179         center_y += widget->allocation.height / 2;
180         center_x += widget->allocation.width / 2;
181
182         Sys_SetCursorPos( window, center_x, center_y );
183
184         last_x = center_x;
185         last_y = center_y;
186
187         m_function = function;
188         m_data = data;
189
190         handle_motion = g_signal_connect( G_OBJECT( window ), "motion_notify_event", G_CALLBACK( motion_delta ), this );
191 }
192
193 void unfreeze_pointer( GtkWindow* window, bool centerize ){
194         g_signal_handler_disconnect( G_OBJECT( window ), handle_motion );
195
196         m_function = 0;
197         m_data = 0;
198
199         if( centerize ){
200                 Sys_SetCursorPos( window, center_x, center_y );
201         }
202         else{
203                 Sys_SetCursorPos( window, recorded_x, recorded_y );
204         }
205 //      gdk_window_set_cursor( GTK_WIDGET( window )->window, 0 );
206         gdk_pointer_ungrab( GDK_CURRENT_TIME );
207         if( m_weedjet )
208                 gtk_grab_remove( m_weedjet );
209 }
210 };
211
212 #endif