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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
30 typedef struct _GtkGCKey GtkGCKey;
31 typedef struct _GtkGCDrawable GtkGCDrawable;
36 GdkColormap *colormap;
48 static void gtk_gc_init (void);
49 static GtkGCKey* gtk_gc_key_dup (GtkGCKey *key);
50 static void gtk_gc_key_destroy (GtkGCKey *key);
51 static gpointer gtk_gc_new (gpointer key);
52 static void gtk_gc_destroy (gpointer value);
53 static guint gtk_gc_key_hash (gpointer key);
54 static guint gtk_gc_value_hash (gpointer value);
55 static gint gtk_gc_key_compare (gpointer a,
57 static guint gtk_gc_drawable_hash (GtkGCDrawable *d);
58 static gint gtk_gc_drawable_compare (GtkGCDrawable *a,
62 static gint initialize = TRUE;
63 static GCache *gc_cache = NULL;
64 static GHashTable *gc_drawable_ht = NULL;
66 static GMemChunk *key_mem_chunk = NULL;
70 gtk_gc_get (gint depth,
71 GdkColormap *colormap,
73 GdkGCValuesMask values_mask)
82 key.colormap = colormap;
84 key.mask = values_mask;
86 gc = g_cache_insert (gc_cache, &key);
92 gtk_gc_release (GdkGC *gc)
97 g_cache_remove (gc_cache, gc);
106 gc_cache = g_cache_new ((GCacheNewFunc) gtk_gc_new,
107 (GCacheDestroyFunc) gtk_gc_destroy,
108 (GCacheDupFunc) gtk_gc_key_dup,
109 (GCacheDestroyFunc) gtk_gc_key_destroy,
110 (GHashFunc) gtk_gc_key_hash,
111 (GHashFunc) gtk_gc_value_hash,
112 (GCompareFunc) gtk_gc_key_compare);
114 gc_drawable_ht = g_hash_table_new ((GHashFunc) gtk_gc_drawable_hash,
115 (GCompareFunc) gtk_gc_drawable_compare);
119 gtk_gc_key_dup (GtkGCKey *key)
124 key_mem_chunk = g_mem_chunk_new ("key mem chunk", sizeof (GtkGCKey),
125 1024, G_ALLOC_AND_FREE);
127 new_key = g_chunk_new (GtkGCKey, key_mem_chunk);
135 gtk_gc_key_destroy (GtkGCKey *key)
137 g_mem_chunk_free (key_mem_chunk, key);
141 gtk_gc_new (gpointer key)
144 GtkGCDrawable *drawable;
149 drawable = g_hash_table_lookup (gc_drawable_ht, &keyval->depth);
152 drawable = g_new (GtkGCDrawable, 1);
153 drawable->depth = keyval->depth;
154 drawable->drawable = gdk_pixmap_new (NULL, 1, 1, drawable->depth);
156 g_hash_table_insert (gc_drawable_ht, &drawable->depth, drawable);
159 gc = gdk_gc_new_with_values (drawable->drawable, &keyval->values, keyval->mask);
160 gdk_gc_set_colormap (gc, keyval->colormap);
162 return (gpointer) gc;
166 gtk_gc_destroy (gpointer value)
168 gdk_gc_destroy ((GdkGC*) value);
172 gtk_gc_key_hash (gpointer key)
180 if (keyval->mask & GDK_GC_FOREGROUND)
182 hash_val += keyval->values.foreground.pixel;
184 if (keyval->mask & GDK_GC_BACKGROUND)
186 hash_val += keyval->values.background.pixel;
188 if (keyval->mask & GDK_GC_FONT)
190 hash_val += gdk_font_id (keyval->values.font);
192 if (keyval->mask & GDK_GC_FUNCTION)
194 hash_val += (gint) keyval->values.function;
196 if (keyval->mask & GDK_GC_FILL)
198 hash_val += (gint) keyval->values.fill;
200 if (keyval->mask & GDK_GC_TILE)
202 hash_val += (glong) keyval->values.tile;
204 if (keyval->mask & GDK_GC_STIPPLE)
206 hash_val += (glong) keyval->values.stipple;
208 if (keyval->mask & GDK_GC_CLIP_MASK)
210 hash_val += (glong) keyval->values.clip_mask;
212 if (keyval->mask & GDK_GC_SUBWINDOW)
214 hash_val += (gint) keyval->values.subwindow_mode;
216 if (keyval->mask & GDK_GC_TS_X_ORIGIN)
218 hash_val += (gint) keyval->values.ts_x_origin;
220 if (keyval->mask & GDK_GC_TS_Y_ORIGIN)
222 hash_val += (gint) keyval->values.ts_y_origin;
224 if (keyval->mask & GDK_GC_CLIP_X_ORIGIN)
226 hash_val += (gint) keyval->values.clip_x_origin;
228 if (keyval->mask & GDK_GC_CLIP_Y_ORIGIN)
230 hash_val += (gint) keyval->values.clip_y_origin;
232 if (keyval->mask & GDK_GC_EXPOSURES)
234 hash_val += (gint) keyval->values.graphics_exposures;
236 if (keyval->mask & GDK_GC_LINE_WIDTH)
238 hash_val += (gint) keyval->values.line_width;
240 if (keyval->mask & GDK_GC_LINE_STYLE)
242 hash_val += (gint) keyval->values.line_style;
244 if (keyval->mask & GDK_GC_CAP_STYLE)
246 hash_val += (gint) keyval->values.cap_style;
248 if (keyval->mask & GDK_GC_JOIN_STYLE)
250 hash_val += (gint) keyval->values.join_style;
257 gtk_gc_value_hash (gpointer value)
259 return (gulong) value;
263 gtk_gc_key_compare (gpointer a,
268 GdkGCValues *avalues;
269 GdkGCValues *bvalues;
274 avalues = &akey->values;
275 bvalues = &bkey->values;
277 if (akey->mask != bkey->mask)
280 if (akey->depth != bkey->depth)
283 if (akey->colormap != bkey->colormap)
286 if (akey->mask & GDK_GC_FOREGROUND)
288 if (avalues->foreground.pixel != bvalues->foreground.pixel)
291 if (akey->mask & GDK_GC_BACKGROUND)
293 if (avalues->background.pixel != bvalues->background.pixel)
296 if (akey->mask & GDK_GC_FONT)
298 if (!gdk_font_equal (avalues->font, bvalues->font))
301 if (akey->mask & GDK_GC_FUNCTION)
303 if (avalues->function != bvalues->function)
306 if (akey->mask & GDK_GC_FILL)
308 if (avalues->fill != bvalues->fill)
311 if (akey->mask & GDK_GC_TILE)
313 if (avalues->tile != bvalues->tile)
316 if (akey->mask & GDK_GC_STIPPLE)
318 if (avalues->stipple != bvalues->stipple)
321 if (akey->mask & GDK_GC_CLIP_MASK)
323 if (avalues->clip_mask != bvalues->clip_mask)
326 if (akey->mask & GDK_GC_SUBWINDOW)
328 if (avalues->subwindow_mode != bvalues->subwindow_mode)
331 if (akey->mask & GDK_GC_TS_X_ORIGIN)
333 if (avalues->ts_x_origin != bvalues->ts_x_origin)
336 if (akey->mask & GDK_GC_TS_Y_ORIGIN)
338 if (avalues->ts_y_origin != bvalues->ts_y_origin)
341 if (akey->mask & GDK_GC_CLIP_X_ORIGIN)
343 if (avalues->clip_x_origin != bvalues->clip_x_origin)
346 if (akey->mask & GDK_GC_CLIP_Y_ORIGIN)
348 if (avalues->clip_y_origin != bvalues->clip_y_origin)
351 if (akey->mask & GDK_GC_EXPOSURES)
353 if (avalues->graphics_exposures != bvalues->graphics_exposures)
356 if (akey->mask & GDK_GC_LINE_WIDTH)
358 if (avalues->line_width != bvalues->line_width)
361 if (akey->mask & GDK_GC_LINE_STYLE)
363 if (avalues->line_style != bvalues->line_style)
366 if (akey->mask & GDK_GC_CAP_STYLE)
368 if (avalues->cap_style != bvalues->cap_style)
371 if (akey->mask & GDK_GC_JOIN_STYLE)
373 if (avalues->join_style != bvalues->join_style)
382 gtk_gc_drawable_hash (GtkGCDrawable *d)
388 gtk_gc_drawable_compare (GtkGCDrawable *a,
391 return (a->depth == b->depth);