]> Pileus Git - ~andy/gtk/blob - gdk/gdkcairo.c
Updated Hungarian translation
[~andy/gtk] / gdk / gdkcairo.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2005 Red Hat, Inc. 
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "gdkcairo.h"
21 #include "gdkdrawable.h"
22 #include "gdkinternals.h"
23 #include "gdkregion-generic.h"
24 #include "gdkalias.h"
25
26 /**
27  * gdk_cairo_create:
28  * @drawable: a #GdkDrawable
29  * 
30  * Creates a Cairo context for drawing to @drawable.
31  *
32  * <note><para>
33  * Note that due to double-buffering, Cairo contexts created 
34  * in a GTK+ expose event handler cannot be cached and reused 
35  * between different expose events. 
36  * </para></note>
37  *
38  * Return value: A newly created Cairo context. Free with
39  *  cairo_destroy() when you are done drawing.
40  * 
41  * Since: 2.8
42  **/
43 cairo_t *
44 gdk_cairo_create (GdkDrawable *drawable)
45 {
46   cairo_surface_t *surface;
47   cairo_t *cr;
48     
49   g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
50
51   surface = _gdk_drawable_ref_cairo_surface (drawable);
52   cr = cairo_create (surface);
53
54   if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
55     GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
56     
57   cairo_surface_destroy (surface);
58
59   return cr;
60 }
61
62 /**
63  * gdk_cairo_reset_clip:
64  * @cr: a #cairo_t
65  * @drawable: a #GdkDrawable
66  *
67  * Resets the clip region for a Cairo context created by gdk_cairo_create().
68  *
69  * This resets the clip region to the "empty" state for the given drawable.
70  * This is required for non-native windows since a direct call to
71  * cairo_reset_clip() would unset the clip region inherited from the
72  * drawable (i.e. the window clip region), and thus let you e.g.
73  * draw outside your window.
74  *
75  * This is rarely needed though, since most code just create a new cairo_t
76  * using gdk_cairo_create() each time they want to draw something.
77  *
78  * Since: 2.18
79  **/
80 void
81 gdk_cairo_reset_clip (cairo_t            *cr,
82                       GdkDrawable        *drawable)
83 {
84   cairo_reset_clip (cr);
85
86   if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
87     GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
88 }
89
90 /**
91  * gdk_cairo_set_source_color:
92  * @cr: a #cairo_t
93  * @color: a #GdkColor
94  * 
95  * Sets the specified #GdkColor as the source color of @cr.
96  *
97  * Since: 2.8
98  **/
99 void
100 gdk_cairo_set_source_color (cairo_t        *cr,
101                             const GdkColor *color)
102 {
103   g_return_if_fail (cr != NULL);
104   g_return_if_fail (color != NULL);
105     
106   cairo_set_source_rgb (cr,
107                         color->red / 65535.,
108                         color->green / 65535.,
109                         color->blue / 65535.);
110 }
111
112 /**
113  * gdk_cairo_rectangle:
114  * @cr: a #cairo_t
115  * @rectangle: a #GdkRectangle
116  * 
117  * Adds the given rectangle to the current path of @cr.
118  *
119  * Since: 2.8
120  **/
121 void
122 gdk_cairo_rectangle (cairo_t            *cr,
123                      const GdkRectangle *rectangle)
124 {
125   g_return_if_fail (cr != NULL);
126   g_return_if_fail (rectangle != NULL);
127
128   cairo_rectangle (cr,
129                    rectangle->x,     rectangle->y,
130                    rectangle->width, rectangle->height);
131 }
132
133 /**
134  * gdk_cairo_region:
135  * @cr: a #cairo_t
136  * @region: a #GdkRegion
137  * 
138  * Adds the given region to the current path of @cr.
139  *
140  * Since: 2.8
141  **/
142 void
143 gdk_cairo_region (cairo_t         *cr,
144                   const GdkRegion *region)
145 {
146   GdkRegionBox *boxes;
147   gint n_boxes, i;
148
149   g_return_if_fail (cr != NULL);
150   g_return_if_fail (region != NULL);
151
152   boxes = region->rects;
153   n_boxes = region->numRects;
154
155   for (i = 0; i < n_boxes; i++)
156     cairo_rectangle (cr,
157                      boxes[i].x1,
158                      boxes[i].y1,
159                      boxes[i].x2 - boxes[i].x1,
160                      boxes[i].y2 - boxes[i].y1);
161 }
162
163 /**
164  * gdk_cairo_set_source_pixbuf:
165  * @cr: a #Cairo context
166  * @pixbuf: a #GdkPixbuf
167  * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
168  * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
169  * 
170  * Sets the given pixbuf as the source pattern for the Cairo context.
171  * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
172  * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y
173  *
174  * Since: 2.8
175  **/
176 void
177 gdk_cairo_set_source_pixbuf (cairo_t         *cr,
178                              const GdkPixbuf *pixbuf,
179                              double           pixbuf_x,
180                              double           pixbuf_y)
181 {
182   gint width = gdk_pixbuf_get_width (pixbuf);
183   gint height = gdk_pixbuf_get_height (pixbuf);
184   guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
185   int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
186   int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
187   int cairo_stride;
188   guchar *cairo_pixels;
189   cairo_format_t format;
190   cairo_surface_t *surface;
191   static const cairo_user_data_key_t key;
192   int j;
193
194   if (n_channels == 3)
195     format = CAIRO_FORMAT_RGB24;
196   else
197     format = CAIRO_FORMAT_ARGB32;
198
199   cairo_stride = cairo_format_stride_for_width (format, width);
200   cairo_pixels = g_malloc (height * cairo_stride);
201   surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
202                                                  format,
203                                                  width, height, cairo_stride);
204
205   cairo_surface_set_user_data (surface, &key,
206                                cairo_pixels, (cairo_destroy_func_t)g_free);
207
208   for (j = height; j; j--)
209     {
210       guchar *p = gdk_pixels;
211       guchar *q = cairo_pixels;
212
213       if (n_channels == 3)
214         {
215           guchar *end = p + 3 * width;
216           
217           while (p < end)
218             {
219 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
220               q[0] = p[2];
221               q[1] = p[1];
222               q[2] = p[0];
223 #else     
224               q[1] = p[0];
225               q[2] = p[1];
226               q[3] = p[2];
227 #endif
228               p += 3;
229               q += 4;
230             }
231         }
232       else
233         {
234           guchar *end = p + 4 * width;
235           guint t1,t2,t3;
236             
237 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
238
239           while (p < end)
240             {
241 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
242               MULT(q[0], p[2], p[3], t1);
243               MULT(q[1], p[1], p[3], t2);
244               MULT(q[2], p[0], p[3], t3);
245               q[3] = p[3];
246 #else     
247               q[0] = p[3];
248               MULT(q[1], p[0], p[3], t1);
249               MULT(q[2], p[1], p[3], t2);
250               MULT(q[3], p[2], p[3], t3);
251 #endif
252               
253               p += 4;
254               q += 4;
255             }
256           
257 #undef MULT
258         }
259
260       gdk_pixels += gdk_rowstride;
261       cairo_pixels += cairo_stride;
262     }
263
264   cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
265   cairo_surface_destroy (surface);
266 }
267
268 /**
269  * gdk_cairo_set_source_pixmap:
270  * @cr: a #Cairo context
271  * @pixmap: a #GdkPixmap
272  * @pixmap_x: X coordinate of location to place upper left corner of @pixmap
273  * @pixmap_y: Y coordinate of location to place upper left corner of @pixmap
274  * 
275  * Sets the given pixmap as the source pattern for the Cairo context.
276  * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
277  * so that the origin of @pixmap is @pixmap_x, @pixmap_y
278  *
279  * Since: 2.10
280  **/
281 void
282 gdk_cairo_set_source_pixmap (cairo_t   *cr,
283                              GdkPixmap *pixmap,
284                              double     pixmap_x,
285                              double     pixmap_y)
286 {
287   cairo_surface_t *surface;
288   
289   surface = _gdk_drawable_ref_cairo_surface (GDK_DRAWABLE (pixmap));
290   cairo_set_source_surface (cr, surface, pixmap_x, pixmap_y);
291   cairo_surface_destroy (surface);
292 }
293
294
295 #define __GDK_CAIRO_C__
296 #include "gdkaliasdef.c"