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