]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyleproperties.c
6cf497390cea55c0cca5b43ee2cfc92426441332
[~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 "gtkstylepropertiesprivate.h"
23
24 #include <stdlib.h>
25 #include <gobject/gvaluecollector.h>
26 #include <cairo-gobject.h>
27
28 #include "gtkstyleprovider.h"
29 #include "gtksymboliccolor.h"
30 #include "gtkthemingengine.h"
31 #include "gtkanimationdescription.h"
32 #include "gtkgradient.h"
33 #include "gtkshadowprivate.h"
34 #include "gtkcsstypesprivate.h"
35
36 #include "gtkstylepropertyprivate.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 ValueData ValueData;
61
62 struct ValueData
63 {
64   GtkStateFlags state;
65   GValue value;
66 };
67
68 struct PropertyData
69 {
70   GArray *values;
71 };
72
73 struct GtkStylePropertiesPrivate
74 {
75   GHashTable *color_map;
76   GHashTable *properties;
77 };
78
79 static void gtk_style_properties_provider_init (GtkStyleProviderIface *iface);
80 static void gtk_style_properties_finalize      (GObject      *object);
81
82
83 G_DEFINE_TYPE_EXTENDED (GtkStyleProperties, gtk_style_properties, G_TYPE_OBJECT, 0,
84                         G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
85                                                gtk_style_properties_provider_init));
86
87 static void
88 gtk_style_properties_class_init (GtkStylePropertiesClass *klass)
89 {
90   GObjectClass *object_class = G_OBJECT_CLASS (klass);
91
92   object_class->finalize = gtk_style_properties_finalize;
93
94   g_type_class_add_private (object_class, sizeof (GtkStylePropertiesPrivate));
95 }
96
97 static PropertyData *
98 property_data_new (void)
99 {
100   PropertyData *data;
101
102   data = g_slice_new0 (PropertyData);
103   data->values = g_array_new (FALSE, FALSE, sizeof (ValueData));
104
105   return data;
106 }
107
108 static void
109 property_data_remove_values (PropertyData *data)
110 {
111   guint i;
112
113   for (i = 0; i < data->values->len; i++)
114     {
115       ValueData *value_data;
116
117       value_data = &g_array_index (data->values, ValueData, i);
118
119       if (G_IS_VALUE (&value_data->value))
120         g_value_unset (&value_data->value);
121     }
122
123   if (data->values->len > 0)
124     g_array_remove_range (data->values, 0, data->values->len);
125 }
126
127 static void
128 property_data_free (PropertyData *data)
129 {
130   property_data_remove_values (data);
131   g_array_free (data->values, TRUE);
132   g_slice_free (PropertyData, data);
133 }
134
135 static gboolean
136 property_data_find_position (PropertyData  *data,
137                              GtkStateFlags  state,
138                              guint         *pos)
139 {
140   gint min, max, mid;
141   gboolean found = FALSE;
142   guint position;
143
144   if (pos)
145     *pos = 0;
146
147   if (data->values->len == 0)
148     return FALSE;
149
150   /* Find position for the given state, or the position where
151    * it would be if not found, the array is ordered by the
152    * state flags.
153    */
154   min = 0;
155   max = data->values->len - 1;
156
157   do
158     {
159       ValueData *value_data;
160
161       mid = (min + max) / 2;
162       value_data = &g_array_index (data->values, ValueData, mid);
163
164       if (value_data->state == state)
165         {
166           found = TRUE;
167           position = mid;
168         }
169       else if (value_data->state < state)
170           position = min = mid + 1;
171       else
172         {
173           max = mid - 1;
174           position = mid;
175         }
176     }
177   while (!found && min <= max);
178
179   if (pos)
180     *pos = position;
181
182   return found;
183 }
184
185 static GValue *
186 property_data_get_value (PropertyData  *data,
187                          GtkStateFlags  state)
188 {
189   ValueData *val_data;
190   guint pos;
191
192   if (!property_data_find_position (data, state, &pos))
193     {
194       ValueData new = { 0 };
195
196       new.state = state;
197       g_array_insert_val (data->values, pos, new);
198     }
199
200   val_data = &g_array_index (data->values, ValueData, pos);
201
202   return &val_data->value;
203 }
204
205 static GValue *
206 property_data_match_state (PropertyData  *data,
207                            GtkStateFlags  state)
208 {
209   guint pos;
210   gint i;
211
212   if (property_data_find_position (data, state, &pos))
213     {
214       ValueData *val_data;
215
216       /* Exact match */
217       val_data = &g_array_index (data->values, ValueData, pos);
218       return &val_data->value;
219     }
220
221   if (pos >= data->values->len)
222     pos = data->values->len - 1;
223
224   /* No exact match, go downwards the list to find
225    * the closest match to the given state flags, as
226    * a side effect, there is an implicit precedence
227    * of higher flags over the smaller ones.
228    */
229   for (i = pos; i >= 0; i--)
230     {
231       ValueData *val_data;
232
233       val_data = &g_array_index (data->values, ValueData, i);
234
235        /* Check whether any of the requested
236         * flags are set, and no other flags are.
237         *
238         * Also, no flags acts as a wildcard, such
239         * value should be always in the first position
240         * in the array (if present) anyways.
241         */
242       if (val_data->state == 0 ||
243           ((val_data->state & state) != 0 &&
244            (val_data->state & ~state) == 0))
245         return &val_data->value;
246     }
247
248   return NULL;
249 }
250
251 static void
252 gtk_style_properties_init (GtkStyleProperties *props)
253 {
254   GtkStylePropertiesPrivate *priv;
255
256   priv = props->priv = G_TYPE_INSTANCE_GET_PRIVATE (props,
257                                                     GTK_TYPE_STYLE_PROPERTIES,
258                                                     GtkStylePropertiesPrivate);
259
260   priv->properties = g_hash_table_new_full (NULL, NULL, NULL,
261                                             (GDestroyNotify) property_data_free);
262 }
263
264 static void
265 gtk_style_properties_finalize (GObject *object)
266 {
267   GtkStylePropertiesPrivate *priv;
268   GtkStyleProperties *props;
269
270   props = GTK_STYLE_PROPERTIES (object);
271   priv = props->priv;
272   g_hash_table_destroy (priv->properties);
273
274   if (priv->color_map)
275     g_hash_table_destroy (priv->color_map);
276
277   G_OBJECT_CLASS (gtk_style_properties_parent_class)->finalize (object);
278 }
279
280 GtkStyleProperties *
281 gtk_style_properties_get_style (GtkStyleProvider *provider,
282                                 GtkWidgetPath    *path)
283 {
284   /* Return style set itself */
285   return g_object_ref (provider);
286 }
287
288 static void
289 gtk_style_properties_provider_init (GtkStyleProviderIface *iface)
290 {
291   iface->get_style = gtk_style_properties_get_style;
292 }
293
294 /* Property registration functions */
295
296 /**
297  * gtk_style_properties_register_property: (skip)
298  * @parse_func: parsing function to use, or %NULL
299  * @pspec: the #GParamSpec for the new property
300  *
301  * Registers a property so it can be used in the CSS file format.
302  * This function is the low-level equivalent of
303  * gtk_theming_engine_register_property(), if you are implementing
304  * a theming engine, you want to use that function instead.
305  *
306  * Since: 3.0
307  **/
308 void
309 gtk_style_properties_register_property (GtkStylePropertyParser  parse_func,
310                                         GParamSpec             *pspec)
311 {
312   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
313
314   _gtk_style_property_register (pspec,
315                                 0,
316                                 parse_func,
317                                 NULL,
318                                 NULL,
319                                 NULL,
320                                 NULL,
321                                 NULL);
322 }
323
324 /**
325  * gtk_style_properties_lookup_property: (skip)
326  * @property_name: property name to look up
327  * @parse_func: (out): return location for the parse function
328  * @pspec: (out) (transfer none): return location for the #GParamSpec
329  *
330  * Returns %TRUE if a property has been registered, if @pspec or
331  * @parse_func are not %NULL, the #GParamSpec and parsing function
332  * will be respectively returned.
333  *
334  * Returns: %TRUE if the property is registered, %FALSE otherwise
335  *
336  * Since: 3.0
337  **/
338 gboolean
339 gtk_style_properties_lookup_property (const gchar             *property_name,
340                                       GtkStylePropertyParser  *parse_func,
341                                       GParamSpec             **pspec)
342 {
343   const GtkStyleProperty *node;
344   gboolean found = FALSE;
345
346   g_return_val_if_fail (property_name != NULL, FALSE);
347
348   node = _gtk_style_property_lookup (property_name);
349
350   if (node)
351     {
352       if (pspec)
353         *pspec = node->pspec;
354
355       if (parse_func)
356         *parse_func = node->property_parse_func;
357
358       found = TRUE;
359     }
360
361   return found;
362 }
363
364 /* GtkStyleProperties methods */
365
366 /**
367  * gtk_style_properties_new:
368  *
369  * Returns a newly created #GtkStyleProperties
370  *
371  * Returns: a new #GtkStyleProperties
372  **/
373 GtkStyleProperties *
374 gtk_style_properties_new (void)
375 {
376   return g_object_new (GTK_TYPE_STYLE_PROPERTIES, NULL);
377 }
378
379 /**
380  * gtk_style_properties_map_color:
381  * @props: a #GtkStyleProperties
382  * @name: color name
383  * @color: #GtkSymbolicColor to map @name to
384  *
385  * Maps @color so it can be referenced by @name. See
386  * gtk_style_properties_lookup_color()
387  *
388  * Since: 3.0
389  **/
390 void
391 gtk_style_properties_map_color (GtkStyleProperties *props,
392                                 const gchar        *name,
393                                 GtkSymbolicColor   *color)
394 {
395   GtkStylePropertiesPrivate *priv;
396
397   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
398   g_return_if_fail (name != NULL);
399   g_return_if_fail (color != NULL);
400
401   priv = props->priv;
402
403   if (G_UNLIKELY (!priv->color_map))
404     priv->color_map = g_hash_table_new_full (g_str_hash,
405                                              g_str_equal,
406                                              (GDestroyNotify) g_free,
407                                              (GDestroyNotify) gtk_symbolic_color_unref);
408
409   g_hash_table_replace (priv->color_map,
410                         g_strdup (name),
411                         gtk_symbolic_color_ref (color));
412 }
413
414 /**
415  * gtk_style_properties_lookup_color:
416  * @props: a #GtkStyleProperties
417  * @name: color name to lookup
418  *
419  * Returns the symbolic color that is mapped
420  * to @name.
421  *
422  * Returns: (transfer none): The mapped color
423  *
424  * Since: 3.0
425  **/
426 GtkSymbolicColor *
427 gtk_style_properties_lookup_color (GtkStyleProperties *props,
428                                    const gchar        *name)
429 {
430   GtkStylePropertiesPrivate *priv;
431
432   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
433   g_return_val_if_fail (name != NULL, NULL);
434
435   priv = props->priv;
436
437   if (!priv->color_map)
438     return NULL;
439
440   return g_hash_table_lookup (priv->color_map, name);
441 }
442
443 void
444 _gtk_style_properties_set_property_by_property (GtkStyleProperties     *props,
445                                                 const GtkStyleProperty *style_prop,
446                                                 GtkStateFlags           state,
447                                                 const GValue           *value)
448 {
449   GtkStylePropertiesPrivate *priv;
450   PropertyData *prop;
451   GType value_type;
452   GValue *val;
453
454   value_type = G_VALUE_TYPE (value);
455
456   if (style_prop->pspec->value_type == GDK_TYPE_RGBA ||
457       style_prop->pspec->value_type == GDK_TYPE_COLOR)
458     {
459       /* Allow GtkSymbolicColor as well */
460       g_return_if_fail (value_type == GDK_TYPE_RGBA ||
461                         value_type == GDK_TYPE_COLOR ||
462                         value_type == GTK_TYPE_SYMBOLIC_COLOR);
463     }
464   else if (style_prop->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN)
465     {
466       /* Allow GtkGradient as a substitute */
467       g_return_if_fail (value_type == CAIRO_GOBJECT_TYPE_PATTERN ||
468                         value_type == GTK_TYPE_GRADIENT);
469     }
470   else if (style_prop->pspec->value_type == G_TYPE_INT)
471     {
472       g_return_if_fail (value_type == G_TYPE_INT ||
473                         value_type == GTK_TYPE_CSS_BORDER_RADIUS);
474     }
475   else
476     g_return_if_fail (style_prop->pspec->value_type == value_type);
477
478   if (_gtk_style_property_is_shorthand (style_prop))
479     {
480       GParameter *parameters;
481       guint i, n_parameters;
482
483       parameters = _gtk_style_property_unpack (style_prop, value, &n_parameters);
484
485       for (i = 0; i < n_parameters; i++)
486         {
487           gtk_style_properties_set_property (props,
488                                              parameters[i].name,
489                                              state,
490                                              &parameters[i].value);
491           g_value_unset (&parameters[i].value);
492         }
493       g_free (parameters);
494       return;
495     }
496
497   priv = props->priv;
498   prop = g_hash_table_lookup (priv->properties, style_prop);
499
500   if (!prop)
501     {
502       prop = property_data_new ();
503       g_hash_table_insert (priv->properties, (gpointer) style_prop, prop);
504     }
505
506   val = property_data_get_value (prop, state);
507
508   if (G_VALUE_TYPE (val) == value_type)
509     g_value_reset (val);
510   else
511     {
512       if (G_IS_VALUE (val))
513         g_value_unset (val);
514
515       g_value_init (val, value_type);
516     }
517
518   g_value_copy (value, val);
519   if (style_prop->pspec->value_type == value_type)
520     g_param_value_validate (style_prop->pspec, val);
521 }
522
523 /**
524  * gtk_style_properties_set_property:
525  * @props: a #GtkStyleProperties
526  * @property: styling property to set
527  * @state: state to set the value for
528  * @value: new value for the property
529  *
530  * Sets a styling property in @props.
531  *
532  * Since: 3.0
533  **/
534 void
535 gtk_style_properties_set_property (GtkStyleProperties *props,
536                                    const gchar        *property,
537                                    GtkStateFlags       state,
538                                    const GValue       *value)
539 {
540   const GtkStyleProperty *node;
541
542   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
543   g_return_if_fail (property != NULL);
544   g_return_if_fail (value != NULL);
545
546   node = _gtk_style_property_lookup (property);
547
548   if (!node)
549     {
550       g_warning ("Style property \"%s\" is not registered", property);
551       return;
552     }
553
554   _gtk_style_properties_set_property_by_property (props,
555                                                   node,
556                                                   state,
557                                                   value);
558 }
559
560 /**
561  * gtk_style_properties_set_valist:
562  * @props: a #GtkStyleProperties
563  * @state: state to set the values for
564  * @args: va_list of property name/value pairs, followed by %NULL
565  *
566  * Sets several style properties on @props.
567  *
568  * Since: 3.0
569  **/
570 void
571 gtk_style_properties_set_valist (GtkStyleProperties *props,
572                                  GtkStateFlags       state,
573                                  va_list             args)
574 {
575   const gchar *property_name;
576
577   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
578
579   property_name = va_arg (args, const gchar *);
580
581   while (property_name)
582     {
583       const GtkStyleProperty *node;
584       gchar *error = NULL;
585       GValue val = { 0 };
586
587       node = _gtk_style_property_lookup (property_name);
588
589       if (!node)
590         {
591           g_warning ("Style property \"%s\" is not registered", property_name);
592           break;
593         }
594
595       G_VALUE_COLLECT_INIT (&val, node->pspec->value_type,
596                             args, 0, &error);
597       if (error)
598         {
599           g_warning ("Could not set style property \"%s\": %s", property_name, error);
600           g_value_unset (&val);
601           g_free (error);
602           break;
603         }
604
605       _gtk_style_properties_set_property_by_property (props, node, state, &val);
606       g_value_unset (&val);
607
608       property_name = va_arg (args, const gchar *);
609     }
610 }
611
612 /**
613  * gtk_style_properties_set:
614  * @props: a #GtkStyleProperties
615  * @state: state to set the values for
616  * @...: property name/value pairs, followed by %NULL
617  *
618  * Sets several style properties on @props.
619  *
620  * Since: 3.0
621  **/
622 void
623 gtk_style_properties_set (GtkStyleProperties *props,
624                           GtkStateFlags       state,
625                           ...)
626 {
627   va_list args;
628
629   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
630
631   va_start (args, state);
632   gtk_style_properties_set_valist (props, state, args);
633   va_end (args);
634 }
635
636 /* NB: Will return NULL for shorthands */
637 const GValue *
638 _gtk_style_properties_peek_property (GtkStyleProperties      *props,
639                                      const gchar             *prop_name,
640                                      GtkStateFlags            state,
641                                      const GtkStyleProperty **property)
642 {
643   GtkStylePropertiesPrivate *priv;
644   const GtkStyleProperty *node;
645   PropertyData *prop;
646   GValue *val;
647
648   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
649   g_return_val_if_fail (prop_name != NULL, NULL);
650
651   node = _gtk_style_property_lookup (prop_name);
652   if (property)
653     *property = node;
654
655   if (!node)
656     {
657       g_warning ("Style property \"%s\" is not registered", prop_name);
658       return NULL;
659     }
660
661   priv = props->priv;
662   prop = g_hash_table_lookup (priv->properties, node);
663
664   if (!prop)
665     return NULL;
666
667   val = property_data_match_state (prop, state);
668   if (val == NULL)
669     return NULL;
670   
671   _gtk_style_property_resolve (node, props, val);
672
673   return val;
674 }
675
676 /**
677  * gtk_style_properties_get_property:
678  * @props: a #GtkStyleProperties
679  * @property: style property name
680  * @state: state to retrieve the property value for
681  * @value: (out) (transfer full):  return location for the style property value.
682  *
683  * Gets a style property from @props for the given state. When done with @value,
684  * g_value_unset() needs to be called to free any allocated memory.
685  *
686  * Returns: %TRUE if the property exists in @props, %FALSE otherwise
687  *
688  * Since: 3.0
689  **/
690 gboolean
691 gtk_style_properties_get_property (GtkStyleProperties *props,
692                                    const gchar        *property,
693                                    GtkStateFlags       state,
694                                    GValue             *value)
695 {
696   const GtkStyleProperty *node;
697   const GValue *val;
698
699   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
700   g_return_val_if_fail (property != NULL, FALSE);
701   g_return_val_if_fail (value != NULL, FALSE);
702
703   val = _gtk_style_properties_peek_property (props, property, state, &node);
704
705   if (!node)
706     return FALSE;
707
708   g_value_init (value, node->pspec->value_type);
709
710   if (val)
711     g_value_copy (val, value);
712   else if (_gtk_style_property_is_shorthand (node))
713     _gtk_style_property_pack (node, props, state, value);
714   else
715     _gtk_style_property_default_value (node, props, value);
716
717   return TRUE;
718 }
719
720 /**
721  * gtk_style_properties_get_valist:
722  * @props: a #GtkStyleProperties
723  * @state: state to retrieve the property values for
724  * @args: va_list of property name/return location pairs, followed by %NULL
725  *
726  * Retrieves several style property values from @props for a given state.
727  *
728  * Since: 3.0
729  **/
730 void
731 gtk_style_properties_get_valist (GtkStyleProperties *props,
732                                  GtkStateFlags       state,
733                                  va_list             args)
734 {
735   const gchar *property_name;
736
737   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
738
739   property_name = va_arg (args, const gchar *);
740
741   while (property_name)
742     {
743       const GtkStyleProperty *node;
744       gchar *error = NULL;
745       const GValue *val;
746
747       val = _gtk_style_properties_peek_property (props, property_name, state, &node);
748       if (!node)
749         break;
750
751       if (val)
752         {
753           G_VALUE_LCOPY (val, args, 0, &error);
754         }
755       else if (_gtk_style_property_is_shorthand (node))
756         {
757           GValue packed = { 0 };
758
759           g_value_init (&packed, node->pspec->value_type);
760           _gtk_style_property_pack (node, props, state, &packed);
761           G_VALUE_LCOPY (&packed, args, 0, &error);
762           g_value_unset (&packed);
763         }
764       else
765         {
766           GValue default_value = { 0 };
767
768           g_value_init (&default_value, node->pspec->value_type);
769           _gtk_style_property_default_value (node, props, &default_value);
770           G_VALUE_LCOPY (&default_value, args, 0, &error);
771           g_value_unset (&default_value);
772         }
773
774       if (error)
775         {
776           g_warning ("Could not get style property \"%s\": %s", property_name, error);
777           g_free (error);
778           break;
779         }
780
781       property_name = va_arg (args, const gchar *);
782     }
783 }
784
785 /**
786  * gtk_style_properties_get:
787  * @props: a #GtkStyleProperties
788  * @state: state to retrieve the property values for
789  * @...: property name /return value pairs, followed by %NULL
790  *
791  * Retrieves several style property values from @props for a
792  * given state.
793  *
794  * Since: 3.0
795  **/
796 void
797 gtk_style_properties_get (GtkStyleProperties *props,
798                           GtkStateFlags       state,
799                           ...)
800 {
801   va_list args;
802
803   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
804
805   va_start (args, state);
806   gtk_style_properties_get_valist (props, state, args);
807   va_end (args);
808 }
809
810 /**
811  * gtk_style_properties_unset_property:
812  * @props: a #GtkStyleProperties
813  * @property: property to unset
814  * @state: state to unset
815  *
816  * Unsets a style property in @props.
817  *
818  * Since: 3.0
819  **/
820 void
821 gtk_style_properties_unset_property (GtkStyleProperties *props,
822                                      const gchar        *property,
823                                      GtkStateFlags       state)
824 {
825   GtkStylePropertiesPrivate *priv;
826   const GtkStyleProperty *node;
827   PropertyData *prop;
828   guint pos;
829
830   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
831   g_return_if_fail (property != NULL);
832
833   node = _gtk_style_property_lookup (property);
834
835   if (!node)
836     {
837       g_warning ("Style property \"%s\" is not registered", property);
838       return;
839     }
840
841   priv = props->priv;
842   prop = g_hash_table_lookup (priv->properties, node);
843
844   if (!prop)
845     return;
846
847   if (property_data_find_position (prop, state, &pos))
848     {
849       ValueData *data;
850
851       data = &g_array_index (prop->values, ValueData, pos);
852
853       if (G_IS_VALUE (&data->value))
854         g_value_unset (&data->value);
855
856       g_array_remove_index (prop->values, pos);
857     }
858 }
859
860 /**
861  * gtk_style_properties_clear:
862  * @props: a #GtkStyleProperties
863  *
864  * Clears all style information from @props.
865  **/
866 void
867 gtk_style_properties_clear (GtkStyleProperties *props)
868 {
869   GtkStylePropertiesPrivate *priv;
870
871   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
872
873   priv = props->priv;
874   g_hash_table_remove_all (priv->properties);
875 }
876
877 /**
878  * gtk_style_properties_merge:
879  * @props: a #GtkStyleProperties
880  * @props_to_merge: a second #GtkStyleProperties
881  * @replace: whether to replace values or not
882  *
883  * Merges into @props all the style information contained
884  * in @props_to_merge. If @replace is %TRUE, the values
885  * will be overwritten, if it is %FALSE, the older values
886  * will prevail.
887  *
888  * Since: 3.0
889  **/
890 void
891 gtk_style_properties_merge (GtkStyleProperties       *props,
892                             const GtkStyleProperties *props_to_merge,
893                             gboolean                  replace)
894 {
895   GtkStylePropertiesPrivate *priv, *priv_to_merge;
896   GHashTableIter iter;
897   gpointer key, value;
898
899   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
900   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props_to_merge));
901
902   priv = props->priv;
903   priv_to_merge = props_to_merge->priv;
904
905   /* Merge symbolic color map */
906   if (priv_to_merge->color_map)
907     {
908       g_hash_table_iter_init (&iter, priv_to_merge->color_map);
909
910       while (g_hash_table_iter_next (&iter, &key, &value))
911         {
912           const gchar *name;
913           GtkSymbolicColor *color;
914
915           name = key;
916           color = value;
917
918           if (!replace &&
919               g_hash_table_lookup (priv->color_map, name))
920             continue;
921
922           gtk_style_properties_map_color (props, name, color);
923         }
924     }
925
926   /* Merge symbolic style properties */
927   g_hash_table_iter_init (&iter, priv_to_merge->properties);
928
929   while (g_hash_table_iter_next (&iter, &key, &value))
930     {
931       PropertyData *prop_to_merge = value;
932       PropertyData *prop;
933       guint i;
934
935       prop = g_hash_table_lookup (priv->properties, key);
936
937       if (!prop)
938         {
939           prop = property_data_new ();
940           g_hash_table_insert (priv->properties, key, prop);
941         }
942
943       for (i = 0; i < prop_to_merge->values->len; i++)
944         {
945           ValueData *data;
946           GValue *value;
947
948           data = &g_array_index (prop_to_merge->values, ValueData, i);
949
950           if (replace && data->state == GTK_STATE_FLAG_NORMAL &&
951               G_VALUE_TYPE (&data->value) != PANGO_TYPE_FONT_DESCRIPTION)
952             {
953               /* Let normal state override all states
954                * previously set in the original set
955                */
956               property_data_remove_values (prop);
957             }
958
959           value = property_data_get_value (prop, data->state);
960
961           if (G_VALUE_TYPE (&data->value) == PANGO_TYPE_FONT_DESCRIPTION &&
962               G_IS_VALUE (value))
963             {
964               PangoFontDescription *font_desc;
965               PangoFontDescription *font_desc_to_merge;
966
967               /* Handle merging of font descriptions */
968               font_desc = g_value_get_boxed (value);
969               font_desc_to_merge = g_value_get_boxed (&data->value);
970
971               pango_font_description_merge (font_desc, font_desc_to_merge, replace);
972             }
973           else if (G_VALUE_TYPE (&data->value) == G_TYPE_PTR_ARRAY &&
974                    G_IS_VALUE (value))
975             {
976               GPtrArray *array, *array_to_merge;
977               gint i;
978
979               /* Append the array, mainly thought
980                * for the gtk-key-bindings property
981                */
982               array = g_value_get_boxed (value);
983               array_to_merge = g_value_get_boxed (&data->value);
984
985               for (i = 0; i < array_to_merge->len; i++)
986                 g_ptr_array_add (array, g_ptr_array_index (array_to_merge, i));
987             }
988           else if (replace || !G_IS_VALUE (value))
989             {
990               if (!G_IS_VALUE (value))
991                 g_value_init (value, G_VALUE_TYPE (&data->value));
992               else if (G_VALUE_TYPE (value) != G_VALUE_TYPE (&data->value))
993                 {
994                   g_value_unset (value);
995                   g_value_init (value, G_VALUE_TYPE (&data->value));
996                 }
997
998               g_value_copy (&data->value, value);
999             }
1000         }
1001     }
1002 }