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