gdkevents-win32.c 92.9 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
grabs  
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
18 19 20 21 22 23
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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

/* Cannot use TrackMouseEvent, as the stupid WM_MOUSELEAVE message
 * doesn't tell us where the mouse has gone. Thus we cannot use it to
 * 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.
36 37 38 39 40 41
 *
 * Update: a combination of TrackMouseEvent, GetCursorPos and 
 * 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
42 43
 */

44
#include "config.h"
45

46
#include <glib/gprintf.h>
47

48
#include "gdk.h"
49
#include "gdkprivate-win32.h"
50 51
#include "gdkinput-win32.h"
#include "gdkkeysyms.h"
52

53 54
#include <windowsx.h>

55 56 57 58 59
#ifdef G_WITH_CYGWIN
#include <fcntl.h>
#include <errno.h>
#endif

60
#include <objbase.h>
61

62 63
#include <imm.h>

Tor Lillqvist's avatar
Tor Lillqvist committed
64 65 66 67 68
#ifndef XBUTTON1
#define XBUTTON1 1
#define XBUTTON2 2
#endif

69 70 71 72 73
#ifndef VK_XBUTTON1
#define VK_XBUTTON1 5
#define VK_XBUTTON2 6
#endif

Tor Lillqvist's avatar
Tor Lillqvist committed
74 75 76 77 78
#ifndef MK_XBUTTON1
#define MK_XBUTTON1 32
#define MK_XBUTTON2 64
#endif

79 80 81 82
/* 
 * Private function declarations
 */

83 84 85 86 87 88
static gboolean gdk_event_translate (MSG        *msg,
				     gint       *ret_valp);
static void     handle_wm_paint     (MSG        *msg,
				     GdkWindow  *window,
				     gboolean    return_exposes,
				     GdkEvent  **event);
89 90 91 92 93 94 95

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);
96

97
static void append_event (GdkEvent *event);
98
static gboolean is_modally_blocked (GdkWindow   *window);
99

100 101 102 103 104 105 106 107 108 109 110
/* Private variable declarations
 */

static GList *client_filters;	/* Filters for client messages */

static HCURSOR p_grab_cursor;

static GSourceFuncs event_funcs = {
  gdk_event_prepare,
  gdk_event_check,
  gdk_event_dispatch,
111
  NULL
112 113 114 115
};

GPollFD event_poll_fd;

116
static GdkWindow *current_toplevel = NULL;
117
static gint current_x, current_y;
118
static gint current_root_x, current_root_y;
119
static UINT client_message;
120

121 122 123
static UINT got_gdk_events_message;
static HWND modal_win32_dialog = NULL;

124
#if 0
Tor Lillqvist's avatar
Tor Lillqvist committed
125
static HKL latin_locale = NULL;
126 127 128
#endif

static gboolean in_ime_composition = FALSE;
129
static UINT     modal_timer;
130
static UINT     sync_timer = 0;
Tor Lillqvist's avatar
Tor Lillqvist committed
131 132

static int debug_indent = 0;
133 134 135 136 137 138 139 140 141 142 143 144 145 146

static void
assign_object (gpointer lhsp,
	       gpointer rhs)
{
  if (*(gpointer *)lhsp != rhs)
    {
      if (*(gpointer *)lhsp != NULL)
	g_object_unref (*(gpointer *)lhsp);
      *(gpointer *)lhsp = rhs;
      if (rhs != NULL)
	g_object_ref (rhs);
    }
}
147

148
static void
Tor Lillqvist's avatar
Tor Lillqvist committed
149 150
track_mouse_event (DWORD dwFlags,
		   HWND  hwnd)
151 152 153
{
  typedef BOOL (WINAPI *PFN_TrackMouseEvent) (LPTRACKMOUSEEVENT);
  static PFN_TrackMouseEvent p_TrackMouseEvent = NULL;
Tor Lillqvist's avatar
Tor Lillqvist committed
154
  static gboolean once = FALSE;
155 156 157 158 159 160 161 162 163 164

  if (!once)
    {
      HMODULE user32;
      HINSTANCE commctrl32;

      user32 = GetModuleHandle ("user32.dll");
      if ((p_TrackMouseEvent = (PFN_TrackMouseEvent)GetProcAddress (user32, "TrackMouseEvent")) == NULL)
        {
          if ((commctrl32 = LoadLibrary ("commctrl32.dll")) != NULL)
Tor Lillqvist's avatar
Tor Lillqvist committed
165 166
	    p_TrackMouseEvent = (PFN_TrackMouseEvent)
	      GetProcAddress (commctrl32, "_TrackMouseEvent");
167
        }
Tor Lillqvist's avatar
Tor Lillqvist committed
168
      once = TRUE;
169 170 171 172 173 174 175 176 177 178 179 180
    }

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

      if (!p_TrackMouseEvent (&tme))
        WIN32_API_FAILED ("TrackMouseEvent");
Tor Lillqvist's avatar
Tor Lillqvist committed
181 182 183 184
      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));
185 186 187
    }
}

188 189 190 191 192 193 194 195
gulong
_gdk_win32_get_next_tick (gulong suggested_tick)
{
  static gulong cur_tick = 0;

  if (suggested_tick == 0)
    suggested_tick = GetTickCount ();
  if (suggested_tick <= cur_tick)
196
    return cur_tick;
197 198 199
  else
    return cur_tick = suggested_tick;
}
Tor Lillqvist's avatar
Tor Lillqvist committed
200

201 202 203 204 205 206 207 208 209 210
static void
generate_focus_event (GdkWindow *window,
		      gboolean   in)
{
  GdkEvent *event;

  event = gdk_event_new (GDK_FOCUS_CHANGE);
  event->focus_change.window = window;
  event->focus_change.in = in;

211
  append_event (event);
212 213
}

214 215 216 217 218 219 220 221 222 223 224 225 226
static void
generate_grab_broken_event (GdkWindow *window,
			    gboolean   keyboard,
			    GdkWindow *grab_window)
{
  GdkEvent *event = gdk_event_new (GDK_GRAB_BROKEN);

  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;
	  
227
  append_event (event);
228 229
}

230
static LRESULT 
Tor Lillqvist's avatar
Tor Lillqvist committed
231 232 233 234
inner_window_procedure (HWND   hwnd,
			UINT   message,
			WPARAM wparam,
			LPARAM lparam)
235 236 237
{
  MSG msg;
  DWORD pos;
238
  gint ret_val = 0;
239

240
  msg.hwnd = hwnd;
241
  msg.message = message;
242 243
  msg.wParam = wparam;
  msg.lParam = lparam;
244
  msg.time = _gdk_win32_get_next_tick (0);
245
  pos = GetMessagePos ();
246 247
  msg.pt.x = GET_X_LPARAM (pos);
  msg.pt.y = GET_Y_LPARAM (pos);
248

249
  if (gdk_event_translate (&msg, &ret_val))
250
    {
251 252 253
      /* If gdk_event_translate() returns TRUE, we return ret_val from
       * the window procedure.
       */
254
      if (modal_win32_dialog)
255 256
	PostMessageW (modal_win32_dialog, got_gdk_events_message,
		      (WPARAM) 1, 0);
257
      return ret_val;
258 259 260
    }
  else
    {
261
      /* Otherwise call DefWindowProcW(). */
262
      GDK_NOTE (EVENTS, g_print (" DefWindowProcW"));
263
      return DefWindowProcW (hwnd, message, wparam, lparam);
264 265 266
    }
}

267
LRESULT CALLBACK
268 269 270 271
_gdk_win32_window_procedure (HWND   hwnd,
                             UINT   message,
                             WPARAM wparam,
                             LPARAM lparam)
272 273 274
{
  LRESULT retval;

Tor Lillqvist's avatar
Tor Lillqvist committed
275 276 277 278 279 280 281
  GDK_NOTE (EVENTS, g_print ("%s%*s%s %p",
			     (debug_indent > 0 ? "\n" : ""),
			     debug_indent, "", 
			     _gdk_win32_message_to_string (message), hwnd));
  debug_indent += 2;
  retval = inner_window_procedure (hwnd, message, wparam, lparam);
  debug_indent -= 2;
282

283
  GDK_NOTE (EVENTS, g_print (" => %I64d%s", (gint64) retval, (debug_indent == 0 ? "\n" : "")));
284 285 286 287

  return retval;
}

288
void 
289
_gdk_events_init (void)
290
{
291
  GSource *source;
292 293

#if 0
Tor Lillqvist's avatar
Tor Lillqvist committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  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,
328 329 330 331
    LANG_FAEROESE
#ifdef LANG_SWAHILI
   ,LANG_SWAHILI
#endif
Tor Lillqvist's avatar
Tor Lillqvist committed
332
  };
333
#endif
334

335
  client_message = RegisterWindowMessage ("GDK_WIN32_CLIENT_MESSAGE");
336
  got_gdk_events_message = RegisterWindowMessage ("GDK_WIN32_GOT_EVENTS");
337 338

#if 0
Tor Lillqvist's avatar
Tor Lillqvist committed
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
  /* 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];
371
	  g_sprintf (id, "%08x", MAKELANGID (latin_languages[i++], SUBLANG_DEFAULT));
Tor Lillqvist's avatar
Tor Lillqvist committed
372 373 374 375 376
	  latin_locale = LoadKeyboardLayout (id, KLF_NOTELLSHELL|KLF_SUBSTITUTE_OK);
	}
    }

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

379 380
  source = g_source_new (&event_funcs, sizeof (GSource));
  g_source_set_priority (source, GDK_PRIORITY_EVENTS);
381

382 383 384 385 386
#ifdef G_WITH_CYGWIN
  event_poll_fd.fd = open ("/dev/windows", O_RDONLY);
  if (event_poll_fd.fd == -1)
    g_error ("can't open \"/dev/windows\": %s", g_strerror (errno));
#else
387
  event_poll_fd.fd = G_WIN32_MSG_HANDLE;
388
#endif
389 390
  event_poll_fd.events = G_IO_IN;
  
391 392 393
  g_source_add_poll (source, &event_poll_fd);
  g_source_set_can_recurse (source, TRUE);
  g_source_attach (source, NULL);
394 395 396 397 398 399
}

gboolean
gdk_events_pending (void)
{
  MSG msg;
400
  return (_gdk_event_queue_find_first (_gdk_display) ||
401
	  (modal_win32_dialog == NULL &&
402
	   PeekMessageW (&msg, NULL, 0, 0, PM_NOREMOVE)));
403 404 405 406 407
}

GdkEvent*
gdk_event_get_graphics_expose (GdkWindow *window)
{
408
  MSG msg;
409
  GdkEvent *event = NULL;
410 411 412 413 414

  g_return_val_if_fail (window != NULL, NULL);
  
  GDK_NOTE (EVENTS, g_print ("gdk_event_get_graphics_expose\n"));

415
  if (PeekMessageW (&msg, GDK_WINDOW_HWND (window), WM_PAINT, WM_PAINT, PM_REMOVE))
416
    {
417 418
      handle_wm_paint (&msg, window, TRUE, &event);
      if (event != NULL)
419 420 421 422
	{
	  GDK_NOTE (EVENTS, g_print ("gdk_event_get_graphics_expose: got it!\n"));
	  return event;
	}
423 424
    }
  
425
  GDK_NOTE (EVENTS, g_print ("gdk_event_get_graphics_expose: nope\n"));
426 427 428
  return NULL;	
}

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

431 432 433 434 435 436 437 438 439
static char *
event_mask_string (GdkEventMask mask)
{
  static char bfr[500];
  char *p = bfr;

  *p = '\0';
#define BIT(x) \
  if (mask & GDK_##x##_MASK) \
440
    p += g_sprintf (p, "%s" #x, (p > bfr ? " " : ""))
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
  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);
462 463 464 465 466
#undef BIT

  return bfr;
}

467 468
#endif

469
GdkGrabStatus
470 471 472 473 474 475 476
_gdk_windowing_pointer_grab (GdkWindow    *window,
			     GdkWindow    *native_window,
			     gboolean	owner_events,
			     GdkEventMask	event_mask,
			     GdkWindow    *confine_to,
			     GdkCursor    *cursor,
			     guint32	time)
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 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
  HCURSOR hcursor;
  GdkCursorPrivate *cursor_private;
  gint return_val;

  g_return_val_if_fail (window != NULL, 0);
  g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
  g_return_val_if_fail (confine_to == NULL || GDK_IS_WINDOW (confine_to), 0);
  
  cursor_private = (GdkCursorPrivate*) cursor;
  
  if (!cursor)
    hcursor = NULL;
  else if ((hcursor = CopyCursor (cursor_private->hcursor)) == NULL)
    WIN32_API_FAILED ("CopyCursor");

  return_val = _gdk_input_grab_pointer (native_window,
					owner_events,
					event_mask,
					confine_to,
					time);

  if (return_val == GDK_GRAB_SUCCESS)
    {
      GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (((GdkWindowObject *) native_window)->impl);

      SetCapture (GDK_WINDOW_HWND (native_window));
      /* TODO_CSW: grab brokens, confine window, input_grab */
      if (p_grab_cursor != NULL)
	{
	  if (GetCursor () == p_grab_cursor)
	    SetCursor (NULL);
	  DestroyCursor (p_grab_cursor);
	}

      p_grab_cursor = hcursor;

      if (p_grab_cursor != NULL)
	SetCursor (p_grab_cursor);
      else if (impl->hcursor != NULL)
	SetCursor (impl->hcursor);
      else
	SetCursor (LoadCursor (NULL, IDC_ARROW));

    }

  return return_val;
524 525 526
}

void
527 528
gdk_display_pointer_ungrab (GdkDisplay *display,
                            guint32     time)
529
{
Cody Russell's avatar
grabs  
Cody Russell committed
530
  GdkPointerGrabInfo *info;
531

Cody Russell's avatar
grabs  
Cody Russell committed
532 533
  info = _gdk_display_get_last_pointer_grab (display);
  if (info)
534
    {
535 536
      info->serial_end = 0;
      ReleaseCapture ();
537
    }
538
  /* TODO_CSW: cursor, confines, etc */
539

540
  _gdk_display_pointer_grab_update (display, 0);
Cody Russell's avatar
grabs  
Cody Russell committed
541
}
542

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

static GdkWindow *
find_window_for_mouse_event (GdkWindow* reported_window,
			     MSG*       msg)
{
  HWND hwnd;
  POINTS points;
  POINT pt;
  GdkWindow* other_window = NULL;

  if (!_gdk_display_get_last_pointer_grab (_gdk_display))
    return reported_window;

  points = MAKEPOINTS (msg->lParam);
  pt.x = points.x;
  pt.y = points.y;
  ClientToScreen (msg->hwnd, &pt);

  hwnd = WindowFromPoint (pt);

  if (hwnd != NULL)
    {
      RECT rect;

      GetClientRect (hwnd, &rect);
      ScreenToClient (hwnd, &pt);
      if (!PtInRect (&rect, pt))
	return _gdk_root;

      other_window = gdk_win32_handle_table_lookup ((GdkNativeWindow) hwnd);
    }

  if (other_window == NULL)
    return _gdk_root;

  /* need to also adjust the coordinates to the new window */
  pt.x = points.x;
  pt.y = points.y;
  ClientToScreen (msg->hwnd, &pt);
  ScreenToClient (GDK_WINDOW_HWND (other_window), &pt);
  /* ATTENTION: need to update client coords */
  msg->lParam = MAKELPARAM (pt.x, pt.y);

  return other_window;
}

589
GdkGrabStatus
590 591 592
gdk_keyboard_grab (GdkWindow *window,
		   gboolean   owner_events,
		   guint32    time)
593
{
Cody Russell's avatar
grabs  
Cody Russell committed
594 595
  GdkDisplay *display;
  GdkWindow  *toplevel;
596

597 598 599
  g_return_val_if_fail (window != NULL, 0);
  g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
  
600 601
  GDK_NOTE (EVENTS, g_print ("gdk_keyboard_grab %p%s\n",
			     GDK_WINDOW_HWND (window), owner_events ? " OWNER_EVENTS" : ""));
602

Cody Russell's avatar
grabs  
Cody Russell committed
603 604
  display = gdk_drawable_get_display (window);
  toplevel = gdk_window_get_toplevel (window);
605

Cody Russell's avatar
grabs  
Cody Russell committed
606 607 608 609 610 611
  _gdk_display_set_has_keyboard_grab (display,
				      window,
				      toplevel,
				      owner_events,
				      0,
				      time);
612

Cody Russell's avatar
grabs  
Cody Russell committed
613
  return GDK_GRAB_SUCCESS;
614 615 616
}

void
617 618
gdk_display_keyboard_ungrab (GdkDisplay *display,
                             guint32 time)
619
{
620
  GDK_NOTE (EVENTS, g_print ("gdk_display_keyboard_ungrab\n"));
Cody Russell's avatar
grabs  
Cody Russell committed
621
  _gdk_display_unset_has_keyboard_grab (display, FALSE);
622 623
}

Tor Lillqvist's avatar
Tor Lillqvist committed
624 625 626 627 628 629 630 631 632 633
void 
gdk_display_add_client_message_filter (GdkDisplay   *display,
				       GdkAtom       message_type,
				       GdkFilterFunc func,
				       gpointer      data)
{
  /* XXX */
  gdk_add_client_message_filter (message_type, func, data);
}

Cody Russell's avatar
grabs  
Cody Russell committed
634
void
635 636 637 638 639 640 641 642 643 644
gdk_add_client_message_filter (GdkAtom       message_type,
			       GdkFilterFunc func,
			       gpointer      data)
{
  GdkClientFilter *filter = g_new (GdkClientFilter, 1);

  filter->type = message_type;
  filter->function = func;
  filter->data = data;
  
645
  client_filters = g_list_append (client_filters, filter);
646 647 648
}

static void
649 650
build_key_event_state (GdkEvent *event,
		       BYTE     *key_state)
651
{
652 653 654
  event->key.state = 0;

  if (key_state[VK_SHIFT] & 0x80)
655
    event->key.state |= GDK_SHIFT_MASK;
656 657

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

660 661 662 663 664 665 666 667 668 669 670
  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;

671 672 673
  if (_gdk_keyboard_has_altgr &&
      (key_state[VK_LCONTROL] & 0x80) &&
      (key_state[VK_RMENU] & 0x80))
674
    {
675 676 677
      event->key.group = 1;
      event->key.state |= GDK_MOD2_MASK;
      if (key_state[VK_RCONTROL] & 0x80)
678
	event->key.state |= GDK_CONTROL_MASK;
679
      if (key_state[VK_LMENU] & 0x80)
680 681
	event->key.state |= GDK_MOD1_MASK;
    }
682
  else
683
    {
684 685 686 687 688
      event->key.group = 0;
      if (key_state[VK_CONTROL] & 0x80)
	event->key.state |= GDK_CONTROL_MASK;
      if (key_state[VK_MENU] & 0x80)
	event->key.state |= GDK_MOD1_MASK;
689
    }
690 691 692
}

static gint
693
build_pointer_event_state (MSG *msg)
694 695 696 697
{
  gint state;
  
  state = 0;
698

699
  if (msg->wParam & MK_CONTROL)
700
    state |= GDK_CONTROL_MASK;
701 702 703 704

  if ((msg->message != WM_LBUTTONDOWN &&
       (msg->wParam & MK_LBUTTON)) ||
      msg->message == WM_LBUTTONUP)
705
    state |= GDK_BUTTON1_MASK;
706 707 708 709

  if ((msg->message != WM_MBUTTONDOWN &&
       (msg->wParam & MK_MBUTTON)) ||
      msg->message == WM_MBUTTONUP)
710
    state |= GDK_BUTTON2_MASK;
711 712 713 714

  if ((msg->message != WM_RBUTTONDOWN &&
       (msg->wParam & MK_RBUTTON)) ||
      msg->message == WM_RBUTTONUP)
715
    state |= GDK_BUTTON3_MASK;
716 717 718 719

  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
720
    state |= GDK_BUTTON4_MASK;
721 722 723 724

  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
725
    state |= GDK_BUTTON5_MASK;
726

727
  if (msg->wParam & MK_SHIFT)
728
    state |= GDK_SHIFT_MASK;
729

730 731
  if (GetKeyState (VK_MENU) < 0)
    state |= GDK_MOD1_MASK;
732

733 734 735 736 737 738 739
  if (GetKeyState (VK_CAPITAL) & 0x1)
    state |= GDK_LOCK_MASK;

  return state;
}

static void
740 741 742 743
build_wm_ime_composition_event (GdkEvent *event,
				MSG      *msg,
				wchar_t   wc,
				BYTE     *key_state)
744
{
745
  event->key.time = _gdk_win32_get_next_tick (msg->time);
746
  
747
  build_key_event_state (event, key_state);
748

749
  event->key.hardware_keycode = 0; /* FIXME: What should it be? */
750 751
  event->key.string = NULL;
  event->key.length = 0;
752
  event->key.keyval = gdk_unicode_to_keyval (wc);
753 754
}

755 756
#ifdef G_ENABLE_DEBUG

757
static void
758
print_event_state (guint state)
759
{
760 761 762 763 764
#define CASE(bit) if (state & GDK_ ## bit ## _MASK) g_print (#bit " ");
  CASE (SHIFT);
  CASE (LOCK);
  CASE (CONTROL);
  CASE (MOD1);
765 766 767 768
  CASE (MOD2);
  CASE (MOD3);
  CASE (MOD4);
  CASE (MOD5);
769 770 771
  CASE (BUTTON1);
  CASE (BUTTON2);
  CASE (BUTTON3);
772 773
  CASE (BUTTON4);
  CASE (BUTTON5);
774 775 776
#undef CASE
}

777 778
void
_gdk_win32_print_event (const GdkEvent *event)
779 780
{
  gchar *escaped, *kvname;
781
  gchar *selection_name, *target_name, *property_name;
782

Tor Lillqvist's avatar
Tor Lillqvist committed
783
  g_print ("%s%*s===> ", (debug_indent > 0 ? "\n" : ""), debug_indent, "");
784 785
  switch (event->any.type)
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
786
#define CASE(x) case x: g_print (#x); break;
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
    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_NO_EXPOSE);
    CASE (GDK_SCROLL);
    CASE (GDK_WINDOW_STATE);
821
    CASE (GDK_SETTING);
822 823
    CASE (GDK_OWNER_CHANGE);
    CASE (GDK_GRAB_BROKEN);
824
#undef CASE
Tor Lillqvist's avatar
Tor Lillqvist committed
825
    default: g_assert_not_reached ();
826
    }
827

Tor Lillqvist's avatar
Tor Lillqvist committed
828
  g_print (" %p ", GDK_WINDOW_HWND (event->any.window));
829 830 831 832

  switch (event->any.type)
    {
    case GDK_EXPOSE:
833 834
      g_print ("%s %d",
	       _gdk_win32_gdkrectangle_to_string (&event->expose.area),
835 836 837
	       event->expose.count);
      break;
    case GDK_MOTION_NOTIFY:
838
      g_print ("(%.4g,%.4g) (%.4g,%.4g) %s",
839
	       event->motion.x, event->motion.y,
840
	       event->motion.x_root, event->motion.y_root,
841 842 843 844 845 846 847
	       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
848
      g_print ("%d (%.4g,%.4g) (%.4g,%.4g) ",
849
	       event->button.button,
850 851
	       event->button.x, event->button.y,
	       event->button.x_root, event->button.y_root);
852 853 854 855 856 857 858 859 860
      print_event_state (event->button.state);
      break;
    case GDK_KEY_PRESS: 
    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);
861
      g_print ("%#.02x group:%d %s %d:\"%s\" ",
862
	       event->key.hardware_keycode, event->key.group,
863 864 865 866 867 868 869 870
	       (kvname ? kvname : "??"),
	       event->key.length,
	       escaped);
      g_free (escaped);
      print_event_state (event->key.state);
      break;
    case GDK_ENTER_NOTIFY:
    case GDK_LEAVE_NOTIFY:
871 872 873 874
      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,
875 876 877 878 879 880 881 882 883 884 885 886
	       (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" : "");
887
      print_event_state (event->crossing.state);
888
      break;
Tor Lillqvist's avatar
Tor Lillqvist committed
889 890 891
    case GDK_FOCUS_CHANGE:
      g_print ("%s", (event->focus_change.in ? "IN" : "OUT"));
      break;
892 893 894 895 896 897 898 899 900 901 902 903
    case GDK_SELECTION_REQUEST:
    case GDK_SELECTION_NOTIFY:
    case GDK_SELECTION_CLEAR:
      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;
Tor Lillqvist's avatar
Tor Lillqvist committed
904 905 906 907 908
    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;
909 910 911 912 913 914 915 916 917 918
    case GDK_CLIENT_EVENT:
      g_print ("%s %d %ld %ld %ld %ld %ld",
	       gdk_atom_name (event->client.message_type),
	       event->client.data_format,
	       event->client.data.l[0],
	       event->client.data.l[1],
	       event->client.data.l[2],
	       event->client.data.l[3],
	       event->client.data.l[4]);
      break;
919
    case GDK_SCROLL:
920
      g_print ("(%.4g,%.4g) (%.4g,%.4g) %s ",
921
	       event->scroll.x, event->scroll.y,
922
	       event->scroll.x_root, event->scroll.y_root,
923 924 925 926 927 928 929
	       (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;
930
    case GDK_WINDOW_STATE:
Tor Lillqvist's avatar
Tor Lillqvist committed
931 932 933
      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));
934 935 936 937 938 939 940 941 942 943 944 945
    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));
946 947 948
    default:
      /* Nothing */
      break;
949
    }  
Tor Lillqvist's avatar
Tor Lillqvist committed
950
  g_print ("%s", (debug_indent == 0 ? "\n" : "")); 
951 952
}

953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
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;
}

972 973
#endif

974 975 976 977 978 979 980 981 982 983 984 985 986
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);
  event->any.send_event = InSendMessage (); 
}

static void
987
append_event (GdkEvent *event)
988
{
989 990
  GList *link;
  
991
  fixup_event (event);
992
#if 1
993
  link = _gdk_event_queue_append (_gdk_display, event);
994
  GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
995
  /* event morphing, the passed in may not be valid afterwards */
996
  _gdk_windowing_got_event (_gdk_display, link, event, 0);
997 998
#else
  _gdk_event_queue_append (_gdk_display, event);
999
  GDK_NOTE (EVENTS, _gdk_win32_print_event (event));
1000
#endif
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
}

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

  /* Fill in event->string crudely, since various programs
   * depend on it.
   */
  
  c = 0;
  if (event->key.keyval != GDK_VoidSymbol)
    c = gdk_keyval_to_unicode (event->key.keyval);

  if (c)
    {
      gsize bytes_written;
      gint len;
      
      /* 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;
	}
      
      len = g_unichar_to_utf8 (c, buf);
      buf[len] = '\0';
	  
      event->key.string = g_locale_from_utf8 (buf, len,
					      NULL, &bytes_written,
					      NULL);
      if (event->key.string)
	event->key.length = bytes_written;
    }
  else if (event->key.keyval == GDK_Escape)
    {
      event->key.length = 1;
      event->key.string = g_strdup ("\033");
    }
  else if (event->key.keyval == GDK_Return ||
	   event->key.keyval == GDK_KP_Enter)
    {
      event->key.length = 1;
      event->key.string = g_strdup ("\r");
    }
  
  if (!event->key.string)
    {
      event->key.length = 0;
      event->key.string = g_strdup ("");
    }
}

static GdkFilterReturn
1071 1072 1073
apply_event_filters (GdkWindow  *window,
		     MSG        *msg,
		     GList      *filters)
1074
{
1075 1076
  GdkFilterReturn result = GDK_FILTER_CONTINUE;
  GdkEvent *event;
1077
  GList *node;
1078
  GList *tmp_list;
1079

1080
  event = gdk_event_new (GDK_NOTHING);