Debian Codegraph @

gdk/win32/gdkevents-win32.c

raw symbols SCIP
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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23 /*
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28 */
29
30 /* Cannot use TrackMouseEvent, as the stupid WM_MOUSELEAVE message
31 * doesn't tell us where the mouse has gone. Thus we cannot use it to
32 * generate a correct GdkNotifyType. Pity, as using TrackMouseEvent
33 * otherwise would make it possible to reliably generate
34 * GDK_LEAVE_NOTIFY events, which would help get rid of those pesky
35 * tooltips sometimes popping up in the wrong place.
36 *
37 * Update: a combination of TrackMouseEvent, GetCursorPos and
38 * GetWindowPos can and is actually used to get rid of those
39 * pesky tooltips. It should be possible to use this for the
40 * whole ENTER/LEAVE NOTIFY handling but some platforms may
41 * not have TrackMouseEvent at all (?) --hb
42 */
43
44 #include "config.h"
45
46 #include <glib/gprintf.h>
47
48 #include "gdk.h"
49 #include "gdkprivate-win32.h"
50 #include "gdkinput-win32.h"
51 #include "gdkkeysyms.h"
52
53 #include <windowsx.h>
54
55 #ifdef G_WITH_CYGWIN
56 #include <fcntl.h>
57 #include <errno.h>
58 #endif
59
60 #include <objbase.h>
61
62 #include <imm.h>
63
64 #ifndef XBUTTON1
65 #define XBUTTON1 1
66 #define XBUTTON2 2
67 #endif
68
69 #ifndef VK_XBUTTON1
70 #define VK_XBUTTON1 5
71 #define VK_XBUTTON2 6
72 #endif
73
74 #ifndef MK_XBUTTON1
75 #define MK_XBUTTON1 32
76 #define MK_XBUTTON2 64
77 #endif
78
79 /* Undefined flags: */
80 #define SWP_NOCLIENTSIZE 0x0800
81 #define SWP_NOCLIENTMOVE 0x1000
82 #define SWP_STATECHANGED 0x8000
83 /*
84 * Private function declarations
85 */
86
87 #define SYNAPSIS_ICON_WINDOW_CLASS "SynTrackCursorWindowClass"
88
89 static gboolean gdk_event_translate (MSG *msg,
90 gint *ret_valp);
91 static void handle_wm_paint (MSG *msg,
92 GdkWindow *window,
93 gboolean return_exposes,
94 GdkEvent **event);
95
96 static gboolean gdk_event_prepare (GSource *source,
97 gint *timeout);
98 static gboolean gdk_event_check (GSource *source);
99 static gboolean gdk_event_dispatch (GSource *source,
100 GSourceFunc callback,
101 gpointer user_data);
102
103 /* Private variable declarations
104 */
105
106 static GList *client_filters; /* Filters for client messages */
107
108 static HCURSOR p_grab_cursor;
109
110 static GSourceFuncs event_funcs = {
111 gdk_event_prepare,
112 gdk_event_check,
113 gdk_event_dispatch,
114 NULL
115 };
116
117 GPollFD event_poll_fd;
118
119 static GdkWindow *mouse_window = NULL;
120 static GdkWindow *mouse_window_ignored_leave = NULL;
121 static gint current_x, current_y;
122 static gint current_root_x, current_root_y;
123 static UINT client_message;
124
125 static UINT got_gdk_events_message;
126 static HWND modal_win32_dialog = NULL;
127
128 #if 0
129 static HKL latin_locale = NULL;
130 #endif
131
132 static gboolean in_ime_composition = FALSE;
133 static UINT modal_timer;
134 static UINT sync_timer = 0;
135
136 static int debug_indent = 0;
137
138 static void
139 assign_object (gpointer lhsp,
140 gpointer rhs)
141 {
142 if (*(gpointer *)lhsp != rhs)
143 {
144 if (*(gpointer *)lhsp != NULL)
145 g_object_unref (*(gpointer *)lhsp);
146 *(gpointer *)lhsp = rhs;
147 if (rhs != NULL)
148 g_object_ref (rhs);
149 }
150 }
151
152 static void
153 track_mouse_event (DWORD dwFlags,
154 HWND hwnd)
155 {
156 TRACKMOUSEEVENT tme;
157
158 tme.cbSize = sizeof(TRACKMOUSEEVENT);
159 tme.dwFlags = dwFlags;
160 tme.hwndTrack = hwnd;
161 tme.dwHoverTime = HOVER_DEFAULT; /* not used */
162
163 if (!TrackMouseEvent (&tme))
164 WIN32_API_FAILED ("TrackMouseEvent");
165 else if (dwFlags == TME_LEAVE)
166 GDK_NOTE (EVENTS, g_print(" (TrackMouseEvent %p)", hwnd));
167 else if (dwFlags == TME_CANCEL)
168 GDK_NOTE (EVENTS, g_print(" (cancel TrackMouseEvent %p)", hwnd));
169 }
170
171 gulong
172 _gdk_win32_get_next_tick (gulong suggested_tick)
173 {
174 static gulong cur_tick = 0;
175
176 if (suggested_tick == 0)
177 suggested_tick = GetTickCount ();
178 if (suggested_tick <= cur_tick)
179 return cur_tick;
180 else
181 return cur_tick = suggested_tick;
182 }
183
184 static void
185 generate_focus_event (GdkWindow *window,
186 gboolean in)
187 {
188 GdkEvent *event;
189
190 event = gdk_event_new (GDK_FOCUS_CHANGE);
191 event->focus_change.window = window;
192 event->focus_change.in = in;
193
194 _gdk_win32_append_event (event);
195 }
196
197 static void
198 generate_grab_broken_event (GdkWindow *window,
199 gboolean keyboard,
200 GdkWindow *grab_window)
201 {
202 GdkEvent *event = gdk_event_new (GDK_GRAB_BROKEN);
203
204 event->grab_broken.window = window;
205 event->grab_broken.send_event = 0;
206 event->grab_broken.keyboard = keyboard;
207 event->grab_broken.implicit = FALSE;
208 event->grab_broken.grab_window = grab_window;
209
210 _gdk_win32_append_event (event);
211 }
212
213 static LRESULT
214 inner_window_procedure (HWND hwnd,
215 UINT message,
216 WPARAM wparam,
217 LPARAM lparam)
218 {
219 MSG msg;
220 DWORD pos;
221 gint ret_val = 0;
222
223 msg.hwnd = hwnd;
224 msg.message = message;
225 msg.wParam = wparam;
226 msg.lParam = lparam;
227 msg.time = _gdk_win32_get_next_tick (0);
228 pos = GetMessagePos ();
229 msg.pt.x = GET_X_LPARAM (pos);
230 msg.pt.y = GET_Y_LPARAM (pos);
231
232 if (gdk_event_translate (&msg, &ret_val))
233 {
234 /* If gdk_event_translate() returns TRUE, we return ret_val from
235 * the window procedure.
236 */
237 if (modal_win32_dialog)
238 PostMessageW (modal_win32_dialog, got_gdk_events_message,
239 (WPARAM) 1, 0);
240 return ret_val;
241 }
242 else
243 {
244 /* Otherwise call DefWindowProcW(). */
245 GDK_NOTE (EVENTS, g_print (" DefWindowProcW"));
246 return DefWindowProcW (hwnd, message, wparam, lparam);
247 }
248 }
249
250 LRESULT CALLBACK
251 _gdk_win32_window_procedure (HWND hwnd,
252 UINT message,
253 WPARAM wparam,
254 LPARAM lparam)
255 {
256 LRESULT retval;
257
258 GDK_NOTE (EVENTS, g_print ("%s%*s%s %p",
259 (debug_indent > 0 ? "\n" : ""),
260 debug_indent, "",
261 _gdk_win32_message_to_string (message), hwnd));
262 debug_indent += 2;
263 retval = inner_window_procedure (hwnd, message, wparam, lparam);
264 debug_indent -= 2;
265
266 GDK_NOTE (EVENTS, g_print (" => %I64d%s", (gint64) retval, (debug_indent == 0 ? "\n" : "")));
267
268 return retval;
269 }
270
271 void
272 _gdk_events_init (void)
273 {
274 GSource *source;
275
276 #if 0
277 int i, j, n;
278
279 /* List of languages that use a latin keyboard. Somewhat sorted in
280 * "order of least surprise", in case we have to load one of them if
281 * the user only has arabic loaded, for instance.
282 */
283 static int latin_languages[] = {
284 LANG_ENGLISH,
285 LANG_SPANISH,
286 LANG_PORTUGUESE,
287 LANG_FRENCH,
288 LANG_GERMAN,
289 /* Rest in numeric order */
290 LANG_CZECH,
291 LANG_DANISH,
292 LANG_FINNISH,
293 LANG_HUNGARIAN,
294 LANG_ICELANDIC,
295 LANG_ITALIAN,
296 LANG_DUTCH,
297 LANG_NORWEGIAN,
298 LANG_POLISH,
299 LANG_ROMANIAN,
300 LANG_SLOVAK,
301 LANG_ALBANIAN,
302 LANG_SWEDISH,
303 LANG_TURKISH,
304 LANG_INDONESIAN,
305 LANG_SLOVENIAN,
306 LANG_ESTONIAN,
307 LANG_LATVIAN,
308 LANG_LITHUANIAN,
309 LANG_VIETNAMESE,
310 LANG_AFRIKAANS,
311 LANG_FAEROESE
312 #ifdef LANG_SWAHILI
313 ,LANG_SWAHILI
314 #endif
315 };
316 #endif
317
318 client_message = RegisterWindowMessage ("GDK_WIN32_CLIENT_MESSAGE");
319 got_gdk_events_message = RegisterWindowMessage ("GDK_WIN32_GOT_EVENTS");
320
321 #if 0
322 /* Check if we have some input locale identifier loaded that uses a
323 * latin keyboard, to be able to get the virtual-key code for the
324 * latin characters corresponding to ASCII control characters.
325 */
326 if ((n = GetKeyboardLayoutList (0, NULL)) == 0)
327 WIN32_API_FAILED ("GetKeyboardLayoutList");
328 else
329 {
330 HKL *hkl_list = g_new (HKL, n);
331 if (GetKeyboardLayoutList (n, hkl_list) == 0)
332 WIN32_API_FAILED ("GetKeyboardLayoutList");
333 else
334 {
335 for (i = 0; latin_locale == NULL && i < n; i++)
336 for (j = 0; j < G_N_ELEMENTS (latin_languages); j++)
337 if (PRIMARYLANGID (LOWORD (hkl_list[i])) == latin_languages[j])
338 {
339 latin_locale = hkl_list [i];
340 break;
341 }
342 }
343 g_free (hkl_list);
344 }
345
346 if (latin_locale == NULL)
347 {
348 /* Try to load a keyboard layout with latin characters then.
349 */
350 i = 0;
351 while (latin_locale == NULL && i < G_N_ELEMENTS (latin_languages))
352 {
353 char id[9];
354 g_sprintf (id, "%08x", MAKELANGID (latin_languages[i++], SUBLANG_DEFAULT));
355 latin_locale = LoadKeyboardLayout (id, KLF_NOTELLSHELL|KLF_SUBSTITUTE_OK);
356 }
357 }
358
359 GDK_NOTE (EVENTS, g_print ("latin_locale = %08x\n", (guint) latin_locale));
360 #endif
361
362 source = g_source_new (&event_funcs, sizeof (GSource));
363 g_source_set_name (source, "GDK Win32 event source");
364 g_source_set_priority (source, GDK_PRIORITY_EVENTS);
365
366 #ifdef G_WITH_CYGWIN
367 event_poll_fd.fd = open ("/dev/windows", O_RDONLY);
368 if (event_poll_fd.fd == -1)
369 g_error ("can't open \"/dev/windows\": %s", g_strerror (errno));
370 #else
371 event_poll_fd.fd = G_WIN32_MSG_HANDLE;
372 #endif
373 event_poll_fd.events = G_IO_IN;
374
375 g_source_add_poll (source, &event_poll_fd);
376 g_source_set_can_recurse (source, TRUE);
377 g_source_attach (source, NULL);
378 }
379
380 gboolean
381 gdk_events_pending (void)
382 {
383 return (_gdk_event_queue_find_first (_gdk_display) ||
384 (modal_win32_dialog == NULL &&
385 GetQueueStatus (QS_ALLINPUT) != 0));
386 }
387
388 GdkEvent*
389 gdk_event_get_graphics_expose (GdkWindow *window)
390 {
391 MSG msg;
392 GdkEvent *event = NULL;
393
394 g_return_val_if_fail (window != NULL, NULL);
395
396 GDK_NOTE (EVENTS, g_print ("gdk_event_get_graphics_expose\n"));
397
398 if (PeekMessageW (&msg, GDK_WINDOW_HWND (window), WM_PAINT, WM_PAINT, PM_REMOVE))
399 {
400 handle_wm_paint (&msg, window, TRUE, &event);
401 if (event != NULL)
402 {
403 GDK_NOTE (EVENTS, g_print ("gdk_event_get_graphics_expose: got it!\n"));
404 return event;
405 }
406 }
407
408 GDK_NOTE (EVENTS, g_print ("gdk_event_get_graphics_expose: nope\n"));
409 return NULL;
410 }
411
412 #if 0 /* Unused, but might be useful to re-introduce in some debugging output? */
413
414 static char *
415 event_mask_string (GdkEventMask mask)
416 {
417 static char bfr[500];
418 char *p = bfr;
419
420 *p = '\0';
421 #define BIT(x) \
422 if (mask & GDK_##x##_MASK) \
423 p += g_sprintf (p, "%s" #x, (p > bfr ? " " : ""))
424 BIT (EXPOSURE);
425 BIT (POINTER_MOTION);
426 BIT (POINTER_MOTION_HINT);
427 BIT (BUTTON_MOTION);
428 BIT (BUTTON1_MOTION);
429 BIT (BUTTON2_MOTION);
430 BIT (BUTTON3_MOTION);
431 BIT (BUTTON_PRESS);
432 BIT (BUTTON_RELEASE);
433 BIT (KEY_PRESS);
434 BIT (KEY_RELEASE);
435 BIT (ENTER_NOTIFY);
436 BIT (LEAVE_NOTIFY);
437 BIT (FOCUS_CHANGE);
438 BIT (STRUCTURE);
439 BIT (PROPERTY_CHANGE);
440 BIT (VISIBILITY_NOTIFY);
441 BIT (PROXIMITY_IN);
442 BIT (PROXIMITY_OUT);
443 BIT (SUBSTRUCTURE);
444 BIT (SCROLL);
445 #undef BIT
446
447 return bfr;
448 }
449
450 #endif
451
452 GdkGrabStatus
453 _gdk_windowing_pointer_grab (GdkWindow *window,
454 GdkWindow *native_window,
455 gboolean owner_events,
456 GdkEventMask event_mask,
457 GdkWindow *confine_to,
458 GdkCursor *cursor,
459 guint32 time)
460 {
461 HCURSOR hcursor;
462 GdkCursorPrivate *cursor_private;
463 gint return_val;
464
465 g_return_val_if_fail (window != NULL, 0);
466 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
467 g_return_val_if_fail (confine_to == NULL || GDK_IS_WINDOW (confine_to), 0);
468
469 cursor_private = (GdkCursorPrivate*) cursor;
470
471 if (!cursor)
472 hcursor = NULL;
473 else if ((hcursor = CopyCursor (cursor_private->hcursor)) == NULL)
474 WIN32_API_FAILED ("CopyCursor");
475
476 return_val = _gdk_input_grab_pointer (native_window,
477 owner_events,
478 event_mask,
479 confine_to,
480 time);
481
482 if (return_val == GDK_GRAB_SUCCESS)
483 {
484 GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (((GdkWindowObject *) native_window)->impl);
485
486 SetCapture (GDK_WINDOW_HWND (native_window));
487 /* TODO_CSW: grab brokens, confine window, input_grab */
488 if (p_grab_cursor != NULL)
489 {
490 if (GetCursor () == p_grab_cursor)
491 SetCursor (NULL);
492 DestroyCursor (p_grab_cursor);
493 }
494
495 p_grab_cursor = hcursor;
496
497 if (p_grab_cursor != NULL)
498 SetCursor (p_grab_cursor);
499 else if (impl->hcursor != NULL)
500 SetCursor (impl->hcursor);
501 else
502 SetCursor (LoadCursor (NULL, IDC_ARROW));
503
504 }
505
506 return return_val;
507 }
508
509 void
510 gdk_display_pointer_ungrab (GdkDisplay *display,
511 guint32 time)
512 {
513 GdkPointerGrabInfo *info;
514
515 info = _gdk_display_get_last_pointer_grab (display);
516 if (info)
517 {
518 info->serial_end = 0;
519 ReleaseCapture ();
520 }
521
522 _gdk_input_ungrab_pointer (time);
523
524 /* TODO_CSW: cursor, confines, etc */
525
526 _gdk_display_pointer_grab_update (display, 0);
527 }
528
529
530 static GdkWindow *
531 find_window_for_mouse_event (GdkWindow* reported_window,
532 MSG* msg)
533 {
534 POINT pt;
535 GdkWindow *event_window;
536 HWND hwnd;
537 RECT rect;
538 GdkPointerGrabInfo *grab;
539
540 grab = _gdk_display_get_last_pointer_grab (_gdk_display);
541 if (grab == NULL)
542 return reported_window;
543
544 pt = msg->pt;
545
546 if (!grab->owner_events)
547 event_window = grab->native_window;
548 else
549 {
550 event_window = NULL;
551 hwnd = WindowFromPoint (pt);
552 if (hwnd != NULL)
553 {
554 POINT client_pt = pt;
555
556 ScreenToClient (hwnd, &client_pt);
557 GetClientRect (hwnd, &rect);
558 if (PtInRect (&rect, client_pt))
559 event_window = gdk_win32_handle_table_lookup ((GdkNativeWindow) hwnd);
560 }
561 if (event_window == NULL)
562 event_window = grab->native_window;
563 }
564
565 /* need to also adjust the coordinates to the new window */
566 ScreenToClient (GDK_WINDOW_HWND (event_window), &pt);
567
568 /* ATTENTION: need to update client coords */
569 msg->lParam = MAKELPARAM (pt.x, pt.y);
570
571 return event_window;
572 }
573
574 GdkGrabStatus
575 gdk_keyboard_grab (GdkWindow *window,
576 gboolean owner_events,
577 guint32 time)
578 {
579 GdkDisplay *display;
580 GdkWindow *toplevel;
581
582 g_return_val_if_fail (window != NULL, 0);
583 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
584
585 GDK_NOTE (EVENTS, g_print ("gdk_keyboard_grab %p%s\n",
586 GDK_WINDOW_HWND (window), owner_events ? " OWNER_EVENTS" : ""));
587
588 display = gdk_drawable_get_display (window);
589 toplevel = gdk_window_get_toplevel (window);
590
591 _gdk_display_set_has_keyboard_grab (display,
592 window,
593 toplevel,
594 owner_events,
595 0,
596 time);
597
598 return GDK_GRAB_SUCCESS;
599 }
600
601 void
602 gdk_display_keyboard_ungrab (GdkDisplay *display,
603 guint32 time)
604 {
605 GDK_NOTE (EVENTS, g_print ("gdk_display_keyboard_ungrab\n"));
606 _gdk_display_unset_has_keyboard_grab (display, FALSE);
607 }
608
609 void
610 gdk_display_add_client_message_filter (GdkDisplay *display,
611 GdkAtom message_type,
612 GdkFilterFunc func,
613 gpointer data)
614 {
615 /* XXX */
616 gdk_add_client_message_filter (message_type, func, data);
617 }
618
619 void
620 gdk_add_client_message_filter (GdkAtom message_type,
621 GdkFilterFunc func,
622 gpointer data)
623 {
624 GdkClientFilter *filter = g_new (GdkClientFilter, 1);
625
626 filter->type = message_type;
627 filter->function = func;
628 filter->data = data;
629
630 client_filters = g_list_append (client_filters, filter);
631 }
632
633 static void
634 build_key_event_state (GdkEvent *event,
635 BYTE *key_state)
636 {
637 GdkWin32Keymap *keymap;
638
639 event->key.state = 0;
640
641 if (key_state[VK_SHIFT] & 0x80)
642 event->key.state |= GDK_SHIFT_MASK;
643
644 if (key_state[VK_CAPITAL] & 0x01)
645 event->key.state |= GDK_LOCK_MASK;
646
647 if (key_state[VK_LBUTTON] & 0x80)
648 event->key.state |= GDK_BUTTON1_MASK;
649 if (key_state[VK_MBUTTON] & 0x80)
650 event->key.state |= GDK_BUTTON2_MASK;
651 if (key_state[VK_RBUTTON] & 0x80)
652 event->key.state |= GDK_BUTTON3_MASK;
653 if (key_state[VK_XBUTTON1] & 0x80)
654 event->key.state |= GDK_BUTTON4_MASK;
655 if (key_state[VK_XBUTTON2] & 0x80)
656 event->key.state |= GDK_BUTTON5_MASK;
657
658 keymap = GDK_WIN32_KEYMAP (gdk_keymap_get_default ());
659 event->key.group = _gdk_win32_keymap_get_active_group (keymap);
660
661 if (_gdk_win32_keymap_has_altgr (keymap) &&
662 (key_state[VK_LCONTROL] & 0x80) &&
663 (key_state[VK_RMENU] & 0x80))
664 {
665 event->key.state |= GDK_MOD2_MASK;
666 if (key_state[VK_RCONTROL] & 0x80)
667 event->key.state |= GDK_CONTROL_MASK;
668 if (key_state[VK_LMENU] & 0x80)
669 event->key.state |= GDK_MOD1_MASK;
670 }
671 else
672 {
673 if (key_state[VK_CONTROL] & 0x80)
674 event->key.state |= GDK_CONTROL_MASK;
675 if (key_state[VK_MENU] & 0x80)
676 event->key.state |= GDK_MOD1_MASK;
677 }
678 }
679
680 static gint
681 build_pointer_event_state (MSG *msg)
682 {
683 gint state;
684
685 state = 0;
686
687 if (msg->wParam & MK_CONTROL)
688 state |= GDK_CONTROL_MASK;
689
690 if ((msg->message != WM_LBUTTONDOWN &&
691 (msg->wParam & MK_LBUTTON)) ||
692 msg->message == WM_LBUTTONUP)
693 state |= GDK_BUTTON1_MASK;
694
695 if ((msg->message != WM_MBUTTONDOWN &&
696 (msg->wParam & MK_MBUTTON)) ||
697 msg->message == WM_MBUTTONUP)
698 state |= GDK_BUTTON2_MASK;
699
700 if ((msg->message != WM_RBUTTONDOWN &&
701 (msg->wParam & MK_RBUTTON)) ||
702 msg->message == WM_RBUTTONUP)
703 state |= GDK_BUTTON3_MASK;
704
705 if (((msg->message != WM_XBUTTONDOWN || HIWORD (msg->wParam) != XBUTTON1) &&
706 (msg->wParam & MK_XBUTTON1)) ||
707 (msg->message == WM_XBUTTONUP && HIWORD (msg->wParam) == XBUTTON1))
708 state |= GDK_BUTTON4_MASK;
709
710 if (((msg->message != WM_XBUTTONDOWN || HIWORD (msg->wParam) != XBUTTON2) &&
711 (msg->wParam & MK_XBUTTON2)) ||
712 (msg->message == WM_XBUTTONUP && HIWORD (msg->wParam) == XBUTTON2))
713 state |= GDK_BUTTON5_MASK;
714
715 if (msg->wParam & MK_SHIFT)
716 state |= GDK_SHIFT_MASK;
717
718 if (GetKeyState (VK_MENU) < 0)
719 state |= GDK_MOD1_MASK;
720
721 if (GetKeyState (VK_CAPITAL) & 0x1)
722 state |= GDK_LOCK_MASK;
723
724 return state;
725 }
726
727 static void
728 build_wm_ime_composition_event (GdkEvent *event,
729 MSG *msg,
730 wchar_t wc,
731 BYTE *key_state)
732 {
733 event->key.time = _gdk_win32_get_next_tick (msg->time);
734
735 build_key_event_state (event, key_state);
736
737 event->key.hardware_keycode = 0; /* FIXME: What should it be? */
738 event->key.string = NULL;
739 event->key.length = 0;
740 event->key.keyval = gdk_unicode_to_keyval (wc);
741 }
742
743 #ifdef G_ENABLE_DEBUG
744
745 static void
746 print_event_state (guint state)
747 {
748 #define CASE(bit) if (state & GDK_ ## bit ## _MASK) g_print (#bit " ");
749 CASE (SHIFT);
750 CASE (LOCK);
751 CASE (CONTROL);
752 CASE (MOD1);
753 CASE (MOD2);
754 CASE (MOD3);
755 CASE (MOD4);
756 CASE (MOD5);
757 CASE (BUTTON1);
758 CASE (BUTTON2);
759 CASE (BUTTON3);
760 CASE (BUTTON4);
761 CASE (BUTTON5);
762 #undef CASE
763 }
764
765 void
766 _gdk_win32_print_event (const GdkEvent *event)
767 {
768 gchar *escaped, *kvname;
769 gchar *selection_name, *target_name, *property_name;
770
771 g_print ("%s%*s===> ", (debug_indent > 0 ? "\n" : ""), debug_indent, "");
772 switch (event->any.type)
773 {
774 #define CASE(x) case x: g_print (#x); break;
775 CASE (GDK_NOTHING);
776 CASE (GDK_DELETE);
777 CASE (GDK_DESTROY);
778 CASE (GDK_EXPOSE);
779 CASE (GDK_MOTION_NOTIFY);
780 CASE (GDK_BUTTON_PRESS);
781 CASE (GDK_2BUTTON_PRESS);
782 CASE (GDK_3BUTTON_PRESS);
783 CASE (GDK_BUTTON_RELEASE);
784 CASE (GDK_KEY_PRESS);
785 CASE (GDK_KEY_RELEASE);
786 CASE (GDK_ENTER_NOTIFY);
787 CASE (GDK_LEAVE_NOTIFY);
788 CASE (GDK_FOCUS_CHANGE);
789 CASE (GDK_CONFIGURE);
790 CASE (GDK_MAP);
791 CASE (GDK_UNMAP);
792 CASE (GDK_PROPERTY_NOTIFY);
793 CASE (GDK_SELECTION_CLEAR);
794 CASE (GDK_SELECTION_REQUEST);
795 CASE (GDK_SELECTION_NOTIFY);
796 CASE (GDK_PROXIMITY_IN);
797 CASE (GDK_PROXIMITY_OUT);
798 CASE (GDK_DRAG_ENTER);
799 CASE (GDK_DRAG_LEAVE);
800 CASE (GDK_DRAG_MOTION);
801 CASE (GDK_DRAG_STATUS);
802 CASE (GDK_DROP_START);
803 CASE (GDK_DROP_FINISHED);
804 CASE (GDK_CLIENT_EVENT);
805 CASE (GDK_VISIBILITY_NOTIFY);
806 CASE (GDK_NO_EXPOSE);
807 CASE (GDK_SCROLL);
808 CASE (GDK_WINDOW_STATE);
809 CASE (GDK_SETTING);
810 CASE (GDK_OWNER_CHANGE);
811 CASE (GDK_GRAB_BROKEN);
812 #undef CASE
813 default: g_assert_not_reached ();
814 }
815
816 g_print (" %p ", event->any.window ? GDK_WINDOW_HWND (event->any.window) : NULL);
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 g_print ("%s %d %ld %ld %ld %ld %ld",
913 gdk_atom_name (event->client.message_type),
914 event->client.data_format,
915 event->client.data.l[0],
916 event->client.data.l[1],
917 event->client.data.l[2],
918 event->client.data.l[3],
919 event->client.data.l[4]);
920 break;
921 case GDK_SCROLL:
922 g_print ("(%.4g,%.4g) (%.4g,%.4g) %s ",
923 event->scroll.x, event->scroll.y,
924 event->scroll.x_root, event->scroll.y_root,
925 (event->scroll.direction == GDK_SCROLL_UP ? "UP" :
926 (event->scroll.direction == GDK_SCROLL_DOWN ? "DOWN" :
927 (event->scroll.direction == GDK_SCROLL_LEFT ? "LEFT" :
928 (event->scroll.direction == GDK_SCROLL_RIGHT ? "RIGHT" :
929 "???")))));
930 print_event_state (event->scroll.state);
931 break;
932 case GDK_WINDOW_STATE:
933 g_print ("%s: %s",
934 _gdk_win32_window_state_to_string (event->window_state.changed_mask),
935 _gdk_win32_window_state_to_string (event->window_state.new_window_state));
936 case GDK_SETTING:
937 g_print ("%s: %s",
938 (event->setting.action == GDK_SETTING_ACTION_NEW ? "NEW" :
939 (event->setting.action == GDK_SETTING_ACTION_CHANGED ? "CHANGED" :
940 (event->setting.action == GDK_SETTING_ACTION_DELETED ? "DELETED" :
941 "???"))),
942 (event->setting.name ? event->setting.name : "NULL"));
943 case GDK_GRAB_BROKEN:
944 g_print ("%s %s %p",
945 (event->grab_broken.keyboard ? "KEYBOARD" : "POINTER"),
946 (event->grab_broken.implicit ? "IMPLICIT" : "EXPLICIT"),
947 (event->grab_broken.grab_window ? GDK_WINDOW_HWND (event->grab_broken.grab_window) : 0));
948 default:
949 /* Nothing */
950 break;
951 }
952 g_print ("%s", (debug_indent == 0 ? "\n" : ""));
953 }
954
955 static char *
956 decode_key_lparam (LPARAM lParam)
957 {
958 static char buf[100];
959 char *p = buf;
960
961 if (HIWORD (lParam) & KF_UP)
962 p += g_sprintf (p, "KF_UP ");
963 if (HIWORD (lParam) & KF_REPEAT)
964 p += g_sprintf (p, "KF_REPEAT ");
965 if (HIWORD (lParam) & KF_ALTDOWN)
966 p += g_sprintf (p, "KF_ALTDOWN ");
967 if (HIWORD (lParam) & KF_EXTENDED)
968 p += g_sprintf (p, "KF_EXTENDED ");
969 p += g_sprintf (p, "sc:%d rep:%d", LOBYTE (HIWORD (lParam)), LOWORD (lParam));
970
971 return buf;
972 }
973
974 #endif
975
976 static void
977 fixup_event (GdkEvent *event)
978 {
979 if (event->any.window)
980 g_object_ref (event->any.window);
981 if (((event->any.type == GDK_ENTER_NOTIFY) ||
982 (event->any.type == GDK_LEAVE_NOTIFY)) &&
983 (event->crossing.subwindow != NULL))
984 g_object_ref (event->crossing.subwindow);
985 event->any.send_event = InSendMessage ();
986 }
987
988 void
989 _gdk_win32_append_event (GdkEvent *event)
990 {
991 GList *link;
992
993 fixup_event (event);
994 #if 1
995 link = _gdk_event_queue_append (_gdk_display, event);
996 GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
997 /* event morphing, the passed in may not be valid afterwards */
998 _gdk_windowing_got_event (_gdk_display, link, event, 0);
999 #else
1000 _gdk_event_queue_append (_gdk_display, event);
1001 GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
1002 #endif
1003 }
1004
1005 static void
1006 fill_key_event_string (GdkEvent *event)
1007 {
1008 gunichar c;
1009 gchar buf[256];
1010
1011 /* Fill in event->string crudely, since various programs
1012 * depend on it.
1013 */
1014
1015 c = 0;
1016 if (event->key.keyval != GDK_VoidSymbol)
1017 c = gdk_keyval_to_unicode (event->key.keyval);
1018
1019 if (c)
1020 {
1021 gsize bytes_written;
1022 gint len;
1023
1024 /* Apply the control key - Taken from Xlib
1025 */
1026 if (event->key.state & GDK_CONTROL_MASK)
1027 {
1028 if ((c >= '@' && c < '\177') || c == ' ')
1029 c &= 0x1F;
1030 else if (c == '2')
1031 {
1032 event->key.string = g_memdup ("\0\0", 2);
1033 event->key.length = 1;
1034 return;
1035 }
1036 else if (c >= '3' && c <= '7')
1037 c -= ('3' - '\033');
1038 else if (c == '8')
1039 c = '\177';
1040 else if (c == '/')
1041 c = '_' & 0x1F;
1042 }
1043
1044 len = g_unichar_to_utf8 (c, buf);
1045 buf[len] = '\0';
1046
1047 event->key.string = g_locale_from_utf8 (buf, len,
1048 NULL, &bytes_written,
1049 NULL);
1050 if (event->key.string)
1051 event->key.length = bytes_written;
1052 }
1053 else if (event->key.keyval == GDK_Escape)
1054 {
1055 event->key.length = 1;
1056 event->key.string = g_strdup ("\033");
1057 }
1058 else if (event->key.keyval == GDK_Return ||
1059 event->key.keyval == GDK_KP_Enter)
1060 {
1061 event->key.length = 1;
1062 event->key.string = g_strdup ("\r");
1063 }
1064
1065 if (!event->key.string)
1066 {
1067 event->key.length = 0;
1068 event->key.string = g_strdup ("");
1069 }
1070 }
1071
1072 static GdkFilterReturn
1073 apply_event_filters (GdkWindow *window,
1074 MSG *msg,
1075 GList **filters)
1076 {
1077 GdkFilterReturn result = GDK_FILTER_CONTINUE;
1078 GdkEvent *event;
1079 GList *node;
1080 GList *tmp_list;
1081
1082 event = gdk_event_new (GDK_NOTHING);
1083 if (window != NULL)
1084 event->any.window = g_object_ref (window);
1085 ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
1086
1087 /* I think GdkFilterFunc semantics require the passed-in event
1088 * to already be in the queue. The filter func can generate
1089 * more events and append them after it if it likes.
1090 */
1091 node = _gdk_event_queue_append (_gdk_display, event);
1092
1093 tmp_list = *filters;
1094 while (tmp_list)
1095 {
1096 GdkEventFilter *filter = (GdkEventFilter *) tmp_list->data;
1097 GList *node;
1098
1099 if ((filter->flags & GDK_EVENT_FILTER_REMOVED) != 0)
1100 {
1101 tmp_list = tmp_list->next;
1102 continue;
1103 }
1104
1105 filter->ref_count++;
1106 result = filter->function (msg, event, filter->data);
1107
1108 /* get the next node after running the function since the
1109 function may add or remove a next node */
1110 node = tmp_list;
1111 tmp_list = tmp_list->next;
1112
1113 filter->ref_count--;
1114 if (filter->ref_count == 0)
1115 {
1116 *filters = g_list_remove_link (*filters, node);
1117 g_list_free_1 (node);
1118 g_free (filter);
1119 }
1120
1121 if (result != GDK_FILTER_CONTINUE)
1122 break;
1123 }
1124
1125 if (result == GDK_FILTER_CONTINUE || result == GDK_FILTER_REMOVE)
1126 {
1127 _gdk_event_queue_remove_link (_gdk_display, node);
1128 g_list_free_1 (node);
1129 gdk_event_free (event);
1130 }
1131 else /* GDK_FILTER_TRANSLATE */
1132 {
1133 ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
1134 fixup_event (event);
1135 GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
1136 }
1137 return result;
1138 }
1139
1140 /*
1141 * On Windows, transient windows will not have their own taskbar entries.
1142 * Because of this, we must hide and restore groups of transients in both
1143 * directions. That is, all transient children must be hidden or restored
1144 * with this window, but if this window's transient owner also has a
1145 * transient owner then this window's transient owner must be hidden/restored
1146 * with this one. And etc, up the chain until we hit an ancestor that has no
1147 * transient owner.
1148 *
1149 * It would be a good idea if applications don't chain transient windows
1150 * together. There's a limit to how much evil GTK can try to shield you
1151 * from.
1152 */
1153 static void
1154 show_window_recurse (GdkWindow *window, gboolean hide_window)
1155 {
1156 GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (GDK_WINDOW_OBJECT (window)->impl);
1157 GSList *children = impl->transient_children;
1158 GdkWindow *child = NULL;
1159
1160 if (!impl->changing_state)
1161 {
1162 impl->changing_state = TRUE;
1163
1164 if (children != NULL)
1165 {
1166 while (children != NULL)
1167 {
1168 child = children->data;
1169 show_window_recurse (child, hide_window);
1170
1171 children = g_slist_next (children);
1172 }
1173 }
1174
1175 if (GDK_WINDOW_IS_MAPPED (window))
1176 {
1177 if (!hide_window)
1178 {
1179 if (GDK_WINDOW_OBJECT (window)->state & GDK_WINDOW_STATE_ICONIFIED)
1180 {
1181 if (GDK_WINDOW_OBJECT (window)->state & GDK_WINDOW_STATE_MAXIMIZED)
1182 {
1183 ShowWindow (GDK_WINDOW_HWND (window), SW_SHOWMAXIMIZED);
1184 }
1185 else
1186 {
1187 ShowWindow (GDK_WINDOW_HWND (window), SW_RESTORE);
1188 }
1189 }
1190 }
1191 else
1192 {
1193 ShowWindow (GDK_WINDOW_HWND (window), SW_MINIMIZE);
1194 }
1195 }
1196
1197 impl->changing_state = FALSE;
1198 }
1199 }
1200
1201 static void
1202 do_show_window (GdkWindow *window, gboolean hide_window)
1203 {
1204 GdkWindow *tmp_window = NULL;
1205 GdkWindowImplWin32 *tmp_impl = GDK_WINDOW_IMPL_WIN32 (GDK_WINDOW_OBJECT (window)->impl);
1206
1207 if (!tmp_impl->changing_state)
1208 {
1209 /* Find the top-level window in our transient chain. */
1210 while (tmp_impl->transient_owner != NULL)
1211 {
1212 tmp_window = tmp_impl->transient_owner;
1213 tmp_impl = GDK_WINDOW_IMPL_WIN32 (GDK_WINDOW_OBJECT (tmp_window)->impl);
1214 }
1215
1216 /* If we couldn't find one, use the window provided. */
1217 if (tmp_window == NULL)
1218 {
1219 tmp_window = window;
1220 }
1221
1222 /* Recursively show/hide every window in the chain. */
1223 if (tmp_window != window)
1224 {
1225 show_window_recurse (tmp_window, hide_window);
1226 }
1227 }
1228 }
1229
1230 static void
1231 send_crossing_event (GdkDisplay *display,
1232 GdkWindowObject *window,
1233 GdkEventType type,
1234 GdkCrossingMode mode,
1235 GdkNotifyType notify_type,
1236 GdkWindow *subwindow,
1237 POINT *screen_pt,
1238 GdkModifierType mask,
1239 guint32 time_)
1240 {
1241 GdkEvent *event;
1242 GdkPointerGrabInfo *grab;
1243 POINT pt;
1244
1245 grab = _gdk_display_has_pointer_grab (display, 0);
1246
1247 if (grab != NULL &&
1248 !grab->owner_events &&
1249 mode != GDK_CROSSING_UNGRAB)
1250 {
1251 /* !owner_event => only report events wrt grab window, ignore rest */
1252 if ((GdkWindow *)window != grab->native_window)
1253 return;
1254 }
1255
1256 pt = *screen_pt;
1257 ScreenToClient (GDK_WINDOW_HWND (window), &pt);
1258
1259 event = gdk_event_new (type);
1260 event->crossing.window = (GdkWindow *)window;
1261 event->crossing.subwindow = subwindow;
1262 event->crossing.time = _gdk_win32_get_next_tick (time_);
1263 event->crossing.x = pt.x;
1264 event->crossing.y = pt.y;
1265 event->crossing.x_root = screen_pt->x + _gdk_offset_x;
1266 event->crossing.y_root = screen_pt->y + _gdk_offset_y;
1267 event->crossing.mode = mode;
1268 event->crossing.detail = notify_type;
1269 event->crossing.mode = mode;
1270 event->crossing.detail = notify_type;
1271 event->crossing.focus = FALSE;
1272 event->crossing.state = mask;
1273
1274 _gdk_win32_append_event (event);
1275
1276 /*
1277 if (((GdkWindowObject *) window)->extension_events != 0)
1278 _gdk_input_crossing_event ((GdkWindow *)window, type == GDK_ENTER_NOTIFY);
1279 */
1280 }
1281
1282 static GdkWindowObject *
1283 get_native_parent (GdkWindowObject *window)
1284 {
1285 if (window->parent != NULL)
1286 return window->parent->impl_window;
1287 return NULL;
1288 }
1289
1290
1291 static GdkWindowObject *
1292 find_common_ancestor (GdkWindowObject *win1,
1293 GdkWindowObject *win2)
1294 {
1295 GdkWindowObject *tmp;
1296 GList *path1 = NULL, *path2 = NULL;
1297 GList *list1, *list2;
1298
1299 tmp = win1;
1300 while (tmp != NULL && tmp->window_type != GDK_WINDOW_ROOT)
1301 {
1302 path1 = g_list_prepend (path1, tmp);
1303 tmp = get_native_parent (tmp);
1304 }
1305
1306 tmp = win2;
1307 while (tmp != NULL && tmp->window_type != GDK_WINDOW_ROOT)
1308 {
1309 path2 = g_list_prepend (path2, tmp);
1310 tmp = get_native_parent (tmp);
1311 }
1312
1313 list1 = path1;
1314 list2 = path2;
1315 tmp = NULL;
1316 while (list1 && list2 && (list1->data == list2->data))
1317 {
1318 tmp = (GdkWindowObject *)list1->data;
1319 list1 = g_list_next (list1);
1320 list2 = g_list_next (list2);
1321 }
1322 g_list_free (path1);
1323 g_list_free (path2);
1324
1325 return tmp;
1326 }
1327
1328 void
1329 synthesize_crossing_events (GdkDisplay *display,
1330 GdkWindow *src,
1331 GdkWindow *dest,
1332 GdkCrossingMode mode,
1333 POINT *screen_pt,
1334 GdkModifierType mask,
1335 guint32 time_,
1336 gboolean non_linear)
1337 {
1338 GdkWindowObject *c;
1339 GdkWindowObject *win, *last, *next;
1340 GList *path, *list;
1341 GdkWindowObject *a;
1342 GdkWindowObject *b;
1343 GdkNotifyType notify_type;
1344
1345 a = (GdkWindowObject *)src;
1346 b = (GdkWindowObject *)dest;
1347 if (a == b)
1348 return; /* No crossings generated between src and dest */
1349
1350 c = find_common_ancestor (a, b);
1351
1352 non_linear |= (c != a) && (c != b);
1353
1354 if (a) /* There might not be a source (i.e. if no previous pointer_in_window) */
1355 {
1356 /* Traverse up from a to (excluding) c sending leave events */
1357 if (non_linear)
1358 notify_type = GDK_NOTIFY_NONLINEAR;
1359 else if (c == a)
1360 notify_type = GDK_NOTIFY_INFERIOR;
1361 else
1362 notify_type = GDK_NOTIFY_ANCESTOR;
1363 send_crossing_event (display,
1364 a, GDK_LEAVE_NOTIFY,
1365 mode,
1366 notify_type,
1367 NULL,
1368 screen_pt,
1369 mask, time_);
1370
1371 if (c != a)
1372 {
1373 if (non_linear)
1374 notify_type = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1375 else
1376 notify_type = GDK_NOTIFY_VIRTUAL;
1377
1378 last = a;
1379 win = get_native_parent (a);
1380 while (win != c && win->window_type != GDK_WINDOW_ROOT)
1381 {
1382 send_crossing_event (display,
1383 win, GDK_LEAVE_NOTIFY,
1384 mode,
1385 notify_type,
1386 (GdkWindow *)last,
1387 screen_pt,
1388 mask, time_);
1389
1390 last = win;
1391 win = get_native_parent (win);
1392 }
1393 }
1394 }
1395
1396 if (b) /* Might not be a dest, e.g. if we're moving out of the window */
1397 {
1398 /* Traverse down from c to b */
1399 if (c != b)
1400 {
1401 path = NULL;
1402 win = get_native_parent (b);
1403 while (win != c && win->window_type != GDK_WINDOW_ROOT)
1404 {
1405 path = g_list_prepend (path, win);
1406 win = get_native_parent (win);
1407 }
1408
1409 if (non_linear)
1410 notify_type = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1411 else
1412 notify_type = GDK_NOTIFY_VIRTUAL;
1413
1414 list = path;
1415 while (list)
1416 {
1417 win = (GdkWindowObject *)list->data;
1418 list = g_list_next (list);
1419 if (list)
1420 next = (GdkWindowObject *)list->data;
1421 else
1422 next = b;
1423
1424 send_crossing_event (display,
1425 win, GDK_ENTER_NOTIFY,
1426 mode,
1427 notify_type,
1428 (GdkWindow *)next,
1429 screen_pt,
1430 mask, time_);
1431 }
1432 g_list_free (path);
1433 }
1434
1435
1436 if (non_linear)
1437 notify_type = GDK_NOTIFY_NONLINEAR;
1438 else if (c == a)
1439 notify_type = GDK_NOTIFY_ANCESTOR;
1440 else
1441 notify_type = GDK_NOTIFY_INFERIOR;
1442
1443 send_crossing_event (display,
1444 b, GDK_ENTER_NOTIFY,
1445 mode,
1446 notify_type,
1447 NULL,
1448 screen_pt,
1449 mask, time_);
1450 }
1451 }
1452
1453 static void
1454 synthesize_expose_events (GdkWindow *window)
1455 {
1456 RECT r;
1457 HDC hdc;
1458 GdkDrawableImplWin32 *impl = GDK_DRAWABLE_IMPL_WIN32 (((GdkWindowObject *) window)->impl);
1459 GList *list = gdk_window_get_children (window);
1460 GList *head = list;
1461 GdkEvent *event;
1462 int k;
1463
1464 while (list)
1465 {
1466 synthesize_expose_events ((GdkWindow *) list->data);
1467 list = list->next;
1468 }
1469
1470 g_list_free (head);
1471
1472 if (((GdkWindowObject *) window)->input_only)
1473 ;
1474 else if (!(hdc = GetDC (impl->handle)))
1475 WIN32_GDI_FAILED ("GetDC");
1476 else
1477 {
1478 if ((k = GetClipBox (hdc, &r)) == ERROR)
1479 WIN32_GDI_FAILED ("GetClipBox");
1480 else if (k != NULLREGION)
1481 {
1482 event = gdk_event_new (GDK_EXPOSE);
1483 event->expose.window = window;
1484 event->expose.area.x = r.left;
1485 event->expose.area.y = r.top;
1486 event->expose.area.width = r.right - r.left;
1487 event->expose.area.height = r.bottom - r.top;
1488 event->expose.region = gdk_region_rectangle (&(event->expose.area));
1489 event->expose.count = 0;
1490
1491 _gdk_win32_append_event (event);
1492 }
1493 GDI_CALL (ReleaseDC, (impl->handle, hdc));
1494 }
1495 }
1496
1497 static void
1498 update_colors (GdkWindow *window,
1499 gboolean top)
1500 {
1501 HDC hdc;
1502 GdkDrawableImplWin32 *impl = GDK_DRAWABLE_IMPL_WIN32 (((GdkWindowObject *) window)->impl);
1503 GList *list = gdk_window_get_children (window);
1504 GList *head = list;
1505
1506 GDK_NOTE (COLORMAP, (top ? g_print ("update_colors:") : (void) 0));
1507
1508 while (list)
1509 {
1510 update_colors ((GdkWindow *) list->data, FALSE);
1511 list = list->next;
1512 }
1513 g_list_free (head);
1514
1515 if (((GdkWindowObject *) window)->input_only ||
1516 impl->colormap == NULL)
1517 return;
1518
1519 if (!(hdc = GetDC (impl->handle)))
1520 WIN32_GDI_FAILED ("GetDC");
1521 else
1522 {
1523 GdkColormapPrivateWin32 *cmapp = GDK_WIN32_COLORMAP_DATA (impl->colormap);
1524 HPALETTE holdpal;
1525 gint k;
1526
1527 if ((holdpal = SelectPalette (hdc, cmapp->hpal, TRUE)) == NULL)
1528 WIN32_GDI_FAILED ("SelectPalette");
1529 else if ((k = RealizePalette (hdc)) == GDI_ERROR)
1530 WIN32_GDI_FAILED ("RealizePalette");
1531 else
1532 {
1533 GDK_NOTE (COLORMAP,
1534 (k > 0 ?
1535 g_print (" %p pal=%p: realized %d colors\n"
1536 "update_colors:",
1537 impl->handle, cmapp->hpal, k) :
1538 (void) 0,
1539 g_print (" %p", impl->handle)));
1540 GDI_CALL (UpdateColors, (hdc));
1541 SelectPalette (hdc, holdpal, TRUE);
1542 RealizePalette (hdc);
1543 }
1544 GDI_CALL (ReleaseDC, (impl->handle, hdc));
1545 }
1546 GDK_NOTE (COLORMAP, (top ? g_print ("\n") : (void) 0));
1547 }
1548
1549 /* The check_extended flag controls whether to check if the windows want
1550 * events from extended input devices and if the message should be skipped
1551 * because an extended input device is active
1552 */
1553 static gboolean
1554 propagate (GdkWindow **window,
1555 MSG *msg,
1556 GdkWindow *grab_window,
1557 gboolean grab_owner_events,
1558 gint grab_mask,
1559 gboolean (*doesnt_want_it) (gint mask,
1560 MSG *msg))
1561 {
1562 if (grab_window != NULL && !grab_owner_events)
1563 {
1564 /* Event source is grabbed with owner_events FALSE */
1565
1566 /* See if the event should be ignored because an extended input
1567 * device is used
1568 */
1569 if ((*doesnt_want_it) (grab_mask, msg))
1570 {
1571 GDK_NOTE (EVENTS, g_print (" (grabber doesn't want it)"));
1572 return FALSE;
1573 }
1574 else
1575 {
1576 GDK_NOTE (EVENTS, g_print (" (to grabber)"));
1577 assign_object (window, grab_window);
1578 return TRUE;
1579 }
1580 }
1581
1582 /* If we come here, we know that if grab_window != NULL then
1583 * grab_owner_events is TRUE
1584 */
1585 while (TRUE)
1586 {
1587 if ((*doesnt_want_it) (((GdkWindowObject *) *window)->event_mask, msg))
1588 {
1589 /* Owner doesn't want it, propagate to parent. */
1590 GdkWindow *parent = gdk_window_get_parent (*window);
1591 if (parent == _gdk_root || parent == NULL)
1592 {
1593 /* No parent; check if grabbed */
1594 if (grab_window != NULL)
1595 {
1596 /* Event source is grabbed with owner_events TRUE */
1597
1598 if ((*doesnt_want_it) (grab_mask, msg))
1599 {
1600 /* Grabber doesn't want it either */
1601 GDK_NOTE (EVENTS, g_print (" (grabber doesn't want it)"));
1602 return FALSE;
1603 }
1604 else
1605 {
1606 /* Grabbed! */
1607 GDK_NOTE (EVENTS, g_print (" (to grabber)"));
1608 assign_object (window, grab_window);
1609 return TRUE;
1610 }
1611 }
1612 else
1613 {
1614 GDK_NOTE (EVENTS, g_print (" (undelivered)"));
1615 return FALSE;
1616 }
1617 }
1618 else
1619 {
1620 assign_object (window, parent);
1621 /* The only branch where we actually continue the loop */
1622 }
1623 }
1624 else
1625 return TRUE;
1626 }
1627 }
1628
1629 static gboolean
1630 doesnt_want_key (gint mask,
1631 MSG *msg)
1632 {
1633 return (((msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP) &&
1634 !(mask & GDK_KEY_RELEASE_MASK)) ||
1635 ((msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN) &&
1636 !(mask & GDK_KEY_PRESS_MASK)));
1637 }
1638
1639 static gboolean
1640 doesnt_want_char (gint mask,
1641 MSG *msg)
1642 {
1643 return !(mask & (GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK));
1644 }
1645
1646 void
1647 _gdk_win32_emit_configure_event (GdkWindow *window)
1648 {
1649 GdkWindowImplWin32 *window_impl;
1650 RECT client_rect;
1651 POINT point;
1652 GdkWindowObject *window_object;
1653 HWND hwnd;
1654
1655 window_object = GDK_WINDOW_OBJECT (window);
1656
1657 window_impl = GDK_WINDOW_IMPL_WIN32 (window_object->impl);
1658 if (window_impl->inhibit_configure)
1659 return;
1660
1661 hwnd = GDK_WINDOW_HWND (window);
1662
1663 GetClientRect (hwnd, &client_rect);
1664 point.x = client_rect.left; /* always 0 */
1665 point.y = client_rect.top;
1666
1667 /* top level windows need screen coords */
1668 if (gdk_window_get_parent (window) == _gdk_root)
1669 {
1670 ClientToScreen (hwnd, &point);
1671 point.x += _gdk_offset_x;
1672 point.y += _gdk_offset_y;
1673 }
1674
1675 window_object->width = client_rect.right - client_rect.left;
1676 window_object->height = client_rect.bottom - client_rect.top;
1677
1678 window_object->x = point.x;
1679 window_object->y = point.y;
1680
1681 _gdk_window_update_size (window);
1682
1683 if (window_object->event_mask & GDK_STRUCTURE_MASK)
1684 {
1685 GdkEvent *event = gdk_event_new (GDK_CONFIGURE);
1686
1687 event->configure.window = window;
1688
1689 event->configure.width = client_rect.right - client_rect.left;
1690 event->configure.height = client_rect.bottom - client_rect.top;
1691
1692 event->configure.x = point.x;
1693 event->configure.y = point.y;
1694
1695 _gdk_win32_append_event (event);
1696 }
1697 }
1698
1699 GdkRegion *
1700 _gdk_win32_hrgn_to_region (HRGN hrgn)
1701 {
1702 RGNDATA *rgndata;
1703 RECT *rects;
1704 GdkRegion *result;
1705 gint nbytes;
1706 guint i;
1707
1708 if ((nbytes = GetRegionData (hrgn, 0, NULL)) == 0)
1709 {
1710 WIN32_GDI_FAILED ("GetRegionData");
1711 return NULL;
1712 }
1713
1714 rgndata = (RGNDATA *) g_malloc (nbytes);
1715
1716 if (GetRegionData (hrgn, nbytes, rgndata) == 0)
1717 {
1718 WIN32_GDI_FAILED ("GetRegionData");
1719 g_free (rgndata);
1720 return NULL;
1721 }
1722
1723 result = gdk_region_new ();
1724 rects = (RECT *) rgndata->Buffer;
1725 for (i = 0; i < rgndata->rdh.nCount; i++)
1726 {
1727 GdkRectangle r;
1728
1729 r.x = rects[i].left;
1730 r.y = rects[i].top;
1731 r.width = rects[i].right - r.x;
1732 r.height = rects[i].bottom - r.y;
1733
1734 gdk_region_union_with_rect (result, &r);
1735 }
1736
1737 g_free (rgndata);
1738
1739 return result;
1740 }
1741
1742 static void
1743 adjust_drag (LONG *drag,
1744 LONG curr,
1745 gint inc)
1746 {
1747 if (*drag > curr)
1748 *drag = curr + ((*drag + inc/2 - curr) / inc) * inc;
1749 else
1750 *drag = curr - ((curr - *drag + inc/2) / inc) * inc;
1751 }
1752
1753 static void
1754 handle_wm_paint (MSG *msg,
1755 GdkWindow *window,
1756 gboolean return_exposes,
1757 GdkEvent **event)
1758 {
1759 HRGN hrgn = CreateRectRgn (0, 0, 0, 0);
1760 HDC hdc;
1761 PAINTSTRUCT paintstruct;
1762 GdkRegion *update_region;
1763
1764 if (GetUpdateRgn (msg->hwnd, hrgn, FALSE) == ERROR)
1765 {
1766 WIN32_GDI_FAILED ("GetUpdateRgn");
1767 DeleteObject (hrgn);
1768 return;
1769 }
1770
1771 hdc = BeginPaint (msg->hwnd, &paintstruct);
1772
1773 GDK_NOTE (EVENTS, g_print (" %s %s dc %p%s",
1774 _gdk_win32_rect_to_string (&paintstruct.rcPaint),
1775 (paintstruct.fErase ? "erase" : ""),
1776 hdc,
1777 (return_exposes ? " return_exposes" : "")));
1778
1779 EndPaint (msg->hwnd, &paintstruct);
1780
1781 if ((paintstruct.rcPaint.right == paintstruct.rcPaint.left) ||
1782 (paintstruct.rcPaint.bottom == paintstruct.rcPaint.top))
1783 {
1784 GDK_NOTE (EVENTS, g_print (" (empty paintstruct, ignored)"));
1785 DeleteObject (hrgn);
1786 return;
1787 }
1788
1789 if (return_exposes)
1790 {
1791 if (!GDK_WINDOW_DESTROYED (window))
1792 {
1793 GList *list = _gdk_display->queued_events;
1794
1795 *event = gdk_event_new (GDK_EXPOSE);
1796 (*event)->expose.window = window;
1797 (*event)->expose.area.x = paintstruct.rcPaint.left;
1798 (*event)->expose.area.y = paintstruct.rcPaint.top;
1799 (*event)->expose.area.width = paintstruct.rcPaint.right - paintstruct.rcPaint.left;
1800 (*event)->expose.area.height = paintstruct.rcPaint.bottom - paintstruct.rcPaint.top;
1801 (*event)->expose.region = _gdk_win32_hrgn_to_region (hrgn);
1802 (*event)->expose.count = 0;
1803
1804 while (list != NULL)
1805 {
1806 GdkEventPrivate *evp = list->data;
1807
1808 if (evp->event.any.type == GDK_EXPOSE &&
1809 evp->event.any.window == window &&
1810 !(evp->flags & GDK_EVENT_PENDING))
1811 evp->event.expose.count++;
1812
1813 list = list->next;
1814 }
1815 }
1816
1817 DeleteObject (hrgn);
1818 return;
1819 }
1820
1821 update_region = _gdk_win32_hrgn_to_region (hrgn);
1822 if (!gdk_region_empty (update_region))
1823 _gdk_window_invalidate_for_expose (window, update_region);
1824 gdk_region_destroy (update_region);
1825
1826 DeleteObject (hrgn);
1827 }
1828
1829 static VOID CALLBACK
1830 modal_timer_proc (HWND hwnd,
1831 UINT msg,
1832 UINT_PTR id,
1833 DWORD time)
1834 {
1835 int arbitrary_limit = 10;
1836
1837 while (_modal_operation_in_progress &&
1838 g_main_context_pending (NULL) &&
1839 arbitrary_limit--)
1840 g_main_context_iteration (NULL, FALSE);
1841 }
1842
1843 void
1844 _gdk_win32_begin_modal_call (void)
1845 {
1846 g_assert (!_modal_operation_in_progress);
1847
1848 _modal_operation_in_progress = TRUE;
1849
1850 modal_timer = SetTimer (NULL, 0, 10, modal_timer_proc);
1851 if (modal_timer == 0)
1852 WIN32_API_FAILED ("SetTimer");
1853 }
1854
1855 void
1856 _gdk_win32_end_modal_call (void)
1857 {
1858 g_assert (_modal_operation_in_progress);
1859
1860 _modal_operation_in_progress = FALSE;
1861
1862 if (modal_timer != 0)
1863 {
1864 API_CALL (KillTimer, (NULL, modal_timer));
1865 modal_timer = 0;
1866 }
1867 }
1868
1869 static VOID CALLBACK
1870 sync_timer_proc (HWND hwnd,
1871 UINT msg,
1872 UINT_PTR id,
1873 DWORD time)
1874 {
1875 MSG message;
1876 if (PeekMessageW (&message, hwnd, WM_PAINT, WM_PAINT, PM_REMOVE))
1877 {
1878 return;
1879 }
1880
1881 RedrawWindow (hwnd, NULL, NULL, RDW_INVALIDATE|RDW_UPDATENOW|RDW_ALLCHILDREN);
1882
1883 KillTimer (hwnd, sync_timer);
1884 }
1885
1886 static void
1887 handle_display_change (void)
1888 {
1889 _gdk_monitor_init ();
1890 _gdk_root_window_size_init ();
1891 g_signal_emit_by_name (_gdk_screen, "size_changed");
1892 }
1893
1894 static void
1895 generate_button_event (GdkEventType type,
1896 gint button,
1897 GdkWindow *window,
1898 MSG *msg)
1899 {
1900 GdkEvent *event = gdk_event_new (type);
1901
1902 event->button.window = window;
1903 event->button.time = _gdk_win32_get_next_tick (msg->time);
1904 event->button.x = current_x = (gint16) GET_X_LPARAM (msg->lParam);
1905 event->button.y = current_y = (gint16) GET_Y_LPARAM (msg->lParam);
1906 event->button.x_root = msg->pt.x + _gdk_offset_x;
1907 event->button.y_root = msg->pt.y + _gdk_offset_y;
1908 event->button.axes = NULL;
1909 event->button.state = build_pointer_event_state (msg);
1910 event->button.button = button;
1911 event->button.device = _gdk_display->core_pointer;
1912
1913 _gdk_win32_append_event (event);
1914 }
1915
1916 static void
1917 ensure_stacking_on_unminimize (MSG *msg)
1918 {
1919 HWND rover;
1920 HWND lowest_transient = NULL;
1921
1922 rover = msg->hwnd;
1923 while ((rover = GetNextWindow (rover, GW_HWNDNEXT)))
1924 {
1925 GdkWindow *rover_gdkw = gdk_win32_handle_table_lookup (rover);
1926
1927 /* Checking window group not implemented yet */
1928 if (rover_gdkw)
1929 {
1930 GdkWindowImplWin32 *rover_impl =
1931 (GdkWindowImplWin32 *)((GdkWindowObject *)rover_gdkw)->impl;
1932
1933 if (GDK_WINDOW_IS_MAPPED (rover_gdkw) &&
1934 (rover_impl->type_hint == GDK_WINDOW_TYPE_HINT_UTILITY ||
1935 rover_impl->type_hint == GDK_WINDOW_TYPE_HINT_DIALOG ||
1936 rover_impl->transient_owner != NULL))
1937 {
1938 lowest_transient = rover;
1939 }
1940 }
1941 }
1942 if (lowest_transient != NULL)
1943 {
1944 GDK_NOTE (EVENTS, g_print (" restacking: %p", lowest_transient));
1945 SetWindowPos (msg->hwnd, lowest_transient, 0, 0, 0, 0,
1946 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
1947 }
1948 }
1949
1950 static gboolean
1951 ensure_stacking_on_window_pos_changing (MSG *msg,
1952 GdkWindow *window)
1953 {
1954 GdkWindowImplWin32 *impl = (GdkWindowImplWin32 *)((GdkWindowObject *) window)->impl;
1955 WINDOWPOS *windowpos = (WINDOWPOS *) msg->lParam;
1956
1957 if (GetActiveWindow () == msg->hwnd &&
1958 impl->type_hint != GDK_WINDOW_TYPE_HINT_UTILITY &&
1959 impl->type_hint != GDK_WINDOW_TYPE_HINT_DIALOG &&
1960 impl->transient_owner == NULL)
1961 {
1962 /* Make sure the window stays behind any transient-type windows
1963 * of the same window group.
1964 *
1965 * If the window is not active and being activated, we let
1966 * Windows bring it to the top and rely on the WM_ACTIVATEAPP
1967 * handling to bring any utility windows on top of it.
1968 */
1969 HWND rover;
1970 gboolean restacking;
1971
1972 rover = windowpos->hwndInsertAfter;
1973 restacking = FALSE;
1974 while (rover)
1975 {
1976 GdkWindow *rover_gdkw = gdk_win32_handle_table_lookup (rover);
1977
1978 /* Checking window group not implemented yet */
1979 if (rover_gdkw)
1980 {
1981 GdkWindowImplWin32 *rover_impl =
1982 (GdkWindowImplWin32 *)((GdkWindowObject *)rover_gdkw)->impl;
1983
1984 if (GDK_WINDOW_IS_MAPPED (rover_gdkw) &&
1985 (rover_impl->type_hint == GDK_WINDOW_TYPE_HINT_UTILITY ||
1986 rover_impl->type_hint == GDK_WINDOW_TYPE_HINT_DIALOG ||
1987 rover_impl->transient_owner != NULL))
1988 {
1989 restacking = TRUE;
1990 windowpos->hwndInsertAfter = rover;
1991 }
1992 }
1993 rover = GetNextWindow (rover, GW_HWNDNEXT);
1994 }
1995
1996 if (restacking)
1997 {
1998 GDK_NOTE (EVENTS, g_print (" restacking: %p", windowpos->hwndInsertAfter));
1999 return TRUE;
2000 }
2001 }
2002 return FALSE;
2003 }
2004
2005 static void
2006 ensure_stacking_on_activate_app (MSG *msg,
2007 GdkWindow *window)
2008 {
2009 GdkWindowImplWin32 *impl = (GdkWindowImplWin32 *)((GdkWindowObject *) window)->impl;
2010
2011 if (impl->type_hint == GDK_WINDOW_TYPE_HINT_UTILITY ||
2012 impl->type_hint == GDK_WINDOW_TYPE_HINT_DIALOG ||
2013 impl->transient_owner != NULL)
2014 {
2015 SetWindowPos (msg->hwnd, HWND_TOP, 0, 0, 0, 0,
2016 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
2017 return;
2018 }
2019
2020 if (IsWindowVisible (msg->hwnd) &&
2021 msg->hwnd == GetActiveWindow ())
2022 {
2023 /* This window is not a transient-type window and it is the
2024 * activated window. Make sure this window is as visible as
2025 * possible, just below the lowest transient-type window of this
2026 * app.
2027 */
2028 HWND rover;
2029
2030 rover = msg->hwnd;
2031 while ((rover = GetNextWindow (rover, GW_HWNDPREV)))
2032 {
2033 GdkWindow *rover_gdkw = gdk_win32_handle_table_lookup (rover);
2034
2035 /* Checking window group not implemented yet */
2036 if (rover_gdkw)
2037 {
2038 GdkWindowImplWin32 *rover_impl =
2039 (GdkWindowImplWin32 *)((GdkWindowObject *)rover_gdkw)->impl;
2040
2041 if (GDK_WINDOW_IS_MAPPED (rover_gdkw) &&
2042 (rover_impl->type_hint == GDK_WINDOW_TYPE_HINT_UTILITY ||
2043 rover_impl->type_hint == GDK_WINDOW_TYPE_HINT_DIALOG ||
2044 rover_impl->transient_owner != NULL))
2045 {
2046 GDK_NOTE (EVENTS, g_print (" restacking: %p", rover));
2047 SetWindowPos (msg->hwnd, rover, 0, 0, 0, 0,
2048 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
2049 break;
2050 }
2051 }
2052 }
2053 }
2054 }
2055
2056 #define GDK_ANY_BUTTON_MASK (GDK_BUTTON1_MASK | \
2057 GDK_BUTTON2_MASK | \
2058 GDK_BUTTON3_MASK | \
2059 GDK_BUTTON4_MASK | \
2060 GDK_BUTTON5_MASK)
2061
2062 static gboolean
2063 gdk_event_translate (MSG *msg,
2064 gint *ret_valp)
2065 {
2066 RECT rect, *drag, orig_drag;
2067 POINT point;
2068 MINMAXINFO *mmi;
2069 HWND hwnd;
2070 HCURSOR hcursor;
2071 BYTE key_state[256];
2072 HIMC himc;
2073 WINDOWPOS *windowpos;
2074 gboolean ignore_leave;
2075
2076 GdkEvent *event;
2077
2078 wchar_t wbuf[100];
2079 gint ccount;
2080
2081 GdkWindow *window = NULL;
2082 GdkWindowImplWin32 *impl;
2083
2084 GdkWindow *orig_window, *new_window;
2085
2086 GdkPointerGrabInfo *grab = NULL;
2087 GdkWindow *grab_window = NULL;
2088
2089 static gint update_colors_counter = 0;
2090 gint button;
2091 GdkAtom target;
2092
2093 gchar buf[256];
2094 gboolean return_val = FALSE;
2095
2096 int i;
2097
2098 if (_gdk_default_filters)
2099 {
2100 /* Apply global filters */
2101
2102 GdkFilterReturn result = apply_event_filters (NULL, msg, &_gdk_default_filters);
2103
2104 /* If result is GDK_FILTER_CONTINUE, we continue as if nothing
2105 * happened. If it is GDK_FILTER_REMOVE or GDK_FILTER_TRANSLATE,
2106 * we return TRUE, and DefWindowProcW() will not be called.
2107 */
2108 if (result == GDK_FILTER_REMOVE || result == GDK_FILTER_TRANSLATE)
2109 return TRUE;
2110 }
2111
2112 window = gdk_win32_handle_table_lookup ((GdkNativeWindow) msg->hwnd);
2113 orig_window = window;
2114
2115 if (window == NULL)
2116 {
2117 /* XXX Handle WM_QUIT here ? */
2118 if (msg->message == WM_QUIT)
2119 {
2120 GDK_NOTE (EVENTS, g_print (" %d", (int) msg->wParam));
2121 exit (msg->wParam);
2122 }
2123 else if (msg->message == WM_CREATE)
2124 {
2125 window = (UNALIGNED GdkWindow*) (((LPCREATESTRUCTW) msg->lParam)->lpCreateParams);
2126 GDK_WINDOW_HWND (window) = msg->hwnd;
2127 }
2128 else
2129 {
2130 GDK_NOTE (EVENTS, g_print (" (no GdkWindow)"));
2131 }
2132 return FALSE;
2133 }
2134
2135 g_object_ref (window);
2136
2137 /* window's refcount has now been increased, so code below should
2138 * not just return from this function, but instead goto done (or
2139 * break out of the big switch). To protect against forgetting this,
2140 * #define return to a syntax error...
2141 */
2142 #define return GOTO_DONE_INSTEAD
2143
2144 if (!GDK_WINDOW_DESTROYED (window) && ((GdkWindowObject *) window)->filters)
2145 {
2146 /* Apply per-window filters */
2147
2148 GdkFilterReturn result = apply_event_filters (window, msg, &((GdkWindowObject *) window)->filters);
2149
2150 if (result == GDK_FILTER_REMOVE || result == GDK_FILTER_TRANSLATE)
2151 {
2152 return_val = TRUE;
2153 goto done;
2154 }
2155 }
2156
2157 if (msg->message == client_message)
2158 {
2159 GList *tmp_list;
2160 GdkFilterReturn result = GDK_FILTER_CONTINUE;
2161 GList *node;
2162
2163 GDK_NOTE (EVENTS, g_print (" client_message"));
2164
2165 event = gdk_event_new (GDK_NOTHING);
2166 ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
2167
2168 node = _gdk_event_queue_append (_gdk_display, event);
2169
2170 tmp_list = client_filters;
2171 while (tmp_list)
2172 {
2173 GdkClientFilter *filter = tmp_list->data;
2174
2175 tmp_list = tmp_list->next;
2176
2177 if (filter->type == GDK_POINTER_TO_ATOM (msg->wParam))
2178 {
2179 GDK_NOTE (EVENTS, g_print (" (match)"));
2180
2181 result = (*filter->function) (msg, event, filter->data);
2182
2183 if (result != GDK_FILTER_CONTINUE)
2184 break;
2185 }
2186 }
2187
2188 switch (result)
2189 {
2190 case GDK_FILTER_REMOVE:
2191 _gdk_event_queue_remove_link (_gdk_display, node);
2192 g_list_free_1 (node);
2193 gdk_event_free (event);
2194 return_val = TRUE;
2195 goto done;
2196
2197 case GDK_FILTER_TRANSLATE:
2198 ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
2199 GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
2200 return_val = TRUE;
2201 goto done;
2202
2203 case GDK_FILTER_CONTINUE:
2204 /* Send unknown client messages on to Gtk for it to use */
2205
2206 event->client.type = GDK_CLIENT_EVENT;
2207 event->client.window = window;
2208 event->client.message_type = GDK_POINTER_TO_ATOM (msg->wParam);
2209 event->client.data_format = 32;
2210 event->client.data.l[0] = msg->lParam;
2211 for (i = 1; i < 5; i++)
2212 event->client.data.l[i] = 0;
2213 GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
2214 return_val = TRUE;
2215 goto done;
2216 }
2217 }
2218
2219 switch (msg->message)
2220 {
2221 case WM_INPUTLANGCHANGE:
2222 _gdk_input_locale = (HKL) msg->lParam;
2223 _gdk_win32_keymap_set_active_layout (GDK_WIN32_KEYMAP (gdk_keymap_get_default ()), _gdk_input_locale);
2224 _gdk_input_locale_is_ime = ImmIsIME (_gdk_input_locale);
2225 GetLocaleInfo (MAKELCID (LOWORD (_gdk_input_locale), SORT_DEFAULT),
2226 LOCALE_IDEFAULTANSICODEPAGE,
2227 buf, sizeof (buf));
2228 _gdk_input_codepage = atoi (buf);
2229 _gdk_keymap_serial++;
2230 GDK_NOTE (EVENTS,
2231 g_print (" cs:%lu hkl:%p%s cp:%d",
2232 (gulong) msg->wParam,
2233 (gpointer) msg->lParam, _gdk_input_locale_is_ime ? " (IME)" : "",
2234 _gdk_input_codepage));
2235 break;
2236
2237 case WM_SYSKEYUP:
2238 case WM_SYSKEYDOWN:
2239 GDK_NOTE (EVENTS,
2240 g_print (" %s ch:%.02x %s",
2241 _gdk_win32_key_to_string (msg->lParam),
2242 (int) msg->wParam,
2243 decode_key_lparam (msg->lParam)));
2244
2245 /* If posted without us having keyboard focus, ignore */
2246 if ((msg->wParam != VK_F10 && msg->wParam != VK_MENU) &&
2247 !(HIWORD (msg->lParam) & KF_ALTDOWN))
2248 break;
2249
2250 /* Let the system handle Alt-Tab, Alt-Space and Alt-F4 unless
2251 * the keyboard is grabbed.
2252 */
2253 if (_gdk_display->keyboard_grab.window == NULL &&
2254 (msg->wParam == VK_TAB ||
2255 msg->wParam == VK_SPACE ||
2256 msg->wParam == VK_F4))
2257 break;
2258
2259 /* Jump to code in common with WM_KEYUP and WM_KEYDOWN */
2260 goto keyup_or_down;
2261
2262 case WM_KEYUP:
2263 case WM_KEYDOWN:
2264 GDK_NOTE (EVENTS,
2265 g_print (" %s ch:%.02x %s",
2266 _gdk_win32_key_to_string (msg->lParam),
2267 (int) msg->wParam,
2268 decode_key_lparam (msg->lParam)));
2269
2270 keyup_or_down:
2271
2272 /* Ignore key messages intended for the IME */
2273 if (msg->wParam == VK_PROCESSKEY ||
2274 in_ime_composition)
2275 break;
2276
2277 /* Ignore autorepeats on modifiers */
2278 if (msg->message == WM_KEYDOWN &&
2279 (msg->wParam == VK_MENU ||
2280 msg->wParam == VK_CONTROL ||
2281 msg->wParam == VK_SHIFT) &&
2282 ((HIWORD(msg->lParam) & KF_REPEAT) >= 1))
2283 break;
2284
2285 if (!propagate (&window, msg,
2286 _gdk_display->keyboard_grab.window,
2287 _gdk_display->keyboard_grab.owner_events,
2288 GDK_ALL_EVENTS_MASK,
2289 doesnt_want_key))
2290 break;
2291
2292 if (GDK_WINDOW_DESTROYED (window))
2293 break;
2294
2295 impl = GDK_WINDOW_IMPL_WIN32 (GDK_WINDOW_OBJECT (window)->impl);
2296
2297 API_CALL (GetKeyboardState, (key_state));
2298
2299 ccount = 0;
2300
2301 if (msg->wParam == VK_PACKET)
2302 {
2303 ccount = ToUnicode (VK_PACKET, HIWORD (msg->lParam), key_state, wbuf, 1, 0);
2304 if (ccount == 1)
2305 {
2306 if (wbuf[0] >= 0xD800 && wbuf[0] < 0xDC00)
2307 {
2308 if (msg->message == WM_KEYDOWN)
2309 impl->leading_surrogate_keydown = wbuf[0];
2310 else
2311 impl->leading_surrogate_keyup = wbuf[0];
2312
2313 /* don't emit an event */
2314 return_val = TRUE;
2315 break;
2316 }
2317 else
2318 {
2319 /* wait until an event is created */;
2320 }
2321 }
2322 }
2323
2324 event = gdk_event_new ((msg->message == WM_KEYDOWN ||
2325 msg->message == WM_SYSKEYDOWN) ?
2326 GDK_KEY_PRESS : GDK_KEY_RELEASE);
2327 event->key.window = window;
2328 event->key.time = _gdk_win32_get_next_tick (msg->time);
2329 event->key.keyval = GDK_VoidSymbol;
2330 event->key.string = NULL;
2331 event->key.length = 0;
2332 event->key.hardware_keycode = msg->wParam;
2333 if (HIWORD (msg->lParam) & KF_EXTENDED)
2334 {
2335 switch (msg->wParam)
2336 {
2337 case VK_CONTROL:
2338 event->key.hardware_keycode = VK_RCONTROL;
2339 break;
2340 case VK_SHIFT: /* Actually, KF_EXTENDED is not set
2341 * for the right shift key.
2342 */
2343 event->key.hardware_keycode = VK_RSHIFT;
2344 break;
2345 case VK_MENU:
2346 event->key.hardware_keycode = VK_RMENU;
2347 break;
2348 }
2349 }
2350 else if (msg->wParam == VK_SHIFT &&
2351 LOBYTE (HIWORD (msg->lParam)) == _gdk_win32_keymap_get_rshift_scancode (GDK_WIN32_KEYMAP (gdk_keymap_get_default ())))
2352 event->key.hardware_keycode = VK_RSHIFT;
2353
2354 /* 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]); */
2355
2356 build_key_event_state (event, key_state);
2357
2358 if (msg->wParam == VK_PACKET && ccount == 1)
2359 {
2360 if (wbuf[0] >= 0xD800 && wbuf[0] < 0xDC00)
2361 {
2362 g_assert_not_reached ();
2363 }
2364 else if (wbuf[0] >= 0xDC00 && wbuf[0] < 0xE000)
2365 {
2366 wchar_t leading;
2367
2368 if (msg->message == WM_KEYDOWN)
2369 leading = impl->leading_surrogate_keydown;
2370 else
2371 leading = impl->leading_surrogate_keyup;
2372
2373 event->key.keyval = gdk_unicode_to_keyval ((leading - 0xD800) * 0x400 + wbuf[0] - 0xDC00 + 0x10000);
2374 }
2375 else
2376 {
2377 event->key.keyval = gdk_unicode_to_keyval (wbuf[0]);
2378 }
2379 }
2380 else
2381 {
2382 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (_gdk_display),
2383 event->key.hardware_keycode,
2384 event->key.state,
2385 event->key.group,
2386 &event->key.keyval,
2387 NULL, NULL, NULL);
2388 }
2389
2390 if (msg->message == WM_KEYDOWN)
2391 impl->leading_surrogate_keydown = 0;
2392 else
2393 impl->leading_surrogate_keyup = 0;
2394
2395 fill_key_event_string (event);
2396
2397 /* Reset MOD1_MASK if it is the Alt key itself */
2398 if (msg->wParam == VK_MENU)
2399 event->key.state &= ~GDK_MOD1_MASK;
2400
2401 _gdk_win32_append_event (event);
2402
2403 return_val = TRUE;
2404 break;
2405
2406 case WM_SYSCHAR:
2407 if (msg->wParam != VK_SPACE)
2408 {
2409 /* To prevent beeps, don't let DefWindowProcW() be called */
2410 return_val = TRUE;
2411 goto done;
2412 }
2413 break;
2414
2415 case WM_IME_STARTCOMPOSITION:
2416 in_ime_composition = TRUE;
2417 break;
2418
2419 case WM_IME_ENDCOMPOSITION:
2420 in_ime_composition = FALSE;
2421 break;
2422
2423 case WM_IME_COMPOSITION:
2424 /* On Win2k WM_IME_CHAR doesn't work correctly for non-Unicode
2425 * applications. Thus, handle WM_IME_COMPOSITION with
2426 * GCS_RESULTSTR instead, fetch the Unicode chars from the IME
2427 * with ImmGetCompositionStringW().
2428 *
2429 * See for instance
2430 * http://groups.google.com/groups?selm=natX5.57%24g77.19788%40nntp2.onemain.com
2431 * and
2432 * http://groups.google.com/groups?selm=u2XfrXw5BHA.1628%40tkmsftngp02
2433 * for comments by other people that seems to have the same
2434 * experience. WM_IME_CHAR just gives question marks, apparently
2435 * because of going through some conversion to the current code
2436 * page.
2437 *
2438 * WM_IME_CHAR might work on NT4 or Win9x with ActiveIMM, but
2439 * use WM_IME_COMPOSITION there, too, to simplify the code.
2440 */
2441 GDK_NOTE (EVENTS, g_print (" %#lx", (long) msg->lParam));
2442
2443 if (!(msg->lParam & GCS_RESULTSTR))
2444 break;
2445
2446 if (!propagate (&window, msg,
2447 _gdk_display->keyboard_grab.window,
2448 _gdk_display->keyboard_grab.owner_events,
2449 GDK_ALL_EVENTS_MASK,
2450 doesnt_want_char))
2451 break;
2452
2453 if (GDK_WINDOW_DESTROYED (window))
2454 break;
2455
2456 himc = ImmGetContext (msg->hwnd);
2457 ccount = ImmGetCompositionStringW (himc, GCS_RESULTSTR,
2458 wbuf, sizeof (wbuf));
2459 ImmReleaseContext (msg->hwnd, himc);
2460
2461 ccount /= 2;
2462
2463 API_CALL (GetKeyboardState, (key_state));
2464
2465 for (i = 0; i < ccount; i++)
2466 {
2467 if (((GdkWindowObject *) window)->event_mask & GDK_KEY_PRESS_MASK)
2468 {
2469 /* Build a key press event */
2470 event = gdk_event_new (GDK_KEY_PRESS);
2471 event->key.window = window;
2472 build_wm_ime_composition_event (event, msg, wbuf[i], key_state);
2473
2474 _gdk_win32_append_event (event);
2475 }
2476
2477 if (((GdkWindowObject *) window)->event_mask & GDK_KEY_RELEASE_MASK)
2478 {
2479 /* Build a key release event. */
2480 event = gdk_event_new (GDK_KEY_RELEASE);
2481 event->key.window = window;
2482 build_wm_ime_composition_event (event, msg, wbuf[i], key_state);
2483
2484 _gdk_win32_append_event (event);
2485 }
2486 }
2487 return_val = TRUE;
2488 break;
2489
2490 case WM_LBUTTONDOWN:
2491 button = 1;
2492 goto buttondown0;
2493
2494 case WM_MBUTTONDOWN:
2495 button = 2;
2496 goto buttondown0;
2497
2498 case WM_RBUTTONDOWN:
2499 button = 3;
2500 goto buttondown0;
2501
2502 case WM_XBUTTONDOWN:
2503 if (HIWORD (msg->wParam) == XBUTTON1)
2504 button = 4;
2505 else
2506 button = 5;
2507
2508 buttondown0:
2509 GDK_NOTE (EVENTS,
2510 g_print (" (%d,%d)",
2511 GET_X_LPARAM (msg->lParam), GET_Y_LPARAM (msg->lParam)));
2512
2513 assign_object (&window, find_window_for_mouse_event (window, msg));
2514
2515 if (GDK_WINDOW_DESTROYED (window))
2516 break;
2517
2518 grab = _gdk_display_get_last_pointer_grab (_gdk_display);
2519 if (grab == NULL)
2520 {
2521 SetCapture (GDK_WINDOW_HWND (window));
2522 }
2523
2524 generate_button_event (GDK_BUTTON_PRESS, button,
2525 window, msg);
2526
2527 return_val = TRUE;
2528 break;
2529
2530 case WM_LBUTTONUP:
2531 button = 1;
2532 goto buttonup0;
2533
2534 case WM_MBUTTONUP:
2535 button = 2;
2536 goto buttonup0;
2537
2538 case WM_RBUTTONUP:
2539 button = 3;
2540 goto buttonup0;
2541
2542 case WM_XBUTTONUP:
2543 if (HIWORD (msg->wParam) == XBUTTON1)
2544 button = 4;
2545 else
2546 button = 5;
2547
2548 buttonup0:
2549 GDK_NOTE (EVENTS,
2550 g_print (" (%d,%d)",
2551 GET_X_LPARAM (msg->lParam), GET_Y_LPARAM (msg->lParam)));
2552
2553 assign_object (&window, find_window_for_mouse_event (window, msg));
2554 grab = _gdk_display_get_last_pointer_grab (_gdk_display);
2555 if (grab != NULL && grab->implicit)
2556 {
2557 gint state = build_pointer_event_state (msg);
2558
2559 /* We keep the implicit grab until no buttons at all are held down */
2560 if ((state & GDK_ANY_BUTTON_MASK & ~(GDK_BUTTON1_MASK << (button - 1))) == 0)
2561 {
2562 ReleaseCapture ();
2563
2564 new_window = NULL;
2565 hwnd = WindowFromPoint (msg->pt);
2566 if (hwnd != NULL)
2567 {
2568 POINT client_pt = msg->pt;
2569
2570 ScreenToClient (hwnd, &client_pt);
2571 GetClientRect (hwnd, &rect);
2572 if (PtInRect (&rect, client_pt))
2573 new_window = gdk_win32_handle_table_lookup ((GdkNativeWindow) hwnd);
2574 }
2575 synthesize_crossing_events (_gdk_display,
2576 grab->native_window, new_window,
2577 GDK_CROSSING_UNGRAB,
2578 &msg->pt,
2579 0, /* TODO: Set right mask */
2580 msg->time,
2581 FALSE);
2582 assign_object (&mouse_window, new_window);
2583 mouse_window_ignored_leave = NULL;
2584 }
2585 }
2586
2587 generate_button_event (GDK_BUTTON_RELEASE, button,
2588 window, msg);
2589
2590 return_val = TRUE;
2591 break;
2592
2593 case WM_MOUSEMOVE:
2594 GDK_NOTE (EVENTS,
2595 g_print (" %p (%d,%d)",
2596 (gpointer) msg->wParam,
2597 GET_X_LPARAM (msg->lParam), GET_Y_LPARAM (msg->lParam)));
2598
2599 new_window = window;
2600
2601 grab = _gdk_display_get_last_pointer_grab (_gdk_display);
2602 if (grab != NULL)
2603 {
2604 POINT pt;
2605 pt = msg->pt;
2606
2607 new_window = NULL;
2608 hwnd = WindowFromPoint (pt);
2609 if (hwnd != NULL)
2610 {
2611 POINT client_pt = pt;
2612
2613 ScreenToClient (hwnd, &client_pt);
2614 GetClientRect (hwnd, &rect);
2615 if (PtInRect (&rect, client_pt))
2616 new_window = gdk_win32_handle_table_lookup ((GdkNativeWindow) hwnd);
2617 }
2618
2619 if (!grab->owner_events &&
2620 new_window != NULL &&
2621 new_window != grab->native_window)
2622 new_window = NULL;
2623 }
2624
2625 if (mouse_window != new_window)
2626 {
2627 GDK_NOTE (EVENTS, g_print (" mouse_sinwod %p -> %p",
2628 mouse_window ? GDK_WINDOW_HWND (mouse_window) : NULL,
2629 new_window ? GDK_WINDOW_HWND (new_window) : NULL));
2630 synthesize_crossing_events (_gdk_display,
2631 mouse_window, new_window,
2632 GDK_CROSSING_NORMAL,
2633 &msg->pt,
2634 0, /* TODO: Set right mask */
2635 msg->time,
2636 FALSE);
2637 assign_object (&mouse_window, new_window);
2638 mouse_window_ignored_leave = NULL;
2639 if (new_window != NULL)
2640 track_mouse_event (TME_LEAVE, GDK_WINDOW_HWND (new_window));
2641 }
2642 else if (new_window != NULL &&
2643 new_window == mouse_window_ignored_leave)
2644 {
2645 /* If we ignored a leave event for this window and we're now getting
2646 input again we need to re-arm the mouse tracking, as that was
2647 cancelled by the mouseleave. */
2648 mouse_window_ignored_leave = NULL;
2649 track_mouse_event (TME_LEAVE, GDK_WINDOW_HWND (new_window));
2650 }
2651
2652 assign_object (&window, find_window_for_mouse_event (window, msg));
2653
2654 /* If we haven't moved, don't create any GDK event. Windows
2655 * sends WM_MOUSEMOVE messages after a new window is shows under
2656 * the mouse, even if the mouse hasn't moved. This disturbs gtk.
2657 */
2658 if (msg->pt.x + _gdk_offset_x == current_root_x &&
2659 msg->pt.y + _gdk_offset_y == current_root_y)
2660 break;
2661
2662 current_root_x = msg->pt.x + _gdk_offset_x;
2663 current_root_y = msg->pt.y + _gdk_offset_y;
2664
2665 event = gdk_event_new (GDK_MOTION_NOTIFY);
2666 event->motion.window = window;
2667 event->motion.time = _gdk_win32_get_next_tick (msg->time);
2668 event->motion.x = current_x = (gint16) GET_X_LPARAM (msg->lParam);
2669 event->motion.y = current_y = (gint16) GET_Y_LPARAM (msg->lParam);
2670 event->motion.x_root = current_root_x;
2671 event->motion.y_root = current_root_y;
2672 event->motion.axes = NULL;
2673 event->motion.state = build_pointer_event_state (msg);
2674 event->motion.is_hint = FALSE;
2675 event->motion.device = _gdk_display->core_pointer;
2676
2677 _gdk_win32_append_event (event);
2678
2679 return_val = TRUE;
2680 break;
2681
2682 case WM_NCMOUSEMOVE:
2683 GDK_NOTE (EVENTS,
2684 g_print (" (%d,%d)",
2685 GET_X_LPARAM (msg->lParam), GET_Y_LPARAM (msg->lParam)));
2686 break;
2687
2688 case WM_MOUSELEAVE:
2689 GDK_NOTE (EVENTS, g_print (" %d (%ld,%ld)",
2690 HIWORD (msg->wParam), msg->pt.x, msg->pt.y));
2691
2692 new_window = NULL;
2693 hwnd = WindowFromPoint (msg->pt);
2694 ignore_leave = FALSE;
2695 if (hwnd != NULL)
2696 {
2697 char classname[64];
2698
2699 POINT client_pt = msg->pt;
2700
2701 /* The synapitics trackpad drivers have this irritating
2702 feature where it pops up a window right under the pointer
2703 when you scroll. We ignore the leave and enter events for
2704 this window */
2705 if (GetClassNameA (hwnd, classname, sizeof(classname)) &&
2706 strcmp (classname, SYNAPSIS_ICON_WINDOW_CLASS) == 0)
2707 ignore_leave = TRUE;
2708
2709 ScreenToClient (hwnd, &client_pt);
2710 GetClientRect (hwnd, &rect);
2711 if (PtInRect (&rect, client_pt))
2712 new_window = gdk_win32_handle_table_lookup ((GdkNativeWindow) hwnd);
2713 }
2714
2715 if (!ignore_leave)
2716 synthesize_crossing_events (_gdk_display,
2717 mouse_window, new_window,
2718 GDK_CROSSING_NORMAL,
2719 &msg->pt,
2720 0, /* TODO: Set right mask */
2721 msg->time,
2722 FALSE);
2723 assign_object (&mouse_window, new_window);
2724 mouse_window_ignored_leave = ignore_leave ? new_window : NULL;
2725
2726
2727 return_val = TRUE;
2728 break;
2729
2730 case WM_MOUSEWHEEL:
2731 case WM_MOUSEHWHEEL:
2732 GDK_NOTE (EVENTS, g_print (" %d", (short) HIWORD (msg->wParam)));
2733
2734 /* WM_MOUSEWHEEL is delivered to the focus window. Work around
2735 * that. Also, the position is in screen coordinates, not client
2736 * coordinates as with the button messages. I love the
2737 * consistency of Windows.
2738 */
2739 point.x = GET_X_LPARAM (msg->lParam);
2740 point.y = GET_Y_LPARAM (msg->lParam);
2741
2742 if ((hwnd = WindowFromPoint (point)) == NULL)
2743 break;
2744
2745 {
2746 char classname[64];
2747
2748 /* The synapitics trackpad drivers have this irritating
2749 feature where it pops up a window right under the pointer
2750 when you scroll. We backtrack and to the toplevel and
2751 find the innermost child instead. */
2752 if (GetClassNameA (hwnd, classname, sizeof(classname)) &&
2753 strcmp (classname, SYNAPSIS_ICON_WINDOW_CLASS) == 0)
2754 {
2755 HWND hwndc;
2756
2757 /* Find our toplevel window */
2758 hwnd = GetAncestor (msg->hwnd, GA_ROOT);
2759
2760 /* Walk back up to the outermost child at the desired point */
2761 do {
2762 ScreenToClient (hwnd, &point);
2763 hwndc = ChildWindowFromPoint (hwnd, point);
2764 ClientToScreen (hwnd, &point);
2765 } while (hwndc != hwnd && (hwnd = hwndc, 1));
2766 }
2767 }
2768
2769 msg->hwnd = hwnd;
2770 if ((new_window = gdk_win32_handle_table_lookup ((GdkNativeWindow) msg->hwnd)) == NULL)
2771 break;
2772
2773 if (new_window != window)
2774 {
2775 assign_object (&window, new_window);
2776 }
2777
2778 ScreenToClient (msg->hwnd, &point);
2779
2780 event = gdk_event_new (GDK_SCROLL);
2781 event->scroll.window = window;
2782
2783 if (msg->message == WM_MOUSEWHEEL)
2784 {
2785 event->scroll.direction = (((short) HIWORD (msg->wParam)) > 0) ?
2786 GDK_SCROLL_UP : GDK_SCROLL_DOWN;
2787 }
2788 else if (msg->message == WM_MOUSEHWHEEL)
2789 {
2790 event->scroll.direction = (((short) HIWORD (msg->wParam)) > 0) ?
2791 GDK_SCROLL_RIGHT : GDK_SCROLL_LEFT;
2792 }
2793 event->scroll.time = _gdk_win32_get_next_tick (msg->time);
2794 event->scroll.x = (gint16) point.x;
2795 event->scroll.y = (gint16) point.y;
2796 event->scroll.x_root = (gint16) GET_X_LPARAM (msg->lParam) + _gdk_offset_x;
2797 event->scroll.y_root = (gint16) GET_Y_LPARAM (msg->lParam) + _gdk_offset_y;
2798 event->scroll.state = build_pointer_event_state (msg);
2799 event->scroll.device = _gdk_display->core_pointer;
2800
2801 _gdk_win32_append_event (event);
2802
2803 return_val = TRUE;
2804 break;
2805
2806 case WM_HSCROLL:
2807 /* Just print more debugging information, don't actually handle it. */
2808 GDK_NOTE (EVENTS,
2809 (g_print (" %s",
2810 (LOWORD (msg->wParam) == SB_ENDSCROLL ? "ENDSCROLL" :
2811 (LOWORD (msg->wParam) == SB_LEFT ? "LEFT" :
2812 (LOWORD (msg->wParam) == SB_RIGHT ? "RIGHT" :
2813 (LOWORD (msg->wParam) == SB_LINELEFT ? "LINELEFT" :
2814 (LOWORD (msg->wParam) == SB_LINERIGHT ? "LINERIGHT" :
2815 (LOWORD (msg->wParam) == SB_PAGELEFT ? "PAGELEFT" :
2816 (LOWORD (msg->wParam) == SB_PAGERIGHT ? "PAGERIGHT" :
2817 (LOWORD (msg->wParam) == SB_THUMBPOSITION ? "THUMBPOSITION" :
2818 (LOWORD (msg->wParam) == SB_THUMBTRACK ? "THUMBTRACK" :
2819 "???")))))))))),
2820 (LOWORD (msg->wParam) == SB_THUMBPOSITION ||
2821 LOWORD (msg->wParam) == SB_THUMBTRACK) ?
2822 (g_print (" %d", HIWORD (msg->wParam)), 0) : 0));
2823 break;
2824
2825 case WM_VSCROLL:
2826 /* Just print more debugging information, don't actually handle it. */
2827 GDK_NOTE (EVENTS,
2828 (g_print (" %s",
2829 (LOWORD (msg->wParam) == SB_ENDSCROLL ? "ENDSCROLL" :
2830 (LOWORD (msg->wParam) == SB_BOTTOM ? "BOTTOM" :
2831 (LOWORD (msg->wParam) == SB_TOP ? "TOP" :
2832 (LOWORD (msg->wParam) == SB_LINEDOWN ? "LINDOWN" :
2833 (LOWORD (msg->wParam) == SB_LINEUP ? "LINEUP" :
2834 (LOWORD (msg->wParam) == SB_PAGEDOWN ? "PAGEDOWN" :
2835 (LOWORD (msg->wParam) == SB_PAGEUP ? "PAGEUP" :
2836 (LOWORD (msg->wParam) == SB_THUMBPOSITION ? "THUMBPOSITION" :
2837 (LOWORD (msg->wParam) == SB_THUMBTRACK ? "THUMBTRACK" :
2838 "???")))))))))),
2839 (LOWORD (msg->wParam) == SB_THUMBPOSITION ||
2840 LOWORD (msg->wParam) == SB_THUMBTRACK) ?
2841 (g_print (" %d", HIWORD (msg->wParam)), 0) : 0));
2842 break;
2843
2844 case WM_QUERYNEWPALETTE:
2845 if (gdk_visual_get_system ()->type == GDK_VISUAL_PSEUDO_COLOR)
2846 {
2847 synthesize_expose_events (window);
2848 update_colors_counter = 0;
2849 }
2850 return_val = TRUE;
2851 break;
2852
2853 case WM_PALETTECHANGED:
2854 GDK_NOTE (EVENTS_OR_COLORMAP, g_print (" %p", (HWND) msg->wParam));
2855 if (gdk_visual_get_system ()->type != GDK_VISUAL_PSEUDO_COLOR)
2856 break;
2857
2858 return_val = TRUE;
2859
2860 if (msg->hwnd == (HWND) msg->wParam)
2861 break;
2862
2863 if (++update_colors_counter == 5)
2864 {
2865 synthesize_expose_events (window);
2866 update_colors_counter = 0;
2867 break;
2868 }
2869
2870 update_colors (window, TRUE);
2871 break;
2872
2873 case WM_MOUSEACTIVATE:
2874 {
2875 if (gdk_window_get_window_type (window) == GDK_WINDOW_TEMP
2876 || !((GdkWindowObject *)window)->accept_focus)
2877 {
2878 *ret_valp = MA_NOACTIVATE;
2879 return_val = TRUE;
2880 }
2881
2882 if (_gdk_modal_blocked (gdk_window_get_toplevel (window)))
2883 {
2884 *ret_valp = MA_NOACTIVATEANDEAT;
2885 return_val = TRUE;
2886 }
2887 }
2888
2889 break;
2890
2891 case WM_KILLFOCUS:
2892 if (_gdk_display->keyboard_grab.window != NULL &&
2893 !GDK_WINDOW_DESTROYED (_gdk_display->keyboard_grab.window))
2894 {
2895 generate_grab_broken_event (_gdk_display->keyboard_grab.window, TRUE, NULL);
2896 }
2897
2898 /* fallthrough */
2899 case WM_SETFOCUS:
2900 if (_gdk_display->keyboard_grab.window != NULL &&
2901 !_gdk_display->keyboard_grab.owner_events)
2902 break;
2903
2904 if (!(((GdkWindowObject *) window)->event_mask & GDK_FOCUS_CHANGE_MASK))
2905 break;
2906
2907 if (GDK_WINDOW_DESTROYED (window))
2908 break;
2909
2910 generate_focus_event (window, (msg->message == WM_SETFOCUS));
2911 return_val = TRUE;
2912 break;
2913
2914 case WM_ERASEBKGND:
2915 GDK_NOTE (EVENTS, g_print (" %p", (HANDLE) msg->wParam));
2916
2917 if (GDK_WINDOW_DESTROYED (window))
2918 break;
2919
2920 return_val = TRUE;
2921 *ret_valp = 1;
2922 break;
2923
2924 case WM_SYNCPAINT:
2925 sync_timer = SetTimer (GDK_WINDOW_HWND (window),
2926 1,
2927 200, sync_timer_proc);
2928 break;
2929
2930 case WM_PAINT:
2931 handle_wm_paint (msg, window, FALSE, NULL);
2932 break;
2933
2934 case WM_SETCURSOR:
2935 GDK_NOTE (EVENTS, g_print (" %#x %#x",
2936 LOWORD (msg->lParam), HIWORD (msg->lParam)));
2937
2938 grab = _gdk_display_get_last_pointer_grab (_gdk_display);
2939 if (grab != NULL)
2940 {
2941 grab_window = grab->window;
2942 }
2943
2944 if (grab_window == NULL && LOWORD (msg->lParam) != HTCLIENT)
2945 break;
2946
2947 if (grab_window != NULL && p_grab_cursor != NULL)
2948 hcursor = p_grab_cursor;
2949 else if (!GDK_WINDOW_DESTROYED (window))
2950 hcursor = GDK_WINDOW_IMPL_WIN32 (((GdkWindowObject *) window)->impl)->hcursor;
2951 else
2952 hcursor = NULL;
2953
2954 if (hcursor != NULL)
2955 {
2956 GDK_NOTE (EVENTS, g_print (" (SetCursor(%p)", hcursor));
2957 SetCursor (hcursor);
2958 return_val = TRUE;
2959 *ret_valp = TRUE;
2960 }
2961 break;
2962
2963 case WM_SYSCOMMAND:
2964 switch (msg->wParam)
2965 {
2966 case SC_MINIMIZE:
2967 case SC_RESTORE:
2968 do_show_window (window, msg->wParam == SC_MINIMIZE ? TRUE : FALSE);
2969 break;
2970 }
2971
2972 break;
2973
2974 case WM_ENTERSIZEMOVE:
2975 case WM_ENTERMENULOOP:
2976 if (msg->message == WM_ENTERSIZEMOVE)
2977 _modal_move_resize_window = msg->hwnd;
2978
2979 _gdk_win32_begin_modal_call ();
2980 break;
2981
2982 case WM_EXITSIZEMOVE:
2983 case WM_EXITMENULOOP:
2984 if (_modal_operation_in_progress)
2985 {
2986 _modal_move_resize_window = NULL;
2987 _gdk_win32_end_modal_call ();
2988 }
2989 break;
2990
2991 case WM_CAPTURECHANGED:
2992 /* Sometimes we don't get WM_EXITSIZEMOVE, for instance when you
2993 select move/size in the menu and then click somewhere without
2994 moving/resizing. We work around this using WM_CAPTURECHANGED. */
2995 if (_modal_operation_in_progress)
2996 {
2997 _modal_move_resize_window = NULL;
2998 _gdk_win32_end_modal_call ();
2999 }
3000 break;
3001
3002 case WM_WINDOWPOSCHANGING:
3003 GDK_NOTE (EVENTS, (windowpos = (WINDOWPOS *) msg->lParam,
3004 g_print (" %s %s %dx%d@%+d%+d now below %p",
3005 _gdk_win32_window_pos_bits_to_string (windowpos->flags),
3006 (windowpos->hwndInsertAfter == HWND_BOTTOM ? "BOTTOM" :
3007 (windowpos->hwndInsertAfter == HWND_NOTOPMOST ? "NOTOPMOST" :
3008 (windowpos->hwndInsertAfter == HWND_TOP ? "TOP" :
3009 (windowpos->hwndInsertAfter == HWND_TOPMOST ? "TOPMOST" :
3010 (sprintf (buf, "%p", windowpos->hwndInsertAfter),
3011 buf))))),
3012 windowpos->cx, windowpos->cy, windowpos->x, windowpos->y,
3013 GetNextWindow (msg->hwnd, GW_HWNDPREV))));
3014
3015 if (GDK_WINDOW_IS_MAPPED (window))
3016 return_val = ensure_stacking_on_window_pos_changing (msg, window);
3017 break;
3018
3019 case WM_WINDOWPOSCHANGED:
3020 windowpos = (WINDOWPOS *) msg->lParam;
3021 GDK_NOTE (EVENTS, g_print (" %s %s %dx%d@%+d%+d",
3022 _gdk_win32_window_pos_bits_to_string (windowpos->flags),
3023 (windowpos->hwndInsertAfter == HWND_BOTTOM ? "BOTTOM" :
3024 (windowpos->hwndInsertAfter == HWND_NOTOPMOST ? "NOTOPMOST" :
3025 (windowpos->hwndInsertAfter == HWND_TOP ? "TOP" :
3026 (windowpos->hwndInsertAfter == HWND_TOPMOST ? "TOPMOST" :
3027 (sprintf (buf, "%p", windowpos->hwndInsertAfter),
3028 buf))))),
3029 windowpos->cx, windowpos->cy, windowpos->x, windowpos->y));
3030
3031 /* Break grabs on unmap or minimize */
3032 if (windowpos->flags & SWP_HIDEWINDOW ||
3033 ((windowpos->flags & SWP_STATECHANGED) && IsIconic (msg->hwnd)))
3034 {
3035 grab = _gdk_display_get_last_pointer_grab (_gdk_display);
3036 if (grab != NULL)
3037 {
3038 if (grab->window == window)
3039 gdk_pointer_ungrab (msg->time);
3040 }
3041
3042 if (_gdk_display->keyboard_grab.window == window)
3043 gdk_keyboard_ungrab (msg->time);
3044 }
3045
3046 /* Send MAP events */
3047 if ((windowpos->flags & SWP_SHOWWINDOW) &&
3048 !GDK_WINDOW_DESTROYED (window))
3049 {
3050 event = gdk_event_new (GDK_MAP);
3051 event->any.window = window;
3052 _gdk_win32_append_event (event);
3053 }
3054
3055 /* Update window state */
3056 if (windowpos->flags & (SWP_STATECHANGED | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
3057 {
3058 GdkWindowState set_bits, unset_bits, old_state, new_state;
3059
3060 old_state = GDK_WINDOW_OBJECT (window)->state;
3061
3062 set_bits = 0;
3063 unset_bits = 0;
3064
3065 if (IsWindowVisible (msg->hwnd))
3066 unset_bits |= GDK_WINDOW_STATE_WITHDRAWN;
3067 else
3068 set_bits |= GDK_WINDOW_STATE_WITHDRAWN;
3069
3070 if (IsIconic (msg->hwnd))
3071 set_bits |= GDK_WINDOW_STATE_ICONIFIED;
3072 else
3073 unset_bits |= GDK_WINDOW_STATE_ICONIFIED;
3074
3075 if (IsZoomed (msg->hwnd))
3076 set_bits |= GDK_WINDOW_STATE_MAXIMIZED;
3077 else
3078 unset_bits |= GDK_WINDOW_STATE_MAXIMIZED;
3079
3080 gdk_synthesize_window_state (window, unset_bits, set_bits);
3081
3082 new_state = GDK_WINDOW_OBJECT (window)->state;
3083
3084 /* Whenever one window changes iconified state we need to also
3085 * change the iconified state in all transient related windows,
3086 * as windows doesn't give icons for transient childrens.
3087 */
3088 if ((old_state & GDK_WINDOW_STATE_ICONIFIED) !=
3089 (new_state & GDK_WINDOW_STATE_ICONIFIED))
3090 do_show_window (window, (new_state & GDK_WINDOW_STATE_ICONIFIED));
3091
3092
3093 /* When un-minimizing, make sure we're stacked under any
3094 transient-type windows. */
3095 if (!(old_state & GDK_WINDOW_STATE_ICONIFIED) &&
3096 (new_state & GDK_WINDOW_STATE_ICONIFIED))
3097 ensure_stacking_on_unminimize (msg);
3098 }
3099
3100 /* Show, New size or position => configure event */
3101 if (!(windowpos->flags & SWP_NOCLIENTMOVE) ||
3102 !(windowpos->flags & SWP_NOCLIENTSIZE) ||
3103 (windowpos->flags & SWP_SHOWWINDOW))
3104 {
3105 if (GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&
3106 !IsIconic (msg->hwnd) &&
3107 !GDK_WINDOW_DESTROYED (window))
3108 _gdk_win32_emit_configure_event (window);
3109
3110 if (((GdkWindowObject *) window)->input_window != NULL)
3111 _gdk_input_configure_event (window);
3112 }
3113
3114 if ((windowpos->flags & SWP_HIDEWINDOW) &&
3115 !GDK_WINDOW_DESTROYED (window))
3116 {
3117 /* Send UNMAP events */
3118 event = gdk_event_new (GDK_UNMAP);
3119 event->any.window = window;
3120 _gdk_win32_append_event (event);
3121
3122 /* Make transient parent the forground window when window unmaps */
3123 impl = GDK_WINDOW_IMPL_WIN32 (GDK_WINDOW_OBJECT (window)->impl);
3124
3125 if (impl->transient_owner &&
3126 GetForegroundWindow () == GDK_WINDOW_HWND (window))
3127 SetForegroundWindow (GDK_WINDOW_HWND (impl->transient_owner));
3128 }
3129
3130 if (!(windowpos->flags & SWP_NOCLIENTSIZE))
3131 {
3132 if (((GdkWindowObject *) window)->resize_count > 1)
3133 ((GdkWindowObject *) window)->resize_count -= 1;
3134 }
3135
3136 /* Call modal timer immediate so that we repaint faster after a resize. */
3137 if (_modal_operation_in_progress)
3138 modal_timer_proc (0,0,0,0);
3139
3140 /* Claim as handled, so that WM_SIZE and WM_MOVE are avoided */
3141 return_val = TRUE;
3142 *ret_valp = 0;
3143 break;
3144
3145 case WM_SIZING:
3146 GetWindowRect (GDK_WINDOW_HWND (window), &rect);
3147 drag = (RECT *) msg->lParam;
3148 GDK_NOTE (EVENTS, g_print (" %s curr:%s drag:%s",
3149 (msg->wParam == WMSZ_BOTTOM ? "BOTTOM" :
3150 (msg->wParam == WMSZ_BOTTOMLEFT ? "BOTTOMLEFT" :
3151 (msg->wParam == WMSZ_LEFT ? "LEFT" :
3152 (msg->wParam == WMSZ_TOPLEFT ? "TOPLEFT" :
3153 (msg->wParam == WMSZ_TOP ? "TOP" :
3154 (msg->wParam == WMSZ_TOPRIGHT ? "TOPRIGHT" :
3155 (msg->wParam == WMSZ_RIGHT ? "RIGHT" :
3156
3157 (msg->wParam == WMSZ_BOTTOMRIGHT ? "BOTTOMRIGHT" :
3158 "???")))))))),
3159 _gdk_win32_rect_to_string (&rect),
3160 _gdk_win32_rect_to_string (drag)));
3161
3162 impl = GDK_WINDOW_IMPL_WIN32 (((GdkWindowObject *) window)->impl);
3163 orig_drag = *drag;
3164 if (impl->hint_flags & GDK_HINT_RESIZE_INC)
3165 {
3166 GDK_NOTE (EVENTS, g_print (" (RESIZE_INC)"));
3167 if (impl->hint_flags & GDK_HINT_BASE_SIZE)
3168 {
3169 /* Resize in increments relative to the base size */
3170 rect.left = rect.top = 0;
3171 rect.right = impl->hints.base_width;
3172 rect.bottom = impl->hints.base_height;
3173 _gdk_win32_adjust_client_rect (window, &rect);
3174 point.x = rect.left;
3175 point.y = rect.top;
3176 ClientToScreen (GDK_WINDOW_HWND (window), &point);
3177 rect.left = point.x;
3178 rect.top = point.y;
3179 point.x = rect.right;
3180 point.y = rect.bottom;
3181 ClientToScreen (GDK_WINDOW_HWND (window), &point);
3182 rect.right = point.x;
3183 rect.bottom = point.y;
3184
3185 GDK_NOTE (EVENTS, g_print (" (also BASE_SIZE, using %s)",
3186 _gdk_win32_rect_to_string (&rect)));
3187 }
3188
3189 switch (msg->wParam)
3190 {
3191 case WMSZ_BOTTOM:
3192 if (drag->bottom == rect.bottom)
3193 break;
3194 adjust_drag (&drag->bottom, rect.bottom, impl->hints.height_inc);
3195 break;
3196
3197 case WMSZ_BOTTOMLEFT:
3198 if (drag->bottom == rect.bottom && drag->left == rect.left)
3199 break;
3200 adjust_drag (&drag->bottom, rect.bottom, impl->hints.height_inc);
3201 adjust_drag (&drag->left, rect.left, impl->hints.width_inc);
3202 break;
3203
3204 case WMSZ_LEFT:
3205 if (drag->left == rect.left)
3206 break;
3207 adjust_drag (&drag->left, rect.left, impl->hints.width_inc);
3208 break;
3209
3210 case WMSZ_TOPLEFT:
3211 if (drag->top == rect.top && drag->left == rect.left)
3212 break;
3213 adjust_drag (&drag->top, rect.top, impl->hints.height_inc);
3214 adjust_drag (&drag->left, rect.left, impl->hints.width_inc);
3215 break;
3216
3217 case WMSZ_TOP:
3218 if (drag->top == rect.top)
3219 break;
3220 adjust_drag (&drag->top, rect.top, impl->hints.height_inc);
3221 break;
3222
3223 case WMSZ_TOPRIGHT:
3224 if (drag->top == rect.top && drag->right == rect.right)
3225 break;
3226 adjust_drag (&drag->top, rect.top, impl->hints.height_inc);
3227 adjust_drag (&drag->right, rect.right, impl->hints.width_inc);
3228 break;
3229
3230 case WMSZ_RIGHT:
3231 if (drag->right == rect.right)
3232 break;
3233 adjust_drag (&drag->right, rect.right, impl->hints.width_inc);
3234 break;
3235
3236 case WMSZ_BOTTOMRIGHT:
3237 if (drag->bottom == rect.bottom && drag->right == rect.right)
3238 break;
3239 adjust_drag (&drag->bottom, rect.bottom, impl->hints.height_inc);
3240 adjust_drag (&drag->right, rect.right, impl->hints.width_inc);
3241 break;
3242 }
3243
3244 if (drag->bottom != orig_drag.bottom || drag->left != orig_drag.left ||
3245 drag->top != orig_drag.top || drag->right != orig_drag.right)
3246 {
3247 *ret_valp = TRUE;
3248 return_val = TRUE;
3249 GDK_NOTE (EVENTS, g_print (" (handled RESIZE_INC: %s)",
3250 _gdk_win32_rect_to_string (drag)));
3251 }
3252 }
3253
3254 /* WM_GETMINMAXINFO handles min_size and max_size hints? */
3255
3256 if (impl->hint_flags & GDK_HINT_ASPECT)
3257 {
3258 RECT decorated_rect;
3259 RECT undecorated_drag;
3260 int decoration_width, decoration_height;
3261 gdouble drag_aspect;
3262 int drag_width, drag_height, new_width, new_height;
3263
3264 GetClientRect (GDK_WINDOW_HWND (window), &rect);
3265 decorated_rect = rect;
3266 _gdk_win32_adjust_client_rect (window, &decorated_rect);
3267
3268 /* Set undecorated_drag to the client area being dragged
3269 * out, in screen coordinates.
3270 */
3271 undecorated_drag = *drag;
3272 undecorated_drag.left -= decorated_rect.left - rect.left;
3273 undecorated_drag.right -= decorated_rect.right - rect.right;
3274 undecorated_drag.top -= decorated_rect.top - rect.top;
3275 undecorated_drag.bottom -= decorated_rect.bottom - rect.bottom;
3276
3277 decoration_width = (decorated_rect.right - decorated_rect.left) - (rect.right - rect.left);
3278 decoration_height = (decorated_rect.bottom - decorated_rect.top) - (rect.bottom - rect.top);
3279
3280 drag_width = undecorated_drag.right - undecorated_drag.left;
3281 drag_height = undecorated_drag.bottom - undecorated_drag.top;
3282
3283 drag_aspect = (gdouble) drag_width / drag_height;
3284
3285 GDK_NOTE (EVENTS, g_print (" (ASPECT:%g--%g curr: %g)",
3286 impl->hints.min_aspect, impl->hints.max_aspect, drag_aspect));
3287
3288 if (drag_aspect < impl->hints.min_aspect)
3289 {
3290 /* Aspect is getting too narrow */
3291 switch (msg->wParam)
3292 {
3293 case WMSZ_BOTTOM:
3294 case WMSZ_TOP:
3295 /* User drags top or bottom edge outward. Keep height, increase width. */
3296 new_width = impl->hints.min_aspect * drag_height;
3297 drag->left -= (new_width - drag_width) / 2;
3298 drag->right = drag->left + new_width + decoration_width;
3299 break;
3300 case WMSZ_BOTTOMLEFT:
3301 case WMSZ_BOTTOMRIGHT:
3302 /* User drags bottom-left or bottom-right corner down. Adjust height. */
3303 new_height = drag_width / impl->hints.min_aspect;
3304 drag->bottom = drag->top + new_height + decoration_height;
3305 break;
3306 case WMSZ_LEFT:
3307 case WMSZ_RIGHT:
3308 /* User drags left or right edge inward. Decrease height */
3309 new_height = drag_width / impl->hints.min_aspect;
3310 drag->top += (drag_height - new_height) / 2;
3311 drag->bottom = drag->top + new_height + decoration_height;
3312 break;
3313 case WMSZ_TOPLEFT:
3314 case WMSZ_TOPRIGHT:
3315 /* User drags top-left or top-right corner up. Adjust height. */
3316 new_height = drag_width / impl->hints.min_aspect;
3317 drag->top = drag->bottom - new_height - decoration_height;
3318 }
3319 }
3320 else if (drag_aspect > impl->hints.max_aspect)
3321 {
3322 /* Aspect is getting too wide */
3323 switch (msg->wParam)
3324 {
3325 case WMSZ_BOTTOM:
3326 case WMSZ_TOP:
3327 /* User drags top or bottom edge inward. Decrease width. */
3328 new_width = impl->hints.max_aspect * drag_height;
3329 drag->left += (drag_width - new_width) / 2;
3330 drag->right = drag->left + new_width + decoration_width;
3331 break;
3332 case WMSZ_BOTTOMLEFT:
3333 case WMSZ_TOPLEFT:
3334 /* User drags bottom-left or top-left corner left. Adjust width. */
3335 new_width = impl->hints.max_aspect * drag_height;
3336 drag->left = drag->right - new_width - decoration_width;
3337 break;
3338 case WMSZ_BOTTOMRIGHT:
3339 case WMSZ_TOPRIGHT:
3340 /* User drags bottom-right or top-right corner right. Adjust width. */
3341 new_width = impl->hints.max_aspect * drag_height;
3342 drag->right = drag->left + new_width + decoration_width;
3343 break;
3344 case WMSZ_LEFT:
3345 case WMSZ_RIGHT:
3346 /* User drags left or right edge outward. Increase height. */
3347 new_height = drag_width / impl->hints.max_aspect;
3348 drag->top -= (new_height - drag_height) / 2;
3349 drag->bottom = drag->top + new_height + decoration_height;
3350 break;
3351 }
3352 }
3353
3354 *ret_valp = TRUE;
3355 return_val = TRUE;
3356 GDK_NOTE (EVENTS, g_print (" (handled ASPECT: %s)",
3357 _gdk_win32_rect_to_string (drag)));
3358 }
3359 break;
3360
3361 case WM_GETMINMAXINFO:
3362 if (GDK_WINDOW_DESTROYED (window))
3363 break;
3364
3365 impl = GDK_WINDOW_IMPL_WIN32 (((GdkWindowObject *) window)->impl);
3366 mmi = (MINMAXINFO*) msg->lParam;
3367 GDK_NOTE (EVENTS, g_print (" (mintrack:%ldx%ld maxtrack:%ldx%ld "
3368 "maxpos:%+ld%+ld maxsize:%ldx%ld)",
3369 mmi->ptMinTrackSize.x, mmi->ptMinTrackSize.y,
3370 mmi->ptMaxTrackSize.x, mmi->ptMaxTrackSize.y,
3371 mmi->ptMaxPosition.x, mmi->ptMaxPosition.y,
3372 mmi->ptMaxSize.x, mmi->ptMaxSize.y));
3373
3374 if (impl->hint_flags & GDK_HINT_MIN_SIZE)
3375 {
3376 rect.left = rect.top = 0;
3377 rect.right = impl->hints.min_width;
3378 rect.bottom = impl->hints.min_height;
3379
3380 _gdk_win32_adjust_client_rect (window, &rect);
3381
3382 mmi->ptMinTrackSize.x = rect.right - rect.left;
3383 mmi->ptMinTrackSize.y = rect.bottom - rect.top;
3384 }
3385
3386 if (impl->hint_flags & GDK_HINT_MAX_SIZE)
3387 {
3388 int maxw, maxh;
3389
3390 rect.left = rect.top = 0;
3391 rect.right = impl->hints.max_width;
3392 rect.bottom = impl->hints.max_height;
3393
3394 _gdk_win32_adjust_client_rect (window, &rect);
3395
3396 /* at least on win9x we have the 16 bit trouble */
3397 maxw = rect.right - rect.left;
3398 maxh = rect.bottom - rect.top;
3399 mmi->ptMaxTrackSize.x = maxw > 0 && maxw < G_MAXSHORT ? maxw : G_MAXSHORT;
3400 mmi->ptMaxTrackSize.y = maxh > 0 && maxh < G_MAXSHORT ? maxh : G_MAXSHORT;
3401 }
3402 else
3403 {
3404 mmi->ptMaxTrackSize.x = 30000;
3405 mmi->ptMaxTrackSize.y = 30000;
3406 }
3407
3408 if (impl->hint_flags & (GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE))
3409 {
3410 /* Don't call DefWindowProcW() */
3411 GDK_NOTE (EVENTS, g_print (" (handled, mintrack:%ldx%ld maxtrack:%ldx%ld "
3412 "maxpos:%+ld%+ld maxsize:%ldx%ld)",
3413 mmi->ptMinTrackSize.x, mmi->ptMinTrackSize.y,
3414 mmi->ptMaxTrackSize.x, mmi->ptMaxTrackSize.y,
3415 mmi->ptMaxPosition.x, mmi->ptMaxPosition.y,
3416 mmi->ptMaxSize.x, mmi->ptMaxSize.y));
3417 return_val = TRUE;
3418 }
3419
3420 return_val = TRUE;
3421 break;
3422
3423 case WM_CLOSE:
3424 if (GDK_WINDOW_DESTROYED (window))
3425 break;
3426
3427 event = gdk_event_new (GDK_DELETE);
3428 event->any.window = window;
3429
3430 _gdk_win32_append_event (event);
3431
3432 impl = GDK_WINDOW_IMPL_WIN32 (GDK_WINDOW_OBJECT (window)->impl);
3433
3434 if (impl->transient_owner && GetForegroundWindow() == GDK_WINDOW_HWND (window))
3435 {
3436 SetForegroundWindow (GDK_WINDOW_HWND (impl->transient_owner));
3437 }
3438
3439 return_val = TRUE;
3440 break;
3441
3442 case WM_NCDESTROY:
3443 grab = _gdk_display_get_last_pointer_grab (_gdk_display);
3444 if (grab != NULL)
3445 {
3446 if (grab->window == window)
3447 gdk_pointer_ungrab (msg->time);
3448 }
3449
3450 if (_gdk_display->keyboard_grab.window == window)
3451 gdk_keyboard_ungrab (msg->time);
3452
3453 if ((window != NULL) && (msg->hwnd != GetDesktopWindow ()))
3454 gdk_window_destroy_notify (window);
3455
3456 if (window == NULL || GDK_WINDOW_DESTROYED (window))
3457 break;
3458
3459 event = gdk_event_new (GDK_DESTROY);
3460 event->any.window = window;
3461
3462 _gdk_win32_append_event (event);
3463
3464 return_val = TRUE;
3465 break;
3466
3467 case WM_DISPLAYCHANGE:
3468 handle_display_change ();
3469 break;
3470
3471 case WM_DESTROYCLIPBOARD:
3472 if (!_ignore_destroy_clipboard)
3473 {
3474 event = gdk_event_new (GDK_SELECTION_CLEAR);
3475 event->selection.window = window;
3476 event->selection.selection = GDK_SELECTION_CLIPBOARD;
3477 event->selection.time = _gdk_win32_get_next_tick (msg->time);
3478 _gdk_win32_append_event (event);
3479 }
3480 else
3481 {
3482 return_val = TRUE;
3483 }
3484
3485 break;
3486
3487 case WM_RENDERFORMAT:
3488 GDK_NOTE (EVENTS, g_print (" %s", _gdk_win32_cf_to_string (msg->wParam)));
3489
3490 if (!(target = g_hash_table_lookup (_format_atom_table, GINT_TO_POINTER (msg->wParam))))
3491 {
3492 GDK_NOTE (EVENTS, g_print (" (target not found)"));
3493 return_val = TRUE;
3494 break;
3495 }
3496
3497 /* We need to render to clipboard immediately, don't call
3498 * _gdk_win32_append_event()
3499 */
3500 if (_gdk_event_func)
3501 {
3502 event = gdk_event_new (GDK_SELECTION_REQUEST);
3503 event->selection.window = window;
3504 event->selection.send_event = FALSE;
3505 event->selection.selection = GDK_SELECTION_CLIPBOARD;
3506 event->selection.target = target;
3507 event->selection.property = _gdk_selection;
3508 event->selection.requestor = msg->hwnd;
3509 event->selection.time = msg->time;
3510
3511 fixup_event (event);
3512 GDK_NOTE (EVENTS, g_print (" (calling gdk_event_func)"));
3513 GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
3514 (*_gdk_event_func) (event, _gdk_event_data);
3515 gdk_event_free (event);
3516
3517 /* Now the clipboard owner should have rendered */
3518 if (!_delayed_rendering_data)
3519 {
3520 GDK_NOTE (EVENTS, g_print (" (no _delayed_rendering_data?)"));
3521 }
3522 else
3523 {
3524 if (msg->wParam == CF_DIB)
3525 {
3526 _delayed_rendering_data =
3527 _gdk_win32_selection_convert_to_dib (_delayed_rendering_data,
3528 target);
3529 if (!_delayed_rendering_data)
3530 {
3531 g_warning ("Cannot convert to DIB from delayed rendered image");
3532 break;
3533 }
3534 }
3535
3536 /* The requestor is holding the clipboard, no
3537 * OpenClipboard() is required/possible
3538 */
3539 GDK_NOTE (DND,
3540 g_print (" SetClipboardData(%s,%p)",
3541 _gdk_win32_cf_to_string (msg->wParam),
3542 _delayed_rendering_data));
3543
3544 API_CALL (SetClipboardData, (msg->wParam, _delayed_rendering_data));
3545 _delayed_rendering_data = NULL;
3546 }
3547 }
3548 break;
3549
3550 case WM_ACTIVATE:
3551 GDK_NOTE (EVENTS, g_print (" %s%s %p",
3552 (LOWORD (msg->wParam) == WA_ACTIVE ? "ACTIVE" :
3553 (LOWORD (msg->wParam) == WA_CLICKACTIVE ? "CLICKACTIVE" :
3554 (LOWORD (msg->wParam) == WA_INACTIVE ? "INACTIVE" : "???"))),
3555 HIWORD (msg->wParam) ? " minimized" : "",
3556 (HWND) msg->lParam));
3557 /* We handle mouse clicks for modally-blocked windows under WM_MOUSEACTIVATE,
3558 * but we still need to deal with alt-tab, or with SetActiveWindow() type
3559 * situations.
3560 */
3561 if (_gdk_modal_blocked (window) && LOWORD (msg->wParam) == WA_ACTIVE)
3562 {
3563 GdkWindow *modal_current = _gdk_modal_current ();
3564 SetActiveWindow (GDK_WINDOW_HWND (modal_current));
3565 *ret_valp = 0;
3566 return_val = TRUE;
3567 break;
3568 }
3569
3570 /* Bring any tablet contexts to the top of the overlap order when
3571 * one of our windows is activated.
3572 * NOTE: It doesn't seem to work well if it is done in WM_ACTIVATEAPP
3573 * instead
3574 */
3575 if (LOWORD(msg->wParam) != WA_INACTIVE)
3576 _gdk_input_set_tablet_active ();
3577 break;
3578
3579 case WM_ACTIVATEAPP:
3580 GDK_NOTE (EVENTS, g_print (" %s thread: %I64d",
3581 msg->wParam ? "YES" : "NO",
3582 (gint64) msg->lParam));
3583 if (msg->wParam && GDK_WINDOW_IS_MAPPED (window))
3584 ensure_stacking_on_activate_app (msg, window);
3585 break;
3586
3587 /* Handle WINTAB events here, as we know that gdkinput.c will
3588 * use the fixed WT_DEFBASE as lcMsgBase, and we thus can use the
3589 * constants as case labels.
3590 */
3591 case WT_PACKET:
3592 GDK_NOTE (EVENTS, g_print (" %d %p",
3593 (int) msg->wParam, (gpointer) msg->lParam));
3594 goto wintab;
3595
3596 case WT_CSRCHANGE:
3597 GDK_NOTE (EVENTS, g_print (" %d %p",
3598 (int) msg->wParam, (gpointer) msg->lParam));
3599 goto wintab;
3600
3601 case WT_PROXIMITY:
3602 GDK_NOTE (EVENTS, g_print (" %p %d %d",
3603 (gpointer) msg->wParam,
3604 LOWORD (msg->lParam),
3605 HIWORD (msg->lParam)));
3606 /* Fall through */
3607 wintab:
3608
3609 event = gdk_event_new (GDK_NOTHING);
3610 event->any.window = NULL;
3611
3612 if (_gdk_input_other_event (event, msg, window))
3613 _gdk_win32_append_event (event);
3614 else
3615 gdk_event_free (event);
3616
3617 break;
3618 }
3619
3620 done:
3621
3622 if (window)
3623 g_object_unref (window);
3624
3625 #undef return
3626 return return_val;
3627 }
3628
3629 void
3630 _gdk_events_queue (GdkDisplay *display)
3631 {
3632 MSG msg;
3633
3634 if (modal_win32_dialog != NULL)
3635 return;
3636
3637 while (PeekMessageW (&msg, NULL, 0, 0, PM_REMOVE))
3638 {
3639 TranslateMessage (&msg);
3640 DispatchMessageW (&msg);
3641 }
3642 }
3643
3644 static gboolean
3645 gdk_event_prepare (GSource *source,
3646 gint *timeout)
3647 {
3648 gboolean retval;
3649
3650 GDK_THREADS_ENTER ();
3651
3652 *timeout = -1;
3653
3654 retval = (_gdk_event_queue_find_first (_gdk_display) != NULL ||
3655 (modal_win32_dialog == NULL &&
3656 GetQueueStatus (QS_ALLINPUT) != 0));
3657
3658 GDK_THREADS_LEAVE ();
3659
3660 return retval;
3661 }
3662
3663 static gboolean
3664 gdk_event_check (GSource *source)
3665 {
3666 gboolean retval;
3667
3668 GDK_THREADS_ENTER ();
3669
3670 if (event_poll_fd.revents & G_IO_IN)
3671 {
3672 retval = (_gdk_event_queue_find_first (_gdk_display) != NULL ||
3673 (modal_win32_dialog == NULL &&
3674 GetQueueStatus (QS_ALLINPUT) != 0));
3675 }
3676 else
3677 {
3678 retval = FALSE;
3679 }
3680
3681 GDK_THREADS_LEAVE ();
3682
3683 return retval;
3684 }
3685
3686 static gboolean
3687 gdk_event_dispatch (GSource *source,
3688 GSourceFunc callback,
3689 gpointer user_data)
3690 {
3691 GdkEvent *event;
3692
3693 GDK_THREADS_ENTER ();
3694
3695 _gdk_events_queue (_gdk_display);
3696 event = _gdk_event_unqueue (_gdk_display);
3697
3698 if (event)
3699 {
3700 if (_gdk_event_func)
3701 (*_gdk_event_func) (event, _gdk_event_data);
3702
3703 gdk_event_free (event);
3704
3705 /* Do drag & drop if it is still pending */
3706 if (_dnd_source_state == GDK_WIN32_DND_PENDING)
3707 {
3708 _dnd_source_state = GDK_WIN32_DND_DRAGGING;
3709 _gdk_win32_dnd_do_dragdrop ();
3710 _dnd_source_state = GDK_WIN32_DND_NONE;
3711 }
3712 }
3713
3714 GDK_THREADS_LEAVE ();
3715
3716 return TRUE;
3717 }
3718
3719 void
3720 gdk_win32_set_modal_dialog_libgtk_only (HWND window)
3721 {
3722 modal_win32_dialog = window;
3723 }
3724
3725 static void
3726 check_for_too_much_data (GdkEvent *event)
3727 {
3728 if (event->client.data.l[1] ||
3729 event->client.data.l[2] ||
3730 event->client.data.l[3] ||
3731 event->client.data.l[4])
3732 {
3733 g_warning ("Only four bytes of data are passed in client messages on Win32\n");
3734 }
3735 }
3736
3737 gboolean
3738 gdk_event_send_client_message_for_display (GdkDisplay *display,
3739 GdkEvent *event,
3740 GdkNativeWindow winid)
3741 {
3742 check_for_too_much_data (event);
3743
3744 return PostMessageW ((HWND) winid, client_message,
3745 (WPARAM) event->client.message_type,
3746 event->client.data.l[0]);
3747 }
3748
3749 void
3750 gdk_screen_broadcast_client_message (GdkScreen *screen,
3751 GdkEvent *event)
3752 {
3753 check_for_too_much_data (event);
3754
3755 PostMessageW (HWND_BROADCAST, client_message,
3756 (WPARAM) event->client.message_type,
3757 event->client.data.l[0]);
3758 }
3759
3760 void
3761 gdk_flush (void)
3762 {
3763 gdk_display_sync (_gdk_display);
3764 }
3765
3766 void
3767 gdk_display_sync (GdkDisplay * display)
3768 {
3769 g_return_if_fail (display == _gdk_display);
3770
3771 GdiFlush ();
3772 }
3773
3774 void
3775 gdk_display_flush (GdkDisplay * display)
3776 {
3777 g_return_if_fail (display == _gdk_display);
3778
3779 GdiFlush ();
3780 }
3781
3782 gboolean
3783 gdk_net_wm_supports (GdkAtom property)
3784 {
3785 return FALSE;
3786 }
3787
3788 void
3789 _gdk_windowing_event_data_copy (const GdkEvent *src,
3790 GdkEvent *dst)
3791 {
3792 }
3793
3794 void
3795 _gdk_windowing_event_data_free (GdkEvent *event)
3796 {
3797 }