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