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