3 * Copyright (C) 2005 Imendio AB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gdkpixmap.h"
24 #include "gdkprivate-quartz.h"
26 static gpointer parent_class;
29 gdk_pixmap_impl_quartz_init (GdkPixmapImplQuartz *impl)
34 gdk_pixmap_impl_quartz_get_size (GdkDrawable *drawable,
39 *width = GDK_PIXMAP_IMPL_QUARTZ (drawable)->width;
41 *height = GDK_PIXMAP_IMPL_QUARTZ (drawable)->height;
45 gdk_pixmap_impl_quartz_get_context (GdkDrawable *drawable,
48 GdkPixmapImplQuartz *impl = GDK_PIXMAP_IMPL_QUARTZ (drawable);
49 CGContextRef cg_context;
52 cg_context = CGBitmapContextCreate (impl->data,
53 CGImageGetWidth (impl->image),
54 CGImageGetHeight (impl->image),
55 CGImageGetBitsPerComponent (impl->image),
56 CGImageGetBytesPerRow (impl->image),
57 CGImageGetColorSpace (impl->image),
58 CGImageGetBitmapInfo (impl->image));
59 CGContextSetAllowsAntialiasing (cg_context, antialias);
61 /* convert coordinates from core graphics to gtk+ */
62 height = CGImageGetHeight (impl->image);
64 CGContextTranslateCTM (cg_context, 0, height);
65 CGContextScaleCTM (cg_context, 1.0, -1.0);
71 gdk_pixmap_impl_quartz_finalize (GObject *object)
73 GdkPixmapImplQuartz *impl = GDK_PIXMAP_IMPL_QUARTZ (object);
75 CGImageRelease (impl->image);
77 _gdk_quartz_drawable_finish (GDK_DRAWABLE (impl));
79 G_OBJECT_CLASS (parent_class)->finalize (object);
83 gdk_pixmap_impl_quartz_class_init (GdkPixmapImplQuartzClass *klass)
85 GObjectClass *object_class = G_OBJECT_CLASS (klass);
86 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
87 GdkDrawableImplQuartzClass *drawable_quartz_class = GDK_DRAWABLE_IMPL_QUARTZ_CLASS (klass);
89 parent_class = g_type_class_peek_parent (klass);
91 object_class->finalize = gdk_pixmap_impl_quartz_finalize;
93 drawable_class->get_size = gdk_pixmap_impl_quartz_get_size;
95 drawable_quartz_class->get_context = gdk_pixmap_impl_quartz_get_context;
99 _gdk_pixmap_impl_quartz_get_type (void)
101 static GType object_type = 0;
105 static const GTypeInfo object_info =
107 sizeof (GdkPixmapImplQuartzClass),
108 (GBaseInitFunc) NULL,
109 (GBaseFinalizeFunc) NULL,
110 (GClassInitFunc) gdk_pixmap_impl_quartz_class_init,
111 NULL, /* class_finalize */
112 NULL, /* class_data */
113 sizeof (GdkPixmapImplQuartz),
115 (GInstanceInitFunc) gdk_pixmap_impl_quartz_init
118 object_type = g_type_register_static (GDK_TYPE_DRAWABLE_IMPL_QUARTZ,
119 "GdkPixmapImplQuartz",
128 _gdk_pixmap_impl_get_type (void)
130 return _gdk_pixmap_impl_quartz_get_type ();
134 data_provider_release (void *info, const void *data, size_t size)
140 _gdk_pixmap_new (GdkDrawable *drawable,
146 GdkDrawableImplQuartz *draw_impl;
147 GdkPixmapImplQuartz *pix_impl;
149 CGColorSpaceRef colorspace;
150 CGDataProviderRef data_provider;
151 CGImageAlphaInfo alpha_info;
152 gint bytes_per_row, bits_per_pixel;
154 g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
155 g_return_val_if_fail ((drawable != NULL) || (depth != -1), NULL);
156 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
158 if (GDK_IS_WINDOW (drawable) && GDK_WINDOW_DESTROYED (drawable))
162 drawable = gdk_screen_get_root_window (gdk_screen_get_default ());
164 window_depth = gdk_drawable_get_depth (GDK_DRAWABLE (drawable));
167 depth = window_depth;
172 alpha_info = kCGImageAlphaNoneSkipLast;
173 bytes_per_row = width * 4;
175 colorspace = CGColorSpaceCreateDeviceRGB ();
178 alpha_info = kCGImageAlphaPremultipliedFirst;
179 bytes_per_row = width * 4;
181 colorspace = CGColorSpaceCreateDeviceRGB ();
184 alpha_info = kCGImageAlphaNone;
185 bytes_per_row = width;
187 colorspace = CGColorSpaceCreateDeviceGray ();
190 g_warning ("Unsupported bit depth %d\n", depth);
194 pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
195 draw_impl = GDK_DRAWABLE_IMPL_QUARTZ (GDK_PIXMAP_OBJECT (pixmap)->impl);
196 pix_impl = GDK_PIXMAP_IMPL_QUARTZ (GDK_PIXMAP_OBJECT (pixmap)->impl);
197 draw_impl->wrapper = GDK_DRAWABLE (pixmap);
199 g_assert (depth == 24 || depth == 32 || depth == 1);
201 pix_impl->data = g_malloc (height * bytes_per_row);
202 data_provider = CGDataProviderCreateWithData (pix_impl->data, pix_impl->data,
203 height * bytes_per_row, data_provider_release);
204 pix_impl->image = CGImageCreate (width, height, 8, bits_per_pixel,
205 bytes_per_row, colorspace,
207 data_provider, NULL, FALSE,
208 kCGRenderingIntentDefault);
209 CGDataProviderRelease (data_provider);
210 CGColorSpaceRelease (colorspace);
212 pix_impl->width = width;
213 pix_impl->height = height;
214 GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
216 if (depth == window_depth) {
217 GdkColormap *colormap = gdk_drawable_get_colormap (drawable);
220 gdk_drawable_set_colormap (pixmap, colormap);
227 _gdk_bitmap_create_from_data (GdkDrawable *window,
233 GdkPixmapImplQuartz *impl;
236 g_return_val_if_fail (data != NULL, NULL);
237 g_return_val_if_fail ((width != 0) && (height != 0), NULL);
238 g_return_val_if_fail (window == NULL || GDK_IS_DRAWABLE (window), NULL);
240 pixmap = gdk_pixmap_new (window, width, height, 1);
241 impl = GDK_PIXMAP_IMPL_QUARTZ (GDK_PIXMAP_OBJECT (pixmap)->impl);
243 g_assert (CGImageGetBytesPerRow (impl->image) == width);
245 /* Bytes per line: Each line consumes an integer number of bytes, possibly
246 * ignoring any excess bits. */
247 bpl = (width + 7) / 8;
248 for (y = 0; y < height; y++)
250 guchar *dst = impl->data + y * width;
251 const gchar *src = data + (y * bpl);
252 for (x = 0; x < width; x++)
254 if ((src[x / 8] >> x % 8) & 1)
267 _gdk_pixmap_create_from_data (GdkDrawable *drawable,
275 /* FIXME: Implement */
280 gdk_pixmap_foreign_new_for_display (GdkDisplay *display,
281 GdkNativeWindow anid)
287 gdk_pixmap_foreign_new (GdkNativeWindow anid)
293 gdk_pixmap_foreign_new_for_screen (GdkScreen *screen,
294 GdkNativeWindow anid,
303 gdk_pixmap_lookup (GdkNativeWindow anid)
309 gdk_pixmap_lookup_for_display (GdkDisplay *display, GdkNativeWindow anid)
311 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);