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.
22 typedef struct _GtkGCKey GtkGCKey;
23 typedef struct _GtkGCDrawable GtkGCDrawable;
28 GdkColormap *colormap;
40 static void gtk_gc_init (void);
41 static GtkGCKey* gtk_gc_key_dup (GtkGCKey *key);
42 static void gtk_gc_key_destroy (GtkGCKey *key);
43 static gpointer gtk_gc_new (gpointer key);
44 static void gtk_gc_destroy (gpointer value);
45 static guint gtk_gc_key_hash (gpointer key);
46 static guint gtk_gc_value_hash (gpointer value);
47 static gint gtk_gc_key_compare (gpointer a,
49 static guint gtk_gc_drawable_hash (GtkGCDrawable *d);
50 static gint gtk_gc_drawable_compare (GtkGCDrawable *a,
54 static gint initialize = TRUE;
55 static GCache *gc_cache = NULL;
56 static GHashTable *gc_drawable_ht = NULL;
58 static GMemChunk *key_mem_chunk = NULL;
62 gtk_gc_get (gint depth,
63 GdkColormap *colormap,
65 GdkGCValuesMask values_mask)
74 key.colormap = colormap;
76 key.mask = values_mask;
78 gc = g_cache_insert (gc_cache, &key);
84 gtk_gc_release (GdkGC *gc)
89 g_cache_remove (gc_cache, gc);
98 gc_cache = g_cache_new ((GCacheNewFunc) gtk_gc_new,
99 (GCacheDestroyFunc) gtk_gc_destroy,
100 (GCacheDupFunc) gtk_gc_key_dup,
101 (GCacheDestroyFunc) gtk_gc_key_destroy,
102 (GHashFunc) gtk_gc_key_hash,
103 (GHashFunc) gtk_gc_value_hash,
104 (GCompareFunc) gtk_gc_key_compare);
106 gc_drawable_ht = g_hash_table_new ((GHashFunc) gtk_gc_drawable_hash,
107 (GCompareFunc) gtk_gc_drawable_compare);
111 gtk_gc_key_dup (GtkGCKey *key)
116 key_mem_chunk = g_mem_chunk_new ("key mem chunk", sizeof (GtkGCKey),
117 1024, G_ALLOC_AND_FREE);
119 new_key = g_chunk_new (GtkGCKey, key_mem_chunk);
127 gtk_gc_key_destroy (GtkGCKey *key)
129 g_mem_chunk_free (key_mem_chunk, key);
133 gtk_gc_new (gpointer key)
136 GtkGCDrawable *drawable;
141 drawable = g_hash_table_lookup (gc_drawable_ht, &keyval->depth);
144 drawable = g_new (GtkGCDrawable, 1);
145 drawable->depth = keyval->depth;
146 drawable->drawable = gdk_pixmap_new (NULL, 1, 1, drawable->depth);
148 g_hash_table_insert (gc_drawable_ht, &drawable->depth, drawable);
151 gc = gdk_gc_new_with_values (drawable->drawable, &keyval->values, keyval->mask);
153 return (gpointer) gc;
157 gtk_gc_destroy (gpointer value)
159 gdk_gc_destroy ((GdkGC*) value);
163 gtk_gc_key_hash (gpointer key)
171 if (keyval->mask & GDK_GC_FOREGROUND)
173 hash_val += keyval->values.foreground.pixel;
175 if (keyval->mask & GDK_GC_BACKGROUND)
177 hash_val += keyval->values.background.pixel;
179 if (keyval->mask & GDK_GC_FONT)
181 hash_val += gdk_font_id (keyval->values.font);
183 if (keyval->mask & GDK_GC_FUNCTION)
185 hash_val += (gint) keyval->values.function;
187 if (keyval->mask & GDK_GC_FILL)
189 hash_val += (gint) keyval->values.fill;
191 if (keyval->mask & GDK_GC_TILE)
193 hash_val += (glong) keyval->values.tile;
195 if (keyval->mask & GDK_GC_STIPPLE)
197 hash_val += (glong) keyval->values.stipple;
199 if (keyval->mask & GDK_GC_CLIP_MASK)
201 hash_val += (glong) keyval->values.clip_mask;
203 if (keyval->mask & GDK_GC_SUBWINDOW)
205 hash_val += (gint) keyval->values.subwindow_mode;
207 if (keyval->mask & GDK_GC_TS_X_ORIGIN)
209 hash_val += (gint) keyval->values.ts_x_origin;
211 if (keyval->mask & GDK_GC_TS_Y_ORIGIN)
213 hash_val += (gint) keyval->values.ts_y_origin;
215 if (keyval->mask & GDK_GC_CLIP_X_ORIGIN)
217 hash_val += (gint) keyval->values.clip_x_origin;
219 if (keyval->mask & GDK_GC_CLIP_Y_ORIGIN)
221 hash_val += (gint) keyval->values.clip_y_origin;
223 if (keyval->mask & GDK_GC_EXPOSURES)
225 hash_val += (gint) keyval->values.graphics_exposures;
227 if (keyval->mask & GDK_GC_LINE_WIDTH)
229 hash_val += (gint) keyval->values.line_width;
231 if (keyval->mask & GDK_GC_LINE_STYLE)
233 hash_val += (gint) keyval->values.line_style;
235 if (keyval->mask & GDK_GC_CAP_STYLE)
237 hash_val += (gint) keyval->values.cap_style;
239 if (keyval->mask & GDK_GC_JOIN_STYLE)
241 hash_val += (gint) keyval->values.join_style;
248 gtk_gc_value_hash (gpointer value)
250 return (gulong) value;
254 gtk_gc_key_compare (gpointer a,
259 GdkGCValues *avalues;
260 GdkGCValues *bvalues;
265 avalues = &akey->values;
266 bvalues = &bkey->values;
268 if (akey->mask != bkey->mask)
271 if (akey->depth != bkey->depth)
274 if (akey->colormap != bkey->colormap)
277 if (akey->mask & GDK_GC_FOREGROUND)
279 if (avalues->foreground.pixel != bvalues->foreground.pixel)
282 if (akey->mask & GDK_GC_BACKGROUND)
284 if (avalues->background.pixel != bvalues->background.pixel)
287 if (akey->mask & GDK_GC_FONT)
289 if (!gdk_font_equal (avalues->font, bvalues->font))
292 if (akey->mask & GDK_GC_FUNCTION)
294 if (avalues->function != bvalues->function)
297 if (akey->mask & GDK_GC_FILL)
299 if (avalues->fill != bvalues->fill)
302 if (akey->mask & GDK_GC_TILE)
304 if (avalues->tile != bvalues->tile)
307 if (akey->mask & GDK_GC_STIPPLE)
309 if (avalues->stipple != bvalues->stipple)
312 if (akey->mask & GDK_GC_CLIP_MASK)
314 if (avalues->clip_mask != bvalues->clip_mask)
317 if (akey->mask & GDK_GC_SUBWINDOW)
319 if (avalues->subwindow_mode != bvalues->subwindow_mode)
322 if (akey->mask & GDK_GC_TS_X_ORIGIN)
324 if (avalues->ts_x_origin != bvalues->ts_x_origin)
327 if (akey->mask & GDK_GC_TS_Y_ORIGIN)
329 if (avalues->ts_y_origin != bvalues->ts_y_origin)
332 if (akey->mask & GDK_GC_CLIP_X_ORIGIN)
334 if (avalues->clip_x_origin != bvalues->clip_x_origin)
337 if (akey->mask & GDK_GC_CLIP_Y_ORIGIN)
339 if (avalues->clip_y_origin != bvalues->clip_y_origin)
342 if (akey->mask & GDK_GC_EXPOSURES)
344 if (avalues->graphics_exposures != bvalues->graphics_exposures)
347 if (akey->mask & GDK_GC_LINE_WIDTH)
349 if (avalues->line_width != bvalues->line_width)
352 if (akey->mask & GDK_GC_LINE_STYLE)
354 if (avalues->line_style != bvalues->line_style)
357 if (akey->mask & GDK_GC_CAP_STYLE)
359 if (avalues->cap_style != bvalues->cap_style)
362 if (akey->mask & GDK_GC_JOIN_STYLE)
364 if (avalues->join_style != bvalues->join_style)
373 gtk_gc_drawable_hash (GtkGCDrawable *d)
379 gtk_gc_drawable_compare (GtkGCDrawable *a,
382 return (a->depth == b->depth);