radiant/cursor: get the display from the widget, attempt to support multiple displays...
[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 GdkCursor* create_blank_cursor(){
31         return gdk_cursor_new( GDK_BLANK_CURSOR );
32 }
33
34 void set_cursor( ui::Widget widget, GdkCursorType cursor_type ){
35         GdkCursor* cursor = gdk_cursor_new( cursor_type );
36         gdk_window_set_cursor( gtk_widget_get_window( widget ), cursor );
37         gdk_cursor_unref( cursor );
38 }
39
40 void blank_cursor( ui::Widget widget ){
41         set_cursor( widget, GDK_BLANK_CURSOR );
42 }
43
44 void default_cursor( ui::Widget widget ){
45         gdk_window_set_cursor( gtk_widget_get_window( widget ), NULL );
46 }
47
48 void Sys_GetCursorPos( ui::Widget widget, int *x, int *y ){
49         GdkDisplay *display = gtk_widget_get_display( GTK_WIDGET( widget ) );
50         // No need to store the screen, it will be recovered from widget again.
51         gdk_display_get_pointer( display, NULL, x, y, NULL );
52 }
53
54 void Sys_SetCursorPos( ui::Widget widget, int x, int y ){
55         GdkDisplay *display = gtk_widget_get_display( GTK_WIDGET( widget ) );
56         GdkScreen *screen = gtk_widget_get_screen( GTK_WIDGET( widget ) );
57         gdk_display_warp_pointer( display, screen, x, y );
58 }
59
60 gboolean DeferredMotion::gtk_motion(ui::Widget widget, GdkEventMotion *event, DeferredMotion *self)
61 {
62     self->motion( event->x, event->y, event->state );
63     return FALSE;
64 }
65
66 gboolean FreezePointer::motion_delta(ui::Widget widget, GdkEventMotion *event, FreezePointer *self)
67 {
68         int current_x, current_y;
69         Sys_GetCursorPos( widget, &current_x, &current_y );
70         int dx = current_x - self->last_x;
71         int dy = current_y - self->last_y;
72         self->last_x = current_x;
73         self->last_y = current_y;
74         if ( dx != 0 || dy != 0 ) {
75                 //globalOutputStream() << "motion x: " << dx << ", y: " << dy << "\n";
76                 int ddx = current_x - self->recorded_x;
77                 int ddy = current_y - self->recorded_y;
78                 if (ddx < -32 || ddx > 32 || ddy < -32 || ddy > 32) {
79                         Sys_SetCursorPos( widget, self->recorded_x, self->recorded_y );
80                         self->last_x = self->recorded_x;
81                         self->last_y = self->recorded_y;
82                 }
83                 self->m_function( dx, dy, event->state, self->m_data );
84         }
85         return FALSE;
86 }
87
88 void FreezePointer::freeze_pointer(ui::Widget widget, FreezePointer::MotionDeltaFunction function, void *data)
89 {
90         ASSERT_MESSAGE( m_function == 0, "can't freeze pointer" );
91
92         const GdkEventMask mask = static_cast<GdkEventMask>( GDK_POINTER_MOTION_MASK
93                                                                                                                  | GDK_POINTER_MOTION_HINT_MASK
94                                                                                                                  | GDK_BUTTON_MOTION_MASK
95                                                                                                                  | GDK_BUTTON1_MOTION_MASK
96                                                                                                                  | GDK_BUTTON2_MOTION_MASK
97                                                                                                                  | GDK_BUTTON3_MOTION_MASK
98                                                                                                                  | GDK_BUTTON_PRESS_MASK
99                                                                                                                  | GDK_BUTTON_RELEASE_MASK
100                                                                                                                  | GDK_VISIBILITY_NOTIFY_MASK );
101
102         GdkCursor* cursor = create_blank_cursor();
103         //GdkGrabStatus status =
104         gdk_pointer_grab( gtk_widget_get_window( widget ), TRUE, mask, 0, cursor, GDK_CURRENT_TIME );
105         gdk_cursor_unref( cursor );
106
107         Sys_GetCursorPos( widget, &recorded_x, &recorded_y );
108
109         Sys_SetCursorPos( widget, recorded_x, recorded_y );
110
111         last_x = recorded_x;
112         last_y = recorded_y;
113
114         m_function = function;
115         m_data = data;
116
117         handle_motion = widget.connect( "motion_notify_event", G_CALLBACK( motion_delta ), this );
118 }
119
120 void FreezePointer::unfreeze_pointer(ui::Widget widget)
121 {
122         g_signal_handler_disconnect( G_OBJECT( widget ), handle_motion );
123
124         m_function = 0;
125         m_data = 0;
126
127         Sys_SetCursorPos( widget, recorded_x, recorded_y );
128
129         gdk_pointer_ungrab( GDK_CURRENT_TIME );
130 }