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