1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2009 Carlos Garnacho <carlosg@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include <gdk/gdkwindow.h>
27 #include "gdkdisplayprivate.h"
28 #include "gdkdevice-win32.h"
31 static gboolean gdk_device_win32_get_history (GdkDevice *device,
35 GdkTimeCoord ***events,
37 static void gdk_device_win32_get_state (GdkDevice *device,
40 GdkModifierType *mask);
41 static void gdk_device_win32_set_window_cursor (GdkDevice *device,
44 static void gdk_device_win32_warp (GdkDevice *device,
48 static gboolean gdk_device_win32_query_state (GdkDevice *device,
50 GdkWindow **root_window,
51 GdkWindow **child_window,
56 GdkModifierType *mask);
57 static GdkGrabStatus gdk_device_win32_grab (GdkDevice *device,
59 gboolean owner_events,
60 GdkEventMask event_mask,
61 GdkWindow *confine_to,
64 static void gdk_device_win32_ungrab (GdkDevice *device,
66 static GdkWindow * gdk_device_win32_window_at_position (GdkDevice *device,
69 GdkModifierType *mask,
70 gboolean get_toplevel);
71 static void gdk_device_win32_select_window_events (GdkDevice *device,
73 GdkEventMask event_mask);
76 G_DEFINE_TYPE (GdkDeviceWin32, gdk_device_win32, GDK_TYPE_DEVICE)
79 gdk_device_win32_class_init (GdkDeviceWin32Class *klass)
81 GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
83 device_class->get_history = gdk_device_win32_get_history;
84 device_class->get_state = gdk_device_win32_get_state;
85 device_class->set_window_cursor = gdk_device_win32_set_window_cursor;
86 device_class->warp = gdk_device_win32_warp;
87 device_class->query_state = gdk_device_win32_query_state;
88 device_class->grab = gdk_device_win32_grab;
89 device_class->ungrab = gdk_device_win32_ungrab;
90 device_class->window_at_position = gdk_device_win32_window_at_position;
91 device_class->select_window_events = gdk_device_win32_select_window_events;
95 gdk_device_win32_init (GdkDeviceWin32 *device_win32)
99 device = GDK_DEVICE (device_win32);
101 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_X, 0, 0, 1);
102 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_Y, 0, 0, 1);
106 gdk_device_win32_get_history (GdkDevice *device,
110 GdkTimeCoord ***events,
117 gdk_device_win32_get_state (GdkDevice *device,
120 GdkModifierType *mask)
124 gdk_window_get_pointer (window, &x_int, &y_int, mask);
134 gdk_device_win32_set_window_cursor (GdkDevice *device,
138 GdkWin32Cursor *cursor_private;
139 GdkWindow *parent_window;
140 GdkWindowImplWin32 *impl;
144 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
145 cursor_private = (GdkWin32Cursor*) cursor;
147 hprevcursor = impl->hcursor;
152 hcursor = cursor_private->hcursor;
156 /* If the pointer is over our window, set new cursor */
157 GdkWindow *curr_window = gdk_window_get_pointer (window, NULL, NULL, NULL);
159 if (curr_window == window ||
160 (curr_window && window == gdk_window_get_toplevel (curr_window)))
164 /* Climb up the tree and find whether our window is the
165 * first ancestor that has cursor defined, and if so, set
168 while (curr_window && curr_window->impl &&
169 !GDK_WINDOW_IMPL_WIN32 (curr_window->impl)->hcursor)
171 curr_window = curr_window->parent;
172 if (curr_window == GDK_WINDOW (window))
181 /* Unset the previous cursor: Need to make sure it's no longer in
182 * use before we destroy it, in case we're not over our window but
183 * the cursor is still set to our old one.
185 if (hprevcursor != NULL &&
186 GetCursor () == hprevcursor)
188 /* Look for a suitable cursor to use instead */
190 parent_window = GDK_WINDOW (window)->parent;
192 while (hcursor == NULL)
196 impl = GDK_WINDOW_IMPL_WIN32 (parent_window->impl);
197 hcursor = impl->hcursor;
198 parent_window = parent_window->parent;
201 hcursor = LoadCursor (NULL, IDC_ARROW);
209 gdk_device_win32_warp (GdkDevice *device,
214 SetCursorPos (x - _gdk_offset_x, y - _gdk_offset_y);
217 static GdkModifierType
218 get_current_mask (void)
220 GdkModifierType mask;
223 GetKeyboardState (kbd);
225 if (kbd[VK_SHIFT] & 0x80)
226 mask |= GDK_SHIFT_MASK;
227 if (kbd[VK_CAPITAL] & 0x80)
228 mask |= GDK_LOCK_MASK;
229 if (kbd[VK_CONTROL] & 0x80)
230 mask |= GDK_CONTROL_MASK;
231 if (kbd[VK_MENU] & 0x80)
232 mask |= GDK_MOD1_MASK;
233 if (kbd[VK_LBUTTON] & 0x80)
234 mask |= GDK_BUTTON1_MASK;
235 if (kbd[VK_MBUTTON] & 0x80)
236 mask |= GDK_BUTTON2_MASK;
237 if (kbd[VK_RBUTTON] & 0x80)
238 mask |= GDK_BUTTON3_MASK;
244 gdk_device_win32_query_state (GdkDevice *device,
246 GdkWindow **root_window,
247 GdkWindow **child_window,
252 GdkModifierType *mask)
258 g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), FALSE);
262 hwnd = GDK_WINDOW_HWND (window);
263 GetCursorPos (&point);
271 ScreenToClient (hwnd, &point);
279 if (window == _gdk_root)
282 *win_x += _gdk_offset_x;
285 *win_y += _gdk_offset_y;
290 hwndc = ChildWindowFromPoint (hwnd, point);
292 if (hwndc && hwndc != hwnd)
293 *child_window = gdk_win32_handle_table_lookup (hwndc);
295 *child_window = NULL; /* Direct child unknown to gdk */
302 screen = gdk_window_get_screen (window);
303 *root_window = gdk_screen_get_root_window (screen);
307 *mask = get_current_mask ();
313 gdk_device_win32_grab (GdkDevice *device,
315 gboolean owner_events,
316 GdkEventMask event_mask,
317 GdkWindow *confine_to,
321 if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
322 SetCapture (GDK_WINDOW_HWND (window));
324 return GDK_GRAB_SUCCESS;
328 gdk_device_win32_ungrab (GdkDevice *device,
331 GdkDeviceGrabInfo *info;
334 display = gdk_device_get_display (device);
335 info = _gdk_display_get_last_device_grab (display, device);
338 info->serial_end = 0;
340 if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
343 _gdk_display_device_grab_update (display, device, NULL, 0);
347 gdk_device_win32_window_at_position (GdkDevice *device,
350 GdkModifierType *mask,
351 gboolean get_toplevel)
358 GetCursorPos (&pointc);
360 hwnd = WindowFromPoint (point);
365 *win_x = pointc.x + _gdk_offset_x;
366 *win_y = pointc.y + _gdk_offset_y;
370 ScreenToClient (hwnd, &point);
375 (window = gdk_win32_handle_table_lookup (hwnd)) != NULL &&
376 GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
379 hwndc = ChildWindowFromPoint (hwnd, point);
380 ClientToScreen (hwnd, &point);
381 ScreenToClient (hwndc, &point);
383 while (hwndc != hwnd && (hwnd = hwndc, 1));
385 window = gdk_win32_handle_table_lookup (hwnd);
387 if (window && (win_x || win_y))
389 GetClientRect (hwnd, &rect);
390 *win_x = point.x - rect.left;
391 *win_y = point.y - rect.top;
398 gdk_device_win32_select_window_events (GdkDevice *device,
400 GdkEventMask event_mask)