]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyleproperties.c
abd9383440088540c64679906b1ad0457d3e2288
[~andy/gtk] / gtk / gtkstyleproperties.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 "gtkstyleproperties.h"
23
24 #include <stdlib.h>
25 #include <gobject/gvaluecollector.h>
26 #include <cairo-gobject.h>
27
28 #include "gtktypebuiltins.h"
29 #include "gtkstyleprovider.h"
30 #include "gtksymboliccolor.h"
31 #include "gtkprivate.h"
32 #include "gtkthemingengine.h"
33 #include "gtkanimationdescription.h"
34 #include "gtkborder.h"
35 #include "gtkgradient.h"
36 #include "gtk9slice.h"
37 #include "gtkintl.h"
38
39 /**
40  * SECTION:gtkstyleproperties
41  * @Short_description: Store for style property information
42  * @Title: GtkStyleProperties
43  *
44  * GtkStyleProperties provides the storage for style information
45  * that is used by #GtkStyleContext and other #GtkStyleProvider
46  * implementations.
47  *
48  * Before style properties can be stored in GtkStyleProperties, they
49  * must be registered with gtk_style_properties_register_property().
50  *
51  * Unless you are writing a #GtkStyleProvider implementation, you
52  * are unlikely to use this API directly, as gtk_style_context_get()
53  * and its variants are the preferred way to access styling information
54  * from widget implementations and theming engine implementations
55  * should use the APIs provided by #GtkThemingEngine instead.
56  */
57
58 typedef struct GtkStylePropertiesPrivate GtkStylePropertiesPrivate;
59 typedef struct PropertyData PropertyData;
60 typedef struct PropertyNode PropertyNode;
61 typedef struct ValueData ValueData;
62
63 struct PropertyNode
64 {
65   GQuark property_quark;
66   GParamSpec *pspec;
67   GtkStylePropertyParser parse_func;
68 };
69
70 struct ValueData
71 {
72   GtkStateFlags state;
73   GValue value;
74 };
75
76 struct PropertyData
77 {
78   GArray *values;
79 };
80
81 struct GtkStylePropertiesPrivate
82 {
83   GHashTable *color_map;
84   GHashTable *properties;
85 };
86
87 static GArray *properties = NULL;
88
89 static void gtk_style_properties_provider_init (GtkStyleProviderIface *iface);
90 static void gtk_style_properties_finalize      (GObject      *object);
91
92
93 G_DEFINE_TYPE_EXTENDED (GtkStyleProperties, gtk_style_properties, G_TYPE_OBJECT, 0,
94                         G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
95                                                gtk_style_properties_provider_init));
96
97 static void
98 gtk_style_properties_class_init (GtkStylePropertiesClass *klass)
99 {
100   GObjectClass *object_class = G_OBJECT_CLASS (klass);
101
102   object_class->finalize = gtk_style_properties_finalize;
103
104   /* Initialize default property set */
105   gtk_style_properties_register_property (NULL,
106                                           g_param_spec_boxed ("color",
107                                                               "Foreground color",
108                                                               "Foreground color",
109                                                               GDK_TYPE_RGBA, 0));
110   gtk_style_properties_register_property (NULL,
111                                           g_param_spec_boxed ("background-color",
112                                                               "Background color",
113                                                               "Background color",
114                                                               GDK_TYPE_RGBA, 0));
115
116   gtk_style_properties_register_property (NULL,
117                                           g_param_spec_boxed ("font",
118                                                               "Font Description",
119                                                               "Font Description",
120                                                               PANGO_TYPE_FONT_DESCRIPTION, 0));
121
122   gtk_style_properties_register_property (NULL,
123                                           g_param_spec_boxed ("margin",
124                                                               "Margin",
125                                                               "Margin",
126                                                               GTK_TYPE_BORDER, 0));
127   gtk_style_properties_register_property (NULL,
128                                           g_param_spec_boxed ("padding",
129                                                               "Padding",
130                                                               "Padding",
131                                                               GTK_TYPE_BORDER, 0));
132   gtk_style_properties_register_property (NULL,
133                                           g_param_spec_boxed ("border-width",
134                                                               "Border width",
135                                                               "Border width, in pixels",
136                                                               GTK_TYPE_BORDER, 0));
137   gtk_style_properties_register_property (NULL,
138                                           g_param_spec_int ("border-radius",
139                                                             "Border radius",
140                                                             "Border radius, in pixels",
141                                                             0, G_MAXINT, 0, 0));
142   gtk_style_properties_register_property (NULL,
143                                           g_param_spec_enum ("border-style",
144                                                              "Border style",
145                                                              "Border style",
146                                                              GTK_TYPE_BORDER_STYLE,
147                                                              GTK_BORDER_STYLE_NONE, 0));
148   gtk_style_properties_register_property (NULL,
149                                           g_param_spec_boxed ("border-color",
150                                                               "Border color",
151                                                               "Border color",
152                                                               GDK_TYPE_RGBA, 0));
153   gtk_style_properties_register_property (NULL,
154                                           g_param_spec_boxed ("background-image",
155                                                               "Background Image",
156                                                               "Background Image",
157                                                               CAIRO_GOBJECT_TYPE_PATTERN, 0));
158   gtk_style_properties_register_property (NULL,
159                                           g_param_spec_boxed ("border-image",
160                                                               "Border Image",
161                                                               "Border Image",
162                                                               GTK_TYPE_9SLICE, 0));
163   gtk_style_properties_register_property (NULL,
164                                           g_param_spec_object ("engine",
165                                                                "Theming Engine",
166                                                                "Theming Engine",
167                                                                GTK_TYPE_THEMING_ENGINE, 0));
168   gtk_style_properties_register_property (NULL,
169                                           g_param_spec_boxed ("transition",
170                                                               "Transition animation description",
171                                                               "Transition animation description",
172                                                               GTK_TYPE_ANIMATION_DESCRIPTION, 0));
173
174   g_type_class_add_private (object_class, sizeof (GtkStylePropertiesPrivate));
175 }
176
177 static PropertyData *
178 property_data_new (void)
179 {
180   PropertyData *data;
181
182   data = g_slice_new0 (PropertyData);
183   data->values = g_array_new (FALSE, FALSE, sizeof (ValueData));
184
185   return data;
186 }
187
188 static void
189 property_data_free (PropertyData *data)
190 {
191   guint i;
192
193   for (i = 0; i < data->values->len; i++)
194     {
195       ValueData *value_data;
196
197       value_data = &g_array_index (data->values, ValueData, i);
198
199       if (G_IS_VALUE (&value_data->value))
200         g_value_unset (&value_data->value);
201     }
202
203   g_array_free (data->values, TRUE);
204   g_slice_free (PropertyData, data);
205 }
206
207 static gboolean
208 property_data_find_position (PropertyData  *data,
209                              GtkStateFlags  state,
210                              guint         *pos)
211 {
212   gint min, max, mid;
213   gboolean found = FALSE;
214   guint position;
215
216   if (pos)
217     *pos = 0;
218
219   if (data->values->len == 0)
220     return FALSE;
221
222   /* Find position for the given state, or the position where
223    * it would be if not found, the array is ordered by the
224    * state flags.
225    */
226   min = 0;
227   max = data->values->len - 1;
228
229   do
230     {
231       ValueData *value_data;
232
233       mid = (min + max) / 2;
234       value_data = &g_array_index (data->values, ValueData, mid);
235
236       if (value_data->state == state)
237         {
238           found = TRUE;
239           position = mid;
240         }
241       else if (value_data->state < state)
242           position = min = mid + 1;
243       else
244         {
245           max = mid - 1;
246           position = mid;
247         }
248     }
249   while (!found && min <= max);
250
251   if (pos)
252     *pos = position;
253
254   return found;
255 }
256
257 static GValue *
258 property_data_get_value (PropertyData  *data,
259                          GtkStateFlags  state)
260 {
261   ValueData *val_data;
262   guint pos;
263
264   if (!property_data_find_position (data, state, &pos))
265     {
266       ValueData new = { 0 };
267
268       new.state = state;
269       g_array_insert_val (data->values, pos, new);
270     }
271
272   val_data = &g_array_index (data->values, ValueData, pos);
273
274   return &val_data->value;
275 }
276
277 static GValue *
278 property_data_match_state (PropertyData  *data,
279                            GtkStateFlags  state)
280 {
281   guint pos;
282   gint i;
283
284   if (property_data_find_position (data, state, &pos))
285     {
286       ValueData *val_data;
287
288       /* Exact match */
289       val_data = &g_array_index (data->values, ValueData, pos);
290       return &val_data->value;
291     }
292
293   if (pos >= data->values->len)
294     pos = data->values->len - 1;
295
296   /* No exact match, go downwards the list to find
297    * the closest match to the given state flags, as
298    * a side effect, there is an implicit precedence
299    * of higher flags over the smaller ones.
300    */
301   for (i = pos; i >= 0; i--)
302     {
303       ValueData *val_data;
304
305       val_data = &g_array_index (data->values, ValueData, i);
306
307        /* Check whether any of the requested
308         * flags are set, and no other flags are.
309         *
310         * Also, no flags acts as a wildcard, such
311         * value should be always in the first position
312         * in the array (if present) anyways.
313         */
314       if (val_data->state == 0 ||
315           ((val_data->state & state) != 0 &&
316            (val_data->state & ~state) == 0))
317         return &val_data->value;
318     }
319
320   return NULL;
321 }
322
323 static void
324 gtk_style_properties_init (GtkStyleProperties *props)
325 {
326   GtkStylePropertiesPrivate *priv;
327
328   priv = props->priv = G_TYPE_INSTANCE_GET_PRIVATE (props,
329                                                     GTK_TYPE_STYLE_PROPERTIES,
330                                                     GtkStylePropertiesPrivate);
331
332   priv->properties = g_hash_table_new_full (NULL, NULL, NULL,
333                                             (GDestroyNotify) property_data_free);
334 }
335
336 static void
337 gtk_style_properties_finalize (GObject *object)
338 {
339   GtkStylePropertiesPrivate *priv;
340   GtkStyleProperties *props;
341
342   props = GTK_STYLE_PROPERTIES (object);
343   priv = props->priv;
344   g_hash_table_destroy (priv->properties);
345
346   if (priv->color_map)
347     g_hash_table_destroy (priv->color_map);
348
349   G_OBJECT_CLASS (gtk_style_properties_parent_class)->finalize (object);
350 }
351
352 GtkStyleProperties *
353 gtk_style_properties_get_style (GtkStyleProvider *provider,
354                                 GtkWidgetPath    *path)
355 {
356   /* Return style set itself */
357   return g_object_ref (provider);
358 }
359
360 static void
361 gtk_style_properties_provider_init (GtkStyleProviderIface *iface)
362 {
363   iface->get_style = gtk_style_properties_get_style;
364 }
365
366 static int
367 compare_property (gconstpointer p1,
368                   gconstpointer p2)
369 {
370   PropertyNode *key = (PropertyNode *) p1;
371   PropertyNode *node = (PropertyNode *) p2;
372
373   if (key->property_quark > node->property_quark)
374     return 1;
375   else if (key->property_quark < node->property_quark)
376     return -1;
377
378   return 0;
379 }
380
381 static PropertyNode *
382 property_node_lookup (GQuark quark)
383 {
384   PropertyNode key = { 0 };
385
386   if (!quark)
387     return NULL;
388
389   if (!properties)
390     return NULL;
391
392   key.property_quark = quark;
393
394   return bsearch (&key, properties->data, properties->len,
395                   sizeof (PropertyNode), compare_property);
396 }
397
398 /* Property registration functions */
399
400 /**
401  * gtk_style_properties_register_property:
402  * @parse_func: parsing function to use, or %NULL
403  * @pspec: the #GParamSpec for the new property
404  *
405  * Registers a property so it can be used in the CSS file format.
406  * This function is the low-level equivalent of
407  * gtk_theming_engine_register_property(), if you are implementing
408  * a theming engine, you want to use that function instead.
409  *
410  * Since: 3.0
411  **/
412 void
413 gtk_style_properties_register_property (GtkStylePropertyParser  parse_func,
414                                         GParamSpec             *pspec)
415 {
416   PropertyNode *node, new = { 0 };
417   GQuark quark;
418   gint i;
419
420   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
421
422   if (G_UNLIKELY (!properties))
423     properties = g_array_new (FALSE, TRUE, sizeof (PropertyNode));
424
425   quark = g_quark_from_string (pspec->name);
426
427   if ((node = property_node_lookup (quark)) != NULL)
428     {
429       g_warning ("Property \"%s\" was already registered with type %s",
430                  pspec->name, g_type_name (node->pspec->value_type));
431       return;
432     }
433
434   new.property_quark = quark;
435   new.pspec = pspec;
436
437   if (parse_func)
438     new.parse_func = parse_func;
439
440   for (i = 0; i < properties->len; i++)
441     {
442       node = &g_array_index (properties, PropertyNode, i);
443
444       if (node->property_quark > quark)
445         break;
446     }
447
448   g_array_insert_val (properties, i, new);
449 }
450
451 /**
452  * gtk_style_properties_lookup_property:
453  * @property_name: property name to look up
454  * @parse_func: (out): return location for the parse function
455  * @pspec: (out): return location for the #GParamSpec
456  *
457  * Returns %TRUE if a property has been registered, if @pspec or
458  * @parse_func are not %NULL, the #GParamSpec and parsing function
459  * will be respectively returned.
460  *
461  * Returns: %TRUE if the property is registered, %FALSE otherwise
462  *
463  * Since: 3.0
464  **/
465 gboolean
466 gtk_style_properties_lookup_property (const gchar             *property_name,
467                                       GtkStylePropertyParser  *parse_func,
468                                       GParamSpec             **pspec)
469 {
470   PropertyNode *node;
471   GtkStylePropertiesClass *klass;
472   gboolean found = FALSE;
473   GQuark quark;
474   gint i;
475
476   g_return_val_if_fail (property_name != NULL, FALSE);
477
478   klass = g_type_class_ref (GTK_TYPE_STYLE_PROPERTIES);
479   quark = g_quark_try_string (property_name);
480
481   if (quark == 0)
482     {
483       g_type_class_unref (klass);
484       return FALSE;
485     }
486
487   for (i = 0; i < properties->len; i++)
488     {
489       node = &g_array_index (properties, PropertyNode, i);
490
491       if (node->property_quark == quark)
492         {
493           if (pspec)
494             *pspec = node->pspec;
495
496           if (parse_func)
497             *parse_func = node->parse_func;
498
499           found = TRUE;
500           break;
501         }
502       else if (node->property_quark > quark)
503         break;
504     }
505
506   g_type_class_unref (klass);
507
508   return found;
509 }
510
511 /* GtkStyleProperties methods */
512
513 /**
514  * gtk_style_properties_new:
515  *
516  * Returns a newly created #GtkStyleProperties
517  *
518  * Returns: a new #GtkStyleProperties
519  **/
520 GtkStyleProperties *
521 gtk_style_properties_new (void)
522 {
523   return g_object_new (GTK_TYPE_STYLE_PROPERTIES, NULL);
524 }
525
526 /**
527  * gtk_style_properties_map_color:
528  * @props: a #GtkStyleProperties
529  * @name: color name
530  * @color: #GtkSymbolicColor to map @name to
531  *
532  * Maps @color so it can be referenced by @name. See
533  * gtk_style_properties_lookup_color()
534  *
535  * Since: 3.0
536  **/
537 void
538 gtk_style_properties_map_color (GtkStyleProperties *props,
539                                 const gchar        *name,
540                                 GtkSymbolicColor   *color)
541 {
542   GtkStylePropertiesPrivate *priv;
543
544   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
545   g_return_if_fail (name != NULL);
546   g_return_if_fail (color != NULL);
547
548   priv = props->priv;
549
550   if (G_UNLIKELY (!priv->color_map))
551     priv->color_map = g_hash_table_new_full (g_str_hash,
552                                              g_str_equal,
553                                              (GDestroyNotify) g_free,
554                                              (GDestroyNotify) gtk_symbolic_color_unref);
555
556   g_hash_table_replace (priv->color_map,
557                         g_strdup (name),
558                         gtk_symbolic_color_ref (color));
559 }
560
561 /**
562  * gtk_style_properties_lookup_color:
563  * @props: a #GtkStyleProperties
564  * @name: color name to lookup
565  *
566  * Returns the symbolic color that is mapped
567  * to @name.
568  *
569  * Returns: The mapped color
570  *
571  * Since: 3.0
572  **/
573 GtkSymbolicColor *
574 gtk_style_properties_lookup_color (GtkStyleProperties *props,
575                                    const gchar        *name)
576 {
577   GtkStylePropertiesPrivate *priv;
578
579   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
580   g_return_val_if_fail (name != NULL, NULL);
581
582   priv = props->priv;
583
584   if (!priv->color_map)
585     return NULL;
586
587   return g_hash_table_lookup (priv->color_map, name);
588 }
589
590 /**
591  * gtk_style_properties_set_property:
592  * @props: a #GtkStyleProperties
593  * @property: styling property to set
594  * @state: state to set the value for
595  * @value: new value for the property
596  *
597  * Sets a styling property in @props.
598  *
599  * Since: 3.0
600  **/
601 void
602 gtk_style_properties_set_property (GtkStyleProperties *props,
603                                    const gchar        *property,
604                                    GtkStateFlags       state,
605                                    const GValue       *value)
606 {
607   GtkStylePropertiesPrivate *priv;
608   PropertyNode *node;
609   PropertyData *prop;
610   GType value_type;
611   GValue *val;
612
613   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
614   g_return_if_fail (property != NULL);
615   g_return_if_fail (value != NULL);
616
617   value_type = G_VALUE_TYPE (value);
618   node = property_node_lookup (g_quark_try_string (property));
619
620   if (!node)
621     {
622       g_warning ("Style property \"%s\" is not registered", property);
623       return;
624     }
625
626   if (node->pspec->value_type == GDK_TYPE_RGBA ||
627       node->pspec->value_type == GDK_TYPE_COLOR)
628     {
629       /* Allow GtkSymbolicColor as well */
630       g_return_if_fail (value_type == GDK_TYPE_RGBA ||
631                         value_type == GDK_TYPE_COLOR ||
632                         value_type == GTK_TYPE_SYMBOLIC_COLOR);
633     }
634   else if (node->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN)
635     {
636       /* Allow GtkGradient as a substitute */
637       g_return_if_fail (value_type == CAIRO_GOBJECT_TYPE_PATTERN ||
638                         value_type == GTK_TYPE_GRADIENT);
639     }
640   else
641     g_return_if_fail (node->pspec->value_type == value_type);
642
643   priv = props->priv;
644   prop = g_hash_table_lookup (priv->properties,
645                               GINT_TO_POINTER (node->property_quark));
646
647   if (!prop)
648     {
649       prop = property_data_new ();
650       g_hash_table_insert (priv->properties,
651                            GINT_TO_POINTER (node->property_quark),
652                            prop);
653     }
654
655   val = property_data_get_value (prop, state);
656
657   if (G_VALUE_TYPE (val) == value_type)
658     g_value_reset (val);
659   else
660     {
661       if (G_IS_VALUE (val))
662         g_value_unset (val);
663
664       g_value_init (val, value_type);
665     }
666
667   g_value_copy (value, val);
668 }
669
670 /**
671  * gtk_style_properties_set_valist:
672  * @props: a #GtkStyleProperties
673  * @state: state to set the values for
674  * @args: va_list of property name/value pairs, followed by %NULL
675  *
676  * Sets several style properties on @props.
677  *
678  * Since: 3.0
679  **/
680 void
681 gtk_style_properties_set_valist (GtkStyleProperties *props,
682                                  GtkStateFlags       state,
683                                  va_list             args)
684 {
685   GtkStylePropertiesPrivate *priv;
686   const gchar *property_name;
687
688   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
689
690   priv = props->priv;
691   property_name = va_arg (args, const gchar *);
692
693   while (property_name)
694     {
695       PropertyNode *node;
696       PropertyData *prop;
697       gchar *error = NULL;
698       GValue *val;
699
700       node = property_node_lookup (g_quark_try_string (property_name));
701
702       if (!node)
703         {
704           g_warning ("Style property \"%s\" is not registered", property_name);
705           break;
706         }
707
708       prop = g_hash_table_lookup (priv->properties,
709                                   GINT_TO_POINTER (node->property_quark));
710
711       if (!prop)
712         {
713           prop = property_data_new ();
714           g_hash_table_insert (priv->properties,
715                                GINT_TO_POINTER (node->property_quark),
716                                prop);
717         }
718
719       val = property_data_get_value (prop, state);
720
721       if (G_IS_VALUE (val))
722         g_value_unset (val);
723
724       g_value_init (val, node->pspec->value_type);
725       G_VALUE_COLLECT (val, args, 0, &error);
726
727       if (error)
728         {
729           g_warning ("Could not set style property \"%s\": %s", property_name, error);
730           g_value_unset (val);
731           g_free (error);
732           break;
733         }
734
735       property_name = va_arg (args, const gchar *);
736     }
737 }
738
739 /**
740  * gtk_style_properties_set:
741  * @props: a #GtkStyleProperties
742  * @state: state to set the values for
743  * @...: property name/value pairs, followed by %NULL
744  *
745  * Sets several style properties on @props.
746  *
747  * Since: 3.0
748  **/
749 void
750 gtk_style_properties_set (GtkStyleProperties *props,
751                           GtkStateFlags       state,
752                           ...)
753 {
754   va_list args;
755
756   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
757
758   va_start (args, state);
759   gtk_style_properties_set_valist (props, state, args);
760   va_end (args);
761 }
762
763 static gboolean
764 resolve_color (GtkStyleProperties *props,
765                GValue             *value)
766 {
767   GdkRGBA color;
768
769   /* Resolve symbolic color to GdkRGBA */
770   if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
771     return FALSE;
772
773   /* Store it back, this is where GdkRGBA caching happens */
774   g_value_unset (value);
775   g_value_init (value, GDK_TYPE_RGBA);
776   g_value_set_boxed (value, &color);
777
778   return TRUE;
779 }
780
781 static gboolean
782 resolve_color_rgb (GtkStyleProperties *props,
783                    GValue             *value)
784 {
785   GdkColor color = { 0 };
786   GdkRGBA rgba;
787
788   if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
789     return FALSE;
790
791   color.red = rgba.red * 65535. + 0.5;
792   color.green = rgba.green * 65535. + 0.5;
793   color.blue = rgba.blue * 65535. + 0.5;
794
795   g_value_unset (value);
796   g_value_init (value, GDK_TYPE_COLOR);
797   g_value_set_boxed (value, &color);
798
799   return TRUE;
800 }
801
802 static gboolean
803 resolve_gradient (GtkStyleProperties *props,
804                   GValue             *value)
805 {
806   cairo_pattern_t *gradient;
807
808   if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
809     return FALSE;
810
811   /* Store it back, this is where cairo_pattern_t caching happens */
812   g_value_unset (value);
813   g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
814   g_value_take_boxed (value, gradient);
815
816   return TRUE;
817 }
818
819 static gboolean
820 style_properties_resolve_type (GtkStyleProperties *props,
821                                PropertyNode       *node,
822                                GValue             *val)
823 {
824   if (val && G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
825     {
826       if (node->pspec->value_type == GDK_TYPE_RGBA)
827         {
828           if (!resolve_color (props, val))
829             return FALSE;
830         }
831       else if (node->pspec->value_type == GDK_TYPE_COLOR)
832         {
833           if (!resolve_color_rgb (props, val))
834             return FALSE;
835         }
836       else
837         return FALSE;
838     }
839   else if (val && G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
840     {
841       g_return_val_if_fail (node->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN, FALSE);
842
843       if (!resolve_gradient (props, val))
844         return FALSE;
845     }
846
847   return TRUE;
848 }
849
850 static void
851 lookup_default_value (PropertyNode *node,
852                       GValue       *value)
853 {
854   if (node->pspec->value_type == GTK_TYPE_THEMING_ENGINE)
855     g_value_set_object (value, gtk_theming_engine_load (NULL));
856   else if (node->pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
857     g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
858   else
859     g_param_value_set_default (node->pspec, value);
860 }
861
862 const GValue *
863 _gtk_style_properties_peek_property (GtkStyleProperties *props,
864                                      const gchar        *prop_name,
865                                      GtkStateFlags       state)
866 {
867   GtkStylePropertiesPrivate *priv;
868   PropertyNode *node;
869   PropertyData *prop;
870   GValue *val;
871
872   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
873   g_return_val_if_fail (prop_name != NULL, NULL);
874
875   node = property_node_lookup (g_quark_try_string (prop_name));
876
877   if (!node)
878     {
879       g_warning ("Style property \"%s\" is not registered", prop_name);
880       return NULL;
881     }
882
883   priv = props->priv;
884   prop = g_hash_table_lookup (priv->properties,
885                               GINT_TO_POINTER (node->property_quark));
886
887   if (!prop)
888     return NULL;
889
890   val = property_data_match_state (prop, state);
891
892   if (val &&
893       !style_properties_resolve_type (props, node, val))
894     return NULL;
895
896   return val;
897 }
898
899 /**
900  * gtk_style_properties_get_property:
901  * @props: a #GtkStyleProperties
902  * @property: style property name
903  * @state: state to retrieve the property value for
904  * @value: (out) (transfer full):  return location for the style property value.
905  *
906  * Gets a style property from @props for the given state. When done with @value,
907  * g_value_unset() needs to be called to free any allocated memory.
908  *
909  * Returns: %TRUE if the property exists in @props, %FALSE otherwise
910  *
911  * Since: 3.0
912  **/
913 gboolean
914 gtk_style_properties_get_property (GtkStyleProperties *props,
915                                    const gchar        *property,
916                                    GtkStateFlags       state,
917                                    GValue             *value)
918 {
919   GtkStylePropertiesPrivate *priv;
920   PropertyNode *node;
921   PropertyData *prop;
922   GValue *val;
923
924   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
925   g_return_val_if_fail (property != NULL, FALSE);
926   g_return_val_if_fail (value != NULL, FALSE);
927
928   node = property_node_lookup (g_quark_try_string (property));
929
930   if (!node)
931     {
932       g_warning ("Style property \"%s\" is not registered", property);
933       return FALSE;
934     }
935
936   priv = props->priv;
937   prop = g_hash_table_lookup (priv->properties,
938                               GINT_TO_POINTER (node->property_quark));
939
940   if (!prop)
941     return FALSE;
942
943   g_value_init (value, node->pspec->value_type);
944   val = property_data_match_state (prop, state);
945
946   if (val &&
947       !style_properties_resolve_type (props, node, val))
948     return FALSE;
949
950   if (val)
951     {
952       g_param_value_validate (node->pspec, val);
953       g_value_copy (val, value);
954     }
955   else
956     lookup_default_value (node, value);
957
958   return TRUE;
959 }
960
961 /**
962  * gtk_style_properties_get_valist:
963  * @props: a #GtkStyleProperties
964  * @state: state to retrieve the property values for
965  * @args: va_list of property name/return location pairs, followed by %NULL
966  *
967  * Retrieves several style property values from @props for a given state.
968  *
969  * Since: 3.0
970  **/
971 void
972 gtk_style_properties_get_valist (GtkStyleProperties *props,
973                                  GtkStateFlags       state,
974                                  va_list             args)
975 {
976   GtkStylePropertiesPrivate *priv;
977   const gchar *property_name;
978
979   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
980
981   priv = props->priv;
982   property_name = va_arg (args, const gchar *);
983
984   while (property_name)
985     {
986       PropertyNode *node;
987       PropertyData *prop;
988       gchar *error = NULL;
989       GValue *val = NULL;
990
991       node = property_node_lookup (g_quark_try_string (property_name));
992
993       if (!node)
994         {
995           g_warning ("Style property \"%s\" is not registered", property_name);
996           break;
997         }
998
999       prop = g_hash_table_lookup (priv->properties,
1000                                   GINT_TO_POINTER (node->property_quark));
1001
1002       if (prop)
1003         val = property_data_match_state (prop, state);
1004
1005       if (val &&
1006           !style_properties_resolve_type (props, node, val))
1007         val = NULL;
1008
1009       if (val)
1010         {
1011           g_param_value_validate (node->pspec, val);
1012           G_VALUE_LCOPY (val, args, 0, &error);
1013         }
1014       else
1015         {
1016           GValue default_value = { 0 };
1017
1018           g_value_init (&default_value, node->pspec->value_type);
1019           lookup_default_value (node, &default_value);
1020           G_VALUE_LCOPY (&default_value, args, 0, &error);
1021           g_value_unset (&default_value);
1022         }
1023
1024       if (error)
1025         {
1026           g_warning ("Could not get style property \"%s\": %s", property_name, error);
1027           g_free (error);
1028           break;
1029         }
1030
1031       property_name = va_arg (args, const gchar *);
1032     }
1033 }
1034
1035 /**
1036  * gtk_style_properties_get:
1037  * @props: a #GtkStyleProperties
1038  * @state: state to retrieve the property values for
1039  * @...: property name /return value pairs, followed by %NULL
1040  *
1041  * Retrieves several style property values from @props for a
1042  * given state.
1043  *
1044  * Since: 3.0
1045  **/
1046 void
1047 gtk_style_properties_get (GtkStyleProperties *props,
1048                           GtkStateFlags       state,
1049                           ...)
1050 {
1051   va_list args;
1052
1053   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1054
1055   va_start (args, state);
1056   gtk_style_properties_get_valist (props, state, args);
1057   va_end (args);
1058 }
1059
1060 /**
1061  * gtk_style_properties_unset_property:
1062  * @props: a #GtkStyleProperties
1063  * @property: property to unset
1064  * @state: state to unset
1065  *
1066  * Unsets a style property in @props.
1067  *
1068  * Since: 3.0
1069  **/
1070 void
1071 gtk_style_properties_unset_property (GtkStyleProperties *props,
1072                                      const gchar        *property,
1073                                      GtkStateFlags       state)
1074 {
1075   GtkStylePropertiesPrivate *priv;
1076   PropertyNode *node;
1077   PropertyData *prop;
1078   guint pos;
1079
1080   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1081   g_return_if_fail (property != NULL);
1082
1083   node = property_node_lookup (g_quark_try_string (property));
1084
1085   if (!node)
1086     {
1087       g_warning ("Style property \"%s\" is not registered", property);
1088       return;
1089     }
1090
1091   priv = props->priv;
1092   prop = g_hash_table_lookup (priv->properties,
1093                               GINT_TO_POINTER (node->property_quark));
1094
1095   if (!prop)
1096     return;
1097
1098   if (property_data_find_position (prop, state, &pos))
1099     {
1100       ValueData *data;
1101
1102       data = &g_array_index (prop->values, ValueData, pos);
1103
1104       if (G_IS_VALUE (&data->value))
1105         g_value_unset (&data->value);
1106
1107       g_array_remove_index (prop->values, pos);
1108     }
1109 }
1110
1111 /**
1112  * gtk_style_properties_clear:
1113  * @props: a #GtkStyleProperties
1114  *
1115  * Clears all style information from @props.
1116  **/
1117 void
1118 gtk_style_properties_clear (GtkStyleProperties *props)
1119 {
1120   GtkStylePropertiesPrivate *priv;
1121
1122   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1123
1124   priv = props->priv;
1125   g_hash_table_remove_all (priv->properties);
1126 }
1127
1128 /**
1129  * gtk_style_properties_merge:
1130  * @props: a #GtkStyleProperties
1131  * @props_to_merge: a second #GtkStyleProperties
1132  * @replace: whether to replace values or not
1133  *
1134  * Merges into @props all the style information contained
1135  * in @props_to_merge. If @replace is %TRUE, the values
1136  * will be overwritten, if it is %FALSE, the older values
1137  * will prevail.
1138  *
1139  * Since: 3.0
1140  **/
1141 void
1142 gtk_style_properties_merge (GtkStyleProperties       *props,
1143                             const GtkStyleProperties *props_to_merge,
1144                             gboolean                  replace)
1145 {
1146   GtkStylePropertiesPrivate *priv, *priv_to_merge;
1147   GHashTableIter iter;
1148   gpointer key, value;
1149
1150   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1151   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props_to_merge));
1152
1153   priv = props->priv;
1154   priv_to_merge = props_to_merge->priv;
1155
1156   /* Merge symbolic color map */
1157   if (priv_to_merge->color_map)
1158     {
1159       g_hash_table_iter_init (&iter, priv_to_merge->color_map);
1160
1161       while (g_hash_table_iter_next (&iter, &key, &value))
1162         {
1163           const gchar *name;
1164           GtkSymbolicColor *color;
1165
1166           name = key;
1167           color = value;
1168
1169           if (!replace &&
1170               g_hash_table_lookup (priv->color_map, name))
1171             continue;
1172
1173           gtk_style_properties_map_color (props, name, color);
1174         }
1175     }
1176
1177   /* Merge symbolic style properties */
1178   g_hash_table_iter_init (&iter, priv_to_merge->properties);
1179
1180   while (g_hash_table_iter_next (&iter, &key, &value))
1181     {
1182       PropertyData *prop_to_merge = value;
1183       PropertyData *prop;
1184       guint i;
1185
1186       prop = g_hash_table_lookup (priv->properties, key);
1187
1188       if (!prop)
1189         {
1190           prop = property_data_new ();
1191           g_hash_table_insert (priv->properties, key, prop);
1192         }
1193
1194       for (i = 0; i < prop_to_merge->values->len; i++)
1195         {
1196           ValueData *data;
1197           GValue *value;
1198
1199           data = &g_array_index (prop_to_merge->values, ValueData, i);
1200           value = property_data_get_value (prop, data->state);
1201
1202           if (G_VALUE_TYPE (&data->value) == PANGO_TYPE_FONT_DESCRIPTION &&
1203               G_IS_VALUE (value))
1204             {
1205               PangoFontDescription *font_desc;
1206               PangoFontDescription *font_desc_to_merge;
1207
1208               /* Handle merging of font descriptions */
1209               font_desc = g_value_get_boxed (value);
1210               font_desc_to_merge = g_value_get_boxed (&data->value);
1211
1212               pango_font_description_merge (font_desc, font_desc_to_merge, replace);
1213             }
1214           else if (replace || !G_IS_VALUE (value))
1215             {
1216               if (!G_IS_VALUE (value))
1217                 g_value_init (value, G_VALUE_TYPE (&data->value));
1218               else if (G_VALUE_TYPE (value) != G_VALUE_TYPE (&data->value))
1219                 {
1220                   g_value_unset (value);
1221                   g_value_init (value, G_VALUE_TYPE (&data->value));
1222                 }
1223
1224               g_value_copy (&data->value, value);
1225             }
1226         }
1227     }
1228 }