]> Pileus Git - ~andy/gtk/blob - gdk/quartz/gdkevents-quartz.c
Fix typo from f2ab3af
[~andy/gtk] / gdk / quartz / gdkevents-quartz.c
1 /* gdkevents-quartz.c
2  *
3  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4  * Copyright (C) 1998-2002 Tor Lillqvist
5  * Copyright (C) 2005-2008 Imendio AB
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "config.h"
22 #include <sys/types.h>
23 #include <sys/sysctl.h>
24 #include <pthread.h>
25 #include <unistd.h>
26
27 #import <Cocoa/Cocoa.h>
28 #include <Carbon/Carbon.h>
29
30 #include <gdk/gdkdisplayprivate.h>
31
32 #include "gdkscreen.h"
33 #include "gdkkeysyms.h"
34 #include "gdkquartz.h"
35 #include "gdkquartzdisplay.h"
36 #include "gdkprivate-quartz.h"
37 #include "gdkquartzdevicemanager-core.h"
38
39 #define GRIP_WIDTH 15
40 #define GRIP_HEIGHT 15
41 #define GDK_LION_RESIZE 5
42
43 #define WINDOW_IS_TOPLEVEL(window)                   \
44   (GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&   \
45    GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN && \
46    GDK_WINDOW_TYPE (window) != GDK_WINDOW_OFFSCREEN)
47
48 /* This is the window corresponding to the key window */
49 static GdkWindow   *current_keyboard_window;
50
51
52 static void append_event                        (GdkEvent  *event,
53                                                  gboolean   windowing);
54
55 static GdkWindow *find_toplevel_under_pointer   (GdkDisplay *display,
56                                                  NSPoint     screen_point,
57                                                  gint       *x,
58                                                  gint       *y);
59
60
61 static void
62 gdk_quartz_ns_notification_callback (CFNotificationCenterRef  center,
63                                      void                    *observer,
64                                      CFStringRef              name,
65                                      const void              *object,
66                                      CFDictionaryRef          userInfo)
67 {
68   GdkEvent new_event;
69
70   new_event.type = GDK_SETTING;
71   new_event.setting.window = gdk_screen_get_root_window (_gdk_screen);
72   new_event.setting.send_event = FALSE;
73   new_event.setting.action = GDK_SETTING_ACTION_CHANGED;
74   new_event.setting.name = NULL;
75
76   /* Translate name */
77   if (CFStringCompare (name,
78                        CFSTR("AppleNoRedisplayAppearancePreferenceChanged"),
79                        0) == kCFCompareEqualTo)
80     new_event.setting.name = "gtk-primary-button-warps-slider";
81
82   if (!new_event.setting.name)
83     return;
84
85   gdk_event_put (&new_event);
86 }
87
88 static void
89 gdk_quartz_events_init_notifications (void)
90 {
91   static gboolean notifications_initialized = FALSE;
92
93   if (notifications_initialized)
94     return;
95   notifications_initialized = TRUE;
96
97   /* Initialize any handlers for notifications we want to push to GTK
98    * through GdkEventSettings.
99    */
100
101   /* This is an undocumented *distributed* notification to listen for changes
102    * in scrollbar jump behavior. It is used by LibreOffice and WebKit as well.
103    */
104   CFNotificationCenterAddObserver (CFNotificationCenterGetDistributedCenter (),
105                                    NULL,
106                                    &gdk_quartz_ns_notification_callback,
107                                    CFSTR ("AppleNoRedisplayAppearancePreferenceChanged"),
108                                    NULL,
109                                    CFNotificationSuspensionBehaviorDeliverImmediately);
110 }
111
112 void
113 _gdk_quartz_events_init (void)
114 {
115   _gdk_quartz_event_loop_init ();
116   gdk_quartz_events_init_notifications ();
117
118   current_keyboard_window = g_object_ref (_gdk_root);
119 }
120
121 gboolean
122 _gdk_quartz_display_has_pending (GdkDisplay *display)
123 {
124   return (_gdk_event_queue_find_first (display) ||
125          (_gdk_quartz_event_loop_check_pending ()));
126 }
127
128 void
129 _gdk_quartz_events_break_all_grabs (guint32 time)
130 {
131   GList *list, *l;
132   GdkDeviceManager *device_manager;
133
134   device_manager = gdk_display_get_device_manager (_gdk_display);
135   list = gdk_device_manager_list_devices (device_manager,
136                                           GDK_DEVICE_TYPE_MASTER);
137   for (l = list; l; l = l->next)
138     {
139       GdkDeviceGrabInfo *grab;
140
141       grab = _gdk_display_get_last_device_grab (_gdk_display, l->data);
142       if (grab)
143         {
144           grab->serial_end = 0;
145           grab->implicit_ungrab = TRUE;
146         }
147
148       _gdk_display_device_grab_update (_gdk_display, l->data, NULL, 0);
149     }
150
151   g_list_free (list);
152 }
153
154 static void
155 fixup_event (GdkEvent *event)
156 {
157   if (event->any.window)
158     g_object_ref (event->any.window);
159   if (((event->any.type == GDK_ENTER_NOTIFY) ||
160        (event->any.type == GDK_LEAVE_NOTIFY)) &&
161       (event->crossing.subwindow != NULL))
162     g_object_ref (event->crossing.subwindow);
163   event->any.send_event = FALSE;
164 }
165
166 static void
167 append_event (GdkEvent *event,
168               gboolean  windowing)
169 {
170   GList *node;
171
172   fixup_event (event);
173   node = _gdk_event_queue_append (_gdk_display, event);
174
175   if (windowing)
176     _gdk_windowing_got_event (_gdk_display, node, event, 0);
177 }
178
179 static gint
180 gdk_event_apply_filters (NSEvent *nsevent,
181                          GdkEvent *event,
182                          GList **filters)
183 {
184   GList *tmp_list;
185   GdkFilterReturn result;
186   
187   tmp_list = *filters;
188
189   while (tmp_list)
190     {
191       GdkEventFilter *filter = (GdkEventFilter*) tmp_list->data;
192       GList *node;
193
194       if ((filter->flags & GDK_EVENT_FILTER_REMOVED) != 0)
195         {
196           tmp_list = tmp_list->next;
197           continue;
198         }
199
200       filter->ref_count++;
201       result = filter->function (nsevent, event, filter->data);
202
203       /* get the next node after running the function since the
204          function may add or remove a next node */
205       node = tmp_list;
206       tmp_list = tmp_list->next;
207
208       filter->ref_count--;
209       if (filter->ref_count == 0)
210         {
211           *filters = g_list_remove_link (*filters, node);
212           g_list_free_1 (node);
213           g_free (filter);
214         }
215
216       if (result !=  GDK_FILTER_CONTINUE)
217         return result;
218     }
219
220   return GDK_FILTER_CONTINUE;
221 }
222
223 static guint32
224 get_time_from_ns_event (NSEvent *event)
225 {
226   double time = [event timestamp];
227
228   /* cast via double->uint64 conversion to make sure that it is
229    * wrapped on 32-bit machines when it overflows
230    */
231   return (guint32) (guint64) (time * 1000.0);
232 }
233
234 static int
235 get_mouse_button_from_ns_event (NSEvent *event)
236 {
237   NSInteger button;
238
239   button = [event buttonNumber];
240
241   switch (button)
242     {
243     case 0:
244       return 1;
245     case 1:
246       return 3;
247     case 2:
248       return 2;
249     default:
250       return button + 1;
251     }
252 }
253
254 static GdkModifierType
255 get_mouse_button_modifiers_from_ns_buttons (NSUInteger nsbuttons)
256 {
257   GdkModifierType modifiers = 0;
258
259   if (nsbuttons & (1 << 0))
260     modifiers |= GDK_BUTTON1_MASK;
261   if (nsbuttons & (1 << 1))
262     modifiers |= GDK_BUTTON3_MASK;
263   if (nsbuttons & (1 << 2))
264     modifiers |= GDK_BUTTON2_MASK;
265   if (nsbuttons & (1 << 3))
266     modifiers |= GDK_BUTTON4_MASK;
267   if (nsbuttons & (1 << 4))
268     modifiers |= GDK_BUTTON5_MASK;
269
270   return modifiers;
271 }
272
273 static GdkModifierType
274 get_mouse_button_modifiers_from_ns_event (NSEvent *event)
275 {
276   int button;
277   GdkModifierType state = 0;
278
279   /* This maps buttons 1 to 5 to GDK_BUTTON[1-5]_MASK */
280   button = get_mouse_button_from_ns_event (event);
281   if (button >= 1 && button <= 5)
282     state = (1 << (button + 7));
283
284   return state;
285 }
286
287 static GdkModifierType
288 get_keyboard_modifiers_from_ns_flags (NSUInteger nsflags)
289 {
290   GdkModifierType modifiers = 0;
291
292   if (nsflags & NSAlphaShiftKeyMask)
293     modifiers |= GDK_LOCK_MASK;
294   if (nsflags & NSShiftKeyMask)
295     modifiers |= GDK_SHIFT_MASK;
296   if (nsflags & NSControlKeyMask)
297     modifiers |= GDK_CONTROL_MASK;
298   if (nsflags & NSAlternateKeyMask)
299     modifiers |= GDK_MOD1_MASK;
300   if (nsflags & NSCommandKeyMask)
301     modifiers |= GDK_MOD2_MASK;
302
303   return modifiers;
304 }
305
306 static GdkModifierType
307 get_keyboard_modifiers_from_ns_event (NSEvent *nsevent)
308 {
309   return get_keyboard_modifiers_from_ns_flags ([nsevent modifierFlags]);
310 }
311
312 /* Return an event mask from an NSEvent */
313 static GdkEventMask
314 get_event_mask_from_ns_event (NSEvent *nsevent)
315 {
316   switch ([nsevent type])
317     {
318     case NSLeftMouseDown:
319     case NSRightMouseDown:
320     case NSOtherMouseDown:
321       return GDK_BUTTON_PRESS_MASK;
322     case NSLeftMouseUp:
323     case NSRightMouseUp:
324     case NSOtherMouseUp:
325       return GDK_BUTTON_RELEASE_MASK;
326     case NSMouseMoved:
327       return GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK;
328     case NSScrollWheel:
329       /* Since applications that want button press events can get
330        * scroll events on X11 (since scroll wheel events are really
331        * button press events there), we need to use GDK_BUTTON_PRESS_MASK too.
332        */
333       return GDK_SCROLL_MASK | GDK_BUTTON_PRESS_MASK;
334     case NSLeftMouseDragged:
335       return (GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK |
336               GDK_BUTTON_MOTION_MASK | GDK_BUTTON1_MOTION_MASK | 
337               GDK_BUTTON1_MASK);
338     case NSRightMouseDragged:
339       return (GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK |
340               GDK_BUTTON_MOTION_MASK | GDK_BUTTON3_MOTION_MASK | 
341               GDK_BUTTON3_MASK);
342     case NSOtherMouseDragged:
343       {
344         GdkEventMask mask;
345
346         mask = (GDK_POINTER_MOTION_MASK |
347                 GDK_POINTER_MOTION_HINT_MASK |
348                 GDK_BUTTON_MOTION_MASK);
349
350         if (get_mouse_button_from_ns_event (nsevent) == 2)
351           mask |= (GDK_BUTTON2_MOTION_MASK | GDK_BUTTON2_MOTION_MASK | 
352                    GDK_BUTTON2_MASK);
353
354         return mask;
355       }
356     case NSKeyDown:
357     case NSKeyUp:
358     case NSFlagsChanged:
359       {
360         switch (_gdk_quartz_keys_event_type (nsevent))
361           {
362           case GDK_KEY_PRESS:
363             return GDK_KEY_PRESS_MASK;
364           case GDK_KEY_RELEASE:
365             return GDK_KEY_RELEASE_MASK;
366           case GDK_NOTHING:
367             return 0;
368           default:
369             g_assert_not_reached ();
370           }
371       }
372       break;
373
374     case NSMouseEntered:
375       return GDK_ENTER_NOTIFY_MASK;
376
377     case NSMouseExited:
378       return GDK_LEAVE_NOTIFY_MASK;
379
380     default:
381       g_assert_not_reached ();
382     }
383
384   return 0;
385 }
386
387 static void
388 get_window_point_from_screen_point (GdkWindow *window,
389                                     NSPoint    screen_point,
390                                     gint      *x,
391                                     gint      *y)
392 {
393   NSPoint point;
394   NSWindow *nswindow;
395
396   nswindow = ((GdkWindowImplQuartz *)window->impl)->toplevel;
397
398   point = [nswindow convertScreenToBase:screen_point];
399
400   *x = point.x;
401   *y = window->height - point.y;
402 }
403
404 static GdkWindow *
405 get_toplevel_from_ns_event (NSEvent *nsevent,
406                             NSPoint *screen_point,
407                             gint    *x,
408                             gint    *y)
409 {
410   GdkWindow *toplevel;
411
412   if ([nsevent window])
413     {
414       GdkQuartzView *view;
415       NSPoint point;
416
417       view = (GdkQuartzView *)[[nsevent window] contentView];
418
419       toplevel = [view gdkWindow];
420
421       point = [nsevent locationInWindow];
422       *screen_point = [[nsevent window] convertBaseToScreen:point];
423
424       *x = point.x;
425       *y = toplevel->height - point.y;
426     }
427   else
428     {
429       /* Fallback used when no NSWindow set.  This happens e.g. when
430        * we allow motion events without a window set in gdk_event_translate()
431        * that occur immediately after the main menu bar was clicked/used.
432        */
433       *screen_point = [NSEvent mouseLocation];
434       toplevel = find_toplevel_under_pointer (_gdk_display,
435                                               *screen_point,
436                                               x, y);
437     }
438
439   return toplevel;
440 }
441
442 static GdkEvent *
443 create_focus_event (GdkWindow *window,
444                     gboolean   in)
445 {
446   GdkEvent *event;
447   GdkQuartzDeviceManagerCore *device_manager;
448
449   event = gdk_event_new (GDK_FOCUS_CHANGE);
450   event->focus_change.window = window;
451   event->focus_change.in = in;
452
453   device_manager = GDK_QUARTZ_DEVICE_MANAGER_CORE (_gdk_display->device_manager);
454   gdk_event_set_device (event, device_manager->core_keyboard);
455
456   return event;
457 }
458
459
460 static void
461 generate_motion_event (GdkWindow *window)
462 {
463   NSPoint screen_point;
464   GdkEvent *event;
465   gint x, y, x_root, y_root;
466
467   event = gdk_event_new (GDK_MOTION_NOTIFY);
468   event->any.window = NULL;
469   event->any.send_event = TRUE;
470
471   screen_point = [NSEvent mouseLocation];
472
473   _gdk_quartz_window_nspoint_to_gdk_xy (screen_point, &x_root, &y_root);
474   get_window_point_from_screen_point (window, screen_point, &x, &y);
475
476   event->any.type = GDK_MOTION_NOTIFY;
477   event->motion.window = window;
478   event->motion.time = GDK_CURRENT_TIME;
479   event->motion.x = x;
480   event->motion.y = y;
481   event->motion.x_root = x_root;
482   event->motion.y_root = y_root;
483   /* FIXME event->axes */
484   event->motion.state = _gdk_quartz_events_get_current_keyboard_modifiers () |
485                         _gdk_quartz_events_get_current_mouse_modifiers ();
486   event->motion.is_hint = FALSE;
487   event->motion.device = _gdk_display->core_pointer;
488
489   append_event (event, TRUE);
490 }
491
492 /* Note: Used to both set a new focus window and to unset the old one. */
493 void
494 _gdk_quartz_events_update_focus_window (GdkWindow *window,
495                                         gboolean   got_focus)
496 {
497   GdkEvent *event;
498
499   if (got_focus && window == current_keyboard_window)
500     return;
501
502   /* FIXME: Don't do this when grabbed? Or make GdkQuartzNSWindow
503    * disallow it in the first place instead?
504    */
505   
506   if (!got_focus && window == current_keyboard_window)
507     {
508       event = create_focus_event (current_keyboard_window, FALSE);
509       append_event (event, FALSE);
510       g_object_unref (current_keyboard_window);
511       current_keyboard_window = NULL;
512     }
513
514   if (got_focus)
515     {
516       if (current_keyboard_window)
517         {
518           event = create_focus_event (current_keyboard_window, FALSE);
519           append_event (event, FALSE);
520           g_object_unref (current_keyboard_window);
521           current_keyboard_window = NULL;
522         }
523       
524       event = create_focus_event (window, TRUE);
525       append_event (event, FALSE);
526       current_keyboard_window = g_object_ref (window);
527
528       /* We just became the active window.  Unlike X11, Mac OS X does
529        * not send us motion events while the window does not have focus
530        * ("is not key").  We send a dummy motion notify event now, so that
531        * everything in the window is set to correct state.
532        */
533       generate_motion_event (window);
534     }
535 }
536
537 void
538 _gdk_quartz_events_send_enter_notify_event (GdkWindow *window)
539 {
540   NSPoint screen_point;
541   GdkEvent *event;
542   gint x, y, x_root, y_root;
543
544   event = gdk_event_new (GDK_ENTER_NOTIFY);
545   event->any.window = NULL;
546   event->any.send_event = FALSE;
547
548   screen_point = [NSEvent mouseLocation];
549
550   _gdk_quartz_window_nspoint_to_gdk_xy (screen_point, &x_root, &y_root);
551   get_window_point_from_screen_point (window, screen_point, &x, &y);
552
553   event->crossing.window = window;
554   event->crossing.subwindow = NULL;
555   event->crossing.time = GDK_CURRENT_TIME;
556   event->crossing.x = x;
557   event->crossing.y = y;
558   event->crossing.x_root = x_root;
559   event->crossing.y_root = y_root;
560   event->crossing.mode = GDK_CROSSING_NORMAL;
561   event->crossing.detail = GDK_NOTIFY_ANCESTOR;
562   event->crossing.state = _gdk_quartz_events_get_current_keyboard_modifiers () |
563                           _gdk_quartz_events_get_current_mouse_modifiers ();
564
565   gdk_event_set_device (event, _gdk_display->core_pointer);
566
567   append_event (event, TRUE);
568 }
569
570 void
571 _gdk_quartz_events_send_map_event (GdkWindow *window)
572 {
573   GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (window->impl);
574
575   if (!impl->toplevel)
576     return;
577
578   if (window->event_mask & GDK_STRUCTURE_MASK)
579     {
580       GdkEvent event;
581
582       event.any.type = GDK_MAP;
583       event.any.window = window;
584   
585       gdk_event_put (&event);
586     }
587 }
588
589 static GdkWindow *
590 find_toplevel_under_pointer (GdkDisplay *display,
591                              NSPoint     screen_point,
592                              gint       *x,
593                              gint       *y)
594 {
595   GdkWindow *toplevel;
596   GdkPointerWindowInfo *info;
597
598   info = _gdk_display_get_pointer_info (display, display->core_pointer);
599   toplevel = info->toplevel_under_pointer;
600   if (toplevel && WINDOW_IS_TOPLEVEL (toplevel))
601     get_window_point_from_screen_point (toplevel, screen_point, x, y);
602
603   return toplevel;
604 }
605
606 static GdkWindow *
607 find_toplevel_for_keyboard_event (NSEvent *nsevent)
608 {
609   GList *list, *l;
610   GdkWindow *window;
611   GdkDisplay *display;
612   GdkQuartzView *view;
613   GdkDeviceManager *device_manager;
614
615   view = (GdkQuartzView *)[[nsevent window] contentView];
616   window = [view gdkWindow];
617
618   display = gdk_window_get_display (window);
619
620   device_manager = gdk_display_get_device_manager (display);
621   list = gdk_device_manager_list_devices (device_manager,
622                                           GDK_DEVICE_TYPE_MASTER);
623   for (l = list; l; l = l->next)
624     {
625       GdkDeviceGrabInfo *grab;
626       GdkDevice *device = l->data;
627
628       if (gdk_device_get_source(device) != GDK_SOURCE_KEYBOARD)
629         continue;
630
631       grab = _gdk_display_get_last_device_grab (display, device);
632       if (grab && grab->window && !grab->owner_events)
633         {
634           window = gdk_window_get_effective_toplevel (grab->window);
635           break;
636         }
637     }
638
639   g_list_free (list);
640
641   return window;
642 }
643
644 static GdkWindow *
645 find_toplevel_for_mouse_event (NSEvent    *nsevent,
646                                gint       *x,
647                                gint       *y)
648 {
649   NSPoint screen_point;
650   NSEventType event_type;
651   GdkWindow *toplevel;
652   GdkDisplay *display;
653   GdkDeviceGrabInfo *grab;
654
655   toplevel = get_toplevel_from_ns_event (nsevent, &screen_point, x, y);
656
657   display = gdk_window_get_display (toplevel);
658
659   event_type = [nsevent type];
660
661   /* From the docs for XGrabPointer:
662    *
663    * If owner_events is True and if a generated pointer event
664    * would normally be reported to this client, it is reported
665    * as usual. Otherwise, the event is reported with respect to
666    * the grab_window and is reported only if selected by
667    * event_mask. For either value of owner_events, unreported
668    * events are discarded.
669    */
670   grab = _gdk_display_get_last_device_grab (display,
671                                             display->core_pointer);
672   if (WINDOW_IS_TOPLEVEL (toplevel) && grab)
673     {
674       /* Implicit grabs do not go through XGrabPointer and thus the
675        * event mask should not be checked.
676        */
677       if (!grab->implicit
678           && (grab->event_mask & get_event_mask_from_ns_event (nsevent)) == 0)
679         return NULL;
680
681       if (grab->owner_events)
682         {
683           /* For owner events, we need to use the toplevel under the
684            * pointer, not the window from the NSEvent, since that is
685            * reported with respect to the key window, which could be
686            * wrong.
687            */
688           GdkWindow *toplevel_under_pointer;
689           gint x_tmp, y_tmp;
690
691           toplevel_under_pointer = find_toplevel_under_pointer (display,
692                                                                 screen_point,
693                                                                 &x_tmp, &y_tmp);
694           if (toplevel_under_pointer)
695             {
696               toplevel = toplevel_under_pointer;
697               *x = x_tmp;
698               *y = y_tmp;
699             }
700
701           return toplevel;
702         }
703       else
704         {
705           /* Finally check the grab window. */
706           GdkWindow *grab_toplevel;
707
708           grab_toplevel = gdk_window_get_effective_toplevel (grab->window);
709           get_window_point_from_screen_point (grab_toplevel, screen_point,
710                                               x, y);
711
712           return grab_toplevel;
713         }
714
715       return NULL;
716     }
717   else 
718     {
719       /* The non-grabbed case. */
720       GdkWindow *toplevel_under_pointer;
721       gint x_tmp, y_tmp;
722
723       /* Ignore all events but mouse moved that might be on the title
724        * bar (above the content view). The reason is that otherwise
725        * gdk gets confused about getting e.g. button presses with no
726        * window (the title bar is not known to it).
727        */
728       if (event_type != NSMouseMoved)
729         if (*y < 0)
730           return NULL;
731
732       /* As for owner events, we need to use the toplevel under the
733        * pointer, not the window from the NSEvent.
734        */
735       toplevel_under_pointer = find_toplevel_under_pointer (display,
736                                                             screen_point,
737                                                             &x_tmp, &y_tmp);
738       if (toplevel_under_pointer
739           && WINDOW_IS_TOPLEVEL (toplevel_under_pointer))
740         {
741           GdkWindowImplQuartz *toplevel_impl;
742
743           toplevel = toplevel_under_pointer;
744
745           toplevel_impl = (GdkWindowImplQuartz *)toplevel->impl;
746
747           *x = x_tmp;
748           *y = y_tmp;
749         }
750
751       return toplevel;
752     }
753
754   return NULL;
755 }
756
757 /* This function finds the correct window to send an event to, taking
758  * into account grabs, event propagation, and event masks.
759  */
760 static GdkWindow *
761 find_window_for_ns_event (NSEvent *nsevent, 
762                           gint    *x, 
763                           gint    *y,
764                           gint    *x_root,
765                           gint    *y_root)
766 {
767   GdkQuartzView *view;
768   GdkWindow *toplevel;
769   NSPoint screen_point;
770   NSEventType event_type;
771
772   view = (GdkQuartzView *)[[nsevent window] contentView];
773
774   toplevel = get_toplevel_from_ns_event (nsevent, &screen_point, x, y);
775   _gdk_quartz_window_nspoint_to_gdk_xy (screen_point, x_root, y_root);
776
777   event_type = [nsevent type];
778
779   switch (event_type)
780     {
781     case NSLeftMouseDown:
782     case NSRightMouseDown:
783     case NSOtherMouseDown:
784     case NSLeftMouseUp:
785     case NSRightMouseUp:
786     case NSOtherMouseUp:
787     case NSMouseMoved:
788     case NSScrollWheel:
789     case NSLeftMouseDragged:
790     case NSRightMouseDragged:
791     case NSOtherMouseDragged:
792       return find_toplevel_for_mouse_event (nsevent, x, y);
793       
794     case NSMouseEntered:
795     case NSMouseExited:
796       /* Only handle our own entered/exited events, not the ones for the
797        * titlebar buttons.
798        */
799       if ([view trackingRect] == [nsevent trackingNumber])
800         return toplevel;
801       else
802         return NULL;
803
804     case NSKeyDown:
805     case NSKeyUp:
806     case NSFlagsChanged:
807       return find_toplevel_for_keyboard_event (nsevent);
808
809     default:
810       /* Ignore everything else. */
811       break;
812     }
813
814   return NULL;
815 }
816
817 static void
818 fill_crossing_event (GdkWindow       *toplevel,
819                      GdkEvent        *event,
820                      NSEvent         *nsevent,
821                      gint             x,
822                      gint             y,
823                      gint             x_root,
824                      gint             y_root,
825                      GdkEventType     event_type,
826                      GdkCrossingMode  mode,
827                      GdkNotifyType    detail)
828 {
829   event->any.type = event_type;
830   event->crossing.window = toplevel;
831   event->crossing.subwindow = NULL;
832   event->crossing.time = get_time_from_ns_event (nsevent);
833   event->crossing.x = x;
834   event->crossing.y = y;
835   event->crossing.x_root = x_root;
836   event->crossing.y_root = y_root;
837   event->crossing.mode = mode;
838   event->crossing.detail = detail;
839   event->crossing.state = get_keyboard_modifiers_from_ns_event (nsevent) |
840                          _gdk_quartz_events_get_current_mouse_modifiers ();
841
842   gdk_event_set_device (event, _gdk_display->core_pointer);
843
844   /* FIXME: Focus and button state? */
845 }
846
847 static void
848 fill_button_event (GdkWindow *window,
849                    GdkEvent  *event,
850                    NSEvent   *nsevent,
851                    gint       x,
852                    gint       y,
853                    gint       x_root,
854                    gint       y_root)
855 {
856   GdkEventType type;
857   gint state;
858
859   state = get_keyboard_modifiers_from_ns_event (nsevent) |
860          _gdk_quartz_events_get_current_mouse_modifiers ();
861
862   switch ([nsevent type])
863     {
864     case NSLeftMouseDown:
865     case NSRightMouseDown:
866     case NSOtherMouseDown:
867       type = GDK_BUTTON_PRESS;
868       state &= ~get_mouse_button_modifiers_from_ns_event (nsevent);
869       break;
870
871     case NSLeftMouseUp:
872     case NSRightMouseUp:
873     case NSOtherMouseUp:
874       type = GDK_BUTTON_RELEASE;
875       state |= get_mouse_button_modifiers_from_ns_event (nsevent);
876       break;
877
878     default:
879       g_assert_not_reached ();
880     }
881
882   event->any.type = type;
883   event->button.window = window;
884   event->button.time = get_time_from_ns_event (nsevent);
885   event->button.x = x;
886   event->button.y = y;
887   event->button.x_root = x_root;
888   event->button.y_root = y_root;
889   /* FIXME event->axes */
890   event->button.state = state;
891   event->button.button = get_mouse_button_from_ns_event (nsevent);
892   event->button.device = _gdk_display->core_pointer;
893 }
894
895 static void
896 fill_motion_event (GdkWindow *window,
897                    GdkEvent  *event,
898                    NSEvent   *nsevent,
899                    gint       x,
900                    gint       y,
901                    gint       x_root,
902                    gint       y_root)
903 {
904   event->any.type = GDK_MOTION_NOTIFY;
905   event->motion.window = window;
906   event->motion.time = get_time_from_ns_event (nsevent);
907   event->motion.x = x;
908   event->motion.y = y;
909   event->motion.x_root = x_root;
910   event->motion.y_root = y_root;
911   /* FIXME event->axes */
912   event->motion.state = get_keyboard_modifiers_from_ns_event (nsevent) |
913                         _gdk_quartz_events_get_current_mouse_modifiers ();
914   event->motion.is_hint = FALSE;
915   event->motion.device = _gdk_display->core_pointer;
916 }
917
918 static void
919 fill_scroll_event (GdkWindow          *window,
920                    GdkEvent           *event,
921                    NSEvent            *nsevent,
922                    gint                x,
923                    gint                y,
924                    gint                x_root,
925                    gint                y_root,
926                    gdouble             delta_x,
927                    gdouble             delta_y,
928                    GdkScrollDirection  direction)
929 {
930   NSPoint point;
931
932   point = [nsevent locationInWindow];
933
934   event->any.type = GDK_SCROLL;
935   event->scroll.window = window;
936   event->scroll.time = get_time_from_ns_event (nsevent);
937   event->scroll.x = x;
938   event->scroll.y = y;
939   event->scroll.x_root = x_root;
940   event->scroll.y_root = y_root;
941   event->scroll.state = get_keyboard_modifiers_from_ns_event (nsevent);
942   event->scroll.direction = direction;
943   event->scroll.device = _gdk_display->core_pointer;
944   event->scroll.delta_x = delta_x;
945   event->scroll.delta_y = delta_y;
946 }
947
948 static void
949 fill_key_event (GdkWindow    *window,
950                 GdkEvent     *event,
951                 NSEvent      *nsevent,
952                 GdkEventType  type)
953 {
954   GdkEventPrivate *priv;
955   GdkQuartzDeviceManagerCore *device_manager;
956   gchar buf[7];
957   gunichar c = 0;
958
959   priv = (GdkEventPrivate *) event;
960   priv->windowing_data = [nsevent retain];
961
962   event->any.type = type;
963   event->key.window = window;
964   event->key.time = get_time_from_ns_event (nsevent);
965   event->key.state = get_keyboard_modifiers_from_ns_event (nsevent);
966   event->key.hardware_keycode = [nsevent keyCode];
967   event->key.group = ([nsevent modifierFlags] & NSAlternateKeyMask) ? 1 : 0;
968   event->key.keyval = GDK_KEY_VoidSymbol;
969
970   device_manager = GDK_QUARTZ_DEVICE_MANAGER_CORE (_gdk_display->device_manager);
971   gdk_event_set_device (event, device_manager->core_keyboard);
972   
973   gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (_gdk_display),
974                                        event->key.hardware_keycode,
975                                        event->key.state, 
976                                        event->key.group,
977                                        &event->key.keyval,
978                                        NULL, NULL, NULL);
979
980   event->key.is_modifier = _gdk_quartz_keys_is_modifier (event->key.hardware_keycode);
981
982   /* If the key press is a modifier, the state should include the mask
983    * for that modifier but only for releases, not presses. This
984    * matches the X11 backend behavior.
985    */
986   if (event->key.is_modifier)
987     {
988       int mask = 0;
989
990       switch (event->key.keyval)
991         {
992         case GDK_KEY_Meta_R:
993         case GDK_KEY_Meta_L:
994           mask = GDK_MOD2_MASK;
995           break;
996         case GDK_KEY_Shift_R:
997         case GDK_KEY_Shift_L:
998           mask = GDK_SHIFT_MASK;
999           break;
1000         case GDK_KEY_Caps_Lock:
1001           mask = GDK_LOCK_MASK;
1002           break;
1003         case GDK_KEY_Alt_R:
1004         case GDK_KEY_Alt_L:
1005           mask = GDK_MOD1_MASK;
1006           break;
1007         case GDK_KEY_Control_R:
1008         case GDK_KEY_Control_L:
1009           mask = GDK_CONTROL_MASK;
1010           break;
1011         default:
1012           mask = 0;
1013         }
1014
1015       if (type == GDK_KEY_PRESS)
1016         event->key.state &= ~mask;
1017       else if (type == GDK_KEY_RELEASE)
1018         event->key.state |= mask;
1019     }
1020
1021   event->key.state |= _gdk_quartz_events_get_current_mouse_modifiers ();
1022
1023   /* The X11 backend adds the first virtual modifier MOD2..MOD5 are
1024    * mapped to. Since we only have one virtual modifier in the quartz
1025    * backend, calling the standard function will do.
1026    */
1027   gdk_keymap_add_virtual_modifiers (gdk_keymap_get_for_display (_gdk_display),
1028                                     &event->key.state);
1029
1030   event->key.string = NULL;
1031
1032   /* Fill in ->string since apps depend on it, taken from the x11 backend. */
1033   if (event->key.keyval != GDK_KEY_VoidSymbol)
1034     c = gdk_keyval_to_unicode (event->key.keyval);
1035
1036   if (c)
1037     {
1038       gsize bytes_written;
1039       gint len;
1040
1041       len = g_unichar_to_utf8 (c, buf);
1042       buf[len] = '\0';
1043       
1044       event->key.string = g_locale_from_utf8 (buf, len,
1045                                               NULL, &bytes_written,
1046                                               NULL);
1047       if (event->key.string)
1048         event->key.length = bytes_written;
1049     }
1050   else if (event->key.keyval == GDK_KEY_Escape)
1051     {
1052       event->key.length = 1;
1053       event->key.string = g_strdup ("\033");
1054     }
1055   else if (event->key.keyval == GDK_KEY_Return ||
1056           event->key.keyval == GDK_KEY_KP_Enter)
1057     {
1058       event->key.length = 1;
1059       event->key.string = g_strdup ("\r");
1060     }
1061
1062   if (!event->key.string)
1063     {
1064       event->key.length = 0;
1065       event->key.string = g_strdup ("");
1066     }
1067
1068   GDK_NOTE(EVENTS,
1069     g_message ("key %s:\t\twindow: %p  key: %12s  %d",
1070           type == GDK_KEY_PRESS ? "press" : "release",
1071           event->key.window,
1072           event->key.keyval ? gdk_keyval_name (event->key.keyval) : "(none)",
1073           event->key.keyval));
1074 }
1075
1076 static gboolean
1077 synthesize_crossing_event (GdkWindow *window,
1078                            GdkEvent  *event,
1079                            NSEvent   *nsevent,
1080                            gint       x,
1081                            gint       y,
1082                            gint       x_root,
1083                            gint       y_root)
1084 {
1085   switch ([nsevent type])
1086     {
1087     case NSMouseEntered:
1088       /* Enter events are considered always to be from the root window as we
1089        * can't know for sure from what window we enter.
1090        */
1091       if (!(window->event_mask & GDK_ENTER_NOTIFY_MASK))
1092         return FALSE;
1093
1094       fill_crossing_event (window, event, nsevent,
1095                            x, y,
1096                            x_root, y_root,
1097                            GDK_ENTER_NOTIFY,
1098                            GDK_CROSSING_NORMAL,
1099                            GDK_NOTIFY_ANCESTOR);
1100       return TRUE;
1101
1102     case NSMouseExited:
1103       /* Exited always is to the root window as far as we are concerned,
1104        * since there is no way to reliably get information about what new
1105        * window is entered when exiting one.
1106        */
1107       if (!(window->event_mask & GDK_LEAVE_NOTIFY_MASK))
1108         return FALSE;
1109
1110       fill_crossing_event (window, event, nsevent,
1111                            x, y,
1112                            x_root, y_root,
1113                            GDK_LEAVE_NOTIFY,
1114                            GDK_CROSSING_NORMAL,
1115                            GDK_NOTIFY_ANCESTOR);
1116       return TRUE;
1117
1118     default:
1119       break;
1120     }
1121
1122   return FALSE;
1123 }
1124
1125 GdkModifierType
1126 _gdk_quartz_events_get_current_keyboard_modifiers (void)
1127 {
1128   if (gdk_quartz_osx_version () >= GDK_OSX_SNOW_LEOPARD)
1129     {
1130       return get_keyboard_modifiers_from_ns_flags ([NSClassFromString(@"NSEvent") modifierFlags]);
1131     }
1132   else
1133     {
1134       guint carbon_modifiers = GetCurrentKeyModifiers ();
1135       GdkModifierType modifiers = 0;
1136
1137       if (carbon_modifiers & alphaLock)
1138         modifiers |= GDK_LOCK_MASK;
1139       if (carbon_modifiers & shiftKey)
1140         modifiers |= GDK_SHIFT_MASK;
1141       if (carbon_modifiers & controlKey)
1142         modifiers |= GDK_CONTROL_MASK;
1143       if (carbon_modifiers & optionKey)
1144         modifiers |= GDK_MOD1_MASK;
1145       if (carbon_modifiers & cmdKey)
1146         modifiers |= GDK_MOD2_MASK;
1147
1148       return modifiers;
1149     }
1150 }
1151
1152 GdkModifierType
1153 _gdk_quartz_events_get_current_mouse_modifiers (void)
1154 {
1155   if (gdk_quartz_osx_version () >= GDK_OSX_SNOW_LEOPARD)
1156     {
1157       return get_mouse_button_modifiers_from_ns_buttons ([NSClassFromString(@"NSEvent") pressedMouseButtons]);
1158     }
1159   else
1160     {
1161       return get_mouse_button_modifiers_from_ns_buttons (GetCurrentButtonState ());
1162     }
1163 }
1164
1165 /* Detect window resizing */
1166
1167 static gboolean
1168 test_resize (NSEvent *event, GdkWindow *toplevel, gint x, gint y)
1169 {
1170   GdkWindowImplQuartz *toplevel_impl;
1171   gboolean lion;
1172
1173   /* Resizing from the resize indicator only begins if an NSLeftMouseButton
1174    * event is received in the resizing area.
1175    */
1176   toplevel_impl = (GdkWindowImplQuartz *)toplevel->impl;
1177   if ([toplevel_impl->toplevel showsResizeIndicator])
1178   if ([event type] == NSLeftMouseDown &&
1179       [toplevel_impl->toplevel showsResizeIndicator])
1180     {
1181       NSRect frame;
1182
1183       /* If the resize indicator is visible and the event
1184        * is in the lower right 15x15 corner, we leave these
1185        * events to Cocoa as to be handled as resize events.
1186        * Applications may have widgets in this area.  These
1187        * will most likely be larger than 15x15 and for
1188        * scroll bars there are also other means to move
1189        * the scroll bar.  Since the resize indicator is
1190        * the only way of resizing windows on Mac OS, it
1191        * is too important to not make functional.
1192        */
1193       frame = [toplevel_impl->view bounds];
1194       if (x > frame.size.width - GRIP_WIDTH &&
1195           x < frame.size.width &&
1196           y > frame.size.height - GRIP_HEIGHT &&
1197           y < frame.size.height)
1198         return TRUE;
1199      }
1200
1201   /* If we're on Lion and within 5 pixels of an edge,
1202    * then assume that the user wants to resize, and
1203    * return NULL to let Quartz get on with it. We check
1204    * the selector isRestorable to see if we're on 10.7.
1205    * This extra check is in case the user starts
1206    * dragging before GDK recognizes the grab.
1207    *
1208    * We perform this check for a button press of all buttons, because we
1209    * do receive, for instance, a right mouse down event for a GDK window
1210    * for x-coordinate range [-3, 0], but we do not want to forward this
1211    * into GDK. Forwarding such events into GDK will confuse the pointer
1212    * window finding code, because there are no GdkWindows present in
1213    * the range [-3, 0].
1214    */
1215   lion = gdk_quartz_osx_version () >= GDK_OSX_LION;
1216   if (lion &&
1217       ([event type] == NSLeftMouseDown ||
1218        [event type] == NSRightMouseDown ||
1219        [event type] == NSOtherMouseDown))
1220     {
1221       if (x < GDK_LION_RESIZE ||
1222           x > toplevel->width - GDK_LION_RESIZE ||
1223           y > toplevel->height - GDK_LION_RESIZE)
1224         return TRUE;
1225     }
1226
1227   return FALSE;
1228 }
1229
1230 static gboolean
1231 gdk_event_translate (GdkEvent *event,
1232                      NSEvent  *nsevent)
1233 {
1234   NSEventType event_type;
1235   NSWindow *nswindow;
1236   GdkWindow *window;
1237   int x, y;
1238   int x_root, y_root;
1239   gboolean return_val;
1240
1241   /* There is no support for real desktop wide grabs, so we break
1242    * grabs when the application loses focus (gets deactivated).
1243    */
1244   event_type = [nsevent type];
1245   if (event_type == NSAppKitDefined)
1246     {
1247       if ([nsevent subtype] == NSApplicationDeactivatedEventType)
1248         _gdk_quartz_events_break_all_grabs (get_time_from_ns_event (nsevent));
1249
1250       /* This could potentially be used to break grabs when clicking
1251        * on the title. The subtype 20 is undocumented so it's probably
1252        * not a good idea: else if (subtype == 20) break_all_grabs ();
1253        */
1254
1255       /* Leave all AppKit events to AppKit. */
1256       return FALSE;
1257     }
1258
1259   if (_gdk_default_filters)
1260     {
1261       /* Apply global filters */
1262       GdkFilterReturn result;
1263
1264       result = gdk_event_apply_filters (nsevent, event, &_gdk_default_filters);
1265       if (result != GDK_FILTER_CONTINUE)
1266         {
1267           return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
1268           goto done;
1269         }
1270     }
1271
1272   nswindow = [nsevent window];
1273
1274   /* Ignore events for windows not created by GDK. */
1275   if (nswindow && ![[nswindow contentView] isKindOfClass:[GdkQuartzView class]])
1276     return FALSE;
1277
1278   /* Ignore events for ones with no windows */
1279   if (!nswindow)
1280     {
1281       GdkWindow *toplevel = NULL;
1282
1283       if (event_type == NSMouseMoved)
1284         {
1285           /* Motion events received after clicking the menu bar do not have the
1286            * window field set.  Instead of giving up on the event immediately,
1287            * we first check whether this event is within our window bounds.
1288            */
1289           NSPoint screen_point = [NSEvent mouseLocation];
1290           gint x_tmp, y_tmp;
1291
1292           toplevel = find_toplevel_under_pointer (_gdk_display,
1293                                                   screen_point,
1294                                                   &x_tmp, &y_tmp);
1295         }
1296
1297       if (!toplevel)
1298         return FALSE;
1299     }
1300
1301   /* Ignore events and break grabs while the window is being
1302    * dragged. This is a workaround for the window getting events for
1303    * the window title.
1304    */
1305   if ([(GdkQuartzNSWindow *)nswindow isInMove])
1306     {
1307       _gdk_quartz_events_break_all_grabs (get_time_from_ns_event (nsevent));
1308       return FALSE;
1309     }
1310
1311   /* Also when in a manual resize, we ignore events so that these are
1312    * pushed to GdkQuartzWindow's sendEvent handler.
1313    */
1314   if ([(GdkQuartzWindow *)nswindow isInManualResize])
1315     return FALSE;
1316
1317   /* Find the right GDK window to send the event to, taking grabs and
1318    * event masks into consideration.
1319    */
1320   window = find_window_for_ns_event (nsevent, &x, &y, &x_root, &y_root);
1321   if (!window)
1322     return FALSE;
1323
1324   /* Quartz handles resizing on its own, so we want to stay out of the way. */
1325   if (test_resize (nsevent, window, x, y))
1326     return FALSE;
1327
1328   /* Apply any window filters. */
1329   if (GDK_IS_WINDOW (window))
1330     {
1331       GdkFilterReturn result;
1332
1333       if (window->filters)
1334         {
1335           g_object_ref (window);
1336
1337           result = gdk_event_apply_filters (nsevent, event, &window->filters);
1338
1339           g_object_unref (window);
1340
1341           if (result != GDK_FILTER_CONTINUE)
1342             {
1343               return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
1344               goto done;
1345             }
1346         }
1347     }
1348
1349   /* If the app is not active leave the event to AppKit so the window gets
1350    * focused correctly and don't do click-through (so we behave like most
1351    * native apps). If the app is active, we focus the window and then handle
1352    * the event, also to match native apps.
1353    */
1354   if ((event_type == NSRightMouseDown ||
1355        event_type == NSOtherMouseDown ||
1356        event_type == NSLeftMouseDown))
1357     {
1358       GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (window->impl);
1359
1360       if (![NSApp isActive])
1361         {
1362           [NSApp activateIgnoringOtherApps:YES];
1363           return FALSE;
1364         }
1365       else if (![impl->toplevel isKeyWindow])
1366         {
1367           GdkDeviceGrabInfo *grab;
1368
1369           grab = _gdk_display_get_last_device_grab (_gdk_display,
1370                                                     _gdk_display->core_pointer);
1371           if (!grab)
1372             [impl->toplevel makeKeyWindow];
1373         }
1374     }
1375
1376   return_val = TRUE;
1377
1378   switch (event_type)
1379     {
1380     case NSLeftMouseDown:
1381     case NSRightMouseDown:
1382     case NSOtherMouseDown:
1383     case NSLeftMouseUp:
1384     case NSRightMouseUp:
1385     case NSOtherMouseUp:
1386       fill_button_event (window, event, nsevent, x, y, x_root, y_root);
1387       break;
1388
1389     case NSLeftMouseDragged:
1390     case NSRightMouseDragged:
1391     case NSOtherMouseDragged:
1392     case NSMouseMoved:
1393       fill_motion_event (window, event, nsevent, x, y, x_root, y_root);
1394       break;
1395
1396     case NSScrollWheel:
1397       {
1398         GdkScrollDirection direction;
1399         float dx;
1400         float dy;
1401 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_7_AND_LATER
1402         if (gdk_quartz_osx_version() >= GDK_OSX_LION &&
1403             [nsevent hasPreciseScrollingDeltas])
1404           {
1405             dx = [nsevent scrollingDeltaX];
1406             dy = [nsevent scrollingDeltaY];
1407             direction = GDK_SCROLL_SMOOTH;
1408
1409             fill_scroll_event (window, event, nsevent, x, y, x_root, y_root,
1410                                -dx, -dy, direction);
1411
1412             /* Fall through for scroll buttons emulation */
1413           }
1414 #endif
1415         dx = [nsevent deltaX];
1416         dy = [nsevent deltaY];
1417
1418         if (dy != 0.0)
1419           {
1420             if (dy < 0.0)
1421               direction = GDK_SCROLL_DOWN;
1422             else
1423               direction = GDK_SCROLL_UP;
1424
1425             dy = fabs (dy);
1426             dx = 0.0;
1427           }
1428         else if (dx != 0.0)
1429           {
1430             if (dx < 0.0)
1431               direction = GDK_SCROLL_RIGHT;
1432             else
1433               direction = GDK_SCROLL_LEFT;
1434
1435             dx = fabs (dx);
1436             dy = 0.0;
1437           }
1438
1439         if (dx != 0.0 || dy != 0.0)
1440           {
1441 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_7_AND_LATER
1442             if (gdk_quartz_osx_version() >= GDK_OSX_LION &&
1443                 [nsevent hasPreciseScrollingDeltas])
1444               {
1445                 GdkEvent *emulated_event;
1446
1447                 emulated_event = gdk_event_new (GDK_SCROLL);
1448                 _gdk_event_set_pointer_emulated (emulated_event, TRUE);
1449                 fill_scroll_event (window, emulated_event, nsevent,
1450                                    x, y, x_root, y_root,
1451                                    dx, dy, direction);
1452                 append_event (emulated_event, TRUE);
1453               }
1454             else
1455 #endif
1456               fill_scroll_event (window, event, nsevent,
1457                                  x, y, x_root, y_root,
1458                                  dx, dy, direction);
1459           }
1460       }
1461       break;
1462
1463     case NSMouseEntered:
1464     case NSMouseExited:
1465       return_val = synthesize_crossing_event (window, event, nsevent, x, y, x_root, y_root);
1466       break;
1467
1468     case NSKeyDown:
1469     case NSKeyUp:
1470     case NSFlagsChanged:
1471       {
1472         GdkEventType type;
1473
1474         type = _gdk_quartz_keys_event_type (nsevent);
1475         if (type == GDK_NOTHING)
1476           return_val = FALSE;
1477         else
1478           fill_key_event (window, event, nsevent, type);
1479       }
1480       break;
1481
1482     default:
1483       /* Ignore everything elsee. */
1484       return_val = FALSE;
1485       break;
1486     }
1487
1488  done:
1489   if (return_val)
1490     {
1491       if (event->any.window)
1492         g_object_ref (event->any.window);
1493       if (((event->any.type == GDK_ENTER_NOTIFY) ||
1494            (event->any.type == GDK_LEAVE_NOTIFY)) &&
1495           (event->crossing.subwindow != NULL))
1496         g_object_ref (event->crossing.subwindow);
1497     }
1498   else
1499     {
1500       /* Mark this event as having no resources to be freed */
1501       event->any.window = NULL;
1502       event->any.type = GDK_NOTHING;
1503     }
1504
1505   return return_val;
1506 }
1507
1508 void
1509 _gdk_quartz_display_queue_events (GdkDisplay *display)
1510 {  
1511   NSEvent *nsevent;
1512
1513   nsevent = _gdk_quartz_event_loop_get_pending ();
1514   if (nsevent)
1515     {
1516       GdkEvent *event;
1517       GList *node;
1518
1519       event = gdk_event_new (GDK_NOTHING);
1520
1521       event->any.window = NULL;
1522       event->any.send_event = FALSE;
1523
1524       ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
1525
1526       node = _gdk_event_queue_append (display, event);
1527
1528       if (gdk_event_translate (event, nsevent))
1529         {
1530           ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
1531           _gdk_windowing_got_event (display, node, event, 0);
1532         }
1533       else
1534         {
1535           _gdk_event_queue_remove_link (display, node);
1536           g_list_free_1 (node);
1537           gdk_event_free (event);
1538
1539           gdk_threads_leave ();
1540           [NSApp sendEvent:nsevent];
1541           gdk_threads_enter ();
1542         }
1543
1544       _gdk_quartz_event_loop_release_event (nsevent);
1545     }
1546 }
1547
1548 void
1549 _gdk_quartz_screen_broadcast_client_message (GdkScreen *screen,
1550                                              GdkEvent  *event)
1551 {
1552   /* Not supported. */
1553 }
1554
1555 gboolean
1556 _gdk_quartz_screen_get_setting (GdkScreen   *screen,
1557                                 const gchar *name,
1558                                 GValue      *value)
1559 {
1560   if (strcmp (name, "gtk-double-click-time") == 0)
1561     {
1562       NSUserDefaults *defaults;
1563       float t;
1564
1565       GDK_QUARTZ_ALLOC_POOL;
1566
1567       defaults = [NSUserDefaults standardUserDefaults];
1568             
1569       t = [defaults floatForKey:@"com.apple.mouse.doubleClickThreshold"];
1570       if (t == 0.0)
1571         {
1572           /* No user setting, use the default in OS X. */
1573           t = 0.5;
1574         }
1575
1576       GDK_QUARTZ_RELEASE_POOL;
1577
1578       g_value_set_int (value, t * 1000);
1579
1580       return TRUE;
1581     }
1582   else if (strcmp (name, "gtk-font-name") == 0)
1583     {
1584       NSString *name;
1585       char *str;
1586
1587       GDK_QUARTZ_ALLOC_POOL;
1588
1589       name = [[NSFont systemFontOfSize:0] familyName];
1590
1591       /* Let's try to use the "views" font size (12pt) by default. This is
1592        * used for lists/text/other "content" which is the largest parts of
1593        * apps, using the "regular control" size (13pt) looks a bit out of
1594        * place. We might have to tweak this.
1595        */
1596
1597       /* The size has to be hardcoded as there doesn't seem to be a way to
1598        * get the views font size programmatically.
1599        */
1600       str = g_strdup_printf ("%s 12", [name UTF8String]);
1601       g_value_set_string (value, str);
1602       g_free (str);
1603
1604       GDK_QUARTZ_RELEASE_POOL;
1605
1606       return TRUE;
1607     }
1608   else if (strcmp (name, "gtk-primary-button-warps-slider") == 0)
1609     {
1610       GDK_QUARTZ_ALLOC_POOL;
1611
1612       BOOL setting = [[NSUserDefaults standardUserDefaults] boolForKey:@"AppleScrollerPagingBehavior"];
1613
1614       /* If the Apple property is YES, it means "warp" */
1615       g_value_set_boolean (value, setting == YES);
1616
1617       GDK_QUARTZ_RELEASE_POOL;
1618
1619       return TRUE;
1620     }
1621   
1622   /* FIXME: Add more settings */
1623
1624   return FALSE;
1625 }
1626
1627 void
1628 _gdk_quartz_display_event_data_copy (GdkDisplay     *display,
1629                                      const GdkEvent *src,
1630                                      GdkEvent       *dst)
1631 {
1632   GdkEventPrivate *priv_src = (GdkEventPrivate *) src;
1633   GdkEventPrivate *priv_dst = (GdkEventPrivate *) dst;
1634
1635   if (priv_src->windowing_data)
1636     {
1637       priv_dst->windowing_data = priv_src->windowing_data;
1638       [(NSEvent *)priv_dst->windowing_data retain];
1639     }
1640 }
1641
1642 void
1643 _gdk_quartz_display_event_data_free (GdkDisplay *display,
1644                                      GdkEvent   *event)
1645 {
1646   GdkEventPrivate *priv = (GdkEventPrivate *) event;
1647
1648   if (priv->windowing_data)
1649     {
1650       [(NSEvent *)priv->windowing_data release];
1651       priv->windowing_data = NULL;
1652     }
1653 }