]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyleproperties.c
bef1fbfc487d1d0e366536d8abb9ecb8d82dff4a
[~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 "gtkcssshorthandpropertyprivate.h"
35 #include "gtkcsstypesprivate.h"
36 #include "gtkborderimageprivate.h"
37
38 #include "gtkprivatetypebuiltins.h"
39 #include "gtkstylepropertyprivate.h"
40 #include "gtkstyleproviderprivate.h"
41 #include "gtkintl.h"
42
43 #include "gtkwin32themeprivate.h"
44
45 /**
46  * SECTION:gtkstyleproperties
47  * @Short_description: Store for style property information
48  * @Title: GtkStyleProperties
49  *
50  * GtkStyleProperties provides the storage for style information
51  * that is used by #GtkStyleContext and other #GtkStyleProvider
52  * implementations.
53  *
54  * Before style properties can be stored in GtkStyleProperties, they
55  * must be registered with gtk_style_properties_register_property().
56  *
57  * Unless you are writing a #GtkStyleProvider implementation, you
58  * are unlikely to use this API directly, as gtk_style_context_get()
59  * and its variants are the preferred way to access styling information
60  * from widget implementations and theming engine implementations
61  * should use the APIs provided by #GtkThemingEngine instead.
62  */
63
64 typedef struct PropertyData PropertyData;
65 typedef struct ValueData ValueData;
66
67 struct ValueData
68 {
69   GtkStateFlags state;
70   GValue value;
71 };
72
73 struct PropertyData
74 {
75   GArray *values;
76 };
77
78 struct _GtkStylePropertiesPrivate
79 {
80   GHashTable *color_map;
81   GHashTable *properties;
82   GtkSymbolicColorLookupFunc color_lookup_func;
83   gpointer color_lookup_data;
84 };
85
86 static void gtk_style_properties_provider_init         (GtkStyleProviderIface            *iface);
87 static void gtk_style_properties_provider_private_init (GtkStyleProviderPrivateInterface *iface);
88 static void gtk_style_properties_finalize              (GObject                          *object);
89
90
91 G_DEFINE_TYPE_EXTENDED (GtkStyleProperties, gtk_style_properties, G_TYPE_OBJECT, 0,
92                         G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
93                                                gtk_style_properties_provider_init)
94                         G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER_PRIVATE,
95                                                gtk_style_properties_provider_private_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   g_type_class_add_private (object_class, sizeof (GtkStylePropertiesPrivate));
105 }
106
107 static PropertyData *
108 property_data_new (void)
109 {
110   PropertyData *data;
111
112   data = g_slice_new0 (PropertyData);
113   data->values = g_array_new (FALSE, FALSE, sizeof (ValueData));
114
115   return data;
116 }
117
118 static void
119 property_data_remove_values (PropertyData *data)
120 {
121   guint i;
122
123   for (i = 0; i < data->values->len; i++)
124     {
125       ValueData *value_data;
126
127       value_data = &g_array_index (data->values, ValueData, i);
128
129       if (G_IS_VALUE (&value_data->value))
130         g_value_unset (&value_data->value);
131     }
132
133   if (data->values->len > 0)
134     g_array_remove_range (data->values, 0, data->values->len);
135 }
136
137 static void
138 property_data_free (PropertyData *data)
139 {
140   property_data_remove_values (data);
141   g_array_free (data->values, TRUE);
142   g_slice_free (PropertyData, data);
143 }
144
145 static gboolean
146 property_data_find_position (PropertyData  *data,
147                              GtkStateFlags  state,
148                              guint         *pos)
149 {
150   gint min, max, mid;
151   gboolean found = FALSE;
152   guint position;
153
154   if (pos)
155     *pos = 0;
156
157   if (data->values->len == 0)
158     return FALSE;
159
160   /* Find position for the given state, or the position where
161    * it would be if not found, the array is ordered by the
162    * state flags.
163    */
164   min = 0;
165   max = data->values->len - 1;
166
167   do
168     {
169       ValueData *value_data;
170
171       mid = (min + max) / 2;
172       value_data = &g_array_index (data->values, ValueData, mid);
173
174       if (value_data->state == state)
175         {
176           found = TRUE;
177           position = mid;
178         }
179       else if (value_data->state < state)
180           position = min = mid + 1;
181       else
182         {
183           max = mid - 1;
184           position = mid;
185         }
186     }
187   while (!found && min <= max);
188
189   if (pos)
190     *pos = position;
191
192   return found;
193 }
194
195 static GValue *
196 property_data_get_value (PropertyData  *data,
197                          GtkStateFlags  state)
198 {
199   ValueData *val_data;
200   guint pos;
201
202   if (!property_data_find_position (data, state, &pos))
203     {
204       ValueData new = { 0 };
205
206       new.state = state;
207       g_array_insert_val (data->values, pos, new);
208     }
209
210   val_data = &g_array_index (data->values, ValueData, pos);
211
212   return &val_data->value;
213 }
214
215 static GValue *
216 property_data_match_state (PropertyData  *data,
217                            GtkStateFlags  state)
218 {
219   guint pos;
220   gint i;
221
222   if (property_data_find_position (data, state, &pos))
223     {
224       ValueData *val_data;
225
226       /* Exact match */
227       val_data = &g_array_index (data->values, ValueData, pos);
228       return &val_data->value;
229     }
230
231   if (pos >= data->values->len)
232     pos = data->values->len - 1;
233
234   /* No exact match, go downwards the list to find
235    * the closest match to the given state flags, as
236    * a side effect, there is an implicit precedence
237    * of higher flags over the smaller ones.
238    */
239   for (i = pos; i >= 0; i--)
240     {
241       ValueData *val_data;
242
243       val_data = &g_array_index (data->values, ValueData, i);
244
245        /* Check whether any of the requested
246         * flags are set, and no other flags are.
247         *
248         * Also, no flags acts as a wildcard, such
249         * value should be always in the first position
250         * in the array (if present) anyways.
251         */
252       if (val_data->state == 0 ||
253           ((val_data->state & state) != 0 &&
254            (val_data->state & ~state) == 0))
255         return &val_data->value;
256     }
257
258   return NULL;
259 }
260
261 static void
262 gtk_style_properties_init (GtkStyleProperties *props)
263 {
264   GtkStylePropertiesPrivate *priv;
265
266   priv = props->priv = G_TYPE_INSTANCE_GET_PRIVATE (props,
267                                                     GTK_TYPE_STYLE_PROPERTIES,
268                                                     GtkStylePropertiesPrivate);
269
270   priv->properties = g_hash_table_new_full (NULL, NULL, NULL,
271                                             (GDestroyNotify) property_data_free);
272 }
273
274 static void
275 gtk_style_properties_finalize (GObject *object)
276 {
277   GtkStylePropertiesPrivate *priv;
278   GtkStyleProperties *props;
279
280   props = GTK_STYLE_PROPERTIES (object);
281   priv = props->priv;
282   g_hash_table_destroy (priv->properties);
283
284   if (priv->color_map)
285     g_hash_table_destroy (priv->color_map);
286
287   G_OBJECT_CLASS (gtk_style_properties_parent_class)->finalize (object);
288 }
289
290 GtkStyleProperties *
291 gtk_style_properties_get_style (GtkStyleProvider *provider,
292                                 GtkWidgetPath    *path)
293 {
294   /* Return style set itself */
295   return g_object_ref (provider);
296 }
297
298 static void
299 gtk_style_properties_provider_init (GtkStyleProviderIface *iface)
300 {
301   iface->get_style = gtk_style_properties_get_style;
302 }
303
304 static GtkSymbolicColor *
305 gtk_style_properties_provider_get_color (GtkStyleProviderPrivate *provider,
306                                          const char              *name)
307 {
308   return gtk_style_properties_lookup_color (GTK_STYLE_PROPERTIES (provider), name);
309 }
310
311 static void
312 gtk_style_properties_provider_lookup (GtkStyleProviderPrivate *provider,
313                                       GtkWidgetPath           *path,
314                                       GtkStateFlags            state,
315                                       GtkCssLookup            *lookup)
316 {
317   GtkStyleProperties *props;
318   GtkStylePropertiesPrivate *priv;
319   GHashTableIter iter;
320   gpointer key, value;
321
322   props = GTK_STYLE_PROPERTIES (provider);
323   priv = props->priv;
324
325   /* Merge symbolic style properties */
326   g_hash_table_iter_init (&iter, priv->properties);
327
328   while (g_hash_table_iter_next (&iter, &key, &value))
329     {
330       GtkCssStyleProperty *prop = key;
331       PropertyData *data = value;
332       const GValue *value;
333       guint id;
334
335       id = _gtk_css_style_property_get_id (prop);
336
337       if (!_gtk_css_lookup_is_missing (lookup, id))
338           continue;
339
340       value = property_data_match_state (data, state);
341       if (value == NULL)
342         continue;
343
344       _gtk_css_lookup_set (lookup, id, value);
345     }
346 }
347
348 static void
349 gtk_style_properties_provider_private_init (GtkStyleProviderPrivateInterface *iface)
350 {
351   iface->get_color = gtk_style_properties_provider_get_color;
352   iface->lookup = gtk_style_properties_provider_lookup;
353 }
354
355 /* GtkStyleProperties methods */
356
357 /**
358  * gtk_style_properties_new:
359  *
360  * Returns a newly created #GtkStyleProperties
361  *
362  * Returns: a new #GtkStyleProperties
363  **/
364 GtkStyleProperties *
365 gtk_style_properties_new (void)
366 {
367   return g_object_new (GTK_TYPE_STYLE_PROPERTIES, NULL);
368 }
369
370 void
371 _gtk_style_properties_set_color_lookup_func (GtkStyleProperties         *props,
372                                              GtkSymbolicColorLookupFunc  func,
373                                              gpointer                    data)
374 {
375   GtkStylePropertiesPrivate *priv;
376
377   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
378   g_return_if_fail (func != NULL);
379
380   priv = props->priv;
381   g_return_if_fail (priv->color_map == NULL);
382
383   priv->color_lookup_func = func;
384   priv->color_lookup_data = data;
385 }
386
387 /**
388  * gtk_style_properties_map_color:
389  * @props: a #GtkStyleProperties
390  * @name: color name
391  * @color: #GtkSymbolicColor to map @name to
392  *
393  * Maps @color so it can be referenced by @name. See
394  * gtk_style_properties_lookup_color()
395  *
396  * Since: 3.0
397  **/
398 void
399 gtk_style_properties_map_color (GtkStyleProperties *props,
400                                 const gchar        *name,
401                                 GtkSymbolicColor   *color)
402 {
403   GtkStylePropertiesPrivate *priv;
404
405   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
406   g_return_if_fail (name != NULL);
407   g_return_if_fail (color != NULL);
408
409   priv = props->priv;
410   g_return_if_fail (priv->color_lookup_func == NULL);
411
412   if (G_UNLIKELY (!priv->color_map))
413     priv->color_map = g_hash_table_new_full (g_str_hash,
414                                              g_str_equal,
415                                              (GDestroyNotify) g_free,
416                                              (GDestroyNotify) gtk_symbolic_color_unref);
417
418   g_hash_table_replace (priv->color_map,
419                         g_strdup (name),
420                         gtk_symbolic_color_ref (color));
421 }
422
423 /**
424  * gtk_style_properties_lookup_color:
425  * @props: a #GtkStyleProperties
426  * @name: color name to lookup
427  *
428  * Returns the symbolic color that is mapped
429  * to @name.
430  *
431  * Returns: (transfer none): The mapped color
432  *
433  * Since: 3.0
434  **/
435 GtkSymbolicColor *
436 gtk_style_properties_lookup_color (GtkStyleProperties *props,
437                                    const gchar        *name)
438 {
439   GtkStylePropertiesPrivate *priv;
440
441   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
442   g_return_val_if_fail (name != NULL, NULL);
443
444   priv = props->priv;
445
446   if (priv->color_lookup_func)
447     return priv->color_lookup_func (priv->color_lookup_data, name);
448
449   if (!priv->color_map)
450     return NULL;
451
452   return g_hash_table_lookup (priv->color_map, name);
453 }
454
455 void
456 _gtk_style_properties_set_property_by_property (GtkStyleProperties  *props,
457                                                 GtkCssStyleProperty *style_prop,
458                                                 GtkStateFlags        state,
459                                                 const GValue        *value)
460 {
461   GtkStylePropertiesPrivate *priv;
462   PropertyData *prop;
463   GValue *val;
464
465   priv = props->priv;
466   prop = g_hash_table_lookup (priv->properties, style_prop);
467
468   if (!prop)
469     {
470       prop = property_data_new ();
471       g_hash_table_insert (priv->properties, (gpointer) style_prop, prop);
472     }
473
474   val = property_data_get_value (prop, state);
475
476   if (G_VALUE_TYPE (val) == G_VALUE_TYPE (value))
477     g_value_reset (val);
478   else
479     {
480       if (G_IS_VALUE (val))
481         g_value_unset (val);
482
483       g_value_init (val, G_VALUE_TYPE (value));
484     }
485
486   g_value_copy (value, val);
487   if (_gtk_style_property_get_value_type (GTK_STYLE_PROPERTY (style_prop)) == G_VALUE_TYPE (value))
488     g_param_value_validate (GTK_STYLE_PROPERTY (style_prop)->pspec, val);
489 }
490
491 /**
492  * gtk_style_properties_set_property:
493  * @props: a #GtkStyleProperties
494  * @property: styling property to set
495  * @state: state to set the value for
496  * @value: new value for the property
497  *
498  * Sets a styling property in @props.
499  *
500  * Since: 3.0
501  **/
502 void
503 gtk_style_properties_set_property (GtkStyleProperties *props,
504                                    const gchar        *property,
505                                    GtkStateFlags       state,
506                                    const GValue       *value)
507 {
508   GtkStyleProperty *node;
509
510   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
511   g_return_if_fail (property != NULL);
512   g_return_if_fail (value != NULL);
513
514   node = _gtk_style_property_lookup (property);
515
516   if (!node)
517     {
518       g_warning ("Style property \"%s\" is not registered", property);
519       return;
520     }
521   if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
522     {
523       g_warning ("Style property \"%s\" is not settable", property);
524       return;
525     }
526   
527   _gtk_style_property_assign (node, props, state, value);
528 }
529
530 /**
531  * gtk_style_properties_set_valist:
532  * @props: a #GtkStyleProperties
533  * @state: state to set the values for
534  * @args: va_list of property name/value pairs, followed by %NULL
535  *
536  * Sets several style properties on @props.
537  *
538  * Since: 3.0
539  **/
540 void
541 gtk_style_properties_set_valist (GtkStyleProperties *props,
542                                  GtkStateFlags       state,
543                                  va_list             args)
544 {
545   const gchar *property_name;
546
547   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
548
549   property_name = va_arg (args, const gchar *);
550
551   while (property_name)
552     {
553       GtkStyleProperty *node;
554       gchar *error = NULL;
555       GType val_type;
556       GValue val = G_VALUE_INIT;
557
558       node = _gtk_style_property_lookup (property_name);
559
560       if (!node)
561         {
562           g_warning ("Style property \"%s\" is not registered", property_name);
563           break;
564         }
565
566       val_type = _gtk_style_property_get_value_type (node);
567       if (val_type == G_TYPE_NONE)
568         {
569           g_warning ("Style property \"%s\" is not settable", property_name);
570           break;
571         }
572
573       G_VALUE_COLLECT_INIT (&val, _gtk_style_property_get_value_type (node),
574                             args, 0, &error);
575       if (error)
576         {
577           g_warning ("Could not set style property \"%s\": %s", property_name, error);
578           g_value_unset (&val);
579           g_free (error);
580           break;
581         }
582
583       _gtk_style_property_assign (node, props, state, &val);
584       g_value_unset (&val);
585
586       property_name = va_arg (args, const gchar *);
587     }
588 }
589
590 /**
591  * gtk_style_properties_set:
592  * @props: a #GtkStyleProperties
593  * @state: state to set the values for
594  * @...: property name/value pairs, followed by %NULL
595  *
596  * Sets several style properties on @props.
597  *
598  * Since: 3.0
599  **/
600 void
601 gtk_style_properties_set (GtkStyleProperties *props,
602                           GtkStateFlags       state,
603                           ...)
604 {
605   va_list args;
606
607   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
608
609   va_start (args, state);
610   gtk_style_properties_set_valist (props, state, args);
611   va_end (args);
612 }
613
614 const GValue *
615 _gtk_style_properties_peek_property (GtkStyleProperties  *props,
616                                      GtkCssStyleProperty *property,
617                                      GtkStateFlags        state)
618 {
619   GtkStylePropertiesPrivate *priv;
620   PropertyData *prop;
621
622   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
623   g_return_val_if_fail (property != NULL, FALSE);
624
625   priv = props->priv;
626   prop = g_hash_table_lookup (priv->properties, property);
627   if (prop == NULL)
628     return NULL;
629
630   return property_data_match_state (prop, state);
631 }
632
633 gboolean
634 _gtk_style_properties_get_property (GtkStyleProperties *props,
635                                     const gchar        *property,
636                                     GtkStateFlags       state,
637                                     GtkStylePropertyContext *context,
638                                     GValue             *value)
639 {
640   GtkStyleProperty *node;
641
642   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
643   g_return_val_if_fail (property != NULL, FALSE);
644   g_return_val_if_fail (value != NULL, FALSE);
645
646   node = _gtk_style_property_lookup (property);
647   if (!node)
648     {
649       g_warning ("Style property \"%s\" is not registered", property);
650       return FALSE;
651     }
652   if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
653     {
654       g_warning ("Style property \"%s\" is not gettable", property);
655       return FALSE;
656     }
657
658   _gtk_style_property_query (node, props, state, context, value);
659   return TRUE;
660 }
661
662 /**
663  * gtk_style_properties_get_property:
664  * @props: a #GtkStyleProperties
665  * @property: style property name
666  * @state: state to retrieve the property value for
667  * @value: (out) (transfer full):  return location for the style property value.
668  *
669  * Gets a style property from @props for the given state. When done with @value,
670  * g_value_unset() needs to be called to free any allocated memory.
671  *
672  * Returns: %TRUE if the property exists in @props, %FALSE otherwise
673  *
674  * Since: 3.0
675  **/
676 gboolean
677 gtk_style_properties_get_property (GtkStyleProperties *props,
678                                    const gchar        *property,
679                                    GtkStateFlags       state,
680                                    GValue             *value)
681 {
682   GtkStylePropertyContext context = { 100, 100};
683
684   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
685   g_return_val_if_fail (property != NULL, FALSE);
686   g_return_val_if_fail (value != NULL, FALSE);
687
688   return _gtk_style_properties_get_property (props,
689                                              property,
690                                              state, &context, value);
691 }
692
693 void
694 _gtk_style_properties_get_valist (GtkStyleProperties *props,
695                                   GtkStateFlags       state,
696                                   GtkStylePropertyContext *context,
697                                   va_list             args)
698 {
699   const gchar *property_name;
700
701   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
702
703   property_name = va_arg (args, const gchar *);
704
705   while (property_name)
706     {
707       gchar *error = NULL;
708       GValue value = G_VALUE_INIT;
709
710       if (!_gtk_style_properties_get_property (props,
711                                                property_name,
712                                                state,
713                                                context,
714                                                &value))
715         break;
716
717       G_VALUE_LCOPY (&value, args, 0, &error);
718       g_value_unset (&value);
719
720       if (error)
721         {
722           g_warning ("Could not get style property \"%s\": %s", property_name, error);
723           g_free (error);
724           break;
725         }
726
727       property_name = va_arg (args, const gchar *);
728     }
729 }
730
731 /**
732  * gtk_style_properties_get_valist:
733  * @props: a #GtkStyleProperties
734  * @state: state to retrieve the property values for
735  * @args: va_list of property name/return location pairs, followed by %NULL
736  *
737  * Retrieves several style property values from @props for a given state.
738  *
739  * Since: 3.0
740  **/
741 void
742 gtk_style_properties_get_valist (GtkStyleProperties *props,
743                                  GtkStateFlags       state,
744                                  va_list             args)
745 {
746   GtkStylePropertyContext context = { 100, 100};
747   
748   return _gtk_style_properties_get_valist (props, state, &context, args);
749 }
750
751 void
752 _gtk_style_properties_get (GtkStyleProperties *props,
753                            GtkStateFlags       state,
754                            GtkStylePropertyContext *context,
755                            ...)
756 {
757   va_list args;
758
759   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
760
761   va_start (args, context);
762   _gtk_style_properties_get_valist (props, state, context, args);
763   va_end (args);
764 }
765
766 /**
767  * gtk_style_properties_get:
768  * @props: a #GtkStyleProperties
769  * @state: state to retrieve the property values for
770  * @...: property name /return value pairs, followed by %NULL
771  *
772  * Retrieves several style property values from @props for a
773  * given state.
774  *
775  * Since: 3.0
776  **/
777 void
778 gtk_style_properties_get (GtkStyleProperties *props,
779                           GtkStateFlags       state,
780                           ...)
781 {
782   va_list args;
783
784   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
785
786   va_start (args, state);
787   gtk_style_properties_get_valist (props, state, args);
788   va_end (args);
789 }
790
791 /**
792  * gtk_style_properties_unset_property:
793  * @props: a #GtkStyleProperties
794  * @property: property to unset
795  * @state: state to unset
796  *
797  * Unsets a style property in @props.
798  *
799  * Since: 3.0
800  **/
801 void
802 gtk_style_properties_unset_property (GtkStyleProperties *props,
803                                      const gchar        *property,
804                                      GtkStateFlags       state)
805 {
806   GtkStylePropertiesPrivate *priv;
807   GtkStyleProperty *node;
808   PropertyData *prop;
809   guint pos;
810
811   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
812   g_return_if_fail (property != NULL);
813
814   node = _gtk_style_property_lookup (property);
815
816   if (!node)
817     {
818       g_warning ("Style property \"%s\" is not registered", property);
819       return;
820     }
821   if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
822     {
823       g_warning ("Style property \"%s\" is not settable", property);
824       return;
825     }
826
827   if (GTK_IS_CSS_SHORTHAND_PROPERTY (node))
828     {
829       GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (node);
830
831       for (pos = 0; pos < _gtk_css_shorthand_property_get_n_subproperties (shorthand); pos++)
832         {
833           GtkCssStyleProperty *sub = _gtk_css_shorthand_property_get_subproperty (shorthand, pos);
834           gtk_style_properties_unset_property (props,
835                                                _gtk_style_property_get_name (GTK_STYLE_PROPERTY (sub)),
836                                                state);
837         }
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 }