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;
54 #define GDK_GC_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDK_TYPE_GC, GdkGCPrivate))
56 G_DEFINE_TYPE (GdkGC, gdk_gc, G_TYPE_OBJECT)
59 gdk_gc_class_init (GdkGCClass *class)
61 GObjectClass *object_class = G_OBJECT_CLASS (class);
63 object_class->finalize = gdk_gc_finalize;
65 g_type_class_add_private (object_class, sizeof (GdkGCPrivate));
69 gdk_gc_init (GdkGC *gc)
71 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
73 priv->fill = GDK_SOLID;
75 /* These are the default X11 value, which we match. They are clearly
76 * wrong for TrueColor displays, so apps have to change them.
84 * @drawable: a #GdkDrawable. The created GC must always be used
85 * with drawables of the same depth as this one.
87 * Create a new graphics context with default values.
89 * Returns: the new graphics context.
92 gdk_gc_new (GdkDrawable *drawable)
94 g_return_val_if_fail (drawable != NULL, NULL);
96 return gdk_gc_new_with_values (drawable, NULL, 0);
100 * gdk_gc_new_with_values:
101 * @drawable: a #GdkDrawable. The created GC must always be used
102 * with drawables of the same depth as this one.
103 * @values: a structure containing initial values for the GC.
104 * @values_mask: a bit mask indicating which fields in @values
107 * Create a new GC with the given initial values.
109 * Return value: the new graphics context.
112 gdk_gc_new_with_values (GdkDrawable *drawable,
114 GdkGCValuesMask values_mask)
116 g_return_val_if_fail (drawable != NULL, NULL);
118 return GDK_DRAWABLE_GET_CLASS (drawable)->create_gc (drawable,
126 * @drawable: a #GdkDrawable.
127 * @values: a structure containing initial values for the GC.
128 * @values_mask: a bit mask indicating which fields in @values
131 * Does initialization of the generic portions of a #GdkGC
132 * created with the specified values and values_mask. This
133 * should be called out of the implementation of
134 * GdkDrawable.create_gc() immediately after creating the
138 _gdk_gc_init (GdkGC *gc,
139 GdkDrawable *drawable,
141 GdkGCValuesMask values_mask)
145 g_return_if_fail (GDK_IS_GC (gc));
147 priv = GDK_GC_GET_PRIVATE (gc);
149 if (values_mask & GDK_GC_CLIP_X_ORIGIN)
150 gc->clip_x_origin = values->clip_x_origin;
151 if (values_mask & GDK_GC_CLIP_Y_ORIGIN)
152 gc->clip_y_origin = values->clip_y_origin;
153 if (values_mask & GDK_GC_TS_X_ORIGIN)
154 gc->ts_x_origin = values->ts_x_origin;
155 if (values_mask & GDK_GC_TS_Y_ORIGIN)
156 gc->ts_y_origin = values->ts_y_origin;
157 if (values_mask & GDK_GC_FILL)
158 priv->fill = values->fill;
159 if (values_mask & GDK_GC_STIPPLE)
161 priv->stipple = values->stipple;
163 g_object_ref (priv->stipple);
165 if (values_mask & GDK_GC_TILE)
167 priv->tile = values->tile;
169 g_object_ref (priv->tile);
171 if (values_mask & GDK_GC_FOREGROUND)
172 priv->fg_pixel = values->foreground.pixel;
173 if (values_mask & GDK_GC_BACKGROUND)
174 priv->bg_pixel = values->background.pixel;
176 gc->colormap = gdk_drawable_get_colormap (drawable);
178 g_object_ref (gc->colormap);
182 gdk_gc_finalize (GObject *object)
184 GdkGC *gc = GDK_GC (object);
185 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
187 if (priv->clip_region)
188 gdk_region_destroy (priv->clip_region);
190 g_object_unref (gc->colormap);
192 g_object_unref (priv->tile);
194 g_object_unref (priv->stipple);
196 G_OBJECT_CLASS (gdk_gc_parent_class)->finalize (object);
203 * Deprecated function; use g_object_ref() instead.
205 * Return value: the gc.
208 gdk_gc_ref (GdkGC *gc)
210 return (GdkGC *) g_object_ref (gc);
217 * Decrement the reference count of @gc.
219 * Deprecated: Use g_object_unref() instead.
222 gdk_gc_unref (GdkGC *gc)
230 * @values: the #GdkGCValues structure in which to store the results.
232 * Retrieves the current values from a graphics context. Note that
233 * only the pixel values of the @values->foreground and @values->background
234 * are filled, use gdk_colormap_query_color() to obtain the rgb values
238 gdk_gc_get_values (GdkGC *gc,
241 g_return_if_fail (GDK_IS_GC (gc));
242 g_return_if_fail (values != NULL);
244 GDK_GC_GET_CLASS (gc)->get_values (gc, values);
250 * @values: struct containing the new values
251 * @values_mask: mask indicating which struct fields are to be used
253 * Sets attributes of a graphics context in bulk. For each flag set in
254 * @values_mask, the corresponding field will be read from @values and
255 * set as the new value for @gc. If you're only setting a few values
256 * on @gc, calling individual "setter" functions is likely more
261 gdk_gc_set_values (GdkGC *gc,
263 GdkGCValuesMask values_mask)
267 g_return_if_fail (GDK_IS_GC (gc));
268 g_return_if_fail (values != NULL);
270 priv = GDK_GC_GET_PRIVATE (gc);
272 if (values_mask & GDK_GC_CLIP_X_ORIGIN)
273 gc->clip_x_origin = values->clip_x_origin;
274 if (values_mask & GDK_GC_CLIP_Y_ORIGIN)
275 gc->clip_y_origin = values->clip_y_origin;
276 if (values_mask & GDK_GC_TS_X_ORIGIN)
277 gc->ts_x_origin = values->ts_x_origin;
278 if (values_mask & GDK_GC_TS_Y_ORIGIN)
279 gc->ts_y_origin = values->ts_y_origin;
280 if (values_mask & GDK_GC_CLIP_MASK)
282 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
283 if (priv->clip_region)
285 gdk_region_destroy (priv->clip_region);
286 priv->clip_region = NULL;
289 if (values_mask & GDK_GC_FILL)
290 priv->fill = values->fill;
291 if (values_mask & GDK_GC_STIPPLE)
293 if (priv->stipple != values->stipple)
296 g_object_unref (priv->stipple);
297 priv->stipple = values->stipple;
299 g_object_ref (priv->stipple);
302 if (values_mask & GDK_GC_TILE)
304 if (priv->tile != values->tile)
307 g_object_unref (priv->tile);
308 priv->tile = values->tile;
310 g_object_ref (priv->tile);
313 if (values_mask & GDK_GC_FOREGROUND)
314 priv->fg_pixel = values->foreground.pixel;
315 if (values_mask & GDK_GC_BACKGROUND)
316 priv->bg_pixel = values->background.pixel;
318 GDK_GC_GET_CLASS (gc)->set_values (gc, values, values_mask);
322 * gdk_gc_set_foreground:
324 * @color: the new foreground color.
326 * Sets the foreground color for a graphics context.
327 * Note that this function uses @color->pixel, use
328 * gdk_gc_set_rgb_fg_color() to specify the foreground
329 * color as red, green, blue components.
332 gdk_gc_set_foreground (GdkGC *gc,
333 const GdkColor *color)
337 g_return_if_fail (GDK_IS_GC (gc));
338 g_return_if_fail (color != NULL);
340 values.foreground = *color;
341 gdk_gc_set_values (gc, &values, GDK_GC_FOREGROUND);
345 * gdk_gc_set_background:
347 * @color: the new background color.
349 * Sets the background color for a graphics context.
350 * Note that this function uses @color->pixel, use
351 * gdk_gc_set_rgb_bg_color() to specify the background
352 * color as red, green, blue components.
355 gdk_gc_set_background (GdkGC *gc,
356 const GdkColor *color)
360 g_return_if_fail (GDK_IS_GC (gc));
361 g_return_if_fail (color != NULL);
363 values.background = *color;
364 gdk_gc_set_values (gc, &values, GDK_GC_BACKGROUND);
370 * @font: the new font.
372 * Sets the font for a graphics context. (Note that
373 * all text-drawing functions in GDK take a @font
374 * argument; the value set here is used when that
375 * argument is %NULL.)
378 gdk_gc_set_font (GdkGC *gc,
383 g_return_if_fail (GDK_IS_GC (gc));
384 g_return_if_fail (font != NULL);
387 gdk_gc_set_values (gc, &values, GDK_GC_FONT);
391 * gdk_gc_set_function:
393 * @function: the #GdkFunction to use
395 * Determines how the current pixel values and the
396 * pixel values being drawn are combined to produce
397 * the final pixel values.
400 gdk_gc_set_function (GdkGC *gc,
401 GdkFunction function)
405 g_return_if_fail (GDK_IS_GC (gc));
407 values.function = function;
408 gdk_gc_set_values (gc, &values, GDK_GC_FUNCTION);
414 * @fill: the new fill mode.
416 * Set the fill mode for a graphics context.
419 gdk_gc_set_fill (GdkGC *gc,
424 g_return_if_fail (GDK_IS_GC (gc));
427 gdk_gc_set_values (gc, &values, GDK_GC_FILL);
433 * @tile: the new tile pixmap.
435 * Set a tile pixmap for a graphics context.
436 * This will only be used if the fill mode
440 gdk_gc_set_tile (GdkGC *gc,
445 g_return_if_fail (GDK_IS_GC (gc));
448 gdk_gc_set_values (gc, &values, GDK_GC_TILE);
452 * gdk_gc_set_stipple:
454 * @stipple: the new stipple bitmap.
456 * Set the stipple bitmap for a graphics context. The
457 * stipple will only be used if the fill mode is
458 * %GDK_STIPPLED or %GDK_OPAQUE_STIPPLED.
461 gdk_gc_set_stipple (GdkGC *gc,
466 g_return_if_fail (GDK_IS_GC (gc));
468 values.stipple = stipple;
469 gdk_gc_set_values (gc, &values, GDK_GC_STIPPLE);
473 * gdk_gc_set_ts_origin:
475 * @x: the x-coordinate of the origin.
476 * @y: the y-coordinate of the origin.
478 * Set the origin when using tiles or stipples with
479 * the GC. The tile or stipple will be aligned such
480 * that the upper left corner of the tile or stipple
481 * will coincide with this point.
484 gdk_gc_set_ts_origin (GdkGC *gc,
490 g_return_if_fail (GDK_IS_GC (gc));
492 values.ts_x_origin = x;
493 values.ts_y_origin = y;
495 gdk_gc_set_values (gc, &values,
496 GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN);
500 * gdk_gc_set_clip_origin:
502 * @x: the x-coordinate of the origin.
503 * @y: the y-coordinate of the origin.
505 * Sets the origin of the clip mask. The coordinates are
506 * interpreted relative to the upper-left corner of
507 * the destination drawable of the current operation.
510 gdk_gc_set_clip_origin (GdkGC *gc,
516 g_return_if_fail (GDK_IS_GC (gc));
518 values.clip_x_origin = x;
519 values.clip_y_origin = y;
521 gdk_gc_set_values (gc, &values,
522 GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
526 * gdk_gc_set_clip_mask:
530 * Sets the clip mask for a graphics context from a bitmap.
531 * The clip mask is interpreted relative to the clip
532 * origin. (See gdk_gc_set_clip_origin()).
535 gdk_gc_set_clip_mask (GdkGC *gc,
540 g_return_if_fail (GDK_IS_GC (gc));
542 values.clip_mask = mask;
543 gdk_gc_set_values (gc, &values, GDK_GC_CLIP_MASK);
547 _gdk_gc_set_clip_region_internal (GdkGC *gc,
550 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
552 if (priv->clip_region)
553 gdk_region_destroy (priv->clip_region);
555 priv->clip_region = region;
557 _gdk_windowing_gc_set_clip_region (gc, region);
561 * gdk_gc_set_clip_rectangle:
563 * @rectangle: the rectangle to clip to.
565 * Sets the clip mask for a graphics context from a
566 * rectangle. The clip mask is interpreted relative to the clip
567 * origin. (See gdk_gc_set_clip_origin()).
570 gdk_gc_set_clip_rectangle (GdkGC *gc,
571 GdkRectangle *rectangle)
575 g_return_if_fail (GDK_IS_GC (gc));
578 region = gdk_region_rectangle (rectangle);
582 _gdk_gc_set_clip_region_internal (gc, region);
586 * gdk_gc_set_clip_region:
588 * @region: the #GdkRegion.
590 * Sets the clip mask for a graphics context from a region structure.
591 * The clip mask is interpreted relative to the clip origin. (See
592 * gdk_gc_set_clip_origin()).
595 gdk_gc_set_clip_region (GdkGC *gc,
598 g_return_if_fail (GDK_IS_GC (gc));
601 region = gdk_region_copy (region);
603 _gdk_gc_set_clip_region_internal (gc, region);
607 * _gdk_gc_get_clip_region:
610 * Gets the current clip region for @gc, if any.
612 * Return value: the clip region for the GC, or %NULL.
613 * (if a clip mask is set, the return will be %NULL)
614 * This value is owned by the GC and must not be freed.
617 _gdk_gc_get_clip_region (GdkGC *gc)
619 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
621 return GDK_GC_GET_PRIVATE (gc)->clip_region;
628 * Gets the current file style for the GC
630 * Return value: the file style for the GC
633 _gdk_gc_get_fill (GdkGC *gc)
635 g_return_val_if_fail (GDK_IS_GC (gc), GDK_SOLID);
637 return GDK_GC_GET_PRIVATE (gc)->fill;
644 * Gets the tile pixmap for @gc, if any
646 * Return value: the tile set on the GC, or %NULL. The
647 * value is owned by the GC and must not be freed.
650 _gdk_gc_get_tile (GdkGC *gc)
652 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
654 return GDK_GC_GET_PRIVATE (gc)->tile;
658 * _gdk_gc_get_stipple:
661 * Gets the stipple pixmap for @gc, if any
663 * Return value: the stipple set on the GC, or %NULL. The
664 * value is owned by the GC and must not be freed.
667 _gdk_gc_get_stipple (GdkGC *gc)
669 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
671 return GDK_GC_GET_PRIVATE (gc)->stipple;
675 * _gdk_gc_get_fg_pixel:
678 * Gets the foreground pixel value for @gc. If the
679 * foreground pixel has never been set, returns the
682 * Return value: the foreground pixel value of the GC
685 _gdk_gc_get_fg_pixel (GdkGC *gc)
687 g_return_val_if_fail (GDK_IS_GC (gc), 0);
689 return GDK_GC_GET_PRIVATE (gc)->fg_pixel;
693 * _gdk_gc_get_bg_pixel:
696 * Gets the background pixel value for @gc.If the
697 * foreground pixel has never been set, returns the
700 * Return value: the foreground pixel value of the GC
703 _gdk_gc_get_bg_pixel (GdkGC *gc)
705 g_return_val_if_fail (GDK_IS_GC (gc), 0);
707 return GDK_GC_GET_PRIVATE (gc)->bg_pixel;
711 * gdk_gc_set_subwindow:
713 * @mode: the subwindow mode.
715 * Sets how drawing with this GC on a window will affect child
716 * windows of that window.
719 gdk_gc_set_subwindow (GdkGC *gc,
720 GdkSubwindowMode mode)
724 g_return_if_fail (GDK_IS_GC (gc));
726 values.subwindow_mode = mode;
727 gdk_gc_set_values (gc, &values, GDK_GC_SUBWINDOW);
731 * gdk_gc_set_exposures:
733 * @exposures: if %TRUE, exposure events will be generated.
735 * Sets whether copying non-visible portions of a drawable
736 * using this graphics context generate exposure events
737 * for the corresponding regions of the destination
738 * drawable. (See gdk_draw_drawable()).
741 gdk_gc_set_exposures (GdkGC *gc,
746 g_return_if_fail (GDK_IS_GC (gc));
748 values.graphics_exposures = exposures;
749 gdk_gc_set_values (gc, &values, GDK_GC_EXPOSURES);
753 * gdk_gc_set_line_attributes:
755 * @line_width: the width of lines.
756 * @line_style: the dash-style for lines.
757 * @cap_style: the manner in which the ends of lines are drawn.
758 * @join_style: the in which lines are joined together.
760 * Sets various attributes of how lines are drawn. See
761 * the corresponding members of #GdkGCValues for full
762 * explanations of the arguments.
765 gdk_gc_set_line_attributes (GdkGC *gc,
767 GdkLineStyle line_style,
768 GdkCapStyle cap_style,
769 GdkJoinStyle join_style)
773 values.line_width = line_width;
774 values.line_style = line_style;
775 values.cap_style = cap_style;
776 values.join_style = join_style;
778 gdk_gc_set_values (gc, &values,
788 * @dash_offset: the phase of the dash pattern.
789 * @dash_list: an array of dash lengths.
790 * @n: the number of elements in @dash_list.
792 * Sets the way dashed-lines are drawn. Lines will be
793 * drawn with alternating on and off segments of the
794 * lengths specified in @dash_list. The manner in
795 * which the on and off segments are drawn is determined
796 * by the @line_style value of the GC. (This can
797 * be changed with gdk_gc_set_line_attributes().)
799 * The @dash_offset defines the phase of the pattern,
800 * specifying how many pixels into the dash-list the pattern
801 * should actually begin.
804 gdk_gc_set_dashes (GdkGC *gc,
809 g_return_if_fail (GDK_IS_GC (gc));
810 g_return_if_fail (dash_list != NULL);
812 GDK_GC_GET_CLASS (gc)->set_dashes (gc, dash_offset, dash_list, n);
818 * @x_offset: amount by which to offset the GC in the X direction
819 * @y_offset: amount by which to offset the GC in the Y direction
821 * Offset attributes such as the clip and tile-stipple origins
822 * of the GC so that drawing at x - x_offset, y - y_offset with
823 * the offset GC has the same effect as drawing at x, y with the original
827 gdk_gc_offset (GdkGC *gc,
831 if (x_offset != 0 || y_offset != 0)
835 values.clip_x_origin = gc->clip_x_origin - x_offset;
836 values.clip_y_origin = gc->clip_y_origin - y_offset;
837 values.ts_x_origin = gc->ts_x_origin - x_offset;
838 values.ts_y_origin = gc->ts_y_origin - y_offset;
840 gdk_gc_set_values (gc, &values,
841 GDK_GC_CLIP_X_ORIGIN |
842 GDK_GC_CLIP_Y_ORIGIN |
850 * @dst_gc: the destination graphics context.
851 * @src_gc: the source graphics context.
853 * Copy the set of values from one graphics context
854 * onto another graphics context.
857 gdk_gc_copy (GdkGC *dst_gc,
860 GdkGCPrivate *dst_priv, *src_priv;
862 g_return_if_fail (GDK_IS_GC (dst_gc));
863 g_return_if_fail (GDK_IS_GC (src_gc));
865 dst_priv = GDK_GC_GET_PRIVATE (dst_gc);
866 src_priv = GDK_GC_GET_PRIVATE (src_gc);
868 _gdk_windowing_gc_copy (dst_gc, src_gc);
870 dst_gc->clip_x_origin = src_gc->clip_x_origin;
871 dst_gc->clip_y_origin = src_gc->clip_y_origin;
872 dst_gc->ts_x_origin = src_gc->ts_x_origin;
873 dst_gc->ts_y_origin = src_gc->ts_y_origin;
875 if (src_gc->colormap)
876 g_object_ref (src_gc->colormap);
878 if (dst_gc->colormap)
879 g_object_unref (dst_gc->colormap);
881 dst_gc->colormap = src_gc->colormap;
883 if (dst_priv->clip_region)
884 gdk_region_destroy (dst_priv->clip_region);
886 if (src_priv->clip_region)
887 dst_priv->clip_region = gdk_region_copy (src_priv->clip_region);
889 dst_priv->clip_region = NULL;
891 dst_priv->fill = src_priv->fill;
893 if (dst_priv->stipple)
894 g_object_unref (dst_priv->stipple);
895 dst_priv->stipple = src_priv->stipple;
896 if (dst_priv->stipple)
897 g_object_ref (dst_priv->stipple);
900 g_object_unref (dst_priv->tile);
901 dst_priv->tile = src_priv->tile;
903 g_object_ref (dst_priv->tile);
905 dst_priv->fg_pixel = src_priv->fg_pixel;
906 dst_priv->bg_pixel = src_priv->bg_pixel;
910 * gdk_gc_set_colormap:
912 * @colormap: a #GdkColormap
914 * Sets the colormap for the GC to the given colormap. The depth
915 * of the colormap's visual must match the depth of the drawable
916 * for which the GC was created.
919 gdk_gc_set_colormap (GdkGC *gc,
920 GdkColormap *colormap)
922 g_return_if_fail (GDK_IS_GC (gc));
923 g_return_if_fail (GDK_IS_COLORMAP (colormap));
925 if (gc->colormap != colormap)
928 g_object_unref (gc->colormap);
930 gc->colormap = colormap;
931 g_object_ref (gc->colormap);
937 * gdk_gc_get_colormap:
940 * Retrieves the colormap for a given GC, if it exists.
941 * A GC will have a colormap if the drawable for which it was created
942 * has a colormap, or if a colormap was set explicitely with
943 * gdk_gc_set_colormap.
945 * Return value: the colormap of @gc, or %NULL if @gc doesn't have one.
948 gdk_gc_get_colormap (GdkGC *gc)
950 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
956 gdk_gc_get_colormap_warn (GdkGC *gc)
958 GdkColormap *colormap = gdk_gc_get_colormap (gc);
961 g_warning ("gdk_gc_set_rgb_fg_color() and gdk_gc_set_rgb_bg_color() can\n"
962 "only be used on GC's with a colormap. A GC will have a colormap\n"
963 "if it is created for a drawable with a colormap, or if a\n"
964 "colormap has been set explicitly with gdk_gc_set_colormap.\n");
972 * gdk_gc_set_rgb_fg_color:
974 * @color: an unallocated #GdkColor.
976 * Set the foreground color of a GC using an unallocated color. The
977 * pixel value for the color will be determined using GdkRGB. If the
978 * colormap for the GC has not previously been initialized for GdkRGB,
979 * then for pseudo-color colormaps (colormaps with a small modifiable
980 * number of colors), a colorcube will be allocated in the colormap.
982 * Calling this function for a GC without a colormap is an error.
985 gdk_gc_set_rgb_fg_color (GdkGC *gc,
986 const GdkColor *color)
991 g_return_if_fail (GDK_IS_GC (gc));
992 g_return_if_fail (color != NULL);
994 cmap = gdk_gc_get_colormap_warn (gc);
999 gdk_rgb_find_color (cmap, &tmp_color);
1000 gdk_gc_set_foreground (gc, &tmp_color);
1004 * gdk_gc_set_rgb_bg_color:
1006 * @color: an unallocated #GdkColor.
1008 * Set the background color of a GC using an unallocated color. The
1009 * pixel value for the color will be determined using GdkRGB. If the
1010 * colormap for the GC has not previously been initialized for GdkRGB,
1011 * then for pseudo-color colormaps (colormaps with a small modifiable
1012 * number of colors), a colorcube will be allocated in the colormap.
1014 * Calling this function for a GC without a colormap is an error.
1017 gdk_gc_set_rgb_bg_color (GdkGC *gc,
1018 const GdkColor *color)
1023 g_return_if_fail (GDK_IS_GC (gc));
1024 g_return_if_fail (color != NULL);
1026 cmap = gdk_gc_get_colormap_warn (gc);
1031 gdk_rgb_find_color (cmap, &tmp_color);
1032 gdk_gc_set_background (gc, &tmp_color);
1035 static cairo_surface_t *
1036 make_stipple_tile_surface (cairo_t *cr,
1038 GdkColor *foreground,
1039 GdkColor *background)
1042 cairo_surface_t *surface;
1043 cairo_surface_t *alpha_surface;
1046 gdk_drawable_get_size (stipple,
1049 alpha_surface = _gdk_drawable_ref_cairo_surface (stipple);
1051 surface = cairo_surface_create_similar (cairo_get_target (cr),
1052 CAIRO_CONTENT_COLOR_ALPHA,
1055 tmp_cr = cairo_create (surface);
1057 cairo_set_operator (tmp_cr, CAIRO_OPERATOR_SOURCE);
1060 gdk_cairo_set_source_color (tmp_cr, background);
1062 cairo_set_source_rgba (tmp_cr, 0, 0, 0 ,0);
1064 cairo_paint (tmp_cr);
1066 cairo_set_operator (tmp_cr, CAIRO_OPERATOR_OVER);
1068 gdk_cairo_set_source_color (tmp_cr, foreground);
1069 cairo_mask_surface (tmp_cr, alpha_surface, 0, 0);
1071 cairo_destroy (tmp_cr);
1072 cairo_surface_destroy (alpha_surface);
1078 gc_get_foreground (GdkGC *gc,
1081 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
1083 color->pixel = priv->bg_pixel;
1086 gdk_colormap_query_color (gc->colormap, priv->fg_pixel, color);
1088 g_warning ("No colormap in gc_get_foreground");
1092 gc_get_background (GdkGC *gc,
1095 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
1097 color->pixel = priv->bg_pixel;
1100 gdk_colormap_query_color (gc->colormap, priv->bg_pixel, color);
1102 g_warning ("No colormap in gc_get_background");
1106 * _gdk_gc_update_context:
1109 * @override_foreground: a foreground color to use to override the
1110 * foreground color of the GC
1111 * @override_stipple: a stipple pattern to use to override the
1112 * stipple from the GC. If this is present and the fill mode
1113 * of the GC isn't %GDK_STIPPLED or %GDK_OPAQUE_STIPPLED
1114 * the fill mode will be forced to %GDK_STIPPLED
1115 * @gc_changed: pass %FALSE if the @gc has not changed since the
1116 * last call to this function
1118 * Set the attributes of a cairo context to match those of a #GdkGC
1119 * as far as possible. Some aspects of a #GdkGC, such as clip masks
1120 * and functions other than %GDK_COPY are not currently handled.
1123 _gdk_gc_update_context (GdkGC *gc,
1125 GdkColor *override_foreground,
1126 GdkBitmap *override_stipple,
1127 gboolean gc_changed)
1131 GdkColor foreground;
1132 GdkColor background;
1133 cairo_surface_t *tile_surface = NULL;
1134 GdkBitmap *stipple = NULL;
1136 g_return_if_fail (GDK_IS_GC (gc));
1137 g_return_if_fail (cr != NULL);
1138 g_return_if_fail (override_stipple == NULL || GDK_IS_PIXMAP (override_stipple));
1140 priv = GDK_GC_GET_PRIVATE (gc);
1143 if (override_stipple && fill != GDK_OPAQUE_STIPPLED)
1144 fill = GDK_STIPPLED;
1146 if (fill != GDK_TILED)
1148 if (override_foreground)
1149 foreground = *override_foreground;
1151 gc_get_foreground (gc, &foreground);
1154 if (fill == GDK_OPAQUE_STIPPLED)
1155 gc_get_background (gc, &background);
1167 case GDK_OPAQUE_STIPPLED:
1168 if (override_stipple)
1169 stipple = override_stipple;
1171 stipple = priv->stipple;
1181 gdk_cairo_set_source_color (cr, &foreground);
1184 tile_surface = _gdk_drawable_ref_cairo_surface (priv->tile);
1187 tile_surface = make_stipple_tile_surface (cr, stipple, &foreground, NULL);
1189 case GDK_OPAQUE_STIPPLED:
1190 tile_surface = make_stipple_tile_surface (cr, stipple, &foreground, &background);
1194 /* Tiles, stipples, and clip regions are all specified in device space,
1195 * not user space. For the clip region, we can simply change the matrix,
1196 * clip, then clip back, but for the source pattern, we need to
1197 * compute the right matrix.
1201 * CTM_inverse * Pattern_matrix = Translate(- ts_x, - ts_y)
1203 * (So that ts_x, ts_y in device space is taken to 0,0 in pattern
1204 * space). So, pattern_matrix = CTM * Translate(- ts_x, - tx_y);
1209 cairo_pattern_t *pattern = cairo_pattern_create_for_surface (tile_surface);
1210 cairo_matrix_t user_to_device;
1211 cairo_matrix_t user_to_pattern;
1212 cairo_matrix_t device_to_pattern;
1214 cairo_get_matrix (cr, &user_to_device);
1215 cairo_matrix_init_translate (&device_to_pattern,
1216 - gc->ts_x_origin, - gc->ts_y_origin);
1217 cairo_matrix_multiply (&user_to_pattern,
1218 &user_to_device, &device_to_pattern);
1220 cairo_pattern_set_matrix (pattern, &user_to_pattern);
1221 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
1222 cairo_set_source (cr, pattern);
1224 cairo_surface_destroy (tile_surface);
1225 cairo_pattern_destroy (pattern);
1231 cairo_reset_clip (cr);
1232 if (priv->clip_region)
1236 cairo_identity_matrix (cr);
1237 cairo_translate (cr, gc->clip_x_origin, gc->clip_y_origin);
1239 cairo_new_path (cr);
1240 gdk_cairo_region (cr, priv->clip_region);
1249 #define __GDK_GC_C__
1250 #include "gdkaliasdef.c"