gdkdrawable-quartz.c 19.8 KB
Newer Older
Anders Carlsson's avatar
Anders Carlsson committed
1 2
/* gdkdrawable-quartz.c
 *
3
 * Copyright (C) 2005-2007 Imendio AB
Anders Carlsson's avatar
Anders Carlsson committed
4 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.
 */

#include <config.h>
22
#include <cairo-quartz.h>
Anders Carlsson's avatar
Anders Carlsson committed
23 24 25 26
#include "gdkprivate-quartz.h"

static gpointer parent_class;

27
static cairo_user_data_key_t gdk_quartz_cairo_key;
Anders Carlsson's avatar
Anders Carlsson committed
28

29 30 31 32
typedef struct {
  GdkDrawable  *drawable;
  CGContextRef  cg_context;
} GdkQuartzCairoSurfaceData;
Anders Carlsson's avatar
Anders Carlsson committed
33 34

static void
35
gdk_quartz_cairo_surface_destroy (void *data)
Anders Carlsson's avatar
Anders Carlsson committed
36
{
37 38
  GdkQuartzCairoSurfaceData *surface_data = data;
  GdkDrawableImplQuartz *impl = GDK_DRAWABLE_IMPL_QUARTZ (surface_data->drawable);
Anders Carlsson's avatar
Anders Carlsson committed
39

40 41
  gdk_quartz_drawable_release_context (surface_data->drawable, 
				       surface_data->cg_context);
Anders Carlsson's avatar
Anders Carlsson committed
42

43 44 45
  impl->cairo_surface = NULL;

  g_free (surface_data);
Anders Carlsson's avatar
Anders Carlsson committed
46 47 48 49 50 51 52 53 54 55 56
}

static cairo_surface_t *
gdk_quartz_ref_cairo_surface (GdkDrawable *drawable)
{
  GdkDrawableImplQuartz *impl = GDK_DRAWABLE_IMPL_QUARTZ (drawable);

  if (GDK_IS_WINDOW_IMPL_QUARTZ (drawable) &&
      GDK_WINDOW_DESTROYED (impl->wrapper))
    return NULL;

57 58 59 60 61 62 63 64 65 66 67
  if (!impl->cairo_surface)
    {
      CGContextRef cg_context;
      int width, height;
      GdkQuartzCairoSurfaceData *surface_data;

      cg_context = gdk_quartz_drawable_get_context (drawable, TRUE);
      if (!cg_context)
	return NULL;

      gdk_drawable_get_size (drawable, &width, &height);
Anders Carlsson's avatar
Anders Carlsson committed
68

69
      impl->cairo_surface = cairo_quartz_surface_create_for_cg_context (cg_context, width, height);
Anders Carlsson's avatar
Anders Carlsson committed
70

71 72 73
      surface_data = g_new (GdkQuartzCairoSurfaceData, 1);
      surface_data->drawable = drawable;
      surface_data->cg_context = cg_context;
Anders Carlsson's avatar
Anders Carlsson committed
74

75 76 77 78 79
      cairo_surface_set_user_data (impl->cairo_surface, &gdk_quartz_cairo_key,
				   surface_data, gdk_quartz_cairo_surface_destroy);
    }
  else
    cairo_surface_reference (impl->cairo_surface);
Anders Carlsson's avatar
Anders Carlsson committed
80

81
  return impl->cairo_surface;
Anders Carlsson's avatar
Anders Carlsson committed
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 121 122 123 124 125 126 127
}

static void
gdk_quartz_set_colormap (GdkDrawable *drawable,
			 GdkColormap *colormap)
{
  GdkDrawableImplQuartz *impl = GDK_DRAWABLE_IMPL_QUARTZ (drawable);

  if (impl->colormap == colormap)
    return;
  
  if (impl->colormap)
    g_object_unref (impl->colormap);
  impl->colormap = colormap;
  if (impl->colormap)
    g_object_ref (impl->colormap);
}

static GdkColormap*
gdk_quartz_get_colormap (GdkDrawable *drawable)
{
  return GDK_DRAWABLE_IMPL_QUARTZ (drawable)->colormap;
}

static GdkScreen*
gdk_quartz_get_screen (GdkDrawable *drawable)
{
  return _gdk_screen;
}

static GdkVisual*
gdk_quartz_get_visual (GdkDrawable *drawable)
{
  return gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_QUARTZ (drawable)->wrapper);
}

static int
gdk_quartz_get_depth (GdkDrawable *drawable)
{
  /* This is a bit bogus but I'm not sure the other way is better */

  return gdk_drawable_get_depth (GDK_DRAWABLE_IMPL_QUARTZ (drawable)->wrapper);
}

static void
gdk_quartz_draw_rectangle (GdkDrawable *drawable,
128 129 130 131 132 133
			   GdkGC       *gc,
			   gboolean     filled,
			   gint         x,
			   gint         y,
			   gint         width,
			   gint         height)
Anders Carlsson's avatar
Anders Carlsson committed
134
{
135
  CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
Anders Carlsson's avatar
Anders Carlsson committed
136 137 138 139

  if (!context)
    return;

140 141 142 143
  gdk_quartz_update_context_from_gc (context, gc,
				     filled ?
				     GDK_QUARTZ_CONTEXT_FILL :
				     GDK_QUARTZ_CONTEXT_STROKE);
Anders Carlsson's avatar
Anders Carlsson committed
144 145 146

  if (filled)
    {
147 148
      CGRect rect = CGRectMake (x, y, width, height);

Anders Carlsson's avatar
Anders Carlsson committed
149 150 151 152
      CGContextFillRect (context, rect);
    }
  else
    {
153 154
      CGRect rect = CGRectMake (x + 0.5, y + 0.5, width, height);

Anders Carlsson's avatar
Anders Carlsson committed
155 156 157
      CGContextStrokeRect (context, rect);
    }

158
  gdk_quartz_drawable_release_context (drawable, context);
Anders Carlsson's avatar
Anders Carlsson committed
159 160 161 162
}

static void
gdk_quartz_draw_arc (GdkDrawable *drawable,
163 164 165 166 167 168 169 170
		     GdkGC       *gc,
		     gboolean     filled,
		     gint         x,
		     gint         y,
		     gint         width,
		     gint         height,
		     gint         angle1,
		     gint         angle2)
Anders Carlsson's avatar
Anders Carlsson committed
171
{
172
  CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
Anders Carlsson's avatar
Anders Carlsson committed
173 174 175 176 177
  float start_angle, end_angle;

  if (!context)
    return;

178 179 180 181
  gdk_quartz_update_context_from_gc (context, gc,
				     filled ?
				     GDK_QUARTZ_CONTEXT_FILL :
				     GDK_QUARTZ_CONTEXT_STROKE);
Anders Carlsson's avatar
Anders Carlsson committed
182 183 184 185 186 187 188 189

  CGContextSaveGState (context);

  start_angle = (2 - (angle1 / (180.0 * 64.0))) * G_PI;
  end_angle = start_angle - (angle2 / (180.0 * 64.0)) * G_PI;

  if (filled)
    {
190 191 192 193 194
      CGContextTranslateCTM (context,
                             x + width / 2.0,
                             y + height / 2.0);
      CGContextScaleCTM (context, 1.0, (double)height / (double)width);

Anders Carlsson's avatar
Anders Carlsson committed
195
      CGContextMoveToPoint (context, 0, 0);
196
      CGContextAddArc (context, 0, 0, width / 2.0,
Anders Carlsson's avatar
Anders Carlsson committed
197 198 199 200 201 202 203
		       start_angle, end_angle,
		       TRUE);
      CGContextClosePath (context);
      CGContextFillPath (context);
    }
  else
    {
204 205 206 207 208 209
      CGContextTranslateCTM (context,
                             x + width / 2.0 + 0.5,
                             y + height / 2.0 + 0.5);
      CGContextScaleCTM (context, 1.0, (double)height / (double)width);

      CGContextAddArc (context, 0, 0, width / 2.0,
Anders Carlsson's avatar
Anders Carlsson committed
210 211 212 213 214 215 216
		       start_angle, end_angle,
		       TRUE);
      CGContextStrokePath (context);
    }

  CGContextRestoreGState (context);

217
  gdk_quartz_drawable_release_context (drawable, context);
Anders Carlsson's avatar
Anders Carlsson committed
218 219 220 221 222 223 224 225 226
}

static void
gdk_quartz_draw_polygon (GdkDrawable *drawable,
			 GdkGC       *gc,
			 gboolean     filled,
			 GdkPoint    *points,
			 gint         npoints)
{
227
  CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
228 229 230 231 232
  int i;

  if (!context)
    return;

233 234 235 236
  gdk_quartz_update_context_from_gc (context, gc,
				     filled ?
				     GDK_QUARTZ_CONTEXT_FILL :
				     GDK_QUARTZ_CONTEXT_STROKE);
237 238

  if (filled)
239 240 241 242
    {
      CGContextMoveToPoint (context, points[0].x, points[0].y);
      for (i = 1; i < npoints; i++)
	CGContextAddLineToPoint (context, points[i].x, points[i].y);
243

244 245 246
      CGContextClosePath (context);
      CGContextFillPath (context);
    }
247
  else
248 249 250 251 252 253 254 255
    {
      CGContextMoveToPoint (context, points[0].x + 0.5, points[0].y + 0.5);
      for (i = 1; i < npoints; i++)
	CGContextAddLineToPoint (context, points[i].x + 0.5, points[i].y + 0.5);

      CGContextClosePath (context);
      CGContextStrokePath (context);
    }
256

257
  gdk_quartz_drawable_release_context (drawable, context);
Anders Carlsson's avatar
Anders Carlsson committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
}

static void
gdk_quartz_draw_text (GdkDrawable *drawable,
		      GdkFont     *font,
		      GdkGC       *gc,
		      gint         x,
		      gint         y,
		      const gchar *text,
		      gint         text_length)
{
  /* FIXME: Implement */
}

static void
gdk_quartz_draw_text_wc (GdkDrawable    *drawable,
			 GdkFont	*font,
			 GdkGC	        *gc,
			 gint	         x,
			 gint	         y,
			 const GdkWChar *text,
			 gint	         text_length)
{
  /* FIXME: Implement */
}

static void
gdk_quartz_draw_drawable (GdkDrawable *drawable,
286 287 288 289 290 291 292 293
			  GdkGC       *gc,
			  GdkPixmap   *src,
			  gint         xsrc,
			  gint         ysrc,
			  gint         xdest,
			  gint         ydest,
			  gint         width,
			  gint         height)
Anders Carlsson's avatar
Anders Carlsson committed
294 295 296 297 298 299 300
{
  int src_depth = gdk_drawable_get_depth (src);
  int dest_depth = gdk_drawable_get_depth (drawable);
  GdkDrawableImplQuartz *src_impl;

  if (GDK_IS_DRAWABLE_IMPL_QUARTZ (src))
    src_impl = GDK_DRAWABLE_IMPL_QUARTZ (src);
301
  else if (GDK_IS_PIXMAP (src))
Anders Carlsson's avatar
Anders Carlsson committed
302
    src_impl = GDK_DRAWABLE_IMPL_QUARTZ (GDK_PIXMAP_OBJECT (src)->impl);
303 304 305 306 307 308 309 310
  else if (GDK_IS_WINDOW (src))
    {
      src_impl = GDK_DRAWABLE_IMPL_QUARTZ (GDK_WINDOW_OBJECT (src)->impl);
      /* FIXME: Implement drawing a window. */
      return;
    }
  else
    g_assert_not_reached ();
Anders Carlsson's avatar
Anders Carlsson committed
311 312 313 314 315 316 317
  
  if (src_depth == 1)
    {
      /* FIXME: src depth 1 is not supported yet */
    }
  else if (dest_depth != 0 && src_depth == dest_depth)
    {
318
      CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
Anders Carlsson's avatar
Anders Carlsson committed
319 320 321 322

      if (!context)
	return;

323 324
      gdk_quartz_update_context_from_gc (context, gc,
					 GDK_QUARTZ_CONTEXT_STROKE);
Anders Carlsson's avatar
Anders Carlsson committed
325 326 327

      CGContextClipToRect (context, CGRectMake (xdest, ydest, width, height));
      CGContextTranslateCTM (context, xdest - xsrc, ydest - ysrc);
328 329 330 331
      CGContextDrawImage (context, 
			  CGRectMake(0, 0, 
				     GDK_PIXMAP_IMPL_QUARTZ (src_impl)->width, 
				     GDK_PIXMAP_IMPL_QUARTZ (src_impl)->height), 
Anders Carlsson's avatar
Anders Carlsson committed
332 333
			  GDK_PIXMAP_IMPL_QUARTZ (src_impl)->image);

334
      gdk_quartz_drawable_release_context (drawable, context);
Anders Carlsson's avatar
Anders Carlsson committed
335 336 337 338 339 340 341 342
    }
  else
    g_warning ("Attempt to draw a drawable with depth %d to a drawable with depth %d",
	       src_depth, dest_depth);
}

static void
gdk_quartz_draw_points (GdkDrawable *drawable,
343 344 345
			GdkGC       *gc,
			GdkPoint    *points,
			gint         npoints)
Anders Carlsson's avatar
Anders Carlsson committed
346
{
347
  CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
Anders Carlsson's avatar
Anders Carlsson committed
348 349
  int i;

350 351 352
  if (!context)
    return;

353 354 355
  gdk_quartz_update_context_from_gc (context, gc,
				     GDK_QUARTZ_CONTEXT_STROKE |
				     GDK_QUARTZ_CONTEXT_FILL);
356

Anders Carlsson's avatar
Anders Carlsson committed
357 358 359
  /* Just draw 1x1 rectangles */
  for (i = 0; i < npoints; i++) 
    {
360
      CGRect rect = CGRectMake (points[i].x, points[i].y, 1, 1);
361
      CGContextFillRect (context, rect);
Anders Carlsson's avatar
Anders Carlsson committed
362
    }
363 364

  gdk_quartz_drawable_release_context (drawable, context);
Anders Carlsson's avatar
Anders Carlsson committed
365 366 367 368
}

static void
gdk_quartz_draw_segments (GdkDrawable    *drawable,
369 370 371
			  GdkGC          *gc,
			  GdkSegment     *segs,
			  gint            nsegs)
Anders Carlsson's avatar
Anders Carlsson committed
372
{
373
  CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
Anders Carlsson's avatar
Anders Carlsson committed
374 375 376 377 378
  int i;

  if (!context)
    return;

379 380
  gdk_quartz_update_context_from_gc (context, gc,
				     GDK_QUARTZ_CONTEXT_STROKE);
Anders Carlsson's avatar
Anders Carlsson committed
381 382 383

  for (i = 0; i < nsegs; i++)
    {
384 385
      CGContextMoveToPoint (context, segs[i].x1 + 0.5, segs[i].y1 + 0.5);
      CGContextAddLineToPoint (context, segs[i].x2 + 0.5, segs[i].y2 + 0.5);
Anders Carlsson's avatar
Anders Carlsson committed
386 387 388 389
    }
  
  CGContextStrokePath (context);

390
  gdk_quartz_drawable_release_context (drawable, context);
Anders Carlsson's avatar
Anders Carlsson committed
391 392 393 394 395 396 397 398
}

static void
gdk_quartz_draw_lines (GdkDrawable *drawable,
		       GdkGC       *gc,
		       GdkPoint    *points,
		       gint         npoints)
{
399
  CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
400 401 402 403 404
  int i;

  if (!context)
    return;

405 406 407 408 409
  gdk_quartz_update_context_from_gc (context, gc,
				     GDK_QUARTZ_CONTEXT_STROKE);

  CGContextMoveToPoint (context, points[0].x + 0.5, points[0].y + 0.5);

410
  for (i = 1; i < npoints; i++)
411 412
    CGContextAddLineToPoint (context, points[i].x + 0.5, points[i].y + 0.5);

413 414
  CGContextStrokePath (context);

415
  gdk_quartz_drawable_release_context (drawable, context);
Anders Carlsson's avatar
Anders Carlsson committed
416 417 418 419
}

static void
gdk_quartz_draw_pixbuf (GdkDrawable     *drawable,
420 421 422 423 424 425 426 427 428 429 430
			GdkGC           *gc,
			GdkPixbuf       *pixbuf,
			gint             src_x,
			gint             src_y,
			gint             dest_x,
			gint             dest_y,
			gint             width,
			gint             height,
			GdkRgbDither     dither,
			gint             x_dither,
			gint             y_dither)
Anders Carlsson's avatar
Anders Carlsson committed
431
{
432
  CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
Anders Carlsson's avatar
Anders Carlsson committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
  CGColorSpaceRef colorspace;
  CGDataProviderRef data_provider;
  CGImageRef image;
  void *data;
  int rowstride, pixbuf_width, pixbuf_height;
  gboolean has_alpha;

  if (!context)
    return;

  pixbuf_width = gdk_pixbuf_get_width (pixbuf);
  pixbuf_height = gdk_pixbuf_get_height (pixbuf);
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);
  has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);

  data = gdk_pixbuf_get_pixels (pixbuf);

  colorspace = CGColorSpaceCreateDeviceRGB ();
  data_provider = CGDataProviderCreateWithData (NULL, data, pixbuf_height * rowstride, NULL);

  image = CGImageCreate (pixbuf_width, pixbuf_height, 8,
			 has_alpha ? 32 : 24, rowstride, 
			 colorspace, 
			 has_alpha ? kCGImageAlphaLast : 0,
			 data_provider, NULL, FALSE, 
			 kCGRenderingIntentDefault);

  CGDataProviderRelease (data_provider);
  CGColorSpaceRelease (colorspace);

463 464
  gdk_quartz_update_context_from_gc (context, gc,
				     GDK_QUARTZ_CONTEXT_STROKE);
Anders Carlsson's avatar
Anders Carlsson committed
465 466 467 468 469

  CGContextClipToRect (context, CGRectMake (dest_x, dest_y, width, height));
  CGContextTranslateCTM (context, dest_x - src_x, dest_y - src_y + pixbuf_height);
  CGContextScaleCTM (context, 1, -1);

470
  CGContextDrawImage (context, CGRectMake (0, 0, pixbuf_width, pixbuf_height), image);
Anders Carlsson's avatar
Anders Carlsson committed
471 472
  CGImageRelease (image);

473
  gdk_quartz_drawable_release_context (drawable, context);
Anders Carlsson's avatar
Anders Carlsson committed
474 475 476 477
}

static void
gdk_quartz_draw_image (GdkDrawable     *drawable,
478 479 480 481 482 483 484 485
		       GdkGC           *gc,
		       GdkImage        *image,
		       gint             xsrc,
		       gint             ysrc,
		       gint             xdest,
		       gint             ydest,
		       gint             width,
		       gint             height)
Anders Carlsson's avatar
Anders Carlsson committed
486
{
487
  CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
Anders Carlsson's avatar
Anders Carlsson committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
  CGColorSpaceRef colorspace;
  CGDataProviderRef data_provider;
  CGImageRef cgimage;

  if (!context)
    return;

  colorspace = CGColorSpaceCreateDeviceRGB ();
  data_provider = CGDataProviderCreateWithData (NULL, image->mem, image->height * image->bpl, NULL);

  /* FIXME: Make sure that this function draws 32-bit images correctly,
  * also check endianness wrt kCGImageAlphaNoneSkipFirst */
  cgimage = CGImageCreate (image->width, image->height, 8,
			   32, image->bpl,
			   colorspace,
			   kCGImageAlphaNoneSkipFirst, 
			   data_provider, NULL, FALSE, kCGRenderingIntentDefault);

  CGDataProviderRelease (data_provider);
  CGColorSpaceRelease (colorspace);

509 510
  gdk_quartz_update_context_from_gc (context, gc,
				     GDK_QUARTZ_CONTEXT_STROKE);
Anders Carlsson's avatar
Anders Carlsson committed
511 512 513 514 515 516 517 518

  CGContextClipToRect (context, CGRectMake (xdest, ydest, width, height));
  CGContextTranslateCTM (context, xdest - xsrc, ydest - ysrc + image->height);
  CGContextScaleCTM (context, 1, -1);

  CGContextDrawImage (context, CGRectMake (0, 0, image->width, image->height), cgimage);
  CGImageRelease (cgimage);

519
  gdk_quartz_drawable_release_context (drawable, context);
Anders Carlsson's avatar
Anders Carlsson committed
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
}

static void
gdk_drawable_impl_quartz_finalize (GObject *object)
{
  GdkDrawableImplQuartz *impl = GDK_DRAWABLE_IMPL_QUARTZ (object);

  if (impl->colormap)
    g_object_unref (impl->colormap);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gdk_drawable_impl_quartz_class_init (GdkDrawableImplQuartzClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);

  parent_class = g_type_class_peek_parent (klass);

  object_class->finalize = gdk_drawable_impl_quartz_finalize;

  drawable_class->create_gc = _gdk_quartz_gc_new;
  drawable_class->draw_rectangle = gdk_quartz_draw_rectangle;
  drawable_class->draw_arc = gdk_quartz_draw_arc;
  drawable_class->draw_polygon = gdk_quartz_draw_polygon;
  drawable_class->draw_text = gdk_quartz_draw_text;
  drawable_class->draw_text_wc = gdk_quartz_draw_text_wc;
  drawable_class->draw_drawable = gdk_quartz_draw_drawable;
  drawable_class->draw_points = gdk_quartz_draw_points;
  drawable_class->draw_segments = gdk_quartz_draw_segments;
  drawable_class->draw_lines = gdk_quartz_draw_lines;
  drawable_class->draw_image = gdk_quartz_draw_image;
  drawable_class->draw_pixbuf = gdk_quartz_draw_pixbuf;

  drawable_class->ref_cairo_surface = gdk_quartz_ref_cairo_surface;

  drawable_class->set_colormap = gdk_quartz_set_colormap;
  drawable_class->get_colormap = gdk_quartz_get_colormap;

  drawable_class->get_depth = gdk_quartz_get_depth;
  drawable_class->get_screen = gdk_quartz_get_screen;
  drawable_class->get_visual = gdk_quartz_get_visual;

  drawable_class->_copy_to_image = _gdk_quartz_copy_to_image;
}

GType
gdk_drawable_impl_quartz_get_type (void)
{
  static GType object_type = 0;

  if (!object_type)
    {
      static const GTypeInfo object_info =
      {
        sizeof (GdkDrawableImplQuartzClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gdk_drawable_impl_quartz_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GdkDrawableImplQuartz),
        0,              /* n_preallocs */
        (GInstanceInitFunc) NULL,
      };
      
      object_type = g_type_register_static (GDK_TYPE_DRAWABLE,
                                            "GdkDrawableImplQuartz",
                                            &object_info, 0);
    }
  
  return object_type;
}

CGContextRef 
597 598
gdk_quartz_drawable_get_context (GdkDrawable *drawable,
				 gboolean     antialias)
Anders Carlsson's avatar
Anders Carlsson committed
599
{
600
  GdkDrawableImplQuartz *drawable_impl = GDK_DRAWABLE_IMPL_QUARTZ (drawable);
Anders Carlsson's avatar
Anders Carlsson committed
601

602 603 604
  if (GDK_IS_WINDOW_IMPL_QUARTZ (drawable) &&
      GDK_WINDOW_DESTROYED (drawable_impl->wrapper))
    return NULL;
Anders Carlsson's avatar
Anders Carlsson committed
605

606 607 608 609 610 611 612 613 614 615 616
  CGContextRef cg_context;
  
  if (GDK_IS_WINDOW_IMPL_QUARTZ (drawable))
    {
      GdkWindowImplQuartz *window_impl = GDK_WINDOW_IMPL_QUARTZ (drawable);
	  
      /* Lock focus when not called as part of begin/end paint cycle. 
       * This is needed to allow broken apps that draw outside of expose
       * to still work (somewhat). 
       */
      if (window_impl->begin_paint_count == 0)
Anders Carlsson's avatar
Anders Carlsson committed
617
	{
618 619 620 621 622
	  window_impl->pool = [[NSAutoreleasePool alloc] init];
	  if (![window_impl->view lockFocusIfCanDraw])
	    {
	      [window_impl->pool release];
	      window_impl->pool = NULL;
Anders Carlsson's avatar
Anders Carlsson committed
623

624 625 626
	      return NULL;
	    }
	}
Anders Carlsson's avatar
Anders Carlsson committed
627

628 629 630 631
      cg_context = [[NSGraphicsContext currentContext] graphicsPort];
      CGContextSaveGState (cg_context);
      CGContextSetAllowsAntialiasing (cg_context, antialias);
	  
632
      /* We'll emulate the clipping caused by double buffering here */
633
      if (window_impl->begin_paint_count != 0)
634 635 636 637 638 639
	{
	  CGRect rect;
	  CGRect *cg_rects;
	  GdkRectangle *rects;
	  gint n_rects, i;
	  
640
	  gdk_region_get_rectangles (window_impl->paint_clip_region,
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
				     &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;
	    }
	  
656
	  CGContextClipToRects (cg_context, cg_rects, n_rects);
657 658 659 660 661
	  
	  g_free (rects);
	  if (cg_rects != &rect)
	    g_free (cg_rects);
	}
Anders Carlsson's avatar
Anders Carlsson committed
662 663 664 665 666
    }
  else if (GDK_IS_PIXMAP_IMPL_QUARTZ (drawable))
    {
      GdkPixmapImplQuartz *impl = GDK_PIXMAP_IMPL_QUARTZ (drawable);
      
667 668 669 670 671 672 673 674 675 676 677 678 679
      cg_context = CGBitmapContextCreate (impl->data,
					  CGImageGetWidth (impl->image),
					  CGImageGetHeight (impl->image),
					  CGImageGetBitsPerComponent (impl->image),
					  CGImageGetBytesPerRow (impl->image),
					  CGImageGetColorSpace (impl->image),
					  CGImageGetBitmapInfo (impl->image));
      CGContextSetAllowsAntialiasing (cg_context, antialias);
    }
  else 
    {
      g_warning ("Tried to create CGContext for something not a quartz window or pixmap");
      cg_context = NULL;
Anders Carlsson's avatar
Anders Carlsson committed
680 681
    }

682
  return cg_context;
Anders Carlsson's avatar
Anders Carlsson committed
683 684 685
}

void
686 687
gdk_quartz_drawable_release_context (GdkDrawable  *drawable, 
				     CGContextRef  cg_context)
Anders Carlsson's avatar
Anders Carlsson committed
688 689 690
{
  if (GDK_IS_WINDOW_IMPL_QUARTZ (drawable))
    {
691
      GdkWindowImplQuartz *window_impl = GDK_WINDOW_IMPL_QUARTZ (drawable);
Anders Carlsson's avatar
Anders Carlsson committed
692

693 694
      CGContextRestoreGState (cg_context);
      CGContextSetAllowsAntialiasing (cg_context, TRUE);
Anders Carlsson's avatar
Anders Carlsson committed
695

696 697 698 699 700 701 702 703
      /* Only flush and unlock if called outside the expose, since it's
       * already handled for otherwise.
       */
      if (window_impl->in_paint_rect_count == 0 && window_impl->begin_paint_count == 0)
	{
	  CGContextFlush (cg_context);
	  [window_impl->view unlockFocus];
	}
704

705 706 707 708 709
      if (window_impl->pool)
	{
	  [window_impl->pool release];
	  window_impl->pool = NULL;
	}
Anders Carlsson's avatar
Anders Carlsson committed
710 711
    }
  else if (GDK_IS_PIXMAP_IMPL_QUARTZ (drawable))
712 713 714 715 716 717 718 719 720
    CGContextRelease (cg_context);
}

void
_gdk_quartz_drawable_finish (GdkDrawable *drawable)
{
  GdkDrawableImplQuartz *impl = GDK_DRAWABLE_IMPL_QUARTZ (drawable);

  if (impl->cairo_surface)
Anders Carlsson's avatar
Anders Carlsson committed
721
    {
722 723 724 725
      cairo_surface_finish (impl->cairo_surface);
      cairo_surface_set_user_data (impl->cairo_surface, &gdk_quartz_cairo_key,
				   NULL, NULL);
      impl->cairo_surface = NULL;
Anders Carlsson's avatar
Anders Carlsson committed
726
    }
727
}