gdkevents-win32.c 94.4 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 5
 *
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7 8 9 10 11 12
 * 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
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16 17 18 19 20 21
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
22
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23 24 25 26 27 28 29 30 31 32 33
 * 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.
34 35 36 37 38 39
 *
 * 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
40 41
 */

42
#include <config.h>
43

44
#include <glib/gprintf.h>
45

46 47 48 49 50 51
#if defined (__GNUC__) && defined (HAVE_DIMM_H)
/* The w32api imm.h clashes a bit with the IE5.5 dimm.h */
# define IMEMENUITEMINFOA hidden_IMEMENUITEMINFOA
# define IMEMENUITEMINFOW hidden_IMEMENUITEMINFOW
#endif

52
#include "gdk.h"
53
#include "gdkprivate-win32.h"
54 55
#include "gdkinput-win32.h"
#include "gdkkeysyms.h"
56

57 58
#include <windowsx.h>

59 60 61 62 63
#ifdef G_WITH_CYGWIN
#include <fcntl.h>
#include <errno.h>
#endif

64
#include <objbase.h>
65

66 67
#include <imm.h>

68 69 70 71 72
#if defined (__GNUC__) && defined (HAVE_DIMM_H)
# undef IMEMENUITEMINFOA
# undef IMEMENUITEMINFOW
#endif

73 74 75 76
#ifdef HAVE_DIMM_H
#include <dimm.h>
#endif

Tor Lillqvist's avatar
Tor Lillqvist committed
77 78 79 80 81
#ifndef XBUTTON1
#define XBUTTON1 1
#define XBUTTON2 2
#endif

82 83 84 85 86
#ifndef VK_XBUTTON1
#define VK_XBUTTON1 5
#define VK_XBUTTON2 6
#endif

Tor Lillqvist's avatar
Tor Lillqvist committed
87 88 89 90 91
#ifndef MK_XBUTTON1
#define MK_XBUTTON1 32
#define MK_XBUTTON2 64
#endif

92 93 94 95 96
/* 
 * Private function declarations
 */

static GdkFilterReturn
97
		 gdk_event_apply_filters(MSG      *msg,
98 99
					 GdkEvent *event,
					 GList    *filters);
100
static gboolean  gdk_event_translate	(GdkDisplay *display,
101
					 MSG	    *msg,
Tor Lillqvist's avatar
Tor Lillqvist committed
102
					 gint       *ret_valp);
103 104 105 106
static void      handle_wm_paint        (MSG        *msg,
					 GdkWindow  *window,
					 gboolean    return_exposes,
					 GdkEvent  **event);
107 108 109 110 111 112 113

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

115 116 117
static void append_event (GdkDisplay *display,
			  GdkEvent   *event);

118 119 120
/* Private variable declarations
 */

121 122
static GdkWindow *p_grab_window = NULL; /* Window that currently holds
					 * the pointer grab
123 124
					 */

125 126
static GdkWindow *p_grab_confine_to = NULL;

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
static GdkWindow *k_grab_window = NULL; /* Window the holds the
					 * keyboard grab
					 */

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

static gboolean p_grab_automatic;
static GdkEventMask p_grab_mask;
static gboolean p_grab_owner_events, k_grab_owner_events;
static HCURSOR p_grab_cursor;

static GSourceFuncs event_funcs = {
  gdk_event_prepare,
  gdk_event_check,
  gdk_event_dispatch,
142
  NULL
143 144 145 146
};

GPollFD event_poll_fd;

147 148
static GdkWindow *current_window = NULL;
static gint current_x, current_y;
149
#if 0
150
static UINT gdk_ping_msg;
151 152 153
#endif
static UINT msh_mousewheel;
static UINT client_message;
154

155
#ifdef HAVE_DIMM_H
156 157
static IActiveIMMApp *active_imm_app = NULL;
static IActiveIMMMessagePumpOwner *active_imm_msgpump_owner = NULL;
158
#endif
159

160
#if 0
Tor Lillqvist's avatar
Tor Lillqvist committed
161
static HKL latin_locale = NULL;
162 163 164
#endif

static gboolean in_ime_composition = FALSE;
165
static UINT     resize_timer;
Tor Lillqvist's avatar
Tor Lillqvist committed
166 167

static int debug_indent = 0;
168 169 170 171 172 173 174 175 176 177 178 179 180 181

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);
    }
}
182

183
static void
Tor Lillqvist's avatar
Tor Lillqvist committed
184 185
track_mouse_event (DWORD dwFlags,
		   HWND  hwnd)
186 187 188
{
  typedef BOOL (WINAPI *PFN_TrackMouseEvent) (LPTRACKMOUSEEVENT);
  static PFN_TrackMouseEvent p_TrackMouseEvent = NULL;
Tor Lillqvist's avatar
Tor Lillqvist committed
189
  static gboolean once = FALSE;
190 191 192 193 194 195 196 197 198 199

  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
200 201
	    p_TrackMouseEvent = (PFN_TrackMouseEvent)
	      GetProcAddress (commctrl32, "_TrackMouseEvent");
202
        }
Tor Lillqvist's avatar
Tor Lillqvist committed
203
      once = TRUE;
204 205 206 207 208 209 210 211 212 213 214 215
    }

  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
216 217 218 219
      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));
220 221 222
    }
}

223 224 225 226 227 228 229 230
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)
231
    return cur_tick;
232 233 234
  else
    return cur_tick = suggested_tick;
}
Tor Lillqvist's avatar
Tor Lillqvist committed
235

236 237 238 239 240 241 242 243 244 245 246 247 248
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;

  append_event (gdk_drawable_get_display (window), event);
}

249
static LRESULT 
Tor Lillqvist's avatar
Tor Lillqvist committed
250 251 252 253
inner_window_procedure (HWND   hwnd,
			UINT   message,
			WPARAM wparam,
			LPARAM lparam)
254
{
Owen Taylor's avatar
Owen Taylor committed
255
  GdkDisplay *display = gdk_display_get_default ();
256 257
  MSG msg;
  DWORD pos;
258
#ifdef HAVE_DIMM_H
259
  LRESULT lres;
260
#endif
261
  gint ret_val = 0;
262

263
  msg.hwnd = hwnd;
264
  msg.message = message;
265 266
  msg.wParam = wparam;
  msg.lParam = lparam;
267
  msg.time = _gdk_win32_get_next_tick (0);
268
  pos = GetMessagePos ();
269 270
  msg.pt.x = GET_X_LPARAM (pos);
  msg.pt.y = GET_Y_LPARAM (pos);
271

Tor Lillqvist's avatar
Tor Lillqvist committed
272
  if (gdk_event_translate (display, &msg, &ret_val))
273
    {
274 275 276 277
      /* If gdk_event_translate() returns TRUE, we return ret_val from
       * the window procedure.
       */
      return ret_val;
278 279 280
    }
  else
    {
281
      /* Otherwise call DefWindowProc(). */
Tor Lillqvist's avatar
Tor Lillqvist committed
282
      GDK_NOTE (EVENTS, g_print (" DefWindowProc"));
283 284 285
#ifndef HAVE_DIMM_H
      return DefWindowProc (hwnd, message, wparam, lparam);
#else
286 287
      if (active_imm_app == NULL ||
	  (*active_imm_app->lpVtbl->OnDefWindowProc) (active_imm_app, hwnd, message, wparam, lparam, &lres) == S_FALSE)
288
	return DefWindowProc (hwnd, message, wparam, lparam);
289 290
      else
	return lres;
291
#endif
292 293 294
    }
}

295
LRESULT CALLBACK
296 297 298 299
_gdk_win32_window_procedure (HWND   hwnd,
                             UINT   message,
                             WPARAM wparam,
                             LPARAM lparam)
300 301 302
{
  LRESULT retval;

Tor Lillqvist's avatar
Tor Lillqvist committed
303 304 305 306 307 308 309
  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;
310

Tor Lillqvist's avatar
Tor Lillqvist committed
311 312
  GDK_NOTE (EVENTS, g_print (" => %ld%s",
			     retval, (debug_indent == 0 ? "\n" : "")));
313 314 315 316

  return retval;
}

317
void 
318
_gdk_events_init (void)
319
{
320
  GSource *source;
321
#ifdef HAVE_DIMM_H
322
  HRESULT hres;
323
#endif
324 325

#if 0
Tor Lillqvist's avatar
Tor Lillqvist committed
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
  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,
360 361 362 363
    LANG_FAEROESE
#ifdef LANG_SWAHILI
   ,LANG_SWAHILI
#endif
Tor Lillqvist's avatar
Tor Lillqvist committed
364
  };
365
#endif
366

367
#if 0
368
  gdk_ping_msg = RegisterWindowMessage ("gdk-ping");
Tor Lillqvist's avatar
Tor Lillqvist committed
369
  GDK_NOTE (EVENTS, g_print ("gdk-ping = %#x\n", gdk_ping_msg));
370
#endif
Tor Lillqvist's avatar
Tor Lillqvist committed
371 372 373 374 375 376

  /* This is the string MSH_MOUSEWHEEL from zmouse.h,
   * http://www.microsoft.com/mouse/intellimouse/sdk/zmouse.h
   * This message is used by mouse drivers than cannot generate WM_MOUSEWHEEL
   * or on Win95.
   */
377
  msh_mousewheel = RegisterWindowMessage ("MSWHEEL_ROLLMSG");
378

379 380 381
  client_message = RegisterWindowMessage ("GDK_WIN32_CLIENT_MESSAGE");

#if 0
Tor Lillqvist's avatar
Tor Lillqvist committed
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
  /* 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];
414
	  g_sprintf (id, "%08x", MAKELANGID (latin_languages[i++], SUBLANG_DEFAULT));
Tor Lillqvist's avatar
Tor Lillqvist committed
415 416 417 418 419
	  latin_locale = LoadKeyboardLayout (id, KLF_NOTELLSHELL|KLF_SUBSTITUTE_OK);
	}
    }

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

422 423
  source = g_source_new (&event_funcs, sizeof (GSource));
  g_source_set_priority (source, GDK_PRIORITY_EVENTS);
424

425 426 427 428 429
#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
430
  event_poll_fd.fd = G_WIN32_MSG_HANDLE;
431
#endif
432 433
  event_poll_fd.events = G_IO_IN;
  
434 435 436 437
  g_source_add_poll (source, &event_poll_fd);
  g_source_set_can_recurse (source, TRUE);
  g_source_attach (source, NULL);

438
#ifdef HAVE_DIMM_H
439 440 441 442
  hres = CoCreateInstance (&CLSID_CActiveIMM,
			   NULL,
			   CLSCTX_ALL,
			   &IID_IActiveIMMApp,
443
			   (LPVOID *) &active_imm_app);
444 445 446
  
  if (hres == S_OK)
    {
447
      GDK_NOTE (EVENTS, g_print ("IActiveIMMApp created %p\n",
448 449
				 active_imm_app));
      (*active_imm_app->lpVtbl->Activate) (active_imm_app, TRUE);
450
      
Tor Lillqvist's avatar
Tor Lillqvist committed
451
      hres = (*active_imm_app->lpVtbl->QueryInterface) (active_imm_app, &IID_IActiveIMMMessagePumpOwner, (void **) &active_imm_msgpump_owner);
452
      GDK_NOTE (EVENTS, g_print ("IActiveIMMMessagePumpOwner created %p\n",
453 454
				 active_imm_msgpump_owner));
      (active_imm_msgpump_owner->lpVtbl->Start) (active_imm_msgpump_owner);
455
    }
456
#endif
457 458 459 460 461 462
}

gboolean
gdk_events_pending (void)
{
  MSG msg;
Owen Taylor's avatar
Owen Taylor committed
463
  GdkDisplay *display = gdk_display_get_default ();
464

465
  return (_gdk_event_queue_find_first (display) ||
466
	  PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE));
467 468 469 470 471
}

GdkEvent*
gdk_event_get_graphics_expose (GdkWindow *window)
{
472
  MSG msg;
473
  GdkEvent *event = NULL;
474 475 476 477 478

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

479
  if (PeekMessage (&msg, GDK_WINDOW_HWND (window), WM_PAINT, WM_PAINT, PM_REMOVE))
480
    {
481 482
      handle_wm_paint (&msg, window, TRUE, &event);
      if (event != NULL)
483 484 485 486
	{
	  GDK_NOTE (EVENTS, g_print ("gdk_event_get_graphics_expose: got it!\n"));
	  return event;
	}
487 488
    }
  
489
  GDK_NOTE (EVENTS, g_print ("gdk_event_get_graphics_expose: nope\n"));
490 491 492
  return NULL;	
}

493 494 495 496 497 498 499 500 501
static char *
event_mask_string (GdkEventMask mask)
{
  static char bfr[500];
  char *p = bfr;

  *p = '\0';
#define BIT(x) \
  if (mask & GDK_##x##_MASK) \
502
    p += g_sprintf (p, "%s" #x, (p > bfr ? " " : ""))
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
  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);
524 525 526 527 528
#undef BIT

  return bfr;
}

529
GdkGrabStatus
530 531 532 533 534 535
gdk_pointer_grab (GdkWindow    *window,
		  gboolean	owner_events,
		  GdkEventMask	event_mask,
		  GdkWindow    *confine_to,
		  GdkCursor    *cursor,
		  guint32	time)
536
{
537
  HCURSOR hcursor;
538
  GdkCursorPrivate *cursor_private;
539
  gint return_val = GDK_GRAB_SUCCESS;
540 541 542 543 544 545 546 547

  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)
548
    hcursor = NULL;
549 550
  else if ((hcursor = CopyCursor (cursor_private->hcursor)) == NULL)
    WIN32_API_FAILED ("CopyCursor");
551

Tor Lillqvist's avatar
Tor Lillqvist committed
552 553 554 555 556
  return_val = _gdk_input_grab_pointer (window,
					owner_events,
					event_mask,
					confine_to,
					time);
557

558
  if (return_val == GDK_GRAB_SUCCESS)
559
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
560 561
      if (!GDK_WINDOW_DESTROYED (window))
	{
Tor Lillqvist's avatar
Tor Lillqvist committed
562 563
	  GDK_NOTE (EVENTS, g_print ("%sgdk_pointer_grab: %p %s %p %s%s",
				     (debug_indent > 0 ? "\n" : ""),
564
				     GDK_WINDOW_HWND (window),
Tor Lillqvist's avatar
Tor Lillqvist committed
565
				     (owner_events ? "TRUE" : "FALSE"),
566
				     hcursor,
Tor Lillqvist's avatar
Tor Lillqvist committed
567 568 569
				     event_mask_string (event_mask),
				     (debug_indent == 0 ? "\n" : "")));

Tor Lillqvist's avatar
Tor Lillqvist committed
570
	  p_grab_mask = event_mask;
571
	  p_grab_owner_events = owner_events;
Tor Lillqvist's avatar
Tor Lillqvist committed
572 573 574 575 576
	  p_grab_automatic = FALSE;
	  
	  SetCapture (GDK_WINDOW_HWND (window));
	  return_val = GDK_GRAB_SUCCESS;
	}
577
      else
578
	return_val = GDK_GRAB_ALREADY_GRABBED;
579 580
    }
  
581
  if (return_val == GDK_GRAB_SUCCESS)
582
    {
583
      GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (((GdkWindowObject *) window)->impl);
584
      p_grab_window = window;
585 586 587 588 589 590 591 592

      if (p_grab_cursor != NULL)
	{
	  if (GetCursor () == p_grab_cursor)
	    SetCursor (NULL);
	  DestroyCursor (p_grab_cursor);
	}

593
      p_grab_cursor = hcursor;
594 595 596

      if (p_grab_cursor != NULL)
	SetCursor (p_grab_cursor);
597 598 599 600
      else if (impl->hcursor != NULL)
	SetCursor (impl->hcursor);
      else
	SetCursor (LoadCursor (NULL, IDC_ARROW));
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618

      if (confine_to != NULL)
	{
	  gint x, y, width, height;
	  RECT rect;

	  gdk_window_get_origin (confine_to, &x, &y);
	  gdk_drawable_get_size (confine_to, &width, &height);

	  rect.left = x;
	  rect.top = y;
	  rect.right = x + width;
	  rect.bottom = y + height;
	  API_CALL (ClipCursor, (&rect));
	  p_grab_confine_to = confine_to;
	}

      /* FIXME: Generate GDK_CROSSING_GRAB events */
619 620 621 622 623 624
    }
  
  return return_val;
}

void
625 626
gdk_display_pointer_ungrab (GdkDisplay *display,
                            guint32     time)
627
{
Owen Taylor's avatar
Owen Taylor committed
628
  g_return_if_fail (display == gdk_display_get_default ());
629

Tor Lillqvist's avatar
Tor Lillqvist committed
630 631 632
  GDK_NOTE (EVENTS, g_print ("%sgdk_display_pointer_ungrab%s",
			     (debug_indent > 0 ? "\n" : ""),
			     (debug_indent == 0 ? "\n" : "")));
633

Tor Lillqvist's avatar
Tor Lillqvist committed
634
  _gdk_input_ungrab_pointer (time);
635

636 637
  if (GetCapture () != NULL)
    ReleaseCapture ();
638 639

  /* FIXME: Generate GDK_CROSSING_UNGRAB events */
640 641

  p_grab_window = NULL;
642 643 644 645 646 647 648
  if (p_grab_cursor != NULL)
    {
      if (GetCursor () == p_grab_cursor)
	SetCursor (NULL);
      DestroyCursor (p_grab_cursor);
      p_grab_cursor = NULL;
    }
649

650 651 652 653 654 655
  if (p_grab_confine_to != NULL)
    {
      API_CALL (ClipCursor, (NULL));
      p_grab_confine_to = NULL;
    }
}
656

657 658 659
static GdkWindow *
find_real_window_for_grabbed_mouse_event (GdkWindow* reported_window,
					  MSG*       msg)
660 661 662 663
{
  HWND hwnd;
  POINTS points;
  POINT pt;
664
  GdkWindow* other_window = NULL;
665

666
  points = MAKEPOINTS (msg->lParam);
667 668
  pt.x = points.x;
  pt.y = points.y;
669
  ClientToScreen (msg->hwnd, &pt);
670

671
  hwnd = WindowFromPoint (pt);
672

Tor Lillqvist's avatar
Tor Lillqvist committed
673
  if (hwnd != NULL)
674
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
675 676 677 678 679
      RECT rect;

      GetClientRect (hwnd, &rect);
      ScreenToClient (hwnd, &pt);
      if (!PtInRect (&rect, pt))
680
	return _gdk_root;
Tor Lillqvist's avatar
Tor Lillqvist committed
681 682

      other_window = gdk_win32_handle_table_lookup ((GdkNativeWindow) hwnd);
683
    }
684

Tor Lillqvist's avatar
Tor Lillqvist committed
685
  if (other_window == NULL)
686
    return _gdk_root;
687 688 689 690

  return other_window;
}

691 692 693 694 695 696 697 698 699 700
static GdkWindow* 
find_window_for_mouse_event (GdkWindow* reported_window,
			     MSG*       msg)
{
  if (p_grab_window == NULL || !p_grab_owner_events)
    return reported_window;
  else
    return find_real_window_for_grabbed_mouse_event (reported_window, msg);
}

701
gboolean
702
gdk_display_pointer_is_grabbed (GdkDisplay *display)
703
{
Owen Taylor's avatar
Owen Taylor committed
704
  g_return_val_if_fail (display == gdk_display_get_default (), FALSE);
705 706
  GDK_NOTE (EVENTS, g_print ("gdk_pointer_is_grabbed: %s\n",
			     p_grab_window != NULL ? "TRUE" : "FALSE"));
707 708 709
  return p_grab_window != NULL;
}

710
gboolean
711 712
gdk_pointer_grab_info_libgtk_only (GdkDisplay *display,
				   GdkWindow **grab_window,
713 714
				   gboolean   *owner_events)
{
Owen Taylor's avatar
Owen Taylor committed
715
  g_return_val_if_fail (display == gdk_display_get_default (), FALSE);
716

717 718 719 720 721 722 723 724 725 726 727 728 729
  if (p_grab_window != NULL)
    {
      if (grab_window)
        *grab_window = p_grab_window;
      if (owner_events)
        *owner_events = p_grab_owner_events;

      return TRUE;
    }
  else
    return FALSE;
}

730
GdkGrabStatus
731 732 733
gdk_keyboard_grab (GdkWindow *window,
		   gboolean   owner_events,
		   guint32    time)
734
{
735 736
  GdkWindow *real_focus_window, *grab_focus_window;

737 738 739 740 741
  gint return_val;
  
  g_return_val_if_fail (window != NULL, 0);
  g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
  
742 743
  GDK_NOTE (EVENTS, g_print ("gdk_keyboard_grab %p\n",
			     GDK_WINDOW_HWND (window)));
744

Tor Lillqvist's avatar
Tor Lillqvist committed
745
  if (!GDK_WINDOW_DESTROYED (window))
746 747
    {
      k_grab_owner_events = owner_events != 0;
748
      return_val = GDK_GRAB_SUCCESS;
749 750
    }
  else
751
    return_val = GDK_GRAB_ALREADY_GRABBED;
752

753
  if (return_val == GDK_GRAB_SUCCESS)
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
    {
      k_grab_window = window;

      if (!k_grab_owner_events)
	{
	  real_focus_window = gdk_win32_handle_table_lookup ((GdkNativeWindow) GetFocus ());
	  if (real_focus_window)
	    real_focus_window = gdk_window_get_toplevel (real_focus_window);
	  grab_focus_window = gdk_window_get_toplevel (k_grab_window);
	  if (real_focus_window != grab_focus_window)
	    {
	      /* Generate events for focus change from the window that really
	       * has focus to the grabber.
	       */
	      if (real_focus_window && !GDK_WINDOW_DESTROYED (real_focus_window)
		  && (((GdkWindowObject *) real_focus_window)->event_mask
		      & GDK_FOCUS_CHANGE_MASK))
		generate_focus_event (real_focus_window, FALSE);

	      if (((GdkWindowObject *) grab_focus_window)->event_mask 
		  & GDK_FOCUS_CHANGE_MASK)
		generate_focus_event (grab_focus_window, TRUE);
	    }
	}
    }
779 780 781 782 783
  
  return return_val;
}

void
784 785
gdk_display_keyboard_ungrab (GdkDisplay *display,
                             guint32 time)
786
{
787 788
  GdkWindow *real_focus_window, *grab_focus_window;

Owen Taylor's avatar
Owen Taylor committed
789
  g_return_if_fail (display == gdk_display_get_default ());
790

791 792
  GDK_NOTE (EVENTS, g_print ("gdk_keyboard_ungrab\n"));

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
  if (k_grab_window && !k_grab_owner_events)
    {
      real_focus_window = gdk_win32_handle_table_lookup ((GdkNativeWindow) GetFocus ());
      if (real_focus_window)
	real_focus_window = gdk_window_get_toplevel (real_focus_window);
      if (!GDK_WINDOW_DESTROYED (k_grab_window))
	grab_focus_window = gdk_window_get_toplevel (k_grab_window);
      else
	grab_focus_window = NULL;
      if (real_focus_window != grab_focus_window)
	{
	  /* Generate events for focus change from grabber to the window that
	   * really has focus. Important for example if a new window is created
	   * while focus is grabbed.
	   */
	  if (grab_focus_window
	      && (((GdkWindowObject *) grab_focus_window)->event_mask
		  & GDK_FOCUS_CHANGE_MASK))
	    generate_focus_event (grab_focus_window, FALSE);

	  if (real_focus_window && !GDK_WINDOW_DESTROYED (real_focus_window)
	      && (((GdkWindowObject *) real_focus_window)->event_mask
		  & GDK_FOCUS_CHANGE_MASK))
	    generate_focus_event (real_focus_window, TRUE);
	}
    }

820 821 822
  k_grab_window = NULL;
}

823
gboolean
824 825
gdk_keyboard_grab_info_libgtk_only (GdkDisplay *display,
				    GdkWindow **grab_window,
826 827
				    gboolean   *owner_events)
{
Owen Taylor's avatar
Owen Taylor committed
828
  g_return_val_if_fail (display == gdk_display_get_default (), FALSE);
829

830 831 832 833 834 835 836 837 838 839 840 841 842
  if (k_grab_window)
    {
      if (grab_window)
        *grab_window = k_grab_window;
      if (owner_events)
        *owner_events = k_grab_owner_events;

      return TRUE;
    }
  else
    return FALSE;
}

843
static GdkFilterReturn
844
gdk_event_apply_filters (MSG      *msg,
845 846 847 848 849 850 851 852 853 854 855 856 857
			 GdkEvent *event,
			 GList    *filters)
{
  GdkEventFilter *filter;
  GList *tmp_list;
  GdkFilterReturn result;
  
  tmp_list = filters;
  
  while (tmp_list)
    {
      filter = (GdkEventFilter *) tmp_list->data;
      
858
      result = (*filter->function) (msg, event, filter->data);
859 860 861 862 863 864 865 866 867
      if (result !=  GDK_FILTER_CONTINUE)
	return result;
      
      tmp_list = tmp_list->next;
    }
  
  return GDK_FILTER_CONTINUE;
}

Tor Lillqvist's avatar
Tor Lillqvist committed
868 869 870 871 872 873 874 875 876 877
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);
}

878 879 880 881 882 883 884 885 886 887 888
void 
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;
  
889
  client_filters = g_list_append (client_filters, filter);
890 891 892
}

static void
893 894
build_key_event_state (GdkEvent *event,
		       BYTE     *key_state)
895
{
896 897 898
  event->key.state = 0;

  if (key_state[VK_SHIFT] & 0x80)
899
    event->key.state |= GDK_SHIFT_MASK;
900 901

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

904 905 906 907 908 909 910 911 912 913 914
  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;

915 916 917 918 919 920 921 922 923
  /* Win9x doesn't distinguish between left and right Control and Alt
   * in the keyboard state as returned by GetKeyboardState(), so we
   * have to punt, and accept either Control + either Alt to be AltGr.
   *
   * Alternatively, we could have some state saved when the Control
   * and Alt keys messages come in, as the KF_EXTENDED bit in lParam
   * does indicate correctly whether it is the right Control or Alt
   * key. But that would be a bit messy.
   */
924
  if (!G_WIN32_IS_NT_BASED () &&
925 926 927 928 929
      _gdk_keyboard_has_altgr &&
      key_state[VK_CONTROL] & 0x80 &&
      key_state[VK_MENU] & 0x80)
    key_state[VK_LCONTROL] = key_state[VK_RMENU] = 0x80;

930 931 932
  if (_gdk_keyboard_has_altgr &&
      (key_state[VK_LCONTROL] & 0x80) &&
      (key_state[VK_RMENU] & 0x80))
933
    {
934 935 936
      event->key.group = 1;
      event->key.state |= GDK_MOD2_MASK;
      if (key_state[VK_RCONTROL] & 0x80)
937
	event->key.state |= GDK_CONTROL_MASK;
938
      if (key_state[VK_LMENU] & 0x80)
939 940
	event->key.state |= GDK_MOD1_MASK;
    }
941
  else
942
    {
943 944 945 946 947
      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;
948
    }
949 950 951
}

static gint
952
build_pointer_event_state (MSG *msg)
953 954 955 956
{
  gint state;
  
  state = 0;
957
  if (msg->wParam & MK_CONTROL)
958
    state |= GDK_CONTROL_MASK;
959
  if (msg->wParam & MK_LBUTTON)
960
    state |= GDK_BUTTON1_MASK;
961
  if (msg->wParam & MK_MBUTTON)
962
    state |= GDK_BUTTON2_MASK;
963
  if (msg->wParam & MK_RBUTTON)
964
    state |= GDK_BUTTON3_MASK;
Tor Lillqvist's avatar
Tor Lillqvist committed
965 966 967 968
  if (msg->wParam & MK_XBUTTON1)
    state |= GDK_BUTTON4_MASK;
  if (msg->wParam & MK_XBUTTON2)
    state |= GDK_BUTTON5_MASK;
969
  if (msg->wParam & MK_SHIFT)
970 971 972 973 974 975 976 977 978 979
    state |= GDK_SHIFT_MASK;
  if (GetKeyState (VK_MENU) < 0)
    state |= GDK_MOD1_MASK;
  if (GetKeyState (VK_CAPITAL) & 0x1)
    state |= GDK_LOCK_MASK;

  return state;
}

static void
980 981 982 983
build_wm_ime_composition_event (GdkEvent *event,
				MSG      *msg,
				wchar_t   wc,
				BYTE     *key_state)
984
{
985
  event->key.time = _gdk_win32_get_next_tick (msg->time);
986
  
987
  build_key_event_state (event, key_state);
988

989
  event->key.hardware_keycode = 0; /* FIXME: What should it be? */
990 991
  event->key.string = NULL;
  event->key.length = 0;
992
  event->key.keyval = gdk_unicode_to_keyval (wc);
993 994
}

995 996
#ifdef G_ENABLE_DEBUG

997
static void
998
print_event_state (guint state)
999
{
1000 1001 1002 1003 1004
#define CASE(bit) if (state & GDK_ ## bit ## _MASK) g_print (#bit " ");
  CASE (SHIFT);
  CASE (LOCK);
  CASE (CONTROL);
  CASE (MOD1);
1005 1006 1007 1008
  CASE (MOD2);
  CASE (MOD3);
  CASE (MOD4);
  CASE (MOD5);
1009 1010 1011
  CASE (BUTTON1);
  CASE (BUTTON2);
  CASE (BUTTON3);
1012 1013
  CASE (BUTTON4);
  CASE (BUTTON5);
1014 1015 1016
#undef CASE
}

1017 1018 1019 1020 1021
static void
print_event (GdkEvent *event)
{
  gchar *escaped, *kvname;

Tor Lillqvist's avatar
Tor Lillqvist committed
1022
  g_print ("%s%*s===> ", (debug_indent > 0 ? "\n" : ""), debug_indent, "");
1023 1024
  switch (event->any.type)
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
1025
#define CASE(x) case x: g_print (#x); break;
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
    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);
1060
    CASE (GDK_SETTING);
1061
#undef CASE
Tor Lillqvist's avatar
Tor Lillqvist committed
1062
    default: g_assert_not_reached ();
1063
    }
1064

Tor Lillqvist's avatar
Tor Lillqvist committed
1065
  g_print (" %p ", GDK_WINDOW_HWND (event->any.window));
1066 1067 1068 1069

  switch (event->any.type)
    {
    case GDK_EXPOSE:
1070 1071
      g_print ("%s %d",
	       _gdk_win32_gdkrectangle_to_string (&event->expose.area),
1072 1073 1074
	       event->expose.count);
      break;
    case GDK_MOTION_NOTIFY:
1075
      g_print ("(%.4g,%.4g) (%.4g,%.4g) %s",
1076
	       event->motion.x, event->motion.y,
1077
	       event->motion.x_root, event->motion.y_root,
1078 1079 1080 1081 1082 1083 1084
	       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
1085
      g_print ("%d (%.4g,%.4g) (%.4g,%.4g) ",
1086
	       event->button.button,
1087 1088
	       event->button.x, event->button.y,
	       event->button.x_root, event->button.y_root);
1089 1090 1091 1092 1093 1094 1095 1096 1097
      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);
1098
      g_print ("%#.02x group:%d %s %d:\"%s\" ",
1099
	       event->key.hardware_keycode, event->key.group,
1100 1101 1102 1103 1104 1105 1106 1107
	       (kvname ? kvname : "??"),
	       event->key.length,
	       escaped);
      g_free (escaped);
      print_event_state (event->key.state);
      break;
    case GDK_ENTER_NOTIFY:
    case GDK_LEAVE_NOTIFY:
1108 1109 1110 1111
      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,
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
	       (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" : "");
1124
      print_event_state (event->crossing.state);
1125
      break;
Tor Lillqvist's avatar
Tor Lillqvist committed
1126 1127 1128
    case GDK_FOCUS_CHANGE:
      g_print ("%s", (event->focus_change.in ? "IN" : "OUT"));
      break;
Tor Lillqvist's avatar
Tor Lillqvist committed
1129 1130 1131 1132 1133
    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;
1134
    case GDK_SCROLL:
1135
      g_print ("(%.4g,%.4g) (%.4g,%.4g)%s",
1136
	       event->scroll.x, event->scroll.y,
1137
	       event->scroll.x_root, event->scroll.y_root,
1138 1139 1140 1141 1142 1143 1144
	       (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;
1145
    case GDK_WINDOW_STATE:
Tor Lillqvist's avatar
Tor Lillqvist committed
1146 1147 1148
      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));
1149 1150 1151
    default:
      /* Nothing */
      break;
1152
    }  
Tor Lillqvist's avatar
Tor Lillqvist committed
1153
  g_print ("%s", (debug_indent == 0 ? "\n" : "")); 
1154 1155
}

1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
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;
}

1175 1176
#endif

1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
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
append_event (GdkDisplay *display,
	      GdkEvent   *event)
{
  fixup_event (event);
  _gdk_event_queue_append (display, event);
  GDK_NOTE (EVENTS, print_event (event));
}

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
apply_filters (GdkDisplay *display,
1267
	       GdkWindow  *window,
1268 1269 1270 1271 1272 1273 1274
	       MSG        *msg,
	       GList      *filters)
{
  GdkFilterReturn result;
  GdkEvent *event = gdk_event_new (GDK_NOTHING);
  GList *node;

1275 1276 1277 1278 1279
  if (window != NULL)
    {
      event->any.window = window;
      g_object_ref (window);
    }
1280 1281 1282 1283 1284 1285 1286 1287
  ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;

  /* 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.
   */
  node = _gdk_event_queue_append (display, event);
  
1288
  result = gdk_event_apply_filters (msg, event, filters);
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
      
  if (result == GDK_FILTER_CONTINUE || result == GDK_FILTER_REMOVE)
    {
      _gdk_event_queue_remove_link (display, node);
      g_list_free_1 (node);
      gdk_event_free (event);
    }
  else /* GDK_FILTER_TRANSLATE */
    {
      ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
      fixup_event (event);
      GDK_NOTE (EVENTS, print_event (event));
    }
  return result;
}

1305
static gboolean
1306 1307
gdk_window_is_ancestor (GdkWindow *ancestor,
			GdkWindow *window)
1308
{
1309
  if (ancestor == NULL || window == NULL)
1310 1311
    return FALSE;

1312 1313
  return (gdk_window_get_parent (window) == ancestor ||
	  gdk_window_is_ancestor (ancestor, gdk_window_get_parent (window)));
1314 1315 1316
}

static void
1317 1318 1319 1320
synthesize_enter_or_leave_event (GdkWindow    	*window,
				 MSG          	*msg,
				 GdkEventType 	 type,
				 GdkCrossingMode mode,
1321
				 GdkNotifyType detail,
1322 1323
				 gint         	 x,
				 gint         	 y