]> Pileus Git - ~andy/gtk/blob - gdk/gdkcairo.c
Fix GtkTooltip destroy the custom widget
[~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   cairo_surface_destroy (surface);
54
55   return cr;
56 }
57
58 /**
59  * gdk_cairo_set_source_color:
60  * @cr: a #cairo_t
61  * @color: a #GdkColor
62  * 
63  * Sets the specified #GdkColor as the source color of @cr.
64  *
65  * Since: 2.8
66  **/
67 void
68 gdk_cairo_set_source_color (cairo_t        *cr,
69                             const GdkColor *color)
70 {
71   g_return_if_fail (cr != NULL);
72   g_return_if_fail (color != NULL);
73     
74   cairo_set_source_rgb (cr,
75                         color->red / 65535.,
76                         color->green / 65535.,
77                         color->blue / 65535.);
78 }
79
80 /**
81  * gdk_cairo_rectangle:
82  * @cr: a #cairo_t
83  * @rectangle: a #GdkRectangle
84  * 
85  * Adds the given rectangle to the current path of @cr.
86  *
87  * Since: 2.8
88  **/
89 void
90 gdk_cairo_rectangle (cairo_t            *cr,
91                      const GdkRectangle *rectangle)
92 {
93   g_return_if_fail (cr != NULL);
94   g_return_if_fail (rectangle != NULL);
95
96   cairo_rectangle (cr,
97                    rectangle->x,     rectangle->y,
98                    rectangle->width, rectangle->height);
99 }
100
101 /**
102  * gdk_cairo_region:
103  * @cr: a #cairo_t
104  * @region: a #GdkRegion
105  * 
106  * Adds the given region to the current path of @cr.
107  *
108  * Since: 2.8
109  **/
110 void
111 gdk_cairo_region (cairo_t         *cr,
112                   const GdkRegion *region)
113 {
114   GdkRegionBox *boxes;
115   gint n_boxes, i;
116
117   g_return_if_fail (cr != NULL);
118   g_return_if_fail (region != NULL);
119
120   boxes = region->rects;
121   n_boxes = region->numRects;
122
123   for (i = 0; i < n_boxes; i++)
124     cairo_rectangle (cr,
125                      boxes[i].x1,
126                      boxes[i].y1,
127                      boxes[i].x2 - boxes[i].x1,
128                      boxes[i].y2 - boxes[i].y1);
129 }
130
131 /**
132  * gdk_cairo_set_source_pixbuf:
133  * @cr: a #Cairo context
134  * @pixbuf: a #GdkPixbuf
135  * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
136  * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
137  * 
138  * Sets the given pixbuf as the source pattern for the Cairo context.
139  * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
140  * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y
141  *
142  * Since: 2.8
143  **/
144 void
145 gdk_cairo_set_source_pixbuf (cairo_t         *cr,
146                              const GdkPixbuf *pixbuf,
147                              double           pixbuf_x,
148                              double           pixbuf_y)
149 {
150   gint width = gdk_pixbuf_get_width (pixbuf);
151   gint height = gdk_pixbuf_get_height (pixbuf);
152   guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
153   int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
154   int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
155   int cairo_stride;
156   guchar *cairo_pixels;
157   cairo_format_t format;
158   cairo_surface_t *surface;
159   static const cairo_user_data_key_t key;
160   int j;
161
162   if (n_channels == 3)
163     format = CAIRO_FORMAT_RGB24;
164   else
165     format = CAIRO_FORMAT_ARGB32;
166
167   cairo_stride = cairo_format_stride_for_width (format, width);
168   cairo_pixels = g_malloc (height * cairo_stride);
169   surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
170                                                  format,
171                                                  width, height, cairo_stride);
172
173   cairo_surface_set_user_data (surface, &key,
174                                cairo_pixels, (cairo_destroy_func_t)g_free);
175
176   for (j = height; j; j--)
177     {
178       guchar *p = gdk_pixels;
179       guchar *q = cairo_pixels;
180
181       if (n_channels == 3)
182         {
183           guchar *end = p + 3 * width;
184           
185           while (p < end)
186             {
187 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
188               q[0] = p[2];
189               q[1] = p[1];
190               q[2] = p[0];
191 #else     
192               q[1] = p[0];
193               q[2] = p[1];
194               q[3] = p[2];
195 #endif
196               p += 3;
197               q += 4;
198             }
199         }
200       else
201         {
202           guchar *end = p + 4 * width;
203           guint t1,t2,t3;
204             
205 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
206
207           while (p < end)
208             {
209 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
210               MULT(q[0], p[2], p[3], t1);
211               MULT(q[1], p[1], p[3], t2);
212               MULT(q[2], p[0], p[3], t3);
213               q[3] = p[3];
214 #else     
215               q[0] = p[3];
216               MULT(q[1], p[0], p[3], t1);
217               MULT(q[2], p[1], p[3], t2);
218               MULT(q[3], p[2], p[3], t3);
219 #endif
220               
221               p += 4;
222               q += 4;
223             }
224           
225 #undef MULT
226         }
227
228       gdk_pixels += gdk_rowstride;
229       cairo_pixels += cairo_stride;
230     }
231
232   cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
233   cairo_surface_destroy (surface);
234 }
235
236 /**
237  * gdk_cairo_set_source_pixmap:
238  * @cr: a #Cairo context
239  * @pixmap: a #GdkPixmap
240  * @pixmap_x: X coordinate of location to place upper left corner of @pixmap
241  * @pixmap_y: Y coordinate of location to place upper left corner of @pixmap
242  * 
243  * Sets the given pixmap as the source pattern for the Cairo context.
244  * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
245  * so that the origin of @pixmap is @pixmap_x, @pixmap_y
246  *
247  * Since: 2.10
248  **/
249 void
250 gdk_cairo_set_source_pixmap (cairo_t   *cr,
251                              GdkPixmap *pixmap,
252                              double     pixmap_x,
253                              double     pixmap_y)
254 {
255   cairo_surface_t *surface;
256   
257   surface = _gdk_drawable_ref_cairo_surface (GDK_DRAWABLE (pixmap));
258   cairo_set_source_surface (cr, surface, pixmap_x, pixmap_y);
259   cairo_surface_destroy (surface);
260 }
261
262
263 #define __GDK_CAIRO_C__
264 #include "gdkaliasdef.c"