]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyleproperties.c
syleprovider: Add a vfunc to get the changes
[~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                                       const GtkCssMatcher     *matcher,
307                                       GtkCssLookup            *lookup)
308 {
309   GtkStyleProperties *props;
310   GtkStylePropertiesPrivate *priv;
311   GHashTableIter iter;
312   gpointer key, value;
313
314   props = GTK_STYLE_PROPERTIES (provider);
315   priv = props->priv;
316
317   /* Merge symbolic style properties */
318   g_hash_table_iter_init (&iter, priv->properties);
319
320   while (g_hash_table_iter_next (&iter, &key, &value))
321     {
322       GtkCssStyleProperty *prop = key;
323       PropertyData *data = value;
324       GtkCssValue *value;
325       guint id;
326
327       id = _gtk_css_style_property_get_id (prop);
328
329       if (!_gtk_css_lookup_is_missing (lookup, id))
330           continue;
331
332       value = property_data_match_state (data, _gtk_css_matcher_get_state (matcher));
333       if (value == NULL)
334         continue;
335
336       _gtk_css_lookup_set_computed (lookup, id, NULL, value);
337     }
338 }
339
340 static GtkCssChange
341 gtk_style_properties_provider_get_change (GtkStyleProviderPrivate *provider,
342                                           const GtkCssMatcher     *matcher)
343 {
344   return GTK_CSS_CHANGE_STATE;
345 }
346
347 static void
348 gtk_style_properties_provider_private_init (GtkStyleProviderPrivateInterface *iface)
349 {
350   iface->get_color = gtk_style_properties_provider_get_color;
351   iface->lookup = gtk_style_properties_provider_lookup;
352   iface->get_change = gtk_style_properties_provider_get_change;
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 /**
371  * gtk_style_properties_map_color:
372  * @props: a #GtkStyleProperties
373  * @name: color name
374  * @color: #GtkSymbolicColor to map @name to
375  *
376  * Maps @color so it can be referenced by @name. See
377  * gtk_style_properties_lookup_color()
378  *
379  * Since: 3.0
380  **/
381 void
382 gtk_style_properties_map_color (GtkStyleProperties *props,
383                                 const gchar        *name,
384                                 GtkSymbolicColor   *color)
385 {
386   GtkStylePropertiesPrivate *priv;
387
388   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
389   g_return_if_fail (name != NULL);
390   g_return_if_fail (color != NULL);
391
392   priv = props->priv;
393
394   if (G_UNLIKELY (!priv->color_map))
395     priv->color_map = g_hash_table_new_full (g_str_hash,
396                                              g_str_equal,
397                                              (GDestroyNotify) g_free,
398                                              (GDestroyNotify) gtk_symbolic_color_unref);
399
400   g_hash_table_replace (priv->color_map,
401                         g_strdup (name),
402                         gtk_symbolic_color_ref (color));
403 }
404
405 /**
406  * gtk_style_properties_lookup_color:
407  * @props: a #GtkStyleProperties
408  * @name: color name to lookup
409  *
410  * Returns the symbolic color that is mapped
411  * to @name.
412  *
413  * Returns: (transfer none): The mapped color
414  *
415  * Since: 3.0
416  **/
417 GtkSymbolicColor *
418 gtk_style_properties_lookup_color (GtkStyleProperties *props,
419                                    const gchar        *name)
420 {
421   GtkStylePropertiesPrivate *priv;
422
423   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
424   g_return_val_if_fail (name != NULL, NULL);
425
426   priv = props->priv;
427
428   if (!priv->color_map)
429     return NULL;
430
431   return g_hash_table_lookup (priv->color_map, name);
432 }
433
434 void
435 _gtk_style_properties_set_property_by_property (GtkStyleProperties  *props,
436                                                 GtkCssStyleProperty *style_prop,
437                                                 GtkStateFlags        state,
438                                                 GtkCssValue         *value)
439 {
440   GtkStylePropertiesPrivate *priv;
441   PropertyData *prop;
442   ValueData *val;
443
444   g_return_if_fail (_gtk_css_value_holds (value, _gtk_css_style_property_get_computed_type (style_prop)));
445
446   priv = props->priv;
447   prop = g_hash_table_lookup (priv->properties, style_prop);
448
449   if (!prop)
450     {
451       prop = property_data_new ();
452       g_hash_table_insert (priv->properties, (gpointer) style_prop, prop);
453     }
454
455   val = property_data_get_value (prop, state);
456
457   _gtk_css_value_unref (val->value);
458   val->value = _gtk_css_value_ref (value);
459 }
460
461 /**
462  * gtk_style_properties_set_property:
463  * @props: a #GtkStyleProperties
464  * @property: styling property to set
465  * @state: state to set the value for
466  * @value: new value for the property
467  *
468  * Sets a styling property in @props.
469  *
470  * Since: 3.0
471  **/
472 void
473 gtk_style_properties_set_property (GtkStyleProperties *props,
474                                    const gchar        *property,
475                                    GtkStateFlags       state,
476                                    const GValue       *value)
477 {
478   GtkStyleProperty *node;
479
480   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
481   g_return_if_fail (property != NULL);
482   g_return_if_fail (value != NULL);
483
484   node = _gtk_style_property_lookup (property);
485
486   if (!node)
487     {
488       g_warning ("Style property \"%s\" is not registered", property);
489       return;
490     }
491   if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
492     {
493       g_warning ("Style property \"%s\" is not settable", property);
494       return;
495     }
496   
497   _gtk_style_property_assign (node, props, state, value);
498 }
499
500 /**
501  * gtk_style_properties_set_valist:
502  * @props: a #GtkStyleProperties
503  * @state: state to set the values for
504  * @args: va_list of property name/value pairs, followed by %NULL
505  *
506  * Sets several style properties on @props.
507  *
508  * Since: 3.0
509  **/
510 void
511 gtk_style_properties_set_valist (GtkStyleProperties *props,
512                                  GtkStateFlags       state,
513                                  va_list             args)
514 {
515   const gchar *property_name;
516
517   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
518
519   property_name = va_arg (args, const gchar *);
520
521   while (property_name)
522     {
523       GtkStyleProperty *node;
524       gchar *error = NULL;
525       GType val_type;
526       GValue val = G_VALUE_INIT;
527
528       node = _gtk_style_property_lookup (property_name);
529
530       if (!node)
531         {
532           g_warning ("Style property \"%s\" is not registered", property_name);
533           break;
534         }
535
536       val_type = _gtk_style_property_get_value_type (node);
537       if (val_type == G_TYPE_NONE)
538         {
539           g_warning ("Style property \"%s\" is not settable", property_name);
540           break;
541         }
542
543       G_VALUE_COLLECT_INIT (&val, _gtk_style_property_get_value_type (node),
544                             args, 0, &error);
545       if (error)
546         {
547           g_warning ("Could not set style property \"%s\": %s", property_name, error);
548           g_value_unset (&val);
549           g_free (error);
550           break;
551         }
552
553       _gtk_style_property_assign (node, props, state, &val);
554       g_value_unset (&val);
555
556       property_name = va_arg (args, const gchar *);
557     }
558 }
559
560 /**
561  * gtk_style_properties_set:
562  * @props: a #GtkStyleProperties
563  * @state: state to set the values for
564  * @...: property name/value pairs, followed by %NULL
565  *
566  * Sets several style properties on @props.
567  *
568  * Since: 3.0
569  **/
570 void
571 gtk_style_properties_set (GtkStyleProperties *props,
572                           GtkStateFlags       state,
573                           ...)
574 {
575   va_list args;
576
577   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
578
579   va_start (args, state);
580   gtk_style_properties_set_valist (props, state, args);
581   va_end (args);
582 }
583
584 GtkCssValue *
585 _gtk_style_properties_peek_property (GtkStyleProperties  *props,
586                                      GtkCssStyleProperty *property,
587                                      GtkStateFlags        state)
588 {
589   GtkStylePropertiesPrivate *priv;
590   PropertyData *prop;
591
592   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
593   g_return_val_if_fail (property != NULL, FALSE);
594
595   priv = props->priv;
596   prop = g_hash_table_lookup (priv->properties, property);
597   if (prop == NULL)
598     return NULL;
599
600   return property_data_match_state (prop, state);
601 }
602
603 typedef struct {
604   GtkStyleProperties *props;
605   GtkStateFlags       state;
606 } StyleQueryData;
607
608 static GtkCssValue *
609 style_query_func (guint    id,
610                   gpointer data)
611 {
612   StyleQueryData *query = data;
613
614   return _gtk_style_properties_peek_property (query->props,
615                                               _gtk_css_style_property_lookup_by_id (id),
616                                               query->state);
617 }
618
619 /**
620  * gtk_style_properties_get_property:
621  * @props: a #GtkStyleProperties
622  * @property: style property name
623  * @state: state to retrieve the property value for
624  * @value: (out) (transfer full):  return location for the style property value.
625  *
626  * Gets a style property from @props for the given state. When done with @value,
627  * g_value_unset() needs to be called to free any allocated memory.
628  *
629  * Returns: %TRUE if the property exists in @props, %FALSE otherwise
630  *
631  * Since: 3.0
632  **/
633 gboolean
634 gtk_style_properties_get_property (GtkStyleProperties *props,
635                                    const gchar        *property,
636                                    GtkStateFlags       state,
637                                    GValue             *value)
638 {
639   StyleQueryData query = { props, state };
640   GtkStyleProperty *node;
641   GtkCssValue *v;
642
643   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
644   g_return_val_if_fail (property != NULL, FALSE);
645   g_return_val_if_fail (value != NULL, FALSE);
646
647   node = _gtk_style_property_lookup (property);
648   if (!node)
649     {
650       g_warning ("Style property \"%s\" is not registered", property);
651       return FALSE;
652     }
653   if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
654     {
655       g_warning ("Style property \"%s\" is not gettable", property);
656       return FALSE;
657     }
658
659   v = _gtk_style_property_query (node,
660                                  style_query_func,
661                                  &query);
662   _gtk_css_value_init_gvalue (v, value);
663   _gtk_css_value_unref (v);
664
665   return TRUE;
666 }
667
668 /**
669  * gtk_style_properties_get_valist:
670  * @props: a #GtkStyleProperties
671  * @state: state to retrieve the property values for
672  * @args: va_list of property name/return location pairs, followed by %NULL
673  *
674  * Retrieves several style property values from @props for a given state.
675  *
676  * Since: 3.0
677  **/
678 void
679 gtk_style_properties_get_valist (GtkStyleProperties *props,
680                                  GtkStateFlags       state,
681                                  va_list             args)
682 {
683   const gchar *property_name;
684
685   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
686
687   property_name = va_arg (args, const gchar *);
688
689   while (property_name)
690     {
691       gchar *error = NULL;
692       GValue value = G_VALUE_INIT;
693
694       if (!gtk_style_properties_get_property (props,
695                                               property_name,
696                                               state,
697                                               &value))
698         break;
699
700       G_VALUE_LCOPY (&value, args, 0, &error);
701       g_value_unset (&value);
702
703       if (error)
704         {
705           g_warning ("Could not get style property \"%s\": %s", property_name, error);
706           g_free (error);
707           break;
708         }
709
710       property_name = va_arg (args, const gchar *);
711     }
712 }
713
714 /**
715  * gtk_style_properties_get:
716  * @props: a #GtkStyleProperties
717  * @state: state to retrieve the property values for
718  * @...: property name /return value pairs, followed by %NULL
719  *
720  * Retrieves several style property values from @props for a
721  * given state.
722  *
723  * Since: 3.0
724  **/
725 void
726 gtk_style_properties_get (GtkStyleProperties *props,
727                           GtkStateFlags       state,
728                           ...)
729 {
730   va_list args;
731
732   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
733
734   va_start (args, state);
735   gtk_style_properties_get_valist (props, state, args);
736   va_end (args);
737 }
738
739 /**
740  * gtk_style_properties_unset_property:
741  * @props: a #GtkStyleProperties
742  * @property: property to unset
743  * @state: state to unset
744  *
745  * Unsets a style property in @props.
746  *
747  * Since: 3.0
748  **/
749 void
750 gtk_style_properties_unset_property (GtkStyleProperties *props,
751                                      const gchar        *property,
752                                      GtkStateFlags       state)
753 {
754   GtkStylePropertiesPrivate *priv;
755   GtkStyleProperty *node;
756   PropertyData *prop;
757   guint pos;
758
759   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
760   g_return_if_fail (property != NULL);
761
762   node = _gtk_style_property_lookup (property);
763
764   if (!node)
765     {
766       g_warning ("Style property \"%s\" is not registered", property);
767       return;
768     }
769   if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
770     {
771       g_warning ("Style property \"%s\" is not settable", property);
772       return;
773     }
774
775   if (GTK_IS_CSS_SHORTHAND_PROPERTY (node))
776     {
777       GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (node);
778
779       for (pos = 0; pos < _gtk_css_shorthand_property_get_n_subproperties (shorthand); pos++)
780         {
781           GtkCssStyleProperty *sub = _gtk_css_shorthand_property_get_subproperty (shorthand, pos);
782           gtk_style_properties_unset_property (props,
783                                                _gtk_style_property_get_name (GTK_STYLE_PROPERTY (sub)),
784                                                state);
785         }
786       return;
787     }
788
789   priv = props->priv;
790   prop = g_hash_table_lookup (priv->properties, node);
791
792   if (!prop)
793     return;
794
795   if (property_data_find_position (prop, state, &pos))
796     {
797       ValueData *data;
798
799       data = &g_array_index (prop->values, ValueData, pos);
800
801       _gtk_css_value_unref (data->value);
802       data->value = NULL;
803
804       g_array_remove_index (prop->values, pos);
805     }
806 }
807
808 /**
809  * gtk_style_properties_clear:
810  * @props: a #GtkStyleProperties
811  *
812  * Clears all style information from @props.
813  **/
814 void
815 gtk_style_properties_clear (GtkStyleProperties *props)
816 {
817   GtkStylePropertiesPrivate *priv;
818
819   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
820
821   priv = props->priv;
822   g_hash_table_remove_all (priv->properties);
823 }
824
825 /**
826  * gtk_style_properties_merge:
827  * @props: a #GtkStyleProperties
828  * @props_to_merge: a second #GtkStyleProperties
829  * @replace: whether to replace values or not
830  *
831  * Merges into @props all the style information contained
832  * in @props_to_merge. If @replace is %TRUE, the values
833  * will be overwritten, if it is %FALSE, the older values
834  * will prevail.
835  *
836  * Since: 3.0
837  **/
838 void
839 gtk_style_properties_merge (GtkStyleProperties       *props,
840                             const GtkStyleProperties *props_to_merge,
841                             gboolean                  replace)
842 {
843   GtkStylePropertiesPrivate *priv, *priv_to_merge;
844   GHashTableIter iter;
845   gpointer key, value;
846
847   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
848   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props_to_merge));
849
850   priv = props->priv;
851   priv_to_merge = props_to_merge->priv;
852
853   /* Merge symbolic color map */
854   if (priv_to_merge->color_map)
855     {
856       g_hash_table_iter_init (&iter, priv_to_merge->color_map);
857
858       while (g_hash_table_iter_next (&iter, &key, &value))
859         {
860           const gchar *name;
861           GtkSymbolicColor *color;
862
863           name = key;
864           color = value;
865
866           if (!replace &&
867               g_hash_table_lookup (priv->color_map, name))
868             continue;
869
870           gtk_style_properties_map_color (props, name, color);
871         }
872     }
873
874   /* Merge symbolic style properties */
875   g_hash_table_iter_init (&iter, priv_to_merge->properties);
876
877   while (g_hash_table_iter_next (&iter, &key, &value))
878     {
879       PropertyData *prop_to_merge = value;
880       PropertyData *prop;
881       guint i;
882
883       prop = g_hash_table_lookup (priv->properties, key);
884
885       if (!prop)
886         {
887           prop = property_data_new ();
888           g_hash_table_insert (priv->properties, key, prop);
889         }
890
891       for (i = 0; i < prop_to_merge->values->len; i++)
892         {
893           ValueData *data;
894           ValueData *value;
895
896           data = &g_array_index (prop_to_merge->values, ValueData, i);
897
898           if (replace && data->state == GTK_STATE_FLAG_NORMAL &&
899               _gtk_css_value_holds (data->value, PANGO_TYPE_FONT_DESCRIPTION))
900             {
901               /* Let normal state override all states
902                * previously set in the original set
903                */
904               property_data_remove_values (prop);
905             }
906
907           value = property_data_get_value (prop, data->state);
908
909           if (_gtk_css_value_holds (data->value, PANGO_TYPE_FONT_DESCRIPTION) &&
910               value->value != NULL)
911             {
912               PangoFontDescription *font_desc;
913               PangoFontDescription *font_desc_to_merge;
914
915               /* Handle merging of font descriptions */
916               font_desc = _gtk_css_value_get_font_description (value->value);
917               font_desc_to_merge = _gtk_css_value_get_font_description (data->value);
918
919               pango_font_description_merge (font_desc, font_desc_to_merge, replace);
920             }
921           else if (_gtk_css_value_holds (data->value, G_TYPE_PTR_ARRAY) &&
922                    value->value != NULL)
923             {
924               GPtrArray *array, *array_to_merge;
925               gint i;
926
927               /* Append the array, mainly thought
928                * for the gtk-key-bindings property
929                */
930               array = _gtk_css_value_get_boxed (value->value);
931               array_to_merge = _gtk_css_value_get_boxed (data->value);
932
933               for (i = 0; i < array_to_merge->len; i++)
934                 g_ptr_array_add (array, g_ptr_array_index (array_to_merge, i));
935             }
936           else if (replace || value->value == NULL)
937             {
938               _gtk_css_value_unref (value->value);
939               value->value = _gtk_css_value_ref (data->value);
940             }
941         }
942     }
943 }