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