]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyleset.c
GtkStyleSet: Add method to register font properties.
[~andy/gtk] / gtk / gtkstyleset.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
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 #include "config.h"
21
22 #include <stdlib.h>
23 #include <gobject/gvaluecollector.h>
24
25 #include "gtkstyleprovider.h"
26 #include "gtkstyleset.h"
27 #include "gtkprivate.h"
28 #include "gtkintl.h"
29
30 #include "gtkalias.h"
31
32 typedef struct GtkStyleSetPrivate GtkStyleSetPrivate;
33 typedef struct PropertyData PropertyData;
34 typedef struct PropertyNode PropertyNode;
35
36 struct PropertyNode
37 {
38   GQuark property_quark;
39   GType property_type;
40   GValue default_value;
41 };
42
43 struct PropertyData
44 {
45   GValue values[GTK_STATE_LAST];
46 };
47
48 struct GtkStyleSetPrivate
49 {
50   GHashTable *properties;
51 };
52
53 static GArray *properties = NULL;
54
55 #define GTK_STYLE_SET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_STYLE_SET, GtkStyleSetPrivate))
56
57 static void gtk_style_set_provider_init (GtkStyleProviderIface *iface);
58 static void gtk_style_set_finalize      (GObject      *object);
59
60
61 G_DEFINE_TYPE_EXTENDED (GtkStyleSet, gtk_style_set, G_TYPE_OBJECT, 0,
62                         G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
63                                                gtk_style_set_provider_init));
64
65 static void
66 gtk_style_set_class_init (GtkStyleSetClass *klass)
67 {
68   GObjectClass *object_class = G_OBJECT_CLASS (klass);
69   GdkColor black = { 0, 0, 0, 0 };
70   GdkColor white = { 0, 65535, 65535, 65535 };
71   PangoFontDescription *font_desc;
72
73   object_class->finalize = gtk_style_set_finalize;
74
75   /* Initialize default property set */
76   gtk_style_set_register_property_color ("foreground-color", &white);
77   gtk_style_set_register_property_color ("background-color", &black);
78   gtk_style_set_register_property_color ("text-color", &white);
79   gtk_style_set_register_property_color ("base-color", &white);
80
81   font_desc = pango_font_description_from_string ("Sans 10");
82   gtk_style_set_register_property_font ("font", font_desc);
83   pango_font_description_free (font_desc);
84
85   g_type_class_add_private (object_class, sizeof (GtkStyleSetPrivate));
86 }
87
88 static PropertyData *
89 property_data_new (void)
90 {
91   PropertyData *data;
92
93   data = g_slice_new0 (PropertyData);
94
95   return data;
96 }
97
98 static void
99 property_data_free (PropertyData *data)
100 {
101   gint i;
102
103   for (i = 0; i <= GTK_STATE_INSENSITIVE; i++)
104     {
105       if (G_IS_VALUE (&data->values[i]))
106         g_value_unset (&data->values[i]);
107     }
108
109   g_slice_free (PropertyData, data);
110 }
111
112 static void
113 gtk_style_set_init (GtkStyleSet *set)
114 {
115   GtkStyleSetPrivate *priv;
116
117   priv = GTK_STYLE_SET_GET_PRIVATE (set);
118   priv->properties = g_hash_table_new_full (NULL, NULL, NULL,
119                                             (GDestroyNotify) property_data_free);
120 }
121
122 static void
123 gtk_style_set_finalize (GObject *object)
124 {
125   GtkStyleSetPrivate *priv;
126
127   priv = GTK_STYLE_SET_GET_PRIVATE (object);
128   g_hash_table_destroy (priv->properties);
129
130   G_OBJECT_CLASS (gtk_style_set_parent_class)->finalize (object);
131 }
132
133 GtkStyleSet *
134 gtk_style_set_get_style (GtkStyleProvider *provider)
135 {
136   /* Return style set itself */
137   return g_object_ref (provider);
138 }
139
140 static void
141 gtk_style_set_provider_init (GtkStyleProviderIface *iface)
142 {
143   iface->get_style = gtk_style_set_get_style;
144 }
145
146 static int
147 compare_property (gconstpointer p1,
148                   gconstpointer p2)
149 {
150   PropertyNode *key = (PropertyNode *) p1;
151   PropertyNode *node = (PropertyNode *) p2;
152
153   return (int) key->property_quark - node->property_quark;
154 }
155
156 static PropertyNode *
157 property_node_lookup (GQuark quark)
158 {
159   PropertyNode key = { 0 };
160
161   if (!quark)
162     return NULL;
163
164   if (!properties)
165     return NULL;
166
167   key.property_quark = quark;
168
169   return bsearch (&key, properties->data, properties->len,
170                   sizeof (PropertyNode), compare_property);
171 }
172
173 /* Property registration functions */
174 void
175 gtk_style_set_register_property (const gchar *property_name,
176                                  GType        type,
177                                  GValue      *default_value)
178 {
179   PropertyNode *node, new = { 0 };
180   GQuark quark;
181   gint i;
182
183   g_return_if_fail (property_name != NULL);
184   g_return_if_fail (default_value != NULL);
185   g_return_if_fail (type == G_VALUE_TYPE (default_value));
186
187   if (G_UNLIKELY (!properties))
188     properties = g_array_new (FALSE, TRUE, sizeof (PropertyNode));
189
190   quark = g_quark_try_string (property_name);
191
192   if ((node = property_node_lookup (quark)) != NULL)
193     {
194       g_warning ("Property \"%s\" was already registered with type %s",
195                  property_name, g_type_name (node->property_type));
196       return;
197     }
198
199   quark = g_quark_from_string (property_name);
200
201   new.property_quark = quark;
202   new.property_type = type;
203
204   g_value_init (&new.default_value, type);
205   g_value_copy (default_value, &new.default_value);
206
207   for (i = 0; i < properties->len; i++)
208     {
209       node = &g_array_index (properties, PropertyNode, i);
210
211       if (node->property_quark > quark)
212         break;
213     }
214
215   g_array_insert_val (properties, i, new);
216 }
217
218 void
219 gtk_style_set_register_property_color (const gchar *property_name,
220                                        GdkColor    *initial_value)
221 {
222   GValue value = { 0 };
223
224   g_return_if_fail (property_name != NULL);
225   g_return_if_fail (initial_value != NULL);
226
227   g_value_init (&value, GDK_TYPE_COLOR);
228   g_value_set_boxed (&value, initial_value);
229
230   gtk_style_set_register_property (property_name, GDK_TYPE_COLOR, &value);
231
232   g_value_unset (&value);
233 }
234
235 void
236 gtk_style_set_register_property_font (const gchar          *property_name,
237                                       PangoFontDescription *initial_value)
238 {
239   GValue value = { 0 };
240
241   g_return_if_fail (property_name != NULL);
242   g_return_if_fail (initial_value != NULL);
243
244   g_value_init (&value, PANGO_TYPE_FONT_DESCRIPTION);
245   g_value_set_boxed (&value, initial_value);
246
247   gtk_style_set_register_property (property_name, PANGO_TYPE_FONT_DESCRIPTION, &value);
248
249   g_value_unset (&value);
250 }
251
252 void
253 gtk_style_set_register_property_int (const gchar *property_name,
254                                      gint         initial_value)
255 {
256   GValue value = { 0 };
257
258   g_return_if_fail (property_name != NULL);
259
260   g_value_init (&value, G_TYPE_INT);
261   g_value_set_int (&value, initial_value);
262
263   gtk_style_set_register_property (property_name, G_TYPE_INT, &value);
264
265   g_value_unset (&value);
266 }
267
268 void
269 gtk_style_set_register_property_uint (const gchar *property_name,
270                                       guint        initial_value)
271 {
272   GValue value = { 0 };
273
274   g_return_if_fail (property_name != NULL);
275
276   g_value_init (&value, G_TYPE_UINT);
277   g_value_set_uint (&value, initial_value);
278
279   gtk_style_set_register_property (property_name, G_TYPE_UINT, &value);
280
281   g_value_unset (&value);
282 }
283
284 void
285 gtk_style_set_register_property_double (const gchar *property_name,
286                                         gdouble      initial_value)
287 {
288   GValue value = { 0 };
289
290   g_return_if_fail (property_name != NULL);
291
292   g_value_init (&value, G_TYPE_DOUBLE);
293   g_value_set_double (&value, initial_value);
294
295   gtk_style_set_register_property (property_name, G_TYPE_DOUBLE, &value);
296
297   g_value_unset (&value);
298 }
299
300 /* GtkStyleSet methods */
301
302 GtkStyleSet *
303 gtk_style_set_new (void)
304 {
305   return g_object_new (GTK_TYPE_STYLE_SET, NULL);
306 }
307
308 void
309 gtk_style_set_set_property (GtkStyleSet  *set,
310                             const gchar  *property,
311                             GtkStateType  state,
312                             const GValue *value)
313 {
314   GtkStyleSetPrivate *priv;
315   PropertyNode *node;
316   PropertyData *prop;
317
318   g_return_if_fail (GTK_IS_STYLE_SET (set));
319   g_return_if_fail (property != NULL);
320   g_return_if_fail (state < GTK_STATE_LAST);
321   g_return_if_fail (value != NULL);
322
323   node = property_node_lookup (g_quark_try_string (property));
324
325   if (!node)
326     {
327       g_warning ("Style property \"%s\" is not registered", property);
328       return;
329     }
330
331   g_return_if_fail (node->property_type == G_VALUE_TYPE (value));
332
333   priv = GTK_STYLE_SET_GET_PRIVATE (set);
334   prop = g_hash_table_lookup (priv->properties,
335                               GINT_TO_POINTER (node->property_quark));
336
337   if (!prop)
338     {
339       prop = property_data_new ();
340       g_hash_table_insert (priv->properties,
341                            GINT_TO_POINTER (node->property_quark),
342                            prop);
343     }
344
345   if (G_IS_VALUE (&prop->values[state]))
346     g_value_reset (&prop->values[state]);
347   else
348     g_value_init (&prop->values[state], node->property_type);
349
350   g_value_copy (value, &prop->values[state]);
351 }
352
353
354 void
355 gtk_style_set_set_valist (GtkStyleSet  *set,
356                           GtkStateType  state,
357                           va_list       args)
358 {
359   GtkStyleSetPrivate *priv;
360   const gchar *property_name;
361
362   g_return_if_fail (GTK_IS_STYLE_SET (set));
363   g_return_if_fail (state < GTK_STATE_LAST);
364
365   priv = GTK_STYLE_SET_GET_PRIVATE (set);
366   property_name = va_arg (args, const gchar *);
367
368   while (property_name)
369     {
370       PropertyNode *node;
371       PropertyData *prop;
372       gchar *error = NULL;
373
374       node = property_node_lookup (g_quark_try_string (property_name));
375
376       if (!node)
377         {
378           g_warning ("Style property \"%s\" is not registered", property_name);
379           break;
380         }
381
382       prop = g_hash_table_lookup (priv->properties,
383                                   GINT_TO_POINTER (node->property_quark));
384
385       if (!prop)
386         {
387           prop = property_data_new ();
388           g_hash_table_insert (priv->properties,
389                                GINT_TO_POINTER (node->property_quark),
390                                prop);
391         }
392
393       g_value_init (&prop->values[state], node->property_type);
394       G_VALUE_COLLECT (&prop->values[state], args, 0, &error);
395
396       if (error)
397         {
398           g_warning ("Could not set style property \"%s\": %s", property_name, error);
399           g_value_unset (&prop->values[state]);
400           g_free (error);
401           break;
402         }
403
404       property_name = va_arg (args, const gchar *);
405     }
406 }
407
408 void
409 gtk_style_set_set (GtkStyleSet  *set,
410                    GtkStateType  state,
411                    ...)
412 {
413   va_list args;
414
415   g_return_if_fail (GTK_IS_STYLE_SET (set));
416   g_return_if_fail (state < GTK_STATE_LAST);
417
418   va_start (args, state);
419   gtk_style_set_set_valist (set, state, args);
420   va_end (args);
421 }
422
423 gboolean
424 gtk_style_set_get_property (GtkStyleSet  *set,
425                             const gchar  *property,
426                             GtkStateType  state,
427                             GValue       *value)
428 {
429   GtkStyleSetPrivate *priv;
430   PropertyNode *node;
431   PropertyData *prop;
432
433   g_return_val_if_fail (GTK_IS_STYLE_SET (set), FALSE);
434   g_return_val_if_fail (property != NULL, FALSE);
435   g_return_val_if_fail (state < GTK_STATE_LAST, FALSE);
436   g_return_val_if_fail (value != NULL, FALSE);
437
438   node = property_node_lookup (g_quark_try_string (property));
439
440   if (!node)
441     {
442       g_warning ("Style property \"%s\" is not registered", property);
443       return FALSE;
444     }
445
446   priv = GTK_STYLE_SET_GET_PRIVATE (set);
447   prop = g_hash_table_lookup (priv->properties,
448                               GINT_TO_POINTER (node->property_quark));
449
450   g_value_init (value, node->property_type);
451
452   if (!prop ||
453       !G_IS_VALUE (&prop->values[state]))
454     g_value_copy (&node->default_value, value);
455   else
456     g_value_copy (&prop->values[state], value);
457
458   return TRUE;
459 }
460
461 void
462 gtk_style_set_get_valist (GtkStyleSet  *set,
463                           GtkStateType  state,
464                           va_list       args)
465 {
466   GtkStyleSetPrivate *priv;
467   const gchar *property_name;
468
469   g_return_if_fail (GTK_IS_STYLE_SET (set));
470   g_return_if_fail (state < GTK_STATE_LAST);
471
472   priv = GTK_STYLE_SET_GET_PRIVATE (set);
473   property_name = va_arg (args, const gchar *);
474
475   while (property_name)
476     {
477       PropertyNode *node;
478       PropertyData *prop;
479       gchar *error = NULL;
480
481       node = property_node_lookup (g_quark_try_string (property_name));
482
483       if (!node)
484         {
485           g_warning ("Style property \"%s\" is not registered", property_name);
486           break;
487         }
488
489       prop = g_hash_table_lookup (priv->properties,
490                                   GINT_TO_POINTER (node->property_quark));
491
492       if (!prop ||
493           !G_IS_VALUE (&prop->values[state]))
494         G_VALUE_LCOPY (&node->default_value, args, 0, &error);
495       else
496         G_VALUE_LCOPY (&prop->values[state], args, 0, &error);
497
498       if (error)
499         {
500           g_warning ("Could not get style property \"%s\": %s", property_name, error);
501           g_free (error);
502           break;
503         }
504
505       property_name = va_arg (args, const gchar *);
506     }
507 }
508
509 void
510 gtk_style_set_get (GtkStyleSet  *set,
511                    GtkStateType  state,
512                    ...)
513 {
514   va_list args;
515
516   g_return_if_fail (GTK_IS_STYLE_SET (set));
517   g_return_if_fail (state < GTK_STATE_LAST);
518
519   va_start (args, state);
520   gtk_style_set_get_valist (set, state, args);
521   va_end (args);
522 }
523
524 void
525 gtk_style_set_unset_property (GtkStyleSet  *set,
526                               const gchar  *property,
527                               GtkStateType  state)
528 {
529   GtkStyleSetPrivate *priv;
530   PropertyNode *node;
531   PropertyData *prop;
532
533   g_return_if_fail (GTK_IS_STYLE_SET (set));
534   g_return_if_fail (property != NULL);
535   g_return_if_fail (state < GTK_STATE_LAST);
536
537   node = property_node_lookup (g_quark_try_string (property));
538
539   if (!node)
540     {
541       g_warning ("Style property \"%s\" is not registered", property);
542       return;
543     }
544
545   priv = GTK_STYLE_SET_GET_PRIVATE (set);
546   prop = g_hash_table_lookup (priv->properties,
547                               GINT_TO_POINTER (node->property_quark));
548
549   if (!prop)
550     return;
551
552   g_value_unset (&prop->values[state]);
553 }
554
555 void
556 gtk_style_set_clear (GtkStyleSet *set)
557 {
558   GtkStyleSetPrivate *priv;
559
560   g_return_if_fail (GTK_IS_STYLE_SET (set));
561
562   priv = GTK_STYLE_SET_GET_PRIVATE (set);
563   g_hash_table_remove_all (priv->properties);
564 }
565
566 void
567 gtk_style_set_merge (GtkStyleSet       *set,
568                      const GtkStyleSet *set_to_merge,
569                      gboolean           replace)
570 {
571   GtkStyleSetPrivate *priv, *priv_to_merge;
572   GHashTableIter iter;
573   gpointer key, value;
574
575   g_return_if_fail (GTK_IS_STYLE_SET (set));
576   g_return_if_fail (GTK_IS_STYLE_SET (set_to_merge));
577
578   priv = GTK_STYLE_SET_GET_PRIVATE (set);
579   priv_to_merge = GTK_STYLE_SET_GET_PRIVATE (set_to_merge);
580
581   g_hash_table_iter_init (&iter, priv_to_merge->properties);
582
583   while (g_hash_table_iter_next (&iter, &key, &value))
584     {
585       PropertyData *prop_to_merge = value;
586       PropertyData *prop = NULL;
587       GtkStateType i;
588
589       for (i = GTK_STATE_NORMAL; i < GTK_STATE_LAST; i++)
590         {
591           if (G_VALUE_TYPE (&prop_to_merge->values[i]) == G_TYPE_INVALID)
592             continue;
593
594           if (!prop)
595             prop = g_hash_table_lookup (priv->properties, key);
596
597           if (!prop)
598             {
599               prop = property_data_new ();
600               g_hash_table_insert (priv->properties, key, prop);
601             }
602
603           if (replace ||
604               G_VALUE_TYPE (&prop->values[i]) == G_TYPE_INVALID)
605             {
606               if (G_VALUE_TYPE (&prop->values[i]) == G_TYPE_INVALID)
607                 g_value_init (&prop->values[i], G_VALUE_TYPE (&prop_to_merge->values[i]));
608
609               g_value_copy (&prop_to_merge->values[i],
610                             &prop->values[i]);
611             }
612         }
613     }
614 }
615
616 #define __GTK_STYLE_SET_C__
617 #include "gtkaliasdef.c"