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"
37 #include "gtkshadowprivate.h"
41 * SECTION:gtkstyleproperties
42 * @Short_description: Store for style property information
43 * @Title: GtkStyleProperties
45 * GtkStyleProperties provides the storage for style information
46 * that is used by #GtkStyleContext and other #GtkStyleProvider
49 * Before style properties can be stored in GtkStyleProperties, they
50 * must be registered with gtk_style_properties_register_property().
52 * Unless you are writing a #GtkStyleProvider implementation, you
53 * are unlikely to use this API directly, as gtk_style_context_get()
54 * and its variants are the preferred way to access styling information
55 * from widget implementations and theming engine implementations
56 * should use the APIs provided by #GtkThemingEngine instead.
59 typedef struct GtkStylePropertiesPrivate GtkStylePropertiesPrivate;
60 typedef struct PropertyData PropertyData;
61 typedef struct _GtkStyleProperty GtkStyleProperty;
62 typedef struct ValueData ValueData;
64 struct _GtkStyleProperty
67 GtkStylePropertyParser parse_func;
81 struct GtkStylePropertiesPrivate
83 GHashTable *color_map;
84 GHashTable *properties;
87 static GHashTable *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);
103 object_class->finalize = gtk_style_properties_finalize;
105 /* Initialize default property set */
106 pspec = g_param_spec_boxed ("color",
110 gtk_style_param_set_inherit (pspec, TRUE);
111 gtk_style_properties_register_property (NULL, pspec);
113 pspec = g_param_spec_boxed ("font",
116 PANGO_TYPE_FONT_DESCRIPTION, 0);
117 gtk_style_param_set_inherit (pspec, TRUE);
118 gtk_style_properties_register_property (NULL, pspec);
120 pspec = g_param_spec_boxed ("text-shadow",
124 gtk_style_param_set_inherit (pspec, TRUE);
125 gtk_style_properties_register_property (NULL, pspec);
127 gtk_style_properties_register_property (NULL,
128 g_param_spec_boxed ("margin",
131 GTK_TYPE_BORDER, 0));
132 gtk_style_properties_register_property (NULL,
133 g_param_spec_boxed ("padding",
136 GTK_TYPE_BORDER, 0));
137 gtk_style_properties_register_property (NULL,
138 g_param_spec_boxed ("border-width",
140 "Border width, in pixels",
141 GTK_TYPE_BORDER, 0));
142 gtk_style_properties_register_property (NULL,
143 g_param_spec_int ("border-radius",
145 "Border radius, in pixels",
147 gtk_style_properties_register_property (NULL,
148 g_param_spec_enum ("border-style",
151 GTK_TYPE_BORDER_STYLE,
152 GTK_BORDER_STYLE_NONE, 0));
153 gtk_style_properties_register_property (NULL,
154 g_param_spec_boxed ("border-color",
158 gtk_style_properties_register_property (NULL,
159 g_param_spec_boxed ("background-color",
163 gtk_style_properties_register_property (NULL,
164 g_param_spec_boxed ("background-image",
167 CAIRO_GOBJECT_TYPE_PATTERN, 0));
168 gtk_style_properties_register_property (NULL,
169 g_param_spec_boxed ("border-image",
172 GTK_TYPE_9SLICE, 0));
173 gtk_style_properties_register_property (NULL,
174 g_param_spec_object ("engine",
177 GTK_TYPE_THEMING_ENGINE, 0));
178 gtk_style_properties_register_property (NULL,
179 g_param_spec_boxed ("transition",
180 "Transition animation description",
181 "Transition animation description",
182 GTK_TYPE_ANIMATION_DESCRIPTION, 0));
184 /* Private property holding the binding sets */
185 gtk_style_properties_register_property (NULL,
186 g_param_spec_boxed ("gtk-key-bindings",
189 G_TYPE_PTR_ARRAY, 0));
191 g_type_class_add_private (object_class, sizeof (GtkStylePropertiesPrivate));
194 static PropertyData *
195 property_data_new (void)
199 data = g_slice_new0 (PropertyData);
200 data->values = g_array_new (FALSE, FALSE, sizeof (ValueData));
206 property_data_remove_values (PropertyData *data)
210 for (i = 0; i < data->values->len; i++)
212 ValueData *value_data;
214 value_data = &g_array_index (data->values, ValueData, i);
216 if (G_IS_VALUE (&value_data->value))
217 g_value_unset (&value_data->value);
220 if (data->values->len > 0)
221 g_array_remove_range (data->values, 0, data->values->len);
225 property_data_free (PropertyData *data)
227 property_data_remove_values (data);
228 g_array_free (data->values, TRUE);
229 g_slice_free (PropertyData, data);
233 property_data_find_position (PropertyData *data,
238 gboolean found = FALSE;
244 if (data->values->len == 0)
247 /* Find position for the given state, or the position where
248 * it would be if not found, the array is ordered by the
252 max = data->values->len - 1;
256 ValueData *value_data;
258 mid = (min + max) / 2;
259 value_data = &g_array_index (data->values, ValueData, mid);
261 if (value_data->state == state)
266 else if (value_data->state < state)
267 position = min = mid + 1;
274 while (!found && min <= max);
283 property_data_get_value (PropertyData *data,
289 if (!property_data_find_position (data, state, &pos))
291 ValueData new = { 0 };
294 g_array_insert_val (data->values, pos, new);
297 val_data = &g_array_index (data->values, ValueData, pos);
299 return &val_data->value;
303 property_data_match_state (PropertyData *data,
309 if (property_data_find_position (data, state, &pos))
314 val_data = &g_array_index (data->values, ValueData, pos);
315 return &val_data->value;
318 if (pos >= data->values->len)
319 pos = data->values->len - 1;
321 /* No exact match, go downwards the list to find
322 * the closest match to the given state flags, as
323 * a side effect, there is an implicit precedence
324 * of higher flags over the smaller ones.
326 for (i = pos; i >= 0; i--)
330 val_data = &g_array_index (data->values, ValueData, i);
332 /* Check whether any of the requested
333 * flags are set, and no other flags are.
335 * Also, no flags acts as a wildcard, such
336 * value should be always in the first position
337 * in the array (if present) anyways.
339 if (val_data->state == 0 ||
340 ((val_data->state & state) != 0 &&
341 (val_data->state & ~state) == 0))
342 return &val_data->value;
349 gtk_style_properties_init (GtkStyleProperties *props)
351 GtkStylePropertiesPrivate *priv;
353 priv = props->priv = G_TYPE_INSTANCE_GET_PRIVATE (props,
354 GTK_TYPE_STYLE_PROPERTIES,
355 GtkStylePropertiesPrivate);
357 priv->properties = g_hash_table_new_full (NULL, NULL, NULL,
358 (GDestroyNotify) property_data_free);
362 gtk_style_properties_finalize (GObject *object)
364 GtkStylePropertiesPrivate *priv;
365 GtkStyleProperties *props;
367 props = GTK_STYLE_PROPERTIES (object);
369 g_hash_table_destroy (priv->properties);
372 g_hash_table_destroy (priv->color_map);
374 G_OBJECT_CLASS (gtk_style_properties_parent_class)->finalize (object);
378 gtk_style_properties_get_style (GtkStyleProvider *provider,
381 /* Return style set itself */
382 return g_object_ref (provider);
386 gtk_style_properties_provider_init (GtkStyleProviderIface *iface)
388 iface->get_style = gtk_style_properties_get_style;
391 static const GtkStyleProperty *
392 gtk_style_property_lookup (const char *name)
397 return g_hash_table_lookup (properties, name);
400 /* Property registration functions */
403 * gtk_style_properties_register_property: (skip)
404 * @parse_func: parsing function to use, or %NULL
405 * @pspec: the #GParamSpec for the new property
407 * Registers a property so it can be used in the CSS file format.
408 * This function is the low-level equivalent of
409 * gtk_theming_engine_register_property(), if you are implementing
410 * a theming engine, you want to use that function instead.
415 gtk_style_properties_register_property (GtkStylePropertyParser parse_func,
418 const GtkStyleProperty *existing;
419 GtkStyleProperty *node;
421 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
423 /* stuff is never freed, so no need for free functions */
424 if (G_UNLIKELY (!properties))
425 properties = g_hash_table_new (g_str_hash, g_str_equal);
427 existing = gtk_style_property_lookup (pspec->name);
428 if (existing != NULL)
430 g_warning ("Property \"%s\" was already registered with type %s",
431 pspec->name, g_type_name (existing->pspec->value_type));
435 node = g_slice_new0 (GtkStyleProperty);
437 node->parse_func = parse_func;
439 g_hash_table_insert (properties, pspec->name, node);
443 * gtk_style_properties_lookup_property: (skip)
444 * @property_name: property name to look up
445 * @parse_func: (out): return location for the parse function
446 * @pspec: (out) (transfer none): return location for the #GParamSpec
448 * Returns %TRUE if a property has been registered, if @pspec or
449 * @parse_func are not %NULL, the #GParamSpec and parsing function
450 * will be respectively returned.
452 * Returns: %TRUE if the property is registered, %FALSE otherwise
457 gtk_style_properties_lookup_property (const gchar *property_name,
458 GtkStylePropertyParser *parse_func,
461 const GtkStyleProperty *node;
462 GtkStylePropertiesClass *klass;
463 gboolean found = FALSE;
465 g_return_val_if_fail (property_name != NULL, FALSE);
467 klass = g_type_class_ref (GTK_TYPE_STYLE_PROPERTIES);
469 node = gtk_style_property_lookup (property_name);
474 *pspec = node->pspec;
477 *parse_func = node->parse_func;
482 g_type_class_unref (klass);
487 /* GParamSpec functionality */
490 GTK_STYLE_PROPERTY_INHERIT = 1 << G_PARAM_USER_SHIFT
494 * gtk_style_param_set_inherit:
495 * @pspec: A style param
496 * @inherit: whether the @pspec value should be inherited
498 * Sets whether a param spec installed with function such as
499 * gtk_style_properties_register_property() or
500 * gtk_widget_class_install_style_property() should inherit their
501 * value from the parent widget if it is not set instead of using
502 * the default value of @pspec. See the
503 * <ulink url="http://www.w3.org/TR/CSS21/cascade.html#inheritance">
504 * CSS specification's description of inheritance</ulink> for a
505 * longer description of this concept.
507 * By default, param specs do not inherit their value.
510 gtk_style_param_set_inherit (GParamSpec *pspec,
514 pspec->flags |= GTK_STYLE_PROPERTY_INHERIT;
516 pspec->flags &= ~GTK_STYLE_PROPERTY_INHERIT;
520 * gtk_style_param_get_inherit:
521 * @pspec: a style param
523 * Checks if the value of this param should be inherited from the parent
524 * #GtkWidget instead of using the default value when it has not been
525 * specified. See gtk_style_param_set_inherit() for more details.
527 * Returns: %TRUE if the param should inherit its value
530 gtk_style_param_get_inherit (GParamSpec *pspec)
532 return (pspec->flags & GTK_STYLE_PROPERTY_INHERIT) ? TRUE : FALSE;
535 /* GtkStyleProperties methods */
538 * gtk_style_properties_new:
540 * Returns a newly created #GtkStyleProperties
542 * Returns: a new #GtkStyleProperties
545 gtk_style_properties_new (void)
547 return g_object_new (GTK_TYPE_STYLE_PROPERTIES, NULL);
551 * gtk_style_properties_map_color:
552 * @props: a #GtkStyleProperties
554 * @color: #GtkSymbolicColor to map @name to
556 * Maps @color so it can be referenced by @name. See
557 * gtk_style_properties_lookup_color()
562 gtk_style_properties_map_color (GtkStyleProperties *props,
564 GtkSymbolicColor *color)
566 GtkStylePropertiesPrivate *priv;
568 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
569 g_return_if_fail (name != NULL);
570 g_return_if_fail (color != NULL);
574 if (G_UNLIKELY (!priv->color_map))
575 priv->color_map = g_hash_table_new_full (g_str_hash,
577 (GDestroyNotify) g_free,
578 (GDestroyNotify) gtk_symbolic_color_unref);
580 g_hash_table_replace (priv->color_map,
582 gtk_symbolic_color_ref (color));
586 * gtk_style_properties_lookup_color:
587 * @props: a #GtkStyleProperties
588 * @name: color name to lookup
590 * Returns the symbolic color that is mapped
593 * Returns: (transfer none): The mapped color
598 gtk_style_properties_lookup_color (GtkStyleProperties *props,
601 GtkStylePropertiesPrivate *priv;
603 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
604 g_return_val_if_fail (name != NULL, NULL);
608 if (!priv->color_map)
611 return g_hash_table_lookup (priv->color_map, name);
615 _gtk_style_properties_set_property_by_pspec (GtkStyleProperties *props,
620 GtkStylePropertiesPrivate *priv;
625 value_type = G_VALUE_TYPE (value);
627 if (pspec->value_type == GDK_TYPE_RGBA ||
628 pspec->value_type == GDK_TYPE_COLOR)
630 /* Allow GtkSymbolicColor as well */
631 g_return_if_fail (value_type == GDK_TYPE_RGBA ||
632 value_type == GDK_TYPE_COLOR ||
633 value_type == GTK_TYPE_SYMBOLIC_COLOR);
635 else if (pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN)
637 /* Allow GtkGradient as a substitute */
638 g_return_if_fail (value_type == CAIRO_GOBJECT_TYPE_PATTERN ||
639 value_type == GTK_TYPE_GRADIENT);
642 g_return_if_fail (pspec->value_type == value_type);
645 prop = g_hash_table_lookup (priv->properties, pspec);
649 prop = property_data_new ();
650 g_hash_table_insert (priv->properties, pspec, prop);
653 val = property_data_get_value (prop, state);
655 if (G_VALUE_TYPE (val) == value_type)
659 if (G_IS_VALUE (val))
662 g_value_init (val, value_type);
665 g_value_copy (value, val);
669 * gtk_style_properties_set_property:
670 * @props: a #GtkStyleProperties
671 * @property: styling property to set
672 * @state: state to set the value for
673 * @value: new value for the property
675 * Sets a styling property in @props.
680 gtk_style_properties_set_property (GtkStyleProperties *props,
681 const gchar *property,
685 const GtkStyleProperty *node;
687 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
688 g_return_if_fail (property != NULL);
689 g_return_if_fail (value != NULL);
691 node = gtk_style_property_lookup (property);
695 g_warning ("Style property \"%s\" is not registered", property);
699 _gtk_style_properties_set_property_by_pspec (props,
706 * gtk_style_properties_set_valist:
707 * @props: a #GtkStyleProperties
708 * @state: state to set the values for
709 * @args: va_list of property name/value pairs, followed by %NULL
711 * Sets several style properties on @props.
716 gtk_style_properties_set_valist (GtkStyleProperties *props,
720 GtkStylePropertiesPrivate *priv;
721 const gchar *property_name;
723 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
726 property_name = va_arg (args, const gchar *);
728 while (property_name)
730 const GtkStyleProperty *node;
735 node = gtk_style_property_lookup (property_name);
739 g_warning ("Style property \"%s\" is not registered", property_name);
743 prop = g_hash_table_lookup (priv->properties, node->pspec);
747 prop = property_data_new ();
748 g_hash_table_insert (priv->properties, node->pspec, prop);
751 val = property_data_get_value (prop, state);
753 if (G_IS_VALUE (val))
756 G_VALUE_COLLECT_INIT (val, node->pspec->value_type,
760 g_warning ("Could not set style property \"%s\": %s", property_name, error);
766 property_name = va_arg (args, const gchar *);
771 * gtk_style_properties_set:
772 * @props: a #GtkStyleProperties
773 * @state: state to set the values for
774 * @...: property name/value pairs, followed by %NULL
776 * Sets several style properties on @props.
781 gtk_style_properties_set (GtkStyleProperties *props,
787 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
789 va_start (args, state);
790 gtk_style_properties_set_valist (props, state, args);
795 resolve_color (GtkStyleProperties *props,
800 /* Resolve symbolic color to GdkRGBA */
801 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
804 /* Store it back, this is where GdkRGBA caching happens */
805 g_value_unset (value);
806 g_value_init (value, GDK_TYPE_RGBA);
807 g_value_set_boxed (value, &color);
813 resolve_color_rgb (GtkStyleProperties *props,
816 GdkColor color = { 0 };
819 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
822 color.red = rgba.red * 65535. + 0.5;
823 color.green = rgba.green * 65535. + 0.5;
824 color.blue = rgba.blue * 65535. + 0.5;
826 g_value_unset (value);
827 g_value_init (value, GDK_TYPE_COLOR);
828 g_value_set_boxed (value, &color);
834 resolve_gradient (GtkStyleProperties *props,
837 cairo_pattern_t *gradient;
839 if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
842 /* Store it back, this is where cairo_pattern_t caching happens */
843 g_value_unset (value);
844 g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
845 g_value_take_boxed (value, gradient);
851 resolve_shadow (GtkStyleProperties *props,
854 GtkShadow *resolved, *base;
856 base = g_value_get_boxed (value);
861 if (_gtk_shadow_get_resolved (base))
864 resolved = _gtk_shadow_resolve (base, props);
865 if (resolved == NULL)
868 g_value_take_boxed (value, resolved);
874 style_properties_resolve_type (GtkStyleProperties *props,
875 const GtkStyleProperty *node,
878 if (val && G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
880 if (node->pspec->value_type == GDK_TYPE_RGBA)
882 if (!resolve_color (props, val))
885 else if (node->pspec->value_type == GDK_TYPE_COLOR)
887 if (!resolve_color_rgb (props, val))
893 else if (val && G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
895 g_return_val_if_fail (node->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN, FALSE);
897 if (!resolve_gradient (props, val))
900 else if (val && G_VALUE_TYPE (val) == GTK_TYPE_SHADOW)
902 if (!resolve_shadow (props, val))
910 lookup_default_value (const GtkStyleProperty *node,
913 if (node->pspec->value_type == GTK_TYPE_THEMING_ENGINE)
914 g_value_set_object (value, gtk_theming_engine_load (NULL));
915 else if (node->pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
916 g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
917 else if (node->pspec->value_type == GDK_TYPE_RGBA)
920 gdk_rgba_parse (&color, "pink");
921 g_value_set_boxed (value, &color);
923 else if (node->pspec->value_type == GTK_TYPE_BORDER)
925 g_value_take_boxed (value, gtk_border_new ());
928 g_param_value_set_default (node->pspec, value);
932 _gtk_style_properties_peek_property (GtkStyleProperties *props,
933 const gchar *prop_name,
936 GtkStylePropertiesPrivate *priv;
937 const GtkStyleProperty *node;
941 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
942 g_return_val_if_fail (prop_name != NULL, NULL);
944 node = gtk_style_property_lookup (prop_name);
948 g_warning ("Style property \"%s\" is not registered", prop_name);
953 prop = g_hash_table_lookup (priv->properties, node->pspec);
958 val = property_data_match_state (prop, state);
961 !style_properties_resolve_type (props, node, val))
968 * gtk_style_properties_get_property:
969 * @props: a #GtkStyleProperties
970 * @property: style property name
971 * @state: state to retrieve the property value for
972 * @value: (out) (transfer full): return location for the style property value.
974 * Gets a style property from @props for the given state. When done with @value,
975 * g_value_unset() needs to be called to free any allocated memory.
977 * Returns: %TRUE if the property exists in @props, %FALSE otherwise
982 gtk_style_properties_get_property (GtkStyleProperties *props,
983 const gchar *property,
987 GtkStylePropertiesPrivate *priv;
988 const GtkStyleProperty *node;
992 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
993 g_return_val_if_fail (property != NULL, FALSE);
994 g_return_val_if_fail (value != NULL, FALSE);
996 node = gtk_style_property_lookup (property);
1000 g_warning ("Style property \"%s\" is not registered", property);
1005 prop = g_hash_table_lookup (priv->properties, node->pspec);
1010 g_value_init (value, node->pspec->value_type);
1011 val = property_data_match_state (prop, state);
1014 !style_properties_resolve_type (props, node, val))
1019 g_param_value_validate (node->pspec, val);
1020 g_value_copy (val, value);
1023 lookup_default_value (node, value);
1029 * gtk_style_properties_get_valist:
1030 * @props: a #GtkStyleProperties
1031 * @state: state to retrieve the property values for
1032 * @args: va_list of property name/return location pairs, followed by %NULL
1034 * Retrieves several style property values from @props for a given state.
1039 gtk_style_properties_get_valist (GtkStyleProperties *props,
1040 GtkStateFlags state,
1043 GtkStylePropertiesPrivate *priv;
1044 const gchar *property_name;
1046 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1049 property_name = va_arg (args, const gchar *);
1051 while (property_name)
1053 const GtkStyleProperty *node;
1055 gchar *error = NULL;
1058 node = gtk_style_property_lookup (property_name);
1062 g_warning ("Style property \"%s\" is not registered", property_name);
1066 prop = g_hash_table_lookup (priv->properties, node->pspec);
1069 val = property_data_match_state (prop, state);
1072 !style_properties_resolve_type (props, node, val))
1077 g_param_value_validate (node->pspec, val);
1078 G_VALUE_LCOPY (val, args, 0, &error);
1082 GValue default_value = { 0 };
1084 g_value_init (&default_value, node->pspec->value_type);
1085 lookup_default_value (node, &default_value);
1086 G_VALUE_LCOPY (&default_value, args, 0, &error);
1087 g_value_unset (&default_value);
1092 g_warning ("Could not get style property \"%s\": %s", property_name, error);
1097 property_name = va_arg (args, const gchar *);
1102 * gtk_style_properties_get:
1103 * @props: a #GtkStyleProperties
1104 * @state: state to retrieve the property values for
1105 * @...: property name /return value pairs, followed by %NULL
1107 * Retrieves several style property values from @props for a
1113 gtk_style_properties_get (GtkStyleProperties *props,
1114 GtkStateFlags state,
1119 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1121 va_start (args, state);
1122 gtk_style_properties_get_valist (props, state, args);
1127 * gtk_style_properties_unset_property:
1128 * @props: a #GtkStyleProperties
1129 * @property: property to unset
1130 * @state: state to unset
1132 * Unsets a style property in @props.
1137 gtk_style_properties_unset_property (GtkStyleProperties *props,
1138 const gchar *property,
1139 GtkStateFlags state)
1141 GtkStylePropertiesPrivate *priv;
1142 const GtkStyleProperty *node;
1146 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1147 g_return_if_fail (property != NULL);
1149 node = gtk_style_property_lookup (property);
1153 g_warning ("Style property \"%s\" is not registered", property);
1158 prop = g_hash_table_lookup (priv->properties, node->pspec);
1163 if (property_data_find_position (prop, state, &pos))
1167 data = &g_array_index (prop->values, ValueData, pos);
1169 if (G_IS_VALUE (&data->value))
1170 g_value_unset (&data->value);
1172 g_array_remove_index (prop->values, pos);
1177 * gtk_style_properties_clear:
1178 * @props: a #GtkStyleProperties
1180 * Clears all style information from @props.
1183 gtk_style_properties_clear (GtkStyleProperties *props)
1185 GtkStylePropertiesPrivate *priv;
1187 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1190 g_hash_table_remove_all (priv->properties);
1194 * gtk_style_properties_merge:
1195 * @props: a #GtkStyleProperties
1196 * @props_to_merge: a second #GtkStyleProperties
1197 * @replace: whether to replace values or not
1199 * Merges into @props all the style information contained
1200 * in @props_to_merge. If @replace is %TRUE, the values
1201 * will be overwritten, if it is %FALSE, the older values
1207 gtk_style_properties_merge (GtkStyleProperties *props,
1208 const GtkStyleProperties *props_to_merge,
1211 GtkStylePropertiesPrivate *priv, *priv_to_merge;
1212 GHashTableIter iter;
1213 gpointer key, value;
1215 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1216 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props_to_merge));
1219 priv_to_merge = props_to_merge->priv;
1221 /* Merge symbolic color map */
1222 if (priv_to_merge->color_map)
1224 g_hash_table_iter_init (&iter, priv_to_merge->color_map);
1226 while (g_hash_table_iter_next (&iter, &key, &value))
1229 GtkSymbolicColor *color;
1235 g_hash_table_lookup (priv->color_map, name))
1238 gtk_style_properties_map_color (props, name, color);
1242 /* Merge symbolic style properties */
1243 g_hash_table_iter_init (&iter, priv_to_merge->properties);
1245 while (g_hash_table_iter_next (&iter, &key, &value))
1247 PropertyData *prop_to_merge = value;
1251 prop = g_hash_table_lookup (priv->properties, key);
1255 prop = property_data_new ();
1256 g_hash_table_insert (priv->properties, key, prop);
1259 for (i = 0; i < prop_to_merge->values->len; i++)
1264 data = &g_array_index (prop_to_merge->values, ValueData, i);
1266 if (replace && data->state == GTK_STATE_FLAG_NORMAL &&
1267 G_VALUE_TYPE (&data->value) != PANGO_TYPE_FONT_DESCRIPTION)
1269 /* Let normal state override all states
1270 * previously set in the original set
1272 property_data_remove_values (prop);
1275 value = property_data_get_value (prop, data->state);
1277 if (G_VALUE_TYPE (&data->value) == PANGO_TYPE_FONT_DESCRIPTION &&
1280 PangoFontDescription *font_desc;
1281 PangoFontDescription *font_desc_to_merge;
1283 /* Handle merging of font descriptions */
1284 font_desc = g_value_get_boxed (value);
1285 font_desc_to_merge = g_value_get_boxed (&data->value);
1287 pango_font_description_merge (font_desc, font_desc_to_merge, replace);
1289 else if (G_VALUE_TYPE (&data->value) == G_TYPE_PTR_ARRAY &&
1292 GPtrArray *array, *array_to_merge;
1295 /* Append the array, mainly thought
1296 * for the gtk-key-bindings property
1298 array = g_value_get_boxed (value);
1299 array_to_merge = g_value_get_boxed (&data->value);
1301 for (i = 0; i < array_to_merge->len; i++)
1302 g_ptr_array_add (array, g_ptr_array_index (array_to_merge, i));
1304 else if (replace || !G_IS_VALUE (value))
1306 if (!G_IS_VALUE (value))
1307 g_value_init (value, G_VALUE_TYPE (&data->value));
1308 else if (G_VALUE_TYPE (value) != G_VALUE_TYPE (&data->value))
1310 g_value_unset (value);
1311 g_value_init (value, G_VALUE_TYPE (&data->value));
1314 g_value_copy (&data->value, value);