Debian Codegraph @

gdk/win32/gdkevents-win32.c

raw
1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 1998-2002 Tor Lillqvist
4 * Copyright (C) 2001,2009 Hans Breuer
5 * Copyright (C) 2007-2009 Cody Russell
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library 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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22 * Modified by the GTK+ Team and others 1997-2020. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26 */
27
28 /* Cannot use TrackMouseEvent, as the stupid WM_MOUSELEAVE message
29 * doesn’t tell us where the mouse has gone. Thus we cannot use it to
30 * generate a correct GdkNotifyType. Pity, as using TrackMouseEvent
31 * otherwise would make it possible to reliably generate
32 * GDK_LEAVE_NOTIFY events, which would help get rid of those pesky
33 * tooltips sometimes popping up in the wrong place.
34 *
35 * Update: a combination of TrackMouseEvent, GetCursorPos and
36 * GetWindowPos can and is actually used to get rid of those
37 * pesky tooltips. It should be possible to use this for the
38 * whole ENTER/LEAVE NOTIFY handling but some platforms may
39 * not have TrackMouseEvent at all (?) --hb
40 */
41
42 #include "config.h"
43
44 #include "gdkprivate-win32.h"
45
46 #include <glib/gprintf.h>
47 #include <cairo-win32.h>
48
49 #include "gdk.h"
50 #include "gdkdisplayprivate.h"
51 #include "gdkmonitorprivate.h"
52 #include "gdkwin32.h"
53 #include "gdkkeysyms.h"
54 #include "gdkglcontext-win32.h"
55 #include "gdkdevicemanager-win32.h"
56 #include "gdkdeviceprivate.h"
57 #include "gdkdevice-virtual.h"
58 #include "gdkdevice-wintab.h"
59 #include "gdkwin32dnd.h"
60 #include "gdkdisplay-win32.h"
61 #include "gdkselection-win32.h"
62 #include "gdkdndprivate.h"
63
64 #include <windowsx.h>
65 #include <tpcshrd.h>
66 #include "winpointer.h"
67
68 #ifdef G_WITH_CYGWIN
69 #include <fcntl.h>
70 #include <errno.h>
71 #endif
72
73 #include <objbase.h>
74
75 #include <imm.h>
76
77 #ifndef XBUTTON1
78 #define XBUTTON1 1
79 #define XBUTTON2 2
80 #endif
81
82 #ifndef VK_XBUTTON1
83 #define VK_XBUTTON1 5
84 #define VK_XBUTTON2 6
85 #endif
86
87 #ifndef MK_XBUTTON1
88 #define MK_XBUTTON1 32
89 #define MK_XBUTTON2 64
90 #endif
91
92 /* Undefined flags: */
93 #define SWP_NOCLIENTSIZE 0x0800
94 #define SWP_NOCLIENTMOVE 0x1000
95 #define SWP_STATECHANGED 0x8000
96 /*
97 * Private function declarations
98 */
99
100 #define SYNAPSIS_ICON_WINDOW_CLASS "SynTrackCursorWindowClass"
101
102 static gboolean gdk_event_translate (MSG *msg,
103 gint *ret_valp);
104 static gboolean gdk_event_prepare (GSource *source,
105 gint *timeout);
106 static gboolean gdk_event_check (GSource *source);
107 static gboolean gdk_event_dispatch (GSource *source,
108 GSourceFunc callback,
109 gpointer user_data);
110
111 /* Private variable declarations
112 */
113
114 static GList *client_filters; /* Filters for client messages */
115 extern gint _gdk_input_ignore_core;
116
117 GdkCursor *_gdk_win32_grab_cursor;
118
119 typedef struct
120 {
121 GSource source;
122
123 GdkDisplay *display;
124 GPollFD event_poll_fd;
125 } GdkWin32EventSource;
126
127 static GSourceFuncs event_funcs = {
128 gdk_event_prepare,
129 gdk_event_check,
130 gdk_event_dispatch,
131 NULL
132 };
133
134 static GdkWindow *mouse_window = NULL;
135 static GdkWindow *mouse_window_ignored_leave = NULL;
136 static gint current_x, current_y;
137 static gint current_root_x, current_root_y;
138 static UINT client_message;
139
140 static UINT got_gdk_events_message;
141 static HWND modal_win32_dialog = NULL;
142
143 #if 0
144 static HKL latin_locale = NULL;
145 #endif
146
147 static gboolean in_ime_composition = FALSE;
148 static UINT modal_timer;
149 static UINT sync_timer = 0;
150
151 static int debug_indent = 0;
152
153 static int both_shift_pressed[2]; /* to store keycodes for shift keys */
154
155 /* low-level keyboard hook handle */
156 static HHOOK keyboard_hook = NULL;
157 static UINT aerosnap_message;
158
159 static gboolean pen_touch_input;
160 static POINT pen_touch_cursor_position;
161 static LONG last_digitizer_time;
162
163 static void
164 track_mouse_event (DWORD dwFlags,
165 HWND hwnd)
166 {
167 TRACKMOUSEEVENT tme;
168
169 tme.cbSize = sizeof(TRACKMOUSEEVENT);
170 tme.dwFlags = dwFlags;
171 tme.hwndTrack = hwnd;
172 tme.dwHoverTime = HOVER_DEFAULT; /* not used */
173
174 if (!TrackMouseEvent (&tme))
175 WIN32_API_FAILED ("TrackMouseEvent");
176 else if (dwFlags == TME_LEAVE)
177 GDK_NOTE (EVENTS, g_print(" (TrackMouseEvent %p)", hwnd));
178 else if (dwFlags == TME_CANCEL)
179 GDK_NOTE (EVENTS, g_print(" (cancel TrackMouseEvent %p)", hwnd));
180 }
181
182 gulong
183 _gdk_win32_get_next_tick (gulong suggested_tick)
184 {
185 static gulong cur_tick = 0;
186
187 if (suggested_tick == 0)
188 suggested_tick = GetTickCount ();
189 /* Ticks eventually wrap around.
190 * This works as long as the interval between ticks is < 2147483648ms */
191 if (suggested_tick <= cur_tick && ((cur_tick - suggested_tick) < 0x7FFFFFFF))
192 return cur_tick;
193 else
194 return cur_tick = suggested_tick;
195 }
196
197 BOOL
198 _gdk_win32_get_cursor_pos (LPPOINT lpPoint)
199 {
200 if (pen_touch_input)
201 {
202 *lpPoint = pen_touch_cursor_position;
203 return TRUE;
204 }
205 else
206 return GetCursorPos (lpPoint);
207 }
208
209 static void
210 generate_focus_event (GdkDeviceManager *device_manager,
211 GdkWindow *window,
212 gboolean in)
213 {
214 GdkDevice *device;
215 GdkDevice *source_device;
216 GdkEvent *event;
217
218 device = GDK_DEVICE_MANAGER_WIN32 (device_manager)->core_keyboard;
219 source_device = GDK_DEVICE_MANAGER_WIN32 (device_manager)->system_keyboard;
220
221 event = gdk_event_new (GDK_FOCUS_CHANGE);
222 event->focus_change.window = window;
223 event->focus_change.in = in;
224 gdk_event_set_device (event, device);
225 gdk_event_set_source_device (event, source_device);
226 gdk_event_set_seat (event, gdk_device_get_seat (device));
227
228 _gdk_win32_append_event (event);
229 }
230
231 static void
232 generate_grab_broken_event (GdkDeviceManager *device_manager,
233 GdkWindow *window,
234 gboolean keyboard,
235 GdkWindow *grab_window)
236 {
237 GdkEvent *event = gdk_event_new (GDK_GRAB_BROKEN);
238 GdkDevice *device;
239 GdkDevice *source_device;
240
241 if (keyboard)
242 {
243 device = GDK_DEVICE_MANAGER_WIN32 (device_manager)->core_keyboard;
244 source_device = GDK_DEVICE_MANAGER_WIN32 (device_manager)->system_keyboard;
245 }
246 else
247 {
248 device = GDK_DEVICE_MANAGER_WIN32 (device_manager)->core_pointer;
249 source_device = GDK_DEVICE_MANAGER_WIN32 (device_manager)->system_pointer;
250 _gdk_device_virtual_set_active (device, source_device);
251 }
252
253 event->grab_broken.window = window;
254 event->grab_broken.send_event = 0;
255 event->grab_broken.keyboard = keyboard;
256 event->grab_broken.implicit = FALSE;
257 event->grab_broken.grab_window = grab_window;
258 gdk_event_set_device (event, device);
259 gdk_event_set_source_device (event, source_device);
260 gdk_event_set_seat (event, gdk_device_get_seat (device));
261
262 _gdk_win32_append_event (event);
263 }
264
265 static LRESULT
266 inner_window_procedure (HWND hwnd,
267 UINT message,
268 WPARAM wparam,
269 LPARAM lparam)
270 {
271 MSG msg;
272 DWORD pos;
273 gint ret_val = 0;
274
275 msg.hwnd = hwnd;
276 msg.message = message;
277 msg.wParam = wparam;
278 msg.lParam = lparam;
279 msg.time = _gdk_win32_get_next_tick (0);
280 pos = GetMessagePos ();
281 msg.pt.x = GET_X_LPARAM (pos);
282 msg.pt.y = GET_Y_LPARAM (pos);
283
284 if (gdk_event_translate (&msg, &ret_val))
285 {
286 /* If gdk_event_translate() returns TRUE, we return ret_val from
287 * the window procedure.
288 */
289 if (modal_win32_dialog)
290 PostMessageW (modal_win32_dialog, got_gdk_events_message,
291 (WPARAM) 1, 0);
292 return ret_val;
293 }
294 else
295 {
296 /* Otherwise call DefWindowProcW(). */
297 GDK_NOTE (EVENTS, g_print (" DefWindowProcW"));
298 return DefWindowProcW (hwnd, message, wparam, lparam);
299 }
300 }
301
302 LRESULT CALLBACK
303 _gdk_win32_window_procedure (HWND hwnd,
304 UINT message,
305 WPARAM wparam,
306 LPARAM lparam)
307 {
308 LRESULT retval;
309
310 GDK_NOTE (EVENTS, g_print ("%s%*s%s %p %#x %#lx",
311 (debug_indent > 0 ? "\n" : ""),
312 debug_indent, "",
313 _gdk_win32_message_to_string (message), hwnd,
314 wparam, lparam));
315 debug_indent += 2;
316 retval = inner_window_procedure (hwnd, message, wparam, lparam);
317 debug_indent -= 2;
318
319 GDK_NOTE (EVENTS, g_print (" => %" G_GINT64_FORMAT "%s", (gint64) retval, (debug_indent == 0 ? "\n" : "")));
320
321 return retval;
322 }
323
324 static LRESULT
325 low_level_keystroke_handler (WPARAM message,
326 KBDLLHOOKSTRUCT *kbdhook,
327 GdkWindow *window)
328 {
329 GdkWindow *toplevel = gdk_window_get_toplevel (window);
330 static DWORD last_keydown = 0;
331
332 if (message == WM_KEYDOWN &&
333 !GDK_WINDOW_DESTROYED (toplevel) &&
334 _gdk_win32_window_lacks_wm_decorations (toplevel) && /* For CSD only */
335 last_keydown != kbdhook->vkCode &&
336 ((GetKeyState (VK_LWIN) & 0x8000) ||
337 (GetKeyState (VK_RWIN) & 0x8000)))
338 {
339 GdkWin32AeroSnapCombo combo = GDK_WIN32_AEROSNAP_COMBO_NOTHING;
340 gboolean lshiftdown = GetKeyState (VK_LSHIFT) & 0x8000;
341 gboolean rshiftdown = GetKeyState (VK_RSHIFT) & 0x8000;
342 gboolean oneshiftdown = (lshiftdown || rshiftdown) && !(lshiftdown && rshiftdown);
343 gboolean maximized = gdk_window_get_state (toplevel) & GDK_WINDOW_STATE_MAXIMIZED;
344
345 switch (kbdhook->vkCode)
346 {
347 case VK_UP:
348 combo = GDK_WIN32_AEROSNAP_COMBO_UP;
349 break;
350 case VK_DOWN:
351 combo = GDK_WIN32_AEROSNAP_COMBO_DOWN;
352 break;
353 case VK_LEFT:
354 combo = GDK_WIN32_AEROSNAP_COMBO_LEFT;
355 break;
356 case VK_RIGHT:
357 combo = GDK_WIN32_AEROSNAP_COMBO_RIGHT;
358 break;
359 }
360
361 if (oneshiftdown && combo != GDK_WIN32_AEROSNAP_COMBO_NOTHING)
362 combo += 4;
363
364 /* These are the only combos that Windows WM does handle for us */
365 if (combo == GDK_WIN32_AEROSNAP_COMBO_SHIFTLEFT ||
366 combo == GDK_WIN32_AEROSNAP_COMBO_SHIFTRIGHT)
367 combo = GDK_WIN32_AEROSNAP_COMBO_NOTHING;
368
369 /* On Windows 10 the WM will handle this specific combo */
370 if (combo == GDK_WIN32_AEROSNAP_COMBO_DOWN && maximized &&
371 g_win32_check_windows_version (6, 4, 0, G_WIN32_OS_ANY))
372 combo = GDK_WIN32_AEROSNAP_COMBO_NOTHING;
373
374 if (combo != GDK_WIN32_AEROSNAP_COMBO_NOTHING)
375 PostMessage (GDK_WINDOW_HWND (toplevel), aerosnap_message, (WPARAM) combo, 0);
376 }
377
378 if (message == WM_KEYDOWN)
379 last_keydown = kbdhook->vkCode;
380 else if (message = WM_KEYUP && last_keydown == kbdhook->vkCode)
381 last_keydown = 0;
382
383 return 0;
384 }
385
386 static LRESULT CALLBACK
387 low_level_keyboard_proc (int code,
388 WPARAM wParam,
389 LPARAM lParam)
390 {
391 KBDLLHOOKSTRUCT *kbdhook;
392 HWND kbd_focus_owner;
393 GdkWindow *gdk_kbd_focus_owner;
394 LRESULT chain;
395
396 do
397 {
398 if (code < 0)
399 break;
400
401 kbd_focus_owner = GetFocus ();
402
403 if (kbd_focus_owner == NULL)
404 break;
405
406 gdk_kbd_focus_owner = gdk_win32_handle_table_lookup (kbd_focus_owner);
407
408 if (gdk_kbd_focus_owner == NULL)
409 break;
410
411 kbdhook = (KBDLLHOOKSTRUCT *) lParam;
412 chain = low_level_keystroke_handler (wParam, kbdhook, gdk_kbd_focus_owner);
413
414 if (chain != 0)
415 return chain;
416 } while (FALSE);
417
418 return CallNextHookEx (0, code, wParam, lParam);
419 }
420
421 static void
422 set_up_low_level_keyboard_hook (void)
423 {
424 HHOOK hook_handle;
425
426 if (keyboard_hook != NULL)
427 return;
428
429 hook_handle = SetWindowsHookEx (WH_KEYBOARD_LL,
430 (HOOKPROC) low_level_keyboard_proc,
431 _gdk_dll_hinstance,
432 0);
433
434 if (hook_handle != NULL)
435 keyboard_hook = hook_handle;
436 else
437 WIN32_API_FAILED ("SetWindowsHookEx");
438
439 aerosnap_message = RegisterWindowMessage ("GDK_WIN32_AEROSNAP_MESSAGE");
440 }
441
442 void
443 _gdk_events_init (GdkDisplay *display)
444 {
445 GSource *source;
446 GdkWin32EventSource *event_source;
447
448 #if 0
449 int i, j, n;
450
451 /* List of languages that use a latin keyboard. Somewhat sorted in
452 * "order of least surprise", in case we have to load one of them if
453 * the user only has arabic loaded, for instance.
454 */
455 static int latin_languages[] = {
456 LANG_ENGLISH,
457 LANG_SPANISH,
458 LANG_PORTUGUESE,
459 LANG_FRENCH,
460 LANG_GERMAN,
461 /* Rest in numeric order */
462 LANG_CZECH,
463 LANG_DANISH,
464 LANG_FINNISH,
465 LANG_HUNGARIAN,
466 LANG_ICELANDIC,
467 LANG_ITALIAN,
468 LANG_DUTCH,
469 LANG_NORWEGIAN,
470 LANG_POLISH,
471 LANG_ROMANIAN,
472 LANG_SLOVAK,
473 LANG_ALBANIAN,
474 LANG_SWEDISH,
475 LANG_TURKISH,
476 LANG_INDONESIAN,
477 LANG_SLOVENIAN,
478 LANG_ESTONIAN,
479 LANG_LATVIAN,
480 LANG_LITHUANIAN,
481 LANG_VIETNAMESE,
482 LANG_AFRIKAANS,
483 LANG_FAEROESE
484 #ifdef LANG_SWAHILI
485 ,LANG_SWAHILI
486 #endif
487 };
488 #endif
489
490 client_message = RegisterWindowMessage ("GDK_WIN32_CLIENT_MESSAGE");
491 got_gdk_events_message = RegisterWindowMessage ("GDK_WIN32_GOT_EVENTS");
492
493 #if 0
494 /* Check if we have some input locale identifier loaded that uses a
495 * latin keyboard, to be able to get the virtual-key code for the
496 * latin characters corresponding to ASCII control characters.
497 */
498 if ((n = GetKeyboardLayoutList (0, NULL)) == 0)
499 WIN32_API_FAILED ("GetKeyboardLayoutList");
500 else
501 {
502 HKL *hkl_list = g_new (HKL, n);
503 if (GetKeyboardLayoutList (n, hkl_list) == 0)
504 WIN32_API_FAILED ("GetKeyboardLayoutList");
505 else
506 {
507 for (i = 0; latin_locale == NULL && i < n; i++)
508 for (j = 0; j < G_N_ELEMENTS (latin_languages); j++)
509 if (PRIMARYLANGID (LOWORD (hkl_list[i])) == latin_languages[j])
510 {
511 latin_locale = hkl_list [i];
512 break;
513 }
514 }
515 g_free (hkl_list);
516 }
517
518 if (latin_locale == NULL)
519 {
520 /* Try to load a keyboard layout with latin characters then.
521 */
522 i = 0;
523 while (latin_locale == NULL && i < G_N_ELEMENTS (latin_languages))
524 {
525 char id[9];
526 g_sprintf (id, "%08x", MAKELANGID (latin_languages[i++], SUBLANG_DEFAULT));
527 latin_locale = LoadKeyboardLayout (id, KLF_NOTELLSHELL|KLF_SUBSTITUTE_OK);
528 }
529 }
530
531 GDK_NOTE (EVENTS, g_print ("latin_locale = %08x\n", (guint) latin_locale));
532 #endif
533
534 source = g_source_new (&event_funcs, sizeof (GdkWin32EventSource));
535 g_source_set_name (source, "GDK Win32 event source");
536 g_source_set_priority (source, GDK_PRIORITY_EVENTS);
537
538 event_source = (GdkWin32EventSource *)source;
539 event_source->display = display;
540 #ifdef G_WITH_CYGWIN
541 event_source->event_poll_fd.fd = open ("/dev/windows", O_RDONLY);
542 if (event_source->event_poll_fd.fd == -1)
543 g_error ("can't open \"/dev/windows\": %s", g_strerror (errno));
544 #else
545 event_source->event_poll_fd.fd = G_WIN32_MSG_HANDLE;
546 #endif
547 event_source->event_poll_fd.events = G_IO_IN;
548
549 g_source_add_poll (source, &event_source->event_poll_fd);
550 g_source_set_can_recurse (source, TRUE);
551 g_source_attach (source, NULL);
552
553 set_up_low_level_keyboard_hook ();
554 }
555
556 gboolean
557 _gdk_win32_display_has_pending (GdkDisplay *display)
558 {
559 return (_gdk_event_queue_find_first (display) ||
560 (modal_win32_dialog == NULL &&
561 GetQueueStatus (QS_ALLINPUT) != 0));
562 }
563
564 #if 0 /* Unused, but might be useful to re-introduce in some debugging output? */
565
566 static char *
567 event_mask_string (GdkEventMask mask)
568 {
569 static char bfr[500];
570 char *p = bfr;
571
572 *p = '\0';
573 #define BIT(x) \
574 if (mask & GDK_##x##_MASK) \
575 p += g_sprintf (p, "%s" #x, (p > bfr ? " " : ""))
576 BIT (EXPOSURE);
577 BIT (POINTER_MOTION);
578 BIT (POINTER_MOTION_HINT);
579 BIT (BUTTON_MOTION);
580 BIT (BUTTON1_MOTION);
581 BIT (BUTTON2_MOTION);
582 BIT (BUTTON3_MOTION);
583 BIT (BUTTON_PRESS);
584 BIT (BUTTON_RELEASE);
585 BIT (KEY_PRESS);
586 BIT (KEY_RELEASE);
587 BIT (ENTER_NOTIFY);
588 BIT (LEAVE_NOTIFY);
589 BIT (FOCUS_CHANGE);
590 BIT (STRUCTURE);
591 BIT (PROPERTY_CHANGE);
592 BIT (VISIBILITY_NOTIFY);
593 BIT (PROXIMITY_IN);
594 BIT (PROXIMITY_OUT);
595 BIT (SUBSTRUCTURE);
596 BIT (SCROLL);
597 #undef BIT
598
599 return bfr;
600 }
601
602 #endif
603
604 static GdkWindow *
605 find_window_for_mouse_event (GdkWindow* reported_window,
606 MSG* msg)
607 {
608 POINT pt;
609 GdkDisplay *display;
610 GdkDeviceManagerWin32 *device_manager;
611 GdkWindow *event_window;
612 HWND hwnd;
613 RECT rect;
614 GdkDeviceGrabInfo *grab;
615
616 display = gdk_display_get_default ();
617 device_manager = GDK_DEVICE_MANAGER_WIN32 (gdk_display_get_device_manager (display));
618
619 grab = _gdk_display_get_last_device_grab (display, device_manager->core_pointer);
620 if (grab == NULL)
621 return reported_window;
622
623 pt = msg->pt;
624
625 if (!grab->owner_events)
626 event_window = grab->native_window;
627 else
628 {
629 event_window = NULL;
630 hwnd = WindowFromPoint (pt);
631 if (hwnd != NULL)
632 {
633 POINT client_pt = pt;
634
635 ScreenToClient (hwnd, &client_pt);
636 GetClientRect (hwnd, &rect);
637 if (PtInRect (&rect, client_pt))
638 event_window = gdk_win32_handle_table_lookup (hwnd);
639 }
640 if (event_window == NULL)
641 event_window = grab->native_window;
642 }
643
644 /* need to also adjust the coordinates to the new window */
645 ScreenToClient (GDK_WINDOW_HWND (event_window), &pt);
646
647 /* ATTENTION: need to update client coords */
648 msg->lParam = MAKELPARAM (pt.x, pt.y);
649
650 return event_window;
651 }
652
653 static void
654 build_key_event_state (GdkEvent *event,
655 BYTE *key_state)
656 {
657 GdkWin32Keymap *keymap;
658 keymap = GDK_WIN32_KEYMAP (_gdk_win32_display_get_keymap (_gdk_display));
659
660 event->key.state = _gdk_win32_keymap_get_mod_mask (keymap);
661
662 if (key_state[VK_CAPITAL] & 0x01)
663 event->key.state |= GDK_LOCK_MASK;
664
665 if (key_state[VK_LBUTTON] & 0x80)
666 event->key.state |= GDK_BUTTON1_MASK;
667 if (key_state[VK_MBUTTON] & 0x80)
668 event->key.state |= GDK_BUTTON2_MASK;
669 if (key_state[VK_RBUTTON] & 0x80)
670 event->key.state |= GDK_BUTTON3_MASK;
671 if (key_state[VK_XBUTTON1] & 0x80)
672 event->key.state |= GDK_BUTTON4_MASK;
673 if (key_state[VK_XBUTTON2] & 0x80)
674 event->key.state |= GDK_BUTTON5_MASK;
675
676 event->key.group = _gdk_win32_keymap_get_active_group (keymap);
677 }
678
679 static gint
680 build_pointer_event_state (MSG *msg)
681 {
682 gint state;
683
684 state = 0;
685
686 if (msg->wParam & MK_CONTROL)
687 state |= GDK_CONTROL_MASK;
688
689 if ((msg->message != WM_LBUTTONDOWN &&
690 (msg->wParam & MK_LBUTTON)) ||
691 msg->message == WM_LBUTTONUP)
692 state |= GDK_BUTTON1_MASK;
693
694 if ((msg->message != WM_MBUTTONDOWN &&
695 (msg->wParam & MK_MBUTTON)) ||
696 msg->message == WM_MBUTTONUP)
697 state |= GDK_BUTTON2_MASK;
698
699 if ((msg->message != WM_RBUTTONDOWN &&
700 (msg->wParam & MK_RBUTTON)) ||
701 msg->message == WM_RBUTTONUP)
702 state |= GDK_BUTTON3_MASK;
703
704 if (((msg->message != WM_XBUTTONDOWN || HIWORD (msg->wParam) != XBUTTON1) &&
705 (msg->wParam & MK_XBUTTON1)) ||
706 (msg->message == WM_XBUTTONUP && HIWORD (msg->wParam) == XBUTTON1))
707 state |= GDK_BUTTON4_MASK;
708
709 if (((msg->message != WM_XBUTTONDOWN || HIWORD (msg->wParam) != XBUTTON2) &&
710 (msg->wParam & MK_XBUTTON2)) ||
711 (msg->message == WM_XBUTTONUP && HIWORD (msg->wParam) == XBUTTON2))
712 state |= GDK_BUTTON5_MASK;
713
714 if (msg->wParam & MK_SHIFT)
715 state |= GDK_SHIFT_MASK;
716
717 if (GetKeyState (VK_MENU) < 0)
718 state |= GDK_MOD1_MASK;
719
720 if (GetKeyState (VK_CAPITAL) & 0x1)
721 state |= GDK_LOCK_MASK;
722
723 return state;
724 }
725
726 static void
727 build_wm_ime_composition_event (GdkEvent *event,
728 MSG *msg,
729 wchar_t wc,
730 BYTE *key_state)
731 {
732 event->key.time = _gdk_win32_get_next_tick (msg->time);
733
734 build_key_event_state (event, key_state);
735
736 event->key.hardware_keycode = 0; /* FIXME: What should it be? */
737 event->key.string = NULL;
738 event->key.length = 0;
739 event->key.keyval = gdk_unicode_to_keyval (wc);
740 }
741
742 #ifdef G_ENABLE_DEBUG
743
744 static void
745 print_event_state (guint state)
746 {
747 #define CASE(bit) if (state & GDK_ ## bit ## _MASK) g_print (#bit " ");
748 CASE (SHIFT);
749 CASE (LOCK);
750 CASE (CONTROL);
751 CASE (MOD1);
752 CASE (MOD2);
753 CASE (MOD3);
754 CASE (MOD4);
755 CASE (MOD5);
756 CASE (BUTTON1);
757 CASE (BUTTON2);
758 CASE (BUTTON3);
759 CASE (BUTTON4);
760 CASE (BUTTON5);
761 #undef CASE
762 }
763
764 void
765 _gdk_win32_print_event (const GdkEvent *event)
766 {
767 gchar *escaped, *kvname;
768 gchar *selection_name, *target_name, *property_name;
769
770 g_print ("%s%*s===> ", (debug_indent > 0 ? "\n" : ""), debug_indent, "");
771 switch (event->any.type)
772 {
773 #define CASE(x) case x: g_print (#x); break;
774 CASE (GDK_NOTHING);
775 CASE (GDK_DELETE);
776 CASE (GDK_DESTROY);
777 CASE (GDK_EXPOSE);
778 CASE (GDK_MOTION_NOTIFY);
779 CASE (GDK_BUTTON_PRESS);
780 CASE (GDK_2BUTTON_PRESS);
781 CASE (GDK_3BUTTON_PRESS);
782 CASE (GDK_BUTTON_RELEASE);
783 CASE (GDK_KEY_PRESS);
784 CASE (GDK_KEY_RELEASE);
785 CASE (GDK_ENTER_NOTIFY);
786 CASE (GDK_LEAVE_NOTIFY);
787 CASE (GDK_FOCUS_CHANGE);
788 CASE (GDK_CONFIGURE);
789 CASE (GDK_MAP);
790 CASE (GDK_UNMAP);
791 CASE (GDK_PROPERTY_NOTIFY);
792 CASE (GDK_SELECTION_CLEAR);
793 CASE (GDK_SELECTION_REQUEST);
794 CASE (GDK_SELECTION_NOTIFY);
795 CASE (GDK_PROXIMITY_IN);
796 CASE (GDK_PROXIMITY_OUT);
797 CASE (GDK_DRAG_ENTER);
798 CASE (GDK_DRAG_LEAVE);
799 CASE (GDK_DRAG_MOTION);
800 CASE (GDK_DRAG_STATUS);
801 CASE (GDK_DROP_START);
802 CASE (GDK_DROP_FINISHED);
803 CASE (GDK_CLIENT_EVENT);
804 CASE (GDK_VISIBILITY_NOTIFY);
805 CASE (GDK_SCROLL);
806 CASE (GDK_WINDOW_STATE);
807 CASE (GDK_SETTING);
808 CASE (GDK_OWNER_CHANGE);
809 CASE (GDK_GRAB_BROKEN);
810 #undef CASE
811 default: g_assert_not_reached ();
812 }
813
814 g_print (" %p @ %ums ",
815 event->any.window ? GDK_WINDOW_HWND (event->any.window) : NULL,
816 gdk_event_get_time (event));
817
818 switch (event->any.type)
819 {
820 case GDK_EXPOSE:
821 g_print ("%s %d",
822 _gdk_win32_gdkrectangle_to_string (&event->expose.area),
823 event->expose.count);
824 break;
825 case GDK_MOTION_NOTIFY:
826 g_print ("(%.4g,%.4g) (%.4g,%.4g) %s",
827 event->motion.x, event->motion.y,
828 event->motion.x_root, event->motion.y_root,
829 event->motion.is_hint ? "HINT " : "");
830 print_event_state (event->motion.state);
831 break;
832 case GDK_BUTTON_PRESS:
833 case GDK_2BUTTON_PRESS:
834 case GDK_3BUTTON_PRESS:
835 case GDK_BUTTON_RELEASE:
836 g_print ("%d (%.4g,%.4g) (%.4g,%.4g) ",
837 event->button.button,
838 event->button.x, event->button.y,
839 event->button.x_root, event->button.y_root);
840 print_event_state (event->button.state);
841 break;
842 case GDK_KEY_PRESS:
843 case GDK_KEY_RELEASE:
844 if (event->key.length == 0)
845 escaped = g_strdup ("");
846 else
847 escaped = g_strescape (event->key.string, NULL);
848 kvname = gdk_keyval_name (event->key.keyval);
849 g_print ("%#.02x group:%d %s %d:\"%s\" ",
850 event->key.hardware_keycode, event->key.group,
851 (kvname ? kvname : "??"),
852 event->key.length,
853 escaped);
854 g_free (escaped);
855 print_event_state (event->key.state);
856 break;
857 case GDK_ENTER_NOTIFY:
858 case GDK_LEAVE_NOTIFY:
859 g_print ("%p (%.4g,%.4g) (%.4g,%.4g) %s %s%s",
860 event->crossing.subwindow == NULL ? NULL : GDK_WINDOW_HWND (event->crossing.subwindow),
861 event->crossing.x, event->crossing.y,
862 event->crossing.x_root, event->crossing.y_root,
863 (event->crossing.mode == GDK_CROSSING_NORMAL ? "NORMAL" :
864 (event->crossing.mode == GDK_CROSSING_GRAB ? "GRAB" :
865 (event->crossing.mode == GDK_CROSSING_UNGRAB ? "UNGRAB" :
866 "???"))),
867 (event->crossing.detail == GDK_NOTIFY_ANCESTOR ? "ANCESTOR" :
868 (event->crossing.detail == GDK_NOTIFY_VIRTUAL ? "VIRTUAL" :
869 (event->crossing.detail == GDK_NOTIFY_INFERIOR ? "INFERIOR" :
870 (event->crossing.detail == GDK_NOTIFY_NONLINEAR ? "NONLINEAR" :
871 (event->crossing.detail == GDK_NOTIFY_NONLINEAR_VIRTUAL ? "NONLINEAR_VIRTUAL" :
872 (event->crossing.detail == GDK_NOTIFY_UNKNOWN ? "UNKNOWN" :
873 "???")))))),
874 event->crossing.focus ? " FOCUS" : "");
875 print_event_state (event->crossing.state);
876 break;
877 case GDK_FOCUS_CHANGE:
878 g_print ("%s", (event->focus_change.in ? "IN" : "OUT"));
879 break;
880 case GDK_CONFIGURE:
881 g_print ("x:%d y:%d w:%d h:%d",
882 event->configure.x, event->configure.y,
883 event->configure.width, event->configure.height);
884 break;
885 case GDK_SELECTION_CLEAR:
886 case GDK_SELECTION_REQUEST:
887 case GDK_SELECTION_NOTIFY:
888 selection_name = gdk_atom_name (event->selection.selection);
889 target_name = gdk_atom_name (event->selection.target);
890 property_name = gdk_atom_name (event->selection.property);
891 g_print ("sel:%s tgt:%s prop:%s",
892 selection_name, target_name, property_name);
893 g_free (selection_name);
894 g_free (target_name);
895 g_free (property_name);
896 break;
897 case GDK_DRAG_ENTER:
898 case GDK_DRAG_LEAVE:
899 case GDK_DRAG_MOTION:
900 case GDK_DRAG_STATUS:
901 case GDK_DROP_START:
902 case GDK_DROP_FINISHED:
903 if (event->dnd.context != NULL)
904 g_print ("ctx:%p: %s %s src:%p dest:%p",
905 event->dnd.context,
906 _gdk_win32_drag_protocol_to_string (event->dnd.context->protocol),
907 event->dnd.context->is_source ? "SOURCE" : "DEST",
908 event->dnd.context->source_window == NULL ? NULL : GDK_WINDOW_HWND (event->dnd.context->source_window),
909 event->dnd.context->dest_window == NULL ? NULL : GDK_WINDOW_HWND (event->dnd.context->dest_window));
910 break;
911 case GDK_CLIENT_EVENT:
912 /* no more GdkEventClient */
913 break;
914 case GDK_SCROLL:
915 g_print ("(%.4g,%.4g) (%.4g,%.4g) %s ",
916 event->scroll.x, event->scroll.y,
917 event->scroll.x_root, event->scroll.y_root,
918 (event->scroll.direction == GDK_SCROLL_UP ? "UP" :
919 (event->scroll.direction == GDK_SCROLL_DOWN ? "DOWN" :
920 (event->scroll.direction == GDK_SCROLL_LEFT ? "LEFT" :
921 (event->scroll.direction == GDK_SCROLL_RIGHT ? "RIGHT" :
922 "???")))));
923 print_event_state (event->scroll.state);
924 break;
925 case GDK_WINDOW_STATE:
926 g_print ("%s: %s",
927 _gdk_win32_window_state_to_string (event->window_state.changed_mask),
928 _gdk_win32_window_state_to_string (event->window_state.new_window_state));
929 case GDK_SETTING:
930 g_print ("%s: %s",
931 (event->setting.action == GDK_SETTING_ACTION_NEW ? "NEW" :
932 (event->setting.action == GDK_SETTING_ACTION_CHANGED ? "CHANGED" :
933 (event->setting.action == GDK_SETTING_ACTION_DELETED ? "DELETED" :
934 "???"))),
935 (event->setting.name ? event->setting.name : "NULL"));
936 case GDK_GRAB_BROKEN:
937 g_print ("%s %s %p",
938 (event->grab_broken.keyboard ? "KEYBOARD" : "POINTER"),
939 (event->grab_broken.implicit ? "IMPLICIT" : "EXPLICIT"),
940 (event->grab_broken.grab_window ? GDK_WINDOW_HWND (event->grab_broken.grab_window) : 0));
941 default:
942 /* Nothing */
943 break;
944 }
945 g_print ("%s", (debug_indent == 0 ? "\n" : ""));
946 }
947
948 static char *
949 decode_key_lparam (LPARAM lParam)
950 {
951 static char buf[100];
952 char *p = buf;
953
954 if (HIWORD (lParam) & KF_UP)
955 p += g_sprintf (p, "KF_UP ");
956 if (HIWORD (lParam) & KF_REPEAT)
957 p += g_sprintf (p, "KF_REPEAT ");
958 if (HIWORD (lParam) & KF_ALTDOWN)
959 p += g_sprintf (p, "KF_ALTDOWN ");
960 if (HIWORD (lParam) & KF_EXTENDED)
961 p += g_sprintf (p, "KF_EXTENDED ");
962 p += g_sprintf (p, "sc:%d rep:%d", LOBYTE (HIWORD (lParam)), LOWORD (lParam));
963
964 return buf;
965 }
966
967 #endif
968
969 static void
970 fixup_event (GdkEvent *event)
971 {
972 if (event->any.window)
973 g_object_ref (event->any.window);
974 if (((event->any.type == GDK_ENTER_NOTIFY) ||
975 (event->any.type == GDK_LEAVE_NOTIFY)) &&
976 (event->crossing.subwindow != NULL))
977 g_object_ref (event->crossing.subwindow);
978 if (((event->any.type == GDK_SELECTION_CLEAR) ||
979 (event->any.type == GDK_SELECTION_NOTIFY) ||
980 (event->any.type == GDK_SELECTION_REQUEST)) &&
981 (event->selection.requestor != NULL))
982 g_object_ref (event->selection.requestor);
983 if ((event->any.type == GDK_OWNER_CHANGE) &&
984 (event->owner_change.owner != NULL))
985 g_object_ref (event->owner_change.owner);
986 event->any.send_event = InSendMessage ();
987 }
988
989 void
990 _gdk_win32_append_event (GdkEvent *event)
991 {
992 GdkDisplay *display;
993 GList *link;
994
995 display = gdk_display_get_default ();
996
997 fixup_event (event);
998 #if 1
999 link = _gdk_event_queue_append (display, event);
1000 GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
1001 /* event morphing, the passed in may not be valid afterwards */
1002 _gdk_windowing_got_event (display, link, event, 0);
1003 #else
1004 _gdk_event_queue_append (display, event);
1005 GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
1006 #endif
1007 }
1008
1009 static void
1010 fill_key_event_string (GdkEvent *event)
1011 {
1012 gunichar c;
1013 gchar buf[256];
1014
1015 /* Fill in event->string crudely, since various programs
1016 * depend on it.
1017 */
1018
1019 c = 0;
1020 if (event->key.keyval != GDK_KEY_VoidSymbol)
1021 c = gdk_keyval_to_unicode (event->key.keyval);
1022
1023 if (c)
1024 {
1025 gsize bytes_written;
1026 gint len;
1027
1028 /* Apply the control key - Taken from Xlib
1029 */
1030 if (event->key.state & GDK_CONTROL_MASK)
1031 {
1032 if ((c >= '@' && c < '\177') || c == ' ')
1033 c &= 0x1F;
1034 else if (c == '2')
1035 {
1036 event->key.string = g_memdup ("\0\0", 2);
1037 event->key.length = 1;
1038 return;
1039 }
1040 else if (c >= '3' && c <= '7')
1041 c -= ('3' - '\033');
1042 else if (c == '8')
1043 c = '\177';
1044 else if (c == '/')
1045 c = '_' & 0x1F;
1046 }
1047
1048 len = g_unichar_to_utf8 (c, buf);
1049 buf[len] = '\0';
1050
1051 event->key.string = g_locale_from_utf8 (buf, len,
1052 NULL, &bytes_written,
1053 NULL);
1054 if (event->key.string)
1055 event->key.length = bytes_written;
1056 }
1057 else if (event->key.keyval == GDK_KEY_Escape)
1058 {
1059 event->key.length = 1;
1060 event->key.string = g_strdup ("\033");
1061 }
1062 else if (event->key.keyval == GDK_KEY_Return ||
1063 event->key.keyval == GDK_KEY_KP_Enter)
1064 {
1065 event->key.length = 1;
1066 event->key.string = g_strdup ("\r");
1067 }
1068
1069 if (!event->key.string)
1070 {
1071 event->key.length = 0;
1072 event->key.string = g_strdup ("");
1073 }
1074 }
1075
1076 static GdkFilterReturn
1077 apply_event_filters (GdkWindow *window,
1078 MSG *msg,
1079 GList **filters)
1080 {
1081 GdkFilterReturn result = GDK_FILTER_CONTINUE;
1082 GdkEvent *event;
1083 GdkDisplay *display;
1084 GList *node;
1085 GList *tmp_list;
1086
1087 event = gdk_event_new (GDK_NOTHING);
1088 event->any.window = g_object_ref (window);
1089 ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
1090
1091 display = gdk_display_get_default ();
1092
1093 /* I think GdkFilterFunc semantics require the passed-in event
1094 * to already be in the queue. The filter func can generate
1095 * more events and append them after it if it likes.
1096 */
1097 node = _gdk_event_queue_append (display, event);
1098
1099 tmp_list = *filters;
1100 while (tmp_list)
1101 {
1102 GdkEventFilter *filter = (GdkEventFilter *) tmp_list->data;
1103 GList *node;
1104
1105 if ((filter->flags & GDK_EVENT_FILTER_REMOVED) != 0)
1106 {
1107 tmp_list = tmp_list->next;
1108 continue;
1109 }
1110
1111 filter->ref_count++;
1112 result = filter->function (msg, event, filter->data);
1113
1114 /* get the next node after running the function since the
1115 function may add or remove a next node */
1116 node = tmp_list;
1117 tmp_list = tmp_list->next;
1118
1119 filter->ref_count--;
1120 if (filter->ref_count == 0)
1121 {
1122 *filters = g_list_remove_link (*filters, node);
1123 g_list_free_1 (node);
1124 g_free (filter);
1125 }
1126
1127 if (result != GDK_FILTER_CONTINUE)
1128 break;
1129 }
1130
1131 if (result == GDK_FILTER_CONTINUE || result == GDK_FILTER_REMOVE)
1132 {
1133 _gdk_event_queue_remove_link (display, node);
1134 g_list_free_1 (node);
1135 gdk_event_free (event);
1136 }
1137 else /* GDK_FILTER_TRANSLATE */
1138 {
1139 ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
1140 fixup_event (event);
1141 GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
1142 }
1143
1144 return result;
1145 }
1146
1147 /*
1148 * On Windows, transient windows will not have their own taskbar entries.
1149 * Because of this, we must hide and restore groups of transients in both
1150 * directions. That is, all transient children must be hidden or restored
1151 * with this window, but if this window’s transient owner also has a
1152 * transient owner then this window’s transient owner must be hidden/restored
1153 * with this one. And etc, up the chain until we hit an ancestor that has no
1154 * transient owner.
1155 *
1156 * It would be a good idea if applications don’t chain transient windows
1157 * together. There’s a limit to how much evil GTK can try to shield you
1158 * from.
1159 */
1160 static void
1161 show_window_recurse (GdkWindow *window, gboolean hide_window)
1162 {
1163 GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1164 GSList *children = impl->transient_children;
1165 GdkWindow *child = NULL;
1166
1167 if (!impl->changing_state)
1168 {
1169 impl->changing_state = TRUE;
1170
1171 if (children != NULL)
1172 {
1173 while (children != NULL)
1174 {
1175 child = children->data;
1176 show_window_recurse (child, hide_window);
1177
1178 children = children->next;
1179 }
1180 }
1181
1182 if (GDK_WINDOW_IS_MAPPED (window))
1183 {
1184 if (!hide_window)
1185 {
1186 if (gdk_window_get_state (window) & GDK_WINDOW_STATE_ICONIFIED)
1187 {
1188 if (gdk_window_get_state (window) & GDK_WINDOW_STATE_MAXIMIZED)
1189 {
1190 GtkShowWindow (window, SW_SHOWMAXIMIZED);
1191 }
1192 else
1193 {
1194 GtkShowWindow (window, SW_RESTORE);
1195 }
1196 }
1197 }
1198 else
1199 {
1200 GtkShowWindow (window, SW_MINIMIZE);
1201 }
1202 }
1203
1204 impl->changing_state = FALSE;
1205 }
1206 }
1207
1208 static void
1209 do_show_window (GdkWindow *window, gboolean hide_window)
1210 {
1211 GdkWindow *tmp_window = NULL;
1212 GdkWindowImplWin32 *tmp_impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1213
1214 if (!tmp_impl->changing_state)
1215 {
1216 /* Find the top-level window in our transient chain. */
1217 while (tmp_impl->transient_owner != NULL)
1218 {
1219 tmp_window = tmp_impl->transient_owner;
1220 tmp_impl = GDK_WINDOW_IMPL_WIN32 (tmp_window->impl);
1221 }
1222
1223 /* If we couldn't find one, use the window provided. */
1224 if (tmp_window == NULL)
1225 {
1226 tmp_window = window;
1227 }
1228
1229 /* Recursively show/hide every window in the chain. */
1230 if (tmp_window != window)
1231 {
1232 show_window_recurse (tmp_window, hide_window);
1233 }
1234 }
1235 }
1236
1237 static void
1238 send_crossing_event (GdkDisplay *display,
1239 GdkDevice *source_device,
1240 GdkWindow *window,
1241 GdkEventType type,
1242 GdkCrossingMode mode,
1243 GdkNotifyType notify_type,
1244 GdkWindow *subwindow,
1245 POINT *screen_pt,
1246 GdkModifierType mask,
1247 guint32 time_)
1248 {
1249 GdkEvent *event;
1250 GdkDeviceGrabInfo *grab;
1251 GdkDeviceManagerWin32 *device_manager;
1252 POINT pt;
1253 GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1254
1255 device_manager = GDK_DEVICE_MANAGER_WIN32 (gdk_display_get_device_manager (display));
1256
1257 grab = _gdk_display_has_device_grab (display, device_manager->core_pointer, 0);
1258
1259 if (grab != NULL &&
1260 !grab->owner_events &&
1261 mode != GDK_CROSSING_UNGRAB)
1262 {
1263 /* !owner_event => only report events wrt grab window, ignore rest */
1264 if ((GdkWindow *)window != grab->native_window)
1265 return;
1266 }
1267
1268 pt = *screen_pt;
1269 ScreenToClient (GDK_WINDOW_HWND (window), &pt);
1270
1271 event = gdk_event_new (type);
1272 event->crossing.window = window;
1273 event->crossing.subwindow = subwindow;
1274 event->crossing.time = time_;
1275 event->crossing.x = pt.x / impl->window_scale;
1276 event->crossing.y = pt.y / impl->window_scale;
1277 event->crossing.x_root = (screen_pt->x + _gdk_offset_x) / impl->window_scale;
1278 event->crossing.y_root = (screen_pt->y + _gdk_offset_y) / impl->window_scale;
1279 event->crossing.mode = mode;
1280 event->crossing.detail = notify_type;
1281 event->crossing.mode = mode;
1282 event->crossing.detail = notify_type;
1283 event->crossing.focus = FALSE;
1284 event->crossing.state = mask;
1285 gdk_event_set_device (event, device_manager->core_pointer);
1286 gdk_event_set_source_device (event, source_device);
1287 gdk_event_set_seat (event, gdk_device_get_seat (device_manager->core_pointer));
1288
1289 _gdk_device_virtual_set_active (device_manager->core_pointer, source_device);
1290
1291 _gdk_win32_append_event (event);
1292 }
1293
1294 static GdkWindow *
1295 get_native_parent (GdkWindow *window)
1296 {
1297 if (window->parent != NULL)
1298 return window->parent->impl_window;
1299 return NULL;
1300 }
1301
1302 static GdkWindow *
1303 find_common_ancestor (GdkWindow *win1,
1304 GdkWindow *win2)
1305 {
1306 GdkWindow *tmp;
1307 GList *path1 = NULL, *path2 = NULL;
1308 GList *list1, *list2;
1309
1310 tmp = win1;
1311 while (tmp != NULL && tmp->window_type != GDK_WINDOW_ROOT)
1312 {
1313 path1 = g_list_prepend (path1, tmp);
1314 tmp = get_native_parent (tmp);
1315 }
1316
1317 tmp = win2;
1318 while (tmp != NULL && tmp->window_type != GDK_WINDOW_ROOT)
1319 {
1320 path2 = g_list_prepend (path2, tmp);
1321 tmp = get_native_parent (tmp);
1322 }
1323
1324 list1 = path1;
1325 list2 = path2;
1326 tmp = NULL;
1327 while (list1 && list2 && (list1->data == list2->data))
1328 {
1329 tmp = (GdkWindow *)list1->data;
1330 list1 = list1->next;
1331 list2 = list2->next;
1332 }
1333 g_list_free (path1);
1334 g_list_free (path2);
1335
1336 return tmp;
1337 }
1338
1339 void
1340 synthesize_crossing_events (GdkDisplay *display,
1341 GdkDevice *source_device,
1342 GdkWindow *src,
1343 GdkWindow *dest,
1344 GdkCrossingMode mode,
1345 POINT *screen_pt,
1346 GdkModifierType mask,
1347 guint32 time_,
1348 gboolean non_linear)
1349 {
1350 GdkWindow *c;
1351 GdkWindow *win, *last, *next;
1352 GList *path, *list;
1353 GdkWindow *a;
1354 GdkWindow *b;
1355 GdkNotifyType notify_type;
1356
1357 a = src;
1358 b = dest;
1359 if (a == b)
1360 return; /* No crossings generated between src and dest */
1361
1362 c = find_common_ancestor (a, b);
1363
1364 non_linear |= (c != a) && (c != b);
1365
1366 if (a) /* There might not be a source (i.e. if no previous pointer_in_window) */
1367 {
1368 /* Traverse up from a to (excluding) c sending leave events */
1369 if (non_linear)
1370 notify_type = GDK_NOTIFY_NONLINEAR;
1371 else if (c == a)
1372 notify_type = GDK_NOTIFY_INFERIOR;
1373 else
1374 notify_type = GDK_NOTIFY_ANCESTOR;
1375 send_crossing_event (display,
1376 source_device,
1377 a, GDK_LEAVE_NOTIFY,
1378 mode,
1379 notify_type,
1380 NULL,
1381 screen_pt,
1382 mask, time_);
1383
1384 if (c != a)
1385 {
1386 if (non_linear)
1387 notify_type = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1388 else
1389 notify_type = GDK_NOTIFY_VIRTUAL;
1390
1391 last = a;
1392 win = get_native_parent (a);
1393 while (win != c && win->window_type != GDK_WINDOW_ROOT)
1394 {
1395 send_crossing_event (display,
1396 source_device,
1397 win, GDK_LEAVE_NOTIFY,
1398 mode,
1399 notify_type,
1400 (GdkWindow *)last,
1401 screen_pt,
1402 mask, time_);
1403
1404 last = win;
1405 win = get_native_parent (win);
1406 }
1407 }
1408 }
1409
1410 if (b) /* Might not be a dest, e.g. if we're moving out of the window */
1411 {
1412 /* Traverse down from c to b */
1413 if (c != b)
1414 {
1415 path = NULL;
1416 win = get_native_parent (b);
1417 while (win != c && win->window_type != GDK_WINDOW_ROOT)
1418 {
1419 path = g_list_prepend (path, win);
1420 win = get_native_parent (win);
1421 }
1422
1423 if (non_linear)
1424 notify_type = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1425 else
1426 notify_type = GDK_NOTIFY_VIRTUAL;
1427
1428 list = path;
1429 while (list)
1430 {
1431 win = (GdkWindow *)list->data;
1432 list = list->next;
1433 if (list)
1434 next = (GdkWindow *)list->data;
1435 else
1436 next = b;
1437
1438 send_crossing_event (display,
1439 source_device,
1440 win, GDK_ENTER_NOTIFY,
1441 mode,
1442 notify_type,
1443 next,
1444 screen_pt,
1445 mask, time_);
1446 }
1447 g_list_free (path);
1448 }
1449
1450
1451 if (non_linear)
1452 notify_type = GDK_NOTIFY_NONLINEAR;
1453 else if (c == a)
1454 notify_type = GDK_NOTIFY_ANCESTOR;
1455 else
1456 notify_type = GDK_NOTIFY_INFERIOR;
1457
1458 send_crossing_event (display,
1459 source_device,
1460 b, GDK_ENTER_NOTIFY,
1461 mode,
1462 notify_type,
1463 NULL,
1464 screen_pt,
1465 mask, time_);
1466 }
1467 }
1468
1469 static void
1470 make_crossing_event (GdkDisplay *display,
1471 GdkDevice *device,
1472 GdkWindow *window,
1473 POINT *screen_pt,
1474 guint32 time_)
1475 {
1476 GDK_NOTE (EVENTS, g_print (" mouse_window %p -> %p",
1477 mouse_window ? GDK_WINDOW_HWND (mouse_window) : NULL,
1478 window ? GDK_WINDOW_HWND (window) : NULL));
1479 synthesize_crossing_events (display,
1480 device,
1481 mouse_window, window,
1482 GDK_CROSSING_NORMAL,
1483 screen_pt,
1484 0, /* TODO: Set right mask */
1485 time_,
1486 FALSE);
1487 g_set_object (&mouse_window, window);
1488 }
1489
1490 /* The check_extended flag controls whether to check if the windows want
1491 * events from extended input devices and if the message should be skipped
1492 * because an extended input device is active
1493 */
1494 static gboolean
1495 propagate (GdkWindow **window,
1496 MSG *msg,
1497 GdkWindow *grab_window,
1498 gboolean grab_owner_events,
1499 gint grab_mask,
1500 gboolean (*doesnt_want_it) (gint mask,
1501 MSG *msg))
1502 {
1503 if (grab_window != NULL && !grab_owner_events)
1504 {
1505 /* Event source is grabbed with owner_events FALSE */
1506
1507 if ((*doesnt_want_it) (grab_mask, msg))
1508 {
1509 GDK_NOTE (EVENTS, g_print (" (grabber doesn't want it)"));
1510 return FALSE;
1511 }
1512 else
1513 {
1514 GDK_NOTE (EVENTS, g_print (" (to grabber)"));
1515 g_set_object (window, grab_window);
1516 return TRUE;
1517 }
1518 }
1519
1520 /* If we come here, we know that if grab_window != NULL then
1521 * grab_owner_events is TRUE
1522 */
1523 while (TRUE)
1524 {
1525 if ((*doesnt_want_it) ((*window)->event_mask, msg))
1526 {
1527 /* Owner doesn't want it, propagate to parent. */
1528 GdkWindow *parent = gdk_window_get_parent (*window);
1529 if (parent == gdk_get_default_root_window () || parent == NULL)
1530 {
1531 /* No parent; check if grabbed */
1532 if (grab_window != NULL)
1533 {
1534 /* Event source is grabbed with owner_events TRUE */
1535
1536 if ((*doesnt_want_it) (grab_mask, msg))
1537 {
1538 /* Grabber doesn't want it either */
1539 GDK_NOTE (EVENTS, g_print (" (grabber doesn't want it)"));
1540 return FALSE;
1541 }
1542 else
1543 {
1544 /* Grabbed! */
1545 GDK_NOTE (EVENTS, g_print (" (to grabber)"));
1546 g_set_object (window, grab_window);
1547 return TRUE;
1548 }
1549 }
1550 else
1551 {
1552 GDK_NOTE (EVENTS, g_print (" (undelivered)"));
1553 return FALSE;
1554 }
1555 }
1556 else
1557 {
1558 g_set_object (window, parent);
1559 /* The only branch where we actually continue the loop */
1560 }
1561 }
1562 else
1563 return TRUE;
1564 }
1565 }
1566
1567 static gboolean
1568 doesnt_want_key (gint mask,
1569 MSG *msg)
1570 {
1571 return (((msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP) &&
1572 !(mask & GDK_KEY_RELEASE_MASK)) ||
1573 ((msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN) &&
1574 !(mask & GDK_KEY_PRESS_MASK)));
1575 }
1576
1577 static gboolean
1578 doesnt_want_char (gint mask,
1579 MSG *msg)
1580 {
1581 return !(mask & (GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK));
1582 }
1583
1584 /* Acquires actual client area size of the underlying native window.
1585 * Rectangle is in GDK screen coordinates (_gdk_offset_* is added).
1586 * Returns FALSE if configure events should be inhibited,
1587 * TRUE otherwise.
1588 */
1589 gboolean
1590 _gdk_win32_get_window_rect (GdkWindow *window,
1591 RECT *rect)
1592 {
1593 GdkWindowImplWin32 *window_impl;
1594 RECT client_rect;
1595 POINT point;
1596 HWND hwnd;
1597
1598 window_impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1599
1600 hwnd = GDK_WINDOW_HWND (window);
1601
1602 GetClientRect (hwnd, &client_rect);
1603 point.x = client_rect.left; /* always 0 */
1604 point.y = client_rect.top;
1605
1606 /* top level windows need screen coords */
1607 if (gdk_window_get_parent (window) == gdk_get_default_root_window ())
1608 {
1609 ClientToScreen (hwnd, &point);
1610 point.x += _gdk_offset_x;
1611 point.y += _gdk_offset_y;
1612 }
1613
1614 rect->left = point.x;
1615 rect->top = point.y;
1616 rect->right = point.x + client_rect.right - client_rect.left;
1617 rect->bottom = point.y + client_rect.bottom - client_rect.top;
1618
1619 return !window_impl->inhibit_configure;
1620 }
1621
1622 void
1623 _gdk_win32_do_emit_configure_event (GdkWindow *window,
1624 RECT rect)
1625 {
1626 GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1627
1628 impl->unscaled_width = rect.right - rect.left;
1629 impl->unscaled_height = rect.bottom - rect.top;
1630 window->width = (impl->unscaled_width + impl->window_scale - 1) / impl->window_scale;
1631 window->height = (impl->unscaled_height + impl->window_scale - 1) / impl->window_scale;
1632 window->x = rect.left / impl->window_scale;
1633 window->y = rect.top / impl->window_scale;
1634
1635 _gdk_window_update_size (window);
1636
1637 if (window->event_mask & GDK_STRUCTURE_MASK)
1638 {
1639 GdkEvent *event = gdk_event_new (GDK_CONFIGURE);
1640
1641 event->configure.window = window;
1642
1643 event->configure.width = window->width;
1644 event->configure.height = window->height;
1645
1646 event->configure.x = window->x;
1647 event->configure.y = window->y;
1648
1649 _gdk_win32_append_event (event);
1650 }
1651 }
1652
1653 void
1654 _gdk_win32_emit_configure_event (GdkWindow *window)
1655 {
1656 RECT rect;
1657
1658 if (!_gdk_win32_get_window_rect (window, &rect))
1659 return;
1660
1661 _gdk_win32_do_emit_configure_event (window, rect);
1662 }
1663
1664 cairo_region_t *
1665 _gdk_win32_hrgn_to_region (HRGN hrgn,
1666 guint scale)
1667 {
1668 RGNDATA *rgndata;
1669 RECT *rects;
1670 cairo_region_t *result;
1671 gint nbytes;
1672 guint i;
1673
1674 if ((nbytes = GetRegionData (hrgn, 0, NULL)) == 0)
1675 {
1676 WIN32_GDI_FAILED ("GetRegionData");
1677 return NULL;
1678 }
1679
1680 rgndata = (RGNDATA *) g_malloc (nbytes);
1681
1682 if (GetRegionData (hrgn, nbytes, rgndata) == 0)
1683 {
1684 WIN32_GDI_FAILED ("GetRegionData");
1685 g_free (rgndata);
1686 return NULL;
1687 }
1688
1689 result = cairo_region_create ();
1690 rects = (RECT *) rgndata->Buffer;
1691 for (i = 0; i < rgndata->rdh.nCount; i++)
1692 {
1693 GdkRectangle r;
1694
1695 r.x = rects[i].left;
1696 r.y = rects[i].top;
1697 r.width = (rects[i].right - r.x) / scale;
1698 r.height = (rects[i].bottom - r.y) / scale;
1699
1700 cairo_region_union_rectangle (result, &r);
1701 }
1702
1703 g_free (rgndata);
1704
1705 return result;
1706 }
1707
1708 static void
1709 adjust_drag (LONG *drag,
1710 LONG curr,
1711 gint inc)
1712 {
1713 if (*drag > curr)
1714 *drag = curr + ((*drag + inc/2 - curr) / inc) * inc;
1715 else
1716 *drag = curr - ((curr - *drag + inc/2) / inc) * inc;
1717 }
1718
1719 static void
1720 handle_wm_paint (MSG *msg,
1721 GdkWindow *window)
1722 {
1723 HRGN hrgn = CreateRectRgn (0, 0, 0, 0);
1724 HDC hdc;
1725 PAINTSTRUCT paintstruct;
1726 cairo_region_t *update_region;
1727 GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1728
1729 if (GetUpdateRgn (msg->hwnd, hrgn, FALSE) == ERROR)
1730 {
1731 WIN32_GDI_FAILED ("GetUpdateRgn");
1732 DeleteObject (hrgn);
1733 return;
1734 }
1735
1736 hdc = BeginPaint (msg->hwnd, &paintstruct);
1737
1738 GDK_NOTE (EVENTS, g_print (" %s %s dc %p",
1739 _gdk_win32_rect_to_string (&paintstruct.rcPaint),
1740 (paintstruct.fErase ? "erase" : ""),
1741 hdc));
1742
1743 EndPaint (msg->hwnd, &paintstruct);
1744
1745 if ((paintstruct.rcPaint.right == paintstruct.rcPaint.left) ||
1746 (paintstruct.rcPaint.bottom == paintstruct.rcPaint.top))
1747 {
1748 GDK_NOTE (EVENTS, g_print (" (empty paintstruct, ignored)"));
1749 DeleteObject (hrgn);
1750 return;
1751 }
1752
1753 update_region = _gdk_win32_hrgn_to_region (hrgn, impl->window_scale);
1754 if (!cairo_region_is_empty (update_region))
1755 _gdk_window_invalidate_for_expose (window, update_region);
1756 cairo_region_destroy (update_region);
1757
1758 DeleteObject (hrgn);
1759 }
1760
1761 static VOID CALLBACK
1762 modal_timer_proc (HWND hwnd,
1763 UINT msg,
1764 UINT_PTR id,
1765 DWORD time)
1766 {
1767 int arbitrary_limit = 10;
1768
1769 while (_modal_operation_in_progress != GDK_WIN32_MODAL_OP_NONE &&
1770 g_main_context_pending (NULL) &&
1771 arbitrary_limit--)
1772 g_main_context_iteration (NULL, FALSE);
1773 }
1774
1775 void
1776 _gdk_win32_begin_modal_call (GdkWin32ModalOpKind kind)
1777 {
1778 GdkWin32ModalOpKind was = _modal_operation_in_progress;
1779 g_assert (!(_modal_operation_in_progress & kind));
1780
1781 _modal_operation_in_progress |= kind;
1782
1783 if (was == GDK_WIN32_MODAL_OP_NONE)
1784 {
1785 modal_timer = SetTimer (NULL, 0, 10, modal_timer_proc);
1786
1787 if (modal_timer == 0)
1788 WIN32_API_FAILED ("SetTimer");
1789 }
1790 }
1791
1792 void
1793 _gdk_win32_end_modal_call (GdkWin32ModalOpKind kind)
1794 {
1795 g_assert (_modal_operation_in_progress & kind);
1796
1797 _modal_operation_in_progress &= ~kind;
1798
1799 if (_modal_operation_in_progress == GDK_WIN32_MODAL_OP_NONE &&
1800 modal_timer != 0)
1801 {
1802 API_CALL (KillTimer, (NULL, modal_timer));
1803 modal_timer = 0;
1804 }
1805 }
1806
1807 static VOID CALLBACK
1808 sync_timer_proc (HWND hwnd,
1809 UINT msg,
1810 UINT_PTR id,
1811 DWORD time)
1812 {
1813 MSG message;
1814 if (PeekMessageW (&message, hwnd, WM_PAINT, WM_PAINT, PM_REMOVE))
1815 {
1816 return;
1817 }
1818
1819 RedrawWindow (hwnd, NULL, NULL, RDW_INVALIDATE|RDW_UPDATENOW|RDW_ALLCHILDREN);
1820
1821 KillTimer (hwnd, sync_timer);
1822 }
1823
1824 static gboolean
1825 handle_nchittest (HWND hwnd,
1826 GdkWindow *window,
1827 gint16 screen_x,
1828 gint16 screen_y,
1829 gint *ret_valp)
1830 {
1831 RECT rect;
1832 GdkWindowImplWin32 *impl;
1833
1834 if (window == NULL || window->input_shape == NULL)
1835 return FALSE;
1836
1837 /* If the window has decorations, DefWindowProc() will take
1838 * care of NCHITTEST.
1839 */
1840 if (!_gdk_win32_window_lacks_wm_decorations (window))
1841 return FALSE;
1842
1843 if (!GetWindowRect (hwnd, &rect))
1844 return FALSE;
1845
1846 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1847 rect.left = screen_x - rect.left;
1848 rect.top = screen_y - rect.top;
1849
1850 /* If it's inside the rect, return FALSE and let DefWindowProc() handle it */
1851 if (cairo_region_contains_point (window->input_shape,
1852 rect.left / impl->window_scale,
1853 rect.top / impl->window_scale))
1854 return FALSE;
1855
1856 /* Otherwise override DefWindowProc() and tell WM that the point is not
1857 * within the window
1858 */
1859 *ret_valp = HTNOWHERE;
1860 return TRUE;
1861 }
1862
1863 static void
1864 generate_button_event (GdkEventType type,
1865 gint button,
1866 GdkWindow *window,
1867 MSG *msg)
1868 {
1869 GdkEvent *event;
1870 GdkDeviceManagerWin32 *device_manager;
1871 GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1872
1873 if (_gdk_input_ignore_core > 0)
1874 return;
1875
1876 device_manager = GDK_DEVICE_MANAGER_WIN32 (gdk_display_get_device_manager (gdk_display_get_default ()));
1877
1878 event = gdk_event_new (type);
1879 event->button.window = window;
1880 event->button.time = _gdk_win32_get_next_tick (msg->time);
1881 event->button.x = current_x = (gint16) GET_X_LPARAM (msg->lParam) / impl->window_scale;
1882 event->button.y = current_y = (gint16) GET_Y_LPARAM (msg->lParam) / impl->window_scale;
1883 event->button.x_root = (msg->pt.x + _gdk_offset_x) / impl->window_scale;
1884 event->button.y_root = (msg->pt.y + _gdk_offset_y) / impl->window_scale;
1885 event->button.axes = NULL;
1886 event->button.state = build_pointer_event_state (msg);
1887 event->button.button = button;
1888 gdk_event_set_device (event, device_manager->core_pointer);
1889 gdk_event_set_source_device (event, device_manager->system_pointer);
1890 gdk_event_set_seat (event, gdk_device_get_seat (device_manager->core_pointer));
1891
1892 _gdk_device_virtual_set_active (device_manager->core_pointer, device_manager->system_pointer);
1893
1894 _gdk_win32_append_event (event);
1895 }
1896
1897 static gboolean
1898 handle_wm_sysmenu (GdkWindow *window, MSG *msg, gint *ret_valp)
1899 {
1900 GdkWindowImplWin32 *impl;
1901 LONG_PTR style, tmp_style;
1902 gboolean maximized, minimized;
1903 LONG_PTR additional_styles;
1904
1905 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1906
1907 style = GetWindowLongPtr (msg->hwnd, GWL_STYLE);
1908
1909 maximized = IsZoomed (msg->hwnd) || (style & WS_MAXIMIZE);
1910 minimized = IsIconic (msg->hwnd) || (style & WS_MINIMIZE);
1911 additional_styles = 0;
1912
1913 if (!(style & WS_SYSMENU))
1914 additional_styles |= WS_SYSMENU;
1915
1916 if (!(style & WS_MAXIMIZEBOX))
1917 additional_styles |= WS_MAXIMIZEBOX;
1918
1919 if (!(style & WS_MINIMIZEBOX))
1920 additional_styles |= WS_MINIMIZEBOX;
1921
1922 if (!(style & WS_SIZEBOX))
1923 additional_styles |= WS_SIZEBOX;
1924
1925 if (!(style & WS_DLGFRAME))
1926 additional_styles |= WS_DLGFRAME;
1927
1928 if (!(style & WS_BORDER))
1929 additional_styles |= WS_BORDER;
1930
1931 if (additional_styles == 0)
1932 /* The caller will eventually pass this to DefWindowProc (),
1933 * only without the style dance, which isn't needed, as it turns out.
1934 */
1935 return FALSE;
1936
1937 /* Note: This code will enable resizing, maximizing and minimizing windows
1938 * via window menu even if these are non-CSD windows that were explicitly
1939 * forbidden from doing this by removing the appropriate styles,
1940 * or if these are CSD windows that were explicitly forbidden from doing
1941 * this by removing appropriate decorations from the headerbar and/or
1942 * changing hints or properties.
1943 *
1944 * If doing this for non-CSD windows is not desired,
1945 * do a _gdk_win32_window_lacks_wm_decorations() check and return FALSE
1946 * if it doesn't pass.
1947 *
1948 * If doing this for CSD windows with disabled decorations is not desired,
1949 * tough luck - GDK can't know which CSD decorations are enabled, and which
1950 * are not.
1951 *
1952 * If doing this for CSD windows with particular hints is not desired,
1953 * check window hints here and return FALSE (DefWindowProc() will return
1954 * FALSE later) or set *ret_valp to 0 and return TRUE.
1955 */
1956 tmp_style = style | additional_styles;
1957 GDK_NOTE (EVENTS, g_print (" Handling WM_SYSMENU: style 0x%lx -> 0x%lx\n", style, tmp_style));
1958 impl->have_temp_styles = TRUE;
1959 impl->temp_styles = additional_styles;
1960 SetWindowLongPtr (msg->hwnd, GWL_STYLE, tmp_style);
1961
1962 *ret_valp = DefWindowProc (msg->hwnd, msg->message, msg->wParam, msg->lParam);
1963
1964 tmp_style = GetWindowLongPtr (msg->hwnd, GWL_STYLE);
1965 style = tmp_style & ~additional_styles;
1966
1967 GDK_NOTE (EVENTS, g_print (" Handling WM_SYSMENU: style 0x%lx <- 0x%lx\n", style, tmp_style));
1968 SetWindowLongPtr (msg->hwnd, GWL_STYLE, style);
1969 impl->have_temp_styles = FALSE;
1970
1971 return TRUE;
1972 }
1973
1974 gboolean
1975 _gdk_win32_window_fill_min_max_info (GdkWindow *window,
1976 MINMAXINFO *mmi)
1977 {
1978 GdkWindowImplWin32 *impl;
1979 RECT rect;
1980
1981 if (GDK_WINDOW_DESTROYED (window))
1982 return FALSE;
1983
1984 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1985
1986 if (impl->hint_flags & GDK_HINT_MIN_SIZE)
1987 {
1988 rect.left = rect.top = 0;
1989 rect.right = impl->hints.min_width * impl->window_scale;
1990 rect.bottom = impl->hints.min_height * impl->window_scale;
1991
1992 _gdk_win32_adjust_client_rect (window, &rect);
1993
1994 mmi->ptMinTrackSize.x = rect.right - rect.left;
1995 mmi->ptMinTrackSize.y = rect.bottom - rect.top;
1996 }
1997
1998 if (impl->hint_flags & GDK_HINT_MAX_SIZE)
1999 {
2000 int maxw, maxh;
2001
2002 rect.left = rect.top = 0;
2003 rect.right = impl->hints.max_width * impl->window_scale;
2004 rect.bottom = impl->hints.max_height * impl->window_scale;
2005
2006 _gdk_win32_adjust_client_rect (window, &rect);
2007
2008 /* at least on win9x we have the 16 bit trouble */
2009 maxw = rect.right - rect.left;
2010 maxh = rect.bottom - rect.top;
2011 mmi->ptMaxTrackSize.x = maxw > 0 && maxw < G_MAXSHORT ? maxw : G_MAXSHORT;
2012 mmi->ptMaxTrackSize.y = maxh > 0 && maxh < G_MAXSHORT ? maxh : G_MAXSHORT;
2013 }
2014 else
2015 {
2016 /* According to "How does the window manager adjust ptMaxSize and
2017 * ptMaxPosition for multiple monitors?" article
2018 * https://blogs.msdn.microsoft.com/oldnewthing/20150501-00/?p=44964
2019 * if ptMaxSize >= primary_monitor_size, then it will be adjusted by
2020 * WM to account for the monitor size differences if the window gets
2021 * maximized on a non-primary monitor, by simply adding the size
2022 * difference (i.e. if non-primary monitor is larger by 100px, then
2023 * window will be made larger exactly by 100px).
2024 * If ptMaxSize < primary_monitor_size at least in one direction,
2025 * nothing is adjusted.
2026 * Therefore, if primary monitor is smaller than the actual monitor,
2027 * then it is not possible to give window a size that is larger than
2028 * the primary monitor and smaller than the non-primary monitor,
2029 * because WM will always enlarge the window.
2030 * Therefore, it is impossible to account for taskbar size.
2031 * So we don't try at all. Instead we just remember that we're trying
2032 * to maximize the window, catch WM_WINDOWPOSCHANGING and
2033 * adjust the size then.
2034 */
2035 HMONITOR nearest_monitor;
2036 MONITORINFO nearest_info;
2037
2038 nearest_monitor = MonitorFromWindow (GDK_WINDOW_HWND (window), MONITOR_DEFAULTTONEAREST);
2039 nearest_info.cbSize = sizeof (nearest_info);
2040
2041 if (GetMonitorInfoA (nearest_monitor, &nearest_info))
2042 {
2043 /* MSDN says that we must specify maximized window
2044 * size as if it was located on the primary monitor.
2045 * However, we still need to account for a taskbar
2046 * that might or might not be on the nearest monitor where
2047 * window will actually end up.
2048 * "0" here is the top-left corner of the primary monitor.
2049 */
2050 /* An investigation into bug 765161 turned up a weird Windows WM behaviour
2051 * where it would interpret "0:0" as "top-left of the workea" (accounting for a taskbar
2052 * possibly being along the left/top edge of the screen) when window has styles
2053 * (i.e. not CSD), and interpret the same "0:0" as "top-left of the screen" (not
2054 * accounting for a taskbar) when window has no styles (i.e. a CSD window).
2055 * This doesn't seem to be documented anywhere.
2056 * The following code uses a simple CSD/non-CSD test, but it could be that
2057 * this behaviour hinges on just one particular window style.
2058 * Finding exactly which style that could be is not very useful for GTK, however.
2059 */
2060 mmi->ptMaxPosition.x = 0;
2061 mmi->ptMaxPosition.y = 0;
2062
2063 if (_gdk_win32_window_lacks_wm_decorations (window))
2064 {
2065 mmi->ptMaxPosition.x += (nearest_info.rcWork.left - nearest_info.rcMonitor.left);
2066 mmi->ptMaxPosition.y += (nearest_info.rcWork.top - nearest_info.rcMonitor.top);
2067 }
2068
2069 mmi->ptMaxSize.x = nearest_info.rcWork.right - nearest_info.rcWork.left;
2070 mmi->ptMaxSize.y = nearest_info.rcWork.bottom - nearest_info.rcWork.top;
2071 }
2072
2073 mmi->ptMaxTrackSize.x = GetSystemMetrics (SM_CXVIRTUALSCREEN) + impl->margins_x * impl->window_scale;
2074 mmi->ptMaxTrackSize.y = GetSystemMetrics (SM_CYVIRTUALSCREEN) + impl->margins_y * impl->window_scale;
2075 }
2076
2077 return TRUE;
2078 }
2079
2080 static void
2081 gdk_settings_notify (GdkWindow *window,
2082 const char *name,
2083 GdkSettingAction action)
2084 {
2085 GdkEvent *new_event;
2086
2087 if (!g_str_has_prefix (name, "gtk-"))
2088 return;
2089
2090 new_event = gdk_event_new (GDK_SETTING);
2091 new_event->setting.window = window;
2092 new_event->setting.send_event = FALSE;
2093 new_event->setting.action = action;
2094 new_event->setting.name = g_strdup (name);
2095
2096 _gdk_win32_append_event (new_event);
2097 }
2098
2099 #define GDK_ANY_BUTTON_MASK (GDK_BUTTON1_MASK | \
2100 GDK_BUTTON2_MASK | \
2101 GDK_BUTTON3_MASK | \
2102 GDK_BUTTON4_MASK | \
2103 GDK_BUTTON5_MASK)
2104
2105 static gboolean
2106 gdk_event_translate (MSG *msg,
2107 gint *ret_valp)
2108 {
2109 RECT rect, *drag, orig_drag;
2110 POINT point;
2111 MINMAXINFO *mmi;
2112 HWND hwnd;
2113 GdkCursor *cursor;
2114 BYTE key_state[256];
2115 HIMC himc;
2116 WINDOWPOS *windowpos;
2117 gboolean ignore_leave;
2118
2119 GdkEvent *event;
2120
2121 wchar_t wbuf[100];
2122 gint ccount;
2123
2124 GdkDisplay *display;
2125 GdkWindow *window = NULL;
2126 GdkWindowImplWin32 *impl;
2127
2128 GdkWindow *new_window = NULL;
2129
2130 GdkDeviceManager *device_manager = NULL;
2131 GdkDeviceManagerWin32 *device_manager_win32 = NULL;
2132
2133 GdkDeviceGrabInfo *keyboard_grab = NULL;
2134 GdkDeviceGrabInfo *pointer_grab = NULL;
2135 GdkWindow *grab_window = NULL;
2136
2137 crossing_cb_t crossing_cb = NULL;
2138
2139 gint button;
2140 GdkAtom target;
2141
2142 gchar buf[256];
2143 gboolean return_val = FALSE;
2144
2145 int i;
2146
2147 GdkWin32Selection *win32_sel = NULL;
2148
2149 STGMEDIUM *property_change_data;
2150
2151 display = gdk_display_get_default ();
2152 window = gdk_win32_handle_table_lookup (msg->hwnd);
2153
2154 if (_gdk_default_filters)
2155 {
2156 /* Apply global filters */
2157
2158 GdkFilterReturn result = apply_event_filters (window ? window : gdk_screen_get_root_window (gdk_display_get_default_screen (display)),
2159 msg,
2160 &_gdk_default_filters);
2161
2162 /* If result is GDK_FILTER_CONTINUE, we continue as if nothing
2163 * happened. If it is GDK_FILTER_REMOVE or GDK_FILTER_TRANSLATE,
2164 * we return TRUE, and DefWindowProcW() will not be called.
2165 */
2166 if (result == GDK_FILTER_REMOVE || result == GDK_FILTER_TRANSLATE)
2167 return TRUE;
2168 }
2169
2170 if (window == NULL)
2171 {
2172 /* XXX Handle WM_QUIT here ? */
2173 if (msg->message == WM_QUIT)
2174 {
2175 GDK_NOTE (EVENTS, g_print (" %d", (int) msg->wParam));
2176 exit (msg->wParam);
2177 }
2178 else if (msg->message == WM_CREATE)
2179 {
2180 window = (UNALIGNED GdkWindow*) (((LPCREATESTRUCTW) msg->lParam)->lpCreateParams);
2181 GDK_WINDOW_HWND (window) = msg->hwnd;
2182 }
2183 else
2184 {
2185 GDK_NOTE (EVENTS, g_print (" (no GdkWindow)"));
2186 }
2187 return FALSE;
2188 }
2189
2190 /* gdk_event_translate() can be called during initialization, if something
2191 * sends MSGs. In this case, the default display or its device manager will
2192 * be NULL, so avoid trying to read the active grabs.
2193 * https://bugzilla.gnome.org/show_bug.cgi?id=774379
2194 */
2195 if (display != NULL)
2196 {
2197 device_manager = gdk_display_get_device_manager (display);
2198 }
2199 else
2200 {
2201 GDK_NOTE (EVENTS, g_print (" (no GdkDisplay)"));
2202 }
2203
2204 if (device_manager != NULL)
2205 {
2206 device_manager_win32 = GDK_DEVICE_MANAGER_WIN32 (device_manager);
2207 }
2208 else
2209 {
2210 GDK_NOTE (EVENTS, g_print (" (no GdkDeviceManager)"));
2211 }
2212
2213 if (device_manager_win32 != NULL)
2214 {
2215 keyboard_grab = _gdk_display_get_last_device_grab (display, device_manager_win32->core_keyboard);
2216 pointer_grab = _gdk_display_get_last_device_grab (display, device_manager_win32->core_pointer);
2217 }
2218
2219 g_object_ref (window);
2220
2221 /* window's refcount has now been increased, so code below should
2222 * not just return from this function, but instead goto done (or
2223 * break out of the big switch). To protect against forgetting this,
2224 * #define return to a syntax error...
2225 */
2226 #define return GOTO_DONE_INSTEAD
2227
2228 if (!GDK_WINDOW_DESTROYED (window) && window->filters)
2229 {
2230 /* Apply per-window filters */
2231
2232 GdkFilterReturn result = apply_event_filters (window, msg, &window->filters);
2233
2234 if (result == GDK_FILTER_REMOVE || result == GDK_FILTER_TRANSLATE)
2235 {
2236 return_val = TRUE;
2237 goto done;
2238 }
2239 }
2240
2241 if (msg->message == client_message)
2242 {
2243 GList *tmp_list;
2244 GdkFilterReturn result = GDK_FILTER_CONTINUE;
2245 GList *node;
2246
2247 GDK_NOTE (EVENTS, g_print (" client_message"));
2248
2249 event = gdk_event_new (GDK_NOTHING);
2250 ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
2251
2252 node = _gdk_event_queue_append (display, event);
2253
2254 tmp_list = client_filters;
2255 while (tmp_list)
2256 {
2257 GdkClientFilter *filter = tmp_list->data;
2258
2259 tmp_list = tmp_list->next;
2260
2261 if (filter->type == GDK_POINTER_TO_ATOM (msg->wParam))
2262 {
2263 GDK_NOTE (EVENTS, g_print (" (match)"));
2264
2265 result = (*filter->function) (msg, event, filter->data);
2266
2267 if (result != GDK_FILTER_CONTINUE)
2268 break;
2269 }
2270 }
2271
2272 switch (result)
2273 {
2274 case GDK_FILTER_REMOVE:
2275 _gdk_event_queue_remove_link (display, node);
2276 g_list_free_1 (node);
2277 gdk_event_free (event);
2278 return_val = TRUE;
2279 goto done;
2280
2281 case GDK_FILTER_TRANSLATE:
2282 ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
2283 GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
2284 return_val = TRUE;
2285 goto done;
2286
2287 case GDK_FILTER_CONTINUE:
2288 /* No more: Send unknown client messages on to Gtk for it to use */
2289 GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
2290 return_val = TRUE;
2291 goto done;
2292 }
2293 }
2294
2295 if (msg->message == aerosnap_message)
2296 _gdk_win32_window_handle_aerosnap (gdk_window_get_toplevel (window),
2297 (GdkWin32AeroSnapCombo) msg->wParam);
2298
2299 switch (msg->message)
2300 {
2301 case WM_INPUTLANGCHANGE:
2302 _gdk_input_locale = (HKL) msg->lParam;
2303 _gdk_win32_keymap_set_active_layout (GDK_WIN32_KEYMAP (_gdk_win32_display_get_keymap (_gdk_display)), _gdk_input_locale);
2304 GetLocaleInfo (MAKELCID (LOWORD (_gdk_input_locale), SORT_DEFAULT),
2305 LOCALE_IDEFAULTANSICODEPAGE,
2306 buf, sizeof (buf));
2307 _gdk_input_codepage = atoi (buf);
2308 _gdk_keymap_serial++;
2309 GDK_NOTE (EVENTS,
2310 g_print (" cs:%lu hkl:%p%s cp:%d",
2311 (gulong) msg->wParam,
2312 (gpointer) msg->lParam, _gdk_input_locale_is_ime ? " (IME)" : "",
2313 _gdk_input_codepage));
2314 gdk_settings_notify (window, "gtk-im-module", GDK_SETTING_ACTION_CHANGED);
2315
2316 /* Generate a dummy key event to "nudge" IMContext */
2317 event = gdk_event_new (GDK_KEY_PRESS);
2318 event->key.window = window;
2319 event->key.time = _gdk_win32_get_next_tick (msg->time);
2320 event->key.keyval = GDK_KEY_VoidSymbol;
2321 event->key.string = NULL;
2322 event->key.length = 0;
2323 event->key.hardware_keycode = 0;
2324 gdk_event_set_scancode (event, 0);
2325 gdk_event_set_device (event, device_manager_win32->core_keyboard);
2326 gdk_event_set_source_device (event, device_manager_win32->system_keyboard);
2327 gdk_event_set_seat (event, gdk_device_get_seat (device_manager_win32->core_keyboard));
2328 event->key.is_modifier = FALSE;
2329 event->key.state = 0;
2330 _gdk_win32_append_event (event);
2331 break;
2332
2333 case WM_SYSKEYUP:
2334 case WM_SYSKEYDOWN:
2335 GDK_NOTE (EVENTS,
2336 g_print (" %s ch:%.02x %s",
2337 _gdk_win32_key_to_string (msg->lParam),
2338 (int) msg->wParam,
2339 decode_key_lparam (msg->lParam)));
2340
2341 /* If posted without us having keyboard focus, ignore */
2342 if ((msg->wParam != VK_F10 && msg->wParam != VK_MENU) &&
2343 !(HIWORD (msg->lParam) & KF_ALTDOWN))
2344 break;
2345
2346 /* Let the system handle Alt-Tab, Alt-Space and Alt-F4 unless
2347 * the keyboard is grabbed.
2348 */
2349 if (!keyboard_grab &&
2350 (msg->wParam == VK_TAB ||
2351 msg->wParam == VK_SPACE ||
2352 msg->wParam == VK_F4))
2353 break;
2354
2355 /* Jump to code in common with WM_KEYUP and WM_KEYDOWN */
2356 goto keyup_or_down;
2357
2358 case WM_KEYUP:
2359 case WM_KEYDOWN:
2360 GDK_NOTE (EVENTS,
2361 g_print (" %s ch:%.02x %s",
2362 _gdk_win32_key_to_string (msg->lParam),
2363 (int) msg->wParam,
2364 decode_key_lparam (msg->lParam)));
2365
2366 keyup_or_down:
2367
2368 /* Ignore key messages intended for the IME */
2369 if (msg->wParam == VK_PROCESSKEY ||
2370 in_ime_composition)
2371 break;
2372
2373 /* Ignore autorepeats on modifiers */
2374 if (msg->message == WM_KEYDOWN &&
2375 (msg->wParam == VK_MENU ||
2376 msg->wParam == VK_CONTROL ||
2377 msg->wParam == VK_SHIFT) &&
2378 ((HIWORD(msg->lParam) & KF_REPEAT) >= 1))
2379 break;
2380
2381 if (keyboard_grab &&
2382 !propagate (&window, msg,
2383 keyboard_grab->window,
2384 keyboard_grab->owner_events,
2385 GDK_ALL_EVENTS_MASK,
2386 doesnt_want_key))
2387 break;
2388
2389 if (GDK_WINDOW_DESTROYED (window))
2390 break;
2391
2392 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
2393
2394 API_CALL (GetKeyboardState, (key_state));
2395
2396 event = gdk_event_new ((msg->message == WM_KEYDOWN ||
2397 msg->message == WM_SYSKEYDOWN) ?
2398 GDK_KEY_PRESS : GDK_KEY_RELEASE);
2399 event->key.window = window;
2400 event->key.time = _gdk_win32_get_next_tick (msg->time);
2401 event->key.keyval = GDK_KEY_VoidSymbol;
2402 event->key.string = NULL;
2403 event->key.length = 0;
2404 event->key.hardware_keycode = msg->wParam;
2405 /* save original scancode */
2406 gdk_event_set_scancode (event, msg->lParam >> 16);
2407 gdk_event_set_device (event, device_manager_win32->core_keyboard);
2408 gdk_event_set_source_device (event, device_manager_win32->system_keyboard);
2409 gdk_event_set_seat (event, gdk_device_get_seat (device_manager_win32->core_keyboard));
2410
2411 /* Get the WinAPI translation of the WM_KEY messages to characters.
2412
2413 The WM_CHAR messages are generated by a previous call to TranslateMessage() and always
2414 follow directly after the corresponding WM_KEY* messages.
2415 There could be 0 or more WM_CHAR messages following (for example dead keys don't generate
2416 WM_CHAR messages - they generate WM_DEAD_CHAR instead, but we are not interested in those
2417 messages). */
2418
2419 if (gdk_event_is_allocated (event)) /* Should always be true */
2420 {
2421 GdkEventPrivate *event_priv = (GdkEventPrivate*) event;
2422
2423 MSG msg2;
2424 while (PeekMessageW (&msg2, msg->hwnd, 0, 0, 0) && (msg2.message == WM_CHAR || msg2.message == WM_SYSCHAR))
2425 {
2426 /* The character is encoded in WPARAM as UTF-16. */
2427 gunichar2 c = msg2.wParam;
2428
2429 /* Ignore control sequences like Backspace */
2430 if (!g_unichar_iscntrl(c))
2431 {
2432 /* Append character to translation string. */
2433 event_priv->translation_len ++;
2434 event_priv->translation = g_realloc (event_priv->translation, event_priv->translation_len * sizeof (event_priv->translation[0]));
2435 event_priv->translation[event_priv->translation_len - 1] = c;
2436 }
2437
2438 /* Remove message from queue */
2439 GetMessageW (&msg2, msg->hwnd, 0, 0);
2440 }
2441 }
2442
2443 if (HIWORD (msg->lParam) & KF_EXTENDED)
2444 {
2445 switch (msg->wParam)
2446 {
2447 case VK_CONTROL:
2448 event->key.hardware_keycode = VK_RCONTROL;
2449 break;
2450 case VK_SHIFT: /* Actually, KF_EXTENDED is not set
2451 * for the right shift key.
2452 */
2453 event->key.hardware_keycode = VK_RSHIFT;
2454 break;
2455 case VK_MENU:
2456 event->key.hardware_keycode = VK_RMENU;
2457 break;
2458 }
2459 }
2460 else if (msg->wParam == VK_SHIFT &&
2461 LOBYTE (HIWORD (msg->lParam)) == _gdk_win32_keymap_get_rshift_scancode (GDK_WIN32_KEYMAP (_gdk_win32_display_get_keymap (_gdk_display))))
2462 event->key.hardware_keycode = VK_RSHIFT;
2463
2464 event->key.is_modifier = (msg->wParam == VK_CONTROL ||
2465 msg->wParam == VK_SHIFT ||
2466 msg->wParam == VK_MENU);
2467 /* g_print ("ctrl:%02x lctrl:%02x rctrl:%02x alt:%02x lalt:%02x ralt:%02x\n", key_state[VK_CONTROL], key_state[VK_LCONTROL], key_state[VK_RCONTROL], key_state[VK_MENU], key_state[VK_LMENU], key_state[VK_RMENU]); */
2468
2469 build_key_event_state (event, key_state);
2470
2471 gdk_keymap_translate_keyboard_state (_gdk_win32_display_get_keymap (display),
2472 event->key.hardware_keycode,
2473 event->key.state,
2474 event->key.group,
2475 &event->key.keyval,
2476 NULL, NULL, NULL);
2477
2478 fill_key_event_string (event);
2479
2480 /* Only one release key event is fired when both shift keys are pressed together
2481 and then released. In order to send the missing event, press events for shift
2482 keys are recorded and sent together when the release event occurs.
2483 Other modifiers (e.g. ctrl, alt) don't have this problem. */
2484 if (msg->message == WM_KEYDOWN && msg->wParam == VK_SHIFT)
2485 {
2486 int pressed_shift = msg->lParam & 0xffffff; /* mask shift modifier */
2487 if (both_shift_pressed[0] == 0)
2488 both_shift_pressed[0] = pressed_shift;
2489 else if (both_shift_pressed[0] != pressed_shift)
2490 both_shift_pressed[1] = pressed_shift;
2491 }
2492
2493 if (msg->message == WM_KEYUP && msg->wParam == VK_SHIFT)
2494 {
2495 if (both_shift_pressed[0] != 0 && both_shift_pressed[1] != 0)
2496 {
2497 gint tmp_retval;
2498 MSG fake_release = *msg;
2499 int pressed_shift = msg->lParam & 0xffffff;
2500
2501 if (both_shift_pressed[0] == pressed_shift)
2502 fake_release.lParam = both_shift_pressed[1];
2503 else
2504 fake_release.lParam = both_shift_pressed[0];
2505
2506 both_shift_pressed[0] = both_shift_pressed[1] = 0;
2507 gdk_event_translate (&fake_release, &tmp_retval);
2508 }
2509 both_shift_pressed[0] = both_shift_pressed[1] = 0;
2510 }
2511
2512 /* Reset MOD1_MASK if it is the Alt key itself */
2513 if (msg->wParam == VK_MENU)
2514 event->key.state &= ~GDK_MOD1_MASK;
2515
2516 _gdk_win32_append_event (event);
2517
2518 return_val = TRUE;
2519 break;
2520
2521 case WM_SYSCHAR:
2522 if (msg->wParam != VK_SPACE)
2523 {
2524 /* To prevent beeps, don't let DefWindowProcW() be called */
2525 return_val = TRUE;
2526 goto done;
2527 }
2528 break;
2529
2530 case WM_IME_STARTCOMPOSITION:
2531 in_ime_composition = TRUE;
2532 break;
2533
2534 case WM_IME_ENDCOMPOSITION:
2535 in_ime_composition = FALSE;
2536 break;
2537
2538 case WM_IME_COMPOSITION:
2539 /* On Win2k WM_IME_CHAR doesn't work correctly for non-Unicode
2540 * applications. Thus, handle WM_IME_COMPOSITION with
2541 * GCS_RESULTSTR instead, fetch the Unicode chars from the IME
2542 * with ImmGetCompositionStringW().
2543 *
2544 * See for instance
2545 * http://groups.google.com/groups?selm=natX5.57%24g77.19788%40nntp2.onemain.com
2546 * and
2547 * http://groups.google.com/groups?selm=u2XfrXw5BHA.1628%40tkmsftngp02
2548 * for comments by other people that seems to have the same
2549 * experience. WM_IME_CHAR just gives question marks, apparently
2550 * because of going through some conversion to the current code
2551 * page.
2552 *
2553 * WM_IME_CHAR might work on NT4 or Win9x with ActiveIMM, but
2554 * use WM_IME_COMPOSITION there, too, to simplify the code.
2555 */
2556 GDK_NOTE (EVENTS, g_print (" %#lx", (long) msg->lParam));
2557
2558 if (!(msg->lParam & GCS_RESULTSTR))
2559 break;
2560
2561 if (keyboard_grab &&
2562 !propagate (&window, msg,
2563 keyboard_grab->window,
2564 keyboard_grab->owner_events,
2565 GDK_ALL_EVENTS_MASK,
2566 doesnt_want_char))
2567 break;
2568
2569 if (GDK_WINDOW_DESTROYED (window))
2570 break;
2571
2572 himc = ImmGetContext (msg->hwnd);
2573 ccount = ImmGetCompositionStringW (himc, GCS_RESULTSTR,
2574 wbuf, sizeof (wbuf));
2575 ImmReleaseContext (msg->hwnd, himc);
2576
2577 ccount /= 2;
2578
2579 API_CALL (GetKeyboardState, (key_state));
2580
2581 for (i = 0; i < ccount; i++)
2582 {
2583 if (window->event_mask & GDK_KEY_PRESS_MASK)
2584 {
2585 /* Build a key press event */
2586 event = gdk_event_new (GDK_KEY_PRESS);
2587 event->key.window = window;
2588 gdk_event_set_device (event, device_manager_win32->core_keyboard);
2589 gdk_event_set_source_device (event, device_manager_win32->system_keyboard);
2590 gdk_event_set_seat (event, gdk_device_get_seat (device_manager_win32->core_keyboard));
2591 build_wm_ime_composition_event (event, msg, wbuf[i], key_state);
2592
2593 _gdk_win32_append_event (event);
2594 }
2595
2596 if (window->event_mask & GDK_KEY_RELEASE_MASK)
2597 {
2598 /* Build a key release event. */
2599 event = gdk_event_new (GDK_KEY_RELEASE);
2600 event->key.window = window;
2601 gdk_event_set_device (event, device_manager_win32->core_keyboard);
2602 gdk_event_set_source_device (event, device_manager_win32->system_keyboard);
2603 gdk_event_set_seat (event, gdk_device_get_seat (device_manager_win32->core_keyboard));
2604 build_wm_ime_composition_event (event, msg, wbuf[i], key_state);
2605
2606 _gdk_win32_append_event (event);
2607 }
2608 }
2609 return_val = TRUE;
2610 break;
2611
2612 case WM_LBUTTONDOWN:
2613 button = 1;
2614 goto buttondown0;
2615
2616 case WM_MBUTTONDOWN:
2617 button = 2;
2618 goto buttondown0;
2619
2620 case WM_RBUTTONDOWN:
2621 button = 3;
2622 goto buttondown0;
2623
2624 case WM_XBUTTONDOWN:
2625 if (HIWORD (msg->wParam) == XBUTTON1)
2626 button = 4;
2627 else
2628 button = 5;
2629
2630 buttondown0:
2631 GDK_NOTE (EVENTS,
2632 g_print (" (%d,%d)",
2633 GET_X_LPARAM (msg->lParam), GET_Y_LPARAM (msg->lParam)));
2634
2635 pen_touch_input = FALSE;
2636
2637 g_set_object (&window, find_window_for_mouse_event (window, msg));
2638 /* TODO_CSW?: there used to some synthesize and propagate */
2639 if (GDK_WINDOW_DESTROYED (window))
2640 break;
2641
2642 if (pointer_grab == NULL)
2643 {
2644 SetCapture (GDK_WINDOW_HWND (window));
2645 }
2646
2647 generate_button_event (GDK_BUTTON_PRESS, button,
2648 window, msg);
2649
2650 return_val = TRUE;
2651 break;
2652
2653 case WM_LBUTTONUP:
2654 button = 1;
2655 goto buttonup0;
2656
2657 case WM_MBUTTONUP:
2658 button = 2;
2659 goto buttonup0;
2660
2661 case WM_RBUTTONUP:
2662 button = 3;
2663 goto buttonup0;
2664
2665 case WM_XBUTTONUP:
2666 if (HIWORD (msg->wParam) == XBUTTON1)
2667 button = 4;
2668 else
2669 button = 5;
2670
2671 buttonup0:
2672 {
2673 gboolean release_implicit_grab = FALSE;
2674 GdkWindow *prev_window = NULL;
2675
2676 GDK_NOTE (EVENTS,
2677 g_print (" (%d,%d)",
2678 GET_X_LPARAM (msg->lParam), GET_Y_LPARAM (msg->lParam)));
2679
2680 pen_touch_input = FALSE;
2681
2682 g_set_object (&window, find_window_for_mouse_event (window, msg));
2683
2684 if (pointer_grab != NULL && pointer_grab->implicit)
2685 {
2686 gint state = build_pointer_event_state (msg);
2687
2688 /* We keep the implicit grab until no buttons at all are held down */
2689 if ((state & GDK_ANY_BUTTON_MASK & ~(GDK_BUTTON1_MASK << (button - 1))) == 0)
2690 {
2691 release_implicit_grab = TRUE;
2692 prev_window = pointer_grab->native_window;
2693 }
2694 }
2695
2696 generate_button_event (GDK_BUTTON_RELEASE, button, window, msg);
2697
2698 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
2699
2700 /* End a drag op when the same button that started it is released */
2701 if (impl->drag_move_resize_context.op != GDK_WIN32_DRAGOP_NONE &&
2702 impl->drag_move_resize_context.button == button)
2703 gdk_win32_window_end_move_resize_drag (window);
2704
2705 if (release_implicit_grab)
2706 {
2707 ReleaseCapture ();
2708
2709 new_window = NULL;
2710 hwnd = WindowFromPoint (msg->pt);
2711 if (hwnd != NULL)
2712 {
2713 POINT client_pt = msg->pt;
2714
2715 ScreenToClient (hwnd, &client_pt);
2716 GetClientRect (hwnd, &rect);
2717 if (PtInRect (&rect, client_pt))
2718 new_window = gdk_win32_handle_table_lookup (hwnd);
2719 }
2720
2721 synthesize_crossing_events (display,
2722 device_manager_win32->system_pointer,
2723 prev_window, new_window,
2724 GDK_CROSSING_UNGRAB,
2725 &msg->pt,
2726 0, /* TODO: Set right mask */
2727 _gdk_win32_get_next_tick (msg->time),
2728 FALSE);
2729 g_set_object (&mouse_window, new_window);
2730 mouse_window_ignored_leave = NULL;
2731 }
2732
2733 return_val = TRUE;
2734 break;
2735 }
2736 case WM_MOUSEMOVE:
2737 GDK_NOTE (EVENTS,
2738 g_print (" %p (%d,%d)",
2739 (gpointer) msg->wParam,
2740 GET_X_LPARAM (msg->lParam), GET_Y_LPARAM (msg->lParam)));
2741
2742 if (_gdk_win32_tablet_input_api == GDK_WIN32_TABLET_INPUT_API_WINPOINTER &&
2743 ( (msg->time - last_digitizer_time) < 200 ||
2744 -(msg->time - last_digitizer_time) < 200 ))
2745 break;
2746
2747 pen_touch_input = FALSE;
2748
2749 g_set_object (&window, find_window_for_mouse_event (window, msg));
2750
2751 if (mouse_window != window)
2752 {
2753 GDK_NOTE (EVENTS, g_print (" mouse_window %p -> %p",
2754 mouse_window ? GDK_WINDOW_HWND (mouse_window) : NULL,
2755 window ? GDK_WINDOW_HWND (window) : NULL));
2756 synthesize_crossing_events (display,
2757 device_manager_win32->system_pointer,
2758 mouse_window, window,
2759 GDK_CROSSING_NORMAL,
2760 &msg->pt,
2761 0, /* TODO: Set right mask */
2762 _gdk_win32_get_next_tick (msg->time),
2763 FALSE);
2764 g_set_object (&mouse_window, window);
2765 mouse_window_ignored_leave = NULL;
2766 if (window != NULL)
2767 track_mouse_event (TME_LEAVE, GDK_WINDOW_HWND (window));
2768 }
2769 else if (window != NULL && window == mouse_window_ignored_leave)
2770 {
2771 /* If we ignored a leave event for this window and we're now getting
2772 input again we need to re-arm the mouse tracking, as that was
2773 cancelled by the mouseleave. */
2774 mouse_window_ignored_leave = NULL;
2775 track_mouse_event (TME_LEAVE, GDK_WINDOW_HWND (window));
2776 }
2777
2778 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
2779
2780 /* If we haven't moved, don't create any GDK event. Windows
2781 * sends WM_MOUSEMOVE messages after a new window is shows under
2782 * the mouse, even if the mouse hasn't moved. This disturbs gtk.
2783 */
2784 if ((msg->pt.x + _gdk_offset_x) / impl->window_scale == current_root_x &&
2785 (msg->pt.y + _gdk_offset_y) / impl->window_scale == current_root_y)
2786 break;
2787
2788 current_root_x = (msg->pt.x + _gdk_offset_x) / impl->window_scale;
2789 current_root_y = (msg->pt.y + _gdk_offset_y) / impl->window_scale;
2790
2791
2792 if (impl->drag_move_resize_context.op != GDK_WIN32_DRAGOP_NONE)
2793 {
2794 gdk_win32_window_do_move_resize_drag (window, current_root_x, current_root_y);
2795 }
2796 else if (_gdk_input_ignore_core == 0)
2797 {
2798 event = gdk_event_new (GDK_MOTION_NOTIFY);
2799 event->motion.window = window;
2800 event->motion.time = _gdk_win32_get_next_tick (msg->time);
2801 event->motion.x = current_x = (gint16) GET_X_LPARAM (msg->lParam) / impl->window_scale;
2802 event->motion.y = current_y = (gint16) GET_Y_LPARAM (msg->lParam) / impl->window_scale;
2803 event->motion.x_root = current_root_x;
2804 event->motion.y_root = current_root_y;
2805 event->motion.axes = NULL;
2806 event->motion.state = build_pointer_event_state (msg);
2807 event->motion.is_hint = FALSE;
2808 gdk_event_set_device (event, device_manager_win32->core_pointer);
2809 gdk_event_set_source_device (event, device_manager_win32->system_pointer);
2810 gdk_event_set_seat (event, gdk_device_get_seat (device_manager_win32->core_pointer));
2811
2812 _gdk_device_virtual_set_active (device_manager_win32->core_pointer, device_manager_win32->system_pointer);
2813
2814 _gdk_win32_append_event (event);
2815 }
2816
2817 return_val = TRUE;
2818 break;
2819
2820 case WM_NCMOUSEMOVE:
2821 GDK_NOTE (EVENTS,
2822 g_print (" (%d,%d)",
2823 GET_X_LPARAM (msg->lParam), GET_Y_LPARAM (msg->lParam)));
2824
2825 pen_touch_input = FALSE;
2826
2827 break;
2828
2829 case WM_MOUSELEAVE:
2830 GDK_NOTE (EVENTS, g_print (" %d (%ld,%ld)",
2831 HIWORD (msg->wParam), msg->pt.x, msg->pt.y));
2832
2833 pen_touch_input = FALSE;
2834
2835 new_window = NULL;
2836 hwnd = WindowFromPoint (msg->pt);
2837 ignore_leave = FALSE;
2838 if (hwnd != NULL)
2839 {
2840 char classname[64];
2841
2842 POINT client_pt = msg->pt;
2843
2844 /* The synapitics trackpad drivers have this irritating
2845 feature where it pops up a window right under the pointer
2846 when you scroll. We ignore the leave and enter events for
2847 this window */
2848 if (GetClassNameA (hwnd, classname, sizeof(classname)) &&
2849 strcmp (classname, SYNAPSIS_ICON_WINDOW_CLASS) == 0)
2850 ignore_leave = TRUE;
2851
2852 ScreenToClient (hwnd, &client_pt);
2853 GetClientRect (hwnd, &rect);
2854 if (PtInRect (&rect, client_pt))
2855 new_window = gdk_win32_handle_table_lookup (hwnd);
2856 }
2857
2858 if (!ignore_leave)
2859 synthesize_crossing_events (display,
2860 device_manager_win32->system_pointer,
2861 mouse_window, new_window,
2862 GDK_CROSSING_NORMAL,
2863 &msg->pt,
2864 0, /* TODO: Set right mask */
2865 _gdk_win32_get_next_tick (msg->time),
2866 FALSE);
2867 g_set_object (&mouse_window, new_window);
2868 mouse_window_ignored_leave = ignore_leave ? new_window : NULL;
2869
2870
2871 return_val = TRUE;
2872 break;
2873
2874 case WM_POINTERDOWN:
2875 if (_gdk_win32_tablet_input_api != GDK_WIN32_TABLET_INPUT_API_WINPOINTER ||
2876 gdk_winpointer_should_forward_message (msg))
2877 {
2878 return_val = FALSE;
2879 break;
2880 }
2881
2882 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
2883
2884 if (IS_POINTER_PRIMARY_WPARAM (msg->wParam))
2885 {
2886 pen_touch_cursor_position.x = GET_X_LPARAM (msg->lParam);
2887 pen_touch_cursor_position.y = GET_Y_LPARAM (msg->lParam);
2888 current_root_x = pen_touch_cursor_position.x / impl->window_scale;
2889 current_root_y = pen_touch_cursor_position.y / impl->window_scale;
2890 pen_touch_input = TRUE;
2891 last_digitizer_time = msg->time;
2892 }
2893
2894 if (pointer_grab != NULL &&
2895 !pointer_grab->implicit &&
2896 !pointer_grab->owner_events)
2897 g_set_object (&window, pointer_grab->native_window);
2898
2899 if (IS_POINTER_PRIMARY_WPARAM (msg->wParam) && mouse_window != window)
2900 crossing_cb = make_crossing_event;
2901
2902 gdk_winpointer_input_events (display, window, crossing_cb, msg);
2903
2904 *ret_valp = 0;
2905 return_val = TRUE;
2906 break;
2907
2908 case WM_POINTERUP:
2909 if (_gdk_win32_tablet_input_api != GDK_WIN32_TABLET_INPUT_API_WINPOINTER ||
2910 gdk_winpointer_should_forward_message (msg))
2911 {
2912 return_val = FALSE;
2913 break;
2914 }
2915
2916 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
2917
2918 if (IS_POINTER_PRIMARY_WPARAM (msg->wParam))
2919 {
2920 pen_touch_cursor_position.x = GET_X_LPARAM (msg->lParam);
2921 pen_touch_cursor_position.y = GET_Y_LPARAM (msg->lParam);
2922 current_root_x = pen_touch_cursor_position.x / impl->window_scale;
2923 current_root_y = pen_touch_cursor_position.y / impl->window_scale;
2924 pen_touch_input = TRUE;
2925 last_digitizer_time = msg->time;
2926 }
2927
2928 if (pointer_grab != NULL &&
2929 !pointer_grab->implicit &&
2930 !pointer_grab->owner_events)
2931 g_set_object (&window, pointer_grab->native_window);
2932
2933 gdk_winpointer_input_events (display, window, NULL, msg);
2934
2935 if (impl->drag_move_resize_context.op != GDK_WIN32_DRAGOP_NONE)
2936 {
2937 gdk_win32_window_end_move_resize_drag (window);
2938 }
2939
2940 *ret_valp = 0;
2941 return_val = TRUE;
2942 break;
2943
2944 case WM_POINTERUPDATE:
2945 if (_gdk_win32_tablet_input_api != GDK_WIN32_TABLET_INPUT_API_WINPOINTER ||
2946 gdk_winpointer_should_forward_message (msg))
2947 {
2948 return_val = FALSE;
2949 break;
2950 }
2951
2952 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
2953
2954 if (IS_POINTER_PRIMARY_WPARAM (msg->wParam))
2955 {
2956 pen_touch_cursor_position.x = GET_X_LPARAM (msg->lParam);
2957 pen_touch_cursor_position.y = GET_Y_LPARAM (msg->lParam);
2958 current_root_x = pen_touch_cursor_position.x / impl->window_scale;
2959 current_root_y = pen_touch_cursor_position.y / impl->window_scale;
2960 pen_touch_input = TRUE;
2961 last_digitizer_time = msg->time;
2962 }
2963
2964 if (pointer_grab != NULL &&
2965 !pointer_grab->implicit &&
2966 !pointer_grab->owner_events)
2967 g_set_object (&window, pointer_grab->native_window);
2968
2969 if (IS_POINTER_PRIMARY_WPARAM (msg->wParam) && mouse_window != window)
2970 crossing_cb = make_crossing_event;
2971
2972 if (impl->drag_move_resize_context.op != GDK_WIN32_DRAGOP_NONE)
2973 {
2974 gdk_win32_window_do_move_resize_drag (window, current_root_x, current_root_y);
2975 }
2976 else
2977 {
2978 gdk_winpointer_input_events (display, window, crossing_cb, msg);
2979 }
2980
2981 *ret_valp = 0;
2982 return_val = TRUE;
2983 break;
2984
2985 case WM_NCPOINTERUPDATE:
2986 if (_gdk_win32_tablet_input_api != GDK_WIN32_TABLET_INPUT_API_WINPOINTER ||
2987 gdk_winpointer_should_forward_message (msg))
2988 {
2989 return_val = FALSE;
2990 break;
2991 }
2992
2993 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
2994
2995 if (IS_POINTER_PRIMARY_WPARAM (msg->wParam))
2996 {
2997 pen_touch_cursor_position.x = GET_X_LPARAM (msg->lParam);
2998 pen_touch_cursor_position.y = GET_Y_LPARAM (msg->lParam);
2999 current_root_x = pen_touch_cursor_position.x / impl->window_scale;
3000 current_root_y = pen_touch_cursor_position.y / impl->window_scale;
3001 pen_touch_input = TRUE;
3002 last_digitizer_time = msg->time;
3003 }
3004
3005 if (IS_POINTER_PRIMARY_WPARAM (msg->wParam) &&
3006 !IS_POINTER_INCONTACT_WPARAM (msg->wParam) &&
3007 mouse_window != NULL)
3008 {
3009 GdkDevice *event_device = NULL;
3010 guint32 event_time = 0;
3011
3012 if (gdk_winpointer_get_message_info (display, msg, &event_device, &event_time))
3013 {
3014 make_crossing_event(display,
3015 event_device,
3016 NULL,
3017 &pen_touch_cursor_position,
3018 event_time);
3019 }
3020 }
3021
3022 return_val = FALSE; /* forward to DefWindowProc */
3023 break;
3024
3025 case WM_POINTERENTER:
3026 if (_gdk_win32_tablet_input_api != GDK_WIN32_TABLET_INPUT_API_WINPOINTER ||
3027 gdk_winpointer_should_forward_message (msg))
3028 {
3029 return_val = FALSE;
3030 break;
3031 }
3032
3033 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
3034
3035 if (IS_POINTER_PRIMARY_WPARAM (msg->wParam))
3036 {
3037 pen_touch_cursor_position.x = GET_X_LPARAM (msg->lParam);
3038 pen_touch_cursor_position.y = GET_Y_LPARAM (msg->lParam);
3039 current_root_x = pen_touch_cursor_position.x / impl->window_scale;
3040 current_root_y = pen_touch_cursor_position.y / impl->window_scale;
3041 pen_touch_input = TRUE;
3042 last_digitizer_time = msg->time;
3043 }
3044
3045 if (pointer_grab != NULL &&
3046 !pointer_grab->implicit &&
3047 !pointer_grab->owner_events)
3048 g_set_object (&window, pointer_grab->native_window);
3049
3050 if (IS_POINTER_NEW_WPARAM (msg->wParam))
3051 {
3052 gdk_winpointer_input_events (display, window, NULL, msg);
3053 }
3054
3055 *ret_valp = 0;
3056 return_val = TRUE;
3057 break;
3058
3059 case WM_POINTERLEAVE:
3060 if (_gdk_win32_tablet_input_api != GDK_WIN32_TABLET_INPUT_API_WINPOINTER ||
3061 gdk_winpointer_should_forward_message (msg))
3062 {
3063 return_val = FALSE;
3064 break;
3065 }
3066
3067 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
3068
3069 if (IS_POINTER_PRIMARY_WPARAM (msg->wParam))
3070 {
3071 pen_touch_cursor_position.x = GET_X_LPARAM (msg->lParam);
3072 pen_touch_cursor_position.y = GET_Y_LPARAM (msg->lParam);
3073 current_root_x = pen_touch_cursor_position.x / impl->window_scale;
3074 current_root_y = pen_touch_cursor_position.y / impl->window_scale;
3075 pen_touch_input = TRUE;
3076 last_digitizer_time = msg->time;
3077 }
3078
3079 if (!IS_POINTER_INRANGE_WPARAM (msg->wParam))
3080 {
3081 gdk_winpointer_input_events (display, window, NULL, msg);
3082 }
3083 else if (IS_POINTER_PRIMARY_WPARAM (msg->wParam) && mouse_window != NULL)
3084 {
3085 GdkDevice *event_device = NULL;
3086 guint32 event_time = 0;
3087
3088 if (gdk_winpointer_get_message_info (display, msg, &event_device, &event_time))
3089 {
3090 make_crossing_event(display,
3091 event_device,
3092 NULL,
3093 &pen_touch_cursor_position,
3094 event_time);
3095 }
3096 }
3097
3098 gdk_winpointer_interaction_ended (msg);
3099
3100 *ret_valp = 0;
3101 return_val = TRUE;
3102 break;
3103
3104 case WM_MOUSEWHEEL:
3105 case WM_MOUSEHWHEEL:
3106 GDK_NOTE (EVENTS, g_print (" %d", (short) HIWORD (msg->wParam)));
3107
3108 /* WM_MOUSEWHEEL is delivered to the focus window. Work around
3109 * that. Also, the position is in screen coordinates, not client
3110 * coordinates as with the button messages. I love the
3111 * consistency of Windows.
3112 */
3113 point.x = GET_X_LPARAM (msg->lParam);
3114 point.y = GET_Y_LPARAM (msg->lParam);
3115
3116 if ((hwnd = WindowFromPoint (point)) == NULL)
3117 break;
3118
3119 {
3120 char classname[64];
3121
3122 /* The synapitics trackpad drivers have this irritating
3123 feature where it pops up a window right under the pointer
3124 when you scroll. We backtrack and to the toplevel and
3125 find the innermost child instead. */
3126 if (GetClassNameA (hwnd, classname, sizeof(classname)) &&
3127 strcmp (classname, SYNAPSIS_ICON_WINDOW_CLASS) == 0)
3128 {
3129 HWND hwndc;
3130
3131 /* Find our toplevel window */
3132 hwnd = GetAncestor (msg->hwnd, GA_ROOT);
3133
3134 /* Walk back up to the outermost child at the desired point */
3135 do {
3136 ScreenToClient (hwnd, &point);
3137 hwndc = ChildWindowFromPoint (hwnd, point);
3138 ClientToScreen (hwnd, &point);
3139 } while (hwndc != hwnd && (hwnd = hwndc, 1));
3140 }
3141 }
3142
3143 msg->hwnd = hwnd;
3144 if ((new_window = gdk_win32_handle_table_lookup (msg->hwnd)) == NULL)
3145 break;
3146
3147 if (new_window != window)
3148 {
3149 g_set_object (&window, new_window);
3150 }
3151
3152 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
3153 ScreenToClient (msg->hwnd, &point);
3154
3155 event = gdk_event_new (GDK_SCROLL);
3156 event->scroll.window = window;
3157 event->scroll.direction = GDK_SCROLL_SMOOTH;
3158
3159 if (msg->message == WM_MOUSEWHEEL)
3160 {
3161 event->scroll.delta_y = (gdouble) GET_WHEEL_DELTA_WPARAM (msg->wParam) / (gdouble) WHEEL_DELTA;
3162 }
3163 else if (msg->message == WM_MOUSEHWHEEL)
3164 {
3165 event->scroll.delta_x = (gdouble) GET_WHEEL_DELTA_WPARAM (msg->wParam) / (gdouble) WHEEL_DELTA;
3166 }
3167 /* Positive delta scrolls up, not down,
3168 see API documentation for WM_MOUSEWHEEL message.
3169 */
3170 event->scroll.delta_y *= -1.0;
3171 event->scroll.time = _gdk_win32_get_next_tick (msg->time);
3172 event->scroll.x = (gint16) point.x / impl->window_scale;
3173 event->scroll.y = (gint16) point.y / impl->window_scale;
3174 event->scroll.x_root = ((gint16) GET_X_LPARAM (msg->lParam) + _gdk_offset_x) / impl->window_scale;
3175 event->scroll.y_root = ((gint16) GET_Y_LPARAM (msg->lParam) + _gdk_offset_y) / impl->window_scale;
3176 event->scroll.state = build_pointer_event_state (msg);
3177 gdk_event_set_device (event, device_manager_win32->core_pointer);
3178 gdk_event_set_source_device (event, device_manager_win32->system_pointer);
3179 gdk_event_set_seat (event, gdk_device_get_seat (device_manager_win32->core_pointer));
3180 gdk_event_set_pointer_emulated (event, FALSE);
3181
3182 _gdk_device_virtual_set_active (device_manager_win32->core_pointer, device_manager_win32->system_pointer);
3183
3184 _gdk_win32_append_event (gdk_event_copy (event));
3185
3186 /* Append the discrete version too */
3187 if (msg->message == WM_MOUSEWHEEL)
3188 event->scroll.direction = (((short) HIWORD (msg->wParam)) > 0) ?
3189 GDK_SCROLL_UP : GDK_SCROLL_DOWN;
3190 else if (msg->message == WM_MOUSEHWHEEL)
3191 event->scroll.direction = (((short) HIWORD (msg->wParam)) > 0) ?
3192 GDK_SCROLL_RIGHT : GDK_SCROLL_LEFT;
3193 event->scroll.delta_x = 0;
3194 event->scroll.delta_y = 0;
3195 gdk_event_set_pointer_emulated (event, TRUE);
3196
3197 _gdk_win32_append_event (event);
3198
3199 return_val = TRUE;
3200 break;
3201
3202 case WM_MOUSEACTIVATE:
3203 {
3204 if (gdk_window_get_window_type (window) == GDK_WINDOW_TEMP
3205 || !window->accept_focus)
3206 {
3207 *ret_valp = MA_NOACTIVATE;
3208 return_val = TRUE;
3209 }
3210
3211 if (_gdk_modal_blocked (gdk_window_get_toplevel (window)))
3212 {
3213 *ret_valp = MA_NOACTIVATEANDEAT;
3214 return_val = TRUE;
3215 }
3216 }
3217
3218 break;
3219
3220 case WM_POINTERACTIVATE:
3221 if (gdk_window_get_window_type (window) == GDK_WINDOW_TEMP ||
3222 !window->accept_focus ||
3223 _gdk_modal_blocked (gdk_window_get_toplevel (window)))
3224 {
3225 *ret_valp = PA_NOACTIVATE;
3226 return_val = TRUE;
3227 }
3228
3229 break;
3230
3231 case WM_KILLFOCUS:
3232 if (keyboard_grab != NULL &&
3233 !GDK_WINDOW_DESTROYED (keyboard_grab->window) &&
3234 (_modal_operation_in_progress & GDK_WIN32_MODAL_OP_DND) == 0)
3235 {
3236 generate_grab_broken_event (device_manager, keyboard_grab->window, TRUE, NULL);
3237 }
3238
3239 /* fallthrough */
3240 case WM_SETFOCUS:
3241 if (keyboard_grab != NULL &&
3242 !keyboard_grab->owner_events)
3243 break;
3244
3245 if (!(window->event_mask & GDK_FOCUS_CHANGE_MASK))
3246 break;
3247
3248 if (GDK_WINDOW_DESTROYED (window))
3249 break;
3250
3251 generate_focus_event (device_manager, window, (msg->message == WM_SETFOCUS));
3252 return_val = TRUE;
3253 break;
3254
3255 case WM_ERASEBKGND:
3256 GDK_NOTE (EVENTS, g_print (" %p", (HANDLE) msg->wParam));
3257
3258 if (GDK_WINDOW_DESTROYED (window))
3259 break;
3260
3261 return_val = TRUE;
3262 *ret_valp = 1;
3263 break;
3264
3265 case WM_SYNCPAINT:
3266 sync_timer = SetTimer (GDK_WINDOW_HWND (window),
3267 1,
3268 200, sync_timer_proc);
3269 break;
3270
3271 case WM_PAINT:
3272 handle_wm_paint (msg, window);
3273 break;
3274
3275 case WM_SETCURSOR:
3276 GDK_NOTE (EVENTS, g_print (" %#x %#x",
3277 LOWORD (msg->lParam), HIWORD (msg->lParam)));
3278
3279 if (pointer_grab != NULL)
3280 grab_window = pointer_grab->window;
3281
3282 if (grab_window == NULL && LOWORD (msg->lParam) != HTCLIENT)
3283 break;
3284
3285 if (grab_window != NULL && _gdk_win32_grab_cursor != NULL)
3286 cursor = _gdk_win32_grab_cursor;
3287 else if (!GDK_WINDOW_DESTROYED (window) && GDK_WINDOW_IMPL_WIN32 (window->impl)->cursor != NULL)
3288 cursor = GDK_WINDOW_IMPL_WIN32 (window->impl)->cursor;
3289 else
3290 cursor = NULL;
3291
3292 if (cursor != NULL)
3293 {
3294 GDK_NOTE (EVENTS, g_print (" (SetCursor(%p)", cursor));
3295 SetCursor (GDK_WIN32_CURSOR (cursor)->hcursor);
3296 return_val = TRUE;
3297 *ret_valp = TRUE;
3298 }
3299 break;
3300
3301 case WM_SYSMENU:
3302 return_val = handle_wm_sysmenu (window, msg, ret_valp);
3303 break;
3304
3305 case WM_INITMENU:
3306 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
3307
3308 if (impl->have_temp_styles)
3309 {
3310 LONG_PTR window_style;
3311
3312 window_style = GetWindowLongPtr (GDK_WINDOW_HWND (window),
3313 GWL_STYLE);
3314 /* Handling WM_SYSMENU added extra styles to this window,
3315 * remove them now.
3316 */
3317 window_style &= ~impl->temp_styles;
3318 SetWindowLongPtr (GDK_WINDOW_HWND (window),
3319 GWL_STYLE,
3320 window_style);
3321 }
3322
3323 break;
3324
3325 case WM_SYSCOMMAND:
3326 switch (msg->wParam)
3327 {
3328 case SC_MINIMIZE:
3329 case SC_RESTORE:
3330 do_show_window (window, msg->wParam == SC_MINIMIZE ? TRUE : FALSE);
3331
3332 if (msg->wParam == SC_RESTORE)
3333 gdk_win32_window_invalidate_egl_framebuffer (window);
3334 break;
3335 case SC_MAXIMIZE:
3336 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
3337 impl->maximizing = TRUE;
3338 break;
3339 }
3340
3341 break;
3342
3343 case WM_ENTERSIZEMOVE:
3344 _modal_move_resize_window = msg->hwnd;
3345 _gdk_win32_begin_modal_call (GDK_WIN32_MODAL_OP_SIZEMOVE_MASK);
3346 break;
3347
3348 case WM_EXITSIZEMOVE:
3349 if (_modal_operation_in_progress & GDK_WIN32_MODAL_OP_SIZEMOVE_MASK)
3350 {
3351 _modal_move_resize_window = NULL;
3352 _gdk_win32_end_modal_call (GDK_WIN32_MODAL_OP_SIZEMOVE_MASK);
3353 }
3354 break;
3355
3356 case WM_ENTERMENULOOP:
3357 _gdk_win32_begin_modal_call (GDK_WIN32_MODAL_OP_MENU);
3358 break;
3359
3360 case WM_EXITMENULOOP:
3361 if (_modal_operation_in_progress & GDK_WIN32_MODAL_OP_MENU)
3362 _gdk_win32_end_modal_call (GDK_WIN32_MODAL_OP_MENU);
3363 break;
3364
3365 break;
3366
3367 /*
3368 * Handle WM_CANCELMODE and do nothing in response to it when DnD is
3369 * active. Otherwise pass it to DefWindowProc, which will call ReleaseCapture()
3370 * on our behalf.
3371 * This prevents us from losing mouse capture when alt-tabbing during DnD
3372 * (this includes the feature of Windows Explorer where dragging stuff over
3373 * a window button in the taskbar causes that window to receive focus, i.e.
3374 * keyboardless alt-tabbing).
3375 */
3376 case WM_CANCELMODE:
3377 if (_modal_operation_in_progress & GDK_WIN32_MODAL_OP_DND)
3378 {
3379 return_val = TRUE;
3380 *ret_valp = 0;
3381 }
3382 break;
3383
3384 case WM_CAPTURECHANGED:
3385 /* Sometimes we don't get WM_EXITSIZEMOVE, for instance when you
3386 select move/size in the menu and then click somewhere without
3387 moving/resizing. We work around this using WM_CAPTURECHANGED. */
3388 if (_modal_operation_in_progress & GDK_WIN32_MODAL_OP_SIZEMOVE_MASK)
3389 {
3390 _modal_move_resize_window = NULL;
3391 _gdk_win32_end_modal_call (GDK_WIN32_MODAL_OP_SIZEMOVE_MASK);
3392 }
3393
3394 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
3395
3396 if (impl->drag_move_resize_context.op != GDK_WIN32_DRAGOP_NONE)
3397 gdk_win32_window_end_move_resize_drag (window);
3398 break;
3399
3400 case WM_WINDOWPOSCHANGING:
3401 GDK_NOTE (EVENTS, (windowpos = (WINDOWPOS *) msg->lParam,
3402 g_print (" %s %s %dx%d@%+d%+d now below %p",
3403 _gdk_win32_window_pos_bits_to_string (windowpos->flags),
3404 (windowpos->hwndInsertAfter == HWND_BOTTOM ? "BOTTOM" :
3405 (windowpos->hwndInsertAfter == HWND_NOTOPMOST ? "NOTOPMOST" :
3406 (windowpos->hwndInsertAfter == HWND_TOP ? "TOP" :
3407 (windowpos->hwndInsertAfter == HWND_TOPMOST ? "TOPMOST" :
3408 (sprintf (buf, "%p", windowpos->hwndInsertAfter),
3409 buf))))),
3410 windowpos->cx, windowpos->cy, windowpos->x, windowpos->y,
3411 GetNextWindow (msg->hwnd, GW_HWNDPREV))));
3412
3413 if (GDK_WINDOW_IS_MAPPED (window))
3414 {
3415 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
3416
3417 if (impl->maximizing)
3418 {
3419 MINMAXINFO our_mmi;
3420
3421 gdk_win32_window_invalidate_egl_framebuffer (window);
3422
3423 if (_gdk_win32_window_fill_min_max_info (window, &our_mmi))
3424 {
3425 windowpos = (WINDOWPOS *) msg->lParam;
3426 windowpos->cx = our_mmi.ptMaxSize.x;
3427 windowpos->cy = our_mmi.ptMaxSize.y;
3428 }
3429
3430 impl->maximizing = FALSE;
3431 }
3432 }
3433
3434 break;
3435
3436 case WM_WINDOWPOSCHANGED:
3437 windowpos = (WINDOWPOS *) msg->lParam;
3438 GDK_NOTE (EVENTS, g_print (" %s %s %dx%d@%+d%+d",
3439 _gdk_win32_window_pos_bits_to_string (windowpos->flags),
3440 (windowpos->hwndInsertAfter == HWND_BOTTOM ? "BOTTOM" :
3441 (windowpos->hwndInsertAfter == HWND_NOTOPMOST ? "NOTOPMOST" :
3442 (windowpos->hwndInsertAfter == HWND_TOP ? "TOP" :
3443 (windowpos->hwndInsertAfter == HWND_TOPMOST ? "TOPMOST" :
3444 (sprintf (buf, "%p", windowpos->hwndInsertAfter),
3445 buf))))),
3446 windowpos->cx, windowpos->cy, windowpos->x, windowpos->y));
3447
3448 /* Break grabs on unmap or minimize */
3449 if (windowpos->flags & SWP_HIDEWINDOW ||
3450 ((windowpos->flags & SWP_STATECHANGED) && IsIconic (msg->hwnd)))
3451 {
3452 GdkDevice *device = gdk_device_manager_get_client_pointer (device_manager);
3453
3454 if ((pointer_grab != NULL && pointer_grab->window == window) ||
3455 (keyboard_grab != NULL && keyboard_grab->window == window))
3456 gdk_device_ungrab (device, msg -> time);
3457 }
3458
3459 /* Send MAP events */
3460 if ((windowpos->flags & SWP_SHOWWINDOW) &&
3461 !GDK_WINDOW_DESTROYED (window))
3462 {
3463 event = gdk_event_new (GDK_MAP);
3464 event->any.window = window;
3465 _gdk_win32_append_event (event);
3466 }
3467
3468 /* Update window state */
3469 if (windowpos->flags & (SWP_STATECHANGED | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
3470 {
3471 GdkWindowState set_bits, unset_bits, old_state, new_state;
3472
3473 old_state = window->state;
3474
3475 set_bits = 0;
3476 unset_bits = 0;
3477
3478 if (IsWindowVisible (msg->hwnd))
3479 unset_bits |= GDK_WINDOW_STATE_WITHDRAWN;
3480 else
3481 set_bits |= GDK_WINDOW_STATE_WITHDRAWN;
3482
3483 if (IsIconic (msg->hwnd))
3484 set_bits |= GDK_WINDOW_STATE_ICONIFIED;
3485 else
3486 unset_bits |= GDK_WINDOW_STATE_ICONIFIED;
3487
3488 if (IsZoomed (msg->hwnd))
3489 set_bits |= GDK_WINDOW_STATE_MAXIMIZED;
3490 else
3491 unset_bits |= GDK_WINDOW_STATE_MAXIMIZED;
3492
3493 gdk_synthesize_window_state (window, unset_bits, set_bits);
3494
3495 new_state = window->state;
3496
3497 /* Whenever one window changes iconified state we need to also
3498 * change the iconified state in all transient related windows,
3499 * as windows doesn't give icons for transient childrens.
3500 */
3501 if ((old_state & GDK_WINDOW_STATE_ICONIFIED) !=
3502 (new_state & GDK_WINDOW_STATE_ICONIFIED))
3503 do_show_window (window, (new_state & GDK_WINDOW_STATE_ICONIFIED));
3504 }
3505
3506 /* Show, New size or position => configure event */
3507 if (!(windowpos->flags & SWP_NOCLIENTMOVE) ||
3508 !(windowpos->flags & SWP_NOCLIENTSIZE) ||
3509 (windowpos->flags & SWP_SHOWWINDOW))
3510 {
3511 if (GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&
3512 !IsIconic (msg->hwnd) &&
3513 !GDK_WINDOW_DESTROYED (window))
3514 _gdk_win32_emit_configure_event (window);
3515 }
3516
3517 if ((windowpos->flags & SWP_HIDEWINDOW) &&
3518 !GDK_WINDOW_DESTROYED (window))
3519 {
3520 /* Send UNMAP events */
3521 event = gdk_event_new (GDK_UNMAP);
3522 event->any.window = window;
3523 _gdk_win32_append_event (event);
3524
3525 /* Make transient parent the forground window when window unmaps */
3526 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
3527
3528 if (impl->transient_owner &&
3529 GetForegroundWindow () == GDK_WINDOW_HWND (window))
3530 SetForegroundWindow (GDK_WINDOW_HWND (impl->transient_owner));
3531 }
3532
3533 if (!(windowpos->flags & SWP_NOCLIENTSIZE))
3534 {
3535 if (window->resize_count > 1)
3536 window->resize_count -= 1;
3537 }
3538
3539 /* Call modal timer immediate so that we repaint faster after a resize. */
3540 if (_modal_operation_in_progress & GDK_WIN32_MODAL_OP_SIZEMOVE_MASK)
3541 modal_timer_proc (0,0,0,0);
3542
3543 /* Claim as handled, so that WM_SIZE and WM_MOVE are avoided */
3544 return_val = TRUE;
3545 *ret_valp = 0;
3546 break;
3547
3548 case WM_SIZING:
3549 GetWindowRect (GDK_WINDOW_HWND (window), &rect);
3550 drag = (RECT *) msg->lParam;
3551 GDK_NOTE (EVENTS, g_print (" %s curr:%s drag:%s",
3552 (msg->wParam == WMSZ_BOTTOM ? "BOTTOM" :
3553 (msg->wParam == WMSZ_BOTTOMLEFT ? "BOTTOMLEFT" :
3554 (msg->wParam == WMSZ_LEFT ? "LEFT" :
3555 (msg->wParam == WMSZ_TOPLEFT ? "TOPLEFT" :
3556 (msg->wParam == WMSZ_TOP ? "TOP" :
3557 (msg->wParam == WMSZ_TOPRIGHT ? "TOPRIGHT" :
3558 (msg->wParam == WMSZ_RIGHT ? "RIGHT" :
3559
3560 (msg->wParam == WMSZ_BOTTOMRIGHT ? "BOTTOMRIGHT" :
3561 "???")))))))),
3562 _gdk_win32_rect_to_string (&rect),
3563 _gdk_win32_rect_to_string (drag)));
3564
3565 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
3566 orig_drag = *drag;
3567 if (impl->hint_flags & GDK_HINT_RESIZE_INC)
3568 {
3569 GDK_NOTE (EVENTS, g_print (" (RESIZE_INC)"));
3570 if (impl->hint_flags & GDK_HINT_BASE_SIZE)
3571 {
3572 /* Resize in increments relative to the base size */
3573 rect.left = rect.top = 0;
3574 rect.right = impl->hints.base_width * impl->window_scale;
3575 rect.bottom = impl->hints.base_height * impl->window_scale;
3576 _gdk_win32_adjust_client_rect (window, &rect);
3577 point.x = rect.left;
3578 point.y = rect.top;
3579 ClientToScreen (GDK_WINDOW_HWND (window), &point);
3580 rect.left = point.x;
3581 rect.top = point.y;
3582 point.x = rect.right;
3583 point.y = rect.bottom;
3584 ClientToScreen (GDK_WINDOW_HWND (window), &point);
3585 rect.right = point.x;
3586 rect.bottom = point.y;
3587
3588 GDK_NOTE (EVENTS, g_print (" (also BASE_SIZE, using %s)",
3589 _gdk_win32_rect_to_string (&rect)));
3590 }
3591
3592 switch (msg->wParam)
3593 {
3594 case WMSZ_BOTTOM:
3595 if (drag->bottom == rect.bottom)
3596 break;
3597 adjust_drag (&drag->bottom, rect.bottom, impl->hints.height_inc * impl->window_scale);
3598 break;
3599
3600 case WMSZ_BOTTOMLEFT:
3601 if (drag->bottom == rect.bottom && drag->left == rect.left)
3602 break;
3603 adjust_drag (&drag->bottom, rect.bottom, impl->hints.height_inc * impl->window_scale);
3604 adjust_drag (&drag->left, rect.left, impl->hints.width_inc * impl->window_scale);
3605 break;
3606
3607 case WMSZ_LEFT:
3608 if (drag->left == rect.left)
3609 break;
3610 adjust_drag (&drag->left, rect.left, impl->hints.width_inc * impl->window_scale);
3611 break;
3612
3613 case WMSZ_TOPLEFT:
3614 if (drag->top == rect.top && drag->left == rect.left)
3615 break;
3616 adjust_drag (&drag->top, rect.top, impl->hints.height_inc * impl->window_scale);
3617 adjust_drag (&drag->left, rect.left, impl->hints.width_inc * impl->window_scale);
3618 break;
3619
3620 case WMSZ_TOP:
3621 if (drag->top == rect.top)
3622 break;
3623 adjust_drag (&drag->top, rect.top, impl->hints.height_inc * impl->window_scale);
3624 break;
3625
3626 case WMSZ_TOPRIGHT:
3627 if (drag->top == rect.top && drag->right == rect.right)
3628 break;
3629 adjust_drag (&drag->top, rect.top, impl->hints.height_inc * impl->window_scale);
3630 adjust_drag (&drag->right, rect.right, impl->hints.width_inc * impl->window_scale);
3631 break;
3632
3633 case WMSZ_RIGHT:
3634 if (drag->right == rect.right)
3635 break;
3636 adjust_drag (&drag->right, rect.right, impl->hints.width_inc * impl->window_scale);
3637 break;
3638
3639 case WMSZ_BOTTOMRIGHT:
3640 if (drag->bottom == rect.bottom && drag->right == rect.right)
3641 break;
3642 adjust_drag (&drag->bottom, rect.bottom, impl->hints.height_inc * impl->window_scale);
3643 adjust_drag (&drag->right, rect.right, impl->hints.width_inc * impl->window_scale);
3644 break;
3645 }
3646
3647 if (drag->bottom != orig_drag.bottom || drag->left != orig_drag.left ||
3648 drag->top != orig_drag.top || drag->right != orig_drag.right)
3649 {
3650 *ret_valp = TRUE;
3651 return_val = TRUE;
3652 GDK_NOTE (EVENTS, g_print (" (handled RESIZE_INC: %s)",
3653 _gdk_win32_rect_to_string (drag)));
3654 }
3655 }
3656
3657 /* WM_GETMINMAXINFO handles min_size and max_size hints? */
3658
3659 if (impl->hint_flags & GDK_HINT_ASPECT)
3660 {
3661 RECT decorated_rect;
3662 RECT undecorated_drag;
3663 int decoration_width, decoration_height;
3664 gdouble drag_aspect;
3665 int drag_width, drag_height, new_width, new_height;
3666
3667 GetClientRect (GDK_WINDOW_HWND (window), &rect);
3668 decorated_rect = rect;
3669 _gdk_win32_adjust_client_rect (window, &decorated_rect);
3670
3671 /* Set undecorated_drag to the client area being dragged
3672 * out, in screen coordinates.
3673 */
3674 undecorated_drag = *drag;
3675 undecorated_drag.left -= decorated_rect.left - rect.left;
3676 undecorated_drag.right -= decorated_rect.right - rect.right;
3677 undecorated_drag.top -= decorated_rect.top - rect.top;
3678 undecorated_drag.bottom -= decorated_rect.bottom - rect.bottom;
3679
3680 decoration_width = (decorated_rect.right - decorated_rect.left) - (rect.right - rect.left);
3681 decoration_height = (decorated_rect.bottom - decorated_rect.top) - (rect.bottom - rect.top);
3682
3683 drag_width = undecorated_drag.right - undecorated_drag.left;
3684 drag_height = undecorated_drag.bottom - undecorated_drag.top;
3685
3686 drag_aspect = (gdouble) drag_width / drag_height;
3687
3688 GDK_NOTE (EVENTS, g_print (" (ASPECT:%g--%g curr: %g)",
3689 impl->hints.min_aspect, impl->hints.max_aspect, drag_aspect));
3690
3691 if (drag_aspect < impl->hints.min_aspect)
3692 {
3693 /* Aspect is getting too narrow */
3694 switch (msg->wParam)
3695 {
3696 case WMSZ_BOTTOM:
3697 case WMSZ_TOP:
3698 /* User drags top or bottom edge outward. Keep height, increase width. */
3699 new_width = impl->hints.min_aspect * drag_height;
3700 drag->left -= (new_width - drag_width) / 2;
3701 drag->right = drag->left + new_width + decoration_width;
3702 break;
3703 case WMSZ_BOTTOMLEFT:
3704 case WMSZ_BOTTOMRIGHT:
3705 /* User drags bottom-left or bottom-right corner down. Adjust height. */
3706 new_height = drag_width / impl->hints.min_aspect;
3707 drag->bottom = drag->top + new_height + decoration_height;
3708 break;
3709 case WMSZ_LEFT:
3710 case WMSZ_RIGHT:
3711 /* User drags left or right edge inward. Decrease height */
3712 new_height = drag_width / impl->hints.min_aspect;
3713 drag->top += (drag_height - new_height) / 2;
3714 drag->bottom = drag->top + new_height + decoration_height;
3715 break;
3716 case WMSZ_TOPLEFT:
3717 case WMSZ_TOPRIGHT:
3718 /* User drags top-left or top-right corner up. Adjust height. */
3719 new_height = drag_width / impl->hints.min_aspect;
3720 drag->top = drag->bottom - new_height - decoration_height;
3721 }
3722 }
3723 else if (drag_aspect > impl->hints.max_aspect)
3724 {
3725 /* Aspect is getting too wide */
3726 switch (msg->wParam)
3727 {
3728 case WMSZ_BOTTOM:
3729 case WMSZ_TOP:
3730 /* User drags top or bottom edge inward. Decrease width. */
3731 new_width = impl->hints.max_aspect * drag_height;
3732 drag->left += (drag_width - new_width) / 2;
3733 drag->right = drag->left + new_width + decoration_width;
3734 break;
3735 case WMSZ_BOTTOMLEFT:
3736 case WMSZ_TOPLEFT:
3737 /* User drags bottom-left or top-left corner left. Adjust width. */
3738 new_width = impl->hints.max_aspect * drag_height;
3739 drag->left = drag->right - new_width - decoration_width;
3740 break;
3741 case WMSZ_BOTTOMRIGHT:
3742 case WMSZ_TOPRIGHT:
3743 /* User drags bottom-right or top-right corner right. Adjust width. */
3744 new_width = impl->hints.max_aspect * drag_height;
3745 drag->right = drag->left + new_width + decoration_width;
3746 break;
3747 case WMSZ_LEFT:
3748 case WMSZ_RIGHT:
3749 /* User drags left or right edge outward. Increase height. */
3750 new_height = drag_width / impl->hints.max_aspect;
3751 drag->top -= (new_height - drag_height) / 2;
3752 drag->bottom = drag->top + new_height + decoration_height;
3753 break;
3754 }
3755 }
3756
3757 *ret_valp = TRUE;
3758 return_val = TRUE;
3759 GDK_NOTE (EVENTS, g_print (" (handled ASPECT: %s)",
3760 _gdk_win32_rect_to_string (drag)));
3761 }
3762 break;
3763
3764 case WM_GETMINMAXINFO:
3765 mmi = (MINMAXINFO*) msg->lParam;
3766
3767 GDK_NOTE (EVENTS, g_print (" (mintrack:%ldx%ld maxtrack:%ldx%ld "
3768 "maxpos:%+ld%+ld maxsize:%ldx%ld)",
3769 mmi->ptMinTrackSize.x, mmi->ptMinTrackSize.y,
3770 mmi->ptMaxTrackSize.x, mmi->ptMaxTrackSize.y,
3771 mmi->ptMaxPosition.x, mmi->ptMaxPosition.y,
3772 mmi->ptMaxSize.x, mmi->ptMaxSize.y));
3773
3774 if (_gdk_win32_window_fill_min_max_info (window, mmi))
3775 {
3776 /* Don't call DefWindowProcW() */
3777 GDK_NOTE (EVENTS,
3778 g_print (" (handled, mintrack:%ldx%ld maxtrack:%ldx%ld "
3779 "maxpos:%+ld%+ld maxsize:%ldx%ld)",
3780 mmi->ptMinTrackSize.x, mmi->ptMinTrackSize.y,
3781 mmi->ptMaxTrackSize.x, mmi->ptMaxTrackSize.y,
3782 mmi->ptMaxPosition.x, mmi->ptMaxPosition.y,
3783 mmi->ptMaxSize.x, mmi->ptMaxSize.y));
3784
3785 return_val = TRUE;
3786 }
3787
3788 break;
3789
3790 case WM_CLOSE:
3791 if (GDK_WINDOW_DESTROYED (window))
3792 break;
3793
3794 event = gdk_event_new (GDK_DELETE);
3795 event->any.window = window;
3796
3797 _gdk_win32_append_event (event);
3798
3799 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
3800
3801 if (impl->transient_owner && GetForegroundWindow() == GDK_WINDOW_HWND (window))
3802 {
3803 SetForegroundWindow (GDK_WINDOW_HWND (impl->transient_owner));
3804 }
3805
3806 return_val = TRUE;
3807 break;
3808
3809 case WM_DESTROY:
3810 /* we have to call RemoveProp before the window is destroyed */
3811 if (_gdk_win32_tablet_input_api == GDK_WIN32_TABLET_INPUT_API_WINPOINTER)
3812 gdk_winpointer_finalize_window (window);
3813
3814 return_val = FALSE;
3815 break;
3816
3817 case WM_NCDESTROY:
3818 if ((pointer_grab != NULL && pointer_grab -> window == window) ||
3819 (keyboard_grab && keyboard_grab -> window == window))
3820 {
3821 GdkDevice *device = gdk_device_manager_get_client_pointer (device_manager);
3822 gdk_device_ungrab (device, msg -> time);
3823 }
3824
3825 if ((window != NULL) && (msg->hwnd != GetDesktopWindow ()))
3826 gdk_window_destroy_notify (window);
3827
3828 if (window == NULL || GDK_WINDOW_DESTROYED (window))
3829 break;
3830
3831 event = gdk_event_new (GDK_DESTROY);
3832 event->any.window = window;
3833
3834 _gdk_win32_append_event (event);
3835
3836 return_val = TRUE;
3837 break;
3838
3839 case WM_DWMCOMPOSITIONCHANGED:
3840 _gdk_win32_window_enable_transparency (window);
3841 break;
3842
3843 case WM_DESTROYCLIPBOARD:
3844 win32_sel = _gdk_win32_selection_get ();
3845
3846 if (!win32_sel->ignore_destroy_clipboard)
3847 {
3848 event = gdk_event_new (GDK_SELECTION_CLEAR);
3849 event->selection.window = window;
3850 event->selection.selection = GDK_SELECTION_CLIPBOARD;
3851 event->selection.time = _gdk_win32_get_next_tick (msg->time);
3852 _gdk_win32_append_event (event);
3853 }
3854 else
3855 {
3856 return_val = TRUE;
3857 }
3858
3859 break;
3860
3861 case WM_RENDERFORMAT:
3862 GDK_NOTE (EVENTS, g_print (" %s", _gdk_win32_cf_to_string (msg->wParam)));
3863
3864 *ret_valp = 0;
3865 return_val = TRUE;
3866
3867 win32_sel = _gdk_win32_selection_get ();
3868
3869 for (target = NULL, i = 0;
3870 i < win32_sel->clipboard_selection_targets->len;
3871 i++)
3872 {
3873 GdkSelTargetFormat target_format = g_array_index (win32_sel->clipboard_selection_targets, GdkSelTargetFormat, i);
3874
3875 if (target_format.format == msg->wParam)
3876 {
3877 target = target_format.target;
3878 win32_sel->property_change_transmute = target_format.transmute;
3879 }
3880 }
3881
3882 if (target == NULL)
3883 {
3884 GDK_NOTE (EVENTS, g_print (" (target not found)"));
3885 break;
3886 }
3887
3888 /* We need to render to clipboard immediately, don't call
3889 * _gdk_win32_append_event()
3890 */
3891 event = gdk_event_new (GDK_SELECTION_REQUEST);
3892 event->selection.window = window;
3893 event->selection.send_event = FALSE;
3894 event->selection.selection = GDK_SELECTION_CLIPBOARD;
3895 event->selection.target = target;
3896 event->selection.property = _gdk_win32_selection_atom (GDK_WIN32_ATOM_INDEX_GDK_SELECTION);
3897 event->selection.requestor = gdk_win32_handle_table_lookup (msg->hwnd);
3898 event->selection.time = msg->time;
3899 property_change_data = g_new0 (STGMEDIUM, 1);
3900 win32_sel->property_change_data = property_change_data;
3901 win32_sel->property_change_format = msg->wParam;
3902 win32_sel->property_change_target_atom = target;
3903
3904 fixup_event (event);
3905 GDK_NOTE (EVENTS, g_print (" (calling _gdk_event_emit)"));
3906 GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
3907 _gdk_event_emit (event);
3908 gdk_event_free (event);
3909 win32_sel->property_change_format = 0;
3910
3911 /* Now the clipboard owner should have rendered */
3912 if (!property_change_data->hGlobal)
3913 {
3914 GDK_NOTE (EVENTS, g_print (" (no _delayed_rendering_data?)"));
3915 }
3916 else
3917 {
3918 /* The requestor is holding the clipboard, no
3919 * OpenClipboard() is required/possible
3920 */
3921 GDK_NOTE (DND,
3922 g_print (" SetClipboardData(%s,%p)",
3923 _gdk_win32_cf_to_string (msg->wParam),
3924 property_change_data->hGlobal));
3925
3926 API_CALL (SetClipboardData, (msg->wParam, property_change_data->hGlobal));
3927 }
3928
3929 g_clear_pointer (&property_change_data, g_free);
3930 *ret_valp = 0;
3931 return_val = TRUE;
3932 break;
3933
3934 case WM_RENDERALLFORMATS:
3935 *ret_valp = 0;
3936 return_val = TRUE;
3937
3938 win32_sel = _gdk_win32_selection_get ();
3939
3940 if (API_CALL (OpenClipboard, (msg->hwnd)))
3941 {
3942 for (target = NULL, i = 0;
3943 i < win32_sel->clipboard_selection_targets->len;
3944 i++)
3945 {
3946 GdkSelTargetFormat target_format = g_array_index (win32_sel->clipboard_selection_targets, GdkSelTargetFormat, i);
3947 if (target_format.format != 0)
3948 SendMessage (msg->hwnd, WM_RENDERFORMAT, target_format.format, 0);
3949 }
3950
3951 API_CALL (CloseClipboard, ());
3952 }
3953 break;
3954
3955 case WM_ACTIVATE:
3956 GDK_NOTE (EVENTS, g_print (" %s%s %p",
3957 (LOWORD (msg->wParam) == WA_ACTIVE ? "ACTIVE" :
3958 (LOWORD (msg->wParam) == WA_CLICKACTIVE ? "CLICKACTIVE" :
3959 (LOWORD (msg->wParam) == WA_INACTIVE ? "INACTIVE" : "???"))),
3960 HIWORD (msg->wParam) ? " minimized" : "",
3961 (HWND) msg->lParam));
3962 /* We handle mouse clicks for modally-blocked windows under WM_MOUSEACTIVATE,
3963 * but we still need to deal with alt-tab, or with SetActiveWindow() type
3964 * situations.
3965 */
3966 if (_gdk_modal_blocked (window) && LOWORD (msg->wParam) == WA_ACTIVE)
3967 {
3968 GdkWindow *modal_current = _gdk_modal_current ();
3969 SetActiveWindow (GDK_WINDOW_HWND (modal_current));
3970 *ret_valp = 0;
3971 return_val = TRUE;
3972 break;
3973 }
3974
3975 if (LOWORD (msg->wParam) == WA_INACTIVE)
3976 gdk_synthesize_window_state (window, GDK_WINDOW_STATE_FOCUSED, 0);
3977 else
3978 gdk_synthesize_window_state (window, 0, GDK_WINDOW_STATE_FOCUSED);
3979
3980 /* Bring any tablet contexts to the top of the overlap order when
3981 * one of our windows is activated.
3982 * NOTE: It doesn't seem to work well if it is done in WM_ACTIVATEAPP
3983 * instead
3984 */
3985 if (LOWORD(msg->wParam) != WA_INACTIVE)
3986 {
3987 if (_gdk_win32_tablet_input_api == GDK_WIN32_TABLET_INPUT_API_WINTAB)
3988 _gdk_wintab_set_tablet_active ();
3989 }
3990 break;
3991
3992 case WM_ACTIVATEAPP:
3993 GDK_NOTE (EVENTS, g_print (" %s thread: %" G_GINT64_FORMAT,
3994 msg->wParam ? "YES" : "NO",
3995 (gint64) msg->lParam));
3996
3997 // Clear graphics tablet state
3998 _gdk_input_ignore_core = 0;
3999 break;
4000
4001 case WM_NCHITTEST:
4002 /* TODO: pass all messages to DwmDefWindowProc() first! */
4003 return_val = handle_nchittest (msg->hwnd, window,
4004 GET_X_LPARAM (msg->lParam),
4005 GET_Y_LPARAM (msg->lParam), ret_valp);
4006 break;
4007
4008 /* Handle WINTAB events here, as we know that the device manager will
4009 * use the fixed WT_DEFBASE as lcMsgBase, and we thus can use the
4010 * constants as case labels.
4011 */
4012 case WT_PACKET:
4013 GDK_NOTE (EVENTS, g_print (" %d %p",
4014 (int) msg->wParam, (gpointer) msg->lParam));
4015 goto wintab;
4016
4017 case WT_CSRCHANGE:
4018 GDK_NOTE (EVENTS, g_print (" %d %p",
4019 (int) msg->wParam, (gpointer) msg->lParam));
4020 goto wintab;
4021
4022 case WT_PROXIMITY:
4023 GDK_NOTE (EVENTS, g_print (" %p %d %d",
4024 (gpointer) msg->wParam,
4025 LOWORD (msg->lParam),
4026 HIWORD (msg->lParam)));
4027 /* Fall through */
4028 wintab:
4029 if (_gdk_win32_tablet_input_api != GDK_WIN32_TABLET_INPUT_API_WINTAB)
4030 break;
4031
4032 event = gdk_event_new (GDK_NOTHING);
4033 event->any.window = window;
4034 g_object_ref (window);
4035
4036 if (gdk_wintab_input_events (display, event, msg, window))
4037 _gdk_win32_append_event (event);
4038 else
4039 gdk_event_free (event);
4040
4041 break;
4042
4043 case WM_TABLET_QUERYSYSTEMGESTURESTATUS:
4044
4045 *ret_valp = TABLET_DISABLE_PRESSANDHOLD |
4046 TABLET_DISABLE_PENTAPFEEDBACK |
4047 TABLET_DISABLE_PENBARRELFEEDBACK |
4048 TABLET_DISABLE_FLICKS |
4049 TABLET_DISABLE_FLICKFALLBACKKEYS;
4050
4051 return_val = TRUE;
4052 break;
4053 }
4054
4055 done:
4056
4057 if (window)
4058 g_object_unref (window);
4059
4060 #undef return
4061 return return_val;
4062 }
4063
4064 void
4065 _gdk_win32_display_queue_events (GdkDisplay *display)
4066 {
4067 MSG msg;
4068
4069 if (modal_win32_dialog != NULL)
4070 return;
4071
4072 while (PeekMessageW (&msg, NULL, 0, 0, PM_REMOVE))
4073 {
4074 TranslateMessage (&msg);
4075 DispatchMessageW (&msg);
4076 }
4077 }
4078
4079 static gboolean
4080 gdk_event_prepare (GSource *source,
4081 gint *timeout)
4082 {
4083 GdkWin32EventSource *event_source = (GdkWin32EventSource *)source;
4084 gboolean retval;
4085
4086 gdk_threads_enter ();
4087
4088 *timeout = -1;
4089
4090 if (event_source->display->event_pause_count > 0)
4091 retval =_gdk_event_queue_find_first (event_source->display) != NULL;
4092 else
4093 retval = (_gdk_event_queue_find_first (event_source->display) != NULL ||
4094 (modal_win32_dialog == NULL &&
4095 GetQueueStatus (QS_ALLINPUT) != 0));
4096
4097 gdk_threads_leave ();
4098
4099 return retval;
4100 }
4101
4102 static gboolean
4103 gdk_event_check (GSource *source)
4104 {
4105 GdkWin32EventSource *event_source = (GdkWin32EventSource *)source;
4106 gboolean retval;
4107
4108 gdk_threads_enter ();
4109
4110 if (event_source->display->event_pause_count > 0)
4111 retval = _gdk_event_queue_find_first (event_source->display) != NULL;
4112 else if (event_source->event_poll_fd.revents & G_IO_IN)
4113 retval = (_gdk_event_queue_find_first (event_source->display) != NULL ||
4114 (modal_win32_dialog == NULL &&
4115 GetQueueStatus (QS_ALLINPUT) != 0));
4116 else
4117 retval = FALSE;
4118
4119 gdk_threads_leave ();
4120
4121 return retval;
4122 }
4123
4124 static gboolean
4125 gdk_event_dispatch (GSource *source,
4126 GSourceFunc callback,
4127 gpointer user_data)
4128 {
4129 GdkWin32EventSource *event_source = (GdkWin32EventSource *)source;
4130 GdkEvent *event;
4131
4132 gdk_threads_enter ();
4133
4134 _gdk_win32_display_queue_events (event_source->display);
4135 event = _gdk_event_unqueue (event_source->display);
4136
4137 if (event)
4138 {
4139 GdkWin32Selection *sel_win32 = _gdk_win32_selection_get ();
4140
4141 _gdk_event_emit (event);
4142
4143 gdk_event_free (event);
4144
4145 /* Do drag & drop if it is still pending */
4146 if (sel_win32->dnd_source_state == GDK_WIN32_DND_PENDING)
4147 {
4148 sel_win32->dnd_source_state = GDK_WIN32_DND_DRAGGING;
4149 _gdk_win32_dnd_do_dragdrop ();
4150 sel_win32->dnd_source_state = GDK_WIN32_DND_NONE;
4151 }
4152 }
4153
4154 gdk_threads_leave ();
4155
4156 return TRUE;
4157 }
4158
4159 void
4160 gdk_win32_set_modal_dialog_libgtk_only (HWND window)
4161 {
4162 modal_win32_dialog = window;
4163 }