]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyleproperties.c
Merge branch 'master' into broadway
[~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
857     g_param_value_set_default (node->pspec, value);
858 }
859
860 const GValue *
861 _gtk_style_properties_peek_property (GtkStyleProperties *props,
862                                      const gchar        *prop_name,
863                                      GtkStateFlags       state)
864 {
865   GtkStylePropertiesPrivate *priv;
866   PropertyNode *node;
867   PropertyData *prop;
868   GValue *val;
869
870   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
871   g_return_val_if_fail (prop_name != NULL, NULL);
872
873   node = property_node_lookup (g_quark_try_string (prop_name));
874
875   if (!node)
876     {
877       g_warning ("Style property \"%s\" is not registered", prop_name);
878       return NULL;
879     }
880
881   priv = props->priv;
882   prop = g_hash_table_lookup (priv->properties,
883                               GINT_TO_POINTER (node->property_quark));
884
885   if (!prop)
886     return NULL;
887
888   val = property_data_match_state (prop, state);
889
890   if (val &&
891       !style_properties_resolve_type (props, node, val))
892     return NULL;
893
894   return val;
895 }
896
897 /**
898  * gtk_style_properties_get_property:
899  * @props: a #GtkStyleProperties
900  * @property: style property name
901  * @state: state to retrieve the property value for
902  * @value: (out) (transfer full):  return location for the style property value.
903  *
904  * Gets a style property from @props for the given state. When done with @value,
905  * g_value_unset() needs to be called to free any allocated memory.
906  *
907  * Returns: %TRUE if the property exists in @props, %FALSE otherwise
908  *
909  * Since: 3.0
910  **/
911 gboolean
912 gtk_style_properties_get_property (GtkStyleProperties *props,
913                                    const gchar        *property,
914                                    GtkStateFlags       state,
915                                    GValue             *value)
916 {
917   GtkStylePropertiesPrivate *priv;
918   PropertyNode *node;
919   PropertyData *prop;
920   GValue *val;
921
922   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
923   g_return_val_if_fail (property != NULL, FALSE);
924   g_return_val_if_fail (value != NULL, FALSE);
925
926   node = property_node_lookup (g_quark_try_string (property));
927
928   if (!node)
929     {
930       g_warning ("Style property \"%s\" is not registered", property);
931       return FALSE;
932     }
933
934   priv = props->priv;
935   prop = g_hash_table_lookup (priv->properties,
936                               GINT_TO_POINTER (node->property_quark));
937
938   if (!prop)
939     return FALSE;
940
941   g_value_init (value, node->pspec->value_type);
942   val = property_data_match_state (prop, state);
943
944   if (val &&
945       !style_properties_resolve_type (props, node, val))
946     return FALSE;
947
948   if (val)
949     {
950       g_param_value_validate (node->pspec, val);
951       g_value_copy (val, value);
952     }
953   else
954     lookup_default_value (node, value);
955
956   return TRUE;
957 }
958
959 /**
960  * gtk_style_properties_get_valist:
961  * @props: a #GtkStyleProperties
962  * @state: state to retrieve the property values for
963  * @args: va_list of property name/return location pairs, followed by %NULL
964  *
965  * Retrieves several style property values from @props for a given state.
966  *
967  * Since: 3.0
968  **/
969 void
970 gtk_style_properties_get_valist (GtkStyleProperties *props,
971                                  GtkStateFlags       state,
972                                  va_list             args)
973 {
974   GtkStylePropertiesPrivate *priv;
975   const gchar *property_name;
976
977   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
978
979   priv = props->priv;
980   property_name = va_arg (args, const gchar *);
981
982   while (property_name)
983     {
984       PropertyNode *node;
985       PropertyData *prop;
986       gchar *error = NULL;
987       GValue *val = NULL;
988
989       node = property_node_lookup (g_quark_try_string (property_name));
990
991       if (!node)
992         {
993           g_warning ("Style property \"%s\" is not registered", property_name);
994           break;
995         }
996
997       prop = g_hash_table_lookup (priv->properties,
998                                   GINT_TO_POINTER (node->property_quark));
999
1000       if (prop)
1001         val = property_data_match_state (prop, state);
1002
1003       if (val &&
1004           !style_properties_resolve_type (props, node, val))
1005         val = NULL;
1006
1007       if (val)
1008         {
1009           g_param_value_validate (node->pspec, val);
1010           G_VALUE_LCOPY (val, args, 0, &error);
1011         }
1012       else
1013         {
1014           GValue default_value = { 0 };
1015
1016           g_value_init (&default_value, node->pspec->value_type);
1017           lookup_default_value (node, &default_value);
1018           G_VALUE_LCOPY (&default_value, args, 0, &error);
1019           g_value_unset (&default_value);
1020         }
1021
1022       if (error)
1023         {
1024           g_warning ("Could not get style property \"%s\": %s", property_name, error);
1025           g_free (error);
1026           break;
1027         }
1028
1029       property_name = va_arg (args, const gchar *);
1030     }
1031 }
1032
1033 /**
1034  * gtk_style_properties_get:
1035  * @props: a #GtkStyleProperties
1036  * @state: state to retrieve the property values for
1037  * @...: property name /return value pairs, followed by %NULL
1038  *
1039  * Retrieves several style property values from @props for a
1040  * given state.
1041  *
1042  * Since: 3.0
1043  **/
1044 void
1045 gtk_style_properties_get (GtkStyleProperties *props,
1046                           GtkStateFlags       state,
1047                           ...)
1048 {
1049   va_list args;
1050
1051   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1052
1053   va_start (args, state);
1054   gtk_style_properties_get_valist (props, state, args);
1055   va_end (args);
1056 }
1057
1058 /**
1059  * gtk_style_properties_unset_property:
1060  * @props: a #GtkStyleProperties
1061  * @property: property to unset
1062  * @state: state to unset
1063  *
1064  * Unsets a style property in @props.
1065  *
1066  * Since: 3.0
1067  **/
1068 void
1069 gtk_style_properties_unset_property (GtkStyleProperties *props,
1070                                      const gchar        *property,
1071                                      GtkStateFlags       state)
1072 {
1073   GtkStylePropertiesPrivate *priv;
1074   PropertyNode *node;
1075   PropertyData *prop;
1076   guint pos;
1077
1078   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1079   g_return_if_fail (property != NULL);
1080
1081   node = property_node_lookup (g_quark_try_string (property));
1082
1083   if (!node)
1084     {
1085       g_warning ("Style property \"%s\" is not registered", property);
1086       return;
1087     }
1088
1089   priv = props->priv;
1090   prop = g_hash_table_lookup (priv->properties,
1091                               GINT_TO_POINTER (node->property_quark));
1092
1093   if (!prop)
1094     return;
1095
1096   if (property_data_find_position (prop, state, &pos))
1097     {
1098       ValueData *data;
1099
1100       data = &g_array_index (prop->values, ValueData, pos);
1101
1102       if (G_IS_VALUE (&data->value))
1103         g_value_unset (&data->value);
1104
1105       g_array_remove_index (prop->values, pos);
1106     }
1107 }
1108
1109 /**
1110  * gtk_style_properties_clear:
1111  * @props: a #GtkStyleProperties
1112  *
1113  * Clears all style information from @props.
1114  **/
1115 void
1116 gtk_style_properties_clear (GtkStyleProperties *props)
1117 {
1118   GtkStylePropertiesPrivate *priv;
1119
1120   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1121
1122   priv = props->priv;
1123   g_hash_table_remove_all (priv->properties);
1124 }
1125
1126 /**
1127  * gtk_style_properties_merge:
1128  * @props: a #GtkStyleProperties
1129  * @props_to_merge: a second #GtkStyleProperties
1130  * @replace: whether to replace values or not
1131  *
1132  * Merges into @props all the style information contained
1133  * in @props_to_merge. If @replace is %TRUE, the values
1134  * will be overwritten, if it is %FALSE, the older values
1135  * will prevail.
1136  *
1137  * Since: 3.0
1138  **/
1139 void
1140 gtk_style_properties_merge (GtkStyleProperties       *props,
1141                             const GtkStyleProperties *props_to_merge,
1142                             gboolean                  replace)
1143 {
1144   GtkStylePropertiesPrivate *priv, *priv_to_merge;
1145   GHashTableIter iter;
1146   gpointer key, value;
1147
1148   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1149   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props_to_merge));
1150
1151   priv = props->priv;
1152   priv_to_merge = props_to_merge->priv;
1153
1154   /* Merge symbolic color map */
1155   if (priv_to_merge->color_map)
1156     {
1157       g_hash_table_iter_init (&iter, priv_to_merge->color_map);
1158
1159       while (g_hash_table_iter_next (&iter, &key, &value))
1160         {
1161           const gchar *name;
1162           GtkSymbolicColor *color;
1163
1164           name = key;
1165           color = value;
1166
1167           if (!replace &&
1168               g_hash_table_lookup (priv->color_map, name))
1169             continue;
1170
1171           gtk_style_properties_map_color (props, name, color);
1172         }
1173     }
1174
1175   /* Merge symbolic style properties */
1176   g_hash_table_iter_init (&iter, priv_to_merge->properties);
1177
1178   while (g_hash_table_iter_next (&iter, &key, &value))
1179     {
1180       PropertyData *prop_to_merge = value;
1181       PropertyData *prop;
1182       guint i;
1183
1184       prop = g_hash_table_lookup (priv->properties, key);
1185
1186       if (!prop)
1187         {
1188           prop = property_data_new ();
1189           g_hash_table_insert (priv->properties, key, prop);
1190         }
1191
1192       for (i = 0; i < prop_to_merge->values->len; i++)
1193         {
1194           ValueData *data;
1195           GValue *value;
1196
1197           data = &g_array_index (prop_to_merge->values, ValueData, i);
1198           value = property_data_get_value (prop, data->state);
1199
1200           if (G_VALUE_TYPE (&data->value) == PANGO_TYPE_FONT_DESCRIPTION &&
1201               G_IS_VALUE (value))
1202             {
1203               PangoFontDescription *font_desc;
1204               PangoFontDescription *font_desc_to_merge;
1205
1206               /* Handle merging of font descriptions */
1207               font_desc = g_value_get_boxed (value);
1208               font_desc_to_merge = g_value_get_boxed (&data->value);
1209
1210               pango_font_description_merge (font_desc, font_desc_to_merge, replace);
1211             }
1212           else if (replace || !G_IS_VALUE (value))
1213             {
1214               if (!G_IS_VALUE (value))
1215                 g_value_init (value, G_VALUE_TYPE (&data->value));
1216               else if (G_VALUE_TYPE (value) != G_VALUE_TYPE (&data->value))
1217                 {
1218                   g_value_unset (value);
1219                   g_value_init (value, G_VALUE_TYPE (&data->value));
1220                 }
1221
1222               g_value_copy (&data->value, value);
1223             }
1224         }
1225     }
1226 }