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/.
31 #include "gdkprivate.h"
33 static void gdk_gc_class_init (GObjectClass *class);
34 static void gdk_gc_finalize (GObject *object);
36 static GObjectClass *parent_class;
39 gdk_gc_get_type (void)
41 static GType object_type = 0;
45 static const GTypeInfo object_info =
49 (GBaseFinalizeFunc) NULL,
50 (GClassInitFunc) gdk_gc_class_init,
51 NULL, /* class_finalize */
52 NULL, /* class_data */
55 (GInstanceInitFunc) NULL,
58 object_type = g_type_register_static (G_TYPE_OBJECT,
67 gdk_gc_class_init (GObjectClass *class)
69 parent_class = g_type_class_peek_parent (class);
71 class->finalize = gdk_gc_finalize;
75 gdk_gc_new (GdkDrawable *drawable)
77 g_return_val_if_fail (drawable != NULL, NULL);
79 return gdk_gc_new_with_values (drawable, NULL, 0);
83 gdk_gc_new_with_values (GdkDrawable *drawable,
85 GdkGCValuesMask values_mask)
89 g_return_val_if_fail (drawable != NULL, NULL);
91 gc = GDK_DRAWABLE_GET_CLASS (drawable)->create_gc (drawable,
95 if (gc == NULL) /* This would mean the drawable was destroyed. */
98 if (values_mask & GDK_GC_CLIP_X_ORIGIN)
99 gc->clip_x_origin = values->clip_x_origin;
100 if (values_mask & GDK_GC_CLIP_Y_ORIGIN)
101 gc->clip_y_origin = values->clip_y_origin;
102 if (values_mask & GDK_GC_TS_X_ORIGIN)
103 gc->ts_x_origin = values->ts_x_origin;
104 if (values_mask & GDK_GC_TS_Y_ORIGIN)
105 gc->ts_y_origin = values->ts_y_origin;
107 gc->colormap = gdk_drawable_get_colormap (drawable);
109 g_object_ref (G_OBJECT (gc->colormap));
115 gdk_gc_finalize (GObject *object)
117 GdkGC *gc = GDK_GC (object);
120 g_object_unref (G_OBJECT (gc->colormap));
122 parent_class->finalize (object);
129 * Deprecated function; use g_object_ref() instead.
131 * Return value: the gc.
134 gdk_gc_ref (GdkGC *gc)
136 return (GdkGC *) g_object_ref (G_OBJECT (gc));
143 * Deprecated function; use g_object_unref() instead.
146 gdk_gc_unref (GdkGC *gc)
148 g_object_unref (G_OBJECT (gc));
152 gdk_gc_get_values (GdkGC *gc,
155 g_return_if_fail (GDK_IS_GC (gc));
156 g_return_if_fail (values != NULL);
158 GDK_GC_GET_CLASS (gc)->get_values (gc, values);
164 * @values: struct containing the new values
165 * @values_mask: mask indicating which struct fields are to be used
167 * Sets attributes of a graphics context in bulk. For each flag set in
168 * @values_mask, the corresponding field will be read from @values and
169 * set as the new value for @gc. If you're only setting a few values
170 * on @gc, calling individual "setter" functions is likely more
175 gdk_gc_set_values (GdkGC *gc,
177 GdkGCValuesMask values_mask)
179 g_return_if_fail (GDK_IS_GC (gc));
180 g_return_if_fail (values != NULL);
182 if (values_mask & GDK_GC_CLIP_X_ORIGIN)
183 gc->clip_x_origin = values->clip_x_origin;
184 if (values_mask & GDK_GC_CLIP_Y_ORIGIN)
185 gc->clip_y_origin = values->clip_y_origin;
186 if (values_mask & GDK_GC_TS_X_ORIGIN)
187 gc->ts_x_origin = values->ts_x_origin;
188 if (values_mask & GDK_GC_TS_Y_ORIGIN)
189 gc->ts_y_origin = values->ts_y_origin;
191 GDK_GC_GET_CLASS (gc)->set_values (gc, values, values_mask);
195 gdk_gc_set_foreground (GdkGC *gc,
200 g_return_if_fail (GDK_IS_GC (gc));
201 g_return_if_fail (color != NULL);
203 values.foreground = *color;
204 gdk_gc_set_values (gc, &values, GDK_GC_FOREGROUND);
208 gdk_gc_set_background (GdkGC *gc,
213 g_return_if_fail (GDK_IS_GC (gc));
214 g_return_if_fail (color != NULL);
216 values.background = *color;
217 gdk_gc_set_values (gc, &values, GDK_GC_BACKGROUND);
221 gdk_gc_set_font (GdkGC *gc,
226 g_return_if_fail (GDK_IS_GC (gc));
227 g_return_if_fail (font != NULL);
230 gdk_gc_set_values (gc, &values, GDK_GC_FONT);
234 gdk_gc_set_function (GdkGC *gc,
235 GdkFunction function)
239 g_return_if_fail (GDK_IS_GC (gc));
241 values.function = function;
242 gdk_gc_set_values (gc, &values, GDK_GC_FUNCTION);
246 gdk_gc_set_fill (GdkGC *gc,
251 g_return_if_fail (GDK_IS_GC (gc));
254 gdk_gc_set_values (gc, &values, GDK_GC_FILL);
258 gdk_gc_set_tile (GdkGC *gc,
263 g_return_if_fail (GDK_IS_GC (gc));
266 gdk_gc_set_values (gc, &values, GDK_GC_TILE);
270 gdk_gc_set_stipple (GdkGC *gc,
275 g_return_if_fail (GDK_IS_GC (gc));
277 values.stipple = stipple;
278 gdk_gc_set_values (gc, &values, GDK_GC_STIPPLE);
282 gdk_gc_set_ts_origin (GdkGC *gc,
288 g_return_if_fail (GDK_IS_GC (gc));
290 values.ts_x_origin = x;
291 values.ts_y_origin = y;
293 gdk_gc_set_values (gc, &values,
294 GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN);
298 gdk_gc_set_clip_origin (GdkGC *gc,
304 g_return_if_fail (GDK_IS_GC (gc));
306 values.clip_x_origin = x;
307 values.clip_y_origin = y;
309 gdk_gc_set_values (gc, &values,
310 GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
314 gdk_gc_set_clip_mask (GdkGC *gc,
319 g_return_if_fail (GDK_IS_GC (gc));
321 values.clip_mask = mask;
322 gdk_gc_set_values (gc, &values, GDK_GC_CLIP_MASK);
327 gdk_gc_set_subwindow (GdkGC *gc,
328 GdkSubwindowMode mode)
332 g_return_if_fail (GDK_IS_GC (gc));
334 values.subwindow_mode = mode;
335 gdk_gc_set_values (gc, &values, GDK_GC_SUBWINDOW);
339 gdk_gc_set_exposures (GdkGC *gc,
344 g_return_if_fail (GDK_IS_GC (gc));
346 values.graphics_exposures = exposures;
347 gdk_gc_set_values (gc, &values, GDK_GC_EXPOSURES);
351 gdk_gc_set_line_attributes (GdkGC *gc,
353 GdkLineStyle line_style,
354 GdkCapStyle cap_style,
355 GdkJoinStyle join_style)
359 values.line_width = line_width;
360 values.line_style = line_style;
361 values.cap_style = cap_style;
362 values.join_style = join_style;
364 gdk_gc_set_values (gc, &values,
372 gdk_gc_set_dashes (GdkGC *gc,
377 g_return_if_fail (GDK_IS_GC (gc));
378 g_return_if_fail (dash_list != NULL);
380 GDK_GC_GET_CLASS (gc)->set_dashes (gc, dash_offset, dash_list, n);
386 * @x_offset: amount by which to offset the GC in the X direction
387 * @y_offset: amount by which to offset the GC in the Y direction
389 * Offset attributes such as the clip and tile-stipple origins
390 * of the GC so that drawing at x - x_offset, y - y_offset with
391 * the offset GC has the same effect as drawing at x, y with the original
395 gdk_gc_offset (GdkGC *gc,
399 if (x_offset != 0 || y_offset != 0)
403 values.clip_x_origin = gc->clip_x_origin - x_offset;
404 values.clip_y_origin = gc->clip_y_origin - y_offset;
405 values.ts_x_origin = gc->ts_x_origin - x_offset;
406 values.ts_y_origin = gc->ts_y_origin - y_offset;
408 gdk_gc_set_values (gc, &values,
409 GDK_GC_CLIP_X_ORIGIN |
410 GDK_GC_CLIP_Y_ORIGIN |
417 * gdk_gc_set_colormap:
419 * @colormap: a #GdkColormap
421 * Sets the colormap for the GC to the given colormap. The depth
422 * of the colormap's visual must match the depth of the drawable
423 * for which the GC was created.
426 gdk_gc_set_colormap (GdkGC *gc,
427 GdkColormap *colormap)
429 g_return_if_fail (GDK_IS_GC (gc));
430 g_return_if_fail (GDK_IS_COLORMAP (colormap));
432 if (gc->colormap != colormap)
435 g_object_unref (G_OBJECT (gc->colormap));
437 gc->colormap = colormap;
438 g_object_ref (G_OBJECT (gc->colormap));
444 * gdk_gc_get_colormap:
447 * Retrieves the colormap for a given GC, if it exists.
448 * A GC will have a colormap if the drawable for which it was created
449 * has a colormap, or if a colormap was set explicitely with
450 * gdk_gc_set_colormap.
455 gdk_gc_get_colormap (GdkGC *gc)
457 g_return_val_if_fail (GDK_IS_GC (gc), NULL);
463 gdk_gc_get_colormap_warn (GdkGC *gc)
465 GdkColormap *colormap = gdk_gc_get_colormap (gc);
468 g_warning ("gdk_gc_set_rgb_fg_color() and gdk_gc_set_rgb_bg_color() can\n"
469 "only be used on GC's with a colormap. A GC will have a colormap\n"
470 "if it is created for a drawable with a colormap, or if a\n"
471 "colormap has been set explicitly with gdk_gc_set_colormap.\n");
479 * gdk_gc_set_rgb_fg_color:
481 * @color: an unallocated #GdkColor.
483 * Set the foreground color of a GC using an unallocated color. The
484 * pixel value for the color will be determined using GdkRGB. If the
485 * colormap for the GC has not previously been initialized for GdkRGB,
486 * then for pseudo-color colormaps (colormaps with a small modifiable
487 * number of colors), a colorcube will be allocated in the colormap.
489 * Calling this function for a GC without a colormap is an error.
492 gdk_gc_set_rgb_fg_color (GdkGC *gc, GdkColor *color)
497 g_return_if_fail (GDK_IS_GC (gc));
498 g_return_if_fail (color != NULL);
500 cmap = gdk_gc_get_colormap_warn (gc);
505 gdk_rgb_find_color (cmap, &tmp_color);
506 gdk_gc_set_foreground (gc, &tmp_color);
510 * gdk_gc_set_rgb_bg_color:
512 * @color: an unallocated #GdkColor.
514 * Set the background color of a GC using an unallocated color. The
515 * pixel value for the color will be determined using GdkRGB. If the
516 * colormap for the GC has not previously been initialized for GdkRGB,
517 * then for pseudo-color colormaps (colormaps with a small modifiable
518 * number of colors), a colorcube will be allocated in the colormap.
520 * Calling this function for a GC without a colormap is an error.
523 gdk_gc_set_rgb_bg_color (GdkGC *gc, GdkColor *color)
528 g_return_if_fail (GDK_IS_GC (gc));
529 g_return_if_fail (color != NULL);
531 cmap = gdk_gc_get_colormap_warn (gc);
536 gdk_rgb_find_color (cmap, &tmp_color);
537 gdk_gc_set_background (gc, &tmp_color);