1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2005 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "gdkdrawable.h"
22 #include "gdkinternals.h"
27 * @drawable: a #GdkDrawable
29 * Creates a Cairo context for drawing to @drawable.
32 * Note that due to double-buffering, Cairo contexts created
33 * in a GTK+ expose event handler cannot be cached and reused
34 * between different expose events.
37 * Return value: A newly created Cairo context. Free with
38 * cairo_destroy() when you are done drawing.
43 gdk_cairo_create (GdkDrawable *drawable)
45 cairo_surface_t *surface;
48 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
50 surface = _gdk_drawable_ref_cairo_surface (drawable);
51 cr = cairo_create (surface);
53 if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
54 GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
56 cairo_surface_destroy (surface);
62 * gdk_cairo_reset_clip:
64 * @drawable: a #GdkDrawable
66 * Resets the clip region for a Cairo context created by gdk_cairo_create().
68 * This resets the clip region to the "empty" state for the given drawable.
69 * This is required for non-native windows since a direct call to
70 * cairo_reset_clip() would unset the clip region inherited from the
71 * drawable (i.e. the window clip region), and thus let you e.g.
72 * draw outside your window.
74 * This is rarely needed though, since most code just create a new cairo_t
75 * using gdk_cairo_create() each time they want to draw something.
80 gdk_cairo_reset_clip (cairo_t *cr,
81 GdkDrawable *drawable)
83 cairo_reset_clip (cr);
85 if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
86 GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
90 * gdk_cairo_set_source_color:
94 * Sets the specified #GdkColor as the source color of @cr.
99 gdk_cairo_set_source_color (cairo_t *cr,
100 const GdkColor *color)
102 g_return_if_fail (cr != NULL);
103 g_return_if_fail (color != NULL);
105 cairo_set_source_rgb (cr,
107 color->green / 65535.,
108 color->blue / 65535.);
112 * gdk_cairo_rectangle:
114 * @rectangle: a #GdkRectangle
116 * Adds the given rectangle to the current path of @cr.
121 gdk_cairo_rectangle (cairo_t *cr,
122 const GdkRectangle *rectangle)
124 g_return_if_fail (cr != NULL);
125 g_return_if_fail (rectangle != NULL);
128 rectangle->x, rectangle->y,
129 rectangle->width, rectangle->height);
135 * @region: a #GdkRegion
137 * Adds the given region to the current path of @cr.
142 gdk_cairo_region (cairo_t *cr,
143 const GdkRegion *region)
145 cairo_rectangle_int_t box;
148 g_return_if_fail (cr != NULL);
149 g_return_if_fail (region != NULL);
151 n_boxes = cairo_region_num_rectangles (region);
153 for (i = 0; i < n_boxes; i++)
155 cairo_region_get_rectangle (region, i, &box);
156 cairo_rectangle (cr, box.x, box.y, box.width, box.height);
161 * gdk_cairo_set_source_pixbuf:
162 * @cr: a #Cairo context
163 * @pixbuf: a #GdkPixbuf
164 * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
165 * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
167 * Sets the given pixbuf as the source pattern for the Cairo context.
168 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
169 * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y
174 gdk_cairo_set_source_pixbuf (cairo_t *cr,
175 const GdkPixbuf *pixbuf,
179 gint width = gdk_pixbuf_get_width (pixbuf);
180 gint height = gdk_pixbuf_get_height (pixbuf);
181 guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
182 int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
183 int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
185 guchar *cairo_pixels;
186 cairo_format_t format;
187 cairo_surface_t *surface;
188 static const cairo_user_data_key_t key;
192 format = CAIRO_FORMAT_RGB24;
194 format = CAIRO_FORMAT_ARGB32;
196 cairo_stride = cairo_format_stride_for_width (format, width);
197 cairo_pixels = g_malloc (height * cairo_stride);
198 surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
200 width, height, cairo_stride);
202 cairo_surface_set_user_data (surface, &key,
203 cairo_pixels, (cairo_destroy_func_t)g_free);
205 for (j = height; j; j--)
207 guchar *p = gdk_pixels;
208 guchar *q = cairo_pixels;
212 guchar *end = p + 3 * width;
216 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
231 guchar *end = p + 4 * width;
234 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
238 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
239 MULT(q[0], p[2], p[3], t1);
240 MULT(q[1], p[1], p[3], t2);
241 MULT(q[2], p[0], p[3], t3);
245 MULT(q[1], p[0], p[3], t1);
246 MULT(q[2], p[1], p[3], t2);
247 MULT(q[3], p[2], p[3], t3);
257 gdk_pixels += gdk_rowstride;
258 cairo_pixels += cairo_stride;
261 cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
262 cairo_surface_destroy (surface);
266 * gdk_cairo_set_source_pixmap:
267 * @cr: a #Cairo context
268 * @pixmap: a #GdkPixmap
269 * @pixmap_x: X coordinate of location to place upper left corner of @pixmap
270 * @pixmap_y: Y coordinate of location to place upper left corner of @pixmap
272 * Sets the given pixmap as the source pattern for the Cairo context.
273 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
274 * so that the origin of @pixmap is @pixmap_x, @pixmap_y
279 gdk_cairo_set_source_pixmap (cairo_t *cr,
284 cairo_surface_t *surface;
286 surface = _gdk_drawable_ref_cairo_surface (GDK_DRAWABLE (pixmap));
287 cairo_set_source_surface (cr, surface, pixmap_x, pixmap_y);
288 cairo_surface_destroy (surface);
292 #define __GDK_CAIRO_C__
293 #include "gdkaliasdef.c"