2005-11-22 10:03:32 +00:00
|
|
|
/* gdkdrawable-quartz.c
|
|
|
|
*
|
2007-03-10 21:58:49 +00:00
|
|
|
* Copyright (C) 2005-2007 Imendio AB
|
2005-11-22 10:03:32 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2008-06-22 14:28:52 +00:00
|
|
|
#include "config.h"
|
2009-02-09 14:46:30 +00:00
|
|
|
#include <sys/time.h>
|
2006-04-16 19:33:11 +00:00
|
|
|
#include <cairo-quartz.h>
|
2005-11-22 10:03:32 +00:00
|
|
|
#include "gdkprivate-quartz.h"
|
|
|
|
|
|
|
|
static gpointer parent_class;
|
|
|
|
|
2007-03-10 21:58:49 +00:00
|
|
|
static cairo_user_data_key_t gdk_quartz_cairo_key;
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2007-03-10 21:58:49 +00:00
|
|
|
typedef struct {
|
|
|
|
GdkDrawable *drawable;
|
|
|
|
CGContextRef cg_context;
|
|
|
|
} GdkQuartzCairoSurfaceData;
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2009-01-05 21:42:36 +00:00
|
|
|
void
|
|
|
|
_gdk_windowing_set_cairo_surface_size (cairo_surface_t *surface,
|
|
|
|
int width,
|
|
|
|
int height)
|
|
|
|
{
|
2009-02-18 15:20:06 +00:00
|
|
|
/* This is not supported with quartz surfaces. */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_quartz_cairo_surface_destroy (void *data)
|
|
|
|
{
|
|
|
|
GdkQuartzCairoSurfaceData *surface_data = data;
|
|
|
|
GdkDrawableImplQuartz *impl = GDK_DRAWABLE_IMPL_QUARTZ (surface_data->drawable);
|
|
|
|
|
|
|
|
impl->cairo_surface = NULL;
|
|
|
|
|
|
|
|
gdk_quartz_drawable_release_context (surface_data->drawable,
|
|
|
|
surface_data->cg_context);
|
|
|
|
|
|
|
|
g_free (surface_data);
|
2009-01-05 21:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cairo_surface_t *
|
|
|
|
_gdk_windowing_create_cairo_surface (GdkDrawable *drawable,
|
|
|
|
int width,
|
|
|
|
int height)
|
|
|
|
{
|
2009-02-18 15:20:06 +00:00
|
|
|
CGContextRef cg_context;
|
|
|
|
GdkQuartzCairoSurfaceData *surface_data;
|
|
|
|
cairo_surface_t *surface;
|
2009-01-05 21:42:36 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
cg_context = gdk_quartz_drawable_get_context (drawable, TRUE);
|
2009-01-20 13:04:03 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
if (!cg_context)
|
|
|
|
return NULL;
|
2009-01-05 21:42:36 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
surface_data = g_new (GdkQuartzCairoSurfaceData, 1);
|
|
|
|
surface_data->drawable = drawable;
|
|
|
|
surface_data->cg_context = cg_context;
|
2009-01-20 13:04:03 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
surface = cairo_quartz_surface_create_for_cg_context (cg_context,
|
|
|
|
width, height);
|
2009-01-05 21:42:36 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
cairo_surface_set_user_data (surface, &gdk_quartz_cairo_key,
|
|
|
|
surface_data,
|
|
|
|
gdk_quartz_cairo_surface_destroy);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
return surface;
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2007-03-10 21:58:49 +00:00
|
|
|
if (!impl->cairo_surface)
|
|
|
|
{
|
|
|
|
int width, height;
|
|
|
|
|
|
|
|
gdk_drawable_get_size (drawable, &width, &height);
|
2009-02-18 15:20:06 +00:00
|
|
|
impl->cairo_surface = _gdk_windowing_create_cairo_surface (drawable,
|
|
|
|
width, height);
|
2007-03-10 21:58:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
cairo_surface_reference (impl->cairo_surface);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2007-03-10 21:58:49 +00:00
|
|
|
return impl->cairo_surface;
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2006-08-08 09:12:10 +00:00
|
|
|
GdkGC *gc,
|
|
|
|
gboolean filled,
|
|
|
|
gint x,
|
|
|
|
gint y,
|
|
|
|
gint width,
|
|
|
|
gint height)
|
2005-11-22 10:03:32 +00:00
|
|
|
{
|
2006-07-29 11:05:15 +00:00
|
|
|
CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
|
|
|
if (!context)
|
|
|
|
return;
|
|
|
|
|
2007-04-06 21:12:48 +00:00
|
|
|
_gdk_quartz_gc_update_cg_context (gc,
|
|
|
|
drawable,
|
|
|
|
context,
|
|
|
|
filled ?
|
|
|
|
GDK_QUARTZ_CONTEXT_FILL :
|
|
|
|
GDK_QUARTZ_CONTEXT_STROKE);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
|
|
|
if (filled)
|
|
|
|
{
|
2006-08-08 09:12:10 +00:00
|
|
|
CGRect rect = CGRectMake (x, y, width, height);
|
|
|
|
|
2005-11-22 10:03:32 +00:00
|
|
|
CGContextFillRect (context, rect);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-08 09:12:10 +00:00
|
|
|
CGRect rect = CGRectMake (x + 0.5, y + 0.5, width, height);
|
|
|
|
|
2005-11-22 10:03:32 +00:00
|
|
|
CGContextStrokeRect (context, rect);
|
|
|
|
}
|
|
|
|
|
2006-07-29 11:05:15 +00:00
|
|
|
gdk_quartz_drawable_release_context (drawable, context);
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_quartz_draw_arc (GdkDrawable *drawable,
|
2006-08-08 09:12:10 +00:00
|
|
|
GdkGC *gc,
|
|
|
|
gboolean filled,
|
|
|
|
gint x,
|
|
|
|
gint y,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
gint angle1,
|
|
|
|
gint angle2)
|
2005-11-22 10:03:32 +00:00
|
|
|
{
|
2006-07-29 11:05:15 +00:00
|
|
|
CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
|
2005-11-22 10:03:32 +00:00
|
|
|
float start_angle, end_angle;
|
2007-06-10 16:29:31 +00:00
|
|
|
gboolean clockwise = FALSE;
|
2005-11-22 10:03:32 +00:00
|
|
|
|
|
|
|
if (!context)
|
|
|
|
return;
|
|
|
|
|
2007-04-06 21:12:48 +00:00
|
|
|
_gdk_quartz_gc_update_cg_context (gc, drawable, context,
|
|
|
|
filled ?
|
|
|
|
GDK_QUARTZ_CONTEXT_FILL :
|
|
|
|
GDK_QUARTZ_CONTEXT_STROKE);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2007-06-10 16:29:31 +00:00
|
|
|
start_angle = angle1 * 2.0 * G_PI / 360.0 / 64.0;
|
|
|
|
end_angle = start_angle + angle2 * 2.0 * G_PI / 360.0 / 64.0;
|
|
|
|
|
|
|
|
/* angle2 is relative to angle1 and can be negative, which switches
|
|
|
|
* the drawing direction
|
|
|
|
*/
|
|
|
|
if (angle2 < 0)
|
|
|
|
clockwise = TRUE;
|
|
|
|
|
|
|
|
/* below, flip the coordinate system back to its original y-diretion
|
|
|
|
* so the angles passed to CGContextAddArc() are interpreted as
|
|
|
|
* expected
|
|
|
|
*
|
|
|
|
* FIXME: the implementation below works only for perfect circles
|
|
|
|
* (width == height). Any other aspect ratio either scales the
|
|
|
|
* line width unevenly or scales away the path entirely for very
|
|
|
|
* small line widths (esp. for line_width == 0, which is a hair
|
|
|
|
* line on X11 but must be approximated with the thinnest possible
|
|
|
|
* line on quartz).
|
|
|
|
*/
|
2005-11-22 10:03:32 +00:00
|
|
|
|
|
|
|
if (filled)
|
|
|
|
{
|
2006-08-08 09:12:10 +00:00
|
|
|
CGContextTranslateCTM (context,
|
|
|
|
x + width / 2.0,
|
|
|
|
y + height / 2.0);
|
2007-06-10 16:29:31 +00:00
|
|
|
CGContextScaleCTM (context, 1.0, - (double)height / (double)width);
|
2006-08-08 09:12:10 +00:00
|
|
|
|
2005-11-22 10:03:32 +00:00
|
|
|
CGContextMoveToPoint (context, 0, 0);
|
2006-08-08 09:12:10 +00:00
|
|
|
CGContextAddArc (context, 0, 0, width / 2.0,
|
2005-11-22 10:03:32 +00:00
|
|
|
start_angle, end_angle,
|
2007-06-10 16:29:31 +00:00
|
|
|
clockwise);
|
2005-11-22 10:03:32 +00:00
|
|
|
CGContextClosePath (context);
|
|
|
|
CGContextFillPath (context);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-08 09:12:10 +00:00
|
|
|
CGContextTranslateCTM (context,
|
|
|
|
x + width / 2.0 + 0.5,
|
|
|
|
y + height / 2.0 + 0.5);
|
2007-06-10 16:29:31 +00:00
|
|
|
CGContextScaleCTM (context, 1.0, - (double)height / (double)width);
|
2006-08-08 09:12:10 +00:00
|
|
|
|
|
|
|
CGContextAddArc (context, 0, 0, width / 2.0,
|
2005-11-22 10:03:32 +00:00
|
|
|
start_angle, end_angle,
|
2007-06-10 16:29:31 +00:00
|
|
|
clockwise);
|
2005-11-22 10:03:32 +00:00
|
|
|
CGContextStrokePath (context);
|
|
|
|
}
|
|
|
|
|
2006-07-29 11:05:15 +00:00
|
|
|
gdk_quartz_drawable_release_context (drawable, context);
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_quartz_draw_polygon (GdkDrawable *drawable,
|
|
|
|
GdkGC *gc,
|
|
|
|
gboolean filled,
|
|
|
|
GdkPoint *points,
|
|
|
|
gint npoints)
|
|
|
|
{
|
2006-07-29 11:05:15 +00:00
|
|
|
CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
|
2006-01-18 09:47:56 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!context)
|
|
|
|
return;
|
|
|
|
|
2007-04-06 21:12:48 +00:00
|
|
|
_gdk_quartz_gc_update_cg_context (gc, drawable, context,
|
|
|
|
filled ?
|
|
|
|
GDK_QUARTZ_CONTEXT_FILL :
|
|
|
|
GDK_QUARTZ_CONTEXT_STROKE);
|
2006-01-18 09:47:56 +00:00
|
|
|
|
|
|
|
if (filled)
|
2006-08-08 09:12:10 +00:00
|
|
|
{
|
|
|
|
CGContextMoveToPoint (context, points[0].x, points[0].y);
|
|
|
|
for (i = 1; i < npoints; i++)
|
|
|
|
CGContextAddLineToPoint (context, points[i].x, points[i].y);
|
2006-01-18 09:47:56 +00:00
|
|
|
|
2006-08-08 09:12:10 +00:00
|
|
|
CGContextClosePath (context);
|
|
|
|
CGContextFillPath (context);
|
|
|
|
}
|
2006-01-18 09:47:56 +00:00
|
|
|
else
|
2006-08-08 09:12:10 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2006-01-18 09:47:56 +00:00
|
|
|
|
2006-07-29 11:05:15 +00:00
|
|
|
gdk_quartz_drawable_release_context (drawable, context);
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_quartz_draw_drawable (GdkDrawable *drawable,
|
2006-07-31 21:05:12 +00:00
|
|
|
GdkGC *gc,
|
|
|
|
GdkPixmap *src,
|
|
|
|
gint xsrc,
|
|
|
|
gint ysrc,
|
|
|
|
gint xdest,
|
|
|
|
gint ydest,
|
|
|
|
gint width,
|
2009-07-01 14:13:31 +00:00
|
|
|
gint height,
|
|
|
|
GdkDrawable *original_src)
|
2005-11-22 10:03:32 +00:00
|
|
|
{
|
|
|
|
int src_depth = gdk_drawable_get_depth (src);
|
|
|
|
int dest_depth = gdk_drawable_get_depth (drawable);
|
|
|
|
GdkDrawableImplQuartz *src_impl;
|
|
|
|
|
2009-01-21 17:47:06 +00:00
|
|
|
if (GDK_IS_WINDOW_IMPL_QUARTZ (src))
|
|
|
|
{
|
|
|
|
GdkWindowImplQuartz *window_impl;
|
|
|
|
|
|
|
|
window_impl = GDK_WINDOW_IMPL_QUARTZ (src);
|
|
|
|
|
|
|
|
/* We do support moving areas on the same drawable, if it can be done
|
|
|
|
* by using a scroll. FIXME: We need to check that the params support
|
|
|
|
* this hack, and make sure it's done properly with any offsets etc?
|
|
|
|
*/
|
2009-02-02 13:21:13 +00:00
|
|
|
if (drawable == (GdkDrawable *)window_impl)
|
2009-01-21 17:47:06 +00:00
|
|
|
{
|
|
|
|
[window_impl->view scrollRect:NSMakeRect (xsrc, ysrc, width, height)
|
|
|
|
by:NSMakeSize (xdest - xsrc, ydest - ysrc)];
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_warning ("Drawing with window source != dest is not supported");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (GDK_IS_DRAWABLE_IMPL_QUARTZ (src))
|
2005-11-22 10:03:32 +00:00
|
|
|
src_impl = GDK_DRAWABLE_IMPL_QUARTZ (src);
|
2006-04-16 20:13:13 +00:00
|
|
|
else if (GDK_IS_PIXMAP (src))
|
2005-11-22 10:03:32 +00:00
|
|
|
src_impl = GDK_DRAWABLE_IMPL_QUARTZ (GDK_PIXMAP_OBJECT (src)->impl);
|
2009-01-21 17:47:06 +00:00
|
|
|
else
|
2006-04-16 20:13:13 +00:00
|
|
|
{
|
2009-01-21 17:47:06 +00:00
|
|
|
g_warning ("Unsupported source %s", G_OBJECT_TYPE_NAME (src));
|
2006-04-16 20:13:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-01-21 17:47:06 +00:00
|
|
|
|
|
|
|
/* Handle drawable and pixmap sources. */
|
2005-11-22 10:03:32 +00:00
|
|
|
if (src_depth == 1)
|
|
|
|
{
|
|
|
|
/* FIXME: src depth 1 is not supported yet */
|
2009-01-21 17:47:06 +00:00
|
|
|
g_warning ("Source with depth 1 unsupported");
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
else if (dest_depth != 0 && src_depth == dest_depth)
|
|
|
|
{
|
2009-02-18 15:20:06 +00:00
|
|
|
GdkPixmapImplQuartz *pixmap_impl = GDK_PIXMAP_IMPL_QUARTZ (src_impl);
|
2006-07-29 11:05:15 +00:00
|
|
|
CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
|
|
|
if (!context)
|
2009-01-21 17:47:06 +00:00
|
|
|
return;
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2007-04-06 21:12:48 +00:00
|
|
|
_gdk_quartz_gc_update_cg_context (gc, drawable, context,
|
2009-01-21 17:47:06 +00:00
|
|
|
GDK_QUARTZ_CONTEXT_STROKE);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2008-08-15 20:27:22 +00:00
|
|
|
CGContextClipToRect (context, CGRectMake (xdest, ydest, width, height));
|
|
|
|
CGContextTranslateCTM (context, xdest - xsrc, ydest - ysrc +
|
2009-02-18 15:20:06 +00:00
|
|
|
pixmap_impl->height);
|
2008-07-20 19:22:37 +00:00
|
|
|
CGContextScaleCTM (context, 1.0, -1.0);
|
|
|
|
|
2009-01-21 17:47:06 +00:00
|
|
|
CGContextDrawImage (context,
|
2009-02-18 15:20:06 +00:00
|
|
|
CGRectMake (0, 0, pixmap_impl->width, pixmap_impl->height),
|
|
|
|
pixmap_impl->image);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2006-07-29 11:05:15 +00:00
|
|
|
gdk_quartz_drawable_release_context (drawable, context);
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
g_warning ("Attempt to draw a drawable with depth %d to a drawable with depth %d",
|
2009-01-21 17:47:06 +00:00
|
|
|
src_depth, dest_depth);
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_quartz_draw_points (GdkDrawable *drawable,
|
2006-08-08 09:12:10 +00:00
|
|
|
GdkGC *gc,
|
|
|
|
GdkPoint *points,
|
|
|
|
gint npoints)
|
2005-11-22 10:03:32 +00:00
|
|
|
{
|
2006-08-07 16:45:16 +00:00
|
|
|
CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
|
2005-11-22 10:03:32 +00:00
|
|
|
int i;
|
|
|
|
|
2006-08-07 16:45:16 +00:00
|
|
|
if (!context)
|
|
|
|
return;
|
|
|
|
|
2007-04-06 21:12:48 +00:00
|
|
|
_gdk_quartz_gc_update_cg_context (gc, drawable, context,
|
|
|
|
GDK_QUARTZ_CONTEXT_STROKE |
|
|
|
|
GDK_QUARTZ_CONTEXT_FILL);
|
2006-08-07 16:45:16 +00:00
|
|
|
|
2005-11-22 10:03:32 +00:00
|
|
|
/* Just draw 1x1 rectangles */
|
|
|
|
for (i = 0; i < npoints; i++)
|
|
|
|
{
|
2006-08-08 09:12:10 +00:00
|
|
|
CGRect rect = CGRectMake (points[i].x, points[i].y, 1, 1);
|
2006-08-07 16:45:16 +00:00
|
|
|
CGContextFillRect (context, rect);
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
2006-08-07 16:45:16 +00:00
|
|
|
|
|
|
|
gdk_quartz_drawable_release_context (drawable, context);
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
2007-06-07 22:26:27 +00:00
|
|
|
static inline void
|
|
|
|
gdk_quartz_fix_cap_not_last_line (GdkGCQuartz *private,
|
|
|
|
gint x1,
|
|
|
|
gint y1,
|
|
|
|
gint x2,
|
|
|
|
gint y2,
|
|
|
|
gint *xfix,
|
|
|
|
gint *yfix)
|
|
|
|
{
|
|
|
|
*xfix = 0;
|
|
|
|
*yfix = 0;
|
|
|
|
|
|
|
|
if (private->cap_style == GDK_CAP_NOT_LAST && private->line_width == 0)
|
|
|
|
{
|
|
|
|
/* fix only vertical and horizontal lines for now */
|
|
|
|
|
|
|
|
if (y1 == y2 && x1 != x2)
|
|
|
|
{
|
|
|
|
*xfix = (x1 < x2) ? -1 : 1;
|
|
|
|
}
|
|
|
|
else if (x1 == x2 && y1 != y2)
|
|
|
|
{
|
|
|
|
*yfix = (y1 < y2) ? -1 : 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-22 10:03:32 +00:00
|
|
|
static void
|
|
|
|
gdk_quartz_draw_segments (GdkDrawable *drawable,
|
2006-08-08 09:12:10 +00:00
|
|
|
GdkGC *gc,
|
|
|
|
GdkSegment *segs,
|
|
|
|
gint nsegs)
|
2005-11-22 10:03:32 +00:00
|
|
|
{
|
2006-07-29 11:05:15 +00:00
|
|
|
CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
|
2007-06-07 22:26:27 +00:00
|
|
|
GdkGCQuartz *private;
|
2005-11-22 10:03:32 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!context)
|
|
|
|
return;
|
|
|
|
|
2007-06-07 22:26:27 +00:00
|
|
|
private = GDK_GC_QUARTZ (gc);
|
|
|
|
|
2007-04-06 21:12:48 +00:00
|
|
|
_gdk_quartz_gc_update_cg_context (gc, drawable, context,
|
|
|
|
GDK_QUARTZ_CONTEXT_STROKE);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nsegs; i++)
|
|
|
|
{
|
2007-06-07 22:26:27 +00:00
|
|
|
gint xfix, yfix;
|
|
|
|
|
|
|
|
gdk_quartz_fix_cap_not_last_line (private,
|
|
|
|
segs[i].x1, segs[i].y1,
|
|
|
|
segs[i].x2, segs[i].y2,
|
|
|
|
&xfix, &yfix);
|
|
|
|
|
2006-03-21 07:49:05 +00:00
|
|
|
CGContextMoveToPoint (context, segs[i].x1 + 0.5, segs[i].y1 + 0.5);
|
2007-06-07 22:26:27 +00:00
|
|
|
CGContextAddLineToPoint (context, segs[i].x2 + 0.5 + xfix, segs[i].y2 + 0.5 + yfix);
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CGContextStrokePath (context);
|
|
|
|
|
2006-07-29 11:05:15 +00:00
|
|
|
gdk_quartz_drawable_release_context (drawable, context);
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_quartz_draw_lines (GdkDrawable *drawable,
|
|
|
|
GdkGC *gc,
|
|
|
|
GdkPoint *points,
|
|
|
|
gint npoints)
|
|
|
|
{
|
2006-07-29 11:05:15 +00:00
|
|
|
CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
|
2007-06-07 22:26:27 +00:00
|
|
|
GdkGCQuartz *private;
|
|
|
|
gint xfix, yfix;
|
|
|
|
gint i;
|
2006-01-18 09:47:56 +00:00
|
|
|
|
|
|
|
if (!context)
|
|
|
|
return;
|
|
|
|
|
2007-06-07 22:26:27 +00:00
|
|
|
private = GDK_GC_QUARTZ (gc);
|
|
|
|
|
2007-04-06 21:12:48 +00:00
|
|
|
_gdk_quartz_gc_update_cg_context (gc, drawable, context,
|
2007-06-07 22:26:27 +00:00
|
|
|
GDK_QUARTZ_CONTEXT_STROKE);
|
2006-09-21 17:05:33 +00:00
|
|
|
|
|
|
|
CGContextMoveToPoint (context, points[0].x + 0.5, points[0].y + 0.5);
|
|
|
|
|
2007-06-07 22:26:27 +00:00
|
|
|
for (i = 1; i < npoints - 1; i++)
|
2006-09-21 17:05:33 +00:00
|
|
|
CGContextAddLineToPoint (context, points[i].x + 0.5, points[i].y + 0.5);
|
|
|
|
|
2007-06-07 22:26:27 +00:00
|
|
|
gdk_quartz_fix_cap_not_last_line (private,
|
|
|
|
points[npoints - 2].x, points[npoints - 2].y,
|
|
|
|
points[npoints - 1].x, points[npoints - 1].y,
|
|
|
|
&xfix, &yfix);
|
|
|
|
|
|
|
|
CGContextAddLineToPoint (context,
|
|
|
|
points[npoints - 1].x + 0.5 + xfix,
|
|
|
|
points[npoints - 1].y + 0.5 + yfix);
|
|
|
|
|
2006-01-18 09:47:56 +00:00
|
|
|
CGContextStrokePath (context);
|
|
|
|
|
2006-07-29 11:05:15 +00:00
|
|
|
gdk_quartz_drawable_release_context (drawable, context);
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_quartz_draw_pixbuf (GdkDrawable *drawable,
|
2006-08-08 09:12:10 +00:00
|
|
|
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)
|
2005-11-22 10:03:32 +00:00
|
|
|
{
|
2006-07-29 11:05:15 +00:00
|
|
|
CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
|
2005-11-22 10:03:32 +00:00
|
|
|
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);
|
|
|
|
|
2007-04-06 21:12:48 +00:00
|
|
|
_gdk_quartz_gc_update_cg_context (gc, drawable, context,
|
|
|
|
GDK_QUARTZ_CONTEXT_STROKE);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2006-03-21 07:49:05 +00:00
|
|
|
CGContextDrawImage (context, CGRectMake (0, 0, pixbuf_width, pixbuf_height), image);
|
2005-11-22 10:03:32 +00:00
|
|
|
CGImageRelease (image);
|
|
|
|
|
2006-07-29 11:05:15 +00:00
|
|
|
gdk_quartz_drawable_release_context (drawable, context);
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_quartz_draw_image (GdkDrawable *drawable,
|
2006-08-08 09:12:10 +00:00
|
|
|
GdkGC *gc,
|
|
|
|
GdkImage *image,
|
|
|
|
gint xsrc,
|
|
|
|
gint ysrc,
|
|
|
|
gint xdest,
|
|
|
|
gint ydest,
|
|
|
|
gint width,
|
|
|
|
gint height)
|
2005-11-22 10:03:32 +00:00
|
|
|
{
|
2006-07-29 11:05:15 +00:00
|
|
|
CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE);
|
2005-11-22 10:03:32 +00:00
|
|
|
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);
|
|
|
|
|
2007-04-06 21:12:48 +00:00
|
|
|
_gdk_quartz_gc_update_cg_context (gc, drawable, context,
|
|
|
|
GDK_QUARTZ_CONTEXT_STROKE);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2006-07-29 11:05:15 +00:00
|
|
|
gdk_quartz_drawable_release_context (drawable, context);
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2009-07-01 17:50:49 +00:00
|
|
|
drawable_class->draw_drawable_with_src = gdk_quartz_draw_drawable;
|
2005-11-22 10:03:32 +00:00
|
|
|
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;
|
|
|
|
|
2007-04-06 21:12:48 +00:00
|
|
|
drawable_class->_copy_to_image = _gdk_quartz_image_copy_to_image;
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GType
|
|
|
|
gdk_drawable_impl_quartz_get_type (void)
|
|
|
|
{
|
|
|
|
static GType object_type = 0;
|
|
|
|
|
|
|
|
if (!object_type)
|
|
|
|
{
|
2009-11-06 00:21:09 +00:00
|
|
|
const GTypeInfo object_info =
|
2005-11-22 10:03:32 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-07-20 16:14:35 +00:00
|
|
|
CGContextRef
|
2006-08-08 09:12:10 +00:00
|
|
|
gdk_quartz_drawable_get_context (GdkDrawable *drawable,
|
|
|
|
gboolean antialias)
|
2005-11-22 10:03:32 +00:00
|
|
|
{
|
2008-07-20 16:14:35 +00:00
|
|
|
if (!GDK_DRAWABLE_IMPL_QUARTZ_GET_CLASS (drawable)->get_context)
|
2007-03-10 21:58:49 +00:00
|
|
|
{
|
2008-07-20 16:14:35 +00:00
|
|
|
g_warning ("%s doesn't implement GdkDrawableImplQuartzClass::get_context()",
|
|
|
|
G_OBJECT_TYPE_NAME (drawable));
|
|
|
|
return NULL;
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
2008-07-20 16:14:35 +00:00
|
|
|
return GDK_DRAWABLE_IMPL_QUARTZ_GET_CLASS (drawable)->get_context (drawable, antialias);
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
/* Help preventing "beam sync penalty" where CG makes all graphics code
|
2009-02-09 14:46:30 +00:00
|
|
|
* block until the next vsync if we try to flush (including call display on
|
|
|
|
* a view) too often. We do this by limiting the manual flushing done
|
2009-02-18 15:20:06 +00:00
|
|
|
* outside of expose calls to less than some frequency when measured over
|
|
|
|
* the last 4 flushes. This is a bit arbitray, but seems to make it possible
|
|
|
|
* for some quick manual flushes (such as gtkruler or gimp's marching ants)
|
|
|
|
* without hitting the max flush frequency.
|
2009-02-09 14:46:30 +00:00
|
|
|
*
|
2009-02-18 15:20:06 +00:00
|
|
|
* If drawable NULL, no flushing is done, only registering that a flush was
|
|
|
|
* done externally.
|
2009-02-09 14:46:30 +00:00
|
|
|
*/
|
|
|
|
void
|
2009-02-18 15:20:06 +00:00
|
|
|
_gdk_quartz_drawable_flush (GdkDrawable *drawable)
|
2009-02-09 14:46:30 +00:00
|
|
|
{
|
|
|
|
static struct timeval prev_tv;
|
2009-02-18 15:20:06 +00:00
|
|
|
static gint intervals[4];
|
|
|
|
static gint index;
|
2009-02-09 14:46:30 +00:00
|
|
|
struct timeval tv;
|
|
|
|
gint ms;
|
|
|
|
|
|
|
|
gettimeofday (&tv, NULL);
|
2009-02-18 15:20:06 +00:00
|
|
|
ms = (tv.tv_sec - prev_tv.tv_sec) * 1000 + (tv.tv_usec - prev_tv.tv_usec) / 1000;
|
|
|
|
intervals[index++ % 4] = ms;
|
2009-02-09 14:46:30 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
if (drawable)
|
2009-02-09 14:46:30 +00:00
|
|
|
{
|
2009-02-18 15:20:06 +00:00
|
|
|
ms = intervals[0] + intervals[1] + intervals[2] + intervals[3];
|
2009-02-09 14:46:30 +00:00
|
|
|
|
2009-02-18 15:20:06 +00:00
|
|
|
/* ~25Hz on average. */
|
|
|
|
if (ms > 4*40)
|
2009-02-09 14:46:30 +00:00
|
|
|
{
|
2009-02-18 15:20:06 +00:00
|
|
|
if (GDK_IS_WINDOW_IMPL_QUARTZ (drawable))
|
|
|
|
{
|
|
|
|
GdkWindowImplQuartz *window_impl = GDK_WINDOW_IMPL_QUARTZ (drawable);
|
|
|
|
|
|
|
|
[window_impl->toplevel flushWindow];
|
|
|
|
}
|
|
|
|
|
2009-02-09 14:46:30 +00:00
|
|
|
prev_tv = tv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
prev_tv = tv;
|
|
|
|
}
|
|
|
|
|
2005-11-22 10:03:32 +00:00
|
|
|
void
|
2007-03-10 21:58:49 +00:00
|
|
|
gdk_quartz_drawable_release_context (GdkDrawable *drawable,
|
|
|
|
CGContextRef cg_context)
|
2005-11-22 10:03:32 +00:00
|
|
|
{
|
|
|
|
if (GDK_IS_WINDOW_IMPL_QUARTZ (drawable))
|
|
|
|
{
|
2007-03-10 21:58:49 +00:00
|
|
|
GdkWindowImplQuartz *window_impl = GDK_WINDOW_IMPL_QUARTZ (drawable);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2007-03-10 21:58:49 +00:00
|
|
|
CGContextRestoreGState (cg_context);
|
|
|
|
CGContextSetAllowsAntialiasing (cg_context, TRUE);
|
2005-11-22 10:03:32 +00:00
|
|
|
|
2007-05-05 10:19:39 +00:00
|
|
|
/* See comment in gdk_quartz_drawable_get_context(). */
|
|
|
|
if (window_impl->in_paint_rect_count == 0)
|
2009-02-09 14:46:30 +00:00
|
|
|
{
|
2009-02-18 15:20:06 +00:00
|
|
|
_gdk_quartz_drawable_flush (drawable);
|
2009-02-09 14:46:30 +00:00
|
|
|
[window_impl->view unlockFocus];
|
|
|
|
}
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
|
|
|
else if (GDK_IS_PIXMAP_IMPL_QUARTZ (drawable))
|
2007-03-10 21:58:49 +00:00
|
|
|
CGContextRelease (cg_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gdk_quartz_drawable_finish (GdkDrawable *drawable)
|
|
|
|
{
|
|
|
|
GdkDrawableImplQuartz *impl = GDK_DRAWABLE_IMPL_QUARTZ (drawable);
|
|
|
|
|
|
|
|
if (impl->cairo_surface)
|
2005-11-22 10:03:32 +00:00
|
|
|
{
|
2007-03-10 21:58:49 +00:00
|
|
|
cairo_surface_finish (impl->cairo_surface);
|
|
|
|
cairo_surface_set_user_data (impl->cairo_surface, &gdk_quartz_cairo_key,
|
|
|
|
NULL, NULL);
|
|
|
|
impl->cairo_surface = NULL;
|
2005-11-22 10:03:32 +00:00
|
|
|
}
|
2007-03-10 21:58:49 +00:00
|
|
|
}
|