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