]> Pileus Git - ~andy/gtk/blob - gtk/gtkgc.c
Add gdk_rgb_find_color() to get a pixel value using GdkRGB functionality
[~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 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.
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  * Library General Public License for more details.
13  *
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.
18  */
19
20 /*
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/. 
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_compare      (gpointer       a,
56                                           gpointer       b);
57 static guint     gtk_gc_drawable_hash    (GtkGCDrawable *d);
58 static gint      gtk_gc_drawable_compare (GtkGCDrawable *a,
59                                           GtkGCDrawable *b);
60
61
62 static gint initialize = TRUE;
63 static GCache *gc_cache = NULL;
64 static GHashTable *gc_drawable_ht = NULL;
65
66 static GMemChunk *key_mem_chunk = NULL;
67
68
69 GdkGC*
70 gtk_gc_get (gint             depth,
71             GdkColormap     *colormap,
72             GdkGCValues     *values,
73             GdkGCValuesMask  values_mask)
74 {
75   GtkGCKey key;
76   GdkGC *gc;
77
78   if (initialize)
79     gtk_gc_init ();
80
81   key.depth = depth;
82   key.colormap = colormap;
83   key.values = *values;
84   key.mask = values_mask;
85
86   gc = g_cache_insert (gc_cache, &key);
87
88   return gc;
89 }
90
91 void
92 gtk_gc_release (GdkGC *gc)
93 {
94   if (initialize)
95     gtk_gc_init ();
96
97   g_cache_remove (gc_cache, gc);
98 }
99
100
101 static void
102 gtk_gc_init (void)
103 {
104   initialize = FALSE;
105
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);
113
114   gc_drawable_ht = g_hash_table_new ((GHashFunc) gtk_gc_drawable_hash,
115                                      (GCompareFunc) gtk_gc_drawable_compare);
116 }
117
118 static GtkGCKey*
119 gtk_gc_key_dup (GtkGCKey *key)
120 {
121   GtkGCKey *new_key;
122
123   if (!key_mem_chunk)
124     key_mem_chunk = g_mem_chunk_new ("key mem chunk", sizeof (GtkGCKey),
125                                      1024, G_ALLOC_AND_FREE);
126
127   new_key = g_chunk_new (GtkGCKey, key_mem_chunk);
128
129   *new_key = *key;
130
131   return new_key;
132 }
133
134 static void
135 gtk_gc_key_destroy (GtkGCKey *key)
136 {
137   g_mem_chunk_free (key_mem_chunk, key);
138 }
139
140 static gpointer
141 gtk_gc_new (gpointer key)
142 {
143   GtkGCKey *keyval;
144   GtkGCDrawable *drawable;
145   GdkGC *gc;
146
147   keyval = key;
148
149   drawable = g_hash_table_lookup (gc_drawable_ht, &keyval->depth);
150   if (!drawable)
151     {
152       drawable = g_new (GtkGCDrawable, 1);
153       drawable->depth = keyval->depth;
154       drawable->drawable = gdk_pixmap_new (NULL, 1, 1, drawable->depth);
155
156       g_hash_table_insert (gc_drawable_ht, &drawable->depth, drawable);
157     }
158
159   gc = gdk_gc_new_with_values (drawable->drawable, &keyval->values, keyval->mask);
160   gdk_gc_set_colormap (gc, keyval->colormap);
161
162   return (gpointer) gc;
163 }
164
165 static void
166 gtk_gc_destroy (gpointer value)
167 {
168   gdk_gc_destroy ((GdkGC*) value);
169 }
170
171 static guint
172 gtk_gc_key_hash (gpointer key)
173 {
174   GtkGCKey *keyval;
175   guint hash_val;
176
177   keyval = key;
178   hash_val = 0;
179
180   if (keyval->mask & GDK_GC_FOREGROUND)
181     {
182       hash_val += keyval->values.foreground.pixel;
183     }
184   if (keyval->mask & GDK_GC_BACKGROUND)
185     {
186       hash_val += keyval->values.background.pixel;
187     }
188   if (keyval->mask & GDK_GC_FONT)
189     {
190       hash_val += gdk_font_id (keyval->values.font);
191     }
192   if (keyval->mask & GDK_GC_FUNCTION)
193     {
194       hash_val += (gint) keyval->values.function;
195     }
196   if (keyval->mask & GDK_GC_FILL)
197     {
198       hash_val += (gint) keyval->values.fill;
199     }
200   if (keyval->mask & GDK_GC_TILE)
201     {
202       hash_val += (glong) keyval->values.tile;
203     }
204   if (keyval->mask & GDK_GC_STIPPLE)
205     {
206       hash_val += (glong) keyval->values.stipple;
207     }
208   if (keyval->mask & GDK_GC_CLIP_MASK)
209     {
210       hash_val += (glong) keyval->values.clip_mask;
211     }
212   if (keyval->mask & GDK_GC_SUBWINDOW)
213     {
214       hash_val += (gint) keyval->values.subwindow_mode;
215     }
216   if (keyval->mask & GDK_GC_TS_X_ORIGIN)
217     {
218       hash_val += (gint) keyval->values.ts_x_origin;
219     }
220   if (keyval->mask & GDK_GC_TS_Y_ORIGIN)
221     {
222       hash_val += (gint) keyval->values.ts_y_origin;
223     }
224   if (keyval->mask & GDK_GC_CLIP_X_ORIGIN)
225     {
226       hash_val += (gint) keyval->values.clip_x_origin;
227     }
228   if (keyval->mask & GDK_GC_CLIP_Y_ORIGIN)
229     {
230       hash_val += (gint) keyval->values.clip_y_origin;
231     }
232   if (keyval->mask & GDK_GC_EXPOSURES)
233     {
234       hash_val += (gint) keyval->values.graphics_exposures;
235     }
236   if (keyval->mask & GDK_GC_LINE_WIDTH)
237     {
238       hash_val += (gint) keyval->values.line_width;
239     }
240   if (keyval->mask & GDK_GC_LINE_STYLE)
241     {
242       hash_val += (gint) keyval->values.line_style;
243     }
244   if (keyval->mask & GDK_GC_CAP_STYLE)
245     {
246       hash_val += (gint) keyval->values.cap_style;
247     }
248   if (keyval->mask & GDK_GC_JOIN_STYLE)
249     {
250       hash_val += (gint) keyval->values.join_style;
251     }
252
253   return hash_val;
254 }
255
256 static guint
257 gtk_gc_value_hash (gpointer value)
258 {
259   return (gulong) value;
260 }
261
262 static gint
263 gtk_gc_key_compare (gpointer a,
264                     gpointer b)
265 {
266   GtkGCKey *akey;
267   GtkGCKey *bkey;
268   GdkGCValues *avalues;
269   GdkGCValues *bvalues;
270
271   akey = a;
272   bkey = b;
273
274   avalues = &akey->values;
275   bvalues = &bkey->values;
276
277   if (akey->mask != bkey->mask)
278     return FALSE;
279
280   if (akey->depth != bkey->depth)
281     return FALSE;
282
283   if (akey->colormap != bkey->colormap)
284     return FALSE;
285
286   if (akey->mask & GDK_GC_FOREGROUND)
287     {
288       if (avalues->foreground.pixel != bvalues->foreground.pixel)
289         return FALSE;
290     }
291   if (akey->mask & GDK_GC_BACKGROUND)
292     {
293       if (avalues->background.pixel != bvalues->background.pixel)
294         return FALSE;
295     }
296   if (akey->mask & GDK_GC_FONT)
297     {
298       if (!gdk_font_equal (avalues->font, bvalues->font))
299         return FALSE;
300     }
301   if (akey->mask & GDK_GC_FUNCTION)
302     {
303       if (avalues->function != bvalues->function)
304         return FALSE;
305     }
306   if (akey->mask & GDK_GC_FILL)
307     {
308       if (avalues->fill != bvalues->fill)
309         return FALSE;
310     }
311   if (akey->mask & GDK_GC_TILE)
312     {
313       if (avalues->tile != bvalues->tile)
314         return FALSE;
315     }
316   if (akey->mask & GDK_GC_STIPPLE)
317     {
318       if (avalues->stipple != bvalues->stipple)
319         return FALSE;
320     }
321   if (akey->mask & GDK_GC_CLIP_MASK)
322     {
323       if (avalues->clip_mask != bvalues->clip_mask)
324         return FALSE;
325     }
326   if (akey->mask & GDK_GC_SUBWINDOW)
327     {
328       if (avalues->subwindow_mode != bvalues->subwindow_mode)
329         return FALSE;
330     }
331   if (akey->mask & GDK_GC_TS_X_ORIGIN)
332     {
333       if (avalues->ts_x_origin != bvalues->ts_x_origin)
334         return FALSE;
335     }
336   if (akey->mask & GDK_GC_TS_Y_ORIGIN)
337     {
338       if (avalues->ts_y_origin != bvalues->ts_y_origin)
339         return FALSE;
340     }
341   if (akey->mask & GDK_GC_CLIP_X_ORIGIN)
342     {
343       if (avalues->clip_x_origin != bvalues->clip_x_origin)
344         return FALSE;
345     }
346   if (akey->mask & GDK_GC_CLIP_Y_ORIGIN)
347     {
348       if (avalues->clip_y_origin != bvalues->clip_y_origin)
349         return FALSE;
350     }
351   if (akey->mask & GDK_GC_EXPOSURES)
352     {
353       if (avalues->graphics_exposures != bvalues->graphics_exposures)
354         return FALSE;
355     }
356   if (akey->mask & GDK_GC_LINE_WIDTH)
357     {
358       if (avalues->line_width != bvalues->line_width)
359         return FALSE;
360     }
361   if (akey->mask & GDK_GC_LINE_STYLE)
362     {
363       if (avalues->line_style != bvalues->line_style)
364         return FALSE;
365     }
366   if (akey->mask & GDK_GC_CAP_STYLE)
367     {
368       if (avalues->cap_style != bvalues->cap_style)
369         return FALSE;
370     }
371   if (akey->mask & GDK_GC_JOIN_STYLE)
372     {
373       if (avalues->join_style != bvalues->join_style)
374         return FALSE;
375     }
376
377   return TRUE;
378 }
379
380
381 static guint
382 gtk_gc_drawable_hash (GtkGCDrawable *d)
383 {
384   return d->depth;
385 }
386
387 static gint
388 gtk_gc_drawable_compare (GtkGCDrawable *a,
389                          GtkGCDrawable *b)
390 {
391   return (a->depth == b->depth);
392 }