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, see <http://www.gnu.org/licenses/>.
20 #include <gdk/gdkwindow.h>
25 #include "gdkdisplayprivate.h"
26 #include "gdkdevice-win32.h"
29 static gboolean gdk_device_win32_get_history (GdkDevice *device,
33 GdkTimeCoord ***events,
35 static void gdk_device_win32_get_state (GdkDevice *device,
38 GdkModifierType *mask);
39 static void gdk_device_win32_set_window_cursor (GdkDevice *device,
42 static void gdk_device_win32_warp (GdkDevice *device,
46 static gboolean gdk_device_win32_query_state (GdkDevice *device,
48 GdkWindow **root_window,
49 GdkWindow **child_window,
54 GdkModifierType *mask);
55 static GdkGrabStatus gdk_device_win32_grab (GdkDevice *device,
57 gboolean owner_events,
58 GdkEventMask event_mask,
59 GdkWindow *confine_to,
62 static void gdk_device_win32_ungrab (GdkDevice *device,
64 static GdkWindow * gdk_device_win32_window_at_position (GdkDevice *device,
67 GdkModifierType *mask,
68 gboolean get_toplevel);
69 static void gdk_device_win32_select_window_events (GdkDevice *device,
71 GdkEventMask event_mask);
74 G_DEFINE_TYPE (GdkDeviceWin32, gdk_device_win32, GDK_TYPE_DEVICE)
77 gdk_device_win32_class_init (GdkDeviceWin32Class *klass)
79 GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
81 device_class->get_history = gdk_device_win32_get_history;
82 device_class->get_state = gdk_device_win32_get_state;
83 device_class->set_window_cursor = gdk_device_win32_set_window_cursor;
84 device_class->warp = gdk_device_win32_warp;
85 device_class->query_state = gdk_device_win32_query_state;
86 device_class->grab = gdk_device_win32_grab;
87 device_class->ungrab = gdk_device_win32_ungrab;
88 device_class->window_at_position = gdk_device_win32_window_at_position;
89 device_class->select_window_events = gdk_device_win32_select_window_events;
93 gdk_device_win32_init (GdkDeviceWin32 *device_win32)
97 device = GDK_DEVICE (device_win32);
99 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_X, 0, 0, 1);
100 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_Y, 0, 0, 1);
104 gdk_device_win32_get_history (GdkDevice *device,
108 GdkTimeCoord ***events,
115 gdk_device_win32_get_state (GdkDevice *device,
118 GdkModifierType *mask)
122 gdk_window_get_pointer (window, &x_int, &y_int, mask);
132 gdk_device_win32_set_window_cursor (GdkDevice *device,
136 GdkWin32Cursor *cursor_private;
137 GdkWindow *parent_window;
138 GdkWindowImplWin32 *impl;
142 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
143 cursor_private = (GdkWin32Cursor*) cursor;
145 hprevcursor = impl->hcursor;
150 hcursor = cursor_private->hcursor;
154 /* If the pointer is over our window, set new cursor */
155 GdkWindow *curr_window = gdk_window_get_pointer (window, NULL, NULL, NULL);
157 if (curr_window == window ||
158 (curr_window && window == gdk_window_get_toplevel (curr_window)))
162 /* Climb up the tree and find whether our window is the
163 * first ancestor that has cursor defined, and if so, set
166 while (curr_window && curr_window->impl &&
167 !GDK_WINDOW_IMPL_WIN32 (curr_window->impl)->hcursor)
169 curr_window = curr_window->parent;
170 if (curr_window == GDK_WINDOW (window))
179 /* Unset the previous cursor: Need to make sure it's no longer in
180 * use before we destroy it, in case we're not over our window but
181 * the cursor is still set to our old one.
183 if (hprevcursor != NULL &&
184 GetCursor () == hprevcursor)
186 /* Look for a suitable cursor to use instead */
188 parent_window = GDK_WINDOW (window)->parent;
190 while (hcursor == NULL)
194 impl = GDK_WINDOW_IMPL_WIN32 (parent_window->impl);
195 hcursor = impl->hcursor;
196 parent_window = parent_window->parent;
199 hcursor = LoadCursor (NULL, IDC_ARROW);
207 gdk_device_win32_warp (GdkDevice *device,
212 SetCursorPos (x - _gdk_offset_x, y - _gdk_offset_y);
215 static GdkModifierType
216 get_current_mask (void)
218 GdkModifierType mask;
221 GetKeyboardState (kbd);
223 if (kbd[VK_SHIFT] & 0x80)
224 mask |= GDK_SHIFT_MASK;
225 if (kbd[VK_CAPITAL] & 0x80)
226 mask |= GDK_LOCK_MASK;
227 if (kbd[VK_CONTROL] & 0x80)
228 mask |= GDK_CONTROL_MASK;
229 if (kbd[VK_MENU] & 0x80)
230 mask |= GDK_MOD1_MASK;
231 if (kbd[VK_LBUTTON] & 0x80)
232 mask |= GDK_BUTTON1_MASK;
233 if (kbd[VK_MBUTTON] & 0x80)
234 mask |= GDK_BUTTON2_MASK;
235 if (kbd[VK_RBUTTON] & 0x80)
236 mask |= GDK_BUTTON3_MASK;
242 gdk_device_win32_query_state (GdkDevice *device,
244 GdkWindow **root_window,
245 GdkWindow **child_window,
250 GdkModifierType *mask)
256 g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), FALSE);
260 hwnd = GDK_WINDOW_HWND (window);
261 GetCursorPos (&point);
269 ScreenToClient (hwnd, &point);
277 if (window == _gdk_root)
280 *win_x += _gdk_offset_x;
283 *win_y += _gdk_offset_y;
288 hwndc = ChildWindowFromPoint (hwnd, point);
290 if (hwndc && hwndc != hwnd)
291 *child_window = gdk_win32_handle_table_lookup (hwndc);
293 *child_window = NULL; /* Direct child unknown to gdk */
300 screen = gdk_window_get_screen (window);
301 *root_window = gdk_screen_get_root_window (screen);
305 *mask = get_current_mask ();
309 gdk_device_win32_grab (GdkDevice *device,
311 gboolean owner_events,
312 GdkEventMask event_mask,
313 GdkWindow *confine_to,
317 GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
319 GdkWin32Cursor *cursor_private;
321 cursor_private = (GdkWin32Cursor*) cursor;
323 if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
327 else if ((hcursor = CopyCursor (cursor_private->hcursor)) == NULL)
328 WIN32_API_FAILED ("CopyCursor");
330 if (_gdk_win32_grab_cursor != NULL)
332 if (GetCursor () == _gdk_win32_grab_cursor)
334 DestroyCursor (_gdk_win32_grab_cursor);
337 _gdk_win32_grab_cursor = hcursor;
339 if (_gdk_win32_grab_cursor != NULL)
340 SetCursor (_gdk_win32_grab_cursor);
341 else if (impl->hcursor != NULL)
342 SetCursor (impl->hcursor);
344 SetCursor (LoadCursor (NULL, IDC_ARROW));
346 SetCapture (GDK_WINDOW_HWND (window));
349 return GDK_GRAB_SUCCESS;
353 gdk_device_win32_ungrab (GdkDevice *device,
356 GdkDeviceGrabInfo *info;
359 display = gdk_device_get_display (device);
360 info = _gdk_display_get_last_device_grab (display, device);
363 info->serial_end = 0;
365 if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
367 if (_gdk_win32_grab_cursor != NULL)
369 if (GetCursor () == _gdk_win32_grab_cursor)
371 DestroyCursor (_gdk_win32_grab_cursor);
373 _gdk_win32_grab_cursor = NULL;
378 _gdk_display_device_grab_update (display, device, NULL, 0);
382 screen_to_client (HWND hwnd, POINT screen_pt, POINT *client_pt)
384 *client_pt = screen_pt;
385 ScreenToClient (hwnd, client_pt);
389 gdk_device_win32_window_at_position (GdkDevice *device,
392 GdkModifierType *mask,
393 gboolean get_toplevel)
395 GdkWindow *window = NULL;
396 POINT screen_pt, client_pt;
400 GetCursorPos (&screen_pt);
404 /* Only consider visible children of the desktop to avoid the various
405 * non-visible windows you often find on a running Windows box. These
406 * might overlap our windows and cause our walk to fail. As we assume
407 * WindowFromPoint() can find our windows, we follow similar logic
408 * here, and ignore invisible and disabled windows.
410 hwnd = GetDesktopWindow ();
412 window = gdk_win32_handle_table_lookup (hwnd);
414 if (window != NULL &&
415 GDK_WINDOW_TYPE (window) != GDK_WINDOW_ROOT &&
416 GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
419 screen_to_client (hwnd, screen_pt, &client_pt);
420 hwndc = ChildWindowFromPointEx (hwnd, client_pt, CWP_SKIPDISABLED |
423 /* Verify that we're really inside the client area of the window */
426 GetClientRect (hwndc, &rect);
427 screen_to_client (hwndc, screen_pt, &client_pt);
428 if (!PtInRect (&rect, client_pt))
432 } while (hwndc != hwnd && (hwnd = hwndc, 1));
437 hwnd = WindowFromPoint (screen_pt);
439 /* Verify that we're really inside the client area of the window */
440 GetClientRect (hwnd, &rect);
441 screen_to_client (hwnd, screen_pt, &client_pt);
442 if (!PtInRect (&rect, client_pt))
445 /* If we didn't hit any window at that point, return the desktop */
449 *win_x = screen_pt.x + _gdk_offset_x;
451 *win_y = screen_pt.y + _gdk_offset_y;
455 window = gdk_win32_handle_table_lookup (hwnd);
458 if (window && (win_x || win_y))
461 *win_x = client_pt.x;
463 *win_y = client_pt.y;
470 gdk_device_win32_select_window_events (GdkDevice *device,
472 GdkEventMask event_mask)