]> Pileus Git - ~andy/gtk/blob - gtk/gtkgc.c
Intern some more strings.
[~andy/gtk] / gtk / gtkgc.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include <config.h>
28 #include "gtkgc.h"
29 #include "gtkintl.h"
30 #include "gtkalias.h"
31
32
33 typedef struct _GtkGCKey       GtkGCKey;
34 typedef struct _GtkGCDrawable  GtkGCDrawable;
35
36 struct _GtkGCKey
37 {
38   gint depth;
39   GdkColormap *colormap;
40   GdkGCValues values;
41   GdkGCValuesMask mask;
42 };
43
44 struct _GtkGCDrawable
45 {
46   gint depth;
47   GdkPixmap *drawable;
48 };
49
50
51 static void      gtk_gc_init             (void);
52 static GtkGCKey* gtk_gc_key_dup          (GtkGCKey      *key);
53 static void      gtk_gc_key_destroy      (GtkGCKey      *key);
54 static gpointer  gtk_gc_new              (gpointer       key);
55 static void      gtk_gc_destroy          (gpointer       value);
56 static guint     gtk_gc_key_hash         (gpointer       key);
57 static guint     gtk_gc_value_hash       (gpointer       value);
58 static gint      gtk_gc_key_equal        (gpointer       a,
59                                           gpointer       b);
60 static guint     gtk_gc_drawable_hash    (GtkGCDrawable *d);
61 static gint      gtk_gc_drawable_equal   (GtkGCDrawable *a,
62                                           GtkGCDrawable *b);
63
64
65 static gint initialize = TRUE;
66 static GCache *gc_cache = NULL;
67
68 static GMemChunk *key_mem_chunk = NULL;
69
70
71 GdkGC*
72 gtk_gc_get (gint             depth,
73             GdkColormap     *colormap,
74             GdkGCValues     *values,
75             GdkGCValuesMask  values_mask)
76 {
77   GtkGCKey key;
78   GdkGC *gc;
79
80   if (initialize)
81     gtk_gc_init ();
82
83   key.depth = depth;
84   key.colormap = colormap;
85   key.values = *values;
86   key.mask = values_mask;
87
88   gc = g_cache_insert (gc_cache, &key);
89
90   return gc;
91 }
92
93 void
94 gtk_gc_release (GdkGC *gc)
95 {
96   if (initialize)
97     gtk_gc_init ();
98
99   g_cache_remove (gc_cache, gc);
100 }
101
102 static void 
103 free_gc_drawable (gpointer data)
104 {
105   GtkGCDrawable *drawable = data;
106   g_object_unref (drawable->drawable);
107   g_free (drawable);
108 }
109
110 static GHashTable*
111 gtk_gc_get_drawable_ht (GdkScreen *screen)
112 {
113   GHashTable *ht = g_object_get_data (G_OBJECT (screen), "gtk-gc-drawable-ht");
114   if (!ht)
115     {
116       ht = g_hash_table_new_full ((GHashFunc) gtk_gc_drawable_hash,
117                                   (GEqualFunc) gtk_gc_drawable_equal,
118                                   NULL, free_gc_drawable);
119       g_object_set_data_full (G_OBJECT (screen), 
120                               I_("gtk-gc-drawable-ht"), ht, 
121                               (GDestroyNotify)g_hash_table_destroy);
122     }
123   
124   return ht;
125 }
126
127 static void
128 gtk_gc_init (void)
129 {
130   initialize = FALSE;
131
132   gc_cache = g_cache_new ((GCacheNewFunc) gtk_gc_new,
133                           (GCacheDestroyFunc) gtk_gc_destroy,
134                           (GCacheDupFunc) gtk_gc_key_dup,
135                           (GCacheDestroyFunc) gtk_gc_key_destroy,
136                           (GHashFunc) gtk_gc_key_hash,
137                           (GHashFunc) gtk_gc_value_hash,
138                           (GEqualFunc) gtk_gc_key_equal);
139 }
140
141 static GtkGCKey*
142 gtk_gc_key_dup (GtkGCKey *key)
143 {
144   GtkGCKey *new_key;
145
146   if (!key_mem_chunk)
147     key_mem_chunk = g_mem_chunk_new ("key mem chunk", sizeof (GtkGCKey),
148                                      1024, G_ALLOC_AND_FREE);
149
150   new_key = g_chunk_new (GtkGCKey, key_mem_chunk);
151
152   *new_key = *key;
153
154   return new_key;
155 }
156
157 static void
158 gtk_gc_key_destroy (GtkGCKey *key)
159 {
160   g_mem_chunk_free (key_mem_chunk, key);
161 }
162
163 static gpointer
164 gtk_gc_new (gpointer key)
165 {
166   GtkGCKey *keyval;
167   GtkGCDrawable *drawable;
168   GdkGC *gc;
169   GHashTable *ht;
170   GdkScreen *screen;
171
172   keyval = key;
173   screen = gdk_colormap_get_screen (keyval->colormap);
174   
175   ht = gtk_gc_get_drawable_ht (screen);
176   drawable = g_hash_table_lookup (ht, &keyval->depth);
177   if (!drawable)
178     {
179       drawable = g_new (GtkGCDrawable, 1);
180       drawable->depth = keyval->depth;
181       drawable->drawable = gdk_pixmap_new (gdk_screen_get_root_window (screen), 
182                                            1, 1, drawable->depth);
183       g_hash_table_insert (ht, &drawable->depth, drawable);
184     }
185
186   gc = gdk_gc_new_with_values (drawable->drawable, &keyval->values, keyval->mask);
187   gdk_gc_set_colormap (gc, keyval->colormap);
188
189   return (gpointer) gc;
190 }
191
192 static void
193 gtk_gc_destroy (gpointer value)
194 {
195   g_object_unref (value);
196 }
197
198 static guint
199 gtk_gc_key_hash (gpointer key)
200 {
201   GtkGCKey *keyval;
202   guint hash_val;
203
204   keyval = key;
205   hash_val = 0;
206
207   if (keyval->mask & GDK_GC_FOREGROUND)
208     {
209       hash_val += keyval->values.foreground.pixel;
210     }
211   if (keyval->mask & GDK_GC_BACKGROUND)
212     {
213       hash_val += keyval->values.background.pixel;
214     }
215   if (keyval->mask & GDK_GC_FONT)
216     {
217       hash_val += gdk_font_id (keyval->values.font);
218     }
219   if (keyval->mask & GDK_GC_FUNCTION)
220     {
221       hash_val += (gint) keyval->values.function;
222     }
223   if (keyval->mask & GDK_GC_FILL)
224     {
225       hash_val += (gint) keyval->values.fill;
226     }
227   if (keyval->mask & GDK_GC_TILE)
228     {
229       hash_val += (glong) keyval->values.tile;
230     }
231   if (keyval->mask & GDK_GC_STIPPLE)
232     {
233       hash_val += (glong) keyval->values.stipple;
234     }
235   if (keyval->mask & GDK_GC_CLIP_MASK)
236     {
237       hash_val += (glong) keyval->values.clip_mask;
238     }
239   if (keyval->mask & GDK_GC_SUBWINDOW)
240     {
241       hash_val += (gint) keyval->values.subwindow_mode;
242     }
243   if (keyval->mask & GDK_GC_TS_X_ORIGIN)
244     {
245       hash_val += (gint) keyval->values.ts_x_origin;
246     }
247   if (keyval->mask & GDK_GC_TS_Y_ORIGIN)
248     {
249       hash_val += (gint) keyval->values.ts_y_origin;
250     }
251   if (keyval->mask & GDK_GC_CLIP_X_ORIGIN)
252     {
253       hash_val += (gint) keyval->values.clip_x_origin;
254     }
255   if (keyval->mask & GDK_GC_CLIP_Y_ORIGIN)
256     {
257       hash_val += (gint) keyval->values.clip_y_origin;
258     }
259   if (keyval->mask & GDK_GC_EXPOSURES)
260     {
261       hash_val += (gint) keyval->values.graphics_exposures;
262     }
263   if (keyval->mask & GDK_GC_LINE_WIDTH)
264     {
265       hash_val += (gint) keyval->values.line_width;
266     }
267   if (keyval->mask & GDK_GC_LINE_STYLE)
268     {
269       hash_val += (gint) keyval->values.line_style;
270     }
271   if (keyval->mask & GDK_GC_CAP_STYLE)
272     {
273       hash_val += (gint) keyval->values.cap_style;
274     }
275   if (keyval->mask & GDK_GC_JOIN_STYLE)
276     {
277       hash_val += (gint) keyval->values.join_style;
278     }
279
280   return hash_val;
281 }
282
283 static guint
284 gtk_gc_value_hash (gpointer value)
285 {
286   return (gulong) value;
287 }
288
289 static gboolean
290 gtk_gc_key_equal (gpointer a,
291                   gpointer b)
292 {
293   GtkGCKey *akey;
294   GtkGCKey *bkey;
295   GdkGCValues *avalues;
296   GdkGCValues *bvalues;
297
298   akey = a;
299   bkey = b;
300
301   avalues = &akey->values;
302   bvalues = &bkey->values;
303
304   if (akey->mask != bkey->mask)
305     return FALSE;
306
307   if (akey->depth != bkey->depth)
308     return FALSE;
309
310   if (akey->colormap != bkey->colormap)
311     return FALSE;
312
313   if (akey->mask & GDK_GC_FOREGROUND)
314     {
315       if (avalues->foreground.pixel != bvalues->foreground.pixel)
316         return FALSE;
317     }
318   if (akey->mask & GDK_GC_BACKGROUND)
319     {
320       if (avalues->background.pixel != bvalues->background.pixel)
321         return FALSE;
322     }
323   if (akey->mask & GDK_GC_FONT)
324     {
325       if (!gdk_font_equal (avalues->font, bvalues->font))
326         return FALSE;
327     }
328   if (akey->mask & GDK_GC_FUNCTION)
329     {
330       if (avalues->function != bvalues->function)
331         return FALSE;
332     }
333   if (akey->mask & GDK_GC_FILL)
334     {
335       if (avalues->fill != bvalues->fill)
336         return FALSE;
337     }
338   if (akey->mask & GDK_GC_TILE)
339     {
340       if (avalues->tile != bvalues->tile)
341         return FALSE;
342     }
343   if (akey->mask & GDK_GC_STIPPLE)
344     {
345       if (avalues->stipple != bvalues->stipple)
346         return FALSE;
347     }
348   if (akey->mask & GDK_GC_CLIP_MASK)
349     {
350       if (avalues->clip_mask != bvalues->clip_mask)
351         return FALSE;
352     }
353   if (akey->mask & GDK_GC_SUBWINDOW)
354     {
355       if (avalues->subwindow_mode != bvalues->subwindow_mode)
356         return FALSE;
357     }
358   if (akey->mask & GDK_GC_TS_X_ORIGIN)
359     {
360       if (avalues->ts_x_origin != bvalues->ts_x_origin)
361         return FALSE;
362     }
363   if (akey->mask & GDK_GC_TS_Y_ORIGIN)
364     {
365       if (avalues->ts_y_origin != bvalues->ts_y_origin)
366         return FALSE;
367     }
368   if (akey->mask & GDK_GC_CLIP_X_ORIGIN)
369     {
370       if (avalues->clip_x_origin != bvalues->clip_x_origin)
371         return FALSE;
372     }
373   if (akey->mask & GDK_GC_CLIP_Y_ORIGIN)
374     {
375       if (avalues->clip_y_origin != bvalues->clip_y_origin)
376         return FALSE;
377     }
378   if (akey->mask & GDK_GC_EXPOSURES)
379     {
380       if (avalues->graphics_exposures != bvalues->graphics_exposures)
381         return FALSE;
382     }
383   if (akey->mask & GDK_GC_LINE_WIDTH)
384     {
385       if (avalues->line_width != bvalues->line_width)
386         return FALSE;
387     }
388   if (akey->mask & GDK_GC_LINE_STYLE)
389     {
390       if (avalues->line_style != bvalues->line_style)
391         return FALSE;
392     }
393   if (akey->mask & GDK_GC_CAP_STYLE)
394     {
395       if (avalues->cap_style != bvalues->cap_style)
396         return FALSE;
397     }
398   if (akey->mask & GDK_GC_JOIN_STYLE)
399     {
400       if (avalues->join_style != bvalues->join_style)
401         return FALSE;
402     }
403
404   return TRUE;
405 }
406
407 static guint
408 gtk_gc_drawable_hash (GtkGCDrawable *d)
409 {
410   return d->depth;
411 }
412
413 static gboolean
414 gtk_gc_drawable_equal (GtkGCDrawable *a,
415                        GtkGCDrawable *b)
416 {
417   return (a->depth == b->depth);
418 }
419
420 #define __GTK_GC_C__
421 #include "gtkaliasdef.c"