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;
61 guint subwindow_mode : 1;
66 #define GDK_GC_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDK_TYPE_GC, GdkGCPrivate))
68 G_DEFINE_TYPE (GdkGC, gdk_gc, G_TYPE_OBJECT)
71 gdk_gc_class_init (GdkGCClass *class)
73 GObjectClass *object_class = G_OBJECT_CLASS (class);
75 object_class->finalize = gdk_gc_finalize;
77 g_type_class_add_private (object_class, sizeof (GdkGCPrivate));
81 gdk_gc_init (GdkGC *gc)
83 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
85 priv->fill = GDK_SOLID;
87 /* These are the default X11 value, which we match. They are clearly
88 * wrong for TrueColor displays, so apps have to change them.
96 * @drawable: a #GdkDrawable. The created GC must always be used
97 * with drawables of the same depth as this one.
99 * Create a new graphics context with default values.
101 * Returns: the new graphics context.
104 gdk_gc_new (GdkDrawable *drawable)
106 g_return_val_if_fail (drawable != NULL, NULL);
108 return gdk_gc_new_with_values (drawable, NULL, 0);
112 * gdk_gc_new_with_values:
113 * @drawable: a #GdkDrawable. The created GC must always be used
114 * with drawables of the same depth as this one.
115 * @values: a structure containing initial values for the GC.
116 * @values_mask: a bit mask indicating which fields in @values
119 * Create a new GC with the given initial values.
121 * Return value: the new graphics context.
124 gdk_gc_new_with_values (GdkDrawable *drawable,
126 GdkGCValuesMask values_mask)
128 g_return_val_if_fail (drawable != NULL, NULL);
130 return GDK_DRAWABLE_GET_CLASS (drawable)->create_gc (drawable,
138 * @drawable: a #GdkDrawable.
139 * @values: a structure containing initial values for the GC.
140 * @values_mask: a bit mask indicating which fields in @values
143 * Does initialization of the generic portions of a #GdkGC
144 * created with the specified values and values_mask. This
145 * should be called out of the implementation of
146 * GdkDrawable.create_gc() immediately after creating the
150 _gdk_gc_init (GdkGC *gc,
151 GdkDrawable *drawable,
153 GdkGCValuesMask values_mask)
157 g_return_if_fail (GDK_IS_GC (gc));
159 priv = GDK_GC_GET_PRIVATE (gc);
161 if (values_mask & GDK_GC_CLIP_X_ORIGIN)
162 gc->clip_x_origin = values->clip_x_origin;
163 if (values_mask & GDK_GC_CLIP_Y_ORIGIN)
164 gc->clip_y_origin = values->clip_y_origin;
165 if ((values_mask & GDK_GC_CLIP_MASK) && values->clip_mask)
166 priv->clip_mask = g_object_ref (values->clip_mask);
167 if (values_mask & GDK_GC_TS_X_ORIGIN)
168 gc->ts_x_origin = values->ts_x_origin;
169 if (values_mask & GDK_GC_TS_Y_ORIGIN)
170 gc->ts_y_origin = values->ts_y_origin;
171 if (values_mask & GDK_GC_FILL)
172 priv->fill = values->fill;
173 if (values_mask & GDK_GC_STIPPLE)
175 priv->stipple = values->stipple;
177 g_object_ref (priv->stipple);
179 if (values_mask & GDK_GC_TILE)
181 priv->tile = values->tile;
183 g_object_ref (priv->tile);
185 if (values_mask & GDK_GC_FOREGROUND)
186 priv->fg_pixel = values->foreground.pixel;
187 if (values_mask & GDK_GC_BACKGROUND)
188 priv->bg_pixel = values->background.pixel;
189 if (values_mask & GDK_GC_SUBWINDOW)
190 priv->subwindow_mode = values->subwindow_mode;
191 if (values_mask & GDK_GC_EXPOSURES)
192 priv->exposures = values->graphics_exposures;
194 priv->exposures = TRUE;
196 gc->colormap = gdk_drawable_get_colormap (drawable);
198 g_object_ref (gc->colormap);
202 gdk_gc_finalize (GObject *object)
204 GdkGC *gc = GDK_GC (object);
205 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
207 if (priv->clip_region)
208 gdk_region_destroy (priv->clip_region);
209 if (priv->old_clip_region)
210 gdk_region_destroy (priv->old_clip_region);
212 g_object_unref (priv->clip_mask);
213 if (priv->old_clip_mask)
214 g_object_unref (priv->old_clip_mask);
216 g_object_unref (gc->colormap);
218 g_object_unref (priv->tile);
220 g_object_unref (priv->stipple);
222 G_OBJECT_CLASS (gdk_gc_parent_class)->finalize (object);
229 * Deprecated function; use g_object_ref() instead.
231 * Return value: the gc.
233 * Deprecated: 2.0: Use g_object_ref() instead.
236 gdk_gc_ref (GdkGC *gc)
238 return (GdkGC *) g_object_ref (gc);
245 * Decrement the reference count of @gc.
247 * Deprecated: 2.0: Use g_object_unref() instead.
250 gdk_gc_unref (GdkGC *gc)
258 * @values: the #GdkGCValues structure in which to store the results.
260 * Retrieves the current values from a graphics context. Note that
261 * only the pixel values of the @values->foreground and @values->background
262 * are filled, use gdk_colormap_query_color() to obtain the rgb values
266 gdk_gc_get_values (GdkGC *gc,
269 g_return_if_fail (GDK_IS_GC (gc));
270 g_return_if_fail (values != NULL);
272 GDK_GC_GET_CLASS (gc)->get_values (gc, values);
278 * @values: struct containing the new values
279 * @values_mask: mask indicating which struct fields are to be used
281 * Sets attributes of a graphics context in bulk. For each flag set in
282 * @values_mask, the corresponding field will be read from @values and
283 * set as the new value for @gc. If you're only setting a few values
284 * on @gc, calling individual "setter" functions is likely more
289 gdk_gc_set_values (GdkGC *gc,
291 GdkGCValuesMask values_mask)
295 g_return_if_fail (GDK_IS_GC (gc));
296 g_return_if_fail (values != NULL);
298 priv = GDK_GC_GET_PRIVATE (gc);
300 if ((values_mask & GDK_GC_CLIP_X_ORIGIN) ||
301 (values_mask & GDK_GC_CLIP_Y_ORIGIN) ||
302 (values_mask & GDK_GC_CLIP_MASK) ||
303 (values_mask & GDK_GC_SUBWINDOW))
304 _gdk_gc_remove_drawable_clip (gc);
306 if (values_mask & GDK_GC_CLIP_X_ORIGIN)
307 gc->clip_x_origin = values->clip_x_origin;
308 if (values_mask & GDK_GC_CLIP_Y_ORIGIN)
309 gc->clip_y_origin = values->clip_y_origin;
310 if (values_mask & GDK_GC_TS_X_ORIGIN)
311 gc->ts_x_origin = values->ts_x_origin;
312 if (values_mask & GDK_GC_TS_Y_ORIGIN)
313 gc->ts_y_origin = values->ts_y_origin;
314 if (values_mask & GDK_GC_CLIP_MASK)
318 g_object_unref (priv->clip_mask);
319 priv->clip_mask = NULL;
321 if (values->clip_mask)
322 priv->clip_mask = g_object_ref (values->clip_mask);
324 if (priv->clip_region)
326 gdk_region_destroy (priv->clip_region);
327 priv->clip_region = NULL;
330 if (values_mask & GDK_GC_FILL)
331 priv->fill = values->fill;
332 if (values_mask & GDK_GC_STIPPLE)
334 if (priv->stipple != values->stipple)
337 g_object_unref (priv->stipple);
338 priv->stipple = values->stipple;
340 g_object_ref (priv->stipple);
343 if (values_mask & GDK_GC_TILE)
345 if (priv->tile != values->tile)
348 g_object_unref (priv->tile);
349 priv->tile = values->tile;
351 g_object_ref (priv->tile);
354 if (values_mask & GDK_GC_FOREGROUND)
355 priv->fg_pixel = values->foreground.pixel;
356 if (values_mask & GDK_GC_BACKGROUND)
357 priv->bg_pixel = values->background.pixel;
358 if (values_mask & GDK_GC_SUBWINDOW)
359 priv->subwindow_mode = values->subwindow_mode;
360 if (values_mask & GDK_GC_EXPOSURES)
361 priv->exposures = values->graphics_exposures;
363 GDK_GC_GET_CLASS (gc)->set_values (gc, values, values_mask);
367 * gdk_gc_set_foreground:
369 * @color: the new foreground color.
371 * Sets the foreground color for a graphics context.
372 * Note that this function uses @color->pixel, use
373 * gdk_gc_set_rgb_fg_color() to specify the foreground
374 * color as red, green, blue components.
377 gdk_gc_set_foreground (GdkGC *gc,
378 const GdkColor *color)
382 g_return_if_fail (GDK_IS_GC (gc));
383 g_return_if_fail (color != NULL);
385 values.foreground = *color;
386 gdk_gc_set_values (gc, &values, GDK_GC_FOREGROUND);
390 * gdk_gc_set_background:
392 * @color: the new background color.
394 * Sets the background color for a graphics context.
395 * Note that this function uses @color->pixel, use
396 * gdk_gc_set_rgb_bg_color() to specify the background
397 * color as red, green, blue components.
400 gdk_gc_set_background (GdkGC *gc,
401 const GdkColor *color)
405 g_return_if_fail (GDK_IS_GC (gc));
406 g_return_if_fail (color != NULL);
408 values.background = *color;
409 gdk_gc_set_values (gc, &values, GDK_GC_BACKGROUND);
415 * @font: the new font.
417 * Sets the font for a graphics context. (Note that
418 * all text-drawing functions in GDK take a @font
419 * argument; the value set here is used when that
420 * argument is %NULL.)
423 gdk_gc_set_font (GdkGC *gc,
428 g_return_if_fail (GDK_IS_GC (gc));
429 g_return_if_fail (font != NULL);
432 gdk_gc_set_values (gc, &values, GDK_GC_FONT);
436 * gdk_gc_set_function:
438 * @function: the #GdkFunction to use
440 * Determines how the current pixel values and the
441 * pixel values being drawn are combined to produce
442 * the final pixel values.
445 gdk_gc_set_function (GdkGC *gc,
446 GdkFunction function)
450 g_return_if_fail (GDK_IS_GC (gc));
452 values.function = function;
453 gdk_gc_set_values (gc, &values, GDK_GC_FUNCTION);
459 * @fill: the new fill mode.
461 * Set the fill mode for a graphics context.
464 gdk_gc_set_fill (GdkGC *gc,
469 g_return_if_fail (GDK_IS_GC (gc));
472 gdk_gc_set_values (gc, &values, GDK_GC_FILL);
478 * @tile: the new tile pixmap.
480 * Set a tile pixmap for a graphics context.
481 * This will only be used if the fill mode
485 gdk_gc_set_tile (GdkGC *gc,
490 g_return_if_fail (GDK_IS_GC (gc));
493 gdk_gc_set_values (gc, &values, GDK_GC_TILE);
497 * gdk_gc_set_stipple:
499 * @stipple: the new stipple bitmap.
501 * Set the stipple bitmap for a graphics context. The
502 * stipple will only be used if the fill mode is
503 * %GDK_STIPPLED or %GDK_OPAQUE_STIPPLED.
506 gdk_gc_set_stipple (GdkGC *gc,
511 g_return_if_fail (GDK_IS_GC (gc));
513 values.stipple = stipple;
514 gdk_gc_set_values (gc, &values, GDK_GC_STIPPLE);
518 * gdk_gc_set_ts_origin:
520 * @x: the x-coordinate of the origin.
521 * @y: the y-coordinate of the origin.
523 * Set the origin when using tiles or stipples with
524 * the GC. The tile or stipple will be aligned such
525 * that the upper left corner of the tile or stipple
526 * will coincide with this point.
529 gdk_gc_set_ts_origin (GdkGC *gc,
535 g_return_if_fail (GDK_IS_GC (gc));
537 values.ts_x_origin = x;
538 values.ts_y_origin = y;
540 gdk_gc_set_values (gc, &values,
541 GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN);
545 * gdk_gc_set_clip_origin:
547 * @x: the x-coordinate of the origin.
548 * @y: the y-coordinate of the origin.
550 * Sets the origin of the clip mask. The coordinates are
551 * interpreted relative to the upper-left corner of
552 * the destination drawable of the current operation.
555 gdk_gc_set_clip_origin (GdkGC *gc,
561 g_return_if_fail (GDK_IS_GC (gc));
563 values.clip_x_origin = x;
564 values.clip_y_origin = y;
566 gdk_gc_set_values (gc, &values,
567 GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
571 * gdk_gc_set_clip_mask:
575 * Sets the clip mask for a graphics context from a bitmap.
576 * The clip mask is interpreted relative to the clip
577 * origin. (See gdk_gc_set_clip_origin()).
580 gdk_gc_set_clip_mask (GdkGC *gc,
585 g_return_if_fail (GDK_IS_GC (gc));
587 values.clip_mask = mask;
588 gdk_gc_set_values (gc, &values, GDK_GC_CLIP_MASK);
591 /* Takes ownership of passed in region */
593 _gdk_gc_set_clip_region_real (GdkGC *gc,
595 gboolean reset_origin)
597 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
601 g_object_unref (priv->clip_mask);
602 priv->clip_mask = NULL;
605 if (priv->clip_region)
606 gdk_region_destroy (priv->clip_region);
608 priv->clip_region = region;
610 _gdk_windowing_gc_set_clip_region (gc, region, reset_origin);
613 /* Doesn't copy region, allows not to reset origin */
615 _gdk_gc_set_clip_region_internal (GdkGC *gc,
617 gboolean reset_origin)
619 _gdk_gc_remove_drawable_clip (gc);
620 _gdk_gc_set_clip_region_real (gc, region, reset_origin);
625 _gdk_gc_add_drawable_clip (GdkGC *gc,
631 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
633 if (priv->region_tag_applied == region_tag &&
634 offset_x == priv->region_tag_offset_x &&
635 offset_y == priv->region_tag_offset_y)
636 return; /* Already appied this drawable region */
638 if (priv->region_tag_applied)
639 _gdk_gc_remove_drawable_clip (gc);
641 region = gdk_region_copy (region);
642 if (offset_x != 0 || offset_y != 0)
643 gdk_region_offset (region, offset_x, offset_y);
650 GdkColor black = {0, 0, 0, 0};
652 GdkOverlapType overlap;
654 gdk_drawable_get_size (priv->clip_mask, &w, &h);
661 /* Its quite common to expose areas that are completely in or outside
662 * the region, so we try to avoid allocating bitmaps that are just fully
663 * set or completely unset.
665 overlap = gdk_region_rect_in (region, &r);
666 if (overlap == GDK_OVERLAP_RECTANGLE_PART)
668 /* The region and the mask intersect, create a new clip mask that
669 includes both areas */
670 priv->old_clip_mask = g_object_ref (priv->clip_mask);
671 new_mask = gdk_pixmap_new (priv->old_clip_mask, w, h, -1);
672 tmp_gc = _gdk_drawable_get_scratch_gc ((GdkDrawable *)new_mask, FALSE);
674 gdk_gc_set_foreground (tmp_gc, &black);
675 gdk_draw_rectangle (new_mask, tmp_gc, TRUE, 0, 0, -1, -1);
676 _gdk_gc_set_clip_region_internal (tmp_gc, region, TRUE); /* Takes ownership of region */
677 gdk_draw_drawable (new_mask,
683 gdk_gc_set_clip_region (tmp_gc, NULL);
684 gdk_gc_set_clip_mask (gc, new_mask);
686 else if (overlap == GDK_OVERLAP_RECTANGLE_OUT)
688 /* No intersection, set empty clip region */
689 GdkRegion *empty = gdk_region_new ();
691 gdk_region_destroy (region);
692 priv->old_clip_mask = g_object_ref (priv->clip_mask);
693 priv->clip_region = empty;
694 _gdk_windowing_gc_set_clip_region (gc, empty, FALSE);
698 /* Completely inside region, don't set unnecessary clip */
699 gdk_region_destroy (region);
705 priv->old_clip_region = priv->clip_region;
706 priv->clip_region = region;
707 if (priv->old_clip_region)
708 gdk_region_intersect (region, priv->old_clip_region);
710 _gdk_windowing_gc_set_clip_region (gc, priv->clip_region, FALSE);
713 priv->region_tag_applied = region_tag;
714 priv->region_tag_offset_x = offset_x;
715 priv->region_tag_offset_y = offset_y;
719 _gdk_gc_remove_drawable_clip (GdkGC *gc)
721 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
723 if (priv->region_tag_applied)
725 priv->region_tag_applied = 0;
726 if (priv->old_clip_mask)
728 gdk_gc_set_clip_mask (gc, priv->old_clip_mask);
729 g_object_unref (priv->old_clip_mask);
730 priv->old_clip_mask = NULL;
732 if (priv->clip_region)
734 g_object_unref (priv->clip_region);
735 priv->clip_region = NULL;
740 _gdk_gc_set_clip_region_real (gc, priv->old_clip_region, FALSE);
741 priv->old_clip_region = NULL;
747 * gdk_gc_set_clip_rectangle:
749 * @rectangle: the rectangle to clip to.
751 * Sets the clip mask for a graphics context from a
752 * rectangle. The clip mask is interpreted relative to the clip
753 * origin. (See gdk_gc_set_clip_origin()).
756 gdk_gc_set_clip_rectangle (GdkGC *gc,
757 const GdkRectangle *rectangle)
761 g_return_if_fail (GDK_IS_GC (gc));
763 _gdk_gc_remove_drawable_clip (gc);
766 region = gdk_region_rectangle (rectangle);
770 _gdk_gc_set_clip_region_real (gc, region, TRUE);
774 * gdk_gc_set_clip_region:
776 * @region: the #GdkRegion.
778 * Sets the clip mask for a graphics context from a region structure.
779 * The clip mask is interpreted relative to the clip origin. (See
780 * gdk_gc_set_clip_origin()).
783 gdk_gc_set_clip_region (GdkGC *gc,
784 const GdkRegion *region)
788 g_return_if_fail (GDK_IS_GC (gc));
790 _gdk_gc_remove_drawable_clip (gc);
793 copy = gdk_region_copy (region);
797 _gdk_gc_set_clip_region_real (gc, copy, TRUE);
801 * _gdk_gc_get_clip_region:
804 * Gets the current clip region for @gc, if any.
806 * Return value: the clip region for the GC, or %NULL.
807 * (if a clip mask is set, the return will be %NULL)
808 * This value is owned by the GC and must not be freed.
811 _gdk_gc_get_clip_region (GdkGC *gc)
813 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
815 return GDK_GC_GET_PRIVATE (gc)->clip_region;
819 * _gdk_gc_get_clip_mask:
822 * Gets the current clip mask for @gc, if any.
824 * Return value: the clip mask for the GC, or %NULL.
825 * (if a clip region is set, the return will be %NULL)
826 * This value is owned by the GC and must not be freed.
829 _gdk_gc_get_clip_mask (GdkGC *gc)
831 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
833 return GDK_GC_GET_PRIVATE (gc)->clip_mask;
840 * Gets the current file style for the GC
842 * Return value: the file style for the GC
845 _gdk_gc_get_fill (GdkGC *gc)
847 g_return_val_if_fail (GDK_IS_GC (gc), GDK_SOLID);
849 return GDK_GC_GET_PRIVATE (gc)->fill;
853 _gdk_gc_get_exposures (GdkGC *gc)
855 g_return_val_if_fail (GDK_IS_GC (gc), FALSE);
857 return GDK_GC_GET_PRIVATE (gc)->exposures;
864 * Gets the tile pixmap for @gc, if any
866 * Return value: the tile set on the GC, or %NULL. The
867 * value is owned by the GC and must not be freed.
870 _gdk_gc_get_tile (GdkGC *gc)
872 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
874 return GDK_GC_GET_PRIVATE (gc)->tile;
878 * _gdk_gc_get_stipple:
881 * Gets the stipple pixmap for @gc, if any
883 * Return value: the stipple set on the GC, or %NULL. The
884 * value is owned by the GC and must not be freed.
887 _gdk_gc_get_stipple (GdkGC *gc)
889 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
891 return GDK_GC_GET_PRIVATE (gc)->stipple;
895 * _gdk_gc_get_fg_pixel:
898 * Gets the foreground pixel value for @gc. If the
899 * foreground pixel has never been set, returns the
902 * Return value: the foreground pixel value of the GC
905 _gdk_gc_get_fg_pixel (GdkGC *gc)
907 g_return_val_if_fail (GDK_IS_GC (gc), 0);
909 return GDK_GC_GET_PRIVATE (gc)->fg_pixel;
913 * _gdk_gc_get_bg_pixel:
916 * Gets the background pixel value for @gc.If the
917 * foreground pixel has never been set, returns the
920 * Return value: the foreground pixel value of the GC
923 _gdk_gc_get_bg_pixel (GdkGC *gc)
925 g_return_val_if_fail (GDK_IS_GC (gc), 0);
927 return GDK_GC_GET_PRIVATE (gc)->bg_pixel;
931 * gdk_gc_set_subwindow:
933 * @mode: the subwindow mode.
935 * Sets how drawing with this GC on a window will affect child
936 * windows of that window.
939 gdk_gc_set_subwindow (GdkGC *gc,
940 GdkSubwindowMode mode)
943 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
945 g_return_if_fail (GDK_IS_GC (gc));
947 /* This could get called a lot to reset the subwindow mode in
948 the client side clipping, so bail out early */
949 if (priv->subwindow_mode == mode)
952 values.subwindow_mode = mode;
953 gdk_gc_set_values (gc, &values, GDK_GC_SUBWINDOW);
957 _gdk_gc_get_subwindow (GdkGC *gc)
959 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
961 return priv->subwindow_mode;
965 * gdk_gc_set_exposures:
967 * @exposures: if %TRUE, exposure events will be generated.
969 * Sets whether copying non-visible portions of a drawable
970 * using this graphics context generate exposure events
971 * for the corresponding regions of the destination
972 * drawable. (See gdk_draw_drawable()).
975 gdk_gc_set_exposures (GdkGC *gc,
980 g_return_if_fail (GDK_IS_GC (gc));
982 values.graphics_exposures = exposures;
983 gdk_gc_set_values (gc, &values, GDK_GC_EXPOSURES);
987 * gdk_gc_set_line_attributes:
989 * @line_width: the width of lines.
990 * @line_style: the dash-style for lines.
991 * @cap_style: the manner in which the ends of lines are drawn.
992 * @join_style: the in which lines are joined together.
994 * Sets various attributes of how lines are drawn. See
995 * the corresponding members of #GdkGCValues for full
996 * explanations of the arguments.
999 gdk_gc_set_line_attributes (GdkGC *gc,
1001 GdkLineStyle line_style,
1002 GdkCapStyle cap_style,
1003 GdkJoinStyle join_style)
1007 values.line_width = line_width;
1008 values.line_style = line_style;
1009 values.cap_style = cap_style;
1010 values.join_style = join_style;
1012 gdk_gc_set_values (gc, &values,
1020 * gdk_gc_set_dashes:
1022 * @dash_offset: the phase of the dash pattern.
1023 * @dash_list: an array of dash lengths.
1024 * @n: the number of elements in @dash_list.
1026 * Sets the way dashed-lines are drawn. Lines will be
1027 * drawn with alternating on and off segments of the
1028 * lengths specified in @dash_list. The manner in
1029 * which the on and off segments are drawn is determined
1030 * by the @line_style value of the GC. (This can
1031 * be changed with gdk_gc_set_line_attributes().)
1033 * The @dash_offset defines the phase of the pattern,
1034 * specifying how many pixels into the dash-list the pattern
1035 * should actually begin.
1038 gdk_gc_set_dashes (GdkGC *gc,
1043 g_return_if_fail (GDK_IS_GC (gc));
1044 g_return_if_fail (dash_list != NULL);
1046 GDK_GC_GET_CLASS (gc)->set_dashes (gc, dash_offset, dash_list, n);
1052 * @x_offset: amount by which to offset the GC in the X direction
1053 * @y_offset: amount by which to offset the GC in the Y direction
1055 * Offset attributes such as the clip and tile-stipple origins
1056 * of the GC so that drawing at x - x_offset, y - y_offset with
1057 * the offset GC has the same effect as drawing at x, y with the original
1061 gdk_gc_offset (GdkGC *gc,
1065 if (x_offset != 0 || y_offset != 0)
1069 values.clip_x_origin = gc->clip_x_origin - x_offset;
1070 values.clip_y_origin = gc->clip_y_origin - y_offset;
1071 values.ts_x_origin = gc->ts_x_origin - x_offset;
1072 values.ts_y_origin = gc->ts_y_origin - y_offset;
1074 gdk_gc_set_values (gc, &values,
1075 GDK_GC_CLIP_X_ORIGIN |
1076 GDK_GC_CLIP_Y_ORIGIN |
1077 GDK_GC_TS_X_ORIGIN |
1078 GDK_GC_TS_Y_ORIGIN);
1084 * @dst_gc: the destination graphics context.
1085 * @src_gc: the source graphics context.
1087 * Copy the set of values from one graphics context
1088 * onto another graphics context.
1091 gdk_gc_copy (GdkGC *dst_gc,
1094 GdkGCPrivate *dst_priv, *src_priv;
1096 g_return_if_fail (GDK_IS_GC (dst_gc));
1097 g_return_if_fail (GDK_IS_GC (src_gc));
1099 dst_priv = GDK_GC_GET_PRIVATE (dst_gc);
1100 src_priv = GDK_GC_GET_PRIVATE (src_gc);
1102 _gdk_windowing_gc_copy (dst_gc, src_gc);
1104 dst_gc->clip_x_origin = src_gc->clip_x_origin;
1105 dst_gc->clip_y_origin = src_gc->clip_y_origin;
1106 dst_gc->ts_x_origin = src_gc->ts_x_origin;
1107 dst_gc->ts_y_origin = src_gc->ts_y_origin;
1109 if (src_gc->colormap)
1110 g_object_ref (src_gc->colormap);
1112 if (dst_gc->colormap)
1113 g_object_unref (dst_gc->colormap);
1115 dst_gc->colormap = src_gc->colormap;
1117 if (dst_priv->clip_region)
1118 gdk_region_destroy (dst_priv->clip_region);
1120 if (src_priv->clip_region)
1121 dst_priv->clip_region = gdk_region_copy (src_priv->clip_region);
1123 dst_priv->clip_region = NULL;
1125 dst_priv->region_tag_applied = src_priv->region_tag_applied;
1127 if (dst_priv->old_clip_region)
1128 gdk_region_destroy (dst_priv->old_clip_region);
1130 if (src_priv->old_clip_region)
1131 dst_priv->old_clip_region = gdk_region_copy (src_priv->old_clip_region);
1133 dst_priv->old_clip_region = NULL;
1135 if (src_priv->clip_mask)
1136 dst_priv->clip_mask = g_object_ref (src_priv->clip_mask);
1138 dst_priv->clip_mask = NULL;
1140 if (src_priv->old_clip_mask)
1141 dst_priv->old_clip_mask = g_object_ref (src_priv->old_clip_mask);
1143 dst_priv->old_clip_mask = NULL;
1145 dst_priv->fill = src_priv->fill;
1147 if (dst_priv->stipple)
1148 g_object_unref (dst_priv->stipple);
1149 dst_priv->stipple = src_priv->stipple;
1150 if (dst_priv->stipple)
1151 g_object_ref (dst_priv->stipple);
1154 g_object_unref (dst_priv->tile);
1155 dst_priv->tile = src_priv->tile;
1157 g_object_ref (dst_priv->tile);
1159 dst_priv->fg_pixel = src_priv->fg_pixel;
1160 dst_priv->bg_pixel = src_priv->bg_pixel;
1161 dst_priv->subwindow_mode = src_priv->subwindow_mode;
1162 dst_priv->exposures = src_priv->exposures;
1166 * gdk_gc_set_colormap:
1168 * @colormap: a #GdkColormap
1170 * Sets the colormap for the GC to the given colormap. The depth
1171 * of the colormap's visual must match the depth of the drawable
1172 * for which the GC was created.
1175 gdk_gc_set_colormap (GdkGC *gc,
1176 GdkColormap *colormap)
1178 g_return_if_fail (GDK_IS_GC (gc));
1179 g_return_if_fail (GDK_IS_COLORMAP (colormap));
1181 if (gc->colormap != colormap)
1184 g_object_unref (gc->colormap);
1186 gc->colormap = colormap;
1187 g_object_ref (gc->colormap);
1193 * gdk_gc_get_colormap:
1196 * Retrieves the colormap for a given GC, if it exists.
1197 * A GC will have a colormap if the drawable for which it was created
1198 * has a colormap, or if a colormap was set explicitely with
1199 * gdk_gc_set_colormap.
1201 * Return value: the colormap of @gc, or %NULL if @gc doesn't have one.
1204 gdk_gc_get_colormap (GdkGC *gc)
1206 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
1208 return gc->colormap;
1211 static GdkColormap *
1212 gdk_gc_get_colormap_warn (GdkGC *gc)
1214 GdkColormap *colormap = gdk_gc_get_colormap (gc);
1217 g_warning ("gdk_gc_set_rgb_fg_color() and gdk_gc_set_rgb_bg_color() can\n"
1218 "only be used on GC's with a colormap. A GC will have a colormap\n"
1219 "if it is created for a drawable with a colormap, or if a\n"
1220 "colormap has been set explicitly with gdk_gc_set_colormap.\n");
1228 * gdk_gc_set_rgb_fg_color:
1230 * @color: an unallocated #GdkColor.
1232 * Set the foreground color of a GC using an unallocated color. The
1233 * pixel value for the color will be determined using GdkRGB. If the
1234 * colormap for the GC has not previously been initialized for GdkRGB,
1235 * then for pseudo-color colormaps (colormaps with a small modifiable
1236 * number of colors), a colorcube will be allocated in the colormap.
1238 * Calling this function for a GC without a colormap is an error.
1241 gdk_gc_set_rgb_fg_color (GdkGC *gc,
1242 const GdkColor *color)
1247 g_return_if_fail (GDK_IS_GC (gc));
1248 g_return_if_fail (color != NULL);
1250 cmap = gdk_gc_get_colormap_warn (gc);
1255 gdk_rgb_find_color (cmap, &tmp_color);
1256 gdk_gc_set_foreground (gc, &tmp_color);
1260 * gdk_gc_set_rgb_bg_color:
1262 * @color: an unallocated #GdkColor.
1264 * Set the background color of a GC using an unallocated color. The
1265 * pixel value for the color will be determined using GdkRGB. If the
1266 * colormap for the GC has not previously been initialized for GdkRGB,
1267 * then for pseudo-color colormaps (colormaps with a small modifiable
1268 * number of colors), a colorcube will be allocated in the colormap.
1270 * Calling this function for a GC without a colormap is an error.
1273 gdk_gc_set_rgb_bg_color (GdkGC *gc,
1274 const GdkColor *color)
1279 g_return_if_fail (GDK_IS_GC (gc));
1280 g_return_if_fail (color != NULL);
1282 cmap = gdk_gc_get_colormap_warn (gc);
1287 gdk_rgb_find_color (cmap, &tmp_color);
1288 gdk_gc_set_background (gc, &tmp_color);
1291 static cairo_surface_t *
1292 make_stipple_tile_surface (cairo_t *cr,
1294 GdkColor *foreground,
1295 GdkColor *background)
1298 cairo_surface_t *surface;
1299 cairo_surface_t *alpha_surface;
1302 gdk_drawable_get_size (stipple,
1305 alpha_surface = _gdk_drawable_ref_cairo_surface (stipple);
1307 surface = cairo_surface_create_similar (cairo_get_target (cr),
1308 CAIRO_CONTENT_COLOR_ALPHA,
1311 tmp_cr = cairo_create (surface);
1313 cairo_set_operator (tmp_cr, CAIRO_OPERATOR_SOURCE);
1316 gdk_cairo_set_source_color (tmp_cr, background);
1318 cairo_set_source_rgba (tmp_cr, 0, 0, 0 ,0);
1320 cairo_paint (tmp_cr);
1322 cairo_set_operator (tmp_cr, CAIRO_OPERATOR_OVER);
1324 gdk_cairo_set_source_color (tmp_cr, foreground);
1325 cairo_mask_surface (tmp_cr, alpha_surface, 0, 0);
1327 cairo_destroy (tmp_cr);
1328 cairo_surface_destroy (alpha_surface);
1334 gc_get_foreground (GdkGC *gc,
1337 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
1339 color->pixel = priv->bg_pixel;
1342 gdk_colormap_query_color (gc->colormap, priv->fg_pixel, color);
1344 g_warning ("No colormap in gc_get_foreground");
1348 gc_get_background (GdkGC *gc,
1351 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
1353 color->pixel = priv->bg_pixel;
1356 gdk_colormap_query_color (gc->colormap, priv->bg_pixel, color);
1358 g_warning ("No colormap in gc_get_background");
1362 * _gdk_gc_update_context:
1365 * @override_foreground: a foreground color to use to override the
1366 * foreground color of the GC
1367 * @override_stipple: a stipple pattern to use to override the
1368 * stipple from the GC. If this is present and the fill mode
1369 * of the GC isn't %GDK_STIPPLED or %GDK_OPAQUE_STIPPLED
1370 * the fill mode will be forced to %GDK_STIPPLED
1371 * @gc_changed: pass %FALSE if the @gc has not changed since the
1372 * last call to this function
1373 * @target_drawable: The drawable you're drawing in. If passed in
1374 * this is used for client side window clip emulation.
1376 * Set the attributes of a cairo context to match those of a #GdkGC
1377 * as far as possible. Some aspects of a #GdkGC, such as clip masks
1378 * and functions other than %GDK_COPY are not currently handled.
1381 _gdk_gc_update_context (GdkGC *gc,
1383 const GdkColor *override_foreground,
1384 GdkBitmap *override_stipple,
1385 gboolean gc_changed,
1386 GdkDrawable *target_drawable)
1390 GdkColor foreground;
1391 GdkColor background;
1392 cairo_surface_t *tile_surface = NULL;
1393 GdkBitmap *stipple = NULL;
1395 g_return_if_fail (GDK_IS_GC (gc));
1396 g_return_if_fail (cr != NULL);
1397 g_return_if_fail (override_stipple == NULL || GDK_IS_PIXMAP (override_stipple));
1399 priv = GDK_GC_GET_PRIVATE (gc);
1401 _gdk_gc_remove_drawable_clip (gc);
1404 if (override_stipple && fill != GDK_OPAQUE_STIPPLED)
1405 fill = GDK_STIPPLED;
1407 if (fill != GDK_TILED)
1409 if (override_foreground)
1410 foreground = *override_foreground;
1412 gc_get_foreground (gc, &foreground);
1415 if (fill == GDK_OPAQUE_STIPPLED)
1416 gc_get_background (gc, &background);
1428 case GDK_OPAQUE_STIPPLED:
1429 if (override_stipple)
1430 stipple = override_stipple;
1432 stipple = priv->stipple;
1442 gdk_cairo_set_source_color (cr, &foreground);
1445 tile_surface = _gdk_drawable_ref_cairo_surface (priv->tile);
1448 tile_surface = make_stipple_tile_surface (cr, stipple, &foreground, NULL);
1450 case GDK_OPAQUE_STIPPLED:
1451 tile_surface = make_stipple_tile_surface (cr, stipple, &foreground, &background);
1455 /* Tiles, stipples, and clip regions are all specified in device space,
1456 * not user space. For the clip region, we can simply change the matrix,
1457 * clip, then clip back, but for the source pattern, we need to
1458 * compute the right matrix.
1462 * CTM_inverse * Pattern_matrix = Translate(- ts_x, - ts_y)
1464 * (So that ts_x, ts_y in device space is taken to 0,0 in pattern
1465 * space). So, pattern_matrix = CTM * Translate(- ts_x, - tx_y);
1470 cairo_pattern_t *pattern = cairo_pattern_create_for_surface (tile_surface);
1471 cairo_matrix_t user_to_device;
1472 cairo_matrix_t user_to_pattern;
1473 cairo_matrix_t device_to_pattern;
1475 cairo_get_matrix (cr, &user_to_device);
1476 cairo_matrix_init_translate (&device_to_pattern,
1477 - gc->ts_x_origin, - gc->ts_y_origin);
1478 cairo_matrix_multiply (&user_to_pattern,
1479 &user_to_device, &device_to_pattern);
1481 cairo_pattern_set_matrix (pattern, &user_to_pattern);
1482 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
1483 cairo_set_source (cr, pattern);
1485 cairo_surface_destroy (tile_surface);
1486 cairo_pattern_destroy (pattern);
1492 cairo_reset_clip (cr);
1493 /* The reset above resets the window clip rect, so we want to re-set that */
1494 if (target_drawable && GDK_DRAWABLE_GET_CLASS (target_drawable)->set_cairo_clip)
1495 GDK_DRAWABLE_GET_CLASS (target_drawable)->set_cairo_clip (target_drawable, cr);
1497 if (priv->clip_region)
1501 cairo_identity_matrix (cr);
1502 cairo_translate (cr, gc->clip_x_origin, gc->clip_y_origin);
1504 cairo_new_path (cr);
1505 gdk_cairo_region (cr, priv->clip_region);
1515 #define __GDK_GC_C__
1516 #include "gdkaliasdef.c"