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