1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
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.
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.
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.
22 #include "gtkstyleproperties.h"
25 #include <gobject/gvaluecollector.h>
26 #include <cairo-gobject.h>
28 #include "gtktypebuiltins.h"
29 #include "gtkstyleprovider.h"
30 #include "gtksymboliccolor.h"
31 #include "gtkprivate.h"
32 #include "gtkthemingengine.h"
33 #include "gtkanimationdescription.h"
34 #include "gtkborder.h"
35 #include "gtkgradient.h"
36 #include "gtk9slice.h"
40 * SECTION:gtkstyleproperties
41 * @Short_description: Store for style property information
42 * @Title: GtkStyleProperties
44 * GtkStyleProperties provides the storage for style information
45 * that is used by #GtkStyleContext and other #GtkStyleProvider
48 * Before style properties can be stored in GtkStyleProperties, they
49 * must be registered with gtk_style_properties_register_property().
51 * Unless you are writing a #GtkStyleProvider implementation, you
52 * are unlikely to use this API directly, as gtk_style_context_get()
53 * and its variants are the preferred way to access styling information
54 * from widget implementations and theming engine implementations
55 * should use the APIs provided by #GtkThemingEngine instead.
58 typedef struct GtkStylePropertiesPrivate GtkStylePropertiesPrivate;
59 typedef struct PropertyData PropertyData;
60 typedef struct PropertyNode PropertyNode;
61 typedef struct ValueData ValueData;
65 GQuark property_quark;
67 GtkStylePropertyParser parse_func;
81 struct GtkStylePropertiesPrivate
83 GHashTable *color_map;
84 GHashTable *properties;
87 static GArray *properties = NULL;
89 static void gtk_style_properties_provider_init (GtkStyleProviderIface *iface);
90 static void gtk_style_properties_finalize (GObject *object);
93 G_DEFINE_TYPE_EXTENDED (GtkStyleProperties, gtk_style_properties, G_TYPE_OBJECT, 0,
94 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
95 gtk_style_properties_provider_init));
98 gtk_style_properties_class_init (GtkStylePropertiesClass *klass)
100 GObjectClass *object_class = G_OBJECT_CLASS (klass);
102 object_class->finalize = gtk_style_properties_finalize;
104 /* Initialize default property set */
105 gtk_style_properties_register_property (NULL,
106 g_param_spec_boxed ("color",
110 gtk_style_properties_register_property (NULL,
111 g_param_spec_boxed ("background-color",
116 gtk_style_properties_register_property (NULL,
117 g_param_spec_boxed ("font",
120 PANGO_TYPE_FONT_DESCRIPTION, 0));
122 gtk_style_properties_register_property (NULL,
123 g_param_spec_boxed ("margin",
126 GTK_TYPE_BORDER, 0));
127 gtk_style_properties_register_property (NULL,
128 g_param_spec_boxed ("padding",
131 GTK_TYPE_BORDER, 0));
132 gtk_style_properties_register_property (NULL,
133 g_param_spec_boxed ("border-width",
135 "Border width, in pixels",
136 GTK_TYPE_BORDER, 0));
137 gtk_style_properties_register_property (NULL,
138 g_param_spec_int ("border-radius",
140 "Border radius, in pixels",
142 gtk_style_properties_register_property (NULL,
143 g_param_spec_enum ("border-style",
146 GTK_TYPE_BORDER_STYLE,
147 GTK_BORDER_STYLE_NONE, 0));
148 gtk_style_properties_register_property (NULL,
149 g_param_spec_boxed ("border-color",
153 gtk_style_properties_register_property (NULL,
154 g_param_spec_boxed ("background-image",
157 CAIRO_GOBJECT_TYPE_PATTERN, 0));
158 gtk_style_properties_register_property (NULL,
159 g_param_spec_boxed ("border-image",
162 GTK_TYPE_9SLICE, 0));
163 gtk_style_properties_register_property (NULL,
164 g_param_spec_object ("engine",
167 GTK_TYPE_THEMING_ENGINE, 0));
168 gtk_style_properties_register_property (NULL,
169 g_param_spec_boxed ("transition",
170 "Transition animation description",
171 "Transition animation description",
172 GTK_TYPE_ANIMATION_DESCRIPTION, 0));
174 /* Private property holding the binding sets */
175 gtk_style_properties_register_property (NULL,
176 g_param_spec_boxed ("gtk-key-bindings",
179 G_TYPE_PTR_ARRAY, 0));
181 g_type_class_add_private (object_class, sizeof (GtkStylePropertiesPrivate));
184 static PropertyData *
185 property_data_new (void)
189 data = g_slice_new0 (PropertyData);
190 data->values = g_array_new (FALSE, FALSE, sizeof (ValueData));
196 property_data_remove_values (PropertyData *data)
200 for (i = 0; i < data->values->len; i++)
202 ValueData *value_data;
204 value_data = &g_array_index (data->values, ValueData, i);
206 if (G_IS_VALUE (&value_data->value))
207 g_value_unset (&value_data->value);
210 if (data->values->len > 0)
211 g_array_remove_range (data->values, 0, data->values->len);
215 property_data_free (PropertyData *data)
217 property_data_remove_values (data);
218 g_array_free (data->values, TRUE);
219 g_slice_free (PropertyData, data);
223 property_data_find_position (PropertyData *data,
228 gboolean found = FALSE;
234 if (data->values->len == 0)
237 /* Find position for the given state, or the position where
238 * it would be if not found, the array is ordered by the
242 max = data->values->len - 1;
246 ValueData *value_data;
248 mid = (min + max) / 2;
249 value_data = &g_array_index (data->values, ValueData, mid);
251 if (value_data->state == state)
256 else if (value_data->state < state)
257 position = min = mid + 1;
264 while (!found && min <= max);
273 property_data_get_value (PropertyData *data,
279 if (!property_data_find_position (data, state, &pos))
281 ValueData new = { 0 };
284 g_array_insert_val (data->values, pos, new);
287 val_data = &g_array_index (data->values, ValueData, pos);
289 return &val_data->value;
293 property_data_match_state (PropertyData *data,
299 if (property_data_find_position (data, state, &pos))
304 val_data = &g_array_index (data->values, ValueData, pos);
305 return &val_data->value;
308 if (pos >= data->values->len)
309 pos = data->values->len - 1;
311 /* No exact match, go downwards the list to find
312 * the closest match to the given state flags, as
313 * a side effect, there is an implicit precedence
314 * of higher flags over the smaller ones.
316 for (i = pos; i >= 0; i--)
320 val_data = &g_array_index (data->values, ValueData, i);
322 /* Check whether any of the requested
323 * flags are set, and no other flags are.
325 * Also, no flags acts as a wildcard, such
326 * value should be always in the first position
327 * in the array (if present) anyways.
329 if (val_data->state == 0 ||
330 ((val_data->state & state) != 0 &&
331 (val_data->state & ~state) == 0))
332 return &val_data->value;
339 gtk_style_properties_init (GtkStyleProperties *props)
341 GtkStylePropertiesPrivate *priv;
343 priv = props->priv = G_TYPE_INSTANCE_GET_PRIVATE (props,
344 GTK_TYPE_STYLE_PROPERTIES,
345 GtkStylePropertiesPrivate);
347 priv->properties = g_hash_table_new_full (NULL, NULL, NULL,
348 (GDestroyNotify) property_data_free);
352 gtk_style_properties_finalize (GObject *object)
354 GtkStylePropertiesPrivate *priv;
355 GtkStyleProperties *props;
357 props = GTK_STYLE_PROPERTIES (object);
359 g_hash_table_destroy (priv->properties);
362 g_hash_table_destroy (priv->color_map);
364 G_OBJECT_CLASS (gtk_style_properties_parent_class)->finalize (object);
368 gtk_style_properties_get_style (GtkStyleProvider *provider,
371 /* Return style set itself */
372 return g_object_ref (provider);
376 gtk_style_properties_provider_init (GtkStyleProviderIface *iface)
378 iface->get_style = gtk_style_properties_get_style;
382 compare_property (gconstpointer p1,
385 PropertyNode *key = (PropertyNode *) p1;
386 PropertyNode *node = (PropertyNode *) p2;
388 if (key->property_quark > node->property_quark)
390 else if (key->property_quark < node->property_quark)
396 static PropertyNode *
397 property_node_lookup (GQuark quark)
399 PropertyNode key = { 0 };
407 key.property_quark = quark;
409 return bsearch (&key, properties->data, properties->len,
410 sizeof (PropertyNode), compare_property);
413 /* Property registration functions */
416 * gtk_style_properties_register_property: (skip)
417 * @parse_func: parsing function to use, or %NULL
418 * @pspec: the #GParamSpec for the new property
420 * Registers a property so it can be used in the CSS file format.
421 * This function is the low-level equivalent of
422 * gtk_theming_engine_register_property(), if you are implementing
423 * a theming engine, you want to use that function instead.
428 gtk_style_properties_register_property (GtkStylePropertyParser parse_func,
431 PropertyNode *node, new = { 0 };
435 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
437 if (G_UNLIKELY (!properties))
438 properties = g_array_new (FALSE, TRUE, sizeof (PropertyNode));
440 quark = g_quark_from_string (pspec->name);
442 if ((node = property_node_lookup (quark)) != NULL)
444 g_warning ("Property \"%s\" was already registered with type %s",
445 pspec->name, g_type_name (node->pspec->value_type));
449 new.property_quark = quark;
453 new.parse_func = parse_func;
455 for (i = 0; i < properties->len; i++)
457 node = &g_array_index (properties, PropertyNode, i);
459 if (node->property_quark > quark)
463 g_array_insert_val (properties, i, new);
467 * gtk_style_properties_lookup_property: (skip)
468 * @property_name: property name to look up
469 * @parse_func: (out): return location for the parse function
470 * @pspec: (out) (transfer none): return location for the #GParamSpec
472 * Returns %TRUE if a property has been registered, if @pspec or
473 * @parse_func are not %NULL, the #GParamSpec and parsing function
474 * will be respectively returned.
476 * Returns: %TRUE if the property is registered, %FALSE otherwise
481 gtk_style_properties_lookup_property (const gchar *property_name,
482 GtkStylePropertyParser *parse_func,
486 GtkStylePropertiesClass *klass;
487 gboolean found = FALSE;
491 g_return_val_if_fail (property_name != NULL, FALSE);
493 klass = g_type_class_ref (GTK_TYPE_STYLE_PROPERTIES);
494 quark = g_quark_try_string (property_name);
498 g_type_class_unref (klass);
502 for (i = 0; i < properties->len; i++)
504 node = &g_array_index (properties, PropertyNode, i);
506 if (node->property_quark == quark)
509 *pspec = node->pspec;
512 *parse_func = node->parse_func;
517 else if (node->property_quark > quark)
521 g_type_class_unref (klass);
526 /* GtkStyleProperties methods */
529 * gtk_style_properties_new:
531 * Returns a newly created #GtkStyleProperties
533 * Returns: a new #GtkStyleProperties
536 gtk_style_properties_new (void)
538 return g_object_new (GTK_TYPE_STYLE_PROPERTIES, NULL);
542 * gtk_style_properties_map_color:
543 * @props: a #GtkStyleProperties
545 * @color: #GtkSymbolicColor to map @name to
547 * Maps @color so it can be referenced by @name. See
548 * gtk_style_properties_lookup_color()
553 gtk_style_properties_map_color (GtkStyleProperties *props,
555 GtkSymbolicColor *color)
557 GtkStylePropertiesPrivate *priv;
559 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
560 g_return_if_fail (name != NULL);
561 g_return_if_fail (color != NULL);
565 if (G_UNLIKELY (!priv->color_map))
566 priv->color_map = g_hash_table_new_full (g_str_hash,
568 (GDestroyNotify) g_free,
569 (GDestroyNotify) gtk_symbolic_color_unref);
571 g_hash_table_replace (priv->color_map,
573 gtk_symbolic_color_ref (color));
577 * gtk_style_properties_lookup_color:
578 * @props: a #GtkStyleProperties
579 * @name: color name to lookup
581 * Returns the symbolic color that is mapped
584 * Returns: (transfer none): The mapped color
589 gtk_style_properties_lookup_color (GtkStyleProperties *props,
592 GtkStylePropertiesPrivate *priv;
594 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
595 g_return_val_if_fail (name != NULL, NULL);
599 if (!priv->color_map)
602 return g_hash_table_lookup (priv->color_map, name);
606 * gtk_style_properties_set_property:
607 * @props: a #GtkStyleProperties
608 * @property: styling property to set
609 * @state: state to set the value for
610 * @value: new value for the property
612 * Sets a styling property in @props.
617 gtk_style_properties_set_property (GtkStyleProperties *props,
618 const gchar *property,
622 GtkStylePropertiesPrivate *priv;
628 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
629 g_return_if_fail (property != NULL);
630 g_return_if_fail (value != NULL);
632 value_type = G_VALUE_TYPE (value);
633 node = property_node_lookup (g_quark_try_string (property));
637 g_warning ("Style property \"%s\" is not registered", property);
641 if (node->pspec->value_type == GDK_TYPE_RGBA ||
642 node->pspec->value_type == GDK_TYPE_COLOR)
644 /* Allow GtkSymbolicColor as well */
645 g_return_if_fail (value_type == GDK_TYPE_RGBA ||
646 value_type == GDK_TYPE_COLOR ||
647 value_type == GTK_TYPE_SYMBOLIC_COLOR);
649 else if (node->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN)
651 /* Allow GtkGradient as a substitute */
652 g_return_if_fail (value_type == CAIRO_GOBJECT_TYPE_PATTERN ||
653 value_type == GTK_TYPE_GRADIENT);
656 g_return_if_fail (node->pspec->value_type == value_type);
659 prop = g_hash_table_lookup (priv->properties,
660 GINT_TO_POINTER (node->property_quark));
664 prop = property_data_new ();
665 g_hash_table_insert (priv->properties,
666 GINT_TO_POINTER (node->property_quark),
670 val = property_data_get_value (prop, state);
672 if (G_VALUE_TYPE (val) == value_type)
676 if (G_IS_VALUE (val))
679 g_value_init (val, value_type);
682 g_value_copy (value, val);
686 * gtk_style_properties_set_valist:
687 * @props: a #GtkStyleProperties
688 * @state: state to set the values for
689 * @args: va_list of property name/value pairs, followed by %NULL
691 * Sets several style properties on @props.
696 gtk_style_properties_set_valist (GtkStyleProperties *props,
700 GtkStylePropertiesPrivate *priv;
701 const gchar *property_name;
703 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
706 property_name = va_arg (args, const gchar *);
708 while (property_name)
715 node = property_node_lookup (g_quark_try_string (property_name));
719 g_warning ("Style property \"%s\" is not registered", property_name);
723 prop = g_hash_table_lookup (priv->properties,
724 GINT_TO_POINTER (node->property_quark));
728 prop = property_data_new ();
729 g_hash_table_insert (priv->properties,
730 GINT_TO_POINTER (node->property_quark),
734 val = property_data_get_value (prop, state);
736 if (G_IS_VALUE (val))
739 G_VALUE_COLLECT_INIT (val, node->pspec->value_type,
743 g_warning ("Could not set style property \"%s\": %s", property_name, error);
749 property_name = va_arg (args, const gchar *);
754 * gtk_style_properties_set:
755 * @props: a #GtkStyleProperties
756 * @state: state to set the values for
757 * @...: property name/value pairs, followed by %NULL
759 * Sets several style properties on @props.
764 gtk_style_properties_set (GtkStyleProperties *props,
770 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
772 va_start (args, state);
773 gtk_style_properties_set_valist (props, state, args);
778 resolve_color (GtkStyleProperties *props,
783 /* Resolve symbolic color to GdkRGBA */
784 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
787 /* Store it back, this is where GdkRGBA caching happens */
788 g_value_unset (value);
789 g_value_init (value, GDK_TYPE_RGBA);
790 g_value_set_boxed (value, &color);
796 resolve_color_rgb (GtkStyleProperties *props,
799 GdkColor color = { 0 };
802 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
805 color.red = rgba.red * 65535. + 0.5;
806 color.green = rgba.green * 65535. + 0.5;
807 color.blue = rgba.blue * 65535. + 0.5;
809 g_value_unset (value);
810 g_value_init (value, GDK_TYPE_COLOR);
811 g_value_set_boxed (value, &color);
817 resolve_gradient (GtkStyleProperties *props,
820 cairo_pattern_t *gradient;
822 if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
825 /* Store it back, this is where cairo_pattern_t caching happens */
826 g_value_unset (value);
827 g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
828 g_value_take_boxed (value, gradient);
834 style_properties_resolve_type (GtkStyleProperties *props,
838 if (val && G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
840 if (node->pspec->value_type == GDK_TYPE_RGBA)
842 if (!resolve_color (props, val))
845 else if (node->pspec->value_type == GDK_TYPE_COLOR)
847 if (!resolve_color_rgb (props, val))
853 else if (val && G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
855 g_return_val_if_fail (node->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN, FALSE);
857 if (!resolve_gradient (props, val))
865 lookup_default_value (PropertyNode *node,
868 if (node->pspec->value_type == GTK_TYPE_THEMING_ENGINE)
869 g_value_set_object (value, gtk_theming_engine_load (NULL));
870 else if (node->pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
871 g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
872 else if (node->pspec->value_type == GDK_TYPE_RGBA)
875 gdk_rgba_parse (&color, "pink");
876 g_value_set_boxed (value, &color);
878 else if (node->pspec->value_type == GTK_TYPE_BORDER)
880 g_value_take_boxed (value, gtk_border_new ());
883 g_param_value_set_default (node->pspec, value);
887 _gtk_style_properties_peek_property (GtkStyleProperties *props,
888 const gchar *prop_name,
891 GtkStylePropertiesPrivate *priv;
896 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
897 g_return_val_if_fail (prop_name != NULL, NULL);
899 node = property_node_lookup (g_quark_try_string (prop_name));
903 g_warning ("Style property \"%s\" is not registered", prop_name);
908 prop = g_hash_table_lookup (priv->properties,
909 GINT_TO_POINTER (node->property_quark));
914 val = property_data_match_state (prop, state);
917 !style_properties_resolve_type (props, node, val))
924 * gtk_style_properties_get_property:
925 * @props: a #GtkStyleProperties
926 * @property: style property name
927 * @state: state to retrieve the property value for
928 * @value: (out) (transfer full): return location for the style property value.
930 * Gets a style property from @props for the given state. When done with @value,
931 * g_value_unset() needs to be called to free any allocated memory.
933 * Returns: %TRUE if the property exists in @props, %FALSE otherwise
938 gtk_style_properties_get_property (GtkStyleProperties *props,
939 const gchar *property,
943 GtkStylePropertiesPrivate *priv;
948 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
949 g_return_val_if_fail (property != NULL, FALSE);
950 g_return_val_if_fail (value != NULL, FALSE);
952 node = property_node_lookup (g_quark_try_string (property));
956 g_warning ("Style property \"%s\" is not registered", property);
961 prop = g_hash_table_lookup (priv->properties,
962 GINT_TO_POINTER (node->property_quark));
967 g_value_init (value, node->pspec->value_type);
968 val = property_data_match_state (prop, state);
971 !style_properties_resolve_type (props, node, val))
976 g_param_value_validate (node->pspec, val);
977 g_value_copy (val, value);
980 lookup_default_value (node, value);
986 * gtk_style_properties_get_valist:
987 * @props: a #GtkStyleProperties
988 * @state: state to retrieve the property values for
989 * @args: va_list of property name/return location pairs, followed by %NULL
991 * Retrieves several style property values from @props for a given state.
996 gtk_style_properties_get_valist (GtkStyleProperties *props,
1000 GtkStylePropertiesPrivate *priv;
1001 const gchar *property_name;
1003 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1006 property_name = va_arg (args, const gchar *);
1008 while (property_name)
1012 gchar *error = NULL;
1015 node = property_node_lookup (g_quark_try_string (property_name));
1019 g_warning ("Style property \"%s\" is not registered", property_name);
1023 prop = g_hash_table_lookup (priv->properties,
1024 GINT_TO_POINTER (node->property_quark));
1027 val = property_data_match_state (prop, state);
1030 !style_properties_resolve_type (props, node, val))
1035 g_param_value_validate (node->pspec, val);
1036 G_VALUE_LCOPY (val, args, 0, &error);
1040 GValue default_value = { 0 };
1042 g_value_init (&default_value, node->pspec->value_type);
1043 lookup_default_value (node, &default_value);
1044 G_VALUE_LCOPY (&default_value, args, 0, &error);
1045 g_value_unset (&default_value);
1050 g_warning ("Could not get style property \"%s\": %s", property_name, error);
1055 property_name = va_arg (args, const gchar *);
1060 * gtk_style_properties_get:
1061 * @props: a #GtkStyleProperties
1062 * @state: state to retrieve the property values for
1063 * @...: property name /return value pairs, followed by %NULL
1065 * Retrieves several style property values from @props for a
1071 gtk_style_properties_get (GtkStyleProperties *props,
1072 GtkStateFlags state,
1077 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1079 va_start (args, state);
1080 gtk_style_properties_get_valist (props, state, args);
1085 * gtk_style_properties_unset_property:
1086 * @props: a #GtkStyleProperties
1087 * @property: property to unset
1088 * @state: state to unset
1090 * Unsets a style property in @props.
1095 gtk_style_properties_unset_property (GtkStyleProperties *props,
1096 const gchar *property,
1097 GtkStateFlags state)
1099 GtkStylePropertiesPrivate *priv;
1104 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1105 g_return_if_fail (property != NULL);
1107 node = property_node_lookup (g_quark_try_string (property));
1111 g_warning ("Style property \"%s\" is not registered", property);
1116 prop = g_hash_table_lookup (priv->properties,
1117 GINT_TO_POINTER (node->property_quark));
1122 if (property_data_find_position (prop, state, &pos))
1126 data = &g_array_index (prop->values, ValueData, pos);
1128 if (G_IS_VALUE (&data->value))
1129 g_value_unset (&data->value);
1131 g_array_remove_index (prop->values, pos);
1136 * gtk_style_properties_clear:
1137 * @props: a #GtkStyleProperties
1139 * Clears all style information from @props.
1142 gtk_style_properties_clear (GtkStyleProperties *props)
1144 GtkStylePropertiesPrivate *priv;
1146 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1149 g_hash_table_remove_all (priv->properties);
1153 * gtk_style_properties_merge:
1154 * @props: a #GtkStyleProperties
1155 * @props_to_merge: a second #GtkStyleProperties
1156 * @replace: whether to replace values or not
1158 * Merges into @props all the style information contained
1159 * in @props_to_merge. If @replace is %TRUE, the values
1160 * will be overwritten, if it is %FALSE, the older values
1166 gtk_style_properties_merge (GtkStyleProperties *props,
1167 const GtkStyleProperties *props_to_merge,
1170 GtkStylePropertiesPrivate *priv, *priv_to_merge;
1171 GHashTableIter iter;
1172 gpointer key, value;
1174 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1175 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props_to_merge));
1178 priv_to_merge = props_to_merge->priv;
1180 /* Merge symbolic color map */
1181 if (priv_to_merge->color_map)
1183 g_hash_table_iter_init (&iter, priv_to_merge->color_map);
1185 while (g_hash_table_iter_next (&iter, &key, &value))
1188 GtkSymbolicColor *color;
1194 g_hash_table_lookup (priv->color_map, name))
1197 gtk_style_properties_map_color (props, name, color);
1201 /* Merge symbolic style properties */
1202 g_hash_table_iter_init (&iter, priv_to_merge->properties);
1204 while (g_hash_table_iter_next (&iter, &key, &value))
1206 PropertyData *prop_to_merge = value;
1210 prop = g_hash_table_lookup (priv->properties, key);
1214 prop = property_data_new ();
1215 g_hash_table_insert (priv->properties, key, prop);
1218 for (i = 0; i < prop_to_merge->values->len; i++)
1223 data = &g_array_index (prop_to_merge->values, ValueData, i);
1225 if (replace && data->state == GTK_STATE_FLAG_NORMAL &&
1226 G_VALUE_TYPE (&data->value) != PANGO_TYPE_FONT_DESCRIPTION)
1228 /* Let normal state override all states
1229 * previously set in the original set
1231 property_data_remove_values (prop);
1234 value = property_data_get_value (prop, data->state);
1236 if (G_VALUE_TYPE (&data->value) == PANGO_TYPE_FONT_DESCRIPTION &&
1239 PangoFontDescription *font_desc;
1240 PangoFontDescription *font_desc_to_merge;
1242 /* Handle merging of font descriptions */
1243 font_desc = g_value_get_boxed (value);
1244 font_desc_to_merge = g_value_get_boxed (&data->value);
1246 pango_font_description_merge (font_desc, font_desc_to_merge, replace);
1248 else if (G_VALUE_TYPE (&data->value) == G_TYPE_PTR_ARRAY &&
1251 GPtrArray *array, *array_to_merge;
1254 /* Append the array, mainly thought
1255 * for the gtk-key-bindings property
1257 array = g_value_get_boxed (value);
1258 array_to_merge = g_value_get_boxed (&data->value);
1260 for (i = 0; i < array_to_merge->len; i++)
1261 g_ptr_array_add (array, g_ptr_array_index (array_to_merge, i));
1263 else if (replace || !G_IS_VALUE (value))
1265 if (!G_IS_VALUE (value))
1266 g_value_init (value, G_VALUE_TYPE (&data->value));
1267 else if (G_VALUE_TYPE (value) != G_VALUE_TYPE (&data->value))
1269 g_value_unset (value);
1270 g_value_init (value, G_VALUE_TYPE (&data->value));
1273 g_value_copy (&data->value, value);