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