]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/cursor.cpp
Merge commit '0d5ebb17b29d4263ec4f1634af24a27620ab47a4' into garux-merge
[xonotic/netradiant.git] / libs / gtkutil / cursor.cpp
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 #include "cursor.h"
23
24 #include "stream/textstream.h"
25
26 #include <string.h>
27 #include <gdk/gdk.h>
28 #include <gtk/gtk.h>
29
30 #if 0
31 GdkCursor* create_blank_cursor(){
32         return gdk_cursor_new(GDK_BLANK_CURSOR);
33 }
34
35 void blank_cursor( ui::Widget widget ){
36         GdkCursor* cursor = create_blank_cursor();
37         gdk_window_set_cursor( gtk_widget_get_window(widget), cursor );
38         gdk_cursor_unref( cursor );
39 }
40
41 void default_cursor( ui::Widget widget ){
42         gdk_window_set_cursor( gtk_widget_get_window(widget), 0 );
43 }
44 #endif
45
46
47 void Sys_GetCursorPos( ui::Window window, int *x, int *y ){
48         gdk_display_get_pointer( gdk_display_get_default(), 0, x, y, 0 );
49 }
50
51 void Sys_SetCursorPos( ui::Window window, int x, int y ){
52         GdkScreen *screen;
53         gdk_display_get_pointer( gdk_display_get_default(), &screen, 0, 0, 0 );
54         gdk_display_warp_pointer( gdk_display_get_default(), screen, x, y );
55 }
56
57 gboolean DeferredMotion::gtk_motion(ui::Widget widget, GdkEventMotion *event, DeferredMotion *self)
58 {
59     self->motion( event->x, event->y, event->state );
60     return FALSE;
61 }
62
63 gboolean FreezePointer::motion_delta(ui::Window widget, GdkEventMotion *event, FreezePointer *self)
64 {
65         int current_x, current_y;
66         Sys_GetCursorPos( widget, &current_x, &current_y );
67         int dx = current_x - self->last_x;
68         int dy = current_y - self->last_y;
69         int ddx = current_x - self->center_x;
70         int ddy = current_y - self->center_y;
71         self->last_x = current_x;
72         self->last_y = current_y;
73         if ( dx != 0 || dy != 0 ) {
74                 //globalOutputStream() << "motion x: " << dx << ", y: " << dy << "\n";
75                 if (ddx < -32 || ddx > 32 || ddy < -32 || ddy > 32) {
76                         Sys_SetCursorPos( widget, self->center_x, self->center_y );
77                         self->last_x = self->center_x;
78                         self->last_y = self->center_y;
79                 }
80                 self->m_function( dx, dy, event->state, self->m_data );
81         }
82         return FALSE;
83 }
84
85 void FreezePointer::freeze_pointer(ui::Window window, ui::Widget widget, FreezePointer::MotionDeltaFunction function, void *data)
86 {
87         ASSERT_MESSAGE( m_function == 0, "can't freeze pointer" );
88
89         const GdkEventMask mask = static_cast<GdkEventMask>( GDK_POINTER_MOTION_MASK
90                                                                                                                  | GDK_POINTER_MOTION_HINT_MASK
91                                                                                                                  | GDK_BUTTON_MOTION_MASK
92                                                                                                                  | GDK_BUTTON1_MOTION_MASK
93                                                                                                                  | GDK_BUTTON2_MOTION_MASK
94                                                                                                                  | GDK_BUTTON3_MOTION_MASK
95                                                                                                                  | GDK_BUTTON_PRESS_MASK
96                                                                                                                  | GDK_BUTTON_RELEASE_MASK
97                                                                                                                  | GDK_VISIBILITY_NOTIFY_MASK );
98
99         //GdkCursor* cursor = create_blank_cursor();
100         GdkCursor* cursor = gdk_cursor_new( GDK_BLANK_CURSOR );
101         //GdkGrabStatus status =
102         /*      fixes cursor runaways during srsly quick drags in camera
103         drags with pressed buttons have no problem at all w/o this      */
104         gdk_pointer_grab( gtk_widget_get_window(window), TRUE, mask, 0, cursor, GDK_CURRENT_TIME );
105         //gdk_window_set_cursor ( GTK_WIDGET( window )->window, cursor );
106         /*      is needed to fix activating neighbour widgets, that happens, if using upper one */
107         gtk_grab_add( widget );
108         m_weedjet = widget;
109
110         gdk_cursor_unref( cursor );
111
112         Sys_GetCursorPos( window, &recorded_x, &recorded_y );
113
114         /*      using center for tracking for max safety        */
115         gdk_window_get_origin( GTK_WIDGET( widget )->window, &center_x, &center_y );
116         auto allocation = widget.dimensions();
117         center_y += allocation.height / 2;
118         center_x += allocation.width / 2;
119
120         Sys_SetCursorPos( window, center_x, center_y );
121
122         last_x = center_x;
123         last_y = center_y;
124
125         m_function = function;
126         m_data = data;
127
128         handle_motion = window.connect( "motion_notify_event", G_CALLBACK( motion_delta ), this );
129 }
130
131 void FreezePointer::unfreeze_pointer(ui::Window window, bool centerize )
132 {
133         g_signal_handler_disconnect( G_OBJECT( window ), handle_motion );
134
135         m_function = 0;
136         m_data = 0;
137
138         if ( centerize ){
139                 Sys_SetCursorPos( window, center_x, center_y );
140         }
141         else{
142                 Sys_SetCursorPos( window, recorded_x, recorded_y );
143         }
144
145 //      gdk_window_set_cursor( GTK_WIDGET( window )->window, 0 );
146         gdk_pointer_ungrab( GDK_CURRENT_TIME );
147
148         if ( m_weedjet )
149         {
150                 gtk_grab_remove( m_weedjet );
151         }
152 }