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);
685 g_object_unref (new_mask);
687 else if (overlap == GDK_OVERLAP_RECTANGLE_OUT)
689 /* No intersection, set empty clip region */
690 GdkRegion *empty = gdk_region_new ();
692 gdk_region_destroy (region);
693 priv->old_clip_mask = g_object_ref (priv->clip_mask);
694 priv->clip_region = empty;
695 _gdk_windowing_gc_set_clip_region (gc, empty, FALSE);
699 /* Completely inside region, don't set unnecessary clip */
700 gdk_region_destroy (region);
706 priv->old_clip_region = priv->clip_region;
707 priv->clip_region = region;
708 if (priv->old_clip_region)
709 gdk_region_intersect (region, priv->old_clip_region);
711 _gdk_windowing_gc_set_clip_region (gc, priv->clip_region, FALSE);
714 priv->region_tag_applied = region_tag;
715 priv->region_tag_offset_x = offset_x;
716 priv->region_tag_offset_y = offset_y;
720 _gdk_gc_remove_drawable_clip (GdkGC *gc)
722 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
724 if (priv->region_tag_applied)
726 priv->region_tag_applied = 0;
727 if (priv->old_clip_mask)
729 gdk_gc_set_clip_mask (gc, priv->old_clip_mask);
730 g_object_unref (priv->old_clip_mask);
731 priv->old_clip_mask = NULL;
733 if (priv->clip_region)
735 g_object_unref (priv->clip_region);
736 priv->clip_region = NULL;
741 _gdk_gc_set_clip_region_real (gc, priv->old_clip_region, FALSE);
742 priv->old_clip_region = NULL;
748 * gdk_gc_set_clip_rectangle:
750 * @rectangle: the rectangle to clip to.
752 * Sets the clip mask for a graphics context from a
753 * rectangle. The clip mask is interpreted relative to the clip
754 * origin. (See gdk_gc_set_clip_origin()).
757 gdk_gc_set_clip_rectangle (GdkGC *gc,
758 const GdkRectangle *rectangle)
762 g_return_if_fail (GDK_IS_GC (gc));
764 _gdk_gc_remove_drawable_clip (gc);
767 region = gdk_region_rectangle (rectangle);
771 _gdk_gc_set_clip_region_real (gc, region, TRUE);
775 * gdk_gc_set_clip_region:
777 * @region: the #GdkRegion.
779 * Sets the clip mask for a graphics context from a region structure.
780 * The clip mask is interpreted relative to the clip origin. (See
781 * gdk_gc_set_clip_origin()).
784 gdk_gc_set_clip_region (GdkGC *gc,
785 const GdkRegion *region)
789 g_return_if_fail (GDK_IS_GC (gc));
791 _gdk_gc_remove_drawable_clip (gc);
794 copy = gdk_region_copy (region);
798 _gdk_gc_set_clip_region_real (gc, copy, TRUE);
802 * _gdk_gc_get_clip_region:
805 * Gets the current clip region for @gc, if any.
807 * Return value: the clip region for the GC, or %NULL.
808 * (if a clip mask is set, the return will be %NULL)
809 * This value is owned by the GC and must not be freed.
812 _gdk_gc_get_clip_region (GdkGC *gc)
814 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
816 return GDK_GC_GET_PRIVATE (gc)->clip_region;
820 * _gdk_gc_get_clip_mask:
823 * Gets the current clip mask for @gc, if any.
825 * Return value: the clip mask for the GC, or %NULL.
826 * (if a clip region is set, the return will be %NULL)
827 * This value is owned by the GC and must not be freed.
830 _gdk_gc_get_clip_mask (GdkGC *gc)
832 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
834 return GDK_GC_GET_PRIVATE (gc)->clip_mask;
841 * Gets the current file style for the GC
843 * Return value: the file style for the GC
846 _gdk_gc_get_fill (GdkGC *gc)
848 g_return_val_if_fail (GDK_IS_GC (gc), GDK_SOLID);
850 return GDK_GC_GET_PRIVATE (gc)->fill;
854 _gdk_gc_get_exposures (GdkGC *gc)
856 g_return_val_if_fail (GDK_IS_GC (gc), FALSE);
858 return GDK_GC_GET_PRIVATE (gc)->exposures;
865 * Gets the tile pixmap for @gc, if any
867 * Return value: the tile set on the GC, or %NULL. The
868 * value is owned by the GC and must not be freed.
871 _gdk_gc_get_tile (GdkGC *gc)
873 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
875 return GDK_GC_GET_PRIVATE (gc)->tile;
879 * _gdk_gc_get_stipple:
882 * Gets the stipple pixmap for @gc, if any
884 * Return value: the stipple set on the GC, or %NULL. The
885 * value is owned by the GC and must not be freed.
888 _gdk_gc_get_stipple (GdkGC *gc)
890 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
892 return GDK_GC_GET_PRIVATE (gc)->stipple;
896 * _gdk_gc_get_fg_pixel:
899 * Gets the foreground pixel value for @gc. If the
900 * foreground pixel has never been set, returns the
903 * Return value: the foreground pixel value of the GC
906 _gdk_gc_get_fg_pixel (GdkGC *gc)
908 g_return_val_if_fail (GDK_IS_GC (gc), 0);
910 return GDK_GC_GET_PRIVATE (gc)->fg_pixel;
914 * _gdk_gc_get_bg_pixel:
917 * Gets the background pixel value for @gc.If the
918 * foreground pixel has never been set, returns the
921 * Return value: the foreground pixel value of the GC
924 _gdk_gc_get_bg_pixel (GdkGC *gc)
926 g_return_val_if_fail (GDK_IS_GC (gc), 0);
928 return GDK_GC_GET_PRIVATE (gc)->bg_pixel;
932 * gdk_gc_set_subwindow:
934 * @mode: the subwindow mode.
936 * Sets how drawing with this GC on a window will affect child
937 * windows of that window.
940 gdk_gc_set_subwindow (GdkGC *gc,
941 GdkSubwindowMode mode)
944 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
946 g_return_if_fail (GDK_IS_GC (gc));
948 /* This could get called a lot to reset the subwindow mode in
949 the client side clipping, so bail out early */
950 if (priv->subwindow_mode == mode)
953 values.subwindow_mode = mode;
954 gdk_gc_set_values (gc, &values, GDK_GC_SUBWINDOW);
958 _gdk_gc_get_subwindow (GdkGC *gc)
960 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
962 return priv->subwindow_mode;
966 * gdk_gc_set_exposures:
968 * @exposures: if %TRUE, exposure events will be generated.
970 * Sets whether copying non-visible portions of a drawable
971 * using this graphics context generate exposure events
972 * for the corresponding regions of the destination
973 * drawable. (See gdk_draw_drawable()).
976 gdk_gc_set_exposures (GdkGC *gc,
981 g_return_if_fail (GDK_IS_GC (gc));
983 values.graphics_exposures = exposures;
984 gdk_gc_set_values (gc, &values, GDK_GC_EXPOSURES);
988 * gdk_gc_set_line_attributes:
990 * @line_width: the width of lines.
991 * @line_style: the dash-style for lines.
992 * @cap_style: the manner in which the ends of lines are drawn.
993 * @join_style: the in which lines are joined together.
995 * Sets various attributes of how lines are drawn. See
996 * the corresponding members of #GdkGCValues for full
997 * explanations of the arguments.
1000 gdk_gc_set_line_attributes (GdkGC *gc,
1002 GdkLineStyle line_style,
1003 GdkCapStyle cap_style,
1004 GdkJoinStyle join_style)
1008 values.line_width = line_width;
1009 values.line_style = line_style;
1010 values.cap_style = cap_style;
1011 values.join_style = join_style;
1013 gdk_gc_set_values (gc, &values,
1021 * gdk_gc_set_dashes:
1023 * @dash_offset: the phase of the dash pattern.
1024 * @dash_list: an array of dash lengths.
1025 * @n: the number of elements in @dash_list.
1027 * Sets the way dashed-lines are drawn. Lines will be
1028 * drawn with alternating on and off segments of the
1029 * lengths specified in @dash_list. The manner in
1030 * which the on and off segments are drawn is determined
1031 * by the @line_style value of the GC. (This can
1032 * be changed with gdk_gc_set_line_attributes().)
1034 * The @dash_offset defines the phase of the pattern,
1035 * specifying how many pixels into the dash-list the pattern
1036 * should actually begin.
1039 gdk_gc_set_dashes (GdkGC *gc,
1044 g_return_if_fail (GDK_IS_GC (gc));
1045 g_return_if_fail (dash_list != NULL);
1047 GDK_GC_GET_CLASS (gc)->set_dashes (gc, dash_offset, dash_list, n);
1053 * @x_offset: amount by which to offset the GC in the X direction
1054 * @y_offset: amount by which to offset the GC in the Y direction
1056 * Offset attributes such as the clip and tile-stipple origins
1057 * of the GC so that drawing at x - x_offset, y - y_offset with
1058 * the offset GC has the same effect as drawing at x, y with the original
1062 gdk_gc_offset (GdkGC *gc,
1066 if (x_offset != 0 || y_offset != 0)
1070 values.clip_x_origin = gc->clip_x_origin - x_offset;
1071 values.clip_y_origin = gc->clip_y_origin - y_offset;
1072 values.ts_x_origin = gc->ts_x_origin - x_offset;
1073 values.ts_y_origin = gc->ts_y_origin - y_offset;
1075 gdk_gc_set_values (gc, &values,
1076 GDK_GC_CLIP_X_ORIGIN |
1077 GDK_GC_CLIP_Y_ORIGIN |
1078 GDK_GC_TS_X_ORIGIN |
1079 GDK_GC_TS_Y_ORIGIN);
1085 * @dst_gc: the destination graphics context.
1086 * @src_gc: the source graphics context.
1088 * Copy the set of values from one graphics context
1089 * onto another graphics context.
1092 gdk_gc_copy (GdkGC *dst_gc,
1095 GdkGCPrivate *dst_priv, *src_priv;
1097 g_return_if_fail (GDK_IS_GC (dst_gc));
1098 g_return_if_fail (GDK_IS_GC (src_gc));
1100 dst_priv = GDK_GC_GET_PRIVATE (dst_gc);
1101 src_priv = GDK_GC_GET_PRIVATE (src_gc);
1103 _gdk_windowing_gc_copy (dst_gc, src_gc);
1105 dst_gc->clip_x_origin = src_gc->clip_x_origin;
1106 dst_gc->clip_y_origin = src_gc->clip_y_origin;
1107 dst_gc->ts_x_origin = src_gc->ts_x_origin;
1108 dst_gc->ts_y_origin = src_gc->ts_y_origin;
1110 if (src_gc->colormap)
1111 g_object_ref (src_gc->colormap);
1113 if (dst_gc->colormap)
1114 g_object_unref (dst_gc->colormap);
1116 dst_gc->colormap = src_gc->colormap;
1118 if (dst_priv->clip_region)
1119 gdk_region_destroy (dst_priv->clip_region);
1121 if (src_priv->clip_region)
1122 dst_priv->clip_region = gdk_region_copy (src_priv->clip_region);
1124 dst_priv->clip_region = NULL;
1126 dst_priv->region_tag_applied = src_priv->region_tag_applied;
1128 if (dst_priv->old_clip_region)
1129 gdk_region_destroy (dst_priv->old_clip_region);
1131 if (src_priv->old_clip_region)
1132 dst_priv->old_clip_region = gdk_region_copy (src_priv->old_clip_region);
1134 dst_priv->old_clip_region = NULL;
1136 if (src_priv->clip_mask)
1137 dst_priv->clip_mask = g_object_ref (src_priv->clip_mask);
1139 dst_priv->clip_mask = NULL;
1141 if (src_priv->old_clip_mask)
1142 dst_priv->old_clip_mask = g_object_ref (src_priv->old_clip_mask);
1144 dst_priv->old_clip_mask = NULL;
1146 dst_priv->fill = src_priv->fill;
1148 if (dst_priv->stipple)
1149 g_object_unref (dst_priv->stipple);
1150 dst_priv->stipple = src_priv->stipple;
1151 if (dst_priv->stipple)
1152 g_object_ref (dst_priv->stipple);
1155 g_object_unref (dst_priv->tile);
1156 dst_priv->tile = src_priv->tile;
1158 g_object_ref (dst_priv->tile);
1160 dst_priv->fg_pixel = src_priv->fg_pixel;
1161 dst_priv->bg_pixel = src_priv->bg_pixel;
1162 dst_priv->subwindow_mode = src_priv->subwindow_mode;
1163 dst_priv->exposures = src_priv->exposures;
1167 * gdk_gc_set_colormap:
1169 * @colormap: a #GdkColormap
1171 * Sets the colormap for the GC to the given colormap. The depth
1172 * of the colormap's visual must match the depth of the drawable
1173 * for which the GC was created.
1176 gdk_gc_set_colormap (GdkGC *gc,
1177 GdkColormap *colormap)
1179 g_return_if_fail (GDK_IS_GC (gc));
1180 g_return_if_fail (GDK_IS_COLORMAP (colormap));
1182 if (gc->colormap != colormap)
1185 g_object_unref (gc->colormap);
1187 gc->colormap = colormap;
1188 g_object_ref (gc->colormap);
1194 * gdk_gc_get_colormap:
1197 * Retrieves the colormap for a given GC, if it exists.
1198 * A GC will have a colormap if the drawable for which it was created
1199 * has a colormap, or if a colormap was set explicitely with
1200 * gdk_gc_set_colormap.
1202 * Return value: the colormap of @gc, or %NULL if @gc doesn't have one.
1205 gdk_gc_get_colormap (GdkGC *gc)
1207 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
1209 return gc->colormap;
1212 static GdkColormap *
1213 gdk_gc_get_colormap_warn (GdkGC *gc)
1215 GdkColormap *colormap = gdk_gc_get_colormap (gc);
1218 g_warning ("gdk_gc_set_rgb_fg_color() and gdk_gc_set_rgb_bg_color() can\n"
1219 "only be used on GC's with a colormap. A GC will have a colormap\n"
1220 "if it is created for a drawable with a colormap, or if a\n"
1221 "colormap has been set explicitly with gdk_gc_set_colormap.\n");
1229 * gdk_gc_set_rgb_fg_color:
1231 * @color: an unallocated #GdkColor.
1233 * Set the foreground color of a GC using an unallocated color. The
1234 * pixel value for the color will be determined using GdkRGB. If the
1235 * colormap for the GC has not previously been initialized for GdkRGB,
1236 * then for pseudo-color colormaps (colormaps with a small modifiable
1237 * number of colors), a colorcube will be allocated in the colormap.
1239 * Calling this function for a GC without a colormap is an error.
1242 gdk_gc_set_rgb_fg_color (GdkGC *gc,
1243 const GdkColor *color)
1248 g_return_if_fail (GDK_IS_GC (gc));
1249 g_return_if_fail (color != NULL);
1251 cmap = gdk_gc_get_colormap_warn (gc);
1256 gdk_rgb_find_color (cmap, &tmp_color);
1257 gdk_gc_set_foreground (gc, &tmp_color);
1261 * gdk_gc_set_rgb_bg_color:
1263 * @color: an unallocated #GdkColor.
1265 * Set the background color of a GC using an unallocated color. The
1266 * pixel value for the color will be determined using GdkRGB. If the
1267 * colormap for the GC has not previously been initialized for GdkRGB,
1268 * then for pseudo-color colormaps (colormaps with a small modifiable
1269 * number of colors), a colorcube will be allocated in the colormap.
1271 * Calling this function for a GC without a colormap is an error.
1274 gdk_gc_set_rgb_bg_color (GdkGC *gc,
1275 const GdkColor *color)
1280 g_return_if_fail (GDK_IS_GC (gc));
1281 g_return_if_fail (color != NULL);
1283 cmap = gdk_gc_get_colormap_warn (gc);
1288 gdk_rgb_find_color (cmap, &tmp_color);
1289 gdk_gc_set_background (gc, &tmp_color);
1292 static cairo_surface_t *
1293 make_stipple_tile_surface (cairo_t *cr,
1295 GdkColor *foreground,
1296 GdkColor *background)
1299 cairo_surface_t *surface;
1300 cairo_surface_t *alpha_surface;
1303 gdk_drawable_get_size (stipple,
1306 alpha_surface = _gdk_drawable_ref_cairo_surface (stipple);
1308 surface = cairo_surface_create_similar (cairo_get_target (cr),
1309 CAIRO_CONTENT_COLOR_ALPHA,
1312 tmp_cr = cairo_create (surface);
1314 cairo_set_operator (tmp_cr, CAIRO_OPERATOR_SOURCE);
1317 gdk_cairo_set_source_color (tmp_cr, background);
1319 cairo_set_source_rgba (tmp_cr, 0, 0, 0 ,0);
1321 cairo_paint (tmp_cr);
1323 cairo_set_operator (tmp_cr, CAIRO_OPERATOR_OVER);
1325 gdk_cairo_set_source_color (tmp_cr, foreground);
1326 cairo_mask_surface (tmp_cr, alpha_surface, 0, 0);
1328 cairo_destroy (tmp_cr);
1329 cairo_surface_destroy (alpha_surface);
1335 gc_get_foreground (GdkGC *gc,
1338 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
1340 color->pixel = priv->bg_pixel;
1343 gdk_colormap_query_color (gc->colormap, priv->fg_pixel, color);
1345 g_warning ("No colormap in gc_get_foreground");
1349 gc_get_background (GdkGC *gc,
1352 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
1354 color->pixel = priv->bg_pixel;
1357 gdk_colormap_query_color (gc->colormap, priv->bg_pixel, color);
1359 g_warning ("No colormap in gc_get_background");
1363 * _gdk_gc_update_context:
1366 * @override_foreground: a foreground color to use to override the
1367 * foreground color of the GC
1368 * @override_stipple: a stipple pattern to use to override the
1369 * stipple from the GC. If this is present and the fill mode
1370 * of the GC isn't %GDK_STIPPLED or %GDK_OPAQUE_STIPPLED
1371 * the fill mode will be forced to %GDK_STIPPLED
1372 * @gc_changed: pass %FALSE if the @gc has not changed since the
1373 * last call to this function
1374 * @target_drawable: The drawable you're drawing in. If passed in
1375 * this is used for client side window clip emulation.
1377 * Set the attributes of a cairo context to match those of a #GdkGC
1378 * as far as possible. Some aspects of a #GdkGC, such as clip masks
1379 * and functions other than %GDK_COPY are not currently handled.
1382 _gdk_gc_update_context (GdkGC *gc,
1384 const GdkColor *override_foreground,
1385 GdkBitmap *override_stipple,
1386 gboolean gc_changed,
1387 GdkDrawable *target_drawable)
1391 GdkColor foreground;
1392 GdkColor background;
1393 cairo_surface_t *tile_surface = NULL;
1394 GdkBitmap *stipple = NULL;
1396 g_return_if_fail (GDK_IS_GC (gc));
1397 g_return_if_fail (cr != NULL);
1398 g_return_if_fail (override_stipple == NULL || GDK_IS_PIXMAP (override_stipple));
1400 priv = GDK_GC_GET_PRIVATE (gc);
1402 _gdk_gc_remove_drawable_clip (gc);
1405 if (override_stipple && fill != GDK_OPAQUE_STIPPLED)
1406 fill = GDK_STIPPLED;
1408 if (fill != GDK_TILED)
1410 if (override_foreground)
1411 foreground = *override_foreground;
1413 gc_get_foreground (gc, &foreground);
1416 if (fill == GDK_OPAQUE_STIPPLED)
1417 gc_get_background (gc, &background);
1429 case GDK_OPAQUE_STIPPLED:
1430 if (override_stipple)
1431 stipple = override_stipple;
1433 stipple = priv->stipple;
1443 gdk_cairo_set_source_color (cr, &foreground);
1446 tile_surface = _gdk_drawable_ref_cairo_surface (priv->tile);
1449 tile_surface = make_stipple_tile_surface (cr, stipple, &foreground, NULL);
1451 case GDK_OPAQUE_STIPPLED:
1452 tile_surface = make_stipple_tile_surface (cr, stipple, &foreground, &background);
1456 /* Tiles, stipples, and clip regions are all specified in device space,
1457 * not user space. For the clip region, we can simply change the matrix,
1458 * clip, then clip back, but for the source pattern, we need to
1459 * compute the right matrix.
1463 * CTM_inverse * Pattern_matrix = Translate(- ts_x, - ts_y)
1465 * (So that ts_x, ts_y in device space is taken to 0,0 in pattern
1466 * space). So, pattern_matrix = CTM * Translate(- ts_x, - tx_y);
1471 cairo_pattern_t *pattern = cairo_pattern_create_for_surface (tile_surface);
1472 cairo_matrix_t user_to_device;
1473 cairo_matrix_t user_to_pattern;
1474 cairo_matrix_t device_to_pattern;
1476 cairo_get_matrix (cr, &user_to_device);
1477 cairo_matrix_init_translate (&device_to_pattern,
1478 - gc->ts_x_origin, - gc->ts_y_origin);
1479 cairo_matrix_multiply (&user_to_pattern,
1480 &user_to_device, &device_to_pattern);
1482 cairo_pattern_set_matrix (pattern, &user_to_pattern);
1483 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
1484 cairo_set_source (cr, pattern);
1486 cairo_surface_destroy (tile_surface);
1487 cairo_pattern_destroy (pattern);
1493 cairo_reset_clip (cr);
1494 /* The reset above resets the window clip rect, so we want to re-set that */
1495 if (target_drawable && GDK_DRAWABLE_GET_CLASS (target_drawable)->set_cairo_clip)
1496 GDK_DRAWABLE_GET_CLASS (target_drawable)->set_cairo_clip (target_drawable, cr);
1498 if (priv->clip_region)
1502 cairo_identity_matrix (cr);
1503 cairo_translate (cr, gc->clip_x_origin, gc->clip_y_origin);
1505 cairo_new_path (cr);
1506 gdk_cairo_region (cr, priv->clip_region);
1516 #define __GDK_GC_C__
1517 #include "gdkaliasdef.c"