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"
35 #include "gdkprivate.h"
38 static void gdk_gc_finalize (GObject *object);
40 typedef struct _GdkGCPrivate GdkGCPrivate;
44 GdkRegion *clip_region;
46 guint32 region_tag_applied;
47 int region_tag_offset_x;
48 int region_tag_offset_y;
50 GdkRegion *old_clip_region;
51 GdkPixmap *old_clip_mask;
53 GdkSubwindowMode subwindow_mode;
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;
191 gc->colormap = gdk_drawable_get_colormap (drawable);
193 g_object_ref (gc->colormap);
197 gdk_gc_finalize (GObject *object)
199 GdkGC *gc = GDK_GC (object);
200 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
202 if (priv->clip_region)
203 gdk_region_destroy (priv->clip_region);
204 if (priv->old_clip_region)
205 gdk_region_destroy (priv->old_clip_region);
207 g_object_unref (priv->clip_mask);
208 if (priv->old_clip_mask)
209 g_object_unref (priv->old_clip_mask);
211 g_object_unref (gc->colormap);
213 g_object_unref (priv->tile);
215 g_object_unref (priv->stipple);
217 G_OBJECT_CLASS (gdk_gc_parent_class)->finalize (object);
224 * Deprecated function; use g_object_ref() instead.
226 * Return value: the gc.
229 gdk_gc_ref (GdkGC *gc)
231 return (GdkGC *) g_object_ref (gc);
238 * Decrement the reference count of @gc.
240 * Deprecated: Use g_object_unref() instead.
243 gdk_gc_unref (GdkGC *gc)
251 * @values: the #GdkGCValues structure in which to store the results.
253 * Retrieves the current values from a graphics context. Note that
254 * only the pixel values of the @values->foreground and @values->background
255 * are filled, use gdk_colormap_query_color() to obtain the rgb values
259 gdk_gc_get_values (GdkGC *gc,
262 g_return_if_fail (GDK_IS_GC (gc));
263 g_return_if_fail (values != NULL);
265 GDK_GC_GET_CLASS (gc)->get_values (gc, values);
271 * @values: struct containing the new values
272 * @values_mask: mask indicating which struct fields are to be used
274 * Sets attributes of a graphics context in bulk. For each flag set in
275 * @values_mask, the corresponding field will be read from @values and
276 * set as the new value for @gc. If you're only setting a few values
277 * on @gc, calling individual "setter" functions is likely more
282 gdk_gc_set_values (GdkGC *gc,
284 GdkGCValuesMask values_mask)
288 g_return_if_fail (GDK_IS_GC (gc));
289 g_return_if_fail (values != NULL);
291 priv = GDK_GC_GET_PRIVATE (gc);
293 if ((values_mask & GDK_GC_CLIP_X_ORIGIN) ||
294 (values_mask & GDK_GC_CLIP_Y_ORIGIN) ||
295 (values_mask & GDK_GC_CLIP_MASK) ||
296 (values_mask & GDK_GC_SUBWINDOW))
297 _gdk_gc_remove_drawable_clip (gc);
299 if (values_mask & GDK_GC_CLIP_X_ORIGIN)
300 gc->clip_x_origin = values->clip_x_origin;
301 if (values_mask & GDK_GC_CLIP_Y_ORIGIN)
302 gc->clip_y_origin = values->clip_y_origin;
303 if (values_mask & GDK_GC_TS_X_ORIGIN)
304 gc->ts_x_origin = values->ts_x_origin;
305 if (values_mask & GDK_GC_TS_Y_ORIGIN)
306 gc->ts_y_origin = values->ts_y_origin;
307 if (values_mask & GDK_GC_CLIP_MASK)
311 g_object_unref (priv->clip_mask);
312 priv->clip_mask = NULL;
314 if (values->clip_mask)
315 priv->clip_mask = g_object_ref (values->clip_mask);
317 if (priv->clip_region)
319 gdk_region_destroy (priv->clip_region);
320 priv->clip_region = NULL;
323 if (values_mask & GDK_GC_FILL)
324 priv->fill = values->fill;
325 if (values_mask & GDK_GC_STIPPLE)
327 if (priv->stipple != values->stipple)
330 g_object_unref (priv->stipple);
331 priv->stipple = values->stipple;
333 g_object_ref (priv->stipple);
336 if (values_mask & GDK_GC_TILE)
338 if (priv->tile != values->tile)
341 g_object_unref (priv->tile);
342 priv->tile = values->tile;
344 g_object_ref (priv->tile);
347 if (values_mask & GDK_GC_FOREGROUND)
348 priv->fg_pixel = values->foreground.pixel;
349 if (values_mask & GDK_GC_BACKGROUND)
350 priv->bg_pixel = values->background.pixel;
351 if (values_mask & GDK_GC_SUBWINDOW)
352 priv->subwindow_mode = values->subwindow_mode;
354 GDK_GC_GET_CLASS (gc)->set_values (gc, values, values_mask);
358 * gdk_gc_set_foreground:
360 * @color: the new foreground color.
362 * Sets the foreground color for a graphics context.
363 * Note that this function uses @color->pixel, use
364 * gdk_gc_set_rgb_fg_color() to specify the foreground
365 * color as red, green, blue components.
368 gdk_gc_set_foreground (GdkGC *gc,
369 const GdkColor *color)
373 g_return_if_fail (GDK_IS_GC (gc));
374 g_return_if_fail (color != NULL);
376 values.foreground = *color;
377 gdk_gc_set_values (gc, &values, GDK_GC_FOREGROUND);
381 * gdk_gc_set_background:
383 * @color: the new background color.
385 * Sets the background color for a graphics context.
386 * Note that this function uses @color->pixel, use
387 * gdk_gc_set_rgb_bg_color() to specify the background
388 * color as red, green, blue components.
391 gdk_gc_set_background (GdkGC *gc,
392 const GdkColor *color)
396 g_return_if_fail (GDK_IS_GC (gc));
397 g_return_if_fail (color != NULL);
399 values.background = *color;
400 gdk_gc_set_values (gc, &values, GDK_GC_BACKGROUND);
406 * @font: the new font.
408 * Sets the font for a graphics context. (Note that
409 * all text-drawing functions in GDK take a @font
410 * argument; the value set here is used when that
411 * argument is %NULL.)
414 gdk_gc_set_font (GdkGC *gc,
419 g_return_if_fail (GDK_IS_GC (gc));
420 g_return_if_fail (font != NULL);
423 gdk_gc_set_values (gc, &values, GDK_GC_FONT);
427 * gdk_gc_set_function:
429 * @function: the #GdkFunction to use
431 * Determines how the current pixel values and the
432 * pixel values being drawn are combined to produce
433 * the final pixel values.
436 gdk_gc_set_function (GdkGC *gc,
437 GdkFunction function)
441 g_return_if_fail (GDK_IS_GC (gc));
443 values.function = function;
444 gdk_gc_set_values (gc, &values, GDK_GC_FUNCTION);
450 * @fill: the new fill mode.
452 * Set the fill mode for a graphics context.
455 gdk_gc_set_fill (GdkGC *gc,
460 g_return_if_fail (GDK_IS_GC (gc));
463 gdk_gc_set_values (gc, &values, GDK_GC_FILL);
469 * @tile: the new tile pixmap.
471 * Set a tile pixmap for a graphics context.
472 * This will only be used if the fill mode
476 gdk_gc_set_tile (GdkGC *gc,
481 g_return_if_fail (GDK_IS_GC (gc));
484 gdk_gc_set_values (gc, &values, GDK_GC_TILE);
488 * gdk_gc_set_stipple:
490 * @stipple: the new stipple bitmap.
492 * Set the stipple bitmap for a graphics context. The
493 * stipple will only be used if the fill mode is
494 * %GDK_STIPPLED or %GDK_OPAQUE_STIPPLED.
497 gdk_gc_set_stipple (GdkGC *gc,
502 g_return_if_fail (GDK_IS_GC (gc));
504 values.stipple = stipple;
505 gdk_gc_set_values (gc, &values, GDK_GC_STIPPLE);
509 * gdk_gc_set_ts_origin:
511 * @x: the x-coordinate of the origin.
512 * @y: the y-coordinate of the origin.
514 * Set the origin when using tiles or stipples with
515 * the GC. The tile or stipple will be aligned such
516 * that the upper left corner of the tile or stipple
517 * will coincide with this point.
520 gdk_gc_set_ts_origin (GdkGC *gc,
526 g_return_if_fail (GDK_IS_GC (gc));
528 values.ts_x_origin = x;
529 values.ts_y_origin = y;
531 gdk_gc_set_values (gc, &values,
532 GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN);
536 * gdk_gc_set_clip_origin:
538 * @x: the x-coordinate of the origin.
539 * @y: the y-coordinate of the origin.
541 * Sets the origin of the clip mask. The coordinates are
542 * interpreted relative to the upper-left corner of
543 * the destination drawable of the current operation.
546 gdk_gc_set_clip_origin (GdkGC *gc,
552 g_return_if_fail (GDK_IS_GC (gc));
554 values.clip_x_origin = x;
555 values.clip_y_origin = y;
557 gdk_gc_set_values (gc, &values,
558 GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
562 * gdk_gc_set_clip_mask:
566 * Sets the clip mask for a graphics context from a bitmap.
567 * The clip mask is interpreted relative to the clip
568 * origin. (See gdk_gc_set_clip_origin()).
571 gdk_gc_set_clip_mask (GdkGC *gc,
576 g_return_if_fail (GDK_IS_GC (gc));
578 values.clip_mask = mask;
579 gdk_gc_set_values (gc, &values, GDK_GC_CLIP_MASK);
582 /* Takes ownership of passed in region */
584 _gdk_gc_set_clip_region_real (GdkGC *gc,
586 gboolean reset_origin)
588 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
592 g_object_unref (priv->clip_mask);
593 priv->clip_mask = NULL;
596 if (priv->clip_region)
597 gdk_region_destroy (priv->clip_region);
599 priv->clip_region = region;
601 _gdk_windowing_gc_set_clip_region (gc, region, reset_origin);
604 /* Doesn't copy region, allows not to reset origin */
606 _gdk_gc_set_clip_region_internal (GdkGC *gc,
608 gboolean reset_origin)
610 _gdk_gc_remove_drawable_clip (gc);
611 _gdk_gc_set_clip_region_real (gc, region, reset_origin);
615 /* returns old clip region */
617 _gdk_gc_add_drawable_clip (GdkGC *gc,
623 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
625 if (priv->region_tag_applied == region_tag &&
626 offset_x == priv->region_tag_offset_x &&
627 offset_y == priv->region_tag_offset_y)
628 return; /* Already appied this drawable region */
630 if (priv->region_tag_applied)
631 _gdk_gc_remove_drawable_clip (gc);
633 region = gdk_region_copy (region);
634 if (offset_x != 0 || offset_y != 0)
635 gdk_region_offset (region, offset_x, offset_y);
642 GdkColor black = {0, 0, 0, 0};
644 priv->old_clip_mask = g_object_ref (priv->clip_mask);
645 gdk_drawable_get_size (priv->old_clip_mask, &w, &h);
647 new_mask = gdk_pixmap_new (priv->old_clip_mask, w, h, -1);
648 tmp_gc = _gdk_drawable_get_scratch_gc ((GdkDrawable *)new_mask, FALSE);
650 gdk_gc_set_foreground (tmp_gc, &black);
651 gdk_draw_rectangle (new_mask, tmp_gc, TRUE, 0, 0, -1, -1);
652 _gdk_gc_set_clip_region_internal (tmp_gc, region, TRUE); /* Takes ownership of region */
653 gdk_draw_drawable (new_mask,
659 gdk_gc_set_clip_region (tmp_gc, NULL);
660 gdk_gc_set_clip_mask (gc, new_mask);
664 priv->old_clip_region = priv->clip_region;
665 priv->clip_region = region;
666 if (priv->old_clip_region)
667 gdk_region_intersect (region, priv->old_clip_region);
669 _gdk_windowing_gc_set_clip_region (gc, priv->clip_region, FALSE);
672 priv->region_tag_applied = region_tag;
673 priv->region_tag_offset_x = offset_x;
674 priv->region_tag_offset_y = offset_y;
678 _gdk_gc_remove_drawable_clip (GdkGC *gc)
680 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
682 if (priv->region_tag_applied)
684 priv->region_tag_applied = 0;
685 if (priv->old_clip_mask)
687 gdk_gc_set_clip_mask (gc, priv->old_clip_mask);
688 g_object_unref (priv->old_clip_mask);
689 priv->old_clip_mask = NULL;
693 _gdk_gc_set_clip_region_real (gc, priv->old_clip_region, FALSE);
694 priv->old_clip_region = NULL;
700 * gdk_gc_set_clip_rectangle:
702 * @rectangle: the rectangle to clip to.
704 * Sets the clip mask for a graphics context from a
705 * rectangle. The clip mask is interpreted relative to the clip
706 * origin. (See gdk_gc_set_clip_origin()).
709 gdk_gc_set_clip_rectangle (GdkGC *gc,
710 const GdkRectangle *rectangle)
714 g_return_if_fail (GDK_IS_GC (gc));
716 _gdk_gc_remove_drawable_clip (gc);
719 region = gdk_region_rectangle (rectangle);
723 _gdk_gc_set_clip_region_real (gc, region, TRUE);
727 * gdk_gc_set_clip_region:
729 * @region: the #GdkRegion.
731 * Sets the clip mask for a graphics context from a region structure.
732 * The clip mask is interpreted relative to the clip origin. (See
733 * gdk_gc_set_clip_origin()).
736 gdk_gc_set_clip_region (GdkGC *gc,
737 const GdkRegion *region)
741 g_return_if_fail (GDK_IS_GC (gc));
743 _gdk_gc_remove_drawable_clip (gc);
746 copy = gdk_region_copy (region);
750 _gdk_gc_set_clip_region_real (gc, copy, TRUE);
754 * _gdk_gc_get_clip_region:
757 * Gets the current clip region for @gc, if any.
759 * Return value: the clip region for the GC, or %NULL.
760 * (if a clip mask is set, the return will be %NULL)
761 * This value is owned by the GC and must not be freed.
764 _gdk_gc_get_clip_region (GdkGC *gc)
766 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
768 return GDK_GC_GET_PRIVATE (gc)->clip_region;
775 * Gets the current file style for the GC
777 * Return value: the file style for the GC
780 _gdk_gc_get_fill (GdkGC *gc)
782 g_return_val_if_fail (GDK_IS_GC (gc), GDK_SOLID);
784 return GDK_GC_GET_PRIVATE (gc)->fill;
791 * Gets the tile pixmap for @gc, if any
793 * Return value: the tile set on the GC, or %NULL. The
794 * value is owned by the GC and must not be freed.
797 _gdk_gc_get_tile (GdkGC *gc)
799 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
801 return GDK_GC_GET_PRIVATE (gc)->tile;
805 * _gdk_gc_get_stipple:
808 * Gets the stipple pixmap for @gc, if any
810 * Return value: the stipple set on the GC, or %NULL. The
811 * value is owned by the GC and must not be freed.
814 _gdk_gc_get_stipple (GdkGC *gc)
816 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
818 return GDK_GC_GET_PRIVATE (gc)->stipple;
822 * _gdk_gc_get_fg_pixel:
825 * Gets the foreground pixel value for @gc. If the
826 * foreground pixel has never been set, returns the
829 * Return value: the foreground pixel value of the GC
832 _gdk_gc_get_fg_pixel (GdkGC *gc)
834 g_return_val_if_fail (GDK_IS_GC (gc), 0);
836 return GDK_GC_GET_PRIVATE (gc)->fg_pixel;
840 * _gdk_gc_get_bg_pixel:
843 * Gets the background pixel value for @gc.If the
844 * foreground pixel has never been set, returns the
847 * Return value: the foreground pixel value of the GC
850 _gdk_gc_get_bg_pixel (GdkGC *gc)
852 g_return_val_if_fail (GDK_IS_GC (gc), 0);
854 return GDK_GC_GET_PRIVATE (gc)->bg_pixel;
858 * gdk_gc_set_subwindow:
860 * @mode: the subwindow mode.
862 * Sets how drawing with this GC on a window will affect child
863 * windows of that window.
866 gdk_gc_set_subwindow (GdkGC *gc,
867 GdkSubwindowMode mode)
870 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
872 g_return_if_fail (GDK_IS_GC (gc));
874 /* This could get called a lot to reset the subwindow mode in
875 the client side clipping, so bail out early */
876 if (priv->subwindow_mode == mode)
879 values.subwindow_mode = mode;
880 gdk_gc_set_values (gc, &values, GDK_GC_SUBWINDOW);
884 _gdk_gc_get_subwindow (GdkGC *gc)
886 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
888 return priv->subwindow_mode;
892 * gdk_gc_set_exposures:
894 * @exposures: if %TRUE, exposure events will be generated.
896 * Sets whether copying non-visible portions of a drawable
897 * using this graphics context generate exposure events
898 * for the corresponding regions of the destination
899 * drawable. (See gdk_draw_drawable()).
902 gdk_gc_set_exposures (GdkGC *gc,
907 g_return_if_fail (GDK_IS_GC (gc));
909 values.graphics_exposures = exposures;
910 gdk_gc_set_values (gc, &values, GDK_GC_EXPOSURES);
914 * gdk_gc_set_line_attributes:
916 * @line_width: the width of lines.
917 * @line_style: the dash-style for lines.
918 * @cap_style: the manner in which the ends of lines are drawn.
919 * @join_style: the in which lines are joined together.
921 * Sets various attributes of how lines are drawn. See
922 * the corresponding members of #GdkGCValues for full
923 * explanations of the arguments.
926 gdk_gc_set_line_attributes (GdkGC *gc,
928 GdkLineStyle line_style,
929 GdkCapStyle cap_style,
930 GdkJoinStyle join_style)
934 values.line_width = line_width;
935 values.line_style = line_style;
936 values.cap_style = cap_style;
937 values.join_style = join_style;
939 gdk_gc_set_values (gc, &values,
949 * @dash_offset: the phase of the dash pattern.
950 * @dash_list: an array of dash lengths.
951 * @n: the number of elements in @dash_list.
953 * Sets the way dashed-lines are drawn. Lines will be
954 * drawn with alternating on and off segments of the
955 * lengths specified in @dash_list. The manner in
956 * which the on and off segments are drawn is determined
957 * by the @line_style value of the GC. (This can
958 * be changed with gdk_gc_set_line_attributes().)
960 * The @dash_offset defines the phase of the pattern,
961 * specifying how many pixels into the dash-list the pattern
962 * should actually begin.
965 gdk_gc_set_dashes (GdkGC *gc,
970 g_return_if_fail (GDK_IS_GC (gc));
971 g_return_if_fail (dash_list != NULL);
973 GDK_GC_GET_CLASS (gc)->set_dashes (gc, dash_offset, dash_list, n);
979 * @x_offset: amount by which to offset the GC in the X direction
980 * @y_offset: amount by which to offset the GC in the Y direction
982 * Offset attributes such as the clip and tile-stipple origins
983 * of the GC so that drawing at x - x_offset, y - y_offset with
984 * the offset GC has the same effect as drawing at x, y with the original
988 gdk_gc_offset (GdkGC *gc,
992 if (x_offset != 0 || y_offset != 0)
996 values.clip_x_origin = gc->clip_x_origin - x_offset;
997 values.clip_y_origin = gc->clip_y_origin - y_offset;
998 values.ts_x_origin = gc->ts_x_origin - x_offset;
999 values.ts_y_origin = gc->ts_y_origin - y_offset;
1001 gdk_gc_set_values (gc, &values,
1002 GDK_GC_CLIP_X_ORIGIN |
1003 GDK_GC_CLIP_Y_ORIGIN |
1004 GDK_GC_TS_X_ORIGIN |
1005 GDK_GC_TS_Y_ORIGIN);
1011 * @dst_gc: the destination graphics context.
1012 * @src_gc: the source graphics context.
1014 * Copy the set of values from one graphics context
1015 * onto another graphics context.
1018 gdk_gc_copy (GdkGC *dst_gc,
1021 GdkGCPrivate *dst_priv, *src_priv;
1023 g_return_if_fail (GDK_IS_GC (dst_gc));
1024 g_return_if_fail (GDK_IS_GC (src_gc));
1026 dst_priv = GDK_GC_GET_PRIVATE (dst_gc);
1027 src_priv = GDK_GC_GET_PRIVATE (src_gc);
1029 _gdk_windowing_gc_copy (dst_gc, src_gc);
1031 dst_gc->clip_x_origin = src_gc->clip_x_origin;
1032 dst_gc->clip_y_origin = src_gc->clip_y_origin;
1033 dst_gc->ts_x_origin = src_gc->ts_x_origin;
1034 dst_gc->ts_y_origin = src_gc->ts_y_origin;
1036 if (src_gc->colormap)
1037 g_object_ref (src_gc->colormap);
1039 if (dst_gc->colormap)
1040 g_object_unref (dst_gc->colormap);
1042 dst_gc->colormap = src_gc->colormap;
1044 if (dst_priv->clip_region)
1045 gdk_region_destroy (dst_priv->clip_region);
1047 if (src_priv->clip_region)
1048 dst_priv->clip_region = gdk_region_copy (src_priv->clip_region);
1050 dst_priv->clip_region = NULL;
1052 dst_priv->region_tag_applied = src_priv->region_tag_applied;
1054 if (dst_priv->old_clip_region)
1055 gdk_region_destroy (dst_priv->old_clip_region);
1057 if (src_priv->old_clip_region)
1058 dst_priv->old_clip_region = gdk_region_copy (src_priv->old_clip_region);
1060 dst_priv->old_clip_region = NULL;
1062 if (src_priv->clip_mask)
1063 dst_priv->clip_mask = g_object_ref (src_priv->clip_mask);
1065 dst_priv->clip_mask = NULL;
1067 if (src_priv->old_clip_mask)
1068 dst_priv->old_clip_mask = g_object_ref (src_priv->old_clip_mask);
1070 dst_priv->old_clip_mask = NULL;
1072 dst_priv->fill = src_priv->fill;
1074 if (dst_priv->stipple)
1075 g_object_unref (dst_priv->stipple);
1076 dst_priv->stipple = src_priv->stipple;
1077 if (dst_priv->stipple)
1078 g_object_ref (dst_priv->stipple);
1081 g_object_unref (dst_priv->tile);
1082 dst_priv->tile = src_priv->tile;
1084 g_object_ref (dst_priv->tile);
1086 dst_priv->fg_pixel = src_priv->fg_pixel;
1087 dst_priv->bg_pixel = src_priv->bg_pixel;
1088 dst_priv->subwindow_mode = src_priv->subwindow_mode;
1092 * gdk_gc_set_colormap:
1094 * @colormap: a #GdkColormap
1096 * Sets the colormap for the GC to the given colormap. The depth
1097 * of the colormap's visual must match the depth of the drawable
1098 * for which the GC was created.
1101 gdk_gc_set_colormap (GdkGC *gc,
1102 GdkColormap *colormap)
1104 g_return_if_fail (GDK_IS_GC (gc));
1105 g_return_if_fail (GDK_IS_COLORMAP (colormap));
1107 if (gc->colormap != colormap)
1110 g_object_unref (gc->colormap);
1112 gc->colormap = colormap;
1113 g_object_ref (gc->colormap);
1119 * gdk_gc_get_colormap:
1122 * Retrieves the colormap for a given GC, if it exists.
1123 * A GC will have a colormap if the drawable for which it was created
1124 * has a colormap, or if a colormap was set explicitely with
1125 * gdk_gc_set_colormap.
1127 * Return value: the colormap of @gc, or %NULL if @gc doesn't have one.
1130 gdk_gc_get_colormap (GdkGC *gc)
1132 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
1134 return gc->colormap;
1137 static GdkColormap *
1138 gdk_gc_get_colormap_warn (GdkGC *gc)
1140 GdkColormap *colormap = gdk_gc_get_colormap (gc);
1143 g_warning ("gdk_gc_set_rgb_fg_color() and gdk_gc_set_rgb_bg_color() can\n"
1144 "only be used on GC's with a colormap. A GC will have a colormap\n"
1145 "if it is created for a drawable with a colormap, or if a\n"
1146 "colormap has been set explicitly with gdk_gc_set_colormap.\n");
1154 * gdk_gc_set_rgb_fg_color:
1156 * @color: an unallocated #GdkColor.
1158 * Set the foreground color of a GC using an unallocated color. The
1159 * pixel value for the color will be determined using GdkRGB. If the
1160 * colormap for the GC has not previously been initialized for GdkRGB,
1161 * then for pseudo-color colormaps (colormaps with a small modifiable
1162 * number of colors), a colorcube will be allocated in the colormap.
1164 * Calling this function for a GC without a colormap is an error.
1167 gdk_gc_set_rgb_fg_color (GdkGC *gc,
1168 const GdkColor *color)
1173 g_return_if_fail (GDK_IS_GC (gc));
1174 g_return_if_fail (color != NULL);
1176 cmap = gdk_gc_get_colormap_warn (gc);
1181 gdk_rgb_find_color (cmap, &tmp_color);
1182 gdk_gc_set_foreground (gc, &tmp_color);
1186 * gdk_gc_set_rgb_bg_color:
1188 * @color: an unallocated #GdkColor.
1190 * Set the background color of a GC using an unallocated color. The
1191 * pixel value for the color will be determined using GdkRGB. If the
1192 * colormap for the GC has not previously been initialized for GdkRGB,
1193 * then for pseudo-color colormaps (colormaps with a small modifiable
1194 * number of colors), a colorcube will be allocated in the colormap.
1196 * Calling this function for a GC without a colormap is an error.
1199 gdk_gc_set_rgb_bg_color (GdkGC *gc,
1200 const GdkColor *color)
1205 g_return_if_fail (GDK_IS_GC (gc));
1206 g_return_if_fail (color != NULL);
1208 cmap = gdk_gc_get_colormap_warn (gc);
1213 gdk_rgb_find_color (cmap, &tmp_color);
1214 gdk_gc_set_background (gc, &tmp_color);
1217 static cairo_surface_t *
1218 make_stipple_tile_surface (cairo_t *cr,
1220 GdkColor *foreground,
1221 GdkColor *background)
1224 cairo_surface_t *surface;
1225 cairo_surface_t *alpha_surface;
1228 gdk_drawable_get_size (stipple,
1231 alpha_surface = _gdk_drawable_ref_cairo_surface (stipple);
1233 surface = cairo_surface_create_similar (cairo_get_target (cr),
1234 CAIRO_CONTENT_COLOR_ALPHA,
1237 tmp_cr = cairo_create (surface);
1239 cairo_set_operator (tmp_cr, CAIRO_OPERATOR_SOURCE);
1242 gdk_cairo_set_source_color (tmp_cr, background);
1244 cairo_set_source_rgba (tmp_cr, 0, 0, 0 ,0);
1246 cairo_paint (tmp_cr);
1248 cairo_set_operator (tmp_cr, CAIRO_OPERATOR_OVER);
1250 gdk_cairo_set_source_color (tmp_cr, foreground);
1251 cairo_mask_surface (tmp_cr, alpha_surface, 0, 0);
1253 cairo_destroy (tmp_cr);
1254 cairo_surface_destroy (alpha_surface);
1260 gc_get_foreground (GdkGC *gc,
1263 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
1265 color->pixel = priv->bg_pixel;
1268 gdk_colormap_query_color (gc->colormap, priv->fg_pixel, color);
1270 g_warning ("No colormap in gc_get_foreground");
1274 gc_get_background (GdkGC *gc,
1277 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
1279 color->pixel = priv->bg_pixel;
1282 gdk_colormap_query_color (gc->colormap, priv->bg_pixel, color);
1284 g_warning ("No colormap in gc_get_background");
1288 * _gdk_gc_update_context:
1291 * @override_foreground: a foreground color to use to override the
1292 * foreground color of the GC
1293 * @override_stipple: a stipple pattern to use to override the
1294 * stipple from the GC. If this is present and the fill mode
1295 * of the GC isn't %GDK_STIPPLED or %GDK_OPAQUE_STIPPLED
1296 * the fill mode will be forced to %GDK_STIPPLED
1297 * @gc_changed: pass %FALSE if the @gc has not changed since the
1298 * last call to this function
1299 * @target_drawable: The drawable you're drawing in. If passed in
1300 * this is used for client side window clip emulation.
1302 * Set the attributes of a cairo context to match those of a #GdkGC
1303 * as far as possible. Some aspects of a #GdkGC, such as clip masks
1304 * and functions other than %GDK_COPY are not currently handled.
1307 _gdk_gc_update_context (GdkGC *gc,
1309 const GdkColor *override_foreground,
1310 GdkBitmap *override_stipple,
1311 gboolean gc_changed,
1312 GdkDrawable *target_drawable)
1316 GdkColor foreground;
1317 GdkColor background;
1318 cairo_surface_t *tile_surface = NULL;
1319 GdkBitmap *stipple = NULL;
1321 g_return_if_fail (GDK_IS_GC (gc));
1322 g_return_if_fail (cr != NULL);
1323 g_return_if_fail (override_stipple == NULL || GDK_IS_PIXMAP (override_stipple));
1325 priv = GDK_GC_GET_PRIVATE (gc);
1328 if (override_stipple && fill != GDK_OPAQUE_STIPPLED)
1329 fill = GDK_STIPPLED;
1331 if (fill != GDK_TILED)
1333 if (override_foreground)
1334 foreground = *override_foreground;
1336 gc_get_foreground (gc, &foreground);
1339 if (fill == GDK_OPAQUE_STIPPLED)
1340 gc_get_background (gc, &background);
1352 case GDK_OPAQUE_STIPPLED:
1353 if (override_stipple)
1354 stipple = override_stipple;
1356 stipple = priv->stipple;
1366 gdk_cairo_set_source_color (cr, &foreground);
1369 tile_surface = _gdk_drawable_ref_cairo_surface (priv->tile);
1372 tile_surface = make_stipple_tile_surface (cr, stipple, &foreground, NULL);
1374 case GDK_OPAQUE_STIPPLED:
1375 tile_surface = make_stipple_tile_surface (cr, stipple, &foreground, &background);
1379 /* Tiles, stipples, and clip regions are all specified in device space,
1380 * not user space. For the clip region, we can simply change the matrix,
1381 * clip, then clip back, but for the source pattern, we need to
1382 * compute the right matrix.
1386 * CTM_inverse * Pattern_matrix = Translate(- ts_x, - ts_y)
1388 * (So that ts_x, ts_y in device space is taken to 0,0 in pattern
1389 * space). So, pattern_matrix = CTM * Translate(- ts_x, - tx_y);
1394 cairo_pattern_t *pattern = cairo_pattern_create_for_surface (tile_surface);
1395 cairo_matrix_t user_to_device;
1396 cairo_matrix_t user_to_pattern;
1397 cairo_matrix_t device_to_pattern;
1399 cairo_get_matrix (cr, &user_to_device);
1400 cairo_matrix_init_translate (&device_to_pattern,
1401 - gc->ts_x_origin, - gc->ts_y_origin);
1402 cairo_matrix_multiply (&user_to_pattern,
1403 &user_to_device, &device_to_pattern);
1405 cairo_pattern_set_matrix (pattern, &user_to_pattern);
1406 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
1407 cairo_set_source (cr, pattern);
1409 cairo_surface_destroy (tile_surface);
1410 cairo_pattern_destroy (pattern);
1416 cairo_reset_clip (cr);
1417 if (priv->clip_region)
1421 cairo_identity_matrix (cr);
1422 cairo_translate (cr, gc->clip_x_origin, gc->clip_y_origin);
1424 cairo_new_path (cr);
1425 gdk_cairo_region (cr, priv->clip_region);
1432 /* The reset above resets the window clip rect, so we want to re-set that */
1433 if (target_drawable && GDK_DRAWABLE_GET_CLASS (target_drawable)->set_cairo_clip)
1434 GDK_DRAWABLE_GET_CLASS (target_drawable)->set_cairo_clip (target_drawable, cr);
1438 #define __GDK_GC_C__
1439 #include "gdkaliasdef.c"