gdkwindow-quartz.c 70.5 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 22
 *
 * 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.
 */

#include <config.h>
23
#include <Carbon/Carbon.h>
Anders Carlsson's avatar
Anders Carlsson committed
24 25 26 27 28 29

#include "gdk.h"
#include "gdkprivate-quartz.h"

static gpointer parent_class;

30 31 32 33
static GSList *update_windows;
static guint   update_idle;

static GSList *main_window_stack;
34

35 36 37
static void update_toplevel_order (void);
static void clear_toplevel_order (void);

Richard Hult's avatar
Richard Hult committed
38 39 40
#define WINDOW_IS_TOPLEVEL(window)		   \
  (GDK_WINDOW_TYPE (window) != GDK_WINDOW_CHILD && \
   GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
41

42 43 44 45 46 47 48 49
NSView *
gdk_quartz_window_get_nsview (GdkWindow *window)
{
  GdkWindowObject *private = (GdkWindowObject *)window;

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

Anders Carlsson's avatar
Anders Carlsson committed
50 51
static void
gdk_window_impl_quartz_get_size (GdkDrawable *drawable,
52 53
				 gint        *width,
				 gint        *height)
Anders Carlsson's avatar
Anders Carlsson committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
{
  g_return_if_fail (GDK_IS_WINDOW_IMPL_QUARTZ (drawable));

  if (width)
    *width = GDK_WINDOW_IMPL_QUARTZ (drawable)->width;
  if (height)
    *height = GDK_WINDOW_IMPL_QUARTZ (drawable)->height;
}

static GdkRegion*
gdk_window_impl_quartz_get_visible_region (GdkDrawable *drawable)
{
  GdkWindowObject *private = GDK_WINDOW_OBJECT (GDK_DRAWABLE_IMPL_QUARTZ (drawable)->wrapper);
  GdkRectangle rect;
  GdkWindowImplQuartz *impl;
  GList *windows = NULL, *l;

  /* FIXME: The clip rectangle should really be cached
   * and recalculated when the window rectangle changes.
   */
  while (private)
    {
      windows = g_list_prepend (windows, private);

      if (private->parent == GDK_WINDOW_OBJECT (_gdk_root))
	break;

      private = private->parent;
    }

  /* Get rectangle for toplevel window */
  private = GDK_WINDOW_OBJECT (windows->data);
  impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);
  
  rect.x = 0;
  rect.y = 0;
  rect.width = impl->width;
  rect.height = impl->height;

  /* Skip toplevel window since we have its rect */
  for (l = windows->next; l; l = l->next)
    {
      private = GDK_WINDOW_OBJECT (l->data);
      impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);
      GdkRectangle tmp_rect;

      tmp_rect.x = -MIN (0, private->x - rect.x);
      tmp_rect.y = -MIN (0, private->y - rect.y);
      tmp_rect.width = MIN (rect.width, impl->width + private->x - rect.x) - MAX (0, private->x - rect.x);
      tmp_rect.height = MIN (rect.height, impl->height + private->y - rect.y) - MAX (0, private->y - rect.y);

      rect = tmp_rect;
    }
  
  g_list_free (windows);
  
  return gdk_region_rectangle (&rect);
}

static void
gdk_window_impl_quartz_finalize (GObject *object)
{
  GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (object);

  if (impl->nscursor)
    [impl->nscursor release];

121 122 123
  if (impl->paint_clip_region)
    gdk_region_destroy (impl->paint_clip_region);

124 125 126
  if (impl->transient_for)
    g_object_unref (impl->transient_for);

Anders Carlsson's avatar
Anders Carlsson committed
127 128 129 130 131 132 133
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gdk_window_impl_quartz_class_init (GdkWindowImplQuartzClass *klass)
{
  GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
134
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
Anders Carlsson's avatar
Anders Carlsson committed
135 136 137

  parent_class = g_type_class_peek_parent (klass);

138 139
  object_class->finalize = gdk_window_impl_quartz_finalize;

Anders Carlsson's avatar
Anders Carlsson committed
140 141 142 143 144 145 146 147 148 149 150 151
  drawable_class->get_size = gdk_window_impl_quartz_get_size;

  /* Visible and clip regions are the same */
  drawable_class->get_clip_region = gdk_window_impl_quartz_get_visible_region;
  drawable_class->get_visible_region = gdk_window_impl_quartz_get_visible_region;
}

static void
gdk_window_impl_quartz_init (GdkWindowImplQuartz *impl)
{
  impl->width = 1;
  impl->height = 1;
152
  impl->type_hint = GDK_WINDOW_TYPE_HINT_NORMAL;
Anders Carlsson's avatar
Anders Carlsson committed
153 154
}

155 156 157 158 159
static void
gdk_window_impl_quartz_begin_paint_region (GdkPaintable *paintable,
					   GdkRegion    *region)
{
  GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (paintable);
160
  GdkDrawableImplQuartz *drawable_impl;
161
  int n_rects;
162
  GdkRectangle *rects;
163 164
  GdkPixmap *bg_pixmap;
  GdkWindow *window;
165 166 167

  drawable_impl = GDK_DRAWABLE_IMPL_QUARTZ (impl);
  bg_pixmap = GDK_WINDOW_OBJECT (drawable_impl->wrapper)->bg_pixmap;
168 169 170 171 172 173

  if (impl->begin_paint_count == 0)
    impl->paint_clip_region = gdk_region_copy (region);
  else
    gdk_region_union (impl->paint_clip_region, region);

174
  impl->begin_paint_count++;
175

176 177
  if (bg_pixmap == GDK_NO_BG)
    return;
178

179
  gdk_region_get_rectangles (region, &rects, &n_rects);
180

181 182
  if (bg_pixmap == NULL)
    {
183 184
      CGContextRef cg_context;
      gfloat r, g, b, a;
185
      gint i;
186 187

      cg_context = gdk_quartz_drawable_get_context (GDK_DRAWABLE (impl), FALSE);
188
      _gdk_quartz_colormap_get_rgba_from_pixel (gdk_drawable_get_colormap (drawable_impl->wrapper),
189 190 191
				      GDK_WINDOW_OBJECT (drawable_impl->wrapper)->bg_color.pixel,
				      &r, &g, &b, &a);
 
192 193
      for (i = 0; i < n_rects; i++) 
        {
194 195
	  CGContextSetRGBFillColor (cg_context, r, g, b, a);
          CGContextFillRect (cg_context,
196 197
			     CGRectMake (rects[i].x, rects[i].y,
					 rects[i].width, rects[i].height));
198
        }
199 200

      gdk_quartz_drawable_release_context (GDK_DRAWABLE (impl), cg_context);
201 202
    }
  else
203
    {
204 205 206 207
      int x, y;
      int x_offset, y_offset;
      int width, height;
      GdkGC *gc;
208

209
      x_offset = y_offset = 0;
210 211

      window = GDK_WINDOW (drawable_impl->wrapper);
212
      while (window && bg_pixmap == GDK_PARENT_RELATIVE_BG)
213 214 215 216 217 218 219 220
        {
          /* 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);
221 222 223 224 225 226 227 228 229 230
          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.
           */ 
          g_free (rects);
          return;
231 232 233 234 235 236
        }

      /* 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));
237

238
      gdk_drawable_get_size (GDK_DRAWABLE (bg_pixmap), &width, &height);
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
      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;
        }
256 257

      g_object_unref (gc);
258
    }
259

260 261 262 263 264 265 266 267
  g_free (rects);
}

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

268
  impl->begin_paint_count--;
269 270 271 272 273 274

  if (impl->begin_paint_count == 0)
    {
      gdk_region_destroy (impl->paint_clip_region);
      impl->paint_clip_region = NULL;
    }
275 276
}

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
static void
gdk_window_quartz_process_updates_internal (GdkWindow *window)
{
  GdkWindowObject *private = (GdkWindowObject *) window;
  GdkWindowImplQuartz *impl = (GdkWindowImplQuartz *) private->impl;
      
  if (private->update_area)
    {
      int i, n_rects;
      GdkRectangle *rects;

      gdk_region_get_rectangles (private->update_area, &rects, &n_rects);

      gdk_region_destroy (private->update_area);
      private->update_area = NULL;

      for (i = 0; i < n_rects; i++) 
	{
	  [impl->view setNeedsDisplayInRect:NSMakeRect (rects[i].x, rects[i].y,
							rects[i].width, rects[i].height)];
	}

      [impl->view displayIfNeeded];

      g_free (rects);
    }
}

305 306 307 308 309 310 311 312 313 314 315
static void
gdk_window_quartz_process_all_updates (void)
{
  GSList *old_update_windows = update_windows;
  GSList *tmp_list = update_windows;

  update_idle = 0;
  update_windows = NULL;

  g_slist_foreach (old_update_windows, (GFunc) g_object_ref, NULL);
  
316 317
  GDK_QUARTZ_ALLOC_POOL;

318 319
  while (tmp_list)
    {
320
      gdk_window_quartz_process_updates_internal (tmp_list->data);
321 322 323 324 325

      g_object_unref (tmp_list->data);
      tmp_list = tmp_list->next;
    }

326 327
  GDK_QUARTZ_RELEASE_POOL;

328 329 330 331
  g_slist_free (old_update_windows);
}

static gboolean
332
gdk_window_quartz_update_idle (gpointer data)
333 334 335 336 337 338 339 340
{
  GDK_THREADS_ENTER ();
  gdk_window_quartz_process_all_updates ();
  GDK_THREADS_LEAVE ();

  return FALSE;
}

341 342 343 344 345 346
static void
gdk_window_impl_quartz_invalidate_maybe_recurse (GdkPaintable *paintable,
						 GdkRegion    *region,
						 gboolean    (*child_func) (GdkWindow *, gpointer),
						 gpointer      user_data)
{
347 348 349 350 351
  GdkWindowImplQuartz *window_impl = GDK_WINDOW_IMPL_QUARTZ (paintable);
  GdkDrawableImplQuartz *drawable_impl = (GdkDrawableImplQuartz *) window_impl;
  GdkWindow *window = (GdkWindow *) drawable_impl->wrapper;
  GdkWindowObject *private = (GdkWindowObject *) window;
  GdkRegion *visible_region;
352

353 354
  visible_region = gdk_drawable_get_visible_region (window);
  gdk_region_intersect (visible_region, region);
355

356
  if (private->update_area)
357
    {
358 359
      gdk_region_union (private->update_area, visible_region);
      gdk_region_destroy (visible_region);
360
    }
361 362 363 364
  else
    {
      update_windows = g_slist_prepend (update_windows, window);
      private->update_area = visible_region;
365

366 367
      if (update_idle == 0)
	update_idle = g_idle_add_full (GDK_PRIORITY_REDRAW,
368
				       gdk_window_quartz_update_idle, NULL, NULL);
369
    }
370 371 372 373 374 375 376
}

static void
gdk_window_impl_quartz_process_updates (GdkPaintable *paintable,
					gboolean      update_children)
{
  GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (paintable);
377 378
  GdkDrawableImplQuartz *drawable_impl = (GdkDrawableImplQuartz *) impl;
  GdkWindowObject *private = (GdkWindowObject *) drawable_impl->wrapper;
379

380 381
  if (private->update_area)
    {
382
      GDK_QUARTZ_ALLOC_POOL;
383 384
      gdk_window_quartz_process_updates_internal ((GdkWindow *) private);
      update_windows = g_slist_remove (update_windows, private);
385
      GDK_QUARTZ_RELEASE_POOL;
386
    }
387 388 389 390 391 392 393 394 395 396 397 398
}

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;

  iface->invalidate_maybe_recurse = gdk_window_impl_quartz_invalidate_maybe_recurse;
  iface->process_updates = gdk_window_impl_quartz_process_updates;
}

Anders Carlsson's avatar
Anders Carlsson committed
399 400 401 402 403 404 405
GType
_gdk_window_impl_quartz_get_type (void)
{
  static GType object_type = 0;

  if (!object_type)
    {
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
      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
	};
425

Anders Carlsson's avatar
Anders Carlsson committed
426 427 428
      object_type = g_type_register_static (GDK_TYPE_DRAWABLE_IMPL_QUARTZ,
                                            "GdkWindowImplQuartz",
                                            &object_info, 0);
429 430 431
      g_type_add_interface_static (object_type,
				   GDK_TYPE_PAINTABLE,
				   &paintable_info);
Anders Carlsson's avatar
Anders Carlsson committed
432
    }
433

Anders Carlsson's avatar
Anders Carlsson committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
  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;
}

455 456 457 458 459 460 461 462 463 464 465 466
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)));
}

467 468 469
/* FIXME: It would be nice to have one function that takes an NSPoint
 * and flips the coords for any window.
 */
Anders Carlsson's avatar
Anders Carlsson committed
470
gint 
471
_gdk_quartz_window_get_inverted_screen_y (gint y)
Anders Carlsson's avatar
Anders Carlsson committed
472 473 474 475 476 477
{
  NSRect rect = [[NSScreen mainScreen] frame];

  return rect.size.height - y;
}

478 479 480 481 482 483 484
static GdkWindow *
find_child_window_helper (GdkWindow *window,
			  gint       x,
			  gint       y,
			  gint       x_offset,
			  gint       y_offset)
{
485
  GdkWindowImplQuartz *impl;
486 487
  GList *l;

488 489 490 491 492 493
  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)
494
    {
495 496
      GdkWindowObject *child_private = l->data;
      GdkWindowImplQuartz *child_impl = GDK_WINDOW_IMPL_QUARTZ (child_private->impl);
497 498
      int temp_x, temp_y;

499
      if (!GDK_WINDOW_IS_MAPPED (child_private))
500 501
	continue;

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
      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;
          int mask;
          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 &&
              x < temp_x + child_impl->width && y < temp_y)
            {
              /* The root means "unknown" i.e. a window not managed by
               * GDK.
               */
              return (GdkWindow *)_gdk_root;
            }
        }

536
      if (x >= temp_x && y >= temp_y &&
537
	  x < temp_x + child_impl->width && y < temp_y + child_impl->height)
538 539 540 541 542 543 544 545 546 547 548 549
	{
	  /* 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
550 551
 * innermost subwindow that contains the point. If the coordinates are
 * outside the passed in window, NULL is returned.
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
 */
GdkWindow *
_gdk_quartz_window_find_child (GdkWindow *window,
			       gint       x,
			       gint       y)
{
  GdkWindowObject *private = GDK_WINDOW_OBJECT (window);
  GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);

  if (x >= 0 && y >= 0 && x < impl->width && y < impl->height)
    return find_child_window_helper (window, x, y, 0, 0);

  return NULL;
}

567 568 569 570 571 572 573
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)
    main_window_stack = g_slist_prepend (main_window_stack, window);
574 575

  clear_toplevel_order ();
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
}

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];
    }
605 606

  clear_toplevel_order ();
607 608
}

Anders Carlsson's avatar
Anders Carlsson committed
609 610 611 612 613 614 615 616 617 618
GdkWindow *
gdk_window_new (GdkWindow     *parent,
		GdkWindowAttr *attributes,
		gint           attributes_mask)
{
  GdkWindow *window;
  GdkWindowObject *private;
  GdkWindowImplQuartz *impl;
  GdkDrawableImplQuartz *draw_impl;
  GdkVisual *visual;
619
  GdkWindowImplQuartz *parent_impl;
Anders Carlsson's avatar
Anders Carlsson committed
620 621

  if (parent && GDK_WINDOW_DESTROYED (parent))
622 623 624
    return NULL;

  GDK_QUARTZ_ALLOC_POOL;
Anders Carlsson's avatar
Anders Carlsson committed
625 626 627 628 629 630 631 632 633 634 635

  if (!parent)
    parent = _gdk_root;

  window = g_object_new (GDK_TYPE_WINDOW, NULL);
  private = (GdkWindowObject *)window;
  impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);
  draw_impl = GDK_DRAWABLE_IMPL_QUARTZ (private->impl);
  draw_impl->wrapper = GDK_DRAWABLE (window);

  private->parent = (GdkWindowObject *)parent;
636
  parent_impl = GDK_WINDOW_IMPL_QUARTZ (private->parent->impl);
Anders Carlsson's avatar
Anders Carlsson committed
637

638 639 640
  private->accept_focus = TRUE;
  private->focus_on_map = TRUE;

Anders Carlsson's avatar
Anders Carlsson committed
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
  if (attributes_mask & GDK_WA_X)
    private->x = attributes->x;
  else
    private->x = 0;
  
  if (attributes_mask & GDK_WA_Y)
    private->y = attributes->y;
  else if (attributes_mask & GDK_WA_X)
    private->y = 100;
  else
    private->y = 0;

  private->event_mask = attributes->event_mask;

  impl->width = attributes->width > 1 ? attributes->width : 1;
  impl->height = attributes->height > 1 ? attributes->height : 1;

  if (attributes_mask & GDK_WA_VISUAL)
    visual = attributes->visual;
  else
    visual = gdk_screen_get_system_visual (_gdk_screen);

  if (attributes->wclass == GDK_INPUT_ONLY)
    {
      /* Backwards compatiblity - we've always ignored
       * attributes->window_type for input-only windows
       * before
       */
      if (parent == _gdk_root)
	private->window_type = GDK_WINDOW_TEMP;
      else
	private->window_type = GDK_WINDOW_CHILD;
    }
  else
    private->window_type = attributes->window_type;

  /* Sanity checks */
  switch (private->window_type)
    {
    case GDK_WINDOW_TOPLEVEL:
    case GDK_WINDOW_DIALOG:
    case GDK_WINDOW_TEMP:
      if (GDK_WINDOW_TYPE (parent) != GDK_WINDOW_ROOT)
	{
	  g_warning (G_STRLOC "Toplevel windows must be created as children of\n"
		     "of a window of type GDK_WINDOW_ROOT or GDK_WINDOW_FOREIGN");
	}
    case GDK_WINDOW_CHILD:
      break;
    default:
      g_warning (G_STRLOC "cannot make windows of type %d", private->window_type);
692
      GDK_QUARTZ_RELEASE_POOL;
Anders Carlsson's avatar
Anders Carlsson committed
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
      return NULL;
    }

  if (attributes->wclass == GDK_INPUT_OUTPUT)
    {
      private->input_only = FALSE;
      private->depth = visual->depth;

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

      private->bg_color.pixel = 0;
      private->bg_color.red = private->bg_color.green = private->bg_color.blue = 0;
    }
  else
    {
      private->depth = 0;
      private->input_only = TRUE;
      draw_impl->colormap = gdk_screen_get_system_colormap (_gdk_screen);
Anders Carlsson's avatar
Anders Carlsson committed
732
      g_object_ref (draw_impl->colormap);
Anders Carlsson's avatar
Anders Carlsson committed
733 734
    }

735 736 737 738 739 740 741
  private->parent->children = g_list_prepend (private->parent->children, window);

  /* Maintain the z-ordered list of children. */
  if (parent != _gdk_root)
    parent_impl->sorted_children = g_list_prepend (parent_impl->sorted_children, window);
  else
    clear_toplevel_order ();
Anders Carlsson's avatar
Anders Carlsson committed
742 743 744 745 746 747 748 749 750 751 752

  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:
      {
753 754 755 756 757 758 759 760 761 762 763
        NSRect content_rect;
        int style_mask;
        const char *title;

        /* Big hack: We start out outside the screen and move the
         * window in before showing it. This makes the initial
         * MouseEntered event work if the window ends up right under
         * the mouse pointer, bad quartz.
         */
        content_rect = NSMakeRect (-500 - impl->width, -500 - impl->height,
                                   impl->width, impl->height);
Anders Carlsson's avatar
Anders Carlsson committed
764

765 766 767 768 769 770 771 772 773 774 775
	switch (attributes->window_type) 
          {
          case GDK_WINDOW_TEMP:
            style_mask = NSBorderlessWindowMask;
            break;
          default:
            style_mask = (NSTitledWindowMask |
                          NSClosableWindowMask |
                          NSMiniaturizableWindowMask |
                          NSResizableWindowMask);
          }
Anders Carlsson's avatar
Anders Carlsson committed
776 777

	impl->toplevel = [[GdkQuartzWindow alloc] initWithContentRect:content_rect 
Anders Carlsson's avatar
Anders Carlsson committed
778
			                                    styleMask:style_mask
Michael Natterer's avatar
Michael Natterer committed
779 780 781
			                                      backing:NSBackingStoreBuffered
			                                        defer:NO];

Anders Carlsson's avatar
Anders Carlsson committed
782 783 784 785 786 787
	if (attributes_mask & GDK_WA_TITLE)
	  title = attributes->title;
	else
	  title = get_default_title ();

	gdk_window_set_title (window, title);
788
  
Anders Carlsson's avatar
Anders Carlsson committed
789 790 791 792 793 794 795 796 797 798 799
	if (draw_impl->colormap == gdk_screen_get_rgba_colormap (_gdk_screen))
	  {
	    [impl->toplevel setOpaque:NO];
	    [impl->toplevel setBackgroundColor:[NSColor clearColor]];
	  }

	impl->view = [[GdkQuartzView alloc] initWithFrame:content_rect];
	[impl->view setGdkWindow:window];
	[impl->toplevel setContentView:impl->view];
      }
      break;
800

Anders Carlsson's avatar
Anders Carlsson committed
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
    case GDK_WINDOW_CHILD:
      {
	GdkWindowImplQuartz *parent_impl = GDK_WINDOW_IMPL_QUARTZ (GDK_WINDOW_OBJECT (parent)->impl);

	if (attributes->wclass == GDK_INPUT_OUTPUT)
	  {
	    NSRect frame_rect = NSMakeRect (private->x, private->y, impl->width, impl->height);
	
	    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;
819

Anders Carlsson's avatar
Anders Carlsson committed
820 821 822 823 824 825
    default:
      g_assert_not_reached ();
    }

  GDK_QUARTZ_RELEASE_POOL;

Cody Russell's avatar
Cody Russell committed
826 827 828
  if (attributes_mask & GDK_WA_TYPE_HINT)
    gdk_window_set_type_hint (window, attributes->type_hint);

Anders Carlsson's avatar
Anders Carlsson committed
829 830 831 832 833 834 835
  return window;
}

void
_gdk_windowing_window_init (void)
{
  GdkWindowObject *private;
Richard Hult's avatar
Richard Hult committed
836 837
  GdkWindowImplQuartz *impl;
  NSRect rect;
Anders Carlsson's avatar
Anders Carlsson committed
838 839 840 841

  g_assert (_gdk_root == NULL);

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

Richard Hult's avatar
Richard Hult committed
843 844 845 846 847 848
  /* Note: This needs to be reworked for multi-screen support. */
  impl = GDK_WINDOW_IMPL_QUARTZ (GDK_WINDOW_OBJECT (_gdk_root)->impl);
  rect = [[NSScreen mainScreen] frame];
  impl->width = rect.size.width;
  impl->height = rect.size.height;

Anders Carlsson's avatar
Anders Carlsson committed
849
  private = (GdkWindowObject *)_gdk_root;
Anders Carlsson's avatar
Anders Carlsson committed
850 851

  private->state = 0; /* We don't want GDK_WINDOW_STATE_WITHDRAWN here */
Anders Carlsson's avatar
Anders Carlsson committed
852 853 854 855 856 857 858 859 860
  private->window_type = GDK_WINDOW_ROOT;
  private->depth = 24;
}

void
_gdk_windowing_window_destroy (GdkWindow *window,
			       gboolean   recursing,
			       gboolean   foreign_destroy)
{
861 862 863 864 865 866 867
  GdkWindowObject *private;
  GdkWindowImplQuartz *impl;
  GdkWindowObject *parent;

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

868
  update_windows = g_slist_remove (update_windows, window);
869
  main_window_stack = g_slist_remove (main_window_stack, window);
870

871 872 873 874 875 876 877 878 879 880 881
  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);
    }

882 883 884 885 886 887 888 889 890
  /* If the destroyed window was targeted for a pointer or keyboard
   * grab, release the grab.
   */
  if (window == _gdk_quartz_pointer_grab_window)
    gdk_pointer_ungrab (0);

  if (window == _gdk_quartz_keyboard_grab_window)
    gdk_keyboard_ungrab (0);

891
  if (!recursing && !foreign_destroy)
Anders Carlsson's avatar
Anders Carlsson committed
892 893
    {
      GdkWindowImplQuartz *impl = GDK_WINDOW_IMPL_QUARTZ (GDK_WINDOW_OBJECT (window)->impl);
894
      GdkWindow *mouse_window;
Anders Carlsson's avatar
Anders Carlsson committed
895

896 897 898 899
      mouse_window = _gdk_quartz_events_get_mouse_window (FALSE);
      if (window == mouse_window ||
          _gdk_quartz_window_is_ancestor (window, mouse_window))
        _gdk_quartz_events_update_mouse_window (_gdk_root);
Anders Carlsson's avatar
Anders Carlsson committed
900

901 902
      GDK_QUARTZ_ALLOC_POOL;

903 904
      _gdk_quartz_drawable_finish (GDK_DRAWABLE (impl));

Anders Carlsson's avatar
Anders Carlsson committed
905 906 907 908
      if (impl->toplevel)
	[impl->toplevel close];
      else if (impl->view)
	[impl->view release];
909 910

      GDK_QUARTZ_RELEASE_POOL;
Anders Carlsson's avatar
Anders Carlsson committed
911 912 913 914 915 916
    }
}

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

Anders Carlsson's avatar
Anders Carlsson committed
920 921 922 923 924 925 926 927 928 929 930 931 932 933
static gboolean
all_parents_shown (GdkWindowObject *private)
{
  while (GDK_WINDOW_IS_MAPPED (private))
    {
      if (private->parent)
	private = (GdkWindowObject *)private->parent;
      else
	return TRUE;
    }

  return FALSE;
}

934 935 936
/* Note: the raise argument is not really used, it doesn't seem
 * possible to show a window without raising it?
 */
Anders Carlsson's avatar
Anders Carlsson committed
937
static void
938 939
show_window_internal (GdkWindow *window,
		      gboolean   raise)
Anders Carlsson's avatar
Anders Carlsson committed
940 941 942
{
  GdkWindowObject *private;
  GdkWindowImplQuartz *impl;
943
  gboolean focus_on_map;
Anders Carlsson's avatar
Anders Carlsson committed
944

945
  if (GDK_WINDOW_DESTROYED (window))
Anders Carlsson's avatar
Anders Carlsson committed
946 947 948 949
    return;

  GDK_QUARTZ_ALLOC_POOL;

950
  private = (GdkWindowObject *)window;
Anders Carlsson's avatar
Anders Carlsson committed
951 952
  impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);

953 954 955 956 957
  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
958 959
  if (impl->toplevel)
    {
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
      /* Move the window into place, to guarantee that we get the
       * initial MouseEntered event.
       */
      if (!GDK_WINDOW_IS_MAPPED (window))
        {
          NSRect content_rect;
          NSRect frame_rect;

          content_rect =
            NSMakeRect (private->x,
                        _gdk_quartz_window_get_inverted_screen_y (private->y) - impl->height,
                        impl->width, impl->height);
          frame_rect = [impl->toplevel frameRectForContentRect:content_rect];
          [impl->toplevel setFrame:frame_rect display:NO];
        }

976 977 978
      /* We should make the window not raise for !raise, but at least
       * this will keep it from getting focused in that case.
       */
979 980 981
      if (private->accept_focus && focus_on_map && raise &&
          private->window_type != GDK_WINDOW_TEMP)
        [impl->toplevel makeKeyAndOrderFront:impl->toplevel];
982 983
      else
        [impl->toplevel orderFront:nil];
Anders Carlsson's avatar
Anders Carlsson committed
984 985 986 987 988 989
    }
  else
    {
      [impl->view setHidden:NO];
    }

990 991
  [impl->view setNeedsDisplay:YES];

Anders Carlsson's avatar
Anders Carlsson committed
992
  if (all_parents_shown (private->parent))
993
    _gdk_quartz_events_send_map_events (window);
Anders Carlsson's avatar
Anders Carlsson committed
994

Anders Carlsson's avatar
Anders Carlsson committed
995 996
  gdk_synthesize_window_state (window, GDK_WINDOW_STATE_WITHDRAWN, 0);

997 998 999 1000 1001 1002
  if (private->state & GDK_WINDOW_STATE_MAXIMIZED)
    gdk_window_maximize (window);

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

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
  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);

1023 1024 1025 1026 1027
  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);
1028
      [parent_impl->toplevel removeChildWindow:impl->toplevel];
1029
    }
1030
}
1031

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
/* 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];
    }
Anders Carlsson's avatar
Anders Carlsson committed
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
}

void
gdk_window_show_unraised (GdkWindow *window)
{
  g_return_if_fail (GDK_IS_WINDOW (window));

  show_window_internal (window, FALSE);
}

void
gdk_window_show (GdkWindow *window)
{
  g_return_if_fail (GDK_IS_WINDOW (window));

  show_window_internal (window, TRUE);
}

void
gdk_window_hide (GdkWindow *window)
{
  GdkWindowObject *private = (GdkWindowObject *)window;
  GdkWindowImplQuartz *impl;
1074
  GdkWindow *mouse_window;
Anders Carlsson's avatar
Anders Carlsson committed
1075 1076 1077 1078 1079 1080

  g_return_if_fail (GDK_IS_WINDOW (window));

  if (GDK_WINDOW_DESTROYED (window))
    return;

1081 1082 1083 1084 1085
  mouse_window = _gdk_quartz_events_get_mouse_window (FALSE);
  if (window == mouse_window || 
      _gdk_quartz_window_is_ancestor (window, mouse_window))
    _gdk_quartz_events_update_mouse_window (_gdk_root);

Anders Carlsson's avatar
Anders Carlsson committed
1086 1087 1088 1089
  if (GDK_WINDOW_IS_MAPPED (window))
    gdk_synthesize_window_state (window,
				 0,
				 GDK_WINDOW_STATE_WITHDRAWN);
1090

Anders Carlsson's avatar
Anders Carlsson committed
1091 1092 1093 1094 1095 1096
  _gdk_window_clear_update_area (window);

  impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);

  if (impl->toplevel) 
    {
1097 1098 1099 1100
      NSRect content_rect;
      NSRect frame_rect;

     /* Update main window. */
1101 1102 1103 1104
      main_window_stack = g_slist_remove (main_window_stack, window);
      if ([NSApp mainWindow] == impl->toplevel)
        _gdk_quartz_window_did_resign_main (window);

1105
      if (impl->transient_for)
1106
        _gdk_quartz_window_detach_from_parent (window);
1107

1108 1109 1110 1111 1112 1113 1114 1115 1116
      /* Big hack in gdk_window_new() and show_window_internal()
       * continued. Move the window away when hidden so that we can
       * move it back before showing it.
       */
      content_rect = NSMakeRect (-500 - impl->width, -500 - impl->height,
                                 impl->width, impl->height);
      frame_rect = [impl->toplevel frameRectForContentRect:content_rect];
      [impl->toplevel setFrame:frame_rect display:NO];

Anders Carlsson's avatar
Anders Carlsson committed
1117
      [impl->toplevel orderOut:nil];
Anders Carlsson's avatar
Anders Carlsson committed
1118 1119 1120 1121 1122
    }
  else if (impl->view)
    {
      [impl->view setHidden:YES];
    }
Anders Carlsson's avatar
Anders Carlsson committed
1123

Anders Carlsson's avatar
Anders Carlsson committed
1124 1125
  if (window == _gdk_quartz_pointer_grab_window)
    gdk_pointer_ungrab (0);
Anders Carlsson's avatar
Anders Carlsson committed
1126

Anders Carlsson's avatar
Anders Carlsson committed
1127 1128
  if (window == _gdk_quartz_keyboard_grab_window)
    gdk_keyboard_ungrab (0);
Anders Carlsson's avatar
Anders Carlsson committed
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
}

void
gdk_window_withdraw (GdkWindow *window)
{
  gdk_window_hide (window);
}

static void
move_resize_window_internal (GdkWindow *window,
			     gint       x,
			     gint       y,
			     gint       width,
			     gint       height)
{
  GdkWindowObject *private = (GdkWindowObject *)window;
  GdkWindowImplQuartz *impl;
1146 1147 1148 1149 1150 1151
  GdkRectangle old_visible;
  GdkRectangle new_visible;
  GdkRectangle scroll_rect;
  GdkRegion *old_region;
  GdkRegion *expose_region;
  NSSize delta;
Anders Carlsson's avatar
Anders Carlsson committed
1152 1153

  if (GDK_WINDOW_DESTROYED (window))
1154
    return;
Anders Carlsson's avatar
Anders Carlsson committed
1155 1156 1157

  impl = GDK_WINDOW_IMPL_QUARTZ (private->impl);

1158 1159 1160 1161 1162 1163 1164 1165
  if ((x == private->x) &&
      (y == private->y) &&
      (width == impl->width) &&
      (height == impl->height))
    {
      return;
    }

1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
  if (!impl->toplevel)
    {
      /* The previously visible area of this window in a coordinate
       * system rooted at the origin of this window.
       */
      old_visible.x = -private->x;
      old_visible.y = -private->y;

      gdk_window_get_size (GDK_DRAWABLE (private->parent), 
                           &old_visible.width, 
                           &old_visible.height);
    }

Anders Carlsson's avatar
Anders Carlsson committed
1179
  if (x != -1)
1180 1181 1182 1183 1184 1185 1186 1187
    {
      delta.width = x - private->x;
      private->x = x;
    }
  else
    {
      delta.width = 0;
    }
Anders Carlsson's avatar
Anders Carlsson committed
1188 1189

  if (y != -1)
1190 1191 1192 1193 1194 1195 1196 1197
    {
      delta.height = y - private->y;
      private->y = y;
    }
  else
    {
      delta.height = 0;
    }
Anders Carlsson's avatar
Anders Carlsson committed
1198 1199 1200 1201 1202 1203 1204

  if (width != -1)
    impl->width = width;

  if (height != -1)
    impl->height = height;

1205 1206
  GDK_QUARTZ_ALLOC_POOL;

Anders Carlsson's avatar
Anders Carlsson committed
1207 1208
  if (impl->toplevel)
    {
1209 1210 1211 1212 1213 1214
      NSRect content_rect;
      NSRect frame_rect;

      content_rect =  NSMakeRect (private->x,
                                  _gdk_quartz_window_get_inverted_screen_y (private->y + impl->height),
                                  impl->width, impl->height);
Anders Carlsson's avatar
Anders Carlsson committed
1215
      
1216
      frame_rect = [impl->toplevel frameRectForContentRect:content_rect];
Anders Carlsson's avatar
Anders Carlsson committed
1217 1218 1219 1220 1221
      [impl->toplevel setFrame:frame_rect display:YES];
    }
  else 
    {
      if (!private->input_only)
1222 1223
        {
          NSRect nsrect;
1224

1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
          nsrect = NSMakeRect (private->x, private->y, impl->width, impl->height);

          /* The newly visible area of this window in a coordinate
           * system rooted at the origin of this window.
           */
          new_visible.x = -private->x;
          new_visible.y = -private->y;
          new_visible.width = old_visible.width;   /* parent has not changed size */
          new_visible.height = old_visible.height; /* parent has not changed size */

          expose_region = gdk_region_rectangle (&new_visible);
          old_region = gdk_region_rectangle (&old_visible);
          gdk_region_subtract (expose_region, old_region);

          /* Determine what (if any) part of the previously visible
           * part of the window can be copied without a redraw
           */
          scroll_rect = old_visible;
          scroll_rect.x -= delta.width;
          scroll_rect.y -= delta.height;
          gdk_rectangle_intersect (&scroll_rect, &old_visible, &scroll_rect);

          if (!gdk_region_empty (expose_region))
            {
              GdkRectangle* rects;
              gint n_rects;
              gint n;

              if (scroll_rect.width != 0 && scroll_rect.height != 0)
                {
                  [impl->view scrollRect:NSMakeRect (scroll_rect.x,
                                                     scroll_rect.y,
                                                     scroll_rect.width,
                                                     scroll_rect.height)
			              by:delta];
                }

              [impl->view setFrame:nsrect];

              gdk_region_get_rectangles (expose_region, &rects, &n_rects);

              for (n = 0; n < n_rects; ++n)
                {
                  [impl->view setNeedsDisplayInRect:NSMakeRect (rects[n].x,
                                                                rects[n].y,
                                                                rects[n].width,
                                                                rects[n].height)];
                }

              g_free (rects);
            }
          else
            {
              [impl->view setFrame:nsrect];
              [impl->view setNeedsDisplay:YES];
            }

          gdk_region_destroy (expose_region);
          gdk_region_destroy (old_region);
        }
Anders Carlsson's avatar
Anders Carlsson committed
1285
    }
1286 1287

  GDK_QUARTZ_RELEASE_POOL;
Anders Carlsson's avatar
Anders Carlsson committed
1288 1289 1290 1291 1292 1293 1294 1295 1296
}

void
gdk_window_move (GdkWindow *window,
		 gint       x,
		 gint       y)
{
  g_return_if_fail (GDK_IS_WINDOW (window));

1297 1298 1299
  if (((GdkWindowObject *)window)->state & GDK_WINDOW_STATE_FULLSCREEN)
    return;

Anders Carlsson's avatar
Anders Carlsson committed
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
  move_resize_window_internal (window, x, y, -1, -1);
}

void
gdk_window_resize (GdkWindow *window,
		   gint       width,
		   gint       height)
{
  g_return_if_fail (GDK_IS_WINDOW (window));

1310 1311 1312
  if (((GdkWindowObject *)window)->state & GDK_WINDOW_STATE_FULLSCREEN)
    return;

Anders Carlsson's avatar
Anders Carlsson committed
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
  if (width < 1)
    width = 1;
  if (height < 1)
    height = 1;

  move_resize_window_internal (window, -1, -1, width, height);
}

void
gdk_window_move_resize (GdkWindow *window,
			gint       x,
			gint       y,
			gint       width,
			gint       height)
{
  if (width < 1)
    width = 1;
  if (height < 1)
    height = 1;

  move_resize_window_internal (window, x, y, width, height);
}

void
gdk_window_reparent (GdkWindow *window,
		     GdkWindow *new_parent,
		     gint       x,
		     gint       y)
{
  g_warning ("gdk_window_reparent: %p %p (%d, %d)", 
	     window, new_parent, x, y);

  /* FIXME: Implement */
}

void
_gdk_windowing_window_clear_area (GdkWindow *window,
				  gint       x,
				  gint       y,
				  gint       width,
				  gint       height)
{
  /* FIXME: Implement */
}

void
_gdk_windowing_window_clear_area_e (GdkWindow *window,
				    gint       x,
				    gint       y,
				    gint       width,
				    gint       height)
{
  /* FIXME: Implement */
}

1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
/* Get the toplevel ordering from NSApp and update our own list. We do
 * this on demand since the NSApp's list is not up to date directly
 * after we get windowDidBecomeMain.
 */
static void
update_toplevel_order (void)
{
  GdkWindowObject *root;
  GdkWindowImplQuartz *root_impl;
  NSEnumerator *enumerator;
  id nswindow;
  GList *toplevels = NULL;

  root = GDK_WINDOW_OBJECT (_gdk_root);
  root_impl = GDK_WINDOW_IMPL_QUARTZ (root->impl);

  if (root_impl->sorted_children)
    return;

Richard Hult's avatar
Richard Hult committed
1387 1388
  GDK_QUARTZ_ALLOC_POOL;

1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
  enumerator = [[NSApp orderedWindows] objectEnumerator];
  while ((nswindow = [enumerator nextObject]))
    {
      GdkWindow *window;

      if (![[nswindow contentView] isKindOfClass:[GdkQuartzView class]])
        continue;

      window = [(GdkQuartzView *)[nswindow contentView] gdkWindow];
      toplevels = g_list_prepend (toplevels, window);
    }

Richard Hult's avatar
Richard Hult committed
1401 1402
  GDK_QUARTZ_RELEASE_POOL;

1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
  root_impl->sorted_chi