1 /* GTK - The GIMP Toolkit
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 typedef struct _GtkGCKey GtkGCKey;
33 typedef struct _GtkGCDrawable GtkGCDrawable;
38 GdkColormap *colormap;
50 static void gtk_gc_init (void);
51 static GtkGCKey* gtk_gc_key_dup (GtkGCKey *key);
52 static void gtk_gc_key_destroy (GtkGCKey *key);
53 static gpointer gtk_gc_new (gpointer key);
54 static void gtk_gc_destroy (gpointer value);
55 static guint gtk_gc_key_hash (gpointer key);
56 static guint gtk_gc_value_hash (gpointer value);
57 static gint gtk_gc_key_equal (gpointer a,
59 static guint gtk_gc_drawable_hash (GtkGCDrawable *d);
60 static gint gtk_gc_drawable_equal (GtkGCDrawable *a,
64 static gint initialize = TRUE;
65 static GCache *gc_cache = NULL;
67 static GMemChunk *key_mem_chunk = NULL;
71 gtk_gc_get (gint depth,
72 GdkColormap *colormap,
74 GdkGCValuesMask values_mask)
83 key.colormap = colormap;
85 key.mask = values_mask;
87 gc = g_cache_insert (gc_cache, &key);
93 gtk_gc_release (GdkGC *gc)
98 g_cache_remove (gc_cache, gc);
102 free_gc_drawable (gpointer data)
104 GtkGCDrawable *drawable = data;
105 g_object_unref (drawable->drawable);
110 gtk_gc_get_drawable_ht (GdkScreen *screen)
112 GHashTable *ht = g_object_get_data (G_OBJECT (screen), "gtk-gc-drawable-ht");
115 ht = g_hash_table_new_full ((GHashFunc) gtk_gc_drawable_hash,
116 (GEqualFunc) gtk_gc_drawable_equal,
117 NULL, free_gc_drawable);
118 g_object_set_data_full (G_OBJECT (screen),
119 "gtk-gc-drawable-ht", ht,
120 (GDestroyNotify)g_hash_table_destroy);
131 gc_cache = g_cache_new ((GCacheNewFunc) gtk_gc_new,
132 (GCacheDestroyFunc) gtk_gc_destroy,
133 (GCacheDupFunc) gtk_gc_key_dup,
134 (GCacheDestroyFunc) gtk_gc_key_destroy,
135 (GHashFunc) gtk_gc_key_hash,
136 (GHashFunc) gtk_gc_value_hash,
137 (GEqualFunc) gtk_gc_key_equal);
141 gtk_gc_key_dup (GtkGCKey *key)
146 key_mem_chunk = g_mem_chunk_new ("key mem chunk", sizeof (GtkGCKey),
147 1024, G_ALLOC_AND_FREE);
149 new_key = g_chunk_new (GtkGCKey, key_mem_chunk);
157 gtk_gc_key_destroy (GtkGCKey *key)
159 g_mem_chunk_free (key_mem_chunk, key);
163 gtk_gc_new (gpointer key)
166 GtkGCDrawable *drawable;
172 screen = gdk_colormap_get_screen (keyval->colormap);
174 ht = gtk_gc_get_drawable_ht (screen);
175 drawable = g_hash_table_lookup (ht, &keyval->depth);
178 drawable = g_new (GtkGCDrawable, 1);
179 drawable->depth = keyval->depth;
180 drawable->drawable = gdk_pixmap_new (gdk_screen_get_root_window (screen),
181 1, 1, drawable->depth);
182 g_hash_table_insert (ht, &drawable->depth, drawable);
185 gc = gdk_gc_new_with_values (drawable->drawable, &keyval->values, keyval->mask);
186 gdk_gc_set_colormap (gc, keyval->colormap);
188 return (gpointer) gc;
192 gtk_gc_destroy (gpointer value)
194 g_object_unref (value);
198 gtk_gc_key_hash (gpointer key)
206 if (keyval->mask & GDK_GC_FOREGROUND)
208 hash_val += keyval->values.foreground.pixel;
210 if (keyval->mask & GDK_GC_BACKGROUND)
212 hash_val += keyval->values.background.pixel;
214 if (keyval->mask & GDK_GC_FONT)
216 hash_val += gdk_font_id (keyval->values.font);
218 if (keyval->mask & GDK_GC_FUNCTION)
220 hash_val += (gint) keyval->values.function;
222 if (keyval->mask & GDK_GC_FILL)
224 hash_val += (gint) keyval->values.fill;
226 if (keyval->mask & GDK_GC_TILE)
228 hash_val += (glong) keyval->values.tile;
230 if (keyval->mask & GDK_GC_STIPPLE)
232 hash_val += (glong) keyval->values.stipple;
234 if (keyval->mask & GDK_GC_CLIP_MASK)
236 hash_val += (glong) keyval->values.clip_mask;
238 if (keyval->mask & GDK_GC_SUBWINDOW)
240 hash_val += (gint) keyval->values.subwindow_mode;
242 if (keyval->mask & GDK_GC_TS_X_ORIGIN)
244 hash_val += (gint) keyval->values.ts_x_origin;
246 if (keyval->mask & GDK_GC_TS_Y_ORIGIN)
248 hash_val += (gint) keyval->values.ts_y_origin;
250 if (keyval->mask & GDK_GC_CLIP_X_ORIGIN)
252 hash_val += (gint) keyval->values.clip_x_origin;
254 if (keyval->mask & GDK_GC_CLIP_Y_ORIGIN)
256 hash_val += (gint) keyval->values.clip_y_origin;
258 if (keyval->mask & GDK_GC_EXPOSURES)
260 hash_val += (gint) keyval->values.graphics_exposures;
262 if (keyval->mask & GDK_GC_LINE_WIDTH)
264 hash_val += (gint) keyval->values.line_width;
266 if (keyval->mask & GDK_GC_LINE_STYLE)
268 hash_val += (gint) keyval->values.line_style;
270 if (keyval->mask & GDK_GC_CAP_STYLE)
272 hash_val += (gint) keyval->values.cap_style;
274 if (keyval->mask & GDK_GC_JOIN_STYLE)
276 hash_val += (gint) keyval->values.join_style;
283 gtk_gc_value_hash (gpointer value)
285 return (gulong) value;
289 gtk_gc_key_equal (gpointer a,
294 GdkGCValues *avalues;
295 GdkGCValues *bvalues;
300 avalues = &akey->values;
301 bvalues = &bkey->values;
303 if (akey->mask != bkey->mask)
306 if (akey->depth != bkey->depth)
309 if (akey->colormap != bkey->colormap)
312 if (akey->mask & GDK_GC_FOREGROUND)
314 if (avalues->foreground.pixel != bvalues->foreground.pixel)
317 if (akey->mask & GDK_GC_BACKGROUND)
319 if (avalues->background.pixel != bvalues->background.pixel)
322 if (akey->mask & GDK_GC_FONT)
324 if (!gdk_font_equal (avalues->font, bvalues->font))
327 if (akey->mask & GDK_GC_FUNCTION)
329 if (avalues->function != bvalues->function)
332 if (akey->mask & GDK_GC_FILL)
334 if (avalues->fill != bvalues->fill)
337 if (akey->mask & GDK_GC_TILE)
339 if (avalues->tile != bvalues->tile)
342 if (akey->mask & GDK_GC_STIPPLE)
344 if (avalues->stipple != bvalues->stipple)
347 if (akey->mask & GDK_GC_CLIP_MASK)
349 if (avalues->clip_mask != bvalues->clip_mask)
352 if (akey->mask & GDK_GC_SUBWINDOW)
354 if (avalues->subwindow_mode != bvalues->subwindow_mode)
357 if (akey->mask & GDK_GC_TS_X_ORIGIN)
359 if (avalues->ts_x_origin != bvalues->ts_x_origin)
362 if (akey->mask & GDK_GC_TS_Y_ORIGIN)
364 if (avalues->ts_y_origin != bvalues->ts_y_origin)
367 if (akey->mask & GDK_GC_CLIP_X_ORIGIN)
369 if (avalues->clip_x_origin != bvalues->clip_x_origin)
372 if (akey->mask & GDK_GC_CLIP_Y_ORIGIN)
374 if (avalues->clip_y_origin != bvalues->clip_y_origin)
377 if (akey->mask & GDK_GC_EXPOSURES)
379 if (avalues->graphics_exposures != bvalues->graphics_exposures)
382 if (akey->mask & GDK_GC_LINE_WIDTH)
384 if (avalues->line_width != bvalues->line_width)
387 if (akey->mask & GDK_GC_LINE_STYLE)
389 if (avalues->line_style != bvalues->line_style)
392 if (akey->mask & GDK_GC_CAP_STYLE)
394 if (avalues->cap_style != bvalues->cap_style)
397 if (akey->mask & GDK_GC_JOIN_STYLE)
399 if (avalues->join_style != bvalues->join_style)
407 gtk_gc_drawable_hash (GtkGCDrawable *d)
413 gtk_gc_drawable_equal (GtkGCDrawable *a,
416 return (a->depth == b->depth);
420 #include "gtkaliasdef.c"