gdkwindow-quartz.c 79.9 KB
Newer Older
Anders Carlsson's avatar
Anders Carlsson committed
1
2
3
/* gdkwindow-quartz.c
 *
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4
 * Copyright (C) 2005-2007 Imendio AB
Anders Carlsson's avatar
Anders Carlsson committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

22
#include "config.h"
23
#include <Carbon/Carbon.h>
Anders Carlsson's avatar
Anders Carlsson committed
24
25

#include "gdk.h"
26
#include "gdkwindowimpl.h"
Anders Carlsson's avatar
Anders Carlsson committed
27
#include "gdkprivate-quartz.h"
28
#include "gdkscreen-quartz.h"
29
#include "gdkinputprivate.h"
Anders Carlsson's avatar
Anders Carlsson committed
30
31
32

static gpointer parent_class;

33
34
static GSList   *update_nswindows;
static gboolean  in_process_all_updates = FALSE;
35
36

static GSList *main_window_stack;
37

38
39
40
41
42
43
44
45
46
47
#define FULLSCREEN_DATA "fullscreen-data"

typedef struct
{
  gint            x, y;
  gint            width, height;
  GdkWMDecoration decor;
} FullscreenSavedGeometry;


48
static void update_toplevel_order (void);
49
50
51
static void clear_toplevel_order  (void);

static FullscreenSavedGeometry *get_fullscreen_geometry (GdkWindow *window);
52

53
54
55
56
#define WINDOW_IS_TOPLEVEL(window)		     \
  (GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD &&   \
   GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN && \
   GDK_WINDOW_TYPE (window) != GDK_WINDOW_OFFSCREEN)
57

58
59
static void gdk_window_impl_iface_init (GdkWindowImplIface *iface);

60
61
62
63
64
NSView *
gdk_quartz_window_get_nsview (GdkWindow *window)
{
  GdkWindowObject *private = (GdkWindowObject *)window;

65
66
67
  if (GDK_WINDOW_DESTROYED (window))
    return NULL;

68
69
70
  return ((GdkWindowImplQuartz *)private->impl)->view;
}

71
72
73
74
75
76
77
78
79
80
81
NSWindow *
gdk_quartz_window_get_nswindow (GdkWindow *window)
{
  GdkWindowObject *private = (GdkWindowObject *)window;

  if (GDK_WINDOW_DESTROYED (window))
    return NULL;

  return ((GdkWindowImplQuartz *)private->impl)->toplevel;
}

Sven Herzberg's avatar
Sven Herzberg committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
static CGContextRef
gdk_window_impl_quartz_get_context (GdkDrawable *drawable,
				    gboolean     antialias)
{
  GdkDrawableImplQuartz *drawable_impl = GDK_DRAWABLE_IMPL_QUARTZ (drawable);
  GdkWindowImplQuartz *window_impl = GDK_WINDOW_IMPL_QUARTZ (drawable);
  CGContextRef cg_context;

  if (GDK_WINDOW_DESTROYED (drawable_impl->wrapper))
    return NULL;

  /* Lock focus when not called as part of a drawRect call. This
   * is needed when called from outside "real" expose events, for
   * example for synthesized expose events when realizing windows
   * and for widgets that send fake expose events like the arrow
97
   * buttons in spinbuttons or the position marker in rulers.
Sven Herzberg's avatar
Sven Herzberg committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
   */
  if (window_impl->in_paint_rect_count == 0)
    {
      if (![window_impl->view lockFocusIfCanDraw])
        return NULL;
    }

  cg_context = [[NSGraphicsContext currentContext] graphicsPort];
  CGContextSaveGState (cg_context);
  CGContextSetAllowsAntialiasing (cg_context, antialias);

  /* We'll emulate the clipping caused by double buffering here */
  if (window_impl->begin_paint_count != 0)
    {
      CGRect rect;
      CGRect *cg_rects;
      GdkRectangle *rects;
      gint n_rects, i;

      gdk_region_get_rectangles (window_impl->paint_clip_region,
                                 &rects, &n_rects);

      if (n_rects == 1)
        cg_rects = &rect;
      else
        cg_rects = g_new (CGRect, n_rects);

      for (i = 0; i < n_rects; i++)
        {
          cg_rects[i].origin.x = rects[i].x;
          cg_rects[i].origin.y = rects[i].y;
          cg_rects[i].size.width = rects[i].width;
          cg_rects[i].size.height = rects[i].height;
        }

      CGContextClipToRects (cg_context, cg_rects, n_rects);

      g_free (rects);
      if (cg_rects != &rect)
        g_free (cg_rects);
    }

  return cg_context;
}

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
static void
check_grab_unmap (GdkWindow *window)
{
  GdkDisplay *display = gdk_drawable_get_display (window);

  _gdk_display_end_pointer_grab (display, 0, window, TRUE);

  if (display->keyboard_grab.window)
    {
      GdkWindowObject *private = GDK_WINDOW_OBJECT (window);
      GdkWindowObject *tmp = GDK_WINDOW_OBJECT (display->keyboard_grab.window);

      while (tmp && tmp != private)
	tmp = tmp->parent;

      if (tmp)
	_gdk_display_unset_has_keyboard_grab (display, TRUE);
    }
}

static void
check_grab_destroy (GdkWindow *window)
{
  GdkDisplay *display = gdk_drawable_get_display (window);
  GdkPointerGrabInfo *grab;

  /* Make sure there is no lasting grab in this native window */
  grab = _gdk_display_get_last_pointer_grab (display);
  if (grab && grab->native_window == window)
    {
      /* Serials are always 0 in quartz, but for clarity: */
      grab->serial_end = grab->serial_start;
      grab->implicit_ungrab = TRUE;
    }

  if (window == display->keyboard_grab.native_window &&
      display->keyboard_grab.window != NULL)
    _gdk_display_unset_has_keyboard_grab (display, TRUE);
}

Anders Carlsson's avatar
Anders Carlsson committed
183
184
185
186
187
static void
gdk_window_impl_quartz_finalize (GObject *object)
{
  GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (object);

188
189
  check_grab_destroy (GDK_DRAWABLE_IMPL_QUARTZ (object)->wrapper);

190
191
192
  if (impl->paint_clip_region)
    gdk_region_destroy (impl->paint_clip_region);

193
194
195
  if (impl->transient_for)
    g_object_unref (impl->transient_for);

Anders Carlsson's avatar
Anders Carlsson committed
196
197
198
199
200
201
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gdk_window_impl_quartz_class_init (GdkWindowImplQuartzClass *klass)
{
202
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
203
  GdkDrawableImplQuartzClass *drawable_quartz_class = GDK_DRAWABLE_IMPL_QUARTZ_CLASS (klass);
Anders Carlsson's avatar
Anders Carlsson committed
204
205
206

  parent_class = g_type_class_peek_parent (klass);

207
208
  object_class->finalize = gdk_window_impl_quartz_finalize;

Sven Herzberg's avatar
Sven Herzberg committed
209
  drawable_quartz_class->get_context = gdk_window_impl_quartz_get_context;
Anders Carlsson's avatar
Anders Carlsson committed
210
211
212
213
214
}

static void
gdk_window_impl_quartz_init (GdkWindowImplQuartz *impl)
{
215
  impl->type_hint = GDK_WINDOW_TYPE_HINT_NORMAL;
Anders Carlsson's avatar
Anders Carlsson committed
216
217
}

218
static void
219
gdk_window_impl_quartz_begin_paint_region (GdkPaintable    *paintable,
220
                                           GdkWindow       *window,
221
					   const GdkRegion *region)
222
223
{
  GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (paintable);
224
  GdkWindowObject *private = (GdkWindowObject*)window;
225
  int n_rects;
226
  GdkRectangle *rects = NULL;
227
  GdkPixmap *bg_pixmap;
228
229
230
231
  GdkRegion *clipped_and_offset_region;
  gboolean free_clipped_and_offset_region = TRUE;

  bg_pixmap = private->bg_pixmap;
232

233
234
235
236
237
238
  clipped_and_offset_region = gdk_region_copy (region);

  gdk_region_intersect (clipped_and_offset_region,
                        private->clip_region_with_children);
  gdk_region_offset (clipped_and_offset_region,
                     private->abs_x, private->abs_y);
239
240

  if (impl->begin_paint_count == 0)
241
242
243
244
    {
      impl->paint_clip_region = clipped_and_offset_region;
      free_clipped_and_offset_region = FALSE;
    }
245
  else
246
    gdk_region_union (impl->paint_clip_region, clipped_and_offset_region);
247

248
  impl->begin_paint_count++;
249

250
  if (bg_pixmap == GDK_NO_BG)
251
    goto done;
252

253
  gdk_region_get_rectangles (clipped_and_offset_region, &rects, &n_rects);
254

255
256
  if (bg_pixmap == NULL)
    {
257
258
      CGContextRef cg_context;
      gfloat r, g, b, a;
259
      gint i;
260
261

      cg_context = gdk_quartz_drawable_get_context (GDK_DRAWABLE (impl), FALSE);
262
263
264
      _gdk_quartz_colormap_get_rgba_from_pixel (gdk_drawable_get_colormap (window),
                                                private->bg_color.pixel,
                                                &r, &g, &b, &a);
265
      CGContextSetRGBFillColor (cg_context, r, g, b, a);
266
 
267
      for (i = 0; i < n_rects; i++)
268
        {
269
          CGContextFillRect (cg_context,
270
271
                             CGRectMake (rects[i].x, rects[i].y,
                                         rects[i].width, rects[i].height));
272
        }
273
274

      gdk_quartz_drawable_release_context (GDK_DRAWABLE (impl), cg_context);
275
276
    }
  else
277
    {
278
279
280
281
      int x, y;
      int x_offset, y_offset;
      int width, height;
      GdkGC *gc;
282

283
      x_offset = y_offset = 0;
284

285
      while (window && bg_pixmap == GDK_PARENT_RELATIVE_BG)
286
287
288
289
290
291
292
293
        {
          /* If this window should have the same background as the parent,
           * fetch the parent. (And if the same goes for the parent, fetch
           * the grandparent, etc.)
           */
          x_offset += ((GdkWindowObject *) window)->x;
          y_offset += ((GdkWindowObject *) window)->y;
          window = GDK_WINDOW (((GdkWindowObject *) window)->parent);
294
295
296
297
298
299
300
301
          bg_pixmap = ((GdkWindowObject *) window)->bg_pixmap;
        }

      if (bg_pixmap == NULL || bg_pixmap == GDK_NO_BG || bg_pixmap == GDK_PARENT_RELATIVE_BG)
        {
          /* Parent relative background but the parent doesn't have a
           * pixmap.
           */ 
302
          goto done;
303
304
305
306
307
308
        }

      /* Note: There should be a CG API to draw tiled images, we might
       * want to look into that for this. 
       */
      gc = gdk_gc_new (GDK_DRAWABLE (impl));
309

310
      gdk_drawable_get_size (GDK_DRAWABLE (bg_pixmap), &width, &height);
311

312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
      x = -x_offset;
      while (x < (rects[0].x + rects[0].width))
        {
          if (x + width >= rects[0].x)
	    {
              y = -y_offset;
              while (y < (rects[0].y + rects[0].height))
                {
                  if (y + height >= rects[0].y)
                    gdk_draw_drawable (GDK_DRAWABLE (impl), gc, bg_pixmap, 0, 0, x, y, width, height);
		  
                  y += height;
                }
            }
          x += width;
        }
328
329

      g_object_unref (gc);
330
    }
331

332
333
334
 done:
  if (free_clipped_and_offset_region)
    gdk_region_destroy (clipped_and_offset_region);
335
336
337
338
339
340
341
342
  g_free (rects);
}

static void
gdk_window_impl_quartz_end_paint (GdkPaintable *paintable)
{
  GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (paintable);

343
  impl->begin_paint_count--;
344
345
346
347
348
349

  if (impl->begin_paint_count == 0)
    {
      gdk_region_destroy (impl->paint_clip_region);
      impl->paint_clip_region = NULL;
    }
350
351
}

352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
void
_gdk_quartz_window_set_needs_display_in_rect (GdkWindow    *window,
                                              GdkRectangle *rect)
{
  GdkWindowObject *private;
  GdkWindowImplQuartz *impl;

  private = GDK_WINDOW_OBJECT (window);
  impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);

  if (!impl->needs_display_region)
    impl->needs_display_region = gdk_region_new ();

  gdk_region_union_with_rect (impl->needs_display_region, rect);

  [impl->view setNeedsDisplayInRect:NSMakeRect (rect->x, rect->y,
                                                rect->width, rect->height)];

}

372
373
374
void
_gdk_windowing_window_process_updates_recurse (GdkWindow *window,
                                               GdkRegion *region)
375
{
376
377
  int i, n_rects;
  GdkRectangle *rects;
378

379
380
381
382
  /* Make sure to only flush each toplevel at most once if we're called
   * from process_all_updates.
   */
  if (in_process_all_updates)
383
    {
384
385
386
      GdkWindow *toplevel;

      toplevel = gdk_window_get_toplevel (window);
387
      if (toplevel)
388
        {
389
390
          GdkWindowObject *toplevel_private;
          GdkWindowImplQuartz *toplevel_impl;
391
392
          NSWindow *nswindow;

393
394
395
          toplevel_private = (GdkWindowObject *)toplevel;
          toplevel_impl = (GdkWindowImplQuartz *)toplevel_private->impl;
          nswindow = toplevel_impl->toplevel;
396

397
398
399
          /* In theory, we could skip the flush disabling, since we only
           * have one NSView.
           */
400
401
          if (nswindow && ![nswindow isFlushWindowDisabled]) 
            {
402
              [nswindow retain];
403
              [nswindow disableFlushWindow];
404
              update_nswindows = g_slist_prepend (update_nswindows, nswindow);
405
            }
406
        }
407
408
    }

409
  gdk_region_get_rectangles (region, &rects, &n_rects);
410

411
  for (i = 0; i < n_rects; i++)
412
    _gdk_quartz_window_set_needs_display_in_rect (window, &rects[i]);
413

414
415
  g_free (rects);

416
417
418
419
  /* NOTE: I'm not sure if we should displayIfNeeded here. It slows down a
   * lot (since it triggers the beam syncing) and things seem to work
   * without it.
   */
420
421
}

422
423
void
_gdk_windowing_before_process_all_updates (void)
424
{
425
  in_process_all_updates = TRUE;
426
427

  NSDisableScreenUpdates ();
428
429
}

430
431
void
_gdk_windowing_after_process_all_updates (void)
432
{
433
434
  GSList *old_update_nswindows = update_nswindows;
  GSList *tmp_list = update_nswindows;
435

436
  update_nswindows = NULL;
437

438
  while (tmp_list)
439
    {
440
441
      NSWindow *nswindow = tmp_list->data;

442
443
      [[nswindow contentView] displayIfNeeded];

444
445
      _gdk_quartz_drawable_flush (NULL);

446
447
448
      [nswindow enableFlushWindow];
      [nswindow flushWindow];
      [nswindow release];
449

450
      tmp_list = tmp_list->next;
451
    }
452

453
  g_slist_free (old_update_nswindows);
454

455
  in_process_all_updates = FALSE;
456
457

  NSEnableScreenUpdates ();
458
459
460
461
462
463
464
465
466
}

static void
gdk_window_impl_quartz_paintable_init (GdkPaintableIface *iface)
{
  iface->begin_paint_region = gdk_window_impl_quartz_begin_paint_region;
  iface->end_paint = gdk_window_impl_quartz_end_paint;
}

Anders Carlsson's avatar
Anders Carlsson committed
467
468
469
470
471
472
473
GType
_gdk_window_impl_quartz_get_type (void)
{
  static GType object_type = 0;

  if (!object_type)
    {
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
      const GTypeInfo object_info =
	{
	  sizeof (GdkWindowImplQuartzClass),
	  (GBaseInitFunc) NULL,
	  (GBaseFinalizeFunc) NULL,
	  (GClassInitFunc) gdk_window_impl_quartz_class_init,
	  NULL,           /* class_finalize */
	  NULL,           /* class_data */
	  sizeof (GdkWindowImplQuartz),
	  0,              /* n_preallocs */
	  (GInstanceInitFunc) gdk_window_impl_quartz_init,
	};

      const GInterfaceInfo paintable_info = 
	{
	  (GInterfaceInitFunc) gdk_window_impl_quartz_paintable_init,
	  NULL,
	  NULL
	};
493

494
495
496
497
498
499
500
      const GInterfaceInfo window_impl_info = 
	{
	  (GInterfaceInitFunc) gdk_window_impl_iface_init,
	  NULL,
	  NULL
	};

Anders Carlsson's avatar
Anders Carlsson committed
501
502
503
      object_type = g_type_register_static (GDK_TYPE_DRAWABLE_IMPL_QUARTZ,
                                            "GdkWindowImplQuartz",
                                            &object_info, 0);
504
505
506
      g_type_add_interface_static (object_type,
				   GDK_TYPE_PAINTABLE,
				   &paintable_info);
507
508
509
      g_type_add_interface_static (object_type,
				   GDK_TYPE_WINDOW_IMPL,
				   &window_impl_info);
Anders Carlsson's avatar
Anders Carlsson committed
510
    }
511

Anders Carlsson's avatar
Anders Carlsson committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
  return object_type;
}

GType
_gdk_window_impl_get_type (void)
{
  return _gdk_window_impl_quartz_get_type ();
}

static const gchar *
get_default_title (void)
{
  const char *title;

  title = g_get_application_name ();
  if (!title)
    title = g_get_prgname ();

  return title;
}

533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
static void
get_ancestor_coordinates_from_child (GdkWindow *child_window,
				     gint       child_x,
				     gint       child_y,
				     GdkWindow *ancestor_window, 
				     gint      *ancestor_x, 
				     gint      *ancestor_y)
{
  GdkWindowObject *child_private = GDK_WINDOW_OBJECT (child_window);
  GdkWindowObject *ancestor_private = GDK_WINDOW_OBJECT (ancestor_window);

  while (child_private != ancestor_private)
    {
      child_x += child_private->x;
      child_y += child_private->y;

      child_private = child_private->parent;
    }

  *ancestor_x = child_x;
  *ancestor_y = child_y;
}

void
557
_gdk_quartz_window_debug_highlight (GdkWindow *window, gint number)
558
559
560
{
  GdkWindowObject *private = GDK_WINDOW_OBJECT (window);
  gint x, y;
561
  gint gx, gy;
562
563
  GdkWindow *toplevel;
  gint tx, ty;
564
565
  static NSWindow *debug_window[10];
  static NSRect old_rect[10];
566
  NSRect rect;
567
568
569
  NSColor *color;

  g_return_if_fail (number >= 0 && number <= 9);
570
571
572
573
574

  if (window == _gdk_root)
    return;

  if (window == NULL)
575
576
577
578
579
580
581
    {
      if (debug_window[number])
        [debug_window[number] close];
      debug_window[number] = NULL;

      return;
    }
582
583
584
585
586
587
588
589

  toplevel = gdk_window_get_toplevel (window);
  get_ancestor_coordinates_from_child (window, 0, 0, toplevel, &x, &y);

  gdk_window_get_origin (toplevel, &tx, &ty);
  x += tx;
  y += ty;

590
591
592
593
  _gdk_quartz_window_gdk_xy_to_xy (x, y + private->height,
                                   &gx, &gy);

  rect = NSMakeRect (gx, gy, private->width, private->height);
594

595
596
597
598
  if (debug_window[number] && NSEqualRects (rect, old_rect[number]))
    return;

  old_rect[number] = rect;
599

600
601
  if (debug_window[number])
    [debug_window[number] close];
602

603
604
605
606
  debug_window[number] = [[NSWindow alloc] initWithContentRect:rect
                                                     styleMask:NSBorderlessWindowMask
			                               backing:NSBackingStoreBuffered
			                                 defer:NO];
607

608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
  switch (number)
    {
    case 0:
      color = [NSColor redColor];
      break;
    case 1:
      color = [NSColor blueColor];
      break;
    case 2:
      color = [NSColor greenColor];
      break;
    case 3:
      color = [NSColor yellowColor];
      break;
    case 4:
      color = [NSColor brownColor];
      break;
    case 5:
      color = [NSColor purpleColor];
      break;
    default:
      color = [NSColor blackColor];
      break;
    }
632

633
634
635
636
637
638
  [debug_window[number] setBackgroundColor:color];
  [debug_window[number] setAlphaValue:0.4];
  [debug_window[number] setOpaque:NO];
  [debug_window[number] setReleasedWhenClosed:YES];
  [debug_window[number] setIgnoresMouseEvents:YES];
  [debug_window[number] setLevel:NSFloatingWindowLevel];
639

640
  [debug_window[number] orderFront:nil];
641
642
}

643
644
645
646
647
648
649
650
651
652
653
654
gboolean
_gdk_quartz_window_is_ancestor (GdkWindow *ancestor,
                                GdkWindow *window)
{
  if (ancestor == NULL || window == NULL)
    return FALSE;

  return (gdk_window_get_parent (window) == ancestor ||
          _gdk_quartz_window_is_ancestor (ancestor, 
                                          gdk_window_get_parent (window)));
}

655

656
/* See notes on top of gdkscreen-quartz.c */
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
void
_gdk_quartz_window_gdk_xy_to_xy (gint  gdk_x,
                                 gint  gdk_y,
                                 gint *ns_x,
                                 gint *ns_y)
{
  GdkScreenQuartz *screen_quartz = GDK_SCREEN_QUARTZ (_gdk_screen);

  if (ns_y)
    *ns_y = screen_quartz->height - gdk_y + screen_quartz->min_y;

  if (ns_x)
    *ns_x = gdk_x + screen_quartz->min_x;
}

void
_gdk_quartz_window_xy_to_gdk_xy (gint  ns_x,
                                 gint  ns_y,
                                 gint *gdk_x,
                                 gint *gdk_y)
Anders Carlsson's avatar
Anders Carlsson committed
677
{
678
  GdkScreenQuartz *screen_quartz = GDK_SCREEN_QUARTZ (_gdk_screen);
Anders Carlsson's avatar
Anders Carlsson committed
679

680
681
  if (gdk_y)
    *gdk_y = screen_quartz->height - ns_y + screen_quartz->min_y;
682

683
684
685
  if (gdk_x)
    *gdk_x = ns_x - screen_quartz->min_x;
}
686

687
688
689
690
691
692
693
void
_gdk_quartz_window_nspoint_to_gdk_xy (NSPoint  point,
                                      gint    *x,
                                      gint    *y)
{
  _gdk_quartz_window_xy_to_gdk_xy (point.x, point.y,
                                   x, y);
Anders Carlsson's avatar
Anders Carlsson committed
694
695
}

696
697
698
699
700
701
702
static GdkWindow *
find_child_window_helper (GdkWindow *window,
			  gint       x,
			  gint       y,
			  gint       x_offset,
			  gint       y_offset)
{
703
  GdkWindowImplQuartz *impl;
704
705
  GList *l;

706
707
708
709
710
711
  impl = GDK_WINDOW_IMPL_QUARTZ (GDK_WINDOW_OBJECT (window)->impl);

  if (window == _gdk_root)
    update_toplevel_order ();

  for (l = impl->sorted_children; l; l = l->next)
712
    {
713
714
      GdkWindowObject *child_private = l->data;
      GdkWindowImplQuartz *child_impl = GDK_WINDOW_IMPL_QUARTZ (child_private->impl);
715
716
      int temp_x, temp_y;

717
      if (!GDK_WINDOW_IS_MAPPED (child_private))
718
719
	continue;

720
721
722
723
724
725
726
727
728
729
730
731
732
      temp_x = x_offset + child_private->x;
      temp_y = y_offset + child_private->y;

      /* Special-case the root window. We have to include the title
       * bar in the checks, otherwise the window below the title bar
       * will be found i.e. events punch through. (If we can find a
       * better way to deal with the events in gdkevents-quartz, this
       * might not be needed.)
       */
      if (window == _gdk_root)
        {
          NSRect frame = NSMakeRect (0, 0, 100, 100);
          NSRect content;
733
          NSUInteger mask;
734
735
736
737
738
739
740
741
742
743
744
          int titlebar_height;

          mask = [child_impl->toplevel styleMask];

          /* Get the title bar height. */
          content = [NSWindow contentRectForFrameRect:frame
                                            styleMask:mask];
          titlebar_height = frame.size.height - content.size.height;

          if (titlebar_height > 0 &&
              x >= temp_x && y >= temp_y - titlebar_height &&
745
              x < temp_x + child_private->width && y < temp_y)
746
747
748
749
750
751
752
753
            {
              /* The root means "unknown" i.e. a window not managed by
               * GDK.
               */
              return (GdkWindow *)_gdk_root;
            }
        }

754
      if (x >= temp_x && y >= temp_y &&
755
	  x < temp_x + child_private->width && y < temp_y + child_private->height)
756
757
758
759
760
761
762
763
764
765
766
767
	{
	  /* Look for child windows. */
	  return find_child_window_helper (l->data,
					   x, y,
					   temp_x, temp_y);
	}
    }
  
  return window;
}

/* Given a GdkWindow and coordinates relative to it, returns the
768
769
 * innermost subwindow that contains the point. If the coordinates are
 * outside the passed in window, NULL is returned.
770
771
772
773
774
775
776
777
 */
GdkWindow *
_gdk_quartz_window_find_child (GdkWindow *window,
			       gint       x,
			       gint       y)
{
  GdkWindowObject *private = GDK_WINDOW_OBJECT (window);

778
  if (x >= 0 && y >= 0 && x < private->width && y < private->height)
779
780
781
782
783
    return find_child_window_helper (window, x, y, 0, 0);

  return NULL;
}

784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808

static void
generate_motion_event (GdkWindow *window)
{
  NSPoint point;
  NSPoint screen_point;
  NSWindow *nswindow;
  GdkQuartzView *view;
  GdkWindowObject *private;
  GdkEvent *event;
  gint x, y, x_root, y_root;
  gdouble xx, yy;
  GList *node;
  GdkWindow *pointer_window;

  event = gdk_event_new (GDK_MOTION_NOTIFY);
  event->any.window = NULL;
  event->any.send_event = TRUE;

  private = (GdkWindowObject *)window;
  nswindow = ((GdkWindowImplQuartz *)private->impl)->toplevel;
  view = (GdkQuartzView *)[nswindow contentView];

  screen_point = [NSEvent mouseLocation];

809
  _gdk_quartz_window_nspoint_to_gdk_xy (screen_point, &x_root, &y_root);
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837

  point = [nswindow convertScreenToBase:screen_point];

  x = point.x;
  y = private->height - point.y;

  pointer_window = _gdk_window_find_descendant_at (window, x, y,
                                                   &xx, &yy);

  event->any.type = GDK_MOTION_NOTIFY;
  event->motion.window = window;
  event->motion.time = GDK_CURRENT_TIME;
  event->motion.x = x;
  event->motion.y = y;
  event->motion.x_root = x_root;
  event->motion.y_root = y_root;
  /* FIXME event->axes */
  event->motion.state = 0;
  event->motion.is_hint = FALSE;
  event->motion.device = _gdk_display->core_pointer;

  if (event->any.window)
    g_object_ref (event->any.window);

  node = _gdk_event_queue_append (gdk_display_get_default (), event);
  _gdk_windowing_got_event (gdk_display_get_default (), node, event, 0);
}

838
839
840
841
842
843
void
_gdk_quartz_window_did_become_main (GdkWindow *window)
{
  main_window_stack = g_slist_remove (main_window_stack, window);

  if (GDK_WINDOW_OBJECT (window)->window_type != GDK_WINDOW_TEMP)
844
845
846
847
848
849
850
851
852
    {
      main_window_stack = g_slist_prepend (main_window_stack, window);

      /* We just became the active window, send a motion-notify
       * event so things like highlights get set up correctly.
       * This motion-notify is sent to the key window.
       */
      generate_motion_event (window);
    }
853
854

  clear_toplevel_order ();
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
}

void
_gdk_quartz_window_did_resign_main (GdkWindow *window)
{
  GdkWindow *new_window = NULL;

  if (main_window_stack)
    new_window = main_window_stack->data;
  else
    {
      GList *toplevels;

      toplevels = gdk_window_get_toplevels ();
      if (toplevels)
        new_window = toplevels->data;
      g_list_free (toplevels);
    }

  if (new_window &&
      new_window != window &&
      GDK_WINDOW_IS_MAPPED (new_window) &&
      GDK_WINDOW_OBJECT (new_window)->window_type != GDK_WINDOW_TEMP)
    {
      GdkWindowObject *private = (GdkWindowObject *) new_window;
      GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);

      [impl->toplevel makeKeyAndOrderFront:impl->toplevel];
    }
884
885

  clear_toplevel_order ();
886
887
}

888
static NSScreen *
Kristian Rietveld's avatar
Kristian Rietveld committed
889
get_nsscreen_for_point (gint x, gint y)
890
891
892
893
894
895
896
897
898
899
900
901
{
  int i;
  NSArray *screens;

  GDK_QUARTZ_ALLOC_POOL;

  screens = [NSScreen screens];

  for (i = 0; i < [screens count]; i++)
    {
      NSRect rect = [[screens objectAtIndex:i] frame];

Kristian Rietveld's avatar
Kristian Rietveld committed
902
903
      if (x >= rect.origin.x && x <= rect.origin.x + rect.size.width &&
          y >= rect.origin.y && y <= rect.origin.y + rect.size.height)
904
905
906
907
908
909
910
911
        return [screens objectAtIndex:i];
    }

  GDK_QUARTZ_RELEASE_POOL;

  return NULL;
}

912
913
914
915
916
917
918
919
void
_gdk_window_impl_new (GdkWindow     *window,
		      GdkWindow     *real_parent,
		      GdkScreen     *screen,
		      GdkVisual     *visual,
		      GdkEventMask   event_mask,
		      GdkWindowAttr *attributes,
		      gint           attributes_mask)
Anders Carlsson's avatar
Anders Carlsson committed
920
921
922
923
{
  GdkWindowObject *private;
  GdkWindowImplQuartz *impl;
  GdkDrawableImplQuartz *draw_impl;
924
  GdkWindowImplQuartz *parent_impl;
Anders Carlsson's avatar
Anders Carlsson committed
925

926
  GDK_QUARTZ_ALLOC_POOL;
Anders Carlsson's avatar
Anders Carlsson committed
927
928

  private = (GdkWindowObject *)window;
929

930
931
932
  impl = g_object_new (_gdk_window_impl_get_type (), NULL);
  private->impl = (GdkDrawable *)impl;
  draw_impl = GDK_DRAWABLE_IMPL_QUARTZ (impl);
Anders Carlsson's avatar
Anders Carlsson committed
933
934
  draw_impl->wrapper = GDK_DRAWABLE (window);

935
  parent_impl = GDK_WINDOW_IMPL_QUARTZ (private->parent->impl);
Anders Carlsson's avatar
Anders Carlsson committed
936
937
938
939
940
941

  switch (private->window_type)
    {
    case GDK_WINDOW_TOPLEVEL:
    case GDK_WINDOW_DIALOG:
    case GDK_WINDOW_TEMP:
942
      if (GDK_WINDOW_TYPE (private->parent) != GDK_WINDOW_ROOT)
Anders Carlsson's avatar
Anders Carlsson committed
943
	{
944
945
	  /* The common code warns for this case */
          parent_impl = GDK_WINDOW_IMPL_QUARTZ (GDK_WINDOW_OBJECT (_gdk_root)->impl);
Anders Carlsson's avatar
Anders Carlsson committed
946
947
948
	}
    }

949
  if (!private->input_only)
Anders Carlsson's avatar
Anders Carlsson committed
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
    {
      if (attributes_mask & GDK_WA_COLORMAP)
	{
	  draw_impl->colormap = attributes->colormap;
	  g_object_ref (attributes->colormap);
	}
      else
	{
	  if (visual == gdk_screen_get_system_visual (_gdk_screen))
	    {
	      draw_impl->colormap = gdk_screen_get_system_colormap (_gdk_screen);
	      g_object_ref (draw_impl->colormap);
	    }
	  else if (visual == gdk_screen_get_rgba_visual (_gdk_screen))
	    {
	      draw_impl->colormap = gdk_screen_get_rgba_colormap (_gdk_screen);
	      g_object_ref (draw_impl->colormap);
	    }
	  else
	    {
	      draw_impl->colormap = gdk_colormap_new (visual, FALSE);
	    }
	}
    }
  else
    {
      draw_impl->colormap = gdk_screen_get_system_colormap (_gdk_screen);
Anders Carlsson's avatar
Anders Carlsson committed
977
      g_object_ref (draw_impl->colormap);
Anders Carlsson's avatar
Anders Carlsson committed
978
979
    }

980
  /* Maintain the z-ordered list of children. */
981
  if (private->parent != (GdkWindowObject *)_gdk_root)
982
983
984
    parent_impl->sorted_children = g_list_prepend (parent_impl->sorted_children, window);
  else
    clear_toplevel_order ();
Anders Carlsson's avatar
Anders Carlsson committed
985
986
987
988
989
990
991
992
993
994
995

  gdk_window_set_cursor (window, ((attributes_mask & GDK_WA_CURSOR) ?
				  (attributes->cursor) :
				  NULL));

  switch (attributes->window_type) 
    {
    case GDK_WINDOW_TOPLEVEL:
    case GDK_WINDOW_DIALOG:
    case GDK_WINDOW_TEMP:
      {
996
997
        NSScreen *screen;
        NSRect screen_rect;
998
        NSRect content_rect;
999
        NSUInteger style_mask;
1000
        int nx, ny;
1001
1002
        const char *title;

1003
1004
1005
1006
1007
1008
1009
        /* initWithContentRect will place on the mainScreen by default.
         * We want to select the screen to place on ourselves.  We need
         * to find the screen the window will be on and correct the
         * content_rect coordinates to be relative to that screen.
         */
        _gdk_quartz_window_gdk_xy_to_xy (private->x, private->y, &nx, &ny);

Kristian Rietveld's avatar
Kristian Rietveld committed
1010
        screen = get_nsscreen_for_point (nx, ny);
1011
1012
1013
1014
1015
        screen_rect = [screen frame];
        nx -= screen_rect.origin.x;
        ny -= screen_rect.origin.y;

        content_rect = NSMakeRect (nx, ny - private->height,
1016
1017
                                   private->width,
                                   private->height);
Anders Carlsson's avatar
Anders Carlsson committed
1018

1019
1020
        if (attributes->window_type == GDK_WINDOW_TEMP ||
            attributes->type_hint == GDK_WINDOW_TYPE_HINT_SPLASHSCREEN)
1021
1022
          {
            style_mask = NSBorderlessWindowMask;
1023
1024
1025
          }
        else
          {
1026
1027
1028
1029
1030
            style_mask = (NSTitledWindowMask |
                          NSClosableWindowMask |
                          NSMiniaturizableWindowMask |
                          NSResizableWindowMask);
          }
Anders Carlsson's avatar
Anders Carlsson committed
1031
1032

	impl->toplevel = [[GdkQuartzWindow alloc] initWithContentRect:content_rect 
Anders Carlsson's avatar
Anders Carlsson committed
1033
			                                    styleMask:style_mask
Michael Natterer's avatar
Michael Natterer committed
1034
			                                      backing:NSBackingStoreBuffered
1035
1036
			                                        defer:NO
                                                                screen:screen];
Michael Natterer's avatar
Michael Natterer committed
1037

Anders Carlsson's avatar
Anders Carlsson committed
1038
1039
1040
1041
1042
1043
	if (attributes_mask & GDK_WA_TITLE)
	  title = attributes->title;
	else
	  title = get_default_title ();

	gdk_window_set_title (window, title);
1044
  
Anders Carlsson's avatar
Anders Carlsson committed
1045
1046
1047
1048
1049
1050
	if (draw_impl->colormap == gdk_screen_get_rgba_colormap (_gdk_screen))
	  {
	    [impl->toplevel setOpaque:NO];
	    [impl->toplevel setBackgroundColor:[NSColor clearColor]];
	  }

1051
1052
1053
        content_rect.origin.x = 0;
        content_rect.origin.y = 0;

Anders Carlsson's avatar
Anders Carlsson committed
1054
1055
1056
1057
1058
	impl->view = [[GdkQuartzView alloc] initWithFrame:content_rect];
	[impl->view setGdkWindow:window];
	[impl->toplevel setContentView:impl->view];
      }
      break;
1059

Anders Carlsson's avatar
Anders Carlsson committed
1060
1061
    case GDK_WINDOW_CHILD:
      {
1062
	GdkWindowImplQuartz *parent_impl = GDK_WINDOW_IMPL_QUARTZ (GDK_WINDOW_OBJECT (private->parent)->impl);
Anders Carlsson's avatar
Anders Carlsson committed
1063

1064
	if (!private->input_only)
Anders Carlsson's avatar
Anders Carlsson committed
1065
	  {
1066
1067
1068
1069
	    NSRect frame_rect = NSMakeRect (private->x + private->parent->abs_x,
                                            private->y + private->parent->abs_y,
                                            private->width,
                                            private->height);
Anders Carlsson's avatar
Anders Carlsson committed
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
	
	    impl->view = [[GdkQuartzView alloc] initWithFrame:frame_rect];
	    
	    [impl->view setGdkWindow:window];

	    /* GdkWindows should be hidden by default */
	    [impl->view setHidden:YES];
	    [parent_impl->view addSubview:impl->view];
	  }
      }
      break;
1081

Anders Carlsson's avatar
Anders Carlsson committed
1082
1083
1084
1085
1086
1087
    default:
      g_assert_not_reached ();
    }

  GDK_QUARTZ_RELEASE_POOL;

Cody Russell's avatar
Cody Russell committed
1088
1089
  if (attributes_mask & GDK_WA_TYPE_HINT)
    gdk_window_set_type_hint (window, attributes->type_hint);
Anders Carlsson's avatar
Anders Carlsson committed
1090
1091
}

1092
void
1093
_gdk_quartz_window_update_position (GdkWindow *window)
1094
{
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
  NSRect frame_rect;
  NSRect content_rect;
  GdkWindowObject *private = (GdkWindowObject *)window;
  GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);

  GDK_QUARTZ_ALLOC_POOL;

  frame_rect = [impl->toplevel frame];
  content_rect = [impl->toplevel contentRectForFrameRect:frame_rect];

  _gdk_quartz_window_xy_to_gdk_xy (content_rect.origin.x,
                                   content_rect.origin.y + content_rect.size.height,
                                   &private->x, &private->y);


  GDK_QUARTZ_RELEASE_POOL;
}

void
_gdk_windowing_update_window_sizes (GdkScreen *screen)
{
  GList *windows, *list;
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
  GdkWindowObject *private = (GdkWindowObject *)_gdk_root;

  /* The size of the root window is so that it can contain all
   * monitors attached to this machine.  The monitors are laid out
   * within this root window.  We calculate the size of the root window
   * and the positions of the different monitors in gdkscreen-quartz.c.
   *
   * This data is updated when the monitor configuration is changed.
   */
  private->x = 0;
  private->y = 0;
  private->abs_x = 0;
  private->abs_y = 0;
  private->width = gdk_screen_get_width (screen);
  private->height = gdk_screen_get_height (screen);
1132
1133
1134
1135
1136
1137
1138

  windows = gdk_screen_get_toplevel_windows (screen);

  for (list = windows; list; list = list->next)
    _gdk_quartz_window_update_position (list->data);

  g_list_free (windows);
1139
1140
}

Anders Carlsson's avatar
Anders Carlsson committed
1141
1142
1143
1144
void
_gdk_windowing_window_init (void)
{
  GdkWindowObject *private;
Richard Hult's avatar
Richard Hult committed
1145
  GdkWindowImplQuartz *impl;
1146
  GdkDrawableImplQuartz *drawable_impl;
Anders Carlsson's avatar
Anders Carlsson committed
1147
1148
1149
1150

  g_assert (_gdk_root == NULL);

  _gdk_root = g_object_new (GDK_TYPE_WINDOW, NULL);
Anders Carlsson's avatar
Anders Carlsson committed
1151

1152
1153
  private = (GdkWindowObject *)_gdk_root;
  private->impl = g_object_new (_gdk_window_impl_get_type (), NULL);
1154
  private->impl_window = private;
1155

Richard Hult's avatar
Richard Hult committed
1156
  impl = GDK_WINDOW_IMPL_QUARTZ (GDK_WINDOW_OBJECT (_gdk_root)->impl);
1157

1158
  _gdk_windowing_update_window_sizes (_gdk_screen);
Richard Hult's avatar
Richard Hult committed
1159

Anders Carlsson's avatar
Anders Carlsson committed
1160
  private->state = 0; /* We don't want GDK_WINDOW_STATE_WITHDRAWN here */
Anders Carlsson's avatar
Anders Carlsson committed
1161
1162
  private->window_type = GDK_WINDOW_ROOT;
  private->depth = 24;
1163
  private->viewable = TRUE;
1164
1165
1166
1167
1168
1169

  drawable_impl = GDK_DRAWABLE_IMPL_QUARTZ (private->impl);
  
  drawable_impl->wrapper = GDK_DRAWABLE (private);
  drawable_impl->colormap = gdk_screen_get_system_colormap (_gdk_screen);
  g_object_ref (drawable_impl->colormap);
Anders Carlsson's avatar
Anders Carlsson committed
1170
1171
}

1172
1173
1174
1175
static void
_gdk_quartz_window_destroy (GdkWindow *window,
                            gboolean   recursing,
                            gboolean   foreign_destroy)
Anders Carlsson's avatar
Anders Carlsson committed
1176
{
1177
1178
1179
1180
1181
1182
1183
  GdkWindowObject *private;
  GdkWindowImplQuartz *impl;
  GdkWindowObject *parent;

  private = GDK_WINDOW_OBJECT (window);
  impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);

1184
  main_window_stack = g_slist_remove (main_window_stack, window);
1185

1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
  g_list_free (impl->sorted_children);
  impl->sorted_children = NULL;

  parent = private->parent;
  if (parent)
    {
      GdkWindowImplQuartz *parent_impl = GDK_WINDOW_IMPL_QUARTZ (parent->impl);

      parent_impl->sorted_children = g_list_remove (parent_impl->sorted_children, window);
    }

1197
  _gdk_quartz_drawable_finish (GDK_DRAWABLE (impl));
Anders Carlsson's avatar
Anders Carlsson committed
1198

1199
1200
  if (!recursing && !foreign_destroy)
    {
1201
1202
      GDK_QUARTZ_ALLOC_POOL;

Anders Carlsson's avatar
Anders Carlsson committed
1203
1204
1205
      if (impl->toplevel)
	[impl->toplevel close];
      else if (impl->view)
1206
	[impl->view removeFromSuperview];
1207
1208

      GDK_QUARTZ_RELEASE_POOL;
Anders Carlsson's avatar
Anders Carlsson committed
1209
1210
1211
1212
1213
1214
    }
}

void
_gdk_windowing_window_destroy_foreign (GdkWindow *window)
{
1215
  /* Foreign windows aren't supported in OSX. */
Anders Carlsson's avatar
Anders Carlsson committed
1216
1217
}

1218
1219
1220
/* FIXME: This might be possible to simplify with client-side windows. Also
 * note that already_mapped is not used yet, see the x11 backend.
*/
Anders Carlsson's avatar
Anders Carlsson committed
1221
static void
1222
gdk_window_quartz_show (GdkWindow *window, gboolean already_mapped)
Anders Carlsson's avatar
Anders Carlsson committed
1223
{
1224
1225
  GdkWindowObject *private = (GdkWindowObject *)window;
  GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);
1226
  gboolean focus_on_map;
Anders Carlsson's avatar
Anders Carlsson committed
1227
1228
1229

  GDK_QUARTZ_ALLOC_POOL;

1230
1231
1232
1233
1234
  if (!GDK_WINDOW_IS_MAPPED (window))
    focus_on_map = private->focus_on_map;
  else
    focus_on_map = TRUE;

Anders Carlsson's avatar
Anders Carlsson committed
1235
1236
  if (impl->toplevel)
    {
1237
1238
      gboolean make_key;

1239
      make_key = (private->accept_focus && focus_on_map &&
1240
                  private->window_type != GDK_WINDOW_TEMP);
1241

1242
      [(GdkQuartzWindow*)impl->toplevel showAndMakeKey:make_key];
1243
      clear_toplevel_order ();
1244

1245
      _gdk_quartz_events_send_map_event (window);
Anders Carlsson's avatar
Anders Carlsson committed
1246
1247
1248
1249
1250
1251
    }
  else
    {
      [impl->view setHidden:NO];
    }

1252
1253
  [impl->view setNeedsDisplay:YES];

Anders Carlsson's avatar
Anders Carlsson committed
1254
1255
  gdk_synthesize_window_state (window, GDK_WINDOW_STATE_WITHDRAWN, 0);

1256
1257
1258
1259
1260
1261
  if (private->state & GDK_WINDOW_STATE_MAXIMIZED)
    gdk_window_maximize (window);

  if (private->state & GDK_WINDOW_STATE_ICONIFIED)
    gdk_window_iconify (window);

1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
  if (impl->transient_for && !GDK_WINDOW_DESTROYED (impl->transient_for))
    _gdk_quartz_window_attach_to_parent (window);

  GDK_QUARTZ_RELEASE_POOL;
}

/* Temporarily unsets the parent window, if the window is a
 * transient. 
 */
void
_gdk_quartz_window_detach_from_parent (GdkWindow *window)
{
  GdkWindowImplQuartz *impl;

  g_return_if_fail (GDK_IS_WINDOW (window));

  impl = GDK_WINDOW_IMPL_QUARTZ (GDK_WINDOW_OBJECT (window)->impl);
  
  g_return_if_fail (impl->toplevel != NULL);

1282
1283
1284
1285
1286
  if (impl->transient_for && !GDK_WINDOW_DESTROYED (impl->transient_for))
    {
      GdkWindowImplQuartz *parent_impl;

      parent_impl = GDK_WINDOW_IMPL_QUARTZ (GDK_WINDOW_OBJECT (impl->transient_for)->impl);
1287
      [parent_impl->toplevel removeChildWindow:impl->toplevel];
1288
      clear_toplevel_order ();
1289
    }
1290
}
1291

1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
/* Re-sets the parent window, if the window is a transient. */
void
_gdk_quartz_window_attach_to_parent (GdkWindow *window)
{
  GdkWindowImplQuartz *impl;

  g_return_if_fail (GDK_IS_WINDOW (window));

  impl = GDK_WINDOW_IMPL_QUARTZ (GDK_WINDOW_OBJECT (window)->impl);
  
  g_return_if_fail (impl->toplevel != NULL);

  if (impl->transient_for && !GDK_WINDOW_DESTROYED (impl->transient_for))
    {
      GdkWindowImplQuartz *parent_impl;

      parent_impl = GDK_WINDOW_IMPL_QUARTZ (GDK_WINDOW_OBJECT (impl->transient_for)->impl);
      [parent_impl->toplevel addChildWindow:impl->toplevel ordered:NSWindowAbove];
1310
      clear_toplevel_order ();
1311
    }
Anders Carlsson's avatar
Anders Carlsson committed
1312
1313
1314
}

void
1315
gdk_window_quartz_hide (GdkWindow *window)
Anders Carlsson's avatar
Anders Carlsson committed
1316
1317
1318
1319
{
  GdkWindowObject *private = (GdkWindowObject *)window;
  GdkWindowImplQuartz *impl;

1320
1321
  /* Make sure we're not stuck in fullscreen mode. */
  if (get_fullscreen_geometry (window))
1322
    SetSystemUIMode (kUIModeNormal, 0);
1323

1324
1325
<