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 if (priv->clip_region)
284 gdk_region_destroy (priv->clip_region);
285 priv->clip_region = NULL;
288 if (values_mask & GDK_GC_FILL)
289 priv->fill = values->fill;
290 if (values_mask & GDK_GC_STIPPLE)
292 if (priv->stipple != values->stipple)
295 g_object_unref (priv->stipple);
296 priv->stipple = values->stipple;
298 g_object_ref (priv->stipple);
301 if (values_mask & GDK_GC_TILE)
303 if (priv->tile != values->tile)
306 g_object_unref (priv->tile);
307 priv->tile = values->tile;
309 g_object_ref (priv->tile);
312 if (values_mask & GDK_GC_FOREGROUND)
313 priv->fg_pixel = values->foreground.pixel;
314 if (values_mask & GDK_GC_BACKGROUND)
315 priv->bg_pixel = values->background.pixel;
317 GDK_GC_GET_CLASS (gc)->set_values (gc, values, values_mask);
321 * gdk_gc_set_foreground:
323 * @color: the new foreground color.
325 * Sets the foreground color for a graphics context.
326 * Note that this function uses @color->pixel, use
327 * gdk_gc_set_rgb_fg_color() to specify the foreground
328 * color as red, green, blue components.
331 gdk_gc_set_foreground (GdkGC *gc,
332 const GdkColor *color)
336 g_return_if_fail (GDK_IS_GC (gc));
337 g_return_if_fail (color != NULL);
339 values.foreground = *color;
340 gdk_gc_set_values (gc, &values, GDK_GC_FOREGROUND);
344 * gdk_gc_set_background:
346 * @color: the new background color.
348 * Sets the background color for a graphics context.
349 * Note that this function uses @color->pixel, use
350 * gdk_gc_set_rgb_bg_color() to specify the background
351 * color as red, green, blue components.
354 gdk_gc_set_background (GdkGC *gc,
355 const GdkColor *color)
359 g_return_if_fail (GDK_IS_GC (gc));
360 g_return_if_fail (color != NULL);
362 values.background = *color;
363 gdk_gc_set_values (gc, &values, GDK_GC_BACKGROUND);
369 * @font: the new font.
371 * Sets the font for a graphics context. (Note that
372 * all text-drawing functions in GDK take a @font
373 * argument; the value set here is used when that
374 * argument is %NULL.)
377 gdk_gc_set_font (GdkGC *gc,
382 g_return_if_fail (GDK_IS_GC (gc));
383 g_return_if_fail (font != NULL);
386 gdk_gc_set_values (gc, &values, GDK_GC_FONT);
390 * gdk_gc_set_function:
392 * @function: the #GdkFunction to use
394 * Determines how the current pixel values and the
395 * pixel values being drawn are combined to produce
396 * the final pixel values.
399 gdk_gc_set_function (GdkGC *gc,
400 GdkFunction function)
404 g_return_if_fail (GDK_IS_GC (gc));
406 values.function = function;
407 gdk_gc_set_values (gc, &values, GDK_GC_FUNCTION);
413 * @fill: the new fill mode.
415 * Set the fill mode for a graphics context.
418 gdk_gc_set_fill (GdkGC *gc,
423 g_return_if_fail (GDK_IS_GC (gc));
426 gdk_gc_set_values (gc, &values, GDK_GC_FILL);
432 * @tile: the new tile pixmap.
434 * Set a tile pixmap for a graphics context.
435 * This will only be used if the fill mode
439 gdk_gc_set_tile (GdkGC *gc,
444 g_return_if_fail (GDK_IS_GC (gc));
447 gdk_gc_set_values (gc, &values, GDK_GC_TILE);
451 * gdk_gc_set_stipple:
453 * @stipple: the new stipple bitmap.
455 * Set the stipple bitmap for a graphics context. The
456 * stipple will only be used if the fill mode is
457 * %GDK_STIPPLED or %GDK_OPAQUE_STIPPLED.
460 gdk_gc_set_stipple (GdkGC *gc,
465 g_return_if_fail (GDK_IS_GC (gc));
467 values.stipple = stipple;
468 gdk_gc_set_values (gc, &values, GDK_GC_STIPPLE);
472 * gdk_gc_set_ts_origin:
474 * @x: the x-coordinate of the origin.
475 * @y: the y-coordinate of the origin.
477 * Set the origin when using tiles or stipples with
478 * the GC. The tile or stipple will be aligned such
479 * that the upper left corner of the tile or stipple
480 * will coincide with this point.
483 gdk_gc_set_ts_origin (GdkGC *gc,
489 g_return_if_fail (GDK_IS_GC (gc));
491 values.ts_x_origin = x;
492 values.ts_y_origin = y;
494 gdk_gc_set_values (gc, &values,
495 GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN);
499 * gdk_gc_set_clip_origin:
501 * @x: the x-coordinate of the origin.
502 * @y: the y-coordinate of the origin.
504 * Sets the origin of the clip mask. The coordinates are
505 * interpreted relative to the upper-left corner of
506 * the destination drawable of the current operation.
509 gdk_gc_set_clip_origin (GdkGC *gc,
515 g_return_if_fail (GDK_IS_GC (gc));
517 values.clip_x_origin = x;
518 values.clip_y_origin = y;
520 gdk_gc_set_values (gc, &values,
521 GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
525 * gdk_gc_set_clip_mask:
529 * Sets the clip mask for a graphics context from a bitmap.
530 * The clip mask is interpreted relative to the clip
531 * origin. (See gdk_gc_set_clip_origin()).
534 gdk_gc_set_clip_mask (GdkGC *gc,
539 g_return_if_fail (GDK_IS_GC (gc));
541 values.clip_mask = mask;
542 gdk_gc_set_values (gc, &values, GDK_GC_CLIP_MASK);
546 _gdk_gc_set_clip_region_internal (GdkGC *gc,
549 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
551 if (priv->clip_region)
552 gdk_region_destroy (priv->clip_region);
554 priv->clip_region = region;
556 _gdk_windowing_gc_set_clip_region (gc, region);
560 * gdk_gc_set_clip_rectangle:
562 * @rectangle: the rectangle to clip to.
564 * Sets the clip mask for a graphics context from a
565 * rectangle. The clip mask is interpreted relative to the clip
566 * origin. (See gdk_gc_set_clip_origin()).
569 gdk_gc_set_clip_rectangle (GdkGC *gc,
570 const GdkRectangle *rectangle)
574 g_return_if_fail (GDK_IS_GC (gc));
577 region = gdk_region_rectangle (rectangle);
581 _gdk_gc_set_clip_region_internal (gc, region);
585 * gdk_gc_set_clip_region:
587 * @region: the #GdkRegion.
589 * Sets the clip mask for a graphics context from a region structure.
590 * The clip mask is interpreted relative to the clip origin. (See
591 * gdk_gc_set_clip_origin()).
594 gdk_gc_set_clip_region (GdkGC *gc,
595 const GdkRegion *region)
599 g_return_if_fail (GDK_IS_GC (gc));
602 copy = gdk_region_copy (region);
606 _gdk_gc_set_clip_region_internal (gc, copy);
610 * _gdk_gc_get_clip_region:
613 * Gets the current clip region for @gc, if any.
615 * Return value: the clip region for the GC, or %NULL.
616 * (if a clip mask is set, the return will be %NULL)
617 * This value is owned by the GC and must not be freed.
620 _gdk_gc_get_clip_region (GdkGC *gc)
622 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
624 return GDK_GC_GET_PRIVATE (gc)->clip_region;
631 * Gets the current file style for the GC
633 * Return value: the file style for the GC
636 _gdk_gc_get_fill (GdkGC *gc)
638 g_return_val_if_fail (GDK_IS_GC (gc), GDK_SOLID);
640 return GDK_GC_GET_PRIVATE (gc)->fill;
647 * Gets the tile pixmap for @gc, if any
649 * Return value: the tile set on the GC, or %NULL. The
650 * value is owned by the GC and must not be freed.
653 _gdk_gc_get_tile (GdkGC *gc)
655 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
657 return GDK_GC_GET_PRIVATE (gc)->tile;
661 * _gdk_gc_get_stipple:
664 * Gets the stipple pixmap for @gc, if any
666 * Return value: the stipple set on the GC, or %NULL. The
667 * value is owned by the GC and must not be freed.
670 _gdk_gc_get_stipple (GdkGC *gc)
672 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
674 return GDK_GC_GET_PRIVATE (gc)->stipple;
678 * _gdk_gc_get_fg_pixel:
681 * Gets the foreground pixel value for @gc. If the
682 * foreground pixel has never been set, returns the
685 * Return value: the foreground pixel value of the GC
688 _gdk_gc_get_fg_pixel (GdkGC *gc)
690 g_return_val_if_fail (GDK_IS_GC (gc), 0);
692 return GDK_GC_GET_PRIVATE (gc)->fg_pixel;
696 * _gdk_gc_get_bg_pixel:
699 * Gets the background pixel value for @gc.If the
700 * foreground pixel has never been set, returns the
703 * Return value: the foreground pixel value of the GC
706 _gdk_gc_get_bg_pixel (GdkGC *gc)
708 g_return_val_if_fail (GDK_IS_GC (gc), 0);
710 return GDK_GC_GET_PRIVATE (gc)->bg_pixel;
714 * gdk_gc_set_subwindow:
716 * @mode: the subwindow mode.
718 * Sets how drawing with this GC on a window will affect child
719 * windows of that window.
722 gdk_gc_set_subwindow (GdkGC *gc,
723 GdkSubwindowMode mode)
727 g_return_if_fail (GDK_IS_GC (gc));
729 values.subwindow_mode = mode;
730 gdk_gc_set_values (gc, &values, GDK_GC_SUBWINDOW);
734 * gdk_gc_set_exposures:
736 * @exposures: if %TRUE, exposure events will be generated.
738 * Sets whether copying non-visible portions of a drawable
739 * using this graphics context generate exposure events
740 * for the corresponding regions of the destination
741 * drawable. (See gdk_draw_drawable()).
744 gdk_gc_set_exposures (GdkGC *gc,
749 g_return_if_fail (GDK_IS_GC (gc));
751 values.graphics_exposures = exposures;
752 gdk_gc_set_values (gc, &values, GDK_GC_EXPOSURES);
756 * gdk_gc_set_line_attributes:
758 * @line_width: the width of lines.
759 * @line_style: the dash-style for lines.
760 * @cap_style: the manner in which the ends of lines are drawn.
761 * @join_style: the in which lines are joined together.
763 * Sets various attributes of how lines are drawn. See
764 * the corresponding members of #GdkGCValues for full
765 * explanations of the arguments.
768 gdk_gc_set_line_attributes (GdkGC *gc,
770 GdkLineStyle line_style,
771 GdkCapStyle cap_style,
772 GdkJoinStyle join_style)
776 values.line_width = line_width;
777 values.line_style = line_style;
778 values.cap_style = cap_style;
779 values.join_style = join_style;
781 gdk_gc_set_values (gc, &values,
791 * @dash_offset: the phase of the dash pattern.
792 * @dash_list: an array of dash lengths.
793 * @n: the number of elements in @dash_list.
795 * Sets the way dashed-lines are drawn. Lines will be
796 * drawn with alternating on and off segments of the
797 * lengths specified in @dash_list. The manner in
798 * which the on and off segments are drawn is determined
799 * by the @line_style value of the GC. (This can
800 * be changed with gdk_gc_set_line_attributes().)
802 * The @dash_offset defines the phase of the pattern,
803 * specifying how many pixels into the dash-list the pattern
804 * should actually begin.
807 gdk_gc_set_dashes (GdkGC *gc,
812 g_return_if_fail (GDK_IS_GC (gc));
813 g_return_if_fail (dash_list != NULL);
815 GDK_GC_GET_CLASS (gc)->set_dashes (gc, dash_offset, dash_list, n);
821 * @x_offset: amount by which to offset the GC in the X direction
822 * @y_offset: amount by which to offset the GC in the Y direction
824 * Offset attributes such as the clip and tile-stipple origins
825 * of the GC so that drawing at x - x_offset, y - y_offset with
826 * the offset GC has the same effect as drawing at x, y with the original
830 gdk_gc_offset (GdkGC *gc,
834 if (x_offset != 0 || y_offset != 0)
838 values.clip_x_origin = gc->clip_x_origin - x_offset;
839 values.clip_y_origin = gc->clip_y_origin - y_offset;
840 values.ts_x_origin = gc->ts_x_origin - x_offset;
841 values.ts_y_origin = gc->ts_y_origin - y_offset;
843 gdk_gc_set_values (gc, &values,
844 GDK_GC_CLIP_X_ORIGIN |
845 GDK_GC_CLIP_Y_ORIGIN |
853 * @dst_gc: the destination graphics context.
854 * @src_gc: the source graphics context.
856 * Copy the set of values from one graphics context
857 * onto another graphics context.
860 gdk_gc_copy (GdkGC *dst_gc,
863 GdkGCPrivate *dst_priv, *src_priv;
865 g_return_if_fail (GDK_IS_GC (dst_gc));
866 g_return_if_fail (GDK_IS_GC (src_gc));
868 dst_priv = GDK_GC_GET_PRIVATE (dst_gc);
869 src_priv = GDK_GC_GET_PRIVATE (src_gc);
871 _gdk_windowing_gc_copy (dst_gc, src_gc);
873 dst_gc->clip_x_origin = src_gc->clip_x_origin;
874 dst_gc->clip_y_origin = src_gc->clip_y_origin;
875 dst_gc->ts_x_origin = src_gc->ts_x_origin;
876 dst_gc->ts_y_origin = src_gc->ts_y_origin;
878 if (src_gc->colormap)
879 g_object_ref (src_gc->colormap);
881 if (dst_gc->colormap)
882 g_object_unref (dst_gc->colormap);
884 dst_gc->colormap = src_gc->colormap;
886 if (dst_priv->clip_region)
887 gdk_region_destroy (dst_priv->clip_region);
889 if (src_priv->clip_region)
890 dst_priv->clip_region = gdk_region_copy (src_priv->clip_region);
892 dst_priv->clip_region = NULL;
894 dst_priv->fill = src_priv->fill;
896 if (dst_priv->stipple)
897 g_object_unref (dst_priv->stipple);
898 dst_priv->stipple = src_priv->stipple;
899 if (dst_priv->stipple)
900 g_object_ref (dst_priv->stipple);
903 g_object_unref (dst_priv->tile);
904 dst_priv->tile = src_priv->tile;
906 g_object_ref (dst_priv->tile);
908 dst_priv->fg_pixel = src_priv->fg_pixel;
909 dst_priv->bg_pixel = src_priv->bg_pixel;
913 * gdk_gc_set_colormap:
915 * @colormap: a #GdkColormap
917 * Sets the colormap for the GC to the given colormap. The depth
918 * of the colormap's visual must match the depth of the drawable
919 * for which the GC was created.
922 gdk_gc_set_colormap (GdkGC *gc,
923 GdkColormap *colormap)
925 g_return_if_fail (GDK_IS_GC (gc));
926 g_return_if_fail (GDK_IS_COLORMAP (colormap));
928 if (gc->colormap != colormap)
931 g_object_unref (gc->colormap);
933 gc->colormap = colormap;
934 g_object_ref (gc->colormap);
940 * gdk_gc_get_colormap:
943 * Retrieves the colormap for a given GC, if it exists.
944 * A GC will have a colormap if the drawable for which it was created
945 * has a colormap, or if a colormap was set explicitely with
946 * gdk_gc_set_colormap.
948 * Return value: the colormap of @gc, or %NULL if @gc doesn't have one.
951 gdk_gc_get_colormap (GdkGC *gc)
953 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
959 gdk_gc_get_colormap_warn (GdkGC *gc)
961 GdkColormap *colormap = gdk_gc_get_colormap (gc);
964 g_warning ("gdk_gc_set_rgb_fg_color() and gdk_gc_set_rgb_bg_color() can\n"
965 "only be used on GC's with a colormap. A GC will have a colormap\n"
966 "if it is created for a drawable with a colormap, or if a\n"
967 "colormap has been set explicitly with gdk_gc_set_colormap.\n");
975 * gdk_gc_set_rgb_fg_color:
977 * @color: an unallocated #GdkColor.
979 * Set the foreground color of a GC using an unallocated color. The
980 * pixel value for the color will be determined using GdkRGB. If the
981 * colormap for the GC has not previously been initialized for GdkRGB,
982 * then for pseudo-color colormaps (colormaps with a small modifiable
983 * number of colors), a colorcube will be allocated in the colormap.
985 * Calling this function for a GC without a colormap is an error.
988 gdk_gc_set_rgb_fg_color (GdkGC *gc,
989 const GdkColor *color)
994 g_return_if_fail (GDK_IS_GC (gc));
995 g_return_if_fail (color != NULL);
997 cmap = gdk_gc_get_colormap_warn (gc);
1002 gdk_rgb_find_color (cmap, &tmp_color);
1003 gdk_gc_set_foreground (gc, &tmp_color);
1007 * gdk_gc_set_rgb_bg_color:
1009 * @color: an unallocated #GdkColor.
1011 * Set the background color of a GC using an unallocated color. The
1012 * pixel value for the color will be determined using GdkRGB. If the
1013 * colormap for the GC has not previously been initialized for GdkRGB,
1014 * then for pseudo-color colormaps (colormaps with a small modifiable
1015 * number of colors), a colorcube will be allocated in the colormap.
1017 * Calling this function for a GC without a colormap is an error.
1020 gdk_gc_set_rgb_bg_color (GdkGC *gc,
1021 const GdkColor *color)
1026 g_return_if_fail (GDK_IS_GC (gc));
1027 g_return_if_fail (color != NULL);
1029 cmap = gdk_gc_get_colormap_warn (gc);
1034 gdk_rgb_find_color (cmap, &tmp_color);
1035 gdk_gc_set_background (gc, &tmp_color);
1038 static cairo_surface_t *
1039 make_stipple_tile_surface (cairo_t *cr,
1041 GdkColor *foreground,
1042 GdkColor *background)
1045 cairo_surface_t *surface;
1046 cairo_surface_t *alpha_surface;
1049 gdk_drawable_get_size (stipple,
1052 alpha_surface = _gdk_drawable_ref_cairo_surface (stipple);
1054 surface = cairo_surface_create_similar (cairo_get_target (cr),
1055 CAIRO_CONTENT_COLOR_ALPHA,
1058 tmp_cr = cairo_create (surface);
1060 cairo_set_operator (tmp_cr, CAIRO_OPERATOR_SOURCE);
1063 gdk_cairo_set_source_color (tmp_cr, background);
1065 cairo_set_source_rgba (tmp_cr, 0, 0, 0 ,0);
1067 cairo_paint (tmp_cr);
1069 cairo_set_operator (tmp_cr, CAIRO_OPERATOR_OVER);
1071 gdk_cairo_set_source_color (tmp_cr, foreground);
1072 cairo_mask_surface (tmp_cr, alpha_surface, 0, 0);
1074 cairo_destroy (tmp_cr);
1075 cairo_surface_destroy (alpha_surface);
1081 gc_get_foreground (GdkGC *gc,
1084 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
1086 color->pixel = priv->bg_pixel;
1089 gdk_colormap_query_color (gc->colormap, priv->fg_pixel, color);
1091 g_warning ("No colormap in gc_get_foreground");
1095 gc_get_background (GdkGC *gc,
1098 GdkGCPrivate *priv = GDK_GC_GET_PRIVATE (gc);
1100 color->pixel = priv->bg_pixel;
1103 gdk_colormap_query_color (gc->colormap, priv->bg_pixel, color);
1105 g_warning ("No colormap in gc_get_background");
1109 * _gdk_gc_update_context:
1112 * @override_foreground: a foreground color to use to override the
1113 * foreground color of the GC
1114 * @override_stipple: a stipple pattern to use to override the
1115 * stipple from the GC. If this is present and the fill mode
1116 * of the GC isn't %GDK_STIPPLED or %GDK_OPAQUE_STIPPLED
1117 * the fill mode will be forced to %GDK_STIPPLED
1118 * @gc_changed: pass %FALSE if the @gc has not changed since the
1119 * last call to this function
1121 * Set the attributes of a cairo context to match those of a #GdkGC
1122 * as far as possible. Some aspects of a #GdkGC, such as clip masks
1123 * and functions other than %GDK_COPY are not currently handled.
1126 _gdk_gc_update_context (GdkGC *gc,
1128 const GdkColor *override_foreground,
1129 GdkBitmap *override_stipple,
1130 gboolean gc_changed)
1134 GdkColor foreground;
1135 GdkColor background;
1136 cairo_surface_t *tile_surface = NULL;
1137 GdkBitmap *stipple = NULL;
1139 g_return_if_fail (GDK_IS_GC (gc));
1140 g_return_if_fail (cr != NULL);
1141 g_return_if_fail (override_stipple == NULL || GDK_IS_PIXMAP (override_stipple));
1143 priv = GDK_GC_GET_PRIVATE (gc);
1146 if (override_stipple && fill != GDK_OPAQUE_STIPPLED)
1147 fill = GDK_STIPPLED;
1149 if (fill != GDK_TILED)
1151 if (override_foreground)
1152 foreground = *override_foreground;
1154 gc_get_foreground (gc, &foreground);
1157 if (fill == GDK_OPAQUE_STIPPLED)
1158 gc_get_background (gc, &background);
1170 case GDK_OPAQUE_STIPPLED:
1171 if (override_stipple)
1172 stipple = override_stipple;
1174 stipple = priv->stipple;
1184 gdk_cairo_set_source_color (cr, &foreground);
1187 tile_surface = _gdk_drawable_ref_cairo_surface (priv->tile);
1190 tile_surface = make_stipple_tile_surface (cr, stipple, &foreground, NULL);
1192 case GDK_OPAQUE_STIPPLED:
1193 tile_surface = make_stipple_tile_surface (cr, stipple, &foreground, &background);
1197 /* Tiles, stipples, and clip regions are all specified in device space,
1198 * not user space. For the clip region, we can simply change the matrix,
1199 * clip, then clip back, but for the source pattern, we need to
1200 * compute the right matrix.
1204 * CTM_inverse * Pattern_matrix = Translate(- ts_x, - ts_y)
1206 * (So that ts_x, ts_y in device space is taken to 0,0 in pattern
1207 * space). So, pattern_matrix = CTM * Translate(- ts_x, - tx_y);
1212 cairo_pattern_t *pattern = cairo_pattern_create_for_surface (tile_surface);
1213 cairo_matrix_t user_to_device;
1214 cairo_matrix_t user_to_pattern;
1215 cairo_matrix_t device_to_pattern;
1217 cairo_get_matrix (cr, &user_to_device);
1218 cairo_matrix_init_translate (&device_to_pattern,
1219 - gc->ts_x_origin, - gc->ts_y_origin);
1220 cairo_matrix_multiply (&user_to_pattern,
1221 &user_to_device, &device_to_pattern);
1223 cairo_pattern_set_matrix (pattern, &user_to_pattern);
1224 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
1225 cairo_set_source (cr, pattern);
1227 cairo_surface_destroy (tile_surface);
1228 cairo_pattern_destroy (pattern);
1234 cairo_reset_clip (cr);
1235 if (priv->clip_region)
1239 cairo_identity_matrix (cr);
1240 cairo_translate (cr, gc->clip_x_origin, gc->clip_y_origin);
1242 cairo_new_path (cr);
1243 gdk_cairo_region (cr, priv->clip_region);
1252 #define __GDK_GC_C__
1253 #include "gdkaliasdef.c"