Debian Codegraph @

gdk/quartz/gdkwindow-quartz.c

raw
1 /* gdkwindow-quartz.c
2 *
3 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * Copyright (C) 2005-2007 Imendio AB
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21
22 #include <gdk/gdk.h>
23 #include <gdk/gdkdeviceprivate.h>
24 #include <gdk/gdkdisplayprivate.h>
25 #include <gdk/gdkframeclockprivate.h>
26
27 #include "gdkwindowimpl.h"
28 #include "gdkwindow-quartz.h"
29 #include "gdkprivate-quartz.h"
30 #include "gdkglcontext-quartz.h"
31 #include "gdkquartzglcontext.h"
32 #include "gdkquartzscreen.h"
33 #include "gdkquartzcursor.h"
34 #include "gdkquartz-cocoa-access.h"
35 #include "gdkinternal-quartz.h"
36
37 #include <Carbon/Carbon.h>
38 #include <AvailabilityMacros.h>
39
40 #include <sys/time.h>
41 #include <cairo-quartz.h>
42
43 static gpointer parent_class;
44 static gpointer root_window_parent_class;
45
46 static GSList *update_nswindows;
47 static gboolean in_process_all_updates = FALSE;
48
49 static GSList *main_window_stack;
50
51 typedef struct
52 {
53 gint x, y;
54 gint width, height;
55 GdkWMDecoration decor;
56 } FullscreenSavedGeometry;
57
58 #if MAC_OS_X_VERSION_MIN_REQUIRED < 101200
59 typedef enum
60 {
61 GDK_QUARTZ_BORDERLESS_WINDOW = NSBorderlessWindowMask,
62 GDK_QUARTZ_CLOSABLE_WINDOW = NSClosableWindowMask,
63 #if MAC_OS_X_VERSION_MIN_REQUIRED > 1060
64 /* Added in 10.7. Apple's docs are wrong to say it's from earlier. */
65 GDK_QUARTZ_FULLSCREEN_WINDOW = NSFullScreenWindowMask,
66 #endif
67 GDK_QUARTZ_MINIATURIZABLE_WINDOW = NSMiniaturizableWindowMask,
68 GDK_QUARTZ_RESIZABLE_WINDOW = NSResizableWindowMask,
69 GDK_QUARTZ_TITLED_WINDOW = NSTitledWindowMask,
70 } GdkQuartzWindowMask;
71 #else
72 typedef enum
73 {
74 GDK_QUARTZ_BORDERLESS_WINDOW = NSWindowStyleMaskBorderless,
75 GDK_QUARTZ_CLOSABLE_WINDOW = NSWindowStyleMaskClosable,
76 GDK_QUARTZ_FULLSCREEN_WINDOW = NSWindowStyleMaskFullScreen,
77 GDK_QUARTZ_MINIATURIZABLE_WINDOW = NSWindowStyleMaskMiniaturizable,
78 GDK_QUARTZ_RESIZABLE_WINDOW = NSWindowStyleMaskResizable,
79 GDK_QUARTZ_TITLED_WINDOW = NSWindowStyleMaskTitled,
80 } GdkQuartzWindowMask;
81 #endif
82
83 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
84 static FullscreenSavedGeometry *get_fullscreen_geometry (GdkWindow *window);
85 #endif
86
87 #define FULLSCREEN_DATA "fullscreen-data"
88
89 static void update_toplevel_order (void);
90 static void clear_toplevel_order (void);
91
92 #define WINDOW_IS_TOPLEVEL(window) \
93 (GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD && \
94 GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN && \
95 GDK_WINDOW_TYPE (window) != GDK_WINDOW_OFFSCREEN)
96
97 /*
98 * GdkQuartzWindow
99 */
100
101 struct _GdkQuartzWindow
102 {
103 GdkWindow parent;
104 };
105
106 struct _GdkQuartzWindowClass
107 {
108 GdkWindowClass parent_class;
109 };
110
111 G_DEFINE_TYPE (GdkQuartzWindow, gdk_quartz_window, GDK_TYPE_WINDOW);
112
113 static void
114 gdk_quartz_window_class_init (GdkQuartzWindowClass *quartz_window_class)
115 {
116 }
117
118 static void
119 gdk_quartz_window_init (GdkQuartzWindow *quartz_window)
120 {
121 }
122
123
124 /*
125 * GdkQuartzWindowImpl
126 */
127 static inline GdkWindowImplQuartz*
128 gdk_window_get_quartz_impl (GdkWindow* window)
129 {
130 GdkWindow *embedder;
131
132 if (GDK_WINDOW_DESTROYED (window))
133 return NULL;
134
135 embedder = gdk_offscreen_window_get_embedder (window);
136
137 while (embedder)
138 {
139 window = embedder;
140 embedder = gdk_offscreen_window_get_embedder (window);
141 }
142
143 g_return_val_if_fail (GDK_IS_WINDOW_IMPL_QUARTZ (window->impl), NULL);
144
145 return GDK_WINDOW_IMPL_QUARTZ (window->impl);
146 }
147
148 NSView *
149 gdk_quartz_window_get_nsview (GdkWindow *window)
150 {
151 GdkWindowImplQuartz *impl = gdk_window_get_quartz_impl (window);
152
153 return impl ? impl->view : NULL;
154 }
155
156 NSWindow *
157 gdk_quartz_window_get_nswindow (GdkWindow *window)
158 {
159 GdkWindowImplQuartz *impl = gdk_window_get_quartz_impl (window);
160
161 return impl ? impl->toplevel : NULL;
162 }
163
164 static CGContextRef
165 gdk_window_impl_quartz_get_context (GdkWindowImplQuartz *window_impl,
166 gboolean antialias)
167 {
168 CGContextRef cg_context = NULL;
169
170 if (GDK_WINDOW_DESTROYED (window_impl->wrapper))
171 return NULL;
172
173 /* Lock focus when not called as part of a drawRect call. This
174 * is needed when called from outside "real" expose events, for
175 * example for synthesized expose events when realizing windows
176 * and for widgets that send fake expose events like the arrow
177 * buttons in spinbuttons or the position marker in rulers.
178 */
179 #if MAC_OS_X_VERSION_MAX_ALLOWED < 101400
180 if (gdk_quartz_osx_version() < GDK_OSX_MOJAVE &&
181 window_impl->in_paint_rect_count == 0)
182 {
183 /* The NSView focus-locking API set was deprecated in MacOS 10.14 and
184 * has a significant cost in MacOS 11 - every lock/unlock seems to
185 * trigger a drawRect: call for the entire window. To return the
186 * lost performance, do not use the locking API in MacOS 11+
187 */
188 if (![window_impl->view lockFocusIfCanDraw])
189 return NULL;
190 }
191 #endif
192 #if MAC_OS_X_VERSION_MIN_REQUIRED < 101000
193 if (gdk_quartz_osx_version () < GDK_OSX_YOSEMITE)
194 cg_context = [[NSGraphicsContext currentContext] graphicsPort];
195 else
196 #endif
197 cg_context = [[NSGraphicsContext currentContext] CGContext];
198
199 if (!cg_context)
200 return NULL;
201 CGContextSaveGState (cg_context);
202 CGContextSetAllowsAntialiasing (cg_context, antialias);
203
204 return cg_context;
205 }
206
207 static void
208 gdk_window_impl_quartz_release_context (GdkWindowImplQuartz *window_impl,
209 CGContextRef cg_context)
210 {
211 if (cg_context)
212 {
213 CGContextRestoreGState (cg_context);
214 CGContextSetAllowsAntialiasing (cg_context, TRUE);
215 }
216
217 /* See comment in gdk_quartz_window_get_context(). */
218 #if MAC_OS_X_VERSION_MIN_REQUIRED < 101400
219 if (gdk_quartz_osx_version() < GDK_OSX_MOJAVE &&
220 window_impl->in_paint_rect_count == 0)
221 {
222 [window_impl->toplevel flushWindow];
223 [window_impl->view unlockFocus];
224 }
225 #endif
226 }
227
228 static void
229 gdk_window_impl_quartz_finalize (GObject *object)
230 {
231 GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (object);
232 GdkDisplay *display = gdk_window_get_display (impl->wrapper);
233 GdkSeat *seat = gdk_display_get_default_seat (display);
234
235 gdk_seat_ungrab (seat);
236
237 if (impl->transient_for)
238 g_object_unref (impl->transient_for);
239
240 if (impl->view)
241 {
242 [[NSNotificationCenter defaultCenter] removeObserver: impl->toplevel
243 name: @"NSViewFrameDidChangeNotification"
244 object: impl->view];
245 [impl->view release];
246 }
247
248 impl->view = NULL;
249 impl->transient_for = NULL;
250 impl->toplevel = NULL;
251
252 G_OBJECT_CLASS (parent_class)->finalize (object);
253 }
254
255 static cairo_user_data_key_t gdk_quartz_cairo_key;
256
257 typedef struct {
258 GdkWindowImplQuartz *window_impl;
259 CGContextRef cg_context;
260 } GdkQuartzCairoSurfaceData;
261
262 static void
263 gdk_quartz_cairo_surface_destroy (void *data)
264 {
265 GdkQuartzCairoSurfaceData *surface_data = data;
266 cairo_surface_t *surface = surface_data->window_impl->cairo_surface;
267
268 if (!cairo_surface_get_reference_count (surface))
269 surface_data->window_impl->cairo_surface = NULL;
270
271 g_free (surface_data);
272 }
273
274 static cairo_surface_t *
275 gdk_quartz_create_cairo_surface (GdkWindowImplQuartz *impl,
276 int width,
277 int height)
278 {
279 GdkQuartzCairoSurfaceData *surface_data;
280 cairo_surface_t *surface;
281
282 surface_data = g_new (GdkQuartzCairoSurfaceData, 1);
283 surface_data->window_impl = impl;
284 surface_data->cg_context = NULL;
285
286 [impl->view createBackingStoreWithWidth: width andHeight: height];
287 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
288 cairo_surface_set_user_data (surface, &gdk_quartz_cairo_key,
289 surface_data,
290 gdk_quartz_cairo_surface_destroy);
291
292 return surface;
293 }
294
295 static cairo_surface_t *
296 gdk_quartz_ref_cairo_surface (GdkWindow *window)
297 {
298 GdkWindowImplQuartz *impl = gdk_window_get_quartz_impl (window);
299
300 if (GDK_WINDOW_DESTROYED (window) || !impl)
301 return NULL;
302
303 if (!impl->cairo_surface)
304 {
305 gint width = gdk_window_get_width (impl->wrapper);
306 gint height = gdk_window_get_height (impl->wrapper);
307 gint scale = gdk_window_get_scale_factor (impl->wrapper);
308 gint scaled_width = width * scale;
309 const gint align = GDK_WINDOW_QUARTZ_ALIGNMENT;
310
311 if (scaled_width % align)
312 scaled_width += align - scaled_width % align; // Surface widths must be 4-pixel aligned
313
314 impl->cairo_surface = gdk_quartz_create_cairo_surface (impl,
315 scaled_width,
316 height * scale);
317 cairo_surface_set_device_scale (impl->cairo_surface, scale, scale);
318 cairo_surface_reference (impl->cairo_surface); // The caller will destroy the returned one.
319 }
320 else
321 {
322 cairo_surface_reference (impl->cairo_surface);
323 }
324
325 return impl->cairo_surface;
326 }
327
328 void
329 _gdk_quartz_unref_cairo_surface (GdkWindow *window)
330 {
331 GdkWindowImplQuartz *impl = gdk_window_get_quartz_impl (window);
332 if (GDK_WINDOW_DESTROYED (window) || !impl)
333 return;
334
335 if (impl->cairo_surface)
336 {
337 cairo_surface_destroy (impl->cairo_surface);
338 if (impl->cairo_surface &&
339 !cairo_surface_get_reference_count (impl->cairo_surface))
340 impl->cairo_surface = NULL;
341 }
342 }
343
344 static void
345 gdk_window_impl_quartz_init (GdkWindowImplQuartz *impl)
346 {
347 impl->type_hint = GDK_WINDOW_TYPE_HINT_NORMAL;
348 impl->view = NULL;
349 impl->toplevel = NULL;
350 impl->transient_for = NULL;
351 }
352
353 static gboolean
354 gdk_window_impl_quartz_begin_paint (GdkWindow *window)
355 {
356 gdk_quartz_ref_cairo_surface (window); //unreffed in GdkQuartzView::updateLayer
357 return FALSE;
358 }
359
360 static void
361 gdk_quartz_window_set_needs_display_in_region (GdkWindow *window,
362 cairo_region_t *region)
363 {
364 GdkWindowImplQuartz *impl;
365 int i, n_rects;
366
367 impl = gdk_window_get_quartz_impl (window);
368
369 if (!impl)
370 return;
371
372 if (!impl->needs_display_region)
373 impl->needs_display_region = cairo_region_create ();
374
375 cairo_region_union (impl->needs_display_region, region);
376
377 n_rects = cairo_region_num_rectangles (region);
378 for (i = 0; i < n_rects; i++)
379 {
380 cairo_rectangle_int_t rect;
381 cairo_region_get_rectangle (region, i, &rect);
382 [impl->view setNeedsDisplayInRect:NSMakeRect (rect.x, rect.y,
383 rect.width, rect.height)];
384 }
385 }
386
387 void
388 _gdk_quartz_window_process_updates_recurse (GdkWindow *window,
389 cairo_region_t *region)
390 {
391 /* Make sure to only flush each toplevel at most once if we're called
392 * from process_all_updates.
393 */
394 if (in_process_all_updates)
395 {
396 GdkWindow *toplevel= gdk_window_get_effective_toplevel (window);
397 GdkWindowImplQuartz *toplevel_impl =
398 gdk_window_get_quartz_impl (toplevel);
399
400 if (toplevel && WINDOW_IS_TOPLEVEL (toplevel) && toplevel_impl)
401 {
402 NSWindow *nswindow = toplevel_impl->toplevel;
403 #if MAC_OS_X_VERSION_MIN_REQUIRED < 101400
404 /* In theory, we could skip the flush disabling, since we only
405 * have one NSView.
406 */
407 if (gdk_quartz_osx_version() < GDK_OSX_MOJAVE &&
408 nswindow && ![nswindow isFlushWindowDisabled])
409 {
410 [nswindow retain];
411 [nswindow disableFlushWindow];
412 }
413 #endif
414 update_nswindows = g_slist_prepend (update_nswindows, nswindow);
415 }
416 }
417
418 if (WINDOW_IS_TOPLEVEL (window))
419 gdk_quartz_window_set_needs_display_in_region (window, region);
420 else
421 _gdk_window_process_updates_recurse (window, region);
422
423 /* NOTE: I'm not sure if we should displayIfNeeded here. It slows down a
424 * lot (since it triggers the beam syncing) and things seem to work
425 * without it.
426 */
427 }
428
429 void
430 _gdk_quartz_display_before_process_all_updates (GdkDisplay *display)
431 {
432 in_process_all_updates = TRUE;
433
434 if (gdk_quartz_osx_version () >= GDK_OSX_EL_CAPITAN)
435 {
436 [NSAnimationContext endGrouping];
437 }
438 #if MAC_OS_X_VERSION_MIN_REQUIRED < 101100
439 else
440 {
441 NSDisableScreenUpdates ();
442 }
443 #endif
444 }
445
446 void
447 _gdk_quartz_display_after_process_all_updates (GdkDisplay *display)
448 {
449 GSList *tmp_list = update_nswindows;
450
451 update_nswindows = NULL;
452
453 while (tmp_list)
454 {
455 NSWindow *nswindow = tmp_list->data;
456
457 [[nswindow contentView] displayIfNeeded];
458
459 #if MAC_OS_X_VERSION_MIN_REQUIRED < 101400
460 if(gdk_quartz_osx_version() < GDK_OSX_BIGSUR)
461 {
462 [nswindow enableFlushWindow];
463 [nswindow flushWindow];
464 }
465 #endif
466 [nswindow release];
467
468 tmp_list = g_slist_remove_link (tmp_list, tmp_list);
469 }
470
471 in_process_all_updates = FALSE;
472
473 if (gdk_quartz_osx_version() >= GDK_OSX_EL_CAPITAN)
474 {
475 [NSAnimationContext beginGrouping];
476 }
477 #if MAC_OS_X_VERSION_MIN_REQUIRED < 101100
478 else
479 {
480 NSEnableScreenUpdates ();
481 }
482 #endif
483 }
484
485 static const gchar *
486 get_default_title (void)
487 {
488 const char *title;
489
490 title = g_get_application_name ();
491 if (!title)
492 title = g_get_prgname ();
493
494 return title;
495 }
496
497 static void
498 get_ancestor_coordinates_from_child (GdkWindow *child_window,
499 gint child_x,
500 gint child_y,
501 GdkWindow *ancestor_window,
502 gint *ancestor_x,
503 gint *ancestor_y)
504 {
505 while (child_window != ancestor_window)
506 {
507 child_x += child_window->x;
508 child_y += child_window->y;
509
510 child_window = child_window->parent;
511 }
512
513 *ancestor_x = child_x;
514 *ancestor_y = child_y;
515 }
516
517 void
518 _gdk_quartz_window_debug_highlight (GdkWindow *window, gint number)
519 {
520 gint x, y;
521 gint gx, gy;
522 GdkWindow *toplevel;
523 gint tx, ty;
524 static NSWindow *debug_window[10];
525 static NSRect old_rect[10];
526 NSRect rect;
527 NSColor *color;
528
529 g_return_if_fail (number >= 0 && number <= 9);
530
531 if (window == _gdk_root)
532 return;
533
534 if (window == NULL)
535 {
536 if (debug_window[number])
537 [debug_window[number] close];
538 debug_window[number] = NULL;
539
540 return;
541 }
542
543 toplevel = gdk_window_get_toplevel (window);
544 get_ancestor_coordinates_from_child (window, 0, 0, toplevel, &x, &y);
545
546 gdk_window_get_origin (toplevel, &tx, &ty);
547 x += tx;
548 y += ty;
549
550 _gdk_quartz_window_gdk_xy_to_xy (x, y + window->height,
551 &gx, &gy);
552
553 rect = NSMakeRect (gx, gy, window->width, window->height);
554
555 if (debug_window[number] && NSEqualRects (rect, old_rect[number]))
556 return;
557
558 old_rect[number] = rect;
559
560 if (debug_window[number])
561 [debug_window[number] close];
562
563 debug_window[number] = [[NSWindow alloc] initWithContentRect:rect
564 #if MAC_OS_X_VERSION_MIN_REQUIRED < 101200
565 styleMask:(NSUInteger)GDK_QUARTZ_BORDERLESS_WINDOW
566 #else
567 styleMask:(NSWindowStyleMask)GDK_QUARTZ_BORDERLESS_WINDOW
568 #endif
569 backing:NSBackingStoreBuffered
570 defer:NO];
571
572 switch (number)
573 {
574 case 0:
575 color = [NSColor redColor];
576 break;
577 case 1:
578 color = [NSColor blueColor];
579 break;
580 case 2:
581 color = [NSColor greenColor];
582 break;
583 case 3:
584 color = [NSColor yellowColor];
585 break;
586 case 4:
587 color = [NSColor brownColor];
588 break;
589 case 5:
590 color = [NSColor purpleColor];
591 break;
592 default:
593 color = [NSColor blackColor];
594 break;
595 }
596
597 [debug_window[number] setBackgroundColor:color];
598 [debug_window[number] setAlphaValue:0.4];
599 [debug_window[number] setOpaque:NO];
600 [debug_window[number] setReleasedWhenClosed:YES];
601 [debug_window[number] setIgnoresMouseEvents:YES];
602 [debug_window[number] setLevel:NSFloatingWindowLevel];
603
604 [debug_window[number] orderFront:nil];
605 }
606
607 gboolean
608 _gdk_quartz_window_is_ancestor (GdkWindow *ancestor,
609 GdkWindow *window)
610 {
611 if (ancestor == NULL || window == NULL)
612 return FALSE;
613
614 return (gdk_window_get_parent (window) == ancestor ||
615 _gdk_quartz_window_is_ancestor (ancestor,
616 gdk_window_get_parent (window)));
617 }
618
619
620 /* See notes on top of gdkscreen-quartz.c */
621 void
622 _gdk_quartz_window_gdk_xy_to_xy (gint gdk_x,
623 gint gdk_y,
624 gint *ns_x,
625 gint *ns_y)
626 {
627 GdkQuartzScreen *screen_quartz = GDK_QUARTZ_SCREEN (_gdk_screen);
628
629 if (ns_y)
630 *ns_y = screen_quartz->orig_y - gdk_y;
631
632 if (ns_x)
633 *ns_x = gdk_x + screen_quartz->orig_x;
634 }
635
636 void
637 _gdk_quartz_window_xy_to_gdk_xy (gint ns_x,
638 gint ns_y,
639 gint *gdk_x,
640 gint *gdk_y)
641 {
642 GdkQuartzScreen *screen_quartz = GDK_QUARTZ_SCREEN (_gdk_screen);
643
644 if (gdk_y)
645 *gdk_y = screen_quartz->orig_y - ns_y;
646
647 if (gdk_x)
648 *gdk_x = ns_x - screen_quartz->orig_x;
649 }
650
651 void
652 _gdk_quartz_window_nspoint_to_gdk_xy (NSPoint point,
653 gint *x,
654 gint *y)
655 {
656 _gdk_quartz_window_xy_to_gdk_xy (point.x, point.y,
657 x, y);
658 }
659
660 static GdkWindow *
661 find_child_window_helper (GdkWindow *window,
662 gint x,
663 gint y,
664 gint x_offset,
665 gint y_offset,
666 gboolean get_toplevel)
667 {
668 GdkWindowImplQuartz *impl;
669 GList *l;
670
671 impl = gdk_window_get_quartz_impl (window);
672
673 if (window == _gdk_root)
674 update_toplevel_order ();
675
676 g_return_val_if_fail (impl, NULL);
677
678 for (l = impl->sorted_children; l; l = l->next)
679 {
680 GdkWindow *child = l->data;
681 GdkWindowImplQuartz *child_impl = gdk_window_get_quartz_impl (child);
682 int temp_x, temp_y;
683
684 if (!GDK_WINDOW_IS_MAPPED (child))
685 continue;
686
687 temp_x = x_offset + child->x;
688 temp_y = y_offset + child->y;
689
690 /* Special-case the root window. We have to include the title
691 * bar in the checks, otherwise the window below the title bar
692 * will be found i.e. events punch through. (If we can find a
693 * better way to deal with the events in gdkevents-quartz, this
694 * might not be needed.)
695 */
696 if (child_impl && window == _gdk_root)
697 {
698 NSRect frame = NSMakeRect (0, 0, 100, 100);
699 NSRect content;
700 NSUInteger mask;
701 int titlebar_height;
702
703 mask = [child_impl->toplevel styleMask];
704
705 /* Get the title bar height. */
706 content = [NSWindow contentRectForFrameRect:frame
707 styleMask:mask];
708 titlebar_height = frame.size.height - content.size.height;
709
710 if (titlebar_height > 0 &&
711 x >= temp_x && y >= temp_y - titlebar_height &&
712 x < temp_x + child->width && y < temp_y)
713 {
714 /* The root means "unknown" i.e. a window not managed by
715 * GDK.
716 */
717 return (GdkWindow *)_gdk_root;
718 }
719 }
720
721 if ((!get_toplevel || (get_toplevel && window == _gdk_root)) &&
722 x >= temp_x && y >= temp_y &&
723 x < temp_x + child->width && y < temp_y + child->height)
724 {
725 /* Look for child windows. */
726 return find_child_window_helper (l->data,
727 x, y,
728 temp_x, temp_y,
729 get_toplevel);
730 }
731 }
732
733 return window;
734 }
735
736 /* Given a GdkWindow and coordinates relative to it, returns the
737 * innermost subwindow that contains the point. If the coordinates are
738 * outside the passed in window, NULL is returned.
739 */
740 GdkWindow *
741 _gdk_quartz_window_find_child (GdkWindow *window,
742 gint x,
743 gint y,
744 gboolean get_toplevel)
745 {
746 if (x >= 0 && y >= 0 && x < window->width && y < window->height)
747 return find_child_window_helper (window, x, y, 0, 0, get_toplevel);
748
749 return NULL;
750 }
751
752 /* Raises a transient window.
753 */
754 static void
755 raise_transient (GdkWindowImplQuartz *impl)
756 {
757 /* In quartz the transient-for behavior is implemented by
758 * attaching the transient-for GdkNSWindows to the parent's
759 * GdkNSWindow. Stacking is managed by Quartz and the order
760 * is that of the parent's childWindows array. The only way
761 * to change that order is to remove the child from the
762 * parent and then add it back in.
763 */
764 GdkWindowImplQuartz *parent_impl =
765 gdk_window_get_quartz_impl (impl->transient_for);
766 if (parent_impl)
767 {
768 [parent_impl->toplevel removeChildWindow:impl->toplevel];
769 [parent_impl->toplevel addChildWindow:impl->toplevel
770 ordered:NSWindowAbove];
771 }
772 }
773
774 void
775 _gdk_quartz_window_did_become_main (GdkWindow *window)
776 {
777 GdkWindowImplQuartz *impl = gdk_window_get_quartz_impl (window);
778
779 main_window_stack = g_slist_remove (main_window_stack, window);
780
781 if (window->window_type != GDK_WINDOW_TEMP)
782 main_window_stack = g_slist_prepend (main_window_stack, window);
783
784 if (impl &&
785 impl->transient_for && !GDK_WINDOW_DESTROYED (impl->transient_for))
786 raise_transient (impl);
787
788 clear_toplevel_order ();
789 }
790
791 void
792 _gdk_quartz_window_did_resign_main (GdkWindow *window)
793 {
794 /* Don't try to make another window key when one resigns main.
795 * macOS handles this automatically, and GTK's attempt to manage focus
796 * here causes focus fighting between windows.
797 * See: https://gitlab.gnome.org/GNOME/gimp/-/issues/14901
798 * https://gitlab.gnome.org/GNOME/gimp/-/issues/15480
799 */
800 clear_toplevel_order ();
801 }
802
803 static NSScreen *
804 get_nsscreen_for_point (gint x, gint y)
805 {
806 int i;
807 NSArray *screens;
808 NSScreen *screen = NULL;
809
810 GDK_QUARTZ_ALLOC_POOL;
811
812 screens = [NSScreen screens];
813
814 for (i = 0; i < [screens count]; i++)
815 {
816 NSRect rect = [[screens objectAtIndex:i] frame];
817
818 if (x >= rect.origin.x && x <= rect.origin.x + rect.size.width &&
819 y >= rect.origin.y && y <= rect.origin.y + rect.size.height)
820 {
821 screen = [screens objectAtIndex:i];
822 break;
823 }
824 }
825
826 GDK_QUARTZ_RELEASE_POOL;
827
828 return screen;
829 }
830
831 static void
832 on_frame_clock_before_paint (GdkFrameClock *frame_clock,
833 GdkWindow *window)
834 {
835 }
836
837 static void
838 on_frame_clock_after_paint (GdkFrameClock *frame_clock,
839 GdkWindow *window)
840 {
841 GdkWindowImplQuartz *impl = gdk_window_get_quartz_impl (window);
842 GdkDisplay *display = gdk_window_get_display (window);
843 GdkFrameTimings *timings;
844
845 timings = gdk_frame_clock_get_current_timings (frame_clock);
846 if (impl && timings != NULL)
847 impl->pending_frame_counter = timings->frame_counter;
848
849 _gdk_quartz_display_add_frame_callback (display, window);
850
851 _gdk_frame_clock_freeze (frame_clock);
852 }
853
854 void
855 _gdk_quartz_display_create_window_impl (GdkDisplay *display,
856 GdkWindow *window,
857 GdkWindow *real_parent,
858 GdkScreen *screen,
859 GdkEventMask event_mask,
860 GdkWindowAttr *attributes,
861 gint attributes_mask)
862 {
863 GdkWindowImplQuartz *impl;
864 GdkWindowImplQuartz *parent_impl;
865 GdkWindowTypeHint type_hint = GDK_WINDOW_TYPE_HINT_NORMAL;
866 GdkFrameClock *frame_clock;
867
868 GDK_QUARTZ_ALLOC_POOL;
869
870 impl = g_object_new (GDK_TYPE_WINDOW_IMPL_QUARTZ, NULL);
871 window->impl = GDK_WINDOW_IMPL (impl);
872 impl->wrapper = window;
873
874 parent_impl = gdk_window_get_quartz_impl (window->parent);
875
876 switch (window->window_type)
877 {
878 case GDK_WINDOW_TOPLEVEL:
879 case GDK_WINDOW_TEMP:
880 if (GDK_WINDOW_TYPE (window->parent) != GDK_WINDOW_ROOT)
881 {
882 /* The common code warns for this case */
883 parent_impl = gdk_window_get_quartz_impl (_gdk_root);
884 }
885 }
886
887 /* Maintain the z-ordered list of children. */
888 if (parent_impl && window->parent != _gdk_root)
889 parent_impl->sorted_children = g_list_prepend (parent_impl->sorted_children, window);
890 else
891 clear_toplevel_order ();
892
893 gdk_window_set_cursor (window, ((attributes_mask & GDK_WA_CURSOR) ?
894 (attributes->cursor) :
895 NULL));
896
897 if (attributes_mask & GDK_WA_TYPE_HINT)
898 {
899 type_hint = attributes->type_hint;
900 gdk_window_set_type_hint (window, type_hint);
901 }
902
903 switch (window->window_type)
904 {
905 case GDK_WINDOW_TOPLEVEL:
906 case GDK_WINDOW_TEMP:
907 {
908 NSScreen *screen;
909 NSRect screen_rect;
910 NSRect content_rect;
911 NSUInteger style_mask;
912 int nx, ny;
913 const char *title;
914 const gint scale = gdk_window_get_scale_factor (window);
915 const guint align = GDK_WINDOW_QUARTZ_ALIGNMENT / scale;
916
917 /* initWithContentRect will place on the mainScreen by default.
918 * We want to select the screen to place on ourselves. We need
919 * to find the screen the window will be on and correct the
920 * content_rect coordinates to be relative to that screen.
921 */
922 _gdk_quartz_window_gdk_xy_to_xy (window->x, window->y, &nx, &ny);
923
924 screen = get_nsscreen_for_point (nx, ny);
925 screen_rect = [screen frame];
926 nx -= screen_rect.origin.x;
927 ny -= screen_rect.origin.y;
928
929 if (window->width % align)
930 window->width += align - window->width % align;
931
932 content_rect = NSMakeRect (nx, ny - window->height,
933 window->width,
934 window->height);
935
936 if (window->window_type == GDK_WINDOW_TEMP ||
937 type_hint == GDK_WINDOW_TYPE_HINT_SPLASHSCREEN)
938 {
939 style_mask = GDK_QUARTZ_BORDERLESS_WINDOW;
940 }
941 else
942 {
943 style_mask = (GDK_QUARTZ_TITLED_WINDOW |
944 GDK_QUARTZ_CLOSABLE_WINDOW |
945 GDK_QUARTZ_MINIATURIZABLE_WINDOW |
946 GDK_QUARTZ_RESIZABLE_WINDOW);
947 }
948
949 impl->toplevel = [[GdkQuartzNSWindow alloc] initWithContentRect:content_rect
950 styleMask:style_mask
951 backing:NSBackingStoreBuffered
952 defer:NO
953 screen:screen];
954
955 if (type_hint != GDK_WINDOW_TYPE_HINT_NORMAL)
956 impl->toplevel.excludedFromWindowsMenu = true;
957
958 if (attributes_mask & GDK_WA_TITLE)
959 title = attributes->title;
960 else
961 title = get_default_title ();
962
963 gdk_window_set_title (window, title);
964
965 if (gdk_window_get_visual (window) == gdk_screen_get_rgba_visual (_gdk_screen))
966 {
967 [impl->toplevel setOpaque:NO];
968 [impl->toplevel setBackgroundColor:[NSColor clearColor]];
969 }
970
971 content_rect.origin.x = 0;
972 content_rect.origin.y = 0;
973
974 impl->view = [[GdkQuartzView alloc] initWithFrame:content_rect];
975 [impl->view setGdkWindow:window];
976 [impl->toplevel setContentView:impl->view];
977 [[NSNotificationCenter defaultCenter] addObserver: impl->toplevel
978 selector: @selector (windowDidResize:)
979 name: @"NSViewFrameDidChangeNotification"
980 object: impl->view];
981 }
982 break;
983
984 case GDK_WINDOW_CHILD:
985 {
986 GdkWindowImplQuartz *parent_impl = gdk_window_get_quartz_impl (window->parent);
987
988 if (parent_impl && !window->input_only)
989 {
990 NSRect frame_rect = NSMakeRect (window->x + window->parent->abs_x,
991 window->y + window->parent->abs_y,
992 window->width,
993 window->height);
994
995 impl->view = [[GdkQuartzView alloc] initWithFrame:frame_rect];
996
997 [impl->view setGdkWindow:window];
998
999 /* GdkWindows should be hidden by default */
1000 [impl->view setHidden:YES];
1001 [parent_impl->view addSubview:impl->view];
1002 }
1003 }
1004 break;
1005
1006 default:
1007 g_assert_not_reached ();
1008 }
1009
1010 GDK_QUARTZ_RELEASE_POOL;
1011
1012 if (attributes_mask & GDK_WA_TYPE_HINT)
1013 gdk_window_set_type_hint (window, attributes->type_hint);
1014
1015 frame_clock = gdk_window_get_frame_clock (window);
1016
1017 g_signal_connect (frame_clock, "before-paint",
1018 G_CALLBACK (on_frame_clock_before_paint), window);
1019 g_signal_connect (frame_clock, "after-paint",
1020 G_CALLBACK (on_frame_clock_after_paint), window);
1021 }
1022
1023 void
1024 _gdk_quartz_window_update_position (GdkWindow *window)
1025 {
1026 NSRect frame_rect;
1027 NSRect content_rect;
1028 GdkWindowImplQuartz *impl = gdk_window_get_quartz_impl (window);
1029
1030 if (!impl)
1031 return;
1032
1033 GDK_QUARTZ_ALLOC_POOL;
1034
1035 frame_rect = [impl->toplevel frame];
1036 content_rect = [impl->toplevel contentRectForFrameRect:frame_rect];
1037
1038 _gdk_quartz_window_xy_to_gdk_xy (content_rect.origin.x,
1039 content_rect.origin.y + content_rect.size.height,
1040 &window->x, &window->y);
1041
1042 GDK_QUARTZ_RELEASE_POOL;
1043 }
1044
1045 void
1046 _gdk_quartz_window_init_windowing (GdkDisplay *display,
1047 GdkScreen *screen)
1048 {
1049 GdkWindowImplQuartz *impl;
1050
1051 g_assert (_gdk_root == NULL);
1052
1053 _gdk_root = _gdk_display_create_window (display);
1054
1055 _gdk_root->impl = g_object_new (_gdk_root_window_impl_quartz_get_type (), NULL);
1056 _gdk_root->impl_window = _gdk_root;
1057 _gdk_root->visual = gdk_screen_get_system_visual (screen);
1058
1059 impl = gdk_window_get_quartz_impl (_gdk_root);
1060 /* If impl is somehow not a GdkWindowImplQuartz we're screwed, so crash. */
1061 if (!impl)
1062 {
1063 g_error ("Initializing quartz windowing produced the wrong kind of impl!");
1064 g_abort ();
1065 }
1066
1067 _gdk_quartz_screen_update_window_sizes (screen);
1068
1069 _gdk_root->state = 0; /* We don't want GDK_WINDOW_STATE_WITHDRAWN here */
1070 _gdk_root->window_type = GDK_WINDOW_ROOT;
1071 _gdk_root->depth = 24;
1072 _gdk_root->viewable = TRUE;
1073
1074 impl->wrapper = _gdk_root;
1075 }
1076
1077 static void
1078 gdk_quartz_window_destroy (GdkWindow *window,
1079 gboolean recursing,
1080 gboolean foreign_destroy)
1081 {
1082 GdkWindowImplQuartz *impl;
1083 GdkWindow *parent;
1084 GdkDisplay *display;
1085
1086 impl = gdk_window_get_quartz_impl (window);
1087
1088 if (!impl)
1089 return;
1090
1091 display = gdk_window_get_display (window);
1092
1093 _gdk_quartz_display_remove_frame_callback (display, window);
1094
1095 main_window_stack = g_slist_remove (main_window_stack, window);
1096
1097 g_list_free (impl->sorted_children);
1098 impl->sorted_children = NULL;
1099
1100 parent = window->parent;
1101 if (parent)
1102 {
1103 GdkWindowImplQuartz *parent_impl = gdk_window_get_quartz_impl (parent);
1104
1105 if (parent_impl)
1106 parent_impl->sorted_children = g_list_remove (parent_impl->sorted_children, window);
1107 }
1108
1109 if (impl->cairo_surface)
1110 {
1111 cairo_surface_finish (impl->cairo_surface);
1112 cairo_surface_set_user_data (impl->cairo_surface, &gdk_quartz_cairo_key,
1113 NULL, NULL);
1114 if (cairo_surface_get_reference_count(impl->cairo_surface))
1115 cairo_surface_destroy(impl->cairo_surface);
1116 impl->cairo_surface = NULL;
1117 }
1118
1119 if (!recursing && !foreign_destroy)
1120 {
1121 GDK_QUARTZ_ALLOC_POOL;
1122
1123 if (impl->toplevel)
1124 [impl->toplevel close];
1125 else if (impl->view)
1126 [impl->view removeFromSuperview];
1127
1128 impl->view = NULL;
1129 impl->toplevel = NULL;
1130
1131 GDK_QUARTZ_RELEASE_POOL;
1132 }
1133 }
1134
1135 static void
1136 gdk_quartz_window_destroy_foreign (GdkWindow *window)
1137 {
1138 /* Foreign windows aren't supported in OSX. */
1139 }
1140
1141 /* FIXME: This might be possible to simplify with client-side windows. Also
1142 * note that already_mapped is not used yet, see the x11 backend.
1143 */
1144 static void
1145 gdk_window_quartz_show (GdkWindow *window, gboolean already_mapped)
1146 {
1147 GdkWindowImplQuartz *impl = gdk_window_get_quartz_impl (window);
1148 gboolean focus_on_map;
1149
1150 if (!impl)
1151 return;
1152
1153 GDK_QUARTZ_ALLOC_POOL;
1154
1155 if (!GDK_WINDOW_IS_MAPPED (window))
1156 focus_on_map = window->focus_on_map;
1157 else
1158 focus_on_map = TRUE;
1159
1160 if (WINDOW_IS_TOPLEVEL (window) && impl->toplevel)
1161 {
1162 gboolean make_key;
1163
1164 make_key = (window->accept_focus && focus_on_map &&
1165 window->window_type != GDK_WINDOW_TEMP);
1166
1167 /* If window is already mapped and visible, skip showAndMakeKey to avoid
1168 * spurious resignMain/becomeMain notifications that cause focus fighting.
1169 * See https://gitlab.gnome.org/GNOME/gimp/-/issues/14901
1170 */
1171 if (!GDK_WINDOW_IS_MAPPED (window) || ![impl->toplevel isVisible])
1172 {
1173 [(GdkQuartzNSWindow*)impl->toplevel showAndMakeKey:make_key];
1174 clear_toplevel_order ();
1175 }
1176
1177 _gdk_quartz_events_send_map_event (window);
1178 }
1179 else
1180 {
1181 [impl->view setHidden:NO];
1182 }
1183
1184 [impl->view setNeedsDisplay:YES];
1185
1186 gdk_synthesize_window_state (window, GDK_WINDOW_STATE_WITHDRAWN, 0);
1187
1188 if (window->state & GDK_WINDOW_STATE_MAXIMIZED)
1189 gdk_window_maximize (window);
1190
1191 if (window->state & GDK_WINDOW_STATE_ICONIFIED)
1192 gdk_window_iconify (window);
1193
1194 if (impl->transient_for && !GDK_WINDOW_DESTROYED (impl->transient_for))
1195 _gdk_quartz_window_attach_to_parent (window);
1196
1197 GDK_QUARTZ_RELEASE_POOL;
1198 }
1199
1200 /* Temporarily unsets the parent window, if the window is a
1201 * transient.
1202 */
1203 void
1204 _gdk_quartz_window_detach_from_parent (GdkWindow *window)
1205 {
1206 GdkWindowImplQuartz *impl;
1207
1208 g_return_if_fail (GDK_IS_WINDOW (window));
1209
1210 impl = gdk_window_get_quartz_impl (window);
1211
1212 g_return_if_fail (impl && impl->toplevel != NULL);
1213
1214 if (impl->transient_for && !GDK_WINDOW_DESTROYED (impl->transient_for))
1215 {
1216 GdkWindowImplQuartz *parent_impl;
1217
1218 parent_impl = gdk_window_get_quartz_impl (impl->transient_for);
1219 if (parent_impl)
1220 {
1221 [parent_impl->toplevel removeChildWindow:impl->toplevel];
1222 clear_toplevel_order ();
1223 }
1224 }
1225 }
1226
1227 /* Re-sets the parent window, if the window is a transient. */
1228 void
1229 _gdk_quartz_window_attach_to_parent (GdkWindow *window)
1230 {
1231 GdkWindowImplQuartz *impl;
1232
1233 g_return_if_fail (GDK_IS_WINDOW (window));
1234
1235 impl = gdk_window_get_quartz_impl (window);
1236
1237 g_return_if_fail (impl && impl->toplevel != NULL);
1238
1239 if (impl->transient_for && !GDK_WINDOW_DESTROYED (impl->transient_for))
1240 {
1241 GdkWindowImplQuartz *parent_impl;
1242
1243 parent_impl = gdk_window_get_quartz_impl (impl->transient_for);
1244 if (parent_impl)
1245 {
1246 [parent_impl->toplevel addChildWindow:impl->toplevel ordered:NSWindowAbove];
1247 clear_toplevel_order ();
1248 }
1249 }
1250 }
1251
1252 void
1253 gdk_window_quartz_hide (GdkWindow *window)
1254 {
1255 GdkWindowImplQuartz *impl;
1256 GdkDisplay *display = gdk_window_get_display (window);
1257 GdkSeat *seat = gdk_display_get_default_seat (display);
1258 gdk_seat_ungrab (seat);
1259
1260 /* Make sure we're not stuck in fullscreen mode. */
1261 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
1262 if (get_fullscreen_geometry (window))
1263 SetSystemUIMode (kUIModeNormal, 0);
1264 #endif
1265
1266 _gdk_window_clear_update_area (window);
1267
1268 impl = gdk_window_get_quartz_impl (window);
1269 if (!impl)
1270 return;
1271
1272 if (WINDOW_IS_TOPLEVEL (window))
1273 {
1274 /* Update main window. */
1275 main_window_stack = g_slist_remove (main_window_stack, window);
1276 if ([NSApp mainWindow] == impl->toplevel)
1277 _gdk_quartz_window_did_resign_main (window);
1278
1279 if (impl->transient_for)
1280 _gdk_quartz_window_detach_from_parent (window);
1281
1282 [(GdkQuartzNSWindow*)impl->toplevel hide];
1283 }
1284 else if (impl->view)
1285 {
1286 [impl->view setHidden:YES];
1287 }
1288 }
1289
1290 void
1291 gdk_window_quartz_withdraw (GdkWindow *window)
1292 {
1293 gdk_window_hide (window);
1294 }
1295
1296 static void
1297 move_resize_window_internal (GdkWindow *window,
1298 gint x,
1299 gint y,
1300 gint width,
1301 gint height)
1302 {
1303 GdkWindowImplQuartz *impl;
1304 GdkRectangle old_visible;
1305 GdkRectangle new_visible;
1306 GdkRectangle scroll_rect;
1307 cairo_region_t *old_region;
1308 cairo_region_t *expose_region;
1309 NSSize delta;
1310
1311 if (GDK_WINDOW_DESTROYED (window))
1312 return;
1313
1314 impl = gdk_window_get_quartz_impl (window);
1315
1316 if (!impl)
1317 return;
1318
1319 if ((x == -1 || (x == window->x)) &&
1320 (y == -1 || (y == window->y)) &&
1321 (width == -1 || (width == window->width)) &&
1322 (height == -1 || (height == window->height)))
1323 {
1324 return;
1325 }
1326
1327 if (!impl->toplevel)
1328 {
1329 /* The previously visible area of this window in a coordinate
1330 * system rooted at the origin of this window.
1331 */
1332 old_visible.x = -window->x;
1333 old_visible.y = -window->y;
1334
1335 old_visible.width = window->width;
1336 old_visible.height = window->height;
1337 }
1338
1339 if (x != -1)
1340 {
1341 delta.width = x - window->x;
1342 window->x = x;
1343 }
1344 else
1345 {
1346 delta.width = 0;
1347 }
1348
1349 if (y != -1)
1350 {
1351 delta.height = y - window->y;
1352 window->y = y;
1353 }
1354 else
1355 {
1356 delta.height = 0;
1357 }
1358
1359 if (width != -1)
1360 {
1361 window->width = width;
1362 }
1363
1364 if (height != -1)
1365 {
1366 window->height = height;
1367 }
1368
1369 GDK_QUARTZ_ALLOC_POOL;
1370
1371 if (impl->toplevel)
1372 {
1373 NSRect content_rect;
1374 NSRect frame_rect;
1375 gint gx, gy;
1376
1377 _gdk_quartz_window_gdk_xy_to_xy (window->x, window->y + window->height,
1378 &gx, &gy);
1379
1380 content_rect = NSMakeRect (gx, gy, window->width, window->height);
1381
1382 frame_rect = [impl->toplevel frameRectForContentRect:content_rect];
1383 [impl->toplevel setFrame:frame_rect display:YES];
1384
1385 if (window->window_type == GDK_WINDOW_TEMP)
1386 [impl->view setFrame:content_rect];
1387
1388 impl->cairo_surface = gdk_quartz_ref_cairo_surface (window);
1389 cairo_surface_destroy (impl->cairo_surface); // Remove the extra reference
1390 }
1391 else
1392 {
1393 if (!window->input_only)
1394 {
1395 NSRect nsrect;
1396
1397 nsrect = NSMakeRect (window->x, window->y, window->width, window->height);
1398
1399 /* The newly visible area of this window in a coordinate
1400 * system rooted at the origin of this window.
1401 */
1402 new_visible.x = -window->x;
1403 new_visible.y = -window->y;
1404 new_visible.width = old_visible.width; /* parent has not changed size */
1405 new_visible.height = old_visible.height; /* parent has not changed size */
1406
1407 expose_region = cairo_region_create_rectangle (&new_visible);
1408 old_region = cairo_region_create_rectangle (&old_visible);
1409 cairo_region_subtract (expose_region, old_region);
1410
1411 /* Determine what (if any) part of the previously visible
1412 * part of the window can be copied without a redraw
1413 */
1414 scroll_rect = old_visible;
1415 scroll_rect.x -= delta.width;
1416 scroll_rect.y -= delta.height;
1417 gdk_rectangle_intersect (&scroll_rect, &old_visible, &scroll_rect);
1418
1419 if (!cairo_region_is_empty (expose_region))
1420 {
1421 if (scroll_rect.width != 0 && scroll_rect.height != 0)
1422 {
1423 [impl->view scrollRect:NSMakeRect (scroll_rect.x,
1424 scroll_rect.y,
1425 scroll_rect.width,
1426 scroll_rect.height)
1427 by:delta];
1428 }
1429
1430 gdk_quartz_window_set_needs_display_in_region (window, expose_region);
1431 }
1432 else
1433 {
1434 [impl->view setNeedsDisplay:YES];
1435 }
1436
1437 cairo_region_destroy (expose_region);
1438 cairo_region_destroy (old_region);
1439 }
1440 }
1441
1442 if (window->gl_paint_context != NULL)
1443 [GDK_QUARTZ_GL_CONTEXT (window->gl_paint_context)->gl_context update];
1444
1445 GDK_QUARTZ_RELEASE_POOL;
1446 }
1447
1448 static inline void
1449 window_quartz_move (GdkWindow *window,
1450 gint x,
1451 gint y)
1452 {
1453 g_return_if_fail (GDK_IS_WINDOW (window));
1454
1455 if (window->state & GDK_WINDOW_STATE_FULLSCREEN)
1456 return;
1457
1458 move_resize_window_internal (window, x, y, -1, -1);
1459 }
1460
1461 static inline void
1462 window_quartz_resize (GdkWindow *window,
1463 gint width,
1464 gint height)
1465 {
1466 g_return_if_fail (GDK_IS_WINDOW (window));
1467
1468 if (window->state & GDK_WINDOW_STATE_FULLSCREEN)
1469 return;
1470
1471 if (width < 1)
1472 width = 1;
1473 if (height < 1)
1474 height = 1;
1475
1476 move_resize_window_internal (window, -1, -1, width, height);
1477 }
1478
1479 static inline void
1480 window_quartz_move_resize (GdkWindow *window,
1481 gint x,
1482 gint y,
1483 gint width,
1484 gint height)
1485 {
1486 if (width < 1)
1487 width = 1;
1488 if (height < 1)
1489 height = 1;
1490
1491 move_resize_window_internal (window, x, y, width, height);
1492 }
1493
1494 static void
1495 gdk_window_quartz_move_resize (GdkWindow *window,
1496 gboolean with_move,
1497 gint x,
1498 gint y,
1499 gint width,
1500 gint height)
1501 {
1502 if (with_move && (width < 0 && height < 0))
1503 window_quartz_move (window, x, y);
1504 else
1505 {
1506 if (with_move)
1507 window_quartz_move_resize (window, x, y, width, height);
1508 else
1509 window_quartz_resize (window, width, height);
1510 }
1511 }
1512
1513 /* FIXME: This might need fixing (reparenting didn't work before client-side
1514 * windows either).
1515 */
1516 static gboolean
1517 gdk_window_quartz_reparent (GdkWindow *window,
1518 GdkWindow *new_parent,
1519 gint x,
1520 gint y)
1521 {
1522 GdkWindow *old_parent;
1523 GdkWindowImplQuartz *impl, *old_parent_impl, *new_parent_impl;
1524 NSView *view, *new_parent_view;
1525
1526 if (new_parent == _gdk_root)
1527 {
1528 /* Could be added, just needs implementing. */
1529 g_warning ("Reparenting to root window is not supported yet in the Mac OS X backend");
1530 return FALSE;
1531 }
1532
1533 impl = gdk_window_get_quartz_impl (window);
1534 if (!impl)
1535 return FALSE;
1536 view = impl->view;
1537
1538 new_parent_impl = gdk_window_get_quartz_impl (new_parent);
1539 if (!new_parent_impl)
1540 return FALSE;
1541
1542 new_parent_view = new_parent_impl->view;
1543
1544 old_parent = window->parent;
1545 old_parent_impl = gdk_window_get_quartz_impl (old_parent);
1546 if (!old_parent_impl)
1547 return FALSE;
1548
1549 [view retain];
1550
1551 [view removeFromSuperview];
1552 [new_parent_view addSubview:view];
1553
1554 [view release];
1555
1556 window->parent = new_parent;
1557
1558 if (old_parent)
1559 {
1560 old_parent_impl->sorted_children = g_list_remove (old_parent_impl->sorted_children, window);
1561 }
1562
1563 new_parent_impl->sorted_children = g_list_prepend (new_parent_impl->sorted_children, window);
1564
1565 return FALSE;
1566 }
1567
1568 /* Get the toplevel ordering from NSApp and update our own list. We do
1569 * this on demand since the NSApp’s list is not up to date directly
1570 * after we get windowDidBecomeMain.
1571 */
1572 static void
1573 update_toplevel_order (void)
1574 {
1575 GdkWindowImplQuartz *root_impl;
1576 NSEnumerator *enumerator;
1577 id nswindow;
1578 GList *toplevels = NULL;
1579
1580 root_impl = gdk_window_get_quartz_impl (_gdk_root);
1581
1582 if (!root_impl || root_impl->sorted_children)
1583 return;
1584
1585 GDK_QUARTZ_ALLOC_POOL;
1586
1587 enumerator = [[NSApp orderedWindows] objectEnumerator];
1588 while ((nswindow = [enumerator nextObject]))
1589 {
1590 GdkWindow *window;
1591
1592 if (![[nswindow contentView] isKindOfClass:[GdkQuartzView class]])
1593 continue;
1594
1595 window = [(GdkQuartzView *)[nswindow contentView] gdkWindow];
1596 toplevels = g_list_prepend (toplevels, window);
1597 }
1598
1599 GDK_QUARTZ_RELEASE_POOL;
1600
1601 root_impl->sorted_children = g_list_reverse (toplevels);
1602 }
1603
1604 static void
1605 clear_toplevel_order (void)
1606 {
1607 GdkWindowImplQuartz *root_impl;
1608
1609 root_impl = gdk_window_get_quartz_impl (_gdk_root);
1610 if (root_impl)
1611 {
1612 g_list_free (root_impl->sorted_children);
1613 root_impl->sorted_children = NULL;
1614 }
1615 }
1616
1617 static void
1618 gdk_window_quartz_raise (GdkWindow *window)
1619 {
1620 if (GDK_WINDOW_DESTROYED (window))
1621 return;
1622
1623 if (WINDOW_IS_TOPLEVEL (window))
1624 {
1625 GdkWindowImplQuartz *impl;
1626
1627 impl = gdk_window_get_quartz_impl (window);
1628 if (!impl)
1629 return;
1630 if (impl->transient_for && !GDK_WINDOW_DESTROYED (impl->transient_for))
1631 raise_transient (impl);
1632 else
1633 [impl->toplevel orderFront:impl->toplevel];
1634
1635 clear_toplevel_order ();
1636 }
1637 else
1638 {
1639 GdkWindow *parent = window->parent;
1640
1641 if (parent)
1642 {
1643 GdkWindowImplQuartz *impl;
1644
1645 impl = gdk_window_get_quartz_impl (parent);
1646 if (impl)
1647 {
1648 impl->sorted_children = g_list_remove (impl->sorted_children, window);
1649 impl->sorted_children = g_list_prepend (impl->sorted_children, window);
1650 }
1651 }
1652 }
1653 }
1654
1655 static void
1656 gdk_window_quartz_lower (GdkWindow *window)
1657 {
1658 if (GDK_WINDOW_DESTROYED (window))
1659 return;
1660
1661 if (WINDOW_IS_TOPLEVEL (window))
1662 {
1663 GdkWindowImplQuartz *impl;
1664
1665 impl = gdk_window_get_quartz_impl (window);
1666 if (impl)
1667 {
1668 [impl->toplevel orderBack:impl->toplevel];
1669
1670 clear_toplevel_order ();
1671 }
1672 }
1673 else
1674 {
1675 GdkWindow *parent = window->parent;
1676
1677 if (parent)
1678 {
1679 GdkWindowImplQuartz *impl;
1680
1681 impl = gdk_window_get_quartz_impl (parent);
1682 if (impl)
1683 {
1684 impl->sorted_children = g_list_remove (impl->sorted_children, window);
1685 impl->sorted_children = g_list_append (impl->sorted_children, window);
1686 }
1687 }
1688 }
1689 }
1690
1691 static void
1692 gdk_window_quartz_restack_toplevel (GdkWindow *window,
1693 GdkWindow *sibling,
1694 gboolean above)
1695 {
1696 GdkWindowImplQuartz *impl;
1697 gint sibling_num = -1;
1698
1699 impl = gdk_window_get_quartz_impl (sibling);
1700 if (impl)
1701 sibling_num = [impl->toplevel windowNumber];
1702
1703 impl = gdk_window_get_quartz_impl (window);
1704 if (!(impl && sibling_num >= 0))
1705 return;
1706 if (above)
1707 [impl->toplevel orderWindow:NSWindowAbove relativeTo:sibling_num];
1708 else
1709 [impl->toplevel orderWindow:NSWindowBelow relativeTo:sibling_num];
1710 }
1711
1712 static void
1713 gdk_window_quartz_set_background (GdkWindow *window,
1714 cairo_pattern_t *pattern)
1715 {
1716 /* FIXME: We could theoretically set the background color for toplevels
1717 * here. (Currently we draw the background before emitting expose events)
1718 */
1719 }
1720
1721 static void
1722 gdk_window_quartz_set_device_cursor (GdkWindow *window,
1723 GdkDevice *device,
1724 GdkCursor *cursor)
1725 {
1726 NSCursor *nscursor;
1727
1728 if (GDK_WINDOW_DESTROYED (window))
1729 return;
1730
1731 nscursor = _gdk_quartz_cursor_get_ns_cursor (cursor);
1732
1733 [nscursor set];
1734 }
1735
1736 static void
1737 gdk_window_quartz_get_geometry (GdkWindow *window,
1738 gint *x,
1739 gint *y,
1740 gint *width,
1741 gint *height)
1742 {
1743 GdkWindowImplQuartz *impl;
1744 NSRect ns_rect;
1745
1746 if (GDK_WINDOW_DESTROYED (window))
1747 return;
1748
1749 impl = gdk_window_get_quartz_impl (window);
1750 if (!impl)
1751 return;
1752 if (window == _gdk_root)
1753 {
1754 if (x)
1755 *x = 0;
1756 if (y)
1757 *y = 0;
1758
1759 if (width)
1760 *width = window->width;
1761 if (height)
1762 *height = window->height;
1763 }
1764 else if (WINDOW_IS_TOPLEVEL (window))
1765 {
1766 ns_rect = [impl->toplevel contentRectForFrameRect:[impl->toplevel frame]];
1767
1768 /* This doesn't work exactly as in X. There doesn't seem to be a
1769 * way to get the coords relative to the parent window (usually
1770 * the window frame), but that seems useless except for
1771 * borderless windows where it's relative to the root window. So
1772 * we return (0, 0) (should be something like (0, 22)) for
1773 * windows with borders and the root relative coordinates
1774 * otherwise.
1775 */
1776 if ([impl->toplevel styleMask] == GDK_QUARTZ_BORDERLESS_WINDOW)
1777 {
1778 _gdk_quartz_window_xy_to_gdk_xy (ns_rect.origin.x,
1779 ns_rect.origin.y + ns_rect.size.height,
1780 x, y);
1781 }
1782 else
1783 {
1784 if (x)
1785 *x = 0;
1786 if (y)
1787 *y = 0;
1788 }
1789
1790 if (width)
1791 *width = ns_rect.size.width;
1792 if (height)
1793 *height = ns_rect.size.height;
1794 }
1795 else
1796 {
1797 ns_rect = [impl->view frame];
1798
1799 if (x)
1800 *x = ns_rect.origin.x;
1801 if (y)
1802 *y = ns_rect.origin.y;
1803 if (width)
1804 *width = ns_rect.size.width;
1805 if (height)
1806 *height = ns_rect.size.height;
1807 }
1808 }
1809
1810 static void
1811 gdk_window_quartz_get_root_coords (GdkWindow *window,
1812 gint x,
1813 gint y,
1814 gint *root_x,
1815 gint *root_y)
1816 {
1817 int tmp_x = 0, tmp_y = 0;
1818 GdkWindow *toplevel;
1819 NSRect content_rect;
1820 GdkWindowImplQuartz *impl;
1821
1822 if (GDK_WINDOW_DESTROYED (window))
1823 {
1824 if (root_x)
1825 *root_x = 0;
1826 if (root_y)
1827 *root_y = 0;
1828
1829 return;
1830 }
1831
1832 if (window == _gdk_root)
1833 {
1834 if (root_x)
1835 *root_x = x;
1836 if (root_y)
1837 *root_y = y;
1838
1839 return;
1840 }
1841
1842 toplevel = gdk_window_get_toplevel (window);
1843 impl = gdk_window_get_quartz_impl (toplevel);
1844
1845 if (!impl)
1846 return;
1847
1848 content_rect = [impl->toplevel contentRectForFrameRect:[impl->toplevel frame]];
1849
1850 _gdk_quartz_window_xy_to_gdk_xy (content_rect.origin.x,
1851 content_rect.origin.y + content_rect.size.height,
1852 &tmp_x, &tmp_y);
1853
1854 tmp_x += x;
1855 tmp_y += y;
1856
1857 while (window != toplevel)
1858 {
1859 if (_gdk_window_has_impl ((GdkWindow *)window))
1860 {
1861 tmp_x += window->x;
1862 tmp_y += window->y;
1863 }
1864
1865 window = window->parent;
1866 }
1867
1868 if (root_x)
1869 *root_x = tmp_x;
1870 if (root_y)
1871 *root_y = tmp_y;
1872 }
1873
1874 /* Returns coordinates relative to the passed in window. */
1875 static GdkWindow *
1876 gdk_window_quartz_get_device_state_helper (GdkWindow *window,
1877 GdkDevice *device,
1878 gdouble *x,
1879 gdouble *y,
1880 GdkModifierType *mask)
1881 {
1882 NSPoint point;
1883 gint x_tmp, y_tmp;
1884 GdkWindow *toplevel;
1885 GdkWindow *found_window;
1886
1887 g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
1888
1889 if (GDK_WINDOW_DESTROYED (window))
1890 {
1891 *x = 0;
1892 *y = 0;
1893 *mask = 0;
1894 return NULL;
1895 }
1896
1897 toplevel = gdk_window_get_toplevel (window);
1898
1899 *mask = _gdk_quartz_events_get_current_keyboard_modifiers () |
1900 _gdk_quartz_events_get_current_mouse_modifiers ();
1901
1902 /* Get the y coordinate, needs to be flipped. */
1903 if (window == _gdk_root)
1904 {
1905 point = [NSEvent mouseLocation];
1906 _gdk_quartz_window_nspoint_to_gdk_xy (point, &x_tmp, &y_tmp);
1907 }
1908 else
1909 {
1910 GdkWindowImplQuartz *impl;
1911 NSWindow *nswindow;
1912
1913 impl = gdk_window_get_quartz_impl (toplevel);
1914 nswindow = impl ? impl->toplevel : NULL;
1915
1916 point = [nswindow mouseLocationOutsideOfEventStream];
1917
1918 x_tmp = point.x;
1919 y_tmp = toplevel->height - point.y;
1920
1921 window = (GdkWindow *)toplevel;
1922 }
1923
1924 found_window = _gdk_quartz_window_find_child (window, x_tmp, y_tmp,
1925 FALSE);
1926
1927 /* We never return the root window. */
1928 if (found_window == _gdk_root)
1929 found_window = NULL;
1930
1931 *x = x_tmp;
1932 *y = y_tmp;
1933
1934 return found_window;
1935 }
1936
1937 static gboolean
1938 gdk_window_quartz_get_device_state (GdkWindow *window,
1939 GdkDevice *device,
1940 gdouble *x,
1941 gdouble *y,
1942 GdkModifierType *mask)
1943 {
1944 return gdk_window_quartz_get_device_state_helper (window,
1945 device,
1946 x, y, mask) != NULL;
1947 }
1948
1949 static GdkEventMask
1950 gdk_window_quartz_get_events (GdkWindow *window)
1951 {
1952 if (GDK_WINDOW_DESTROYED (window))
1953 return 0;
1954 else
1955 return window->event_mask;
1956 }
1957
1958 static void
1959 gdk_window_quartz_set_events (GdkWindow *window,
1960 GdkEventMask event_mask)
1961 {
1962 /* The mask is set in the common code. */
1963 }
1964
1965 static void
1966 gdk_quartz_window_set_urgency_hint (GdkWindow *window,
1967 gboolean urgent)
1968 {
1969 if (GDK_WINDOW_DESTROYED (window) ||
1970 !WINDOW_IS_TOPLEVEL (window))
1971 return;
1972
1973 /* FIXME: Implement */
1974 }
1975
1976 static void
1977 gdk_quartz_window_set_geometry_hints (GdkWindow *window,
1978 const GdkGeometry *geometry,
1979 GdkWindowHints geom_mask)
1980 {
1981 GdkWindowImplQuartz *impl;
1982
1983 g_return_if_fail (geometry != NULL);
1984
1985 if (GDK_WINDOW_DESTROYED (window) ||
1986 !WINDOW_IS_TOPLEVEL (window))
1987 return;
1988
1989 impl = gdk_window_get_quartz_impl (window);
1990 if (!(impl && impl->toplevel))
1991 return;
1992
1993 if (geom_mask & GDK_HINT_POS)
1994 {
1995 /* FIXME: Implement */
1996 }
1997
1998 if (geom_mask & GDK_HINT_USER_POS)
1999 {
2000 /* FIXME: Implement */
2001 }
2002
2003 if (geom_mask & GDK_HINT_USER_SIZE)
2004 {
2005 /* FIXME: Implement */
2006 }
2007
2008 if (geom_mask & GDK_HINT_MIN_SIZE)
2009 {
2010 NSSize size;
2011
2012 size.width = geometry->min_width;
2013 size.height = geometry->min_height;
2014
2015 [impl->toplevel setContentMinSize:size];
2016 }
2017
2018 if (geom_mask & GDK_HINT_MAX_SIZE)
2019 {
2020 NSSize size;
2021
2022 size.width = geometry->max_width;
2023 size.height = geometry->max_height;
2024
2025 [impl->toplevel setContentMaxSize:size];
2026 }
2027
2028 if (geom_mask & GDK_HINT_BASE_SIZE)
2029 {
2030 /* FIXME: Implement */
2031 }
2032
2033 if (geom_mask & GDK_HINT_RESIZE_INC)
2034 {
2035 NSSize size;
2036
2037 size.width = geometry->width_inc;
2038 size.height = geometry->height_inc;
2039
2040 [impl->toplevel setContentResizeIncrements:size];
2041 }
2042
2043 if (geom_mask & GDK_HINT_ASPECT)
2044 {
2045 NSSize size;
2046
2047 if (geometry->min_aspect != geometry->max_aspect)
2048 {
2049 g_warning ("Only equal minimum and maximum aspect ratios are supported on Mac OS. Using minimum aspect ratio...");
2050 }
2051
2052 size.width = geometry->min_aspect;
2053 size.height = 1.0;
2054
2055 [impl->toplevel setContentAspectRatio:size];
2056 }
2057
2058 if (geom_mask & GDK_HINT_WIN_GRAVITY)
2059 {
2060 /* FIXME: Implement */
2061 }
2062 }
2063
2064 static void
2065 gdk_quartz_window_set_title (GdkWindow *window,
2066 const gchar *title)
2067 {
2068 GdkWindowImplQuartz *impl;
2069
2070 g_return_if_fail (title != NULL);
2071
2072 if (GDK_WINDOW_DESTROYED (window) ||
2073 !WINDOW_IS_TOPLEVEL (window))
2074 return;
2075
2076 impl = gdk_window_get_quartz_impl (window);
2077
2078 if (impl && impl->toplevel)
2079 {
2080 GDK_QUARTZ_ALLOC_POOL;
2081 [impl->toplevel setTitle:[NSString stringWithUTF8String:title]];
2082 GDK_QUARTZ_RELEASE_POOL;
2083 }
2084 }
2085
2086 static void
2087 gdk_quartz_window_set_role (GdkWindow *window,
2088 const gchar *role)
2089 {
2090 if (GDK_WINDOW_DESTROYED (window) ||
2091 WINDOW_IS_TOPLEVEL (window))
2092 return;
2093
2094 /* FIXME: Implement */
2095 }
2096
2097 static void
2098 gdk_quartz_window_set_startup_id (GdkWindow *window,
2099 const gchar *startup_id)
2100 {
2101 /* FIXME: Implement? */
2102 }
2103
2104 static void
2105 gdk_quartz_window_set_transient_for (GdkWindow *window,
2106 GdkWindow *parent)
2107 {
2108 GdkWindowImplQuartz *window_impl;
2109 GdkWindowImplQuartz *parent_impl;
2110
2111 if (GDK_WINDOW_DESTROYED (window) || GDK_WINDOW_DESTROYED (parent) ||
2112 !WINDOW_IS_TOPLEVEL (window))
2113 return;
2114
2115 window_impl = gdk_window_get_quartz_impl (window);
2116 if (!(window_impl && window_impl->toplevel))
2117 return;
2118
2119 GDK_QUARTZ_ALLOC_POOL;
2120
2121 if (window_impl->transient_for)
2122 {
2123 _gdk_quartz_window_detach_from_parent (window);
2124
2125 g_object_unref (window_impl->transient_for);
2126 window_impl->transient_for = NULL;
2127 }
2128
2129 parent_impl = gdk_window_get_quartz_impl (parent);
2130 if (parent_impl && parent_impl->toplevel)
2131 {
2132 /* We save the parent because it needs to be unset/reset when
2133 * hiding and showing the window.
2134 */
2135
2136 /* We don't set transients for tooltips, they are already
2137 * handled by the window level being the top one. If we do, then
2138 * the parent window will be brought to the top just because the
2139 * tooltip is, which is not what we want.
2140 */
2141 if (gdk_window_get_type_hint (window) != GDK_WINDOW_TYPE_HINT_TOOLTIP)
2142 {
2143 window_impl->transient_for = g_object_ref (parent);
2144
2145 /* We only add the window if it is shown, otherwise it will
2146 * be shown unconditionally here. If it is not shown, the
2147 * window will be added in show() instead.
2148 */
2149 if (!(window->state & GDK_WINDOW_STATE_WITHDRAWN))
2150 _gdk_quartz_window_attach_to_parent (window);
2151 }
2152 }
2153
2154 GDK_QUARTZ_RELEASE_POOL;
2155 }
2156
2157 static void
2158 gdk_window_quartz_shape_combine_region (GdkWindow *window,
2159 const cairo_region_t *shape,
2160 gint x,
2161 gint y)
2162 {
2163 /* FIXME: Implement */
2164 }
2165
2166 static void
2167 gdk_window_quartz_input_shape_combine_region (GdkWindow *window,
2168 const cairo_region_t *shape_region,
2169 gint offset_x,
2170 gint offset_y)
2171 {
2172 /* FIXME: Implement */
2173 }
2174
2175 static void
2176 gdk_quartz_window_set_override_redirect (GdkWindow *window,
2177 gboolean override_redirect)
2178 {
2179 /* FIXME: Implement */
2180 }
2181
2182 static void
2183 gdk_quartz_window_set_accept_focus (GdkWindow *window,
2184 gboolean accept_focus)
2185 {
2186 window->accept_focus = accept_focus != FALSE;
2187 }
2188
2189 static void
2190 gdk_quartz_window_set_focus_on_map (GdkWindow *window,
2191 gboolean focus_on_map)
2192 {
2193 window->focus_on_map = focus_on_map != FALSE;
2194 }
2195
2196 static void
2197 gdk_quartz_window_set_icon_name (GdkWindow *window,
2198 const gchar *name)
2199 {
2200 /* FIXME: Implement */
2201 }
2202
2203 static void
2204 gdk_quartz_window_focus (GdkWindow *window,
2205 guint32 timestamp)
2206 {
2207 GdkWindowImplQuartz *impl;
2208
2209 impl = gdk_window_get_quartz_impl (window);
2210
2211 if (GDK_WINDOW_DESTROYED (window) ||
2212 !WINDOW_IS_TOPLEVEL (window) ||
2213 !impl)
2214 return;
2215
2216 if (window->accept_focus && window->window_type != GDK_WINDOW_TEMP)
2217 {
2218 GDK_QUARTZ_ALLOC_POOL;
2219 [impl->toplevel makeKeyAndOrderFront:impl->toplevel];
2220 clear_toplevel_order ();
2221 GDK_QUARTZ_RELEASE_POOL;
2222 }
2223 }
2224
2225 static gint
2226 window_type_hint_to_level (GdkWindowTypeHint hint)
2227 {
2228 /* the order in this switch statement corresponds to the actual
2229 * stacking order: the first group is top, the last group is bottom
2230 */
2231 switch (hint)
2232 {
2233 case GDK_WINDOW_TYPE_HINT_POPUP_MENU:
2234 case GDK_WINDOW_TYPE_HINT_COMBO:
2235 case GDK_WINDOW_TYPE_HINT_DND:
2236 case GDK_WINDOW_TYPE_HINT_TOOLTIP:
2237 return NSPopUpMenuWindowLevel;
2238
2239 case GDK_WINDOW_TYPE_HINT_NOTIFICATION:
2240 case GDK_WINDOW_TYPE_HINT_SPLASHSCREEN:
2241 return NSStatusWindowLevel;
2242
2243 case GDK_WINDOW_TYPE_HINT_MENU: /* Torn-off menu */
2244 case GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU: /* Menu from menubar */
2245 return NSTornOffMenuWindowLevel;
2246
2247 case GDK_WINDOW_TYPE_HINT_DOCK:
2248 return NSFloatingWindowLevel; /* NSDockWindowLevel is deprecated, and not replaced */
2249
2250 case GDK_WINDOW_TYPE_HINT_UTILITY:
2251 case GDK_WINDOW_TYPE_HINT_DIALOG: /* Dialog window */
2252 case GDK_WINDOW_TYPE_HINT_NORMAL: /* Normal toplevel window */
2253 case GDK_WINDOW_TYPE_HINT_TOOLBAR: /* Window used to implement toolbars */
2254 return NSNormalWindowLevel;
2255
2256 case GDK_WINDOW_TYPE_HINT_DESKTOP:
2257 return kCGDesktopWindowLevelKey; /* doesn't map to any real Cocoa model */
2258
2259 default:
2260 break;
2261 }
2262
2263 return NSNormalWindowLevel;
2264 }
2265
2266 static gboolean
2267 window_type_hint_to_shadow (GdkWindowTypeHint hint)
2268 {
2269 switch (hint)
2270 {
2271 case GDK_WINDOW_TYPE_HINT_NORMAL: /* Normal toplevel window */
2272 case GDK_WINDOW_TYPE_HINT_DIALOG: /* Dialog window */
2273 case GDK_WINDOW_TYPE_HINT_DOCK:
2274 case GDK_WINDOW_TYPE_HINT_UTILITY:
2275 case GDK_WINDOW_TYPE_HINT_MENU: /* Torn-off menu */
2276 case GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU: /* Menu from menubar */
2277 case GDK_WINDOW_TYPE_HINT_SPLASHSCREEN:
2278 case GDK_WINDOW_TYPE_HINT_POPUP_MENU:
2279 case GDK_WINDOW_TYPE_HINT_COMBO:
2280 case GDK_WINDOW_TYPE_HINT_NOTIFICATION:
2281 case GDK_WINDOW_TYPE_HINT_TOOLTIP:
2282 return TRUE;
2283
2284 case GDK_WINDOW_TYPE_HINT_TOOLBAR: /* Window used to implement toolbars */
2285 case GDK_WINDOW_TYPE_HINT_DESKTOP: /* N/A */
2286 case GDK_WINDOW_TYPE_HINT_DND:
2287 break;
2288
2289 default:
2290 break;
2291 }
2292
2293 return FALSE;
2294 }
2295
2296 static gboolean
2297 window_type_hint_to_hides_on_deactivate (GdkWindowTypeHint hint)
2298 {
2299 switch (hint)
2300 {
2301 case GDK_WINDOW_TYPE_HINT_UTILITY:
2302 case GDK_WINDOW_TYPE_HINT_MENU: /* Torn-off menu */
2303 case GDK_WINDOW_TYPE_HINT_SPLASHSCREEN:
2304 case GDK_WINDOW_TYPE_HINT_NOTIFICATION:
2305 case GDK_WINDOW_TYPE_HINT_TOOLTIP:
2306 return TRUE;
2307
2308 default:
2309 break;
2310 }
2311
2312 return FALSE;
2313 }
2314
2315 static void
2316 _gdk_quartz_window_update_has_shadow (GdkWindowImplQuartz *impl)
2317 {
2318 gboolean has_shadow;
2319
2320 /* In case there is any shadow set we have to turn off the
2321 * NSWindow setHasShadow as the system drawn ones wont match our
2322 * window boundary anymore */
2323 has_shadow = (window_type_hint_to_shadow (impl->type_hint) && !impl->shadow_max);
2324
2325 [impl->toplevel setHasShadow: has_shadow];
2326 }
2327
2328 static void
2329 _gdk_quartz_window_set_collection_behavior (NSWindow *nswindow,
2330 GdkWindowTypeHint hint)
2331 {
2332 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
2333 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 101100
2334 #define GDK_QUARTZ_ALLOWS_TILING NSWindowCollectionBehaviorFullScreenAllowsTiling
2335 #define GDK_QUARTZ_DISALLOWS_TILING NSWindowCollectionBehaviorFullScreenDisallowsTiling
2336 #else
2337 #define GDK_QUARTZ_ALLOWS_TILING 1 << 11
2338 #define GDK_QUARTZ_DISALLOWS_TILING 1 << 12
2339 #endif
2340 if (gdk_quartz_osx_version() >= GDK_OSX_LION)
2341 {
2342 /* Fullscreen Collection Behavior */
2343 NSWindowCollectionBehavior behavior = [nswindow collectionBehavior];
2344 switch (hint)
2345 {
2346 case GDK_WINDOW_TYPE_HINT_NORMAL:
2347 case GDK_WINDOW_TYPE_HINT_SPLASHSCREEN:
2348 behavior &= ~(NSWindowCollectionBehaviorFullScreenAuxiliary &
2349 GDK_QUARTZ_DISALLOWS_TILING);
2350 behavior |= (NSWindowCollectionBehaviorFullScreenPrimary |
2351 GDK_QUARTZ_ALLOWS_TILING);
2352
2353 break;
2354 default:
2355 behavior &= ~(NSWindowCollectionBehaviorFullScreenPrimary &
2356 GDK_QUARTZ_ALLOWS_TILING);
2357 behavior |= (NSWindowCollectionBehaviorFullScreenAuxiliary |
2358 GDK_QUARTZ_DISALLOWS_TILING);
2359 break;
2360 }
2361 [nswindow setCollectionBehavior:behavior];
2362 }
2363 #undef GDK_QUARTZ_ALLOWS_TILING
2364 #undef GDK_QUARTZ_DISALLOWS_TILING
2365 #endif
2366 }
2367
2368 static void
2369 gdk_quartz_window_set_type_hint (GdkWindow *window,
2370 GdkWindowTypeHint hint)
2371 {
2372 GdkWindowImplQuartz *impl;
2373
2374 if (GDK_WINDOW_DESTROYED (window) ||
2375 !WINDOW_IS_TOPLEVEL (window))
2376 return;
2377
2378 impl = gdk_window_get_quartz_impl (window);
2379
2380 if (!impl)
2381 return;
2382
2383 if (hint == GDK_WINDOW_TYPE_HINT_NORMAL &&
2384 window->window_type == GDK_WINDOW_TEMP)
2385 hint = GDK_WINDOW_TYPE_HINT_POPUP_MENU;
2386
2387 impl->type_hint = hint;
2388
2389 /* Match the documentation, only do something if we're not mapped yet. */
2390 if (GDK_WINDOW_IS_MAPPED (window))
2391 return;
2392
2393 _gdk_quartz_window_update_has_shadow (impl);
2394 if (impl->toplevel)
2395 _gdk_quartz_window_set_collection_behavior (impl->toplevel, hint);
2396 [impl->toplevel setLevel: window_type_hint_to_level (hint)];
2397 [impl->toplevel setHidesOnDeactivate: window_type_hint_to_hides_on_deactivate (hint)];
2398 }
2399
2400 static GdkWindowTypeHint
2401 gdk_quartz_window_get_type_hint (GdkWindow *window)
2402 {
2403 if (GDK_WINDOW_DESTROYED (window) ||
2404 !WINDOW_IS_TOPLEVEL (window))
2405 return GDK_WINDOW_TYPE_HINT_NORMAL;
2406
2407 return gdk_window_get_quartz_impl (window)->type_hint;
2408 }
2409
2410 static void
2411 gdk_quartz_window_set_modal_hint (GdkWindow *window,
2412 gboolean modal)
2413 {
2414 if (GDK_WINDOW_DESTROYED (window) ||
2415 !WINDOW_IS_TOPLEVEL (window))
2416 return;
2417
2418 /* FIXME: Implement */
2419 }
2420
2421 static void
2422 gdk_quartz_window_set_skip_taskbar_hint (GdkWindow *window,
2423 gboolean skips_taskbar)
2424 {
2425 if (GDK_WINDOW_DESTROYED (window) ||
2426 !WINDOW_IS_TOPLEVEL (window))
2427 return;
2428
2429 /* FIXME: Implement */
2430 }
2431
2432 static void
2433 gdk_quartz_window_set_skip_pager_hint (GdkWindow *window,
2434 gboolean skips_pager)
2435 {
2436 if (GDK_WINDOW_DESTROYED (window) ||
2437 !WINDOW_IS_TOPLEVEL (window))
2438 return;
2439
2440 /* FIXME: Implement */
2441 }
2442
2443 static void
2444 gdk_quartz_window_begin_resize_drag (GdkWindow *window,
2445 GdkWindowEdge edge,
2446 GdkDevice *device,
2447 gint button,
2448 gint root_x,
2449 gint root_y,
2450 guint32 timestamp)
2451 {
2452 GdkWindowImplQuartz *impl;
2453
2454 g_return_if_fail (GDK_IS_WINDOW (window));
2455
2456 if (GDK_WINDOW_DESTROYED (window))
2457 return;
2458
2459 impl = gdk_window_get_quartz_impl (window);
2460
2461 if (!(impl && impl->toplevel))
2462 {
2463 g_warning ("Can't call gdk_window_begin_resize_drag on non-quartz or non-toplevel window");
2464 return;
2465 }
2466
2467 [(GdkQuartzNSWindow *)impl->toplevel beginManualResize:edge];
2468 }
2469
2470 static void
2471 gdk_quartz_window_begin_move_drag (GdkWindow *window,
2472 GdkDevice *device,
2473 gint button,
2474 gint root_x,
2475 gint root_y,
2476 guint32 timestamp)
2477 {
2478 GdkWindowImplQuartz *impl;
2479
2480 if (GDK_WINDOW_DESTROYED (window) ||
2481 !WINDOW_IS_TOPLEVEL (window))
2482 return;
2483
2484 impl = gdk_window_get_quartz_impl (window);
2485
2486 if (!(impl && impl->toplevel))
2487 {
2488 g_warning ("Can't call gdk_window_begin_move_drag on non-quartz or non-toplevel window");
2489 return;
2490 }
2491
2492 [(GdkQuartzNSWindow *)impl->toplevel beginManualMove];
2493 }
2494
2495 static void
2496 gdk_quartz_window_set_icon_list (GdkWindow *window,
2497 GList *pixbufs)
2498 {
2499 /* FIXME: Implement */
2500 }
2501
2502 static void
2503 gdk_quartz_window_get_frame_extents (GdkWindow *window,
2504 GdkRectangle *rect)
2505 {
2506 GdkWindow *toplevel;
2507 GdkWindowImplQuartz *impl;
2508 NSRect ns_rect;
2509
2510 g_return_if_fail (rect != NULL);
2511
2512
2513 rect->x = 0;
2514 rect->y = 0;
2515 rect->width = 1;
2516 rect->height = 1;
2517
2518 toplevel = gdk_window_get_effective_toplevel (window);
2519 impl = gdk_window_get_quartz_impl (toplevel);
2520
2521 if (!impl)
2522 return;
2523
2524 ns_rect = [impl->toplevel frame];
2525
2526 _gdk_quartz_window_xy_to_gdk_xy (ns_rect.origin.x,
2527 ns_rect.origin.y + ns_rect.size.height,
2528 &rect->x, &rect->y);
2529
2530 rect->width = ns_rect.size.width;
2531 rect->height = ns_rect.size.height;
2532 }
2533
2534 /* Fake protocol to make gcc think that it's OK to call setStyleMask
2535 even if it isn't. We check to make sure before actually calling
2536 it. */
2537
2538 @protocol CanSetStyleMask
2539 - (void)setStyleMask:(int)mask;
2540 @end
2541
2542 static void
2543 gdk_quartz_window_set_decorations (GdkWindow *window,
2544 GdkWMDecoration decorations)
2545 {
2546 GdkWindowImplQuartz *impl;
2547 NSUInteger old_mask, new_mask;
2548 NSView *old_view;
2549
2550 if (GDK_WINDOW_DESTROYED (window) ||
2551 !WINDOW_IS_TOPLEVEL (window))
2552 return;
2553
2554 impl = gdk_window_get_quartz_impl (window);
2555
2556 if (GDK_WINDOW_TYPE (window) == GDK_WINDOW_TEMP ||
2557 (impl &&
2558 impl->type_hint == GDK_WINDOW_TYPE_HINT_SPLASHSCREEN ))
2559 {
2560 new_mask = GDK_QUARTZ_BORDERLESS_WINDOW;
2561 }
2562 else if (decorations == 0) {
2563 new_mask = GDK_QUARTZ_BORDERLESS_WINDOW | GDK_QUARTZ_MINIATURIZABLE_WINDOW;
2564 }
2565 else
2566 {
2567 /* FIXME: Honor other GDK_DECOR_* flags. */
2568 new_mask = (GDK_QUARTZ_TITLED_WINDOW | GDK_QUARTZ_CLOSABLE_WINDOW |
2569 GDK_QUARTZ_MINIATURIZABLE_WINDOW |
2570 GDK_QUARTZ_RESIZABLE_WINDOW);
2571 }
2572
2573 GDK_QUARTZ_ALLOC_POOL;
2574
2575 old_mask = [impl->toplevel styleMask];
2576
2577 if (old_mask != new_mask)
2578 {
2579 NSRect rect;
2580
2581 old_view = [[impl->toplevel contentView] retain];
2582
2583 rect = [impl->toplevel frame];
2584
2585 /* Properly update the size of the window when the titlebar is
2586 * added or removed.
2587 */
2588 if (old_mask == GDK_QUARTZ_BORDERLESS_WINDOW &&
2589 new_mask != GDK_QUARTZ_BORDERLESS_WINDOW)
2590 {
2591 rect = [NSWindow frameRectForContentRect:rect styleMask:new_mask];
2592
2593 }
2594 else if (old_mask != GDK_QUARTZ_BORDERLESS_WINDOW &&
2595 new_mask == GDK_QUARTZ_BORDERLESS_WINDOW)
2596 {
2597 rect = [NSWindow contentRectForFrameRect:rect styleMask:old_mask];
2598 }
2599
2600 /* Note, before OS 10.6 there doesn't seem to be a way to change this
2601 * without recreating the toplevel. From 10.6 onward, a simple call to
2602 * setStyleMask takes care of most of this, except for ensuring that the
2603 * title is set.
2604 */
2605 if ([impl->toplevel respondsToSelector:@selector(setStyleMask:)])
2606 {
2607 NSString *title = [impl->toplevel title];
2608
2609 [(id<CanSetStyleMask>)impl->toplevel setStyleMask:new_mask];
2610
2611 /* It appears that unsetting and then resetting
2612 * GDK_QUARTZ_TITLED_WINDOW does not reset the title in the
2613 * title bar as might be expected.
2614 *
2615 * In theory we only need to set this if new_mask includes
2616 * GDK_QUARTZ_TITLED_WINDOW. This behaved extremely oddly when
2617 * conditionalized upon that and since it has no side effects (i.e.
2618 * if GDK_QUARTZ_TITLED_WINDOW is not requested, the title will not be
2619 * displayed) just do it unconditionally. We also must null check
2620 * 'title' before setting it to avoid crashing.
2621 */
2622 if (title)
2623 [impl->toplevel setTitle:title];
2624 }
2625 else
2626 {
2627 NSString *title = [impl->toplevel title];
2628 NSColor *bg = [impl->toplevel backgroundColor];
2629 NSScreen *screen = [impl->toplevel screen];
2630
2631 /* Make sure the old window is closed, recall that releasedWhenClosed
2632 * is set on GdkQuartzWindows.
2633 */
2634 [impl->toplevel close];
2635
2636 impl->toplevel = [[GdkQuartzNSWindow alloc] initWithContentRect:rect
2637 styleMask:new_mask
2638 backing:NSBackingStoreBuffered
2639 defer:NO
2640 screen:screen];
2641 _gdk_quartz_window_update_has_shadow (impl);
2642
2643 [impl->toplevel setLevel: window_type_hint_to_level (impl->type_hint)];
2644 if (title)
2645 [impl->toplevel setTitle:title];
2646 [impl->toplevel setBackgroundColor:bg];
2647 [impl->toplevel setHidesOnDeactivate: window_type_hint_to_hides_on_deactivate (impl->type_hint)];
2648 [impl->toplevel setContentView:old_view];
2649 }
2650
2651 if (new_mask == GDK_QUARTZ_BORDERLESS_WINDOW)
2652 {
2653 [impl->toplevel setContentSize:rect.size];
2654 }
2655 else
2656 [impl->toplevel setFrame:rect display:YES];
2657
2658 /* Invalidate the window shadow for non-opaque views that have shadow
2659 * enabled, to get the shadow shape updated.
2660 */
2661 if (![old_view isOpaque] && [impl->toplevel hasShadow])
2662 [(GdkQuartzView*)old_view setNeedsInvalidateShadow:YES];
2663
2664 [old_view release];
2665 }
2666
2667 GDK_QUARTZ_RELEASE_POOL;
2668 }
2669
2670 static gboolean
2671 gdk_quartz_window_get_decorations (GdkWindow *window,
2672 GdkWMDecoration *decorations)
2673 {
2674 GdkWindowImplQuartz *impl;
2675
2676 if (GDK_WINDOW_DESTROYED (window) ||
2677 !WINDOW_IS_TOPLEVEL (window))
2678 return FALSE;
2679
2680 impl = gdk_window_get_quartz_impl (window);
2681
2682 if (decorations)
2683 {
2684 /* Borderless is 0, so we can't check it as a bit being set. */
2685 if (impl &&
2686 [impl->toplevel styleMask] == GDK_QUARTZ_BORDERLESS_WINDOW)
2687 {
2688 *decorations = 0;
2689 }
2690 else
2691 {
2692 /* FIXME: Honor the other GDK_DECOR_* flags. */
2693 *decorations = GDK_DECOR_ALL;
2694 }
2695 }
2696
2697 return TRUE;
2698 }
2699
2700 static void
2701 gdk_quartz_window_set_functions (GdkWindow *window,
2702 GdkWMFunction functions)
2703 {
2704 GdkWindowImplQuartz *impl;
2705 gboolean min, max, close;
2706
2707 g_return_if_fail (GDK_IS_WINDOW (window));
2708
2709 impl = gdk_window_get_quartz_impl (window);
2710
2711 if (functions & GDK_FUNC_ALL)
2712 {
2713 min = !(functions & GDK_FUNC_MINIMIZE);
2714 max = !(functions & GDK_FUNC_MAXIMIZE);
2715 close = !(functions & GDK_FUNC_CLOSE);
2716 }
2717 else
2718 {
2719 min = (functions & GDK_FUNC_MINIMIZE);
2720 max = (functions & GDK_FUNC_MAXIMIZE);
2721 close = (functions & GDK_FUNC_CLOSE);
2722 }
2723
2724 if (impl && impl->toplevel)
2725 {
2726 NSUInteger mask = [impl->toplevel styleMask];
2727
2728 if (min)
2729 mask = mask | GDK_QUARTZ_MINIATURIZABLE_WINDOW;
2730 else
2731 mask = mask & ~GDK_QUARTZ_MINIATURIZABLE_WINDOW;
2732
2733 if (max)
2734 mask = mask | GDK_QUARTZ_RESIZABLE_WINDOW;
2735 else
2736 mask = mask & ~GDK_QUARTZ_RESIZABLE_WINDOW;
2737
2738 if (close)
2739 mask = mask | GDK_QUARTZ_CLOSABLE_WINDOW;
2740 else
2741 mask = mask & ~GDK_QUARTZ_CLOSABLE_WINDOW;
2742
2743 [impl->toplevel setStyleMask:mask];
2744 }
2745 }
2746
2747 static void
2748 gdk_quartz_window_stick (GdkWindow *window)
2749 {
2750 if (GDK_WINDOW_DESTROYED (window) ||
2751 !WINDOW_IS_TOPLEVEL (window))
2752 return;
2753 }
2754
2755 static void
2756 gdk_quartz_window_unstick (GdkWindow *window)
2757 {
2758 if (GDK_WINDOW_DESTROYED (window) ||
2759 !WINDOW_IS_TOPLEVEL (window))
2760 return;
2761 }
2762
2763 static void
2764 gdk_quartz_window_maximize (GdkWindow *window)
2765 {
2766 GdkWindowImplQuartz *impl;
2767 gboolean maximized;
2768
2769 if (GDK_WINDOW_DESTROYED (window) ||
2770 !WINDOW_IS_TOPLEVEL (window))
2771 return;
2772
2773 impl = gdk_window_get_quartz_impl (window);
2774 maximized = gdk_window_get_state (window) & GDK_WINDOW_STATE_MAXIMIZED;
2775
2776 if (GDK_WINDOW_IS_MAPPED (window) &&
2777 impl)
2778 {
2779 GDK_QUARTZ_ALLOC_POOL;
2780
2781 if (impl->toplevel && !maximized)
2782 [impl->toplevel zoom:nil];
2783
2784 GDK_QUARTZ_RELEASE_POOL;
2785 }
2786 }
2787
2788 static void
2789 gdk_quartz_window_unmaximize (GdkWindow *window)
2790 {
2791 GdkWindowImplQuartz *impl;
2792 gboolean maximized;
2793
2794 if (GDK_WINDOW_DESTROYED (window) ||
2795 !WINDOW_IS_TOPLEVEL (window))
2796 return;
2797
2798 impl = gdk_window_get_quartz_impl (window);
2799 maximized = gdk_window_get_state (window) & GDK_WINDOW_STATE_MAXIMIZED;
2800
2801 if (GDK_WINDOW_IS_MAPPED (window) &&
2802 impl)
2803 {
2804 GDK_QUARTZ_ALLOC_POOL;
2805
2806 if (impl->toplevel && maximized)
2807 [impl->toplevel zoom:nil];
2808
2809 GDK_QUARTZ_RELEASE_POOL;
2810 }
2811 }
2812
2813 static void
2814 gdk_quartz_window_iconify (GdkWindow *window)
2815 {
2816 GdkWindowImplQuartz *impl;
2817
2818 if (GDK_WINDOW_DESTROYED (window) ||
2819 !WINDOW_IS_TOPLEVEL (window))
2820 return;
2821
2822 impl = gdk_window_get_quartz_impl (window);
2823
2824 if (GDK_WINDOW_IS_MAPPED (window) &&
2825 impl)
2826 {
2827 GDK_QUARTZ_ALLOC_POOL;
2828
2829 if (impl->toplevel)
2830 [impl->toplevel miniaturize:nil];
2831
2832 GDK_QUARTZ_RELEASE_POOL;
2833 }
2834 else
2835 {
2836 gdk_synthesize_window_state (window,
2837 0,
2838 GDK_WINDOW_STATE_ICONIFIED);
2839 }
2840 }
2841
2842 static void
2843 gdk_quartz_window_deiconify (GdkWindow *window)
2844 {
2845 GdkWindowImplQuartz *impl;
2846
2847 if (GDK_WINDOW_DESTROYED (window) ||
2848 !WINDOW_IS_TOPLEVEL (window))
2849 return;
2850
2851 impl = gdk_window_get_quartz_impl (window);
2852
2853 if (GDK_WINDOW_IS_MAPPED (window) &&
2854 impl)
2855 {
2856 GDK_QUARTZ_ALLOC_POOL;
2857
2858 if (impl->toplevel)
2859 [impl->toplevel deminiaturize:nil];
2860
2861 GDK_QUARTZ_RELEASE_POOL;
2862 }
2863 else
2864 {
2865 gdk_synthesize_window_state (window,
2866 GDK_WINDOW_STATE_ICONIFIED,
2867 0);
2868 }
2869 }
2870
2871 static gboolean
2872 window_is_fullscreen (GdkWindow *window)
2873 {
2874 GdkWindowImplQuartz *impl = gdk_window_get_quartz_impl (window);
2875
2876 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
2877 if (impl &&
2878 gdk_quartz_osx_version() >= GDK_OSX_LION)
2879 return ([impl->toplevel styleMask] & GDK_QUARTZ_FULLSCREEN_WINDOW) != 0;
2880 else
2881 #endif
2882 return g_object_get_data (G_OBJECT (window), FULLSCREEN_DATA) != NULL;
2883 }
2884
2885 static void
2886 gdk_quartz_window_fullscreen (GdkWindow *window)
2887 {
2888 GdkWindowImplQuartz *impl;
2889
2890 if (GDK_WINDOW_DESTROYED (window) ||
2891 !WINDOW_IS_TOPLEVEL (window))
2892 return;
2893
2894 impl = gdk_window_get_quartz_impl (window);
2895
2896 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
2897 if (gdk_quartz_osx_version() >= GDK_OSX_LION &&
2898 impl)
2899 {
2900 if (!window_is_fullscreen (window))
2901 [impl->toplevel toggleFullScreen:nil];
2902 }
2903 else
2904 {
2905 #endif
2906 FullscreenSavedGeometry *geometry;
2907 GdkWindowImplQuartz *impl = gdk_window_get_quartz_impl (window);
2908 NSRect frame;
2909
2910 if (GDK_WINDOW_DESTROYED (window) ||
2911 !WINDOW_IS_TOPLEVEL (window) ||
2912 !impl)
2913 return;
2914
2915 geometry = get_fullscreen_geometry (window);
2916 if (!geometry)
2917 {
2918 geometry = g_new (FullscreenSavedGeometry, 1);
2919
2920 geometry->x = window->x;
2921 geometry->y = window->y;
2922 geometry->width = window->width;
2923 geometry->height = window->height;
2924
2925 if (!gdk_window_get_decorations (window, &geometry->decor))
2926 geometry->decor = GDK_DECOR_ALL;
2927
2928 g_object_set_data_full (G_OBJECT (window),
2929 FULLSCREEN_DATA, geometry,
2930 g_free);
2931
2932 gdk_window_set_decorations (window, 0);
2933
2934 frame = [[impl->toplevel screen] frame];
2935 move_resize_window_internal (window,
2936 0, 0,
2937 frame.size.width, frame.size.height);
2938 [impl->toplevel setContentSize:frame.size];
2939 [impl->toplevel makeKeyAndOrderFront:impl->toplevel];
2940
2941 clear_toplevel_order ();
2942 }
2943
2944 SetSystemUIMode (kUIModeAllHidden, kUIOptionAutoShowMenuBar);
2945
2946 gdk_synthesize_window_state (window, 0, GDK_WINDOW_STATE_FULLSCREEN);
2947 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
2948 }
2949 #endif
2950 }
2951
2952 static void
2953 gdk_quartz_window_unfullscreen (GdkWindow *window)
2954 {
2955 GdkWindowImplQuartz *impl;
2956
2957 if (GDK_WINDOW_DESTROYED (window) ||
2958 !WINDOW_IS_TOPLEVEL (window))
2959 return;
2960
2961 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
2962 if (gdk_quartz_osx_version() >= GDK_OSX_LION)
2963 {
2964 impl = gdk_window_get_quartz_impl (window);
2965
2966 if (window_is_fullscreen (window) &&
2967 impl)
2968 [impl->toplevel toggleFullScreen:nil];
2969 }
2970 else
2971 {
2972 #endif
2973 GdkWindowImplQuartz *impl = gdk_window_get_quartz_impl (window);
2974 FullscreenSavedGeometry *geometry;
2975
2976 if (GDK_WINDOW_DESTROYED (window) ||
2977 !WINDOW_IS_TOPLEVEL (window) ||
2978 !impl)
2979 return;
2980
2981 geometry = get_fullscreen_geometry (window);
2982 if (geometry)
2983 {
2984 SetSystemUIMode (kUIModeNormal, 0);
2985
2986 move_resize_window_internal (window,
2987 geometry->x,
2988 geometry->y,
2989 geometry->width,
2990 geometry->height);
2991
2992 gdk_window_set_decorations (window, geometry->decor);
2993
2994 g_object_set_data (G_OBJECT (window), FULLSCREEN_DATA, NULL);
2995
2996 [impl->toplevel makeKeyAndOrderFront:impl->toplevel];
2997 clear_toplevel_order ();
2998
2999 gdk_synthesize_window_state (window, GDK_WINDOW_STATE_FULLSCREEN, 0);
3000 }
3001
3002 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
3003 }
3004 #endif
3005 }
3006
3007 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
3008 static FullscreenSavedGeometry *
3009 get_fullscreen_geometry (GdkWindow *window)
3010 {
3011 return g_object_get_data (G_OBJECT (window), FULLSCREEN_DATA);
3012 }
3013 #endif
3014
3015 void
3016 _gdk_quartz_window_update_fullscreen_state (GdkWindow *window)
3017 {
3018 if (GDK_WINDOW_DESTROYED (window) || !WINDOW_IS_TOPLEVEL (window))
3019 return;
3020
3021 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
3022 if (gdk_quartz_osx_version() >= GDK_OSX_LION)
3023 {
3024 gboolean is_fullscreen = window_is_fullscreen (window);
3025 gboolean was_fullscreen = (gdk_window_get_state (window) &
3026 GDK_WINDOW_STATE_FULLSCREEN) != 0;
3027
3028 if (is_fullscreen != was_fullscreen)
3029 {
3030 if (is_fullscreen)
3031 gdk_synthesize_window_state (window, 0, GDK_WINDOW_STATE_FULLSCREEN);
3032 else
3033 gdk_synthesize_window_state (window, GDK_WINDOW_STATE_FULLSCREEN, 0);
3034 }
3035 }
3036 #endif
3037 }
3038
3039 static void
3040 gdk_quartz_window_set_keep_above (GdkWindow *window,
3041 gboolean setting)
3042 {
3043 GdkWindowImplQuartz *impl = gdk_window_get_quartz_impl (window);
3044 gint level;
3045
3046 g_return_if_fail (GDK_IS_WINDOW (window));
3047 g_return_if_fail (impl);
3048
3049 if (GDK_WINDOW_DESTROYED (window) ||
3050 !WINDOW_IS_TOPLEVEL (window))
3051 return;
3052
3053 level = window_type_hint_to_level (gdk_window_get_type_hint (window));
3054
3055 /* Adjust normal window level by one if necessary. */
3056 [impl->toplevel setLevel: level + (setting ? 1 : 0)];
3057 }
3058
3059 static void
3060 gdk_quartz_window_set_keep_below (GdkWindow *window,
3061 gboolean setting)
3062 {
3063 GdkWindowImplQuartz *impl = gdk_window_get_quartz_impl (window);
3064 gint level;
3065
3066 g_return_if_fail (GDK_IS_WINDOW (window));
3067 g_return_if_fail (impl);
3068
3069 if (GDK_WINDOW_DESTROYED (window) ||
3070 !WINDOW_IS_TOPLEVEL (window))
3071 return;
3072
3073 level = window_type_hint_to_level (gdk_window_get_type_hint (window));
3074
3075 /* Adjust normal window level by one if necessary. */
3076 [impl->toplevel setLevel: level - (setting ? 1 : 0)];
3077 }
3078
3079 /* X11 "feature" not useful in other backends. */
3080 static GdkWindow *
3081 gdk_quartz_window_get_group (GdkWindow *window)
3082 {
3083 return NULL;
3084 }
3085
3086 /* X11 "feature" not useful in other backends. */
3087 static void
3088 gdk_quartz_window_set_group (GdkWindow *window,
3089 GdkWindow *leader)
3090 {
3091 }
3092
3093 static void
3094 gdk_quartz_window_destroy_notify (GdkWindow *window)
3095 {
3096 GdkDisplay *display = gdk_window_get_display (window);
3097 GdkSeat *seat = gdk_display_get_default_seat (display);
3098 gdk_seat_ungrab (seat);
3099 }
3100
3101 static void
3102 gdk_quartz_window_set_opacity (GdkWindow *window,
3103 gdouble opacity)
3104 {
3105 GdkWindowImplQuartz *impl = gdk_window_get_quartz_impl (window);
3106
3107 g_return_if_fail (GDK_IS_WINDOW (window));
3108 g_return_if_fail (WINDOW_IS_TOPLEVEL (window));
3109 g_return_if_fail (impl);
3110
3111 if (GDK_WINDOW_DESTROYED (window) ||
3112 !WINDOW_IS_TOPLEVEL (window))
3113 return;
3114
3115 if (opacity < 0)
3116 opacity = 0;
3117 else if (opacity > 1)
3118 opacity = 1;
3119
3120 [impl->toplevel setAlphaValue: opacity];
3121 }
3122
3123 static void
3124 gdk_quartz_window_set_shadow_width (GdkWindow *window,
3125 gint left,
3126 gint right,
3127 gint top,
3128 gint bottom)
3129 {
3130 GdkWindowImplQuartz *impl = gdk_window_get_quartz_impl (window);
3131
3132 g_return_if_fail (GDK_IS_WINDOW (window));
3133 g_return_if_fail (WINDOW_IS_TOPLEVEL (window));
3134 g_return_if_fail (impl);
3135
3136 if (GDK_WINDOW_DESTROYED (window) ||
3137 !WINDOW_IS_TOPLEVEL (window))
3138 return;
3139
3140 impl->shadow_top = top;
3141 impl->shadow_max = MAX (MAX (left, right), MAX (top, bottom));
3142 _gdk_quartz_window_update_has_shadow (impl);
3143 }
3144
3145 static cairo_region_t *
3146 gdk_quartz_window_get_shape (GdkWindow *window)
3147 {
3148 /* FIXME: implement */
3149 return NULL;
3150 }
3151
3152 static cairo_region_t *
3153 gdk_quartz_window_get_input_shape (GdkWindow *window)
3154 {
3155 /* FIXME: implement */
3156 return NULL;
3157 }
3158
3159 /* Protocol to build cleanly for OSX < 10.7 */
3160 @protocol ScaleFactor
3161 - (CGFloat) backingScaleFactor;
3162 @end
3163
3164 static gint
3165 gdk_quartz_window_get_scale_factor (GdkWindow *window)
3166 {
3167 GdkWindowImplQuartz *impl;
3168
3169 if (GDK_WINDOW_DESTROYED (window))
3170 return 1;
3171
3172 impl = gdk_window_get_quartz_impl (window);
3173
3174 if (impl && impl->toplevel != NULL &&
3175 gdk_quartz_osx_version() >= GDK_OSX_LION)
3176 return [(id <ScaleFactor>) impl->toplevel backingScaleFactor];
3177
3178 return 1;
3179 }
3180
3181 static void
3182 gdk_window_impl_quartz_class_init (GdkWindowImplQuartzClass *klass)
3183 {
3184 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3185 GdkWindowImplClass *impl_class = GDK_WINDOW_IMPL_CLASS (klass);
3186 GdkWindowImplQuartzClass *impl_quartz_class = GDK_WINDOW_IMPL_QUARTZ_CLASS (klass);
3187
3188 parent_class = g_type_class_peek_parent (klass);
3189
3190 object_class->finalize = gdk_window_impl_quartz_finalize;
3191
3192 impl_class->ref_cairo_surface = gdk_quartz_ref_cairo_surface;
3193 impl_class->show = gdk_window_quartz_show;
3194 impl_class->hide = gdk_window_quartz_hide;
3195 impl_class->withdraw = gdk_window_quartz_withdraw;
3196 impl_class->set_events = gdk_window_quartz_set_events;
3197 impl_class->get_events = gdk_window_quartz_get_events;
3198 impl_class->raise = gdk_window_quartz_raise;
3199 impl_class->lower = gdk_window_quartz_lower;
3200 impl_class->restack_toplevel = gdk_window_quartz_restack_toplevel;
3201 impl_class->move_resize = gdk_window_quartz_move_resize;
3202 impl_class->set_background = gdk_window_quartz_set_background;
3203 impl_class->reparent = gdk_window_quartz_reparent;
3204 impl_class->set_device_cursor = gdk_window_quartz_set_device_cursor;
3205 impl_class->get_geometry = gdk_window_quartz_get_geometry;
3206 impl_class->get_root_coords = gdk_window_quartz_get_root_coords;
3207 impl_class->get_device_state = gdk_window_quartz_get_device_state;
3208 impl_class->shape_combine_region = gdk_window_quartz_shape_combine_region;
3209 impl_class->input_shape_combine_region = gdk_window_quartz_input_shape_combine_region;
3210 impl_class->destroy = gdk_quartz_window_destroy;
3211 impl_class->destroy_foreign = gdk_quartz_window_destroy_foreign;
3212 impl_class->get_shape = gdk_quartz_window_get_shape;
3213 impl_class->get_input_shape = gdk_quartz_window_get_input_shape;
3214 impl_class->begin_paint = gdk_window_impl_quartz_begin_paint;
3215 impl_class->get_scale_factor = gdk_quartz_window_get_scale_factor;
3216
3217 impl_class->focus = gdk_quartz_window_focus;
3218 impl_class->set_type_hint = gdk_quartz_window_set_type_hint;
3219 impl_class->get_type_hint = gdk_quartz_window_get_type_hint;
3220 impl_class->set_modal_hint = gdk_quartz_window_set_modal_hint;
3221 impl_class->set_skip_taskbar_hint = gdk_quartz_window_set_skip_taskbar_hint;
3222 impl_class->set_skip_pager_hint = gdk_quartz_window_set_skip_pager_hint;
3223 impl_class->set_urgency_hint = gdk_quartz_window_set_urgency_hint;
3224 impl_class->set_geometry_hints = gdk_quartz_window_set_geometry_hints;
3225 impl_class->set_title = gdk_quartz_window_set_title;
3226 impl_class->set_role = gdk_quartz_window_set_role;
3227 impl_class->set_startup_id = gdk_quartz_window_set_startup_id;
3228 impl_class->set_transient_for = gdk_quartz_window_set_transient_for;
3229 impl_class->get_frame_extents = gdk_quartz_window_get_frame_extents;
3230 impl_class->set_override_redirect = gdk_quartz_window_set_override_redirect;
3231 impl_class->set_accept_focus = gdk_quartz_window_set_accept_focus;
3232 impl_class->set_focus_on_map = gdk_quartz_window_set_focus_on_map;
3233 impl_class->set_icon_list = gdk_quartz_window_set_icon_list;
3234 impl_class->set_icon_name = gdk_quartz_window_set_icon_name;
3235 impl_class->iconify = gdk_quartz_window_iconify;
3236 impl_class->deiconify = gdk_quartz_window_deiconify;
3237 impl_class->stick = gdk_quartz_window_stick;
3238 impl_class->unstick = gdk_quartz_window_unstick;
3239 impl_class->maximize = gdk_quartz_window_maximize;
3240 impl_class->unmaximize = gdk_quartz_window_unmaximize;
3241 impl_class->fullscreen = gdk_quartz_window_fullscreen;
3242 impl_class->unfullscreen = gdk_quartz_window_unfullscreen;
3243 impl_class->set_keep_above = gdk_quartz_window_set_keep_above;
3244 impl_class->set_keep_below = gdk_quartz_window_set_keep_below;
3245 impl_class->get_group = gdk_quartz_window_get_group;
3246 impl_class->set_group = gdk_quartz_window_set_group;
3247 impl_class->set_decorations = gdk_quartz_window_set_decorations;
3248 impl_class->get_decorations = gdk_quartz_window_get_decorations;
3249 impl_class->set_functions = gdk_quartz_window_set_functions;
3250 impl_class->begin_resize_drag = gdk_quartz_window_begin_resize_drag;
3251 impl_class->begin_move_drag = gdk_quartz_window_begin_move_drag;
3252 impl_class->set_opacity = gdk_quartz_window_set_opacity;
3253 impl_class->set_shadow_width = gdk_quartz_window_set_shadow_width;
3254 impl_class->destroy_notify = gdk_quartz_window_destroy_notify;
3255 impl_class->register_dnd = _gdk_quartz_window_register_dnd;
3256 impl_class->drag_begin = _gdk_quartz_window_drag_begin;
3257 impl_class->process_updates_recurse = _gdk_quartz_window_process_updates_recurse;
3258 impl_class->sync_rendering = _gdk_quartz_window_sync_rendering;
3259 impl_class->simulate_key = _gdk_quartz_window_simulate_key;
3260 impl_class->simulate_button = _gdk_quartz_window_simulate_button;
3261 impl_class->get_property = _gdk_quartz_window_get_property;
3262 impl_class->change_property = _gdk_quartz_window_change_property;
3263 impl_class->delete_property = _gdk_quartz_window_delete_property;
3264
3265 impl_class->create_gl_context = gdk_quartz_window_create_gl_context;
3266 impl_class->invalidate_for_new_frame = gdk_quartz_window_invalidate_for_new_frame;
3267
3268 impl_quartz_class->get_context = gdk_window_impl_quartz_get_context;
3269 impl_quartz_class->release_context = gdk_window_impl_quartz_release_context;
3270 }
3271
3272 GType
3273 _gdk_window_impl_quartz_get_type (void)
3274 {
3275 static GType object_type = 0;
3276
3277 if (!object_type)
3278 {
3279 const GTypeInfo object_info =
3280 {
3281 sizeof (GdkWindowImplQuartzClass),
3282 (GBaseInitFunc) NULL,
3283 (GBaseFinalizeFunc) NULL,
3284 (GClassInitFunc) gdk_window_impl_quartz_class_init,
3285 NULL, /* class_finalize */
3286 NULL, /* class_data */
3287 sizeof (GdkWindowImplQuartz),
3288 0, /* n_preallocs */
3289 (GInstanceInitFunc) gdk_window_impl_quartz_init,
3290 };
3291
3292 object_type = g_type_register_static (GDK_TYPE_WINDOW_IMPL,
3293 "GdkWindowImplQuartz",
3294 &object_info, 0);
3295 }
3296
3297 return object_type;
3298 }
3299
3300 CGContextRef
3301 gdk_quartz_window_get_context (GdkWindowImplQuartz *window,
3302 gboolean antialias)
3303 {
3304 if (!GDK_WINDOW_IMPL_QUARTZ_GET_CLASS (window)->get_context)
3305 {
3306 g_warning ("%s doesn't implement GdkWindowImplQuartzClass::get_context()",
3307 G_OBJECT_TYPE_NAME (window));
3308 return NULL;
3309 }
3310
3311 return GDK_WINDOW_IMPL_QUARTZ_GET_CLASS (window)->get_context (window, antialias);
3312 }
3313
3314 void
3315 gdk_quartz_window_release_context (GdkWindowImplQuartz *window,
3316 CGContextRef cg_context)
3317 {
3318 if (!GDK_WINDOW_IMPL_QUARTZ_GET_CLASS (window)->release_context)
3319 {
3320 g_warning ("%s doesn't implement GdkWindowImplQuartzClass::release_context()",
3321 G_OBJECT_TYPE_NAME (window));
3322 return;
3323 }
3324
3325 g_return_if_fail (cg_context);
3326 GDK_WINDOW_IMPL_QUARTZ_GET_CLASS (window)->release_context (window, cg_context);
3327 }
3328
3329 /* macOS doesn't define a root window, but Gdk needs one for two
3330 * purposes: To be a parent reference for some toplevels and to be a
3331 * fallback window when gdk_window_create_image_surface is called with
3332 * a NULL GdkWindow.
3333 *
3334 */
3335
3336 static CGContextRef gdk_root_window_impl_quartz_get_context (GdkWindowImplQuartz *window, gboolean antialias);
3337 static void gdk_root_window_impl_quartz_release_context (GdkWindowImplQuartz *window, CGContextRef cg_context);
3338
3339 static void
3340 gdk_root_window_impl_quartz_class_init (GdkRootWindowImplQuartzClass *klass)
3341 {
3342 GdkWindowImplQuartzClass *window_quartz_class = GDK_WINDOW_IMPL_QUARTZ_CLASS (klass);
3343
3344 root_window_parent_class = g_type_class_peek_parent (klass);
3345
3346 window_quartz_class->get_context = gdk_root_window_impl_quartz_get_context;
3347 window_quartz_class->release_context = gdk_root_window_impl_quartz_release_context;
3348 }
3349
3350 static void
3351 gdk_root_window_impl_quartz_init (GdkRootWindowImplQuartz *impl)
3352 {
3353 CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB ();
3354 /* Alpha channel Info: Cairo, CGImage, and CVPixelBuffer all use
3355 * kCGImageAlphaPremultipliedFirst, CALayer.contents wants
3356 * kCGImageAlphaPremultipliedLast.
3357 */
3358 CGBitmapInfo info = (CGBitmapInfo)kCGImageAlphaPremultipliedLast;
3359 impl->cg_context = CGBitmapContextCreate (NULL, 1, 1, 8, 4,
3360 colorspace, info);
3361 CGColorSpaceRelease (colorspace);
3362 impl->cg_layers = NULL;
3363 }
3364
3365 static void
3366 gdk_root_window_impl_quartz_dispose (GdkRootWindowImplQuartz *impl)
3367 {
3368 g_list_free_full (impl->cg_layers, (GDestroyNotify)CGLayerRelease);
3369 }
3370
3371 GType
3372 _gdk_root_window_impl_quartz_get_type (void)
3373 {
3374 static GType object_type = 0;
3375
3376 if (!object_type)
3377 {
3378 const GTypeInfo object_info =
3379 {
3380 sizeof (GdkRootWindowImplQuartzClass),
3381 (GBaseInitFunc) NULL,
3382 (GBaseFinalizeFunc) NULL,
3383 (GClassInitFunc) gdk_root_window_impl_quartz_class_init,
3384 NULL, /* class_finalize */
3385 NULL, /* class_data */
3386 sizeof (GdkRootWindowImplQuartz),
3387 0, /* n_preallocs */
3388 (GInstanceInitFunc) gdk_root_window_impl_quartz_init,
3389 };
3390
3391 object_type = g_type_register_static (GDK_TYPE_WINDOW_IMPL_QUARTZ,
3392 "GdkRootWindowQuartz",
3393 &object_info, 0);
3394 }
3395
3396 return object_type;
3397 }
3398
3399
3400 static CGContextRef
3401 gdk_root_window_impl_quartz_get_context (GdkWindowImplQuartz *window_impl,
3402 gboolean antialias)
3403 {
3404 GdkRootWindowImplQuartz *impl = GDK_ROOT_WINDOW_IMPL_QUARTZ (window_impl);
3405 CGSize size;
3406 CGLayerRef layer;
3407
3408 if (!window_impl || GDK_WINDOW_DESTROYED (window_impl->wrapper))
3409 return NULL;
3410
3411 size.width = gdk_window_get_width (window_impl->wrapper);
3412 size.height = gdk_window_get_height (window_impl->wrapper);
3413 layer = CGLayerCreateWithContext(impl->cg_context, size, NULL);
3414 impl->cg_layers = g_list_prepend(impl->cg_layers, CGLayerRetain (layer));
3415 return CGContextRetain (CGLayerGetContext (layer));
3416 }
3417
3418 static void
3419 gdk_root_window_impl_quartz_release_context (GdkWindowImplQuartz *window,
3420 CGContextRef cg_context)
3421 {
3422 CGContextRelease (cg_context);
3423 }