1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * 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/.
32 #include "gdkinternals.h"
33 #include "gdkpixmap.h"
34 #include "gdkprivate.h"
37 static void gdk_gc_finalize (GObject *object);
39 typedef struct _GdkGCPrivate GdkGCPrivate;
43 cairo_region_t *clip_region;
45 guint32 region_tag_applied;
46 int region_tag_offset_x;
47 int region_tag_offset_y;
49 cairo_region_t *old_clip_region;
50 GdkPixmap *old_clip_mask;
60 guint subwindow_mode : 1;
65 #define GDK_GC_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDK_TYPE_GC, GdkGCPrivate))
67 G_DEFINE_TYPE (GdkGC, gdk_gc, G_TYPE_OBJECT)
70 gdk_gc_class_init (GdkGCClass *class)
72 GObjectClass *object_class = G_OBJECT_CLASS (class);
74 object_class->finalize = gdk_gc_finalize;
76 g_type_class_add_private (object_class, sizeof (GdkGCPrivate));
80 gdk_gc_init (GdkGC *gc)
82 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
84 priv->fill = GDK_SOLID;
86 /* These are the default X11 value, which we match. They are clearly
87 * wrong for TrueColor displays, so apps have to change them.
95 * @drawable: a #GdkDrawable. The created GC must always be used
96 * with drawables of the same depth as this one.
98 * Create a new graphics context with default values.
100 * Returns: the new graphics context.
103 gdk_gc_new (GdkDrawable *drawable)
105 g_return_val_if_fail (drawable != NULL, NULL);
107 return gdk_gc_new_with_values (drawable, NULL, 0);
111 * gdk_gc_new_with_values:
112 * @drawable: a #GdkDrawable. The created GC must always be used
113 * with drawables of the same depth as this one.
114 * @values: a structure containing initial values for the GC.
115 * @values_mask: a bit mask indicating which fields in @values
118 * Create a new GC with the given initial values.
120 * Return value: the new graphics context.
123 gdk_gc_new_with_values (GdkDrawable *drawable,
125 GdkGCValuesMask values_mask)
127 g_return_val_if_fail (drawable != NULL, NULL);
129 return GDK_DRAWABLE_GET_CLASS (drawable)->create_gc (drawable,
137 * @drawable: a #GdkDrawable.
138 * @values: a structure containing initial values for the GC.
139 * @values_mask: a bit mask indicating which fields in @values
142 * Does initialization of the generic portions of a #GdkGC
143 * created with the specified values and values_mask. This
144 * should be called out of the implementation of
145 * GdkDrawable.create_gc() immediately after creating the
149 _gdk_gc_init (GdkGC *gc,
150 GdkDrawable *drawable,
152 GdkGCValuesMask values_mask)
156 g_return_if_fail (GDK_IS_GC (gc));
158 priv = GDK_GC_GET_PRIVATE (gc);
160 if (values_mask & GDK_GC_CLIP_X_ORIGIN)
161 gc->clip_x_origin = values->clip_x_origin;
162 if (values_mask & GDK_GC_CLIP_Y_ORIGIN)
163 gc->clip_y_origin = values->clip_y_origin;
164 if ((values_mask & GDK_GC_CLIP_MASK) && values->clip_mask)
165 priv->clip_mask = g_object_ref (values->clip_mask);
166 if (values_mask & GDK_GC_TS_X_ORIGIN)
167 gc->ts_x_origin = values->ts_x_origin;
168 if (values_mask & GDK_GC_TS_Y_ORIGIN)
169 gc->ts_y_origin = values->ts_y_origin;
170 if (values_mask & GDK_GC_FILL)
171 priv->fill = values->fill;
172 if (values_mask & GDK_GC_STIPPLE)
174 priv->stipple = values->stipple;
176 g_object_ref (priv->stipple);
178 if (values_mask & GDK_GC_TILE)
180 priv->tile = values->tile;
182 g_object_ref (priv->tile);
184 if (values_mask & GDK_GC_FOREGROUND)
185 priv->fg_pixel = values->foreground.pixel;
186 if (values_mask & GDK_GC_BACKGROUND)
187 priv->bg_pixel = values->background.pixel;
188 if (values_mask & GDK_GC_SUBWINDOW)
189 priv->subwindow_mode = values->subwindow_mode;
190 if (values_mask & GDK_GC_EXPOSURES)
191 priv->exposures = values->graphics_exposures;
193 priv->exposures = TRUE;
195 gc->colormap = gdk_drawable_get_colormap (drawable);
197 g_object_ref (gc->colormap);
201 gdk_gc_finalize (GObject *object)
203 GdkGC *gc = GDK_GC (object);
204 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
206 if (priv->clip_region)
207 cairo_region_destroy (priv->clip_region);
208 if (priv->old_clip_region)
209 cairo_region_destroy (priv->old_clip_region);
211 g_object_unref (priv->clip_mask);
212 if (priv->old_clip_mask)
213 g_object_unref (priv->old_clip_mask);
215 g_object_unref (gc->colormap);
217 g_object_unref (priv->tile);
219 g_object_unref (priv->stipple);
221 G_OBJECT_CLASS (gdk_gc_parent_class)->finalize (object);
227 * @values: the #GdkGCValues structure in which to store the results.
229 * Retrieves the current values from a graphics context. Note that
230 * only the pixel values of the @values->foreground and @values->background
231 * are filled, use gdk_colormap_query_color() to obtain the rgb values
235 gdk_gc_get_values (GdkGC *gc,
238 g_return_if_fail (GDK_IS_GC (gc));
239 g_return_if_fail (values != NULL);
241 GDK_GC_GET_CLASS (gc)->get_values (gc, values);
247 * @values: struct containing the new values
248 * @values_mask: mask indicating which struct fields are to be used
250 * Sets attributes of a graphics context in bulk. For each flag set in
251 * @values_mask, the corresponding field will be read from @values and
252 * set as the new value for @gc. If you're only setting a few values
253 * on @gc, calling individual "setter" functions is likely more
258 gdk_gc_set_values (GdkGC *gc,
260 GdkGCValuesMask values_mask)
264 g_return_if_fail (GDK_IS_GC (gc));
265 g_return_if_fail (values != NULL);
267 priv = GDK_GC_GET_PRIVATE (gc);
269 if ((values_mask & GDK_GC_CLIP_X_ORIGIN) ||
270 (values_mask & GDK_GC_CLIP_Y_ORIGIN) ||
271 (values_mask & GDK_GC_CLIP_MASK) ||
272 (values_mask & GDK_GC_SUBWINDOW))
273 _gdk_gc_remove_drawable_clip (gc);
275 if (values_mask & GDK_GC_CLIP_X_ORIGIN)
276 gc->clip_x_origin = values->clip_x_origin;
277 if (values_mask & GDK_GC_CLIP_Y_ORIGIN)
278 gc->clip_y_origin = values->clip_y_origin;
279 if (values_mask & GDK_GC_TS_X_ORIGIN)
280 gc->ts_x_origin = values->ts_x_origin;
281 if (values_mask & GDK_GC_TS_Y_ORIGIN)
282 gc->ts_y_origin = values->ts_y_origin;
283 if (values_mask & GDK_GC_CLIP_MASK)
287 g_object_unref (priv->clip_mask);
288 priv->clip_mask = NULL;
290 if (values->clip_mask)
291 priv->clip_mask = g_object_ref (values->clip_mask);
293 if (priv->clip_region)
295 cairo_region_destroy (priv->clip_region);
296 priv->clip_region = NULL;
299 if (values_mask & GDK_GC_FILL)
300 priv->fill = values->fill;
301 if (values_mask & GDK_GC_STIPPLE)
303 if (priv->stipple != values->stipple)
306 g_object_unref (priv->stipple);
307 priv->stipple = values->stipple;
309 g_object_ref (priv->stipple);
312 if (values_mask & GDK_GC_TILE)
314 if (priv->tile != values->tile)
317 g_object_unref (priv->tile);
318 priv->tile = values->tile;
320 g_object_ref (priv->tile);
323 if (values_mask & GDK_GC_FOREGROUND)
324 priv->fg_pixel = values->foreground.pixel;
325 if (values_mask & GDK_GC_BACKGROUND)
326 priv->bg_pixel = values->background.pixel;
327 if (values_mask & GDK_GC_SUBWINDOW)
328 priv->subwindow_mode = values->subwindow_mode;
329 if (values_mask & GDK_GC_EXPOSURES)
330 priv->exposures = values->graphics_exposures;
332 GDK_GC_GET_CLASS (gc)->set_values (gc, values, values_mask);
336 * gdk_gc_set_foreground:
338 * @color: the new foreground color.
340 * Sets the foreground color for a graphics context.
341 * Note that this function uses @color->pixel, use
342 * gdk_gc_set_rgb_fg_color() to specify the foreground
343 * color as red, green, blue components.
346 gdk_gc_set_foreground (GdkGC *gc,
347 const GdkColor *color)
351 g_return_if_fail (GDK_IS_GC (gc));
352 g_return_if_fail (color != NULL);
354 values.foreground = *color;
355 gdk_gc_set_values (gc, &values, GDK_GC_FOREGROUND);
359 * gdk_gc_set_background:
361 * @color: the new background color.
363 * Sets the background color for a graphics context.
364 * Note that this function uses @color->pixel, use
365 * gdk_gc_set_rgb_bg_color() to specify the background
366 * color as red, green, blue components.
369 gdk_gc_set_background (GdkGC *gc,
370 const GdkColor *color)
374 g_return_if_fail (GDK_IS_GC (gc));
375 g_return_if_fail (color != NULL);
377 values.background = *color;
378 gdk_gc_set_values (gc, &values, GDK_GC_BACKGROUND);
382 * gdk_gc_set_function:
384 * @function: the #GdkFunction to use
386 * Determines how the current pixel values and the
387 * pixel values being drawn are combined to produce
388 * the final pixel values.
391 gdk_gc_set_function (GdkGC *gc,
392 GdkFunction function)
396 g_return_if_fail (GDK_IS_GC (gc));
398 values.function = function;
399 gdk_gc_set_values (gc, &values, GDK_GC_FUNCTION);
405 * @fill: the new fill mode.
407 * Set the fill mode for a graphics context.
410 gdk_gc_set_fill (GdkGC *gc,
415 g_return_if_fail (GDK_IS_GC (gc));
418 gdk_gc_set_values (gc, &values, GDK_GC_FILL);
424 * @tile: the new tile pixmap.
426 * Set a tile pixmap for a graphics context.
427 * This will only be used if the fill mode
431 gdk_gc_set_tile (GdkGC *gc,
436 g_return_if_fail (GDK_IS_GC (gc));
439 gdk_gc_set_values (gc, &values, GDK_GC_TILE);
443 * gdk_gc_set_stipple:
445 * @stipple: the new stipple bitmap.
447 * Set the stipple bitmap for a graphics context. The
448 * stipple will only be used if the fill mode is
449 * %GDK_STIPPLED or %GDK_OPAQUE_STIPPLED.
452 gdk_gc_set_stipple (GdkGC *gc,
457 g_return_if_fail (GDK_IS_GC (gc));
459 values.stipple = stipple;
460 gdk_gc_set_values (gc, &values, GDK_GC_STIPPLE);
464 * gdk_gc_set_ts_origin:
466 * @x: the x-coordinate of the origin.
467 * @y: the y-coordinate of the origin.
469 * Set the origin when using tiles or stipples with
470 * the GC. The tile or stipple will be aligned such
471 * that the upper left corner of the tile or stipple
472 * will coincide with this point.
475 gdk_gc_set_ts_origin (GdkGC *gc,
481 g_return_if_fail (GDK_IS_GC (gc));
483 values.ts_x_origin = x;
484 values.ts_y_origin = y;
486 gdk_gc_set_values (gc, &values,
487 GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN);
491 * gdk_gc_set_clip_origin:
493 * @x: the x-coordinate of the origin.
494 * @y: the y-coordinate of the origin.
496 * Sets the origin of the clip mask. The coordinates are
497 * interpreted relative to the upper-left corner of
498 * the destination drawable of the current operation.
501 gdk_gc_set_clip_origin (GdkGC *gc,
507 g_return_if_fail (GDK_IS_GC (gc));
509 values.clip_x_origin = x;
510 values.clip_y_origin = y;
512 gdk_gc_set_values (gc, &values,
513 GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
517 * gdk_gc_set_clip_mask:
521 * Sets the clip mask for a graphics context from a bitmap.
522 * The clip mask is interpreted relative to the clip
523 * origin. (See gdk_gc_set_clip_origin()).
526 gdk_gc_set_clip_mask (GdkGC *gc,
531 g_return_if_fail (GDK_IS_GC (gc));
533 values.clip_mask = mask;
534 gdk_gc_set_values (gc, &values, GDK_GC_CLIP_MASK);
537 /* Takes ownership of passed in region */
539 _gdk_gc_set_clip_region_real (GdkGC *gc,
540 cairo_region_t *region,
541 gboolean reset_origin)
543 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
547 g_object_unref (priv->clip_mask);
548 priv->clip_mask = NULL;
551 if (priv->clip_region)
552 cairo_region_destroy (priv->clip_region);
554 priv->clip_region = region;
556 _gdk_windowing_gc_set_clip_region (gc, region, reset_origin);
559 /* Doesn't copy region, allows not to reset origin */
561 _gdk_gc_set_clip_region_internal (GdkGC *gc,
562 cairo_region_t *region,
563 gboolean reset_origin)
565 _gdk_gc_remove_drawable_clip (gc);
566 _gdk_gc_set_clip_region_real (gc, region, reset_origin);
571 _gdk_gc_add_drawable_clip (GdkGC *gc,
573 cairo_region_t *region,
577 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
579 if (priv->region_tag_applied == region_tag &&
580 offset_x == priv->region_tag_offset_x &&
581 offset_y == priv->region_tag_offset_y)
582 return; /* Already appied this drawable region */
584 if (priv->region_tag_applied)
585 _gdk_gc_remove_drawable_clip (gc);
587 region = cairo_region_copy (region);
588 if (offset_x != 0 || offset_y != 0)
589 cairo_region_translate (region, offset_x, offset_y);
596 GdkColor black = {0, 0, 0, 0};
598 cairo_region_overlap_t overlap;
600 gdk_drawable_get_size (priv->clip_mask, &w, &h);
607 /* Its quite common to expose areas that are completely in or outside
608 * the region, so we try to avoid allocating bitmaps that are just fully
609 * set or completely unset.
611 overlap = cairo_region_contains_rectangle (region, &r);
612 if (overlap == CAIRO_REGION_OVERLAP_PART)
616 /* The region and the mask intersect, create a new clip mask that
617 includes both areas */
618 priv->old_clip_mask = g_object_ref (priv->clip_mask);
619 new_mask = gdk_pixmap_new (priv->old_clip_mask, w, h, -1);
621 cr = gdk_cairo_create (new_mask);
623 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
626 gdk_cairo_set_source_pixmap (cr, priv->old_clip_mask, 0, 0);
627 gdk_cairo_region (cr, region);
632 cairo_region_destroy (region);
634 gdk_gc_set_clip_mask (gc, new_mask);
636 g_object_unref (new_mask);
638 else if (overlap == CAIRO_REGION_OVERLAP_OUT)
640 /* No intersection, set empty clip region */
641 cairo_region_t *empty = cairo_region_create ();
643 cairo_region_destroy (region);
644 priv->old_clip_mask = g_object_ref (priv->clip_mask);
645 priv->clip_region = empty;
646 _gdk_windowing_gc_set_clip_region (gc, empty, FALSE);
650 /* Completely inside region, don't set unnecessary clip */
651 cairo_region_destroy (region);
657 priv->old_clip_region = priv->clip_region;
658 priv->clip_region = region;
659 if (priv->old_clip_region)
660 cairo_region_intersect (region, priv->old_clip_region);
662 _gdk_windowing_gc_set_clip_region (gc, priv->clip_region, FALSE);
665 priv->region_tag_applied = region_tag;
666 priv->region_tag_offset_x = offset_x;
667 priv->region_tag_offset_y = offset_y;
671 _gdk_gc_remove_drawable_clip (GdkGC *gc)
673 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
675 if (priv->region_tag_applied)
677 priv->region_tag_applied = 0;
678 if (priv->old_clip_mask)
680 gdk_gc_set_clip_mask (gc, priv->old_clip_mask);
681 g_object_unref (priv->old_clip_mask);
682 priv->old_clip_mask = NULL;
684 if (priv->clip_region)
686 g_object_unref (priv->clip_region);
687 priv->clip_region = NULL;
692 _gdk_gc_set_clip_region_real (gc, priv->old_clip_region, FALSE);
693 priv->old_clip_region = NULL;
699 * gdk_gc_set_clip_rectangle:
701 * @rectangle: the rectangle to clip to.
703 * Sets the clip mask for a graphics context from a
704 * rectangle. The clip mask is interpreted relative to the clip
705 * origin. (See gdk_gc_set_clip_origin()).
708 gdk_gc_set_clip_rectangle (GdkGC *gc,
709 const GdkRectangle *rectangle)
711 cairo_region_t *region;
713 g_return_if_fail (GDK_IS_GC (gc));
715 _gdk_gc_remove_drawable_clip (gc);
718 region = cairo_region_create_rectangle (rectangle);
722 _gdk_gc_set_clip_region_real (gc, region, TRUE);
726 * gdk_gc_set_clip_region:
728 * @region: the #cairo_region_t.
730 * Sets the clip mask for a graphics context from a region structure.
731 * The clip mask is interpreted relative to the clip origin. (See
732 * gdk_gc_set_clip_origin()).
735 gdk_gc_set_clip_region (GdkGC *gc,
736 const cairo_region_t *region)
738 cairo_region_t *copy;
740 g_return_if_fail (GDK_IS_GC (gc));
742 _gdk_gc_remove_drawable_clip (gc);
745 copy = cairo_region_copy (region);
749 _gdk_gc_set_clip_region_real (gc, copy, TRUE);
753 * _gdk_gc_get_clip_region:
756 * Gets the current clip region for @gc, if any.
758 * Return value: the clip region for the GC, or %NULL.
759 * (if a clip mask is set, the return will be %NULL)
760 * This value is owned by the GC and must not be freed.
763 _gdk_gc_get_clip_region (GdkGC *gc)
765 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
767 return GDK_GC_GET_PRIVATE (gc)->clip_region;
771 * _gdk_gc_get_clip_mask:
774 * Gets the current clip mask for @gc, if any.
776 * Return value: the clip mask for the GC, or %NULL.
777 * (if a clip region is set, the return will be %NULL)
778 * This value is owned by the GC and must not be freed.
781 _gdk_gc_get_clip_mask (GdkGC *gc)
783 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
785 return GDK_GC_GET_PRIVATE (gc)->clip_mask;
792 * Gets the current file style for the GC
794 * Return value: the file style for the GC
797 _gdk_gc_get_fill (GdkGC *gc)
799 g_return_val_if_fail (GDK_IS_GC (gc), GDK_SOLID);
801 return GDK_GC_GET_PRIVATE (gc)->fill;
805 _gdk_gc_get_exposures (GdkGC *gc)
807 g_return_val_if_fail (GDK_IS_GC (gc), FALSE);
809 return GDK_GC_GET_PRIVATE (gc)->exposures;
816 * Gets the tile pixmap for @gc, if any
818 * Return value: the tile set on the GC, or %NULL. The
819 * value is owned by the GC and must not be freed.
822 _gdk_gc_get_tile (GdkGC *gc)
824 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
826 return GDK_GC_GET_PRIVATE (gc)->tile;
830 * _gdk_gc_get_stipple:
833 * Gets the stipple pixmap for @gc, if any
835 * Return value: the stipple set on the GC, or %NULL. The
836 * value is owned by the GC and must not be freed.
839 _gdk_gc_get_stipple (GdkGC *gc)
841 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
843 return GDK_GC_GET_PRIVATE (gc)->stipple;
847 * _gdk_gc_get_fg_pixel:
850 * Gets the foreground pixel value for @gc. If the
851 * foreground pixel has never been set, returns the
854 * Return value: the foreground pixel value of the GC
857 _gdk_gc_get_fg_pixel (GdkGC *gc)
859 g_return_val_if_fail (GDK_IS_GC (gc), 0);
861 return GDK_GC_GET_PRIVATE (gc)->fg_pixel;
865 * _gdk_gc_get_bg_pixel:
868 * Gets the background pixel value for @gc.If the
869 * foreground pixel has never been set, returns the
872 * Return value: the foreground pixel value of the GC
875 _gdk_gc_get_bg_pixel (GdkGC *gc)
877 g_return_val_if_fail (GDK_IS_GC (gc), 0);
879 return GDK_GC_GET_PRIVATE (gc)->bg_pixel;
883 * gdk_gc_set_subwindow:
885 * @mode: the subwindow mode.
887 * Sets how drawing with this GC on a window will affect child
888 * windows of that window.
891 gdk_gc_set_subwindow (GdkGC *gc,
892 GdkSubwindowMode mode)
895 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
897 g_return_if_fail (GDK_IS_GC (gc));
899 /* This could get called a lot to reset the subwindow mode in
900 the client side clipping, so bail out early */
901 if (priv->subwindow_mode == mode)
904 values.subwindow_mode = mode;
905 gdk_gc_set_values (gc, &values, GDK_GC_SUBWINDOW);
909 _gdk_gc_get_subwindow (GdkGC *gc)
911 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
913 return priv->subwindow_mode;
917 * gdk_gc_set_exposures:
919 * @exposures: if %TRUE, exposure events will be generated.
921 * Sets whether copying non-visible portions of a drawable
922 * using this graphics context generate exposure events
923 * for the corresponding regions of the destination
924 * drawable. (See gdk_draw_drawable()).
927 gdk_gc_set_exposures (GdkGC *gc,
932 g_return_if_fail (GDK_IS_GC (gc));
934 values.graphics_exposures = exposures;
935 gdk_gc_set_values (gc, &values, GDK_GC_EXPOSURES);
939 * gdk_gc_set_line_attributes:
941 * @line_width: the width of lines.
942 * @line_style: the dash-style for lines.
943 * @cap_style: the manner in which the ends of lines are drawn.
944 * @join_style: the in which lines are joined together.
946 * Sets various attributes of how lines are drawn. See
947 * the corresponding members of #GdkGCValues for full
948 * explanations of the arguments.
951 gdk_gc_set_line_attributes (GdkGC *gc,
953 GdkLineStyle line_style,
954 GdkCapStyle cap_style,
955 GdkJoinStyle join_style)
959 values.line_width = line_width;
960 values.line_style = line_style;
961 values.cap_style = cap_style;
962 values.join_style = join_style;
964 gdk_gc_set_values (gc, &values,
974 * @dash_offset: the phase of the dash pattern.
975 * @dash_list: an array of dash lengths.
976 * @n: the number of elements in @dash_list.
978 * Sets the way dashed-lines are drawn. Lines will be
979 * drawn with alternating on and off segments of the
980 * lengths specified in @dash_list. The manner in
981 * which the on and off segments are drawn is determined
982 * by the @line_style value of the GC. (This can
983 * be changed with gdk_gc_set_line_attributes().)
985 * The @dash_offset defines the phase of the pattern,
986 * specifying how many pixels into the dash-list the pattern
987 * should actually begin.
990 gdk_gc_set_dashes (GdkGC *gc,
995 g_return_if_fail (GDK_IS_GC (gc));
996 g_return_if_fail (dash_list != NULL);
998 GDK_GC_GET_CLASS (gc)->set_dashes (gc, dash_offset, dash_list, n);
1004 * @x_offset: amount by which to offset the GC in the X direction
1005 * @y_offset: amount by which to offset the GC in the Y direction
1007 * Offset attributes such as the clip and tile-stipple origins
1008 * of the GC so that drawing at x - x_offset, y - y_offset with
1009 * the offset GC has the same effect as drawing at x, y with the original
1013 gdk_gc_offset (GdkGC *gc,
1017 if (x_offset != 0 || y_offset != 0)
1021 values.clip_x_origin = gc->clip_x_origin - x_offset;
1022 values.clip_y_origin = gc->clip_y_origin - y_offset;
1023 values.ts_x_origin = gc->ts_x_origin - x_offset;
1024 values.ts_y_origin = gc->ts_y_origin - y_offset;
1026 gdk_gc_set_values (gc, &values,
1027 GDK_GC_CLIP_X_ORIGIN |
1028 GDK_GC_CLIP_Y_ORIGIN |
1029 GDK_GC_TS_X_ORIGIN |
1030 GDK_GC_TS_Y_ORIGIN);
1036 * @dst_gc: the destination graphics context.
1037 * @src_gc: the source graphics context.
1039 * Copy the set of values from one graphics context
1040 * onto another graphics context.
1043 gdk_gc_copy (GdkGC *dst_gc,
1046 GdkGCPrivate *dst_priv, *src_priv;
1048 g_return_if_fail (GDK_IS_GC (dst_gc));
1049 g_return_if_fail (GDK_IS_GC (src_gc));
1051 dst_priv = GDK_GC_GET_PRIVATE (dst_gc);
1052 src_priv = GDK_GC_GET_PRIVATE (src_gc);
1054 _gdk_windowing_gc_copy (dst_gc, src_gc);
1056 dst_gc->clip_x_origin = src_gc->clip_x_origin;
1057 dst_gc->clip_y_origin = src_gc->clip_y_origin;
1058 dst_gc->ts_x_origin = src_gc->ts_x_origin;
1059 dst_gc->ts_y_origin = src_gc->ts_y_origin;
1061 if (src_gc->colormap)
1062 g_object_ref (src_gc->colormap);
1064 if (dst_gc->colormap)
1065 g_object_unref (dst_gc->colormap);
1067 dst_gc->colormap = src_gc->colormap;
1069 if (dst_priv->clip_region)
1070 cairo_region_destroy (dst_priv->clip_region);
1072 if (src_priv->clip_region)
1073 dst_priv->clip_region = cairo_region_copy (src_priv->clip_region);
1075 dst_priv->clip_region = NULL;
1077 dst_priv->region_tag_applied = src_priv->region_tag_applied;
1079 if (dst_priv->old_clip_region)
1080 cairo_region_destroy (dst_priv->old_clip_region);
1082 if (src_priv->old_clip_region)
1083 dst_priv->old_clip_region = cairo_region_copy (src_priv->old_clip_region);
1085 dst_priv->old_clip_region = NULL;
1087 if (src_priv->clip_mask)
1088 dst_priv->clip_mask = g_object_ref (src_priv->clip_mask);
1090 dst_priv->clip_mask = NULL;
1092 if (src_priv->old_clip_mask)
1093 dst_priv->old_clip_mask = g_object_ref (src_priv->old_clip_mask);
1095 dst_priv->old_clip_mask = NULL;
1097 dst_priv->fill = src_priv->fill;
1099 if (dst_priv->stipple)
1100 g_object_unref (dst_priv->stipple);
1101 dst_priv->stipple = src_priv->stipple;
1102 if (dst_priv->stipple)
1103 g_object_ref (dst_priv->stipple);
1106 g_object_unref (dst_priv->tile);
1107 dst_priv->tile = src_priv->tile;
1109 g_object_ref (dst_priv->tile);
1111 dst_priv->fg_pixel = src_priv->fg_pixel;
1112 dst_priv->bg_pixel = src_priv->bg_pixel;
1113 dst_priv->subwindow_mode = src_priv->subwindow_mode;
1114 dst_priv->exposures = src_priv->exposures;
1118 * gdk_gc_set_colormap:
1120 * @colormap: a #GdkColormap
1122 * Sets the colormap for the GC to the given colormap. The depth
1123 * of the colormap's visual must match the depth of the drawable
1124 * for which the GC was created.
1127 gdk_gc_set_colormap (GdkGC *gc,
1128 GdkColormap *colormap)
1130 g_return_if_fail (GDK_IS_GC (gc));
1131 g_return_if_fail (GDK_IS_COLORMAP (colormap));
1133 if (gc->colormap != colormap)
1136 g_object_unref (gc->colormap);
1138 gc->colormap = colormap;
1139 g_object_ref (gc->colormap);
1145 * gdk_gc_get_colormap:
1148 * Retrieves the colormap for a given GC, if it exists.
1149 * A GC will have a colormap if the drawable for which it was created
1150 * has a colormap, or if a colormap was set explicitely with
1151 * gdk_gc_set_colormap.
1153 * Return value: the colormap of @gc, or %NULL if @gc doesn't have one.
1156 gdk_gc_get_colormap (GdkGC *gc)
1158 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
1160 return gc->colormap;
1163 static GdkColormap *
1164 gdk_gc_get_colormap_warn (GdkGC *gc)
1166 GdkColormap *colormap = gdk_gc_get_colormap (gc);
1169 g_warning ("gdk_gc_set_rgb_fg_color() and gdk_gc_set_rgb_bg_color() can\n"
1170 "only be used on GC's with a colormap. A GC will have a colormap\n"
1171 "if it is created for a drawable with a colormap, or if a\n"
1172 "colormap has been set explicitly with gdk_gc_set_colormap.\n");
1180 * gdk_gc_set_rgb_fg_color:
1182 * @color: an unallocated #GdkColor.
1184 * Set the foreground color of a GC using an unallocated color. The
1185 * pixel value for the color will be determined using GdkRGB. If the
1186 * colormap for the GC has not previously been initialized for GdkRGB,
1187 * then for pseudo-color colormaps (colormaps with a small modifiable
1188 * number of colors), a colorcube will be allocated in the colormap.
1190 * Calling this function for a GC without a colormap is an error.
1193 gdk_gc_set_rgb_fg_color (GdkGC *gc,
1194 const GdkColor *color)
1199 g_return_if_fail (GDK_IS_GC (gc));
1200 g_return_if_fail (color != NULL);
1202 cmap = gdk_gc_get_colormap_warn (gc);
1207 if (!gdk_colormap_alloc_color (cmap, &tmp_color, FALSE, TRUE))
1209 gdk_gc_set_foreground (gc, &tmp_color);
1213 * gdk_gc_set_rgb_bg_color:
1215 * @color: an unallocated #GdkColor.
1217 * Set the background color of a GC using an unallocated color. The
1218 * pixel value for the color will be determined using GdkRGB. If the
1219 * colormap for the GC has not previously been initialized for GdkRGB,
1220 * then for pseudo-color colormaps (colormaps with a small modifiable
1221 * number of colors), a colorcube will be allocated in the colormap.
1223 * Calling this function for a GC without a colormap is an error.
1226 gdk_gc_set_rgb_bg_color (GdkGC *gc,
1227 const GdkColor *color)
1232 g_return_if_fail (GDK_IS_GC (gc));
1233 g_return_if_fail (color != NULL);
1235 cmap = gdk_gc_get_colormap_warn (gc);
1240 if (!gdk_colormap_alloc_color (cmap, &tmp_color, FALSE, TRUE))
1242 gdk_gc_set_background (gc, &tmp_color);
1245 static cairo_surface_t *
1246 make_stipple_tile_surface (cairo_t *cr,
1248 GdkColor *foreground,
1249 GdkColor *background)
1252 cairo_surface_t *surface;
1253 cairo_surface_t *alpha_surface;
1256 gdk_drawable_get_size (stipple,
1259 alpha_surface = _gdk_drawable_ref_cairo_surface (stipple);
1261 surface = cairo_surface_create_similar (cairo_get_target (cr),
1262 CAIRO_CONTENT_COLOR_ALPHA,
1265 tmp_cr = cairo_create (surface);
1267 cairo_set_operator (tmp_cr, CAIRO_OPERATOR_SOURCE);
1270 gdk_cairo_set_source_color (tmp_cr, background);
1272 cairo_set_source_rgba (tmp_cr, 0, 0, 0 ,0);
1274 cairo_paint (tmp_cr);
1276 cairo_set_operator (tmp_cr, CAIRO_OPERATOR_OVER);
1278 gdk_cairo_set_source_color (tmp_cr, foreground);
1279 cairo_mask_surface (tmp_cr, alpha_surface, 0, 0);
1281 cairo_destroy (tmp_cr);
1282 cairo_surface_destroy (alpha_surface);
1288 gc_get_foreground (GdkGC *gc,
1291 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
1293 color->pixel = priv->bg_pixel;
1296 gdk_colormap_query_color (gc->colormap, priv->fg_pixel, color);
1298 g_warning ("No colormap in gc_get_foreground");
1302 gc_get_background (GdkGC *gc,
1305 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
1307 color->pixel = priv->bg_pixel;
1310 gdk_colormap_query_color (gc->colormap, priv->bg_pixel, color);
1312 g_warning ("No colormap in gc_get_background");
1316 * _gdk_gc_update_context:
1319 * @override_foreground: a foreground color to use to override the
1320 * foreground color of the GC
1321 * @override_stipple: a stipple pattern to use to override the
1322 * stipple from the GC. If this is present and the fill mode
1323 * of the GC isn't %GDK_STIPPLED or %GDK_OPAQUE_STIPPLED
1324 * the fill mode will be forced to %GDK_STIPPLED
1325 * @gc_changed: pass %FALSE if the @gc has not changed since the
1326 * last call to this function
1327 * @target_drawable: The drawable you're drawing in. If passed in
1328 * this is used for client side window clip emulation.
1330 * Set the attributes of a cairo context to match those of a #GdkGC
1331 * as far as possible. Some aspects of a #GdkGC, such as clip masks
1332 * and functions other than %GDK_COPY are not currently handled.
1335 _gdk_gc_update_context (GdkGC *gc,
1337 const GdkColor *override_foreground,
1338 GdkBitmap *override_stipple,
1339 gboolean gc_changed,
1340 GdkDrawable *target_drawable)
1344 GdkColor foreground;
1345 GdkColor background;
1346 cairo_surface_t *tile_surface = NULL;
1347 GdkBitmap *stipple = NULL;
1349 g_return_if_fail (GDK_IS_GC (gc));
1350 g_return_if_fail (cr != NULL);
1351 g_return_if_fail (override_stipple == NULL || GDK_IS_PIXMAP (override_stipple));
1353 priv = GDK_GC_GET_PRIVATE (gc);
1355 _gdk_gc_remove_drawable_clip (gc);
1358 if (override_stipple && fill != GDK_OPAQUE_STIPPLED)
1359 fill = GDK_STIPPLED;
1361 if (fill != GDK_TILED)
1363 if (override_foreground)
1364 foreground = *override_foreground;
1366 gc_get_foreground (gc, &foreground);
1369 if (fill == GDK_OPAQUE_STIPPLED)
1370 gc_get_background (gc, &background);
1382 case GDK_OPAQUE_STIPPLED:
1383 if (override_stipple)
1384 stipple = override_stipple;
1386 stipple = priv->stipple;
1396 gdk_cairo_set_source_color (cr, &foreground);
1399 tile_surface = _gdk_drawable_ref_cairo_surface (priv->tile);
1402 tile_surface = make_stipple_tile_surface (cr, stipple, &foreground, NULL);
1404 case GDK_OPAQUE_STIPPLED:
1405 tile_surface = make_stipple_tile_surface (cr, stipple, &foreground, &background);
1409 /* Tiles, stipples, and clip regions are all specified in device space,
1410 * not user space. For the clip region, we can simply change the matrix,
1411 * clip, then clip back, but for the source pattern, we need to
1412 * compute the right matrix.
1416 * CTM_inverse * Pattern_matrix = Translate(- ts_x, - ts_y)
1418 * (So that ts_x, ts_y in device space is taken to 0,0 in pattern
1419 * space). So, pattern_matrix = CTM * Translate(- ts_x, - tx_y);
1424 cairo_pattern_t *pattern = cairo_pattern_create_for_surface (tile_surface);
1425 cairo_matrix_t user_to_device;
1426 cairo_matrix_t user_to_pattern;
1427 cairo_matrix_t device_to_pattern;
1429 cairo_get_matrix (cr, &user_to_device);
1430 cairo_matrix_init_translate (&device_to_pattern,
1431 - gc->ts_x_origin, - gc->ts_y_origin);
1432 cairo_matrix_multiply (&user_to_pattern,
1433 &user_to_device, &device_to_pattern);
1435 cairo_pattern_set_matrix (pattern, &user_to_pattern);
1436 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
1437 cairo_set_source (cr, pattern);
1439 cairo_surface_destroy (tile_surface);
1440 cairo_pattern_destroy (pattern);
1446 cairo_reset_clip (cr);
1447 /* The reset above resets the window clip rect, so we want to re-set that */
1448 if (target_drawable && GDK_DRAWABLE_GET_CLASS (target_drawable)->set_cairo_clip)
1449 GDK_DRAWABLE_GET_CLASS (target_drawable)->set_cairo_clip (target_drawable, cr);
1451 if (priv->clip_region)
1455 cairo_identity_matrix (cr);
1456 cairo_translate (cr, gc->clip_x_origin, gc->clip_y_origin);
1458 cairo_new_path (cr);
1459 gdk_cairo_region (cr, priv->clip_region);