gdkevents-win32.c 119 KB
Newer Older
1 2
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3
 * Copyright (C) 1998-2002 Tor Lillqvist
4
 * Copyright (C) 2001,2009 Hans Breuer
Cody Russell's avatar
Cody Russell committed
5
 * Copyright (C) 2007-2009 Cody Russell
6 7
 *
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9 10 11 12 13 14
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
18
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 20 21
 */

/*
22
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23 24
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
25
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26 27 28
 */

/* Cannot use TrackMouseEvent, as the stupid WM_MOUSELEAVE message
29
 * doesn’t tell us where the mouse has gone. Thus we cannot use it to
30 31 32 33
 * generate a correct GdkNotifyType. Pity, as using TrackMouseEvent
 * otherwise would make it possible to reliably generate
 * GDK_LEAVE_NOTIFY events, which would help get rid of those pesky
 * tooltips sometimes popping up in the wrong place.
34
 *
35
 * Update: a combination of TrackMouseEvent, GetCursorPos and
36 37 38 39
 * GetWindowPos can and is actually used to get rid of those
 * pesky tooltips. It should be possible to use this for the
 * whole ENTER/LEAVE NOTIFY handling but some platforms may
 * not have TrackMouseEvent at all (?) --hb
40 41
 */

42
#include "config.h"
43

44 45
#include "gdkprivate-win32.h"

46
#include <glib/gprintf.h>
47
#include <cairo-win32.h>
48

49
#include "gdk.h"
Hans Breuer's avatar
Hans Breuer committed
50
#include "gdkdisplayprivate.h"
51
#include "gdkmonitorprivate.h"
52
#include "gdkwin32.h"
53
#include "gdkkeysyms.h"
54
#include "gdkglcontext-win32.h"
55 56 57
#include "gdkdevicemanager-win32.h"
#include "gdkdeviceprivate.h"
#include "gdkdevice-wintab.h"
Hans Breuer's avatar
Hans Breuer committed
58
#include "gdkwin32dnd.h"
59
#include "gdkdisplay-win32.h"
LRN's avatar
LRN committed
60
#include "gdkselection-win32.h"
Hans Breuer's avatar
Hans Breuer committed
61
#include "gdkdndprivate.h"
62

63 64
#include <windowsx.h>

65 66 67 68 69
#ifdef G_WITH_CYGWIN
#include <fcntl.h>
#include <errno.h>
#endif

70
#include <objbase.h>
71

72 73
#include <imm.h>

Tor Lillqvist's avatar
Tor Lillqvist committed
74 75 76 77 78
#ifndef XBUTTON1
#define XBUTTON1 1
#define XBUTTON2 2
#endif

79 80 81 82 83
#ifndef VK_XBUTTON1
#define VK_XBUTTON1 5
#define VK_XBUTTON2 6
#endif

Tor Lillqvist's avatar
Tor Lillqvist committed
84 85 86 87 88
#ifndef MK_XBUTTON1
#define MK_XBUTTON1 32
#define MK_XBUTTON2 64
#endif

89 90 91 92
/* Undefined flags: */
#define SWP_NOCLIENTSIZE 0x0800
#define SWP_NOCLIENTMOVE 0x1000
#define SWP_STATECHANGED 0x8000
93
/*
94 95 96
 * Private function declarations
 */

97 98
#define SYNAPSIS_ICON_WINDOW_CLASS "SynTrackCursorWindowClass"

99 100
static gboolean gdk_event_translate (MSG        *msg,
				     gint       *ret_valp);
101 102 103 104 105 106
static gboolean gdk_event_prepare  (GSource     *source,
				    gint        *timeout);
static gboolean gdk_event_check    (GSource     *source);
static gboolean gdk_event_dispatch (GSource     *source,
				    GSourceFunc  callback,
				    gpointer     user_data);
107 108 109 110 111

/* Private variable declarations
 */

static GList *client_filters;	/* Filters for client messages */
112
extern gint       _gdk_input_ignore_core;
113

114
GdkCursor *_gdk_win32_grab_cursor;
115

116 117 118 119 120 121 122 123
typedef struct
{
  GSource source;

  GdkDisplay *display;
  GPollFD event_poll_fd;
} GdkWin32EventSource;

124 125 126 127
static GSourceFuncs event_funcs = {
  gdk_event_prepare,
  gdk_event_check,
  gdk_event_dispatch,
128
  NULL
129 130
};

131
static GdkWindow *mouse_window = NULL;
132
static GdkWindow *mouse_window_ignored_leave = NULL;
133
static gint current_x, current_y;
134
static gint current_root_x, current_root_y;
135
static UINT client_message;
136

137 138 139
static UINT got_gdk_events_message;
static HWND modal_win32_dialog = NULL;

140
#if 0
Tor Lillqvist's avatar
Tor Lillqvist committed
141
static HKL latin_locale = NULL;
142 143 144
#endif

static gboolean in_ime_composition = FALSE;
145
static UINT     modal_timer;
146
static UINT     sync_timer = 0;
Tor Lillqvist's avatar
Tor Lillqvist committed
147 148

static int debug_indent = 0;
149

150 151
static int both_shift_pressed[2]; /* to store keycodes for shift keys */

152 153 154 155
/* low-level keyboard hook handle */
static HHOOK keyboard_hook = NULL;
static UINT aerosnap_message;

156
static void
Tor Lillqvist's avatar
Tor Lillqvist committed
157 158
track_mouse_event (DWORD dwFlags,
		   HWND  hwnd)
159
{
160 161 162 163 164 165 166 167 168 169 170 171 172
  TRACKMOUSEEVENT tme;

  tme.cbSize = sizeof(TRACKMOUSEEVENT);
  tme.dwFlags = dwFlags;
  tme.hwndTrack = hwnd;
  tme.dwHoverTime = HOVER_DEFAULT; /* not used */

  if (!TrackMouseEvent (&tme))
    WIN32_API_FAILED ("TrackMouseEvent");
  else if (dwFlags == TME_LEAVE)
    GDK_NOTE (EVENTS, g_print(" (TrackMouseEvent %p)", hwnd));
  else if (dwFlags == TME_CANCEL)
    GDK_NOTE (EVENTS, g_print(" (cancel TrackMouseEvent %p)", hwnd));
173 174
}

175 176 177 178 179 180 181
gulong
_gdk_win32_get_next_tick (gulong suggested_tick)
{
  static gulong cur_tick = 0;

  if (suggested_tick == 0)
    suggested_tick = GetTickCount ();
LRN's avatar
LRN committed
182 183 184
  /* Ticks eventually wrap around.
   * This works as long as the interval between ticks is < 2147483648ms */
  if (suggested_tick <= cur_tick && ((cur_tick - suggested_tick) < 0x7FFFFFFF))
185
    return cur_tick;
186 187 188
  else
    return cur_tick = suggested_tick;
}
Tor Lillqvist's avatar
Tor Lillqvist committed
189

190
static void
191 192 193
generate_focus_event (GdkDeviceManager *device_manager,
                      GdkWindow        *window,
                      gboolean          in)
194
{
195
  GdkDevice *device;
196
  GdkDevice *source_device;
197 198
  GdkEvent *event;

199
  device = GDK_DEVICE_MANAGER_WIN32 (device_manager)->core_keyboard;
200
  source_device = GDK_DEVICE_MANAGER_WIN32 (device_manager)->system_keyboard;
201

202 203 204
  event = gdk_event_new (GDK_FOCUS_CHANGE);
  event->focus_change.window = window;
  event->focus_change.in = in;
205
  gdk_event_set_device (event, device);
206
  gdk_event_set_source_device (event, source_device);
207
  gdk_event_set_seat (event, gdk_device_get_seat (device));
208

209
  _gdk_win32_append_event (event);
210 211
}

212
static void
213 214 215 216
generate_grab_broken_event (GdkDeviceManager *device_manager,
                            GdkWindow        *window,
                            gboolean          keyboard,
                            GdkWindow        *grab_window)
217 218
{
  GdkEvent *event = gdk_event_new (GDK_GRAB_BROKEN);
219
  GdkDevice *device;
220
  GdkDevice *source_device;
221 222

  if (keyboard)
223 224 225 226
    {
      device = GDK_DEVICE_MANAGER_WIN32 (device_manager)->core_keyboard;
      source_device = GDK_DEVICE_MANAGER_WIN32 (device_manager)->system_keyboard;
    }
227
  else
228 229 230 231
    {
      device = GDK_DEVICE_MANAGER_WIN32 (device_manager)->core_pointer;
      source_device = GDK_DEVICE_MANAGER_WIN32 (device_manager)->system_pointer;
    }
232 233 234 235 236 237

  event->grab_broken.window = window;
  event->grab_broken.send_event = 0;
  event->grab_broken.keyboard = keyboard;
  event->grab_broken.implicit = FALSE;
  event->grab_broken.grab_window = grab_window;
238
  gdk_event_set_device (event, device);
239
  gdk_event_set_source_device (event, source_device);
240
  gdk_event_set_seat (event, gdk_device_get_seat (device));
241

242
  _gdk_win32_append_event (event);
243 244
}

245
static LRESULT
Tor Lillqvist's avatar
Tor Lillqvist committed
246 247 248 249
inner_window_procedure (HWND   hwnd,
			UINT   message,
			WPARAM wparam,
			LPARAM lparam)
250 251 252
{
  MSG msg;
  DWORD pos;
253
  gint ret_val = 0;
254

255
  msg.hwnd = hwnd;
256
  msg.message = message;
257 258
  msg.wParam = wparam;
  msg.lParam = lparam;
259
  msg.time = _gdk_win32_get_next_tick (0);
260
  pos = GetMessagePos ();
261 262
  msg.pt.x = GET_X_LPARAM (pos);
  msg.pt.y = GET_Y_LPARAM (pos);
263

264
  if (gdk_event_translate (&msg, &ret_val))
265
    {
266 267 268
      /* If gdk_event_translate() returns TRUE, we return ret_val from
       * the window procedure.
       */
269
      if (modal_win32_dialog)
270 271
	PostMessageW (modal_win32_dialog, got_gdk_events_message,
		      (WPARAM) 1, 0);
272
      return ret_val;
273 274 275
    }
  else
    {
276
      /* Otherwise call DefWindowProcW(). */
277
      GDK_NOTE (EVENTS, g_print (" DefWindowProcW"));
278
      return DefWindowProcW (hwnd, message, wparam, lparam);
279 280 281
    }
}

282
LRESULT CALLBACK
283 284 285 286
_gdk_win32_window_procedure (HWND   hwnd,
                             UINT   message,
                             WPARAM wparam,
                             LPARAM lparam)
287 288 289
{
  LRESULT retval;

290
  GDK_NOTE (EVENTS, g_print ("%s%*s%s %p %#x %#lx",
Tor Lillqvist's avatar
Tor Lillqvist committed
291
			     (debug_indent > 0 ? "\n" : ""),
292
			     debug_indent, "",
293 294
			     _gdk_win32_message_to_string (message), hwnd,
			     wparam, lparam));
Tor Lillqvist's avatar
Tor Lillqvist committed
295 296 297
  debug_indent += 2;
  retval = inner_window_procedure (hwnd, message, wparam, lparam);
  debug_indent -= 2;
298

LRN's avatar
LRN committed
299
  GDK_NOTE (EVENTS, g_print (" => %" G_GINT64_FORMAT "%s", (gint64) retval, (debug_indent == 0 ? "\n" : "")));
300 301 302 303

  return retval;
}

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
static LRESULT
low_level_keystroke_handler (WPARAM message,
                                       KBDLLHOOKSTRUCT *kbdhook,
                                       GdkWindow *window)
{
  GdkWindow *toplevel = gdk_window_get_toplevel (window);
  static DWORD last_keydown = 0;

  if (message == WM_KEYDOWN &&
      !GDK_WINDOW_DESTROYED (toplevel) &&
      _gdk_win32_window_lacks_wm_decorations (toplevel) && /* For CSD only */
      last_keydown != kbdhook->vkCode &&
      ((GetKeyState (VK_LWIN) & 0x8000) ||
      (GetKeyState (VK_RWIN) & 0x8000)))
	{
	  GdkWin32AeroSnapCombo combo = GDK_WIN32_AEROSNAP_COMBO_NOTHING;
	  gboolean lshiftdown = GetKeyState (VK_LSHIFT) & 0x8000;
          gboolean rshiftdown = GetKeyState (VK_RSHIFT) & 0x8000;
          gboolean oneshiftdown = (lshiftdown || rshiftdown) && !(lshiftdown && rshiftdown);
          gboolean maximized = gdk_window_get_state (toplevel) & GDK_WINDOW_STATE_MAXIMIZED;

	  switch (kbdhook->vkCode)
	    {
	    case VK_UP:
	      combo = GDK_WIN32_AEROSNAP_COMBO_UP;
	      break;
	    case VK_DOWN:
	      combo = GDK_WIN32_AEROSNAP_COMBO_DOWN;
	      break;
	    case VK_LEFT:
	      combo = GDK_WIN32_AEROSNAP_COMBO_LEFT;
	      break;
	    case VK_RIGHT:
	      combo = GDK_WIN32_AEROSNAP_COMBO_RIGHT;
	      break;
	    }

	  if (oneshiftdown && combo != GDK_WIN32_AEROSNAP_COMBO_NOTHING)
	    combo += 4;

	  /* These are the only combos that Windows WM does handle for us */
	  if (combo == GDK_WIN32_AEROSNAP_COMBO_SHIFTLEFT ||
              combo == GDK_WIN32_AEROSNAP_COMBO_SHIFTRIGHT)
            combo = GDK_WIN32_AEROSNAP_COMBO_NOTHING;

          /* On Windows 10 the WM will handle this specific combo */
          if (combo == GDK_WIN32_AEROSNAP_COMBO_DOWN && maximized &&
              g_win32_check_windows_version (6, 4, 0, G_WIN32_OS_ANY))
            combo = GDK_WIN32_AEROSNAP_COMBO_NOTHING;

	  if (combo != GDK_WIN32_AEROSNAP_COMBO_NOTHING)
            PostMessage (GDK_WINDOW_HWND (toplevel), aerosnap_message, (WPARAM) combo, 0);
	}

  if (message == WM_KEYDOWN)
    last_keydown = kbdhook->vkCode;
  else if (message = WM_KEYUP && last_keydown == kbdhook->vkCode)
    last_keydown = 0;

  return 0;
}

static LRESULT CALLBACK
low_level_keyboard_proc (int    code,
                         WPARAM wParam,
                         LPARAM lParam)
{
  KBDLLHOOKSTRUCT *kbdhook;
  HWND kbd_focus_owner;
  GdkWindow *gdk_kbd_focus_owner;
  LRESULT chain;

  do
  {
    if (code < 0)
      break;

    kbd_focus_owner = GetFocus ();

    if (kbd_focus_owner == NULL)
      break;

    gdk_kbd_focus_owner = gdk_win32_handle_table_lookup (kbd_focus_owner);

    if (gdk_kbd_focus_owner == NULL)
      break;

    kbdhook = (KBDLLHOOKSTRUCT *) lParam;
    chain = low_level_keystroke_handler (wParam, kbdhook, gdk_kbd_focus_owner);

    if (chain != 0)
      return chain;
  } while (FALSE);

  return CallNextHookEx (0, code, wParam, lParam);
}

static void
set_up_low_level_keyboard_hook (void)
{
  HHOOK hook_handle;

  if (keyboard_hook != NULL)
    return;

  hook_handle = SetWindowsHookEx (WH_KEYBOARD_LL,
                                  (HOOKPROC) low_level_keyboard_proc,
                                  _gdk_dll_hinstance,
                                  0);

  if (hook_handle != NULL)
    keyboard_hook = hook_handle;
  else
    WIN32_API_FAILED ("SetWindowsHookEx");

  aerosnap_message = RegisterWindowMessage ("GDK_WIN32_AEROSNAP_MESSAGE");
}

422
void
423
_gdk_events_init (GdkDisplay *display)
424
{
425
  GSource *source;
426
  GdkWin32EventSource *event_source;
427 428

#if 0
Tor Lillqvist's avatar
Tor Lillqvist committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
  int i, j, n;

  /* List of languages that use a latin keyboard. Somewhat sorted in
   * "order of least surprise", in case we have to load one of them if
   * the user only has arabic loaded, for instance.
   */
  static int latin_languages[] = {
    LANG_ENGLISH,
    LANG_SPANISH,
    LANG_PORTUGUESE,
    LANG_FRENCH,
    LANG_GERMAN,
    /* Rest in numeric order */
    LANG_CZECH,
    LANG_DANISH,
    LANG_FINNISH,
    LANG_HUNGARIAN,
    LANG_ICELANDIC,
    LANG_ITALIAN,
    LANG_DUTCH,
    LANG_NORWEGIAN,
    LANG_POLISH,
    LANG_ROMANIAN,
    LANG_SLOVAK,
    LANG_ALBANIAN,
    LANG_SWEDISH,
    LANG_TURKISH,
    LANG_INDONESIAN,
    LANG_SLOVENIAN,
    LANG_ESTONIAN,
    LANG_LATVIAN,
    LANG_LITHUANIAN,
    LANG_VIETNAMESE,
    LANG_AFRIKAANS,
463 464 465 466
    LANG_FAEROESE
#ifdef LANG_SWAHILI
   ,LANG_SWAHILI
#endif
Tor Lillqvist's avatar
Tor Lillqvist committed
467
  };
468
#endif
469

470
  client_message = RegisterWindowMessage ("GDK_WIN32_CLIENT_MESSAGE");
471
  got_gdk_events_message = RegisterWindowMessage ("GDK_WIN32_GOT_EVENTS");
472 473

#if 0
Tor Lillqvist's avatar
Tor Lillqvist committed
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
  /* Check if we have some input locale identifier loaded that uses a
   * latin keyboard, to be able to get the virtual-key code for the
   * latin characters corresponding to ASCII control characters.
   */
  if ((n = GetKeyboardLayoutList (0, NULL)) == 0)
    WIN32_API_FAILED ("GetKeyboardLayoutList");
  else
    {
      HKL *hkl_list = g_new (HKL, n);
      if (GetKeyboardLayoutList (n, hkl_list) == 0)
	WIN32_API_FAILED ("GetKeyboardLayoutList");
      else
	{
	  for (i = 0; latin_locale == NULL && i < n; i++)
	    for (j = 0; j < G_N_ELEMENTS (latin_languages); j++)
	      if (PRIMARYLANGID (LOWORD (hkl_list[i])) == latin_languages[j])
		{
		  latin_locale = hkl_list [i];
		  break;
		}
	}
      g_free (hkl_list);
    }

  if (latin_locale == NULL)
    {
      /* Try to load a keyboard layout with latin characters then.
       */
      i = 0;
      while (latin_locale == NULL && i < G_N_ELEMENTS (latin_languages))
	{
	  char id[9];
506
	  g_sprintf (id, "%08x", MAKELANGID (latin_languages[i++], SUBLANG_DEFAULT));
Tor Lillqvist's avatar
Tor Lillqvist committed
507 508 509 510 511
	  latin_locale = LoadKeyboardLayout (id, KLF_NOTELLSHELL|KLF_SUBSTITUTE_OK);
	}
    }

  GDK_NOTE (EVENTS, g_print ("latin_locale = %08x\n", (guint) latin_locale));
512
#endif
Tor Lillqvist's avatar
Tor Lillqvist committed
513

514
  source = g_source_new (&event_funcs, sizeof (GdkWin32EventSource));
515
  g_source_set_name (source, "GDK Win32 event source");
516
  g_source_set_priority (source, GDK_PRIORITY_EVENTS);
517

518 519
  event_source = (GdkWin32EventSource *)source;
  event_source->display = display;
520
#ifdef G_WITH_CYGWIN
521 522
  event_source->event_poll_fd.fd = open ("/dev/windows", O_RDONLY);
  if (event_source->event_poll_fd.fd == -1)
523 524
    g_error ("can't open \"/dev/windows\": %s", g_strerror (errno));
#else
525
  event_source->event_poll_fd.fd = G_WIN32_MSG_HANDLE;
526
#endif
527
  event_source->event_poll_fd.events = G_IO_IN;
528

529
  g_source_add_poll (source, &event_source->event_poll_fd);
530 531
  g_source_set_can_recurse (source, TRUE);
  g_source_attach (source, NULL);
532 533

  set_up_low_level_keyboard_hook ();
534 535 536
}

gboolean
Hans Breuer's avatar
Hans Breuer committed
537
_gdk_win32_display_has_pending (GdkDisplay *display)
538
{
Hans Breuer's avatar
Hans Breuer committed
539
  return (_gdk_event_queue_find_first (display) ||
540
	  (modal_win32_dialog == NULL &&
541
	   GetQueueStatus (QS_ALLINPUT) != 0));
542 543
}

544 545
#if 0 /* Unused, but might be useful to re-introduce in some debugging output? */

546 547 548 549 550 551 552 553 554
static char *
event_mask_string (GdkEventMask mask)
{
  static char bfr[500];
  char *p = bfr;

  *p = '\0';
#define BIT(x) \
  if (mask & GDK_##x##_MASK) \
555
    p += g_sprintf (p, "%s" #x, (p > bfr ? " " : ""))
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
  BIT (EXPOSURE);
  BIT (POINTER_MOTION);
  BIT (POINTER_MOTION_HINT);
  BIT (BUTTON_MOTION);
  BIT (BUTTON1_MOTION);
  BIT (BUTTON2_MOTION);
  BIT (BUTTON3_MOTION);
  BIT (BUTTON_PRESS);
  BIT (BUTTON_RELEASE);
  BIT (KEY_PRESS);
  BIT (KEY_RELEASE);
  BIT (ENTER_NOTIFY);
  BIT (LEAVE_NOTIFY);
  BIT (FOCUS_CHANGE);
  BIT (STRUCTURE);
  BIT (PROPERTY_CHANGE);
  BIT (VISIBILITY_NOTIFY);
  BIT (PROXIMITY_IN);
  BIT (PROXIMITY_OUT);
  BIT (SUBSTRUCTURE);
  BIT (SCROLL);
577 578 579 580 581
#undef BIT

  return bfr;
}

582 583
#endif

584 585 586 587 588
static GdkWindow *
find_window_for_mouse_event (GdkWindow* reported_window,
			     MSG*       msg)
{
  POINT pt;
Paolo Borelli's avatar
Paolo Borelli committed
589
  GdkDisplay *display;
590
  GdkDeviceManagerWin32 *device_manager;
591 592 593 594
  GdkWindow *event_window;
  HWND hwnd;
  RECT rect;
  GdkDeviceGrabInfo *grab;
595

Paolo Borelli's avatar
Paolo Borelli committed
596 597
  display = gdk_display_get_default ();
  device_manager = GDK_DEVICE_MANAGER_WIN32 (gdk_display_get_device_manager (display));
598

Paolo Borelli's avatar
Paolo Borelli committed
599
  grab = _gdk_display_get_last_device_grab (display, device_manager->core_pointer);
600
  if (grab == NULL)
601 602
    return reported_window;

603
  pt = msg->pt;
604

605 606 607
  if (!grab->owner_events)
    event_window = grab->native_window;
  else
608
    {
609 610 611 612 613
      event_window = NULL;
      hwnd = WindowFromPoint (pt);
      if (hwnd != NULL)
	{
	  POINT client_pt = pt;
614

615 616 617 618 619 620 621
	  ScreenToClient (hwnd, &client_pt);
	  GetClientRect (hwnd, &rect);
	  if (PtInRect (&rect, client_pt))
	    event_window = gdk_win32_handle_table_lookup (hwnd);
	}
      if (event_window == NULL)
	event_window = grab->native_window;
622 623 624
    }

  /* need to also adjust the coordinates to the new window */
625 626
  ScreenToClient (GDK_WINDOW_HWND (event_window), &pt);

627 628 629
  /* ATTENTION: need to update client coords */
  msg->lParam = MAKELPARAM (pt.x, pt.y);

630
  return event_window;
631 632
}

633
static void
634 635
build_key_event_state (GdkEvent *event,
		       BYTE     *key_state)
636
{
637 638
  GdkWin32Keymap *keymap;

639 640 641
  event->key.state = 0;

  if (key_state[VK_SHIFT] & 0x80)
642
    event->key.state |= GDK_SHIFT_MASK;
643 644

  if (key_state[VK_CAPITAL] & 0x01)
645
    event->key.state |= GDK_LOCK_MASK;
646

647 648 649 650 651 652 653 654 655 656 657
  if (key_state[VK_LBUTTON] & 0x80)
    event->key.state |= GDK_BUTTON1_MASK;
  if (key_state[VK_MBUTTON] & 0x80)
    event->key.state |= GDK_BUTTON2_MASK;
  if (key_state[VK_RBUTTON] & 0x80)
    event->key.state |= GDK_BUTTON3_MASK;
  if (key_state[VK_XBUTTON1] & 0x80)
    event->key.state |= GDK_BUTTON4_MASK;
  if (key_state[VK_XBUTTON2] & 0x80)
    event->key.state |= GDK_BUTTON5_MASK;

658 659 660 661
  keymap = GDK_WIN32_KEYMAP (_gdk_win32_display_get_keymap (_gdk_display));
  event->key.group = _gdk_win32_keymap_get_active_group (keymap);

  if (_gdk_win32_keymap_has_altgr (keymap) &&
662 663
      (key_state[VK_LCONTROL] & 0x80) &&
      (key_state[VK_RMENU] & 0x80))
664
    {
665 666
      event->key.state |= GDK_MOD2_MASK;
      if (key_state[VK_RCONTROL] & 0x80)
667
	event->key.state |= GDK_CONTROL_MASK;
668
      if (key_state[VK_LMENU] & 0x80)
669 670
	event->key.state |= GDK_MOD1_MASK;
    }
671
  else
672
    {
673 674 675 676
      if (key_state[VK_CONTROL] & 0x80)
	event->key.state |= GDK_CONTROL_MASK;
      if (key_state[VK_MENU] & 0x80)
	event->key.state |= GDK_MOD1_MASK;
677
    }
678 679 680
}

static gint
681
build_pointer_event_state (MSG *msg)
682 683
{
  gint state;
684

685
  state = 0;
686

687
  if (msg->wParam & MK_CONTROL)
688
    state |= GDK_CONTROL_MASK;
689 690 691 692

  if ((msg->message != WM_LBUTTONDOWN &&
       (msg->wParam & MK_LBUTTON)) ||
      msg->message == WM_LBUTTONUP)
693
    state |= GDK_BUTTON1_MASK;
694 695 696 697

  if ((msg->message != WM_MBUTTONDOWN &&
       (msg->wParam & MK_MBUTTON)) ||
      msg->message == WM_MBUTTONUP)
698
    state |= GDK_BUTTON2_MASK;
699 700 701 702

  if ((msg->message != WM_RBUTTONDOWN &&
       (msg->wParam & MK_RBUTTON)) ||
      msg->message == WM_RBUTTONUP)
703
    state |= GDK_BUTTON3_MASK;
704 705 706 707

  if (((msg->message != WM_XBUTTONDOWN || HIWORD (msg->wParam) != XBUTTON1) &&
       (msg->wParam & MK_XBUTTON1)) ||
      (msg->message == WM_XBUTTONUP && HIWORD (msg->wParam) == XBUTTON1))
Tor Lillqvist's avatar
Tor Lillqvist committed
708
    state |= GDK_BUTTON4_MASK;
709 710 711 712

  if (((msg->message != WM_XBUTTONDOWN || HIWORD (msg->wParam) != XBUTTON2) &&
       (msg->wParam & MK_XBUTTON2)) ||
      (msg->message == WM_XBUTTONUP && HIWORD (msg->wParam) == XBUTTON2))
Tor Lillqvist's avatar
Tor Lillqvist committed
713
    state |= GDK_BUTTON5_MASK;
714

715
  if (msg->wParam & MK_SHIFT)
716
    state |= GDK_SHIFT_MASK;
717

718 719
  if (GetKeyState (VK_MENU) < 0)
    state |= GDK_MOD1_MASK;
720

721 722 723 724 725 726 727
  if (GetKeyState (VK_CAPITAL) & 0x1)
    state |= GDK_LOCK_MASK;

  return state;
}

static void
728 729 730 731
build_wm_ime_composition_event (GdkEvent *event,
				MSG      *msg,
				wchar_t   wc,
				BYTE     *key_state)
732
{
733
  event->key.time = _gdk_win32_get_next_tick (msg->time);
734

735
  build_key_event_state (event, key_state);
736

737
  event->key.hardware_keycode = 0; /* FIXME: What should it be? */
738 739
  event->key.string = NULL;
  event->key.length = 0;
740
  event->key.keyval = gdk_unicode_to_keyval (wc);
741 742
}

743 744
#ifdef G_ENABLE_DEBUG

745
static void
746
print_event_state (guint state)
747
{
748 749 750 751 752
#define CASE(bit) if (state & GDK_ ## bit ## _MASK) g_print (#bit " ");
  CASE (SHIFT);
  CASE (LOCK);
  CASE (CONTROL);
  CASE (MOD1);
753 754 755 756
  CASE (MOD2);
  CASE (MOD3);
  CASE (MOD4);
  CASE (MOD5);
757 758 759
  CASE (BUTTON1);
  CASE (BUTTON2);
  CASE (BUTTON3);
760 761
  CASE (BUTTON4);
  CASE (BUTTON5);
762 763 764
#undef CASE
}

765 766
void
_gdk_win32_print_event (const GdkEvent *event)
767 768
{
  gchar *escaped, *kvname;
769
  gchar *selection_name, *target_name, *property_name;
770

Tor Lillqvist's avatar
Tor Lillqvist committed
771
  g_print ("%s%*s===> ", (debug_indent > 0 ? "\n" : ""), debug_indent, "");
772 773
  switch (event->any.type)
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
774
#define CASE(x) case x: g_print (#x); break;
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
    CASE (GDK_NOTHING);
    CASE (GDK_DELETE);
    CASE (GDK_DESTROY);
    CASE (GDK_EXPOSE);
    CASE (GDK_MOTION_NOTIFY);
    CASE (GDK_BUTTON_PRESS);
    CASE (GDK_2BUTTON_PRESS);
    CASE (GDK_3BUTTON_PRESS);
    CASE (GDK_BUTTON_RELEASE);
    CASE (GDK_KEY_PRESS);
    CASE (GDK_KEY_RELEASE);
    CASE (GDK_ENTER_NOTIFY);
    CASE (GDK_LEAVE_NOTIFY);
    CASE (GDK_FOCUS_CHANGE);
    CASE (GDK_CONFIGURE);
    CASE (GDK_MAP);
    CASE (GDK_UNMAP);
    CASE (GDK_PROPERTY_NOTIFY);
    CASE (GDK_SELECTION_CLEAR);
    CASE (GDK_SELECTION_REQUEST);
    CASE (GDK_SELECTION_NOTIFY);
    CASE (GDK_PROXIMITY_IN);
    CASE (GDK_PROXIMITY_OUT);
    CASE (GDK_DRAG_ENTER);
    CASE (GDK_DRAG_LEAVE);
    CASE (GDK_DRAG_MOTION);
    CASE (GDK_DRAG_STATUS);
    CASE (GDK_DROP_START);
    CASE (GDK_DROP_FINISHED);
    CASE (GDK_CLIENT_EVENT);
    CASE (GDK_VISIBILITY_NOTIFY);
    CASE (GDK_SCROLL);
    CASE (GDK_WINDOW_STATE);
808
    CASE (GDK_SETTING);
809 810
    CASE (GDK_OWNER_CHANGE);
    CASE (GDK_GRAB_BROKEN);
811
#undef CASE
Tor Lillqvist's avatar
Tor Lillqvist committed
812
    default: g_assert_not_reached ();
813
    }
814

815 816 817
  g_print (" %p @ %ums ",
           event->any.window ? GDK_WINDOW_HWND (event->any.window) : NULL,
           gdk_event_get_time (event));
818 819 820 821

  switch (event->any.type)
    {
    case GDK_EXPOSE:
822 823
      g_print ("%s %d",
	       _gdk_win32_gdkrectangle_to_string (&event->expose.area),
824 825 826
	       event->expose.count);
      break;
    case GDK_MOTION_NOTIFY:
827
      g_print ("(%.4g,%.4g) (%.4g,%.4g) %s",
828
	       event->motion.x, event->motion.y,
829
	       event->motion.x_root, event->motion.y_root,
830 831 832 833 834 835 836
	       event->motion.is_hint ? "HINT " : "");
      print_event_state (event->motion.state);
      break;
    case GDK_BUTTON_PRESS:
    case GDK_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
Tor Lillqvist's avatar
Tor Lillqvist committed
837
      g_print ("%d (%.4g,%.4g) (%.4g,%.4g) ",
838
	       event->button.button,
839 840
	       event->button.x, event->button.y,
	       event->button.x_root, event->button.y_root);
841 842
      print_event_state (event->button.state);
      break;
843
    case GDK_KEY_PRESS:
844 845 846 847 848 849
    case GDK_KEY_RELEASE:
      if (event->key.length == 0)
	escaped = g_strdup ("");
      else
	escaped = g_strescape (event->key.string, NULL);
      kvname = gdk_keyval_name (event->key.keyval);
850
      g_print ("%#.02x group:%d %s %d:\"%s\" ",
851
	       event->key.hardware_keycode, event->key.group,
852 853 854 855 856 857 858 859
	       (kvname ? kvname : "??"),
	       event->key.length,
	       escaped);
      g_free (escaped);
      print_event_state (event->key.state);
      break;
    case GDK_ENTER_NOTIFY:
    case GDK_LEAVE_NOTIFY:
860 861 862 863
      g_print ("%p (%.4g,%.4g) (%.4g,%.4g) %s %s%s",
	       event->crossing.subwindow == NULL ? NULL : GDK_WINDOW_HWND (event->crossing.subwindow),
	       event->crossing.x, event->crossing.y,
	       event->crossing.x_root, event->crossing.y_root,
864 865 866 867 868 869 870 871 872 873 874 875
	       (event->crossing.mode == GDK_CROSSING_NORMAL ? "NORMAL" :
		(event->crossing.mode == GDK_CROSSING_GRAB ? "GRAB" :
		 (event->crossing.mode == GDK_CROSSING_UNGRAB ? "UNGRAB" :
		  "???"))),
	       (event->crossing.detail == GDK_NOTIFY_ANCESTOR ? "ANCESTOR" :
		(event->crossing.detail == GDK_NOTIFY_VIRTUAL ? "VIRTUAL" :
		 (event->crossing.detail == GDK_NOTIFY_INFERIOR ? "INFERIOR" :
		  (event->crossing.detail == GDK_NOTIFY_NONLINEAR ? "NONLINEAR" :
		   (event->crossing.detail == GDK_NOTIFY_NONLINEAR_VIRTUAL ? "NONLINEAR_VIRTUAL" :
		    (event->crossing.detail == GDK_NOTIFY_UNKNOWN ? "UNKNOWN" :
		     "???")))))),
	       event->crossing.focus ? " FOCUS" : "");
876
      print_event_state (event->crossing.state);
877
      break;
Tor Lillqvist's avatar
Tor Lillqvist committed
878 879 880
    case GDK_FOCUS_CHANGE:
      g_print ("%s", (event->focus_change.in ? "IN" : "OUT"));
      break;
881 882 883 884 885 886
    case GDK_CONFIGURE:
      g_print ("x:%d y:%d w:%d h:%d",
	       event->configure.x, event->configure.y,
	       event->configure.width, event->configure.height);
      break;
    case GDK_SELECTION_CLEAR:
887 888 889 890 891 892 893 894 895 896 897
    case GDK_SELECTION_REQUEST:
    case GDK_SELECTION_NOTIFY:
      selection_name = gdk_atom_name (event->selection.selection);
      target_name = gdk_atom_name (event->selection.target);
      property_name = gdk_atom_name (event->selection.property);
      g_print ("sel:%s tgt:%s prop:%s",
	       selection_name, target_name, property_name);
      g_free (selection_name);
      g_free (target_name);
      g_free (property_name);
      break;
898 899 900 901 902 903 904 905 906 907 908 909 910
    case GDK_DRAG_ENTER:
    case GDK_DRAG_LEAVE:
    case GDK_DRAG_MOTION:
    case GDK_DRAG_STATUS:
    case GDK_DROP_START:
    case GDK_DROP_FINISHED:
      if (event->dnd.context != NULL)
	g_print ("ctx:%p: %s %s src:%p dest:%p",
		 event->dnd.context,
		 _gdk_win32_drag_protocol_to_string (event->dnd.context->protocol),
		 event->dnd.context->is_source ? "SOURCE" : "DEST",
		 event->dnd.context->source_window == NULL ? NULL : GDK_WINDOW_HWND (event->dnd.context->source_window),
		 event->dnd.context->dest_window == NULL ? NULL : GDK_WINDOW_HWND (event->dnd.context->dest_window));
Tor Lillqvist's avatar
Tor Lillqvist committed
911
      break;
912
    case GDK_CLIENT_EVENT:
913
      /* no more GdkEventClient */
914
      break;
915
    case GDK_SCROLL:
916
      g_print ("(%.4g,%.4g) (%.4g,%.4g) %s ",
917
	       event->scroll.x, event->scroll.y,
918
	       event->scroll.x_root, event->scroll.y_root,
919 920 921 922 923 924 925
	       (event->scroll.direction == GDK_SCROLL_UP ? "UP" :
		(event->scroll.direction == GDK_SCROLL_DOWN ? "DOWN" :
		 (event->scroll.direction == GDK_SCROLL_LEFT ? "LEFT" :
		  (event->scroll.direction == GDK_SCROLL_RIGHT ? "RIGHT" :
		   "???")))));
      print_event_state (event->scroll.state);
      break;
926
    case GDK_WINDOW_STATE:
Tor Lillqvist's avatar
Tor Lillqvist committed
927 928 929
      g_print ("%s: %s",
	       _gdk_win32_window_state_to_string (event->window_state.changed_mask),
	       _gdk_win32_window_state_to_string (event->window_state.new_window_state));
930 931 932 933 934 935 936 937 938 939 940 941
    case GDK_SETTING:
      g_print ("%s: %s",
	       (event->setting.action == GDK_SETTING_ACTION_NEW ? "NEW" :
		(event->setting.action == GDK_SETTING_ACTION_CHANGED ? "CHANGED" :
		 (event->setting.action == GDK_SETTING_ACTION_DELETED ? "DELETED" :
		  "???"))),
	       (event->setting.name ? event->setting.name : "NULL"));
    case GDK_GRAB_BROKEN:
      g_print ("%s %s %p",
	       (event->grab_broken.keyboard ? "KEYBOARD" : "POINTER"),
	       (event->grab_broken.implicit ? "IMPLICIT" : "EXPLICIT"),
	       (event->grab_broken.grab_window ? GDK_WINDOW_HWND (event->grab_broken.grab_window) : 0));
942 943 944
    default:
      /* Nothing */
      break;
945 946
    }
  g_print ("%s", (debug_indent == 0 ? "\n" : ""));
947 948
}

949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
static char *
decode_key_lparam (LPARAM lParam)
{
  static char buf[100];
  char *p = buf;

  if (HIWORD (lParam) & KF_UP)
    p += g_sprintf (p, "KF_UP ");
  if (HIWORD (lParam) & KF_REPEAT)
    p += g_sprintf (p, "KF_REPEAT ");
  if (HIWORD (lParam) & KF_ALTDOWN)
    p += g_sprintf (p, "KF_ALTDOWN ");
  if (HIWORD (lParam) & KF_EXTENDED)
    p += g_sprintf (p, "KF_EXTENDED ");
  p += g_sprintf (p, "sc:%d rep:%d", LOBYTE (HIWORD (lParam)), LOWORD (lParam));

  return buf;
}

968 969
#endif

970 971 972 973 974 975 976 977 978
static void
fixup_event (GdkEvent *event)
{
  if (event->any.window)
    g_object_ref (event->any.window);
  if (((event->any.type == GDK_ENTER_NOTIFY) ||
       (event->any.type == GDK_LEAVE_NOTIFY)) &&
      (event->crossing.subwindow != NULL))
    g_object_ref (event->crossing.subwindow);
979 980 981 982 983 984 985 986
  if (((event->any.type == GDK_SELECTION_CLEAR) ||
       (event->any.type == GDK_SELECTION_NOTIFY) ||
       (event->any.type == GDK_SELECTION_REQUEST)) &&
      (event->selection.requestor != NULL))
    g_object_ref (event->selection.requestor);
  if ((event->any.type == GDK_OWNER_CHANGE) &&
      (event->owner_change.owner != NULL))
    g_object_ref (event->owner_change.owner);
987
  event->any.send_event = InSendMessage ();
988 989
}

990 991
void
_gdk_win32_append_event (GdkEvent *event)
992
{
Paolo Borelli's avatar
Paolo Borelli committed
993
  GdkDisplay *display;
994
  GList *link;
995

Paolo Borelli's avatar
Paolo Borelli committed
996 997
  display = gdk_display_get_default ();

998
  fixup_event (event);
999
#if 1
Paolo Borelli's avatar
Paolo Borelli committed
1000
  link = _gdk_event_queue_append (display, event);
1001
  GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
1002
  /* event morphing, the passed in may not be valid afterwards */
Paolo Borelli's avatar
Paolo Borelli committed
1003
  _gdk_windowing_got_event (display, link, event, 0);
1004
#else
Paolo Borelli's avatar
Paolo Borelli committed
1005
  _gdk_event_queue_append (display, event);
1006
  GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
1007
#endif
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
}

static void
fill_key_event_string (GdkEvent *event)
{
  gunichar c;
  gchar buf[256];

  /* Fill in event->string crudely, since various programs
   * depend on it.
   */
1019

1020
  c = 0;
1021
  if (event->key.keyval != GDK_KEY_VoidSymbol)
1022 1023 1024 1025 1026 1027
    c = gdk_keyval_to_unicode (event->key.keyval);

  if (c)
    {
      gsize bytes_written;
      gint len;
1028

1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
      /* Apply the control key - Taken from Xlib
       */
      if (event->key.state & GDK_CONTROL_MASK)
	{
	  if ((c >= '@' && c < '\177') || c == ' ')
	    c &= 0x1F;
	  else if (c == '2')
	    {
	      event->key.string = g_memdup ("\0\0", 2);
	      event->key.length = 1;
	      return;
	    }
	  else if (c >= '3' && c <= '7')
	    c -= ('3' - '\033');
	  else if (c == '8')
	    c = '\177';
	  else if (c == '/')
	    c = '_' & 0x1F;
	}
1048

1049 1050
      len = g_unichar_to_utf8 (c, buf);
      buf[len] = '\0';
1051

1052 1053 1054 1055 1056 1057
      event->key.string = g_locale_from_utf8 (buf, len,
					      NULL, &bytes_written,
					      NULL);
      if (event->key.string)
	event->key.length = bytes_written;
    }
1058
  else if (event->key.keyval == GDK_KEY_Escape)
1059 1060 1061 1062
    {
      event->key.length = 1;
      event->key.string = g_strdup ("\033");
    }
1063 1064
  else if (event->key.keyval == GDK_KEY_Return ||
	   event->key.keyval == GDK_KEY_KP_Enter)
1065 1066 1067 1068
    {
      event->key.length = 1;
      event->key.string = g_strdup ("\r");
    }
1069

1070 1071 1072 1073 1074 1075 1076 1077
  if (!event->key.string)
    {
      event->key.length = 0;
      event->key.string = g_strdup ("");
    }
}

static GdkFilterReturn
1078 1079
apply_event_filters (GdkWindow  *window,
		     MSG        *msg,
1080
		     GList     **filters)
1081
{
1082 1083
  GdkFilterReturn result = GDK_FILTER_CONTINUE;
  GdkEvent *event;
Paolo Borelli's avatar
Paolo Borelli committed
1084
  GdkDisplay *display;
1085
  GList *node;
1086
  GList *tmp_list;
1087

1088
  event = gdk_event_new (GDK_NOTHING);
1089
  event->any.window = g_object_ref (window);
1090 1091
  ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;

Paolo Borelli's avatar
Paolo Borelli committed
1092 1093
  display = gdk_display_get_default ();

1094 1095 1096 1097
  /* I think GdkFilterFunc semantics require the passed-in event
   * to already be in the queue. The filter func can generate
   * more events and append them after it if it likes.
   */
Paolo Borelli's avatar
Paolo Borelli committed
1098
  node = _gdk_event_queue_append (display, event);
1099

1100
  tmp_list = *filters;
1101 1102 1103
  while (tmp_list)
    {
      GdkEventFilter *filter = (GdkEventFilter *) tmp_list->data;
1104 1105 1106 1107 1108 1109 1110 1111 1112
      GList *node;

      if ((filter->flags & GDK_EVENT_FILTER_REMOVED) != 0)
        {
          tmp_list = tmp_list->next;
          continue;
        }

      filter->ref_count++;
1113
      result = filter->function (msg, event, filter->data);
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127

      /* get the next node after running the function since the
         function may add or remove a next node */
      node = tmp_list;
      tmp_list = tmp_list->next;

      filter->ref_count--;
      if (filter->ref_count == 0)
        {
          *filters = g_list_remove_link (*filters, node);
          g_list_free_1 (node);
          g_free (filter);
        }

1128 1129 1130 1131
      if (result !=  GDK_FILTER_CONTINUE)
	break;
    }

1132 1133
  if (result == GDK_FILTER_CONTINUE || result == GDK_FILTER_REMOVE)
    {
Paolo Borelli's avatar
Paolo Borelli committed
1134
      _gdk_event_queue_remove_link (display, node);
1135 1136 1137 1138 1139 1140 1141
      g_list_free_1 (node);
      gdk_event_free (event);
    }
  else /* GDK_FILTER_TRANSLATE */
    {
      ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
      fixup_event (event);
1142
      GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
1143
    }
Paolo Borelli's avatar
Paolo Borelli committed
1144

1145 1146 1147
  return result;
}

1148 1149 1150 1151
/*
 * On Windows, transient windows will not have their own taskbar entries.
 * Because of this, we must hide and restore groups of transients in both
 * directions.  That is, all transient children must be hidden or restored
1152 1153
 * with this window, but if this window’s transient owner also has a
 * transient owner then this window’s transient owner must be hidden/restored
1154 1155 1156
 * with this one.  And etc, up the chain until we hit an ancestor that has no
 * transient owner.
 *
1157
 * It would be a good idea if applications don’t chain transient windows
1158
 * together.  There’s a limit to how much evil GTK can try to shield you
1159 1160
 * from.
 */
1161 1162 1163
static void
show_window_recurse (GdkWindow *window, gboolean hide_window)
{
Hans Breuer's avatar
Hans Breuer committed
1164
  GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
  GSList *children = impl->transient_children;
  GdkWindow *child = NULL;

  if (!impl->changing_state)
    {
      impl->changing_state = TRUE;

      if (children != NULL)
	{
	  while (children != NULL)
	    {
	      child = children->data;
	      show_window_recurse (child, hide_window);

1179
	      children = children->next;
1180 1181 1182
	    }
	}

1183 1184 1185
      if (GDK_WINDOW_IS_MAPPED (window))
	{
	  if (!hide_window)
1186
	    {
Hans Breuer's avatar
Hans Breuer committed
1187
	      if (gdk_window_get_state (window) & GDK_WINDOW_STATE_ICONIFIED)
1188
		{
Hans Breuer's avatar
Hans Breuer committed
1189
		  if (gdk_window_get_state (window) & GDK_WINDOW_STATE_MAXIMIZED)
1190
		    {
1191
		      GtkShowWindow (window, SW_SHOWMAXIMIZED);
1192 1193 1194
		    }
		  else
		    {
1195
		      GtkShowWindow (window, SW_RESTORE);
1196
		    }
1197 1198
		}
	    }
1199
	  else
1200
	    {
1201
	      GtkShowWindow (window, SW_MINIMIZE);
1202
	    }
1203
	}
1204 1205 1206 1207 1208

      impl->changing_state = FALSE;
    }
}

1209
static void
1210
do_show_window (GdkWindow *window, gboolean hide_window)
1211 1212
{
  GdkWindow *tmp_window = NULL;
Hans Breuer's avatar
Hans Breuer committed
1213
  GdkWindowImplWin32 *tmp_impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1214 1215 1216 1217 1218 1219 1220

  if (!tmp_impl->changing_state)
    {
      /* Find the top-level window in our transient chain. */
      while (tmp_impl->transient_owner != NULL)
	{
	  tmp_window = tmp_impl->transient_owner;
Hans Breuer's avatar
Hans Breuer committed
1221
	  tmp_impl = GDK_WINDOW_IMPL_WIN32 (tmp_window->impl);
1222 1223 1224 1225 1226 1227 1228 1229 1230
	}

      /* If we couldn't find one, use the window provided. */
      if (tmp_window == NULL)
	{
	  tmp_window = window;
	}

      /* Recursively show/hide every window in the chain. */
1231 1232 1233 1234
      if (tmp_window != window)
	{
	  show_window_recurse (tmp_window, hide_window);
	}
1235 1236 1237
    }
}

1238
static void
1239 1240 1241 1242 1243 1244 1245 1246 1247
send_crossing_event (GdkDisplay                 *display,
		     GdkWindow                  *window,
		     GdkEventType                type,
		     GdkCrossingMode             mode,
		     GdkNotifyType               notify_type,
		     GdkWindow                  *subwindow,
		     POINT                      *screen_pt,
		     GdkModifierType             mask,
		     guint32                     time_)
1248 1249
{
  GdkEvent *event;
1250 1251
  GdkDeviceGrabInfo *grab;
  GdkDeviceManagerWin32 *device_manager;
1252
  POINT pt;
1253
  GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
1254

1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
  device_manager = GDK_DEVICE_MANAGER_WIN32 (gdk_display_get_device_manager (display));

  grab = _gdk_display_has_device_grab (display, device_manager->core_pointer, 0);

  if (grab != NULL &&
      !grab->owner_events &&
      mode != GDK_CROSSING_UNGRAB)
    {
      /* !owner_event => only report events wrt grab window, ignore rest */
      if ((GdkWindow *)window != grab->native_window)
	return;
    }

  pt = *screen_pt;
1269
  ScreenToClient (GDK_WINDOW_HWND (window), &pt);
1270

1271
  event = gdk_event_new (type);
1272
  event->crossing.window = window;
1273 1274
  event->crossing.subwindow = subwindow;
  event->crossing.time = _gdk_win32_get_next_tick (time_);
1275 1276 1277 1278
  event->crossing.x = pt.x / impl->window_scale;
  event->crossing.y = pt.y / impl->window_scale;
  event->crossing.x_root = (screen_pt->x + _gdk_offset_x) / impl->window_scale;
  event->crossing.y_root = (screen_pt->y + _gdk_offset_y) / impl->window_scale;
1279 1280
  event->crossing.mode = mode;
  event->crossing.detail = notify_type;
1281
  event->crossing.mode = mode;
1282 1283 1284
  event->crossing.detail = notify_type;
  event->crossing.focus = FALSE;
  event->crossing.state = mask;
1285 1286
  gdk_event_set_device (event, device_manager->core_pointer);
  gdk_event_set_source_device (event, device_manager->system_pointer);
1287
  gdk_event_set_seat (event, gdk_device_get_seat (device_manager->core_pointer));
Cody Russell's avatar
Cody Russell committed
1288

1289
  _gdk_win32_append_event (event);
1290
}
1291

1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
static GdkWindow *
get_native_parent (GdkWindow *window)
{
  if (window->parent != NULL)
    return window->parent->impl_window;
  return NULL;
}

static GdkWindow *
find_common_ancestor (GdkWindow *win1,
		      GdkWindow *win2)
{
  GdkWindow *tmp;
  GList *path1 = NULL, *path2 = NULL;
  GList *list1, *list2;

  tmp = win1;
  while (tmp != NULL && tmp->window_type != GDK_WINDOW_ROOT)
    {
      path1 = g_list_prepend (path1, tmp);
      tmp = get_native_parent (tmp);
    }

  tmp = win2;
  while (tmp != NULL && tmp->window_type != GDK_WINDOW_ROOT)
    {
      path2 = g_list_prepend (path2, tmp);
      tmp = get_native_parent (tmp);
    }

  list1 = path1;
  list2 = path2;
  tmp = NULL;
  while (list1 && list2 && (list1->data == list2->data))
    {
      tmp = (GdkWindow *)list1->data;
1328 1329
      list1 = list1->next;
      list2 = list2->next;
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
    }
  g_list_free (path1);
  g_list_free (path2);

  return tmp;
}

void
synthesize_crossing_events (GdkDisplay                 *display,
			    GdkWindow                  *src,
			    GdkWindow                  *dest,
			    GdkCrossingMode             mode,
			    POINT                      *screen_pt,
			    GdkModifierType             mask,
			    guint32                     time_,
			    gboolean                    non_linear)
{
  GdkWindow *c;
  GdkWindow *win, *last, *next;
  GList *path, *list;
  GdkWindow *a;
  GdkWindow *b;
  GdkNotifyType notify_type;

  a = src;
  b = dest;
  if (a == b)
    return; /* No crossings generated between src and dest */

  c = find_common_ancestor (a, b);

  non_linear |= (c != a) && (c != b);

  if (a) /* There might not be a source (i.e. if no previous pointer_in_window) */
    {
      /* Traverse up from a to (excluding) c sending leave events */
      if (non_linear)
	notify_type = GDK_NOTIFY_NONLINEAR;
      else if (c == a)
	notify_type = GDK_NOTIFY_INFERIOR;
      else
	notify_type = GDK_NOTIFY_ANCESTOR;
      send_crossing_event (display,
			   a, GDK_LEAVE_NOTIFY,
			   mode,
			   notify_type,
			   NULL,
			   screen_pt,
			   mask, time_);

      if (c != a)
	{
	  if (non_linear)
	    notify_type = GDK_NOTIFY_NONLINEAR_VIRTUAL;
	  else
	    notify_type = GDK_NOTIFY_VIRTUAL;

	  last = a;
	  win = get_native_parent (a);
	  while (win != c && win->window_type != GDK_WINDOW_ROOT)
	    {
	      send_crossing_event (display,
				   win, GDK_LEAVE_NOTIFY,
				   mode,
				   notify_type,
				   (GdkWindow *)last,
				   screen_pt,
				   mask, time_);

	      last = win;
	      win = get_native_parent (win);
	    }
	}
    }

  if (b) /* Might not be a dest, e.g. if we're moving out of the window */
    {
      /* Traverse down from c to b */
      if (c != b)
	{
	  path = NULL;
	  win = get_native_parent (b);
	  while (win != c && win->window_type != GDK_WINDOW_ROOT)
	    {
	      path = g_list_prepend (path, win);
	      win = get_native_parent (win);
	    }

	  if (non_linear)
	    notify_type = GDK_NOTIFY_NONLINEAR_VIRTUAL;
	  else
	    notify_type = GDK_NOTIFY_VIRTUAL;

	  list = path;
	  while (list)
	    {
	      win = (GdkWindow *)list->data;
1427
	      list = list->next;
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
	      if (list)
		next = (GdkWindow *)list->data;
	      else
		next = b;

	      send_crossing_event (display,
				   win, GDK_ENTER_NOTIFY,
				   mode,
				   notify_type,
				   next,
				   screen_pt,
				   mask, time_);
	    }
	  g_list_free (path);
	}


      if (non_linear)
	notify_type = GDK_NOTIFY_NONLINEAR;
      else if (c == a)
	notify_type = GDK_NOTIFY_ANCESTOR;
      else
	notify_type = GDK_NOTIFY_INFERIOR;

      send_crossing_event (display,
			   b, GDK_ENTER_NOTIFY,
			   mode,
			   notify_type,
			   NULL,
			   screen_pt,
			   mask, time_);
    }
}

1462 1463
/* The check_extended flag controls whether to check if the windows want
 * events from extended input devices and if the message should be skipped
1464 1465
 * because an extended input device is active
 */
1466 1467
static gboolean
propagate (GdkWindow  **window,
1468
	   MSG         *msg,
1469 1470 1471 1472
	   GdkWindow   *grab_window,
	   gboolean     grab_owner_events,
	   gint	        grab_mask,
	   gboolean   (*doesnt_want_it) (gint mask,
1473
					 MSG *msg))
1474 1475 1476 1477
{
  if (grab_window != NULL && !grab_owner_events)
    {
      /* Event source is grabbed with owner_events FALSE */
1478

1479
      if ((*doesnt_want_it) (grab_mask, msg))
1480
	{
Tor Lillqvist's avatar
Tor Lillqvist committed
1481
	  GDK_NOTE (EVENTS, g_print (" (grabber doesn't want it)"));
1482 1483 1484 1485
	  return FALSE;
	}
      else
	{