]> Pileus Git - ~andy/gtk/blob - gdk/gdkgc.c
GtkToolPalette: docs: Minor English corrections.
[~andy/gtk] / gdk / gdkgc.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28 #include <string.h>
29
30 #include "gdkcairo.h"
31 #include "gdkgc.h"
32 #include "gdkinternals.h"
33 #include "gdkpixmap.h"
34 #include "gdkrgb.h"
35 #include "gdkprivate.h"
36 #include "gdkalias.h"
37
38 static void gdk_gc_finalize   (GObject      *object);
39
40 typedef struct _GdkGCPrivate GdkGCPrivate;
41
42 struct _GdkGCPrivate
43 {
44   GdkRegion *clip_region;
45
46   guint32 region_tag_applied;
47   int region_tag_offset_x;
48   int region_tag_offset_y;
49
50   GdkRegion *old_clip_region;
51   GdkPixmap *old_clip_mask;
52
53   GdkBitmap *stipple;
54   GdkPixmap *tile;
55
56   GdkPixmap *clip_mask;
57
58   guint32 fg_pixel;
59   guint32 bg_pixel;
60
61   guint subwindow_mode : 1;
62   guint fill : 2;
63   guint exposures : 2;
64 };
65
66 #define GDK_GC_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDK_TYPE_GC, GdkGCPrivate))
67
68 G_DEFINE_TYPE (GdkGC, gdk_gc, G_TYPE_OBJECT)
69
70 static void
71 gdk_gc_class_init (GdkGCClass *class)
72 {
73   GObjectClass *object_class = G_OBJECT_CLASS (class);
74   
75   object_class->finalize = gdk_gc_finalize;
76
77   g_type_class_add_private (object_class, sizeof (GdkGCPrivate));
78 }
79
80 static void
81 gdk_gc_init (GdkGC *gc)
82 {
83   GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
84
85   priv->fill = GDK_SOLID;
86
87   /* These are the default X11 value, which we match. They are clearly
88    * wrong for TrueColor displays, so apps have to change them.
89    */
90   priv->fg_pixel = 0;
91   priv->bg_pixel = 1;
92 }
93
94 /**
95  * gdk_gc_new:
96  * @drawable: a #GdkDrawable. The created GC must always be used
97  *   with drawables of the same depth as this one.
98  *
99  * Create a new graphics context with default values. 
100  *
101  * Returns: the new graphics context.
102  **/
103 GdkGC*
104 gdk_gc_new (GdkDrawable *drawable)
105 {
106   g_return_val_if_fail (drawable != NULL, NULL);
107
108   return gdk_gc_new_with_values (drawable, NULL, 0);
109 }
110
111 /**
112  * gdk_gc_new_with_values:
113  * @drawable: a #GdkDrawable. The created GC must always be used
114  *   with drawables of the same depth as this one.
115  * @values: a structure containing initial values for the GC.
116  * @values_mask: a bit mask indicating which fields in @values
117  *   are set.
118  * 
119  * Create a new GC with the given initial values.
120  * 
121  * Return value: the new graphics context.
122  **/
123 GdkGC*
124 gdk_gc_new_with_values (GdkDrawable     *drawable,
125                         GdkGCValues     *values,
126                         GdkGCValuesMask  values_mask)
127 {
128   g_return_val_if_fail (drawable != NULL, NULL);
129
130   return GDK_DRAWABLE_GET_CLASS (drawable)->create_gc (drawable,
131                                                        values,
132                                                        values_mask);
133 }
134
135 /**
136  * _gdk_gc_init:
137  * @gc: a #GdkGC
138  * @drawable: a #GdkDrawable.
139  * @values: a structure containing initial values for the GC.
140  * @values_mask: a bit mask indicating which fields in @values
141  *   are set.
142  * 
143  * Does initialization of the generic portions of a #GdkGC
144  * created with the specified values and values_mask. This
145  * should be called out of the implementation of
146  * GdkDrawable.create_gc() immediately after creating the
147  * #GdkGC object.
148  **/
149 void
150 _gdk_gc_init (GdkGC           *gc,
151               GdkDrawable     *drawable,
152               GdkGCValues     *values,
153               GdkGCValuesMask  values_mask)
154 {
155   GdkGCPrivate *priv;
156
157   g_return_if_fail (GDK_IS_GC (gc));
158
159   priv = GDK_GC_GET_PRIVATE (gc);
160
161   if (values_mask & GDK_GC_CLIP_X_ORIGIN)
162     gc->clip_x_origin = values->clip_x_origin;
163   if (values_mask & GDK_GC_CLIP_Y_ORIGIN)
164     gc->clip_y_origin = values->clip_y_origin;
165   if ((values_mask & GDK_GC_CLIP_MASK) && values->clip_mask)
166     priv->clip_mask = g_object_ref (values->clip_mask);
167   if (values_mask & GDK_GC_TS_X_ORIGIN)
168     gc->ts_x_origin = values->ts_x_origin;
169   if (values_mask & GDK_GC_TS_Y_ORIGIN)
170     gc->ts_y_origin = values->ts_y_origin;
171   if (values_mask & GDK_GC_FILL)
172     priv->fill = values->fill;
173   if (values_mask & GDK_GC_STIPPLE)
174     {
175       priv->stipple = values->stipple;
176       if (priv->stipple)
177         g_object_ref (priv->stipple);
178     }
179   if (values_mask & GDK_GC_TILE)
180     {
181       priv->tile = values->tile;
182       if (priv->tile)
183         g_object_ref (priv->tile);
184     }
185   if (values_mask & GDK_GC_FOREGROUND)
186     priv->fg_pixel = values->foreground.pixel;
187   if (values_mask & GDK_GC_BACKGROUND)
188     priv->bg_pixel = values->background.pixel;
189   if (values_mask & GDK_GC_SUBWINDOW)
190     priv->subwindow_mode = values->subwindow_mode;
191   if (values_mask & GDK_GC_EXPOSURES)
192     priv->exposures = values->graphics_exposures;
193   else
194     priv->exposures = TRUE;
195
196   gc->colormap = gdk_drawable_get_colormap (drawable);
197   if (gc->colormap)
198     g_object_ref (gc->colormap);
199 }
200
201 static void
202 gdk_gc_finalize (GObject *object)
203 {
204   GdkGC *gc = GDK_GC (object);
205   GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
206
207   if (priv->clip_region)
208     gdk_region_destroy (priv->clip_region);
209   if (priv->old_clip_region)
210     gdk_region_destroy (priv->old_clip_region);
211   if (priv->clip_mask)
212     g_object_unref (priv->clip_mask);
213   if (priv->old_clip_mask)
214     g_object_unref (priv->old_clip_mask);
215   if (gc->colormap)
216     g_object_unref (gc->colormap);
217   if (priv->tile)
218     g_object_unref (priv->tile);
219   if (priv->stipple)
220     g_object_unref (priv->stipple);
221
222   G_OBJECT_CLASS (gdk_gc_parent_class)->finalize (object);
223 }
224
225 /**
226  * gdk_gc_ref:
227  * @gc: a #GdkGC
228  *
229  * Deprecated function; use g_object_ref() instead.
230  *
231  * Return value: the gc.
232  *
233  * Deprecated: 2.0: Use g_object_ref() instead.
234  **/
235 GdkGC *
236 gdk_gc_ref (GdkGC *gc)
237 {
238   return (GdkGC *) g_object_ref (gc);
239 }
240
241 /**
242  * gdk_gc_unref:
243  * @gc: a #GdkGC
244  *
245  * Decrement the reference count of @gc.
246  *
247  * Deprecated: 2.0: Use g_object_unref() instead.
248  **/
249 void
250 gdk_gc_unref (GdkGC *gc)
251 {
252   g_object_unref (gc);
253 }
254
255 /**
256  * gdk_gc_get_values:
257  * @gc:  a #GdkGC.
258  * @values: the #GdkGCValues structure in which to store the results.
259  * 
260  * Retrieves the current values from a graphics context. Note that 
261  * only the pixel values of the @values->foreground and @values->background
262  * are filled, use gdk_colormap_query_color() to obtain the rgb values
263  * if you need them.
264  **/
265 void
266 gdk_gc_get_values (GdkGC       *gc,
267                    GdkGCValues *values)
268 {
269   g_return_if_fail (GDK_IS_GC (gc));
270   g_return_if_fail (values != NULL);
271
272   GDK_GC_GET_CLASS (gc)->get_values (gc, values);
273 }
274
275 /**
276  * gdk_gc_set_values:
277  * @gc: a #GdkGC
278  * @values: struct containing the new values
279  * @values_mask: mask indicating which struct fields are to be used
280  *
281  * Sets attributes of a graphics context in bulk. For each flag set in
282  * @values_mask, the corresponding field will be read from @values and
283  * set as the new value for @gc. If you're only setting a few values
284  * on @gc, calling individual "setter" functions is likely more
285  * convenient.
286  * 
287  **/
288 void
289 gdk_gc_set_values (GdkGC           *gc,
290                    GdkGCValues     *values,
291                    GdkGCValuesMask  values_mask)
292 {
293   GdkGCPrivate *priv;
294
295   g_return_if_fail (GDK_IS_GC (gc));
296   g_return_if_fail (values != NULL);
297
298   priv = GDK_GC_GET_PRIVATE (gc);
299
300   if ((values_mask & GDK_GC_CLIP_X_ORIGIN) ||
301       (values_mask & GDK_GC_CLIP_Y_ORIGIN) ||
302       (values_mask & GDK_GC_CLIP_MASK) ||
303       (values_mask & GDK_GC_SUBWINDOW))
304     _gdk_gc_remove_drawable_clip (gc);
305   
306   if (values_mask & GDK_GC_CLIP_X_ORIGIN)
307     gc->clip_x_origin = values->clip_x_origin;
308   if (values_mask & GDK_GC_CLIP_Y_ORIGIN)
309     gc->clip_y_origin = values->clip_y_origin;
310   if (values_mask & GDK_GC_TS_X_ORIGIN)
311     gc->ts_x_origin = values->ts_x_origin;
312   if (values_mask & GDK_GC_TS_Y_ORIGIN)
313     gc->ts_y_origin = values->ts_y_origin;
314   if (values_mask & GDK_GC_CLIP_MASK)
315     {
316       if (priv->clip_mask)
317         {
318           g_object_unref (priv->clip_mask);
319           priv->clip_mask = NULL;
320         }
321       if (values->clip_mask)
322         priv->clip_mask = g_object_ref (values->clip_mask);
323       
324       if (priv->clip_region)
325         {
326           gdk_region_destroy (priv->clip_region);
327           priv->clip_region = NULL;
328         }
329     }
330   if (values_mask & GDK_GC_FILL)
331     priv->fill = values->fill;
332   if (values_mask & GDK_GC_STIPPLE)
333     {
334       if (priv->stipple != values->stipple)
335         {
336           if (priv->stipple)
337             g_object_unref (priv->stipple);
338           priv->stipple = values->stipple;
339           if (priv->stipple)
340             g_object_ref (priv->stipple);
341         }
342     }
343   if (values_mask & GDK_GC_TILE)
344     {
345       if (priv->tile != values->tile)
346         {
347           if (priv->tile)
348             g_object_unref (priv->tile);
349           priv->tile = values->tile;
350           if (priv->tile)
351             g_object_ref (priv->tile);
352         }
353     }
354   if (values_mask & GDK_GC_FOREGROUND)
355     priv->fg_pixel = values->foreground.pixel;
356   if (values_mask & GDK_GC_BACKGROUND)
357     priv->bg_pixel = values->background.pixel;
358   if (values_mask & GDK_GC_SUBWINDOW)
359     priv->subwindow_mode = values->subwindow_mode;
360   if (values_mask & GDK_GC_EXPOSURES)
361     priv->exposures = values->graphics_exposures;
362   
363   GDK_GC_GET_CLASS (gc)->set_values (gc, values, values_mask);
364 }
365
366 /**
367  * gdk_gc_set_foreground:
368  * @gc: a #GdkGC.
369  * @color: the new foreground color.
370  * 
371  * Sets the foreground color for a graphics context.
372  * Note that this function uses @color->pixel, use 
373  * gdk_gc_set_rgb_fg_color() to specify the foreground 
374  * color as red, green, blue components.
375  **/
376 void
377 gdk_gc_set_foreground (GdkGC          *gc,
378                        const GdkColor *color)
379 {
380   GdkGCValues values;
381
382   g_return_if_fail (GDK_IS_GC (gc));
383   g_return_if_fail (color != NULL);
384
385   values.foreground = *color;
386   gdk_gc_set_values (gc, &values, GDK_GC_FOREGROUND);
387 }
388
389 /**
390  * gdk_gc_set_background:
391  * @gc: a #GdkGC.
392  * @color: the new background color.
393  * 
394  * Sets the background color for a graphics context.
395  * Note that this function uses @color->pixel, use 
396  * gdk_gc_set_rgb_bg_color() to specify the background 
397  * color as red, green, blue components.
398  **/
399 void
400 gdk_gc_set_background (GdkGC          *gc,
401                        const GdkColor *color)
402 {
403   GdkGCValues values;
404
405   g_return_if_fail (GDK_IS_GC (gc));
406   g_return_if_fail (color != NULL);
407
408   values.background = *color;
409   gdk_gc_set_values (gc, &values, GDK_GC_BACKGROUND);
410 }
411
412 /**
413  * gdk_gc_set_font:
414  * @gc: a #GdkGC.
415  * @font: the new font. 
416  * 
417  * Sets the font for a graphics context. (Note that
418  * all text-drawing functions in GDK take a @font
419  * argument; the value set here is used when that
420  * argument is %NULL.)
421  **/
422 void
423 gdk_gc_set_font (GdkGC   *gc,
424                  GdkFont *font)
425 {
426   GdkGCValues values;
427
428   g_return_if_fail (GDK_IS_GC (gc));
429   g_return_if_fail (font != NULL);
430
431   values.font = font;
432   gdk_gc_set_values (gc, &values, GDK_GC_FONT);
433 }
434
435 /**
436  * gdk_gc_set_function:
437  * @gc: a #GdkGC.
438  * @function: the #GdkFunction to use
439  * 
440  * Determines how the current pixel values and the
441  * pixel values being drawn are combined to produce
442  * the final pixel values.
443  **/
444 void
445 gdk_gc_set_function (GdkGC       *gc,
446                      GdkFunction  function)
447 {
448   GdkGCValues values;
449
450   g_return_if_fail (GDK_IS_GC (gc));
451
452   values.function = function;
453   gdk_gc_set_values (gc, &values, GDK_GC_FUNCTION);
454 }
455
456 /**
457  * gdk_gc_set_fill:
458  * @gc: a #GdkGC.
459  * @fill: the new fill mode.
460  * 
461  * Set the fill mode for a graphics context.
462  **/
463 void
464 gdk_gc_set_fill (GdkGC   *gc,
465                  GdkFill  fill)
466 {
467   GdkGCValues values;
468
469   g_return_if_fail (GDK_IS_GC (gc));
470
471   values.fill = fill;
472   gdk_gc_set_values (gc, &values, GDK_GC_FILL);
473 }
474
475 /**
476  * gdk_gc_set_tile:
477  * @gc:  a #GdkGC.
478  * @tile:  the new tile pixmap.
479  * 
480  * Set a tile pixmap for a graphics context.
481  * This will only be used if the fill mode
482  * is %GDK_TILED.
483  **/
484 void
485 gdk_gc_set_tile (GdkGC     *gc,
486                  GdkPixmap *tile)
487 {
488   GdkGCValues values;
489
490   g_return_if_fail (GDK_IS_GC (gc));
491
492   values.tile = tile;
493   gdk_gc_set_values (gc, &values, GDK_GC_TILE);
494 }
495
496 /**
497  * gdk_gc_set_stipple:
498  * @gc: a #GdkGC.
499  * @stipple: the new stipple bitmap.
500  * 
501  * Set the stipple bitmap for a graphics context. The
502  * stipple will only be used if the fill mode is
503  * %GDK_STIPPLED or %GDK_OPAQUE_STIPPLED.
504  **/
505 void
506 gdk_gc_set_stipple (GdkGC     *gc,
507                     GdkPixmap *stipple)
508 {
509   GdkGCValues values;
510
511   g_return_if_fail (GDK_IS_GC (gc));
512
513   values.stipple = stipple;
514   gdk_gc_set_values (gc, &values, GDK_GC_STIPPLE);
515 }
516
517 /**
518  * gdk_gc_set_ts_origin:
519  * @gc:  a #GdkGC.
520  * @x: the x-coordinate of the origin.
521  * @y: the y-coordinate of the origin.
522  * 
523  * Set the origin when using tiles or stipples with
524  * the GC. The tile or stipple will be aligned such
525  * that the upper left corner of the tile or stipple
526  * will coincide with this point.
527  **/
528 void
529 gdk_gc_set_ts_origin (GdkGC *gc,
530                       gint   x,
531                       gint   y)
532 {
533   GdkGCValues values;
534
535   g_return_if_fail (GDK_IS_GC (gc));
536
537   values.ts_x_origin = x;
538   values.ts_y_origin = y;
539   
540   gdk_gc_set_values (gc, &values,
541                      GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN);
542 }
543
544 /**
545  * gdk_gc_set_clip_origin:
546  * @gc: a #GdkGC.
547  * @x: the x-coordinate of the origin.
548  * @y: the y-coordinate of the origin.
549  * 
550  * Sets the origin of the clip mask. The coordinates are
551  * interpreted relative to the upper-left corner of
552  * the destination drawable of the current operation.
553  **/
554 void
555 gdk_gc_set_clip_origin (GdkGC *gc,
556                         gint   x,
557                         gint   y)
558 {
559   GdkGCValues values;
560
561   g_return_if_fail (GDK_IS_GC (gc));
562
563   values.clip_x_origin = x;
564   values.clip_y_origin = y;
565   
566   gdk_gc_set_values (gc, &values,
567                      GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
568 }
569
570 /**
571  * gdk_gc_set_clip_mask:
572  * @gc: the #GdkGC.
573  * @mask: a bitmap.
574  * 
575  * Sets the clip mask for a graphics context from a bitmap.
576  * The clip mask is interpreted relative to the clip
577  * origin. (See gdk_gc_set_clip_origin()).
578  **/
579 void
580 gdk_gc_set_clip_mask (GdkGC     *gc,
581                       GdkBitmap *mask)
582 {
583   GdkGCValues values;
584   
585   g_return_if_fail (GDK_IS_GC (gc));
586   
587   values.clip_mask = mask;
588   gdk_gc_set_values (gc, &values, GDK_GC_CLIP_MASK);
589 }
590
591 /* Takes ownership of passed in region */
592 static void
593 _gdk_gc_set_clip_region_real (GdkGC     *gc,
594                               GdkRegion *region,
595                               gboolean reset_origin)
596 {
597   GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
598
599   if (priv->clip_mask)
600     {
601       g_object_unref (priv->clip_mask);
602       priv->clip_mask = NULL;
603     }
604   
605   if (priv->clip_region)
606     gdk_region_destroy (priv->clip_region);
607
608   priv->clip_region = region;
609
610   _gdk_windowing_gc_set_clip_region (gc, region, reset_origin);
611 }
612
613 /* Doesn't copy region, allows not to reset origin */
614 void
615 _gdk_gc_set_clip_region_internal (GdkGC     *gc,
616                                   GdkRegion *region,
617                                   gboolean reset_origin)
618 {
619   _gdk_gc_remove_drawable_clip (gc);
620   _gdk_gc_set_clip_region_real (gc, region, reset_origin);
621 }
622
623
624 void
625 _gdk_gc_add_drawable_clip (GdkGC     *gc,
626                            guint32    region_tag,
627                            GdkRegion *region,
628                            int        offset_x,
629                            int        offset_y)
630 {
631   GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
632
633   if (priv->region_tag_applied == region_tag &&
634       offset_x == priv->region_tag_offset_x &&
635       offset_y == priv->region_tag_offset_y)
636     return; /* Already appied this drawable region */
637   
638   if (priv->region_tag_applied)
639     _gdk_gc_remove_drawable_clip (gc);
640
641   region = gdk_region_copy (region);
642   if (offset_x != 0 || offset_y != 0)
643     gdk_region_offset (region, offset_x, offset_y);
644
645   if (priv->clip_mask)
646     {
647       int w, h;
648       GdkPixmap *new_mask;
649       GdkGC *tmp_gc;
650       GdkColor black = {0, 0, 0, 0};
651       GdkRectangle r;
652       GdkOverlapType overlap;
653
654       gdk_drawable_get_size (priv->clip_mask, &w, &h);
655
656       r.x = 0;
657       r.y = 0;
658       r.width = w;
659       r.height = h;
660
661       /* Its quite common to expose areas that are completely in or outside
662        * the region, so we try to avoid allocating bitmaps that are just fully
663        * set or completely unset.
664        */
665       overlap = gdk_region_rect_in (region, &r);
666       if (overlap == GDK_OVERLAP_RECTANGLE_PART)
667         {
668            /* The region and the mask intersect, create a new clip mask that
669               includes both areas */
670           priv->old_clip_mask = g_object_ref (priv->clip_mask);
671           new_mask = gdk_pixmap_new (priv->old_clip_mask, w, h, -1);
672           tmp_gc = _gdk_drawable_get_scratch_gc ((GdkDrawable *)new_mask, FALSE);
673
674           gdk_gc_set_foreground (tmp_gc, &black);
675           gdk_draw_rectangle (new_mask, tmp_gc, TRUE, 0, 0, -1, -1);
676           _gdk_gc_set_clip_region_internal (tmp_gc, region, TRUE); /* Takes ownership of region */
677           gdk_draw_drawable  (new_mask,
678                               tmp_gc,
679                               priv->old_clip_mask,
680                               0, 0,
681                               0, 0,
682                               -1, -1);
683           gdk_gc_set_clip_region (tmp_gc, NULL);
684           gdk_gc_set_clip_mask (gc, new_mask);
685         }
686       else if (overlap == GDK_OVERLAP_RECTANGLE_OUT)
687         {
688           /* No intersection, set empty clip region */
689           GdkRegion *empty = gdk_region_new ();
690
691           gdk_region_destroy (region);
692           priv->old_clip_mask = g_object_ref (priv->clip_mask);
693           priv->clip_region = empty;
694           _gdk_windowing_gc_set_clip_region (gc, empty, FALSE);
695         }
696       else
697         {
698           /* Completely inside region, don't set unnecessary clip */
699           gdk_region_destroy (region);
700           return;
701         }
702     }
703   else
704     {
705       priv->old_clip_region = priv->clip_region;
706       priv->clip_region = region;
707       if (priv->old_clip_region)
708         gdk_region_intersect (region, priv->old_clip_region);
709
710       _gdk_windowing_gc_set_clip_region (gc, priv->clip_region, FALSE);
711     }
712
713   priv->region_tag_applied = region_tag;
714   priv->region_tag_offset_x = offset_x;
715   priv->region_tag_offset_y = offset_y;
716 }
717
718 void
719 _gdk_gc_remove_drawable_clip (GdkGC *gc)
720 {
721   GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
722
723   if (priv->region_tag_applied)
724     {
725       priv->region_tag_applied = 0;
726       if (priv->old_clip_mask)
727         {
728           gdk_gc_set_clip_mask (gc, priv->old_clip_mask);
729           g_object_unref (priv->old_clip_mask);
730           priv->old_clip_mask = NULL;
731
732           if (priv->clip_region)
733             {
734               g_object_unref (priv->clip_region);
735               priv->clip_region = NULL;
736             }
737         }
738       else
739         {
740           _gdk_gc_set_clip_region_real (gc, priv->old_clip_region, FALSE);
741           priv->old_clip_region = NULL;
742         }
743     }
744 }
745
746 /**
747  * gdk_gc_set_clip_rectangle:
748  * @gc: a #GdkGC.
749  * @rectangle: the rectangle to clip to.
750  * 
751  * Sets the clip mask for a graphics context from a
752  * rectangle. The clip mask is interpreted relative to the clip
753  * origin. (See gdk_gc_set_clip_origin()).
754  **/
755 void
756 gdk_gc_set_clip_rectangle (GdkGC              *gc,
757                            const GdkRectangle *rectangle)
758 {
759   GdkRegion *region;
760   
761   g_return_if_fail (GDK_IS_GC (gc));
762
763   _gdk_gc_remove_drawable_clip (gc);
764   
765   if (rectangle)
766     region = gdk_region_rectangle (rectangle);
767   else
768     region = NULL;
769
770   _gdk_gc_set_clip_region_real (gc, region, TRUE);
771 }
772
773 /**
774  * gdk_gc_set_clip_region:
775  * @gc: a #GdkGC.
776  * @region: the #GdkRegion. 
777  * 
778  * Sets the clip mask for a graphics context from a region structure.
779  * The clip mask is interpreted relative to the clip origin. (See
780  * gdk_gc_set_clip_origin()).
781  **/
782 void
783 gdk_gc_set_clip_region (GdkGC           *gc,
784                         const GdkRegion *region)
785 {
786   GdkRegion *copy;
787
788   g_return_if_fail (GDK_IS_GC (gc));
789
790   _gdk_gc_remove_drawable_clip (gc);
791   
792   if (region)
793     copy = gdk_region_copy (region);
794   else
795     copy = NULL;
796
797   _gdk_gc_set_clip_region_real (gc, copy, TRUE);
798 }
799
800 /**
801  * _gdk_gc_get_clip_region:
802  * @gc: a #GdkGC
803  * 
804  * Gets the current clip region for @gc, if any.
805  * 
806  * Return value: the clip region for the GC, or %NULL.
807  *   (if a clip mask is set, the return will be %NULL)
808  *   This value is owned by the GC and must not be freed.
809  **/
810 GdkRegion *
811 _gdk_gc_get_clip_region (GdkGC *gc)
812 {
813   g_return_val_if_fail (GDK_IS_GC (gc), NULL);
814
815   return GDK_GC_GET_PRIVATE (gc)->clip_region;
816 }
817
818 /**
819  * _gdk_gc_get_clip_mask:
820  * @gc: a #GdkGC
821  *
822  * Gets the current clip mask for @gc, if any.
823  *
824  * Return value: the clip mask for the GC, or %NULL.
825  *   (if a clip region is set, the return will be %NULL)
826  *   This value is owned by the GC and must not be freed.
827  **/
828 GdkBitmap *
829 _gdk_gc_get_clip_mask (GdkGC *gc)
830 {
831   g_return_val_if_fail (GDK_IS_GC (gc), NULL);
832
833   return GDK_GC_GET_PRIVATE (gc)->clip_mask;
834 }
835
836 /**
837  * _gdk_gc_get_fill:
838  * @gc: a #GdkGC
839  * 
840  * Gets the current file style for the GC
841  * 
842  * Return value: the file style for the GC
843  **/
844 GdkFill
845 _gdk_gc_get_fill (GdkGC *gc)
846 {
847   g_return_val_if_fail (GDK_IS_GC (gc), GDK_SOLID);
848
849   return GDK_GC_GET_PRIVATE (gc)->fill;
850 }
851
852 gboolean
853 _gdk_gc_get_exposures (GdkGC *gc)
854 {
855   g_return_val_if_fail (GDK_IS_GC (gc), FALSE);
856
857   return GDK_GC_GET_PRIVATE (gc)->exposures;
858 }
859
860 /**
861  * _gdk_gc_get_tile:
862  * @gc: a #GdkGC
863  * 
864  * Gets the tile pixmap for @gc, if any
865  * 
866  * Return value: the tile set on the GC, or %NULL. The
867  *   value is owned by the GC and must not be freed.
868  **/
869 GdkPixmap *
870 _gdk_gc_get_tile (GdkGC *gc)
871 {
872   g_return_val_if_fail (GDK_IS_GC (gc), NULL);
873
874   return GDK_GC_GET_PRIVATE (gc)->tile;
875 }
876
877 /**
878  * _gdk_gc_get_stipple:
879  * @gc: a #GdkGC
880  * 
881  * Gets the stipple pixmap for @gc, if any
882  * 
883  * Return value: the stipple set on the GC, or %NULL. The
884  *   value is owned by the GC and must not be freed.
885  **/
886 GdkBitmap *
887 _gdk_gc_get_stipple (GdkGC *gc)
888 {
889   g_return_val_if_fail (GDK_IS_GC (gc), NULL);
890
891   return GDK_GC_GET_PRIVATE (gc)->stipple;
892 }
893
894 /**
895  * _gdk_gc_get_fg_pixel:
896  * @gc: a #GdkGC
897  * 
898  * Gets the foreground pixel value for @gc. If the
899  * foreground pixel has never been set, returns the
900  * default value 0.
901  * 
902  * Return value: the foreground pixel value of the GC
903  **/
904 guint32
905 _gdk_gc_get_fg_pixel (GdkGC *gc)
906 {
907   g_return_val_if_fail (GDK_IS_GC (gc), 0);
908   
909   return GDK_GC_GET_PRIVATE (gc)->fg_pixel;
910 }
911
912 /**
913  * _gdk_gc_get_bg_pixel:
914  * @gc: a #GdkGC
915  * 
916  * Gets the background pixel value for @gc.If the
917  * foreground pixel has never been set, returns the
918  * default value 1.
919  * 
920  * Return value: the foreground pixel value of the GC
921  **/
922 guint32
923 _gdk_gc_get_bg_pixel (GdkGC *gc)
924 {
925   g_return_val_if_fail (GDK_IS_GC (gc), 0);
926   
927   return GDK_GC_GET_PRIVATE (gc)->bg_pixel;
928 }
929
930 /**
931  * gdk_gc_set_subwindow:
932  * @gc: a #GdkGC.
933  * @mode: the subwindow mode.
934  * 
935  * Sets how drawing with this GC on a window will affect child
936  * windows of that window. 
937  **/
938 void
939 gdk_gc_set_subwindow (GdkGC            *gc,
940                       GdkSubwindowMode  mode)
941 {
942   GdkGCValues values;
943   GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
944
945   g_return_if_fail (GDK_IS_GC (gc));
946
947   /* This could get called a lot to reset the subwindow mode in
948      the client side clipping, so bail out early */ 
949   if (priv->subwindow_mode == mode)
950     return;
951   
952   values.subwindow_mode = mode;
953   gdk_gc_set_values (gc, &values, GDK_GC_SUBWINDOW);
954 }
955
956 GdkSubwindowMode
957 _gdk_gc_get_subwindow (GdkGC *gc)
958 {
959   GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
960
961   return priv->subwindow_mode;
962 }
963
964 /**
965  * gdk_gc_set_exposures:
966  * @gc: a #GdkGC.
967  * @exposures: if %TRUE, exposure events will be generated.
968  * 
969  * Sets whether copying non-visible portions of a drawable
970  * using this graphics context generate exposure events
971  * for the corresponding regions of the destination
972  * drawable. (See gdk_draw_drawable()).
973  **/
974 void
975 gdk_gc_set_exposures (GdkGC     *gc,
976                       gboolean   exposures)
977 {
978   GdkGCValues values;
979
980   g_return_if_fail (GDK_IS_GC (gc));
981
982   values.graphics_exposures = exposures;
983   gdk_gc_set_values (gc, &values, GDK_GC_EXPOSURES);
984 }
985
986 /**
987  * gdk_gc_set_line_attributes:
988  * @gc: a #GdkGC.
989  * @line_width: the width of lines.
990  * @line_style: the dash-style for lines.
991  * @cap_style: the manner in which the ends of lines are drawn.
992  * @join_style: the in which lines are joined together.
993  * 
994  * Sets various attributes of how lines are drawn. See
995  * the corresponding members of #GdkGCValues for full
996  * explanations of the arguments.
997  **/
998 void
999 gdk_gc_set_line_attributes (GdkGC       *gc,
1000                             gint         line_width,
1001                             GdkLineStyle line_style,
1002                             GdkCapStyle  cap_style,
1003                             GdkJoinStyle join_style)
1004 {
1005   GdkGCValues values;
1006
1007   values.line_width = line_width;
1008   values.line_style = line_style;
1009   values.cap_style = cap_style;
1010   values.join_style = join_style;
1011
1012   gdk_gc_set_values (gc, &values,
1013                      GDK_GC_LINE_WIDTH |
1014                      GDK_GC_LINE_STYLE |
1015                      GDK_GC_CAP_STYLE |
1016                      GDK_GC_JOIN_STYLE);
1017 }
1018
1019 /**
1020  * gdk_gc_set_dashes:
1021  * @gc: a #GdkGC.
1022  * @dash_offset: the phase of the dash pattern.
1023  * @dash_list: an array of dash lengths.
1024  * @n: the number of elements in @dash_list.
1025  * 
1026  * Sets the way dashed-lines are drawn. Lines will be
1027  * drawn with alternating on and off segments of the
1028  * lengths specified in @dash_list. The manner in
1029  * which the on and off segments are drawn is determined
1030  * by the @line_style value of the GC. (This can
1031  * be changed with gdk_gc_set_line_attributes().)
1032  *
1033  * The @dash_offset defines the phase of the pattern, 
1034  * specifying how many pixels into the dash-list the pattern 
1035  * should actually begin.
1036  **/
1037 void
1038 gdk_gc_set_dashes (GdkGC *gc,
1039                    gint   dash_offset,
1040                    gint8  dash_list[],
1041                    gint   n)
1042 {
1043   g_return_if_fail (GDK_IS_GC (gc));
1044   g_return_if_fail (dash_list != NULL);
1045
1046   GDK_GC_GET_CLASS (gc)->set_dashes (gc, dash_offset, dash_list, n);
1047 }
1048
1049 /**
1050  * gdk_gc_offset:
1051  * @gc: a #GdkGC
1052  * @x_offset: amount by which to offset the GC in the X direction
1053  * @y_offset: amount by which to offset the GC in the Y direction
1054  * 
1055  * Offset attributes such as the clip and tile-stipple origins
1056  * of the GC so that drawing at x - x_offset, y - y_offset with
1057  * the offset GC  has the same effect as drawing at x, y with the original
1058  * GC.
1059  **/
1060 void
1061 gdk_gc_offset (GdkGC *gc,
1062                gint   x_offset,
1063                gint   y_offset)
1064 {
1065   if (x_offset != 0 || y_offset != 0)
1066     {
1067       GdkGCValues values;
1068
1069       values.clip_x_origin = gc->clip_x_origin - x_offset;
1070       values.clip_y_origin = gc->clip_y_origin - y_offset;
1071       values.ts_x_origin = gc->ts_x_origin - x_offset;
1072       values.ts_y_origin = gc->ts_y_origin - y_offset;
1073       
1074       gdk_gc_set_values (gc, &values,
1075                          GDK_GC_CLIP_X_ORIGIN |
1076                          GDK_GC_CLIP_Y_ORIGIN |
1077                          GDK_GC_TS_X_ORIGIN |
1078                          GDK_GC_TS_Y_ORIGIN);
1079     }
1080 }
1081
1082 /**
1083  * gdk_gc_copy:
1084  * @dst_gc: the destination graphics context.
1085  * @src_gc: the source graphics context.
1086  * 
1087  * Copy the set of values from one graphics context
1088  * onto another graphics context.
1089  **/
1090 void
1091 gdk_gc_copy (GdkGC *dst_gc,
1092              GdkGC *src_gc)
1093 {
1094   GdkGCPrivate *dst_priv, *src_priv;
1095   
1096   g_return_if_fail (GDK_IS_GC (dst_gc));
1097   g_return_if_fail (GDK_IS_GC (src_gc));
1098
1099   dst_priv = GDK_GC_GET_PRIVATE (dst_gc);
1100   src_priv = GDK_GC_GET_PRIVATE (src_gc);
1101
1102   _gdk_windowing_gc_copy (dst_gc, src_gc);
1103
1104   dst_gc->clip_x_origin = src_gc->clip_x_origin;
1105   dst_gc->clip_y_origin = src_gc->clip_y_origin;
1106   dst_gc->ts_x_origin = src_gc->ts_x_origin;
1107   dst_gc->ts_y_origin = src_gc->ts_y_origin;
1108
1109   if (src_gc->colormap)
1110     g_object_ref (src_gc->colormap);
1111
1112   if (dst_gc->colormap)
1113     g_object_unref (dst_gc->colormap);
1114
1115   dst_gc->colormap = src_gc->colormap;
1116
1117   if (dst_priv->clip_region)
1118     gdk_region_destroy (dst_priv->clip_region);
1119
1120   if (src_priv->clip_region)
1121     dst_priv->clip_region = gdk_region_copy (src_priv->clip_region);
1122   else
1123     dst_priv->clip_region = NULL;
1124
1125   dst_priv->region_tag_applied = src_priv->region_tag_applied;
1126   
1127   if (dst_priv->old_clip_region)
1128     gdk_region_destroy (dst_priv->old_clip_region);
1129
1130   if (src_priv->old_clip_region)
1131     dst_priv->old_clip_region = gdk_region_copy (src_priv->old_clip_region);
1132   else
1133     dst_priv->old_clip_region = NULL;
1134
1135   if (src_priv->clip_mask)
1136     dst_priv->clip_mask = g_object_ref (src_priv->clip_mask);
1137   else
1138     dst_priv->clip_mask = NULL;
1139   
1140   if (src_priv->old_clip_mask)
1141     dst_priv->old_clip_mask = g_object_ref (src_priv->old_clip_mask);
1142   else
1143     dst_priv->old_clip_mask = NULL;
1144   
1145   dst_priv->fill = src_priv->fill;
1146   
1147   if (dst_priv->stipple)
1148     g_object_unref (dst_priv->stipple);
1149   dst_priv->stipple = src_priv->stipple;
1150   if (dst_priv->stipple)
1151     g_object_ref (dst_priv->stipple);
1152   
1153   if (dst_priv->tile)
1154     g_object_unref (dst_priv->tile);
1155   dst_priv->tile = src_priv->tile;
1156   if (dst_priv->tile)
1157     g_object_ref (dst_priv->tile);
1158
1159   dst_priv->fg_pixel = src_priv->fg_pixel;
1160   dst_priv->bg_pixel = src_priv->bg_pixel;
1161   dst_priv->subwindow_mode = src_priv->subwindow_mode;
1162   dst_priv->exposures = src_priv->exposures;
1163 }
1164
1165 /**
1166  * gdk_gc_set_colormap:
1167  * @gc: a #GdkGC
1168  * @colormap: a #GdkColormap
1169  * 
1170  * Sets the colormap for the GC to the given colormap. The depth
1171  * of the colormap's visual must match the depth of the drawable
1172  * for which the GC was created.
1173  **/
1174 void
1175 gdk_gc_set_colormap (GdkGC       *gc,
1176                      GdkColormap *colormap)
1177 {
1178   g_return_if_fail (GDK_IS_GC (gc));
1179   g_return_if_fail (GDK_IS_COLORMAP (colormap));
1180
1181   if (gc->colormap != colormap)
1182     {
1183       if (gc->colormap)
1184         g_object_unref (gc->colormap);
1185
1186       gc->colormap = colormap;
1187       g_object_ref (gc->colormap);
1188     }
1189     
1190 }
1191
1192 /**
1193  * gdk_gc_get_colormap:
1194  * @gc: a #GdkGC
1195  * 
1196  * Retrieves the colormap for a given GC, if it exists.
1197  * A GC will have a colormap if the drawable for which it was created
1198  * has a colormap, or if a colormap was set explicitely with
1199  * gdk_gc_set_colormap.
1200  * 
1201  * Return value: the colormap of @gc, or %NULL if @gc doesn't have one.
1202  **/
1203 GdkColormap *
1204 gdk_gc_get_colormap (GdkGC *gc)
1205 {
1206   g_return_val_if_fail (GDK_IS_GC (gc), NULL);
1207
1208   return gc->colormap;
1209 }
1210
1211 static GdkColormap *
1212 gdk_gc_get_colormap_warn (GdkGC *gc)
1213 {
1214   GdkColormap *colormap = gdk_gc_get_colormap (gc);
1215   if (!colormap)
1216     {
1217       g_warning ("gdk_gc_set_rgb_fg_color() and gdk_gc_set_rgb_bg_color() can\n"
1218                  "only be used on GC's with a colormap. A GC will have a colormap\n"
1219                  "if it is created for a drawable with a colormap, or if a\n"
1220                  "colormap has been set explicitly with gdk_gc_set_colormap.\n");
1221       return NULL;
1222     }
1223
1224   return colormap;
1225 }
1226
1227 /**
1228  * gdk_gc_set_rgb_fg_color:
1229  * @gc: a #GdkGC
1230  * @color: an unallocated #GdkColor.
1231  * 
1232  * Set the foreground color of a GC using an unallocated color. The
1233  * pixel value for the color will be determined using GdkRGB. If the
1234  * colormap for the GC has not previously been initialized for GdkRGB,
1235  * then for pseudo-color colormaps (colormaps with a small modifiable
1236  * number of colors), a colorcube will be allocated in the colormap.
1237  * 
1238  * Calling this function for a GC without a colormap is an error.
1239  **/
1240 void
1241 gdk_gc_set_rgb_fg_color (GdkGC          *gc,
1242                          const GdkColor *color)
1243 {
1244   GdkColormap *cmap;
1245   GdkColor tmp_color;
1246
1247   g_return_if_fail (GDK_IS_GC (gc));
1248   g_return_if_fail (color != NULL);
1249
1250   cmap = gdk_gc_get_colormap_warn (gc);
1251   if (!cmap)
1252     return;
1253
1254   tmp_color = *color;
1255   gdk_rgb_find_color (cmap, &tmp_color);
1256   gdk_gc_set_foreground (gc, &tmp_color);
1257 }
1258
1259 /**
1260  * gdk_gc_set_rgb_bg_color:
1261  * @gc: a #GdkGC
1262  * @color: an unallocated #GdkColor.
1263  * 
1264  * Set the background color of a GC using an unallocated color. The
1265  * pixel value for the color will be determined using GdkRGB. If the
1266  * colormap for the GC has not previously been initialized for GdkRGB,
1267  * then for pseudo-color colormaps (colormaps with a small modifiable
1268  * number of colors), a colorcube will be allocated in the colormap.
1269  * 
1270  * Calling this function for a GC without a colormap is an error.
1271  **/
1272 void
1273 gdk_gc_set_rgb_bg_color (GdkGC          *gc,
1274                          const GdkColor *color)
1275 {
1276   GdkColormap *cmap;
1277   GdkColor tmp_color;
1278
1279   g_return_if_fail (GDK_IS_GC (gc));
1280   g_return_if_fail (color != NULL);
1281
1282   cmap = gdk_gc_get_colormap_warn (gc);
1283   if (!cmap)
1284     return;
1285
1286   tmp_color = *color;
1287   gdk_rgb_find_color (cmap, &tmp_color);
1288   gdk_gc_set_background (gc, &tmp_color);
1289 }
1290
1291 static cairo_surface_t *
1292 make_stipple_tile_surface (cairo_t   *cr,
1293                            GdkBitmap *stipple,
1294                            GdkColor  *foreground,
1295                            GdkColor  *background)
1296 {
1297   cairo_t *tmp_cr;
1298   cairo_surface_t *surface; 
1299   cairo_surface_t *alpha_surface;
1300   gint width, height;
1301
1302   gdk_drawable_get_size (stipple,
1303                          &width, &height);
1304   
1305   alpha_surface = _gdk_drawable_ref_cairo_surface (stipple);
1306   
1307   surface = cairo_surface_create_similar (cairo_get_target (cr),
1308                                           CAIRO_CONTENT_COLOR_ALPHA,
1309                                           width, height);
1310
1311   tmp_cr = cairo_create (surface);
1312   
1313   cairo_set_operator (tmp_cr, CAIRO_OPERATOR_SOURCE);
1314  
1315   if (background)
1316       gdk_cairo_set_source_color (tmp_cr, background);
1317   else
1318       cairo_set_source_rgba (tmp_cr, 0, 0, 0 ,0);
1319
1320   cairo_paint (tmp_cr);
1321
1322   cairo_set_operator (tmp_cr, CAIRO_OPERATOR_OVER);
1323
1324   gdk_cairo_set_source_color (tmp_cr, foreground);
1325   cairo_mask_surface (tmp_cr, alpha_surface, 0, 0);
1326   
1327   cairo_destroy (tmp_cr);
1328   cairo_surface_destroy (alpha_surface);
1329
1330   return surface;
1331 }
1332
1333 static void
1334 gc_get_foreground (GdkGC    *gc,
1335                    GdkColor *color)
1336 {
1337   GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
1338   
1339   color->pixel = priv->bg_pixel;
1340
1341   if (gc->colormap)
1342     gdk_colormap_query_color (gc->colormap, priv->fg_pixel, color);
1343   else
1344     g_warning ("No colormap in gc_get_foreground");
1345 }
1346
1347 static void
1348 gc_get_background (GdkGC    *gc,
1349                    GdkColor *color)
1350 {
1351   GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
1352   
1353   color->pixel = priv->bg_pixel;
1354
1355   if (gc->colormap)
1356     gdk_colormap_query_color (gc->colormap, priv->bg_pixel, color);
1357   else
1358     g_warning ("No colormap in gc_get_background");
1359 }
1360
1361 /**
1362  * _gdk_gc_update_context:
1363  * @gc: a #GdkGC
1364  * @cr: a #cairo_t
1365  * @override_foreground: a foreground color to use to override the
1366  *   foreground color of the GC
1367  * @override_stipple: a stipple pattern to use to override the
1368  *   stipple from the GC. If this is present and the fill mode
1369  *   of the GC isn't %GDK_STIPPLED or %GDK_OPAQUE_STIPPLED
1370  *   the fill mode will be forced to %GDK_STIPPLED
1371  * @gc_changed: pass %FALSE if the @gc has not changed since the
1372  *     last call to this function
1373  * @target_drawable: The drawable you're drawing in. If passed in
1374  *     this is used for client side window clip emulation.
1375  * 
1376  * Set the attributes of a cairo context to match those of a #GdkGC
1377  * as far as possible. Some aspects of a #GdkGC, such as clip masks
1378  * and functions other than %GDK_COPY are not currently handled.
1379  **/
1380 void
1381 _gdk_gc_update_context (GdkGC          *gc,
1382                         cairo_t        *cr,
1383                         const GdkColor *override_foreground,
1384                         GdkBitmap      *override_stipple,
1385                         gboolean        gc_changed,
1386                         GdkDrawable    *target_drawable)
1387 {
1388   GdkGCPrivate *priv;
1389   GdkFill fill;
1390   GdkColor foreground;
1391   GdkColor background;
1392   cairo_surface_t *tile_surface = NULL;
1393   GdkBitmap *stipple = NULL;
1394
1395   g_return_if_fail (GDK_IS_GC (gc));
1396   g_return_if_fail (cr != NULL);
1397   g_return_if_fail (override_stipple == NULL || GDK_IS_PIXMAP (override_stipple));
1398
1399   priv = GDK_GC_GET_PRIVATE (gc);
1400
1401   _gdk_gc_remove_drawable_clip (gc);
1402
1403   fill = priv->fill;
1404   if (override_stipple && fill != GDK_OPAQUE_STIPPLED)
1405     fill = GDK_STIPPLED;
1406
1407   if (fill != GDK_TILED)
1408     {
1409       if (override_foreground)
1410         foreground = *override_foreground;
1411       else
1412         gc_get_foreground (gc, &foreground);
1413     }
1414
1415   if (fill == GDK_OPAQUE_STIPPLED)
1416     gc_get_background (gc, &background);
1417
1418
1419   switch (fill)
1420     {
1421     case GDK_SOLID:
1422       break;
1423     case GDK_TILED:
1424       if (!priv->tile)
1425         fill = GDK_SOLID;
1426       break;
1427     case GDK_STIPPLED:
1428     case GDK_OPAQUE_STIPPLED:
1429       if (override_stipple)
1430         stipple = override_stipple;
1431       else
1432         stipple = priv->stipple;
1433       
1434       if (!stipple)
1435         fill = GDK_SOLID;
1436       break;
1437     }
1438   
1439   switch (fill)
1440     {
1441     case GDK_SOLID:
1442       gdk_cairo_set_source_color (cr, &foreground);
1443       break;
1444     case GDK_TILED:
1445       tile_surface = _gdk_drawable_ref_cairo_surface (priv->tile);
1446       break;
1447     case GDK_STIPPLED:
1448       tile_surface = make_stipple_tile_surface (cr, stipple, &foreground, NULL);
1449       break;
1450     case GDK_OPAQUE_STIPPLED:
1451       tile_surface = make_stipple_tile_surface (cr, stipple, &foreground, &background);
1452       break;
1453     }
1454
1455   /* Tiles, stipples, and clip regions are all specified in device space,
1456    * not user space. For the clip region, we can simply change the matrix,
1457    * clip, then clip back, but for the source pattern, we need to
1458    * compute the right matrix.
1459    *
1460    * What we want is:
1461    *
1462    *     CTM_inverse * Pattern_matrix = Translate(- ts_x, - ts_y)
1463    *
1464    * (So that ts_x, ts_y in device space is taken to 0,0 in pattern
1465    * space). So, pattern_matrix = CTM * Translate(- ts_x, - tx_y);
1466    */
1467
1468   if (tile_surface)
1469     {
1470       cairo_pattern_t *pattern = cairo_pattern_create_for_surface (tile_surface);
1471       cairo_matrix_t user_to_device;
1472       cairo_matrix_t user_to_pattern;
1473       cairo_matrix_t device_to_pattern;
1474
1475       cairo_get_matrix (cr, &user_to_device);
1476       cairo_matrix_init_translate (&device_to_pattern,
1477                                    - gc->ts_x_origin, - gc->ts_y_origin);
1478       cairo_matrix_multiply (&user_to_pattern,
1479                              &user_to_device, &device_to_pattern);
1480       
1481       cairo_pattern_set_matrix (pattern, &user_to_pattern);
1482       cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
1483       cairo_set_source (cr, pattern);
1484       
1485       cairo_surface_destroy (tile_surface);
1486       cairo_pattern_destroy (pattern);
1487     }
1488
1489   if (!gc_changed)
1490     return;
1491
1492   cairo_reset_clip (cr);
1493   /* The reset above resets the window clip rect, so we want to re-set that */
1494   if (target_drawable && GDK_DRAWABLE_GET_CLASS (target_drawable)->set_cairo_clip)
1495     GDK_DRAWABLE_GET_CLASS (target_drawable)->set_cairo_clip (target_drawable, cr);
1496
1497   if (priv->clip_region)
1498     {
1499       cairo_save (cr);
1500
1501       cairo_identity_matrix (cr);
1502       cairo_translate (cr, gc->clip_x_origin, gc->clip_y_origin);
1503
1504       cairo_new_path (cr);
1505       gdk_cairo_region (cr, priv->clip_region);
1506
1507       cairo_restore (cr);
1508
1509       cairo_clip (cr);
1510     }
1511
1512 }
1513
1514
1515 #define __GDK_GC_C__
1516 #include "gdkaliasdef.c"