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, see <http://www.gnu.org/licenses/>.
20 #include "gtkstylepropertiesprivate.h"
23 #include <gobject/gvaluecollector.h>
24 #include <cairo-gobject.h>
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"
36 #include "gtkprivatetypebuiltins.h"
37 #include "gtkstylepropertyprivate.h"
38 #include "gtkstyleproviderprivate.h"
41 #include "gtkwin32themeprivate.h"
44 * SECTION:gtkstyleproperties
45 * @Short_description: Store for style property information
46 * @Title: GtkStyleProperties
48 * GtkStyleProperties provides the storage for style information
49 * that is used by #GtkStyleContext and other #GtkStyleProvider
52 * Before style properties can be stored in GtkStyleProperties, they
53 * must be registered with gtk_style_properties_register_property().
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.
62 typedef struct PropertyData PropertyData;
63 typedef struct ValueData ValueData;
76 struct _GtkStylePropertiesPrivate
78 GHashTable *color_map;
79 GHashTable *properties;
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);
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));
94 gtk_style_properties_class_init (GtkStylePropertiesClass *klass)
96 GObjectClass *object_class = G_OBJECT_CLASS (klass);
98 object_class->finalize = gtk_style_properties_finalize;
100 g_type_class_add_private (object_class, sizeof (GtkStylePropertiesPrivate));
103 static PropertyData *
104 property_data_new (void)
108 data = g_slice_new0 (PropertyData);
109 data->values = g_array_new (FALSE, FALSE, sizeof (ValueData));
115 property_data_remove_values (PropertyData *data)
119 for (i = 0; i < data->values->len; i++)
121 ValueData *value_data;
123 value_data = &g_array_index (data->values, ValueData, i);
125 _gtk_css_value_unref (value_data->value);
126 value_data->value = NULL;
129 if (data->values->len > 0)
130 g_array_remove_range (data->values, 0, data->values->len);
134 property_data_free (PropertyData *data)
136 property_data_remove_values (data);
137 g_array_free (data->values, TRUE);
138 g_slice_free (PropertyData, data);
142 property_data_find_position (PropertyData *data,
147 gboolean found = FALSE;
153 if (data->values->len == 0)
156 /* Find position for the given state, or the position where
157 * it would be if not found, the array is ordered by the
161 max = data->values->len - 1;
165 ValueData *value_data;
167 mid = (min + max) / 2;
168 value_data = &g_array_index (data->values, ValueData, mid);
170 if (value_data->state == state)
175 else if (value_data->state < state)
176 position = min = mid + 1;
183 while (!found && min <= max);
192 property_data_get_value (PropertyData *data,
197 if (!property_data_find_position (data, state, &pos))
199 ValueData new = { 0 };
202 g_array_insert_val (data->values, pos, new);
205 return &g_array_index (data->values, ValueData, pos);
209 property_data_match_state (PropertyData *data,
215 if (property_data_find_position (data, state, &pos))
220 val_data = &g_array_index (data->values, ValueData, pos);
221 return val_data->value;
224 if (pos >= data->values->len)
225 pos = data->values->len - 1;
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.
232 for (i = pos; i >= 0; i--)
236 val_data = &g_array_index (data->values, ValueData, i);
238 /* Check whether any of the requested
239 * flags are set, and no other flags are.
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.
245 if (val_data->state == 0 ||
246 ((val_data->state & state) != 0 &&
247 (val_data->state & ~state) == 0))
248 return val_data->value;
255 gtk_style_properties_init (GtkStyleProperties *props)
257 GtkStylePropertiesPrivate *priv;
259 priv = props->priv = G_TYPE_INSTANCE_GET_PRIVATE (props,
260 GTK_TYPE_STYLE_PROPERTIES,
261 GtkStylePropertiesPrivate);
263 priv->properties = g_hash_table_new_full (NULL, NULL, NULL,
264 (GDestroyNotify) property_data_free);
268 gtk_style_properties_finalize (GObject *object)
270 GtkStylePropertiesPrivate *priv;
271 GtkStyleProperties *props;
273 props = GTK_STYLE_PROPERTIES (object);
275 g_hash_table_destroy (priv->properties);
278 g_hash_table_destroy (priv->color_map);
280 G_OBJECT_CLASS (gtk_style_properties_parent_class)->finalize (object);
284 gtk_style_properties_get_style (GtkStyleProvider *provider,
287 /* Return style set itself */
288 return g_object_ref (provider);
292 gtk_style_properties_provider_init (GtkStyleProviderIface *iface)
294 iface->get_style = gtk_style_properties_get_style;
297 static GtkSymbolicColor *
298 gtk_style_properties_provider_get_color (GtkStyleProviderPrivate *provider,
301 return gtk_style_properties_lookup_color (GTK_STYLE_PROPERTIES (provider), name);
305 gtk_style_properties_provider_lookup (GtkStyleProviderPrivate *provider,
308 GtkCssLookup *lookup)
310 GtkStyleProperties *props;
311 GtkStylePropertiesPrivate *priv;
315 props = GTK_STYLE_PROPERTIES (provider);
318 /* Merge symbolic style properties */
319 g_hash_table_iter_init (&iter, priv->properties);
321 while (g_hash_table_iter_next (&iter, &key, &value))
323 GtkCssStyleProperty *prop = key;
324 PropertyData *data = value;
328 id = _gtk_css_style_property_get_id (prop);
330 if (!_gtk_css_lookup_is_missing (lookup, id))
333 value = property_data_match_state (data, state);
337 _gtk_css_lookup_set_computed (lookup, id, NULL, value);
342 gtk_style_properties_provider_private_init (GtkStyleProviderPrivateInterface *iface)
344 iface->get_color = gtk_style_properties_provider_get_color;
345 iface->lookup = gtk_style_properties_provider_lookup;
348 /* GtkStyleProperties methods */
351 * gtk_style_properties_new:
353 * Returns a newly created #GtkStyleProperties
355 * Returns: a new #GtkStyleProperties
358 gtk_style_properties_new (void)
360 return g_object_new (GTK_TYPE_STYLE_PROPERTIES, NULL);
364 * gtk_style_properties_map_color:
365 * @props: a #GtkStyleProperties
367 * @color: #GtkSymbolicColor to map @name to
369 * Maps @color so it can be referenced by @name. See
370 * gtk_style_properties_lookup_color()
375 gtk_style_properties_map_color (GtkStyleProperties *props,
377 GtkSymbolicColor *color)
379 GtkStylePropertiesPrivate *priv;
381 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
382 g_return_if_fail (name != NULL);
383 g_return_if_fail (color != NULL);
387 if (G_UNLIKELY (!priv->color_map))
388 priv->color_map = g_hash_table_new_full (g_str_hash,
390 (GDestroyNotify) g_free,
391 (GDestroyNotify) gtk_symbolic_color_unref);
393 g_hash_table_replace (priv->color_map,
395 gtk_symbolic_color_ref (color));
399 * gtk_style_properties_lookup_color:
400 * @props: a #GtkStyleProperties
401 * @name: color name to lookup
403 * Returns the symbolic color that is mapped
406 * Returns: (transfer none): The mapped color
411 gtk_style_properties_lookup_color (GtkStyleProperties *props,
414 GtkStylePropertiesPrivate *priv;
416 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
417 g_return_val_if_fail (name != NULL, NULL);
421 if (!priv->color_map)
424 return g_hash_table_lookup (priv->color_map, name);
428 _gtk_style_properties_set_property_by_property (GtkStyleProperties *props,
429 GtkCssStyleProperty *style_prop,
433 GtkStylePropertiesPrivate *priv;
437 g_return_if_fail (_gtk_css_value_holds (value, _gtk_css_style_property_get_computed_type (style_prop)));
440 prop = g_hash_table_lookup (priv->properties, style_prop);
444 prop = property_data_new ();
445 g_hash_table_insert (priv->properties, (gpointer) style_prop, prop);
448 val = property_data_get_value (prop, state);
450 _gtk_css_value_unref (val->value);
451 val->value = _gtk_css_value_ref (value);
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
461 * Sets a styling property in @props.
466 gtk_style_properties_set_property (GtkStyleProperties *props,
467 const gchar *property,
471 GtkStyleProperty *node;
473 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
474 g_return_if_fail (property != NULL);
475 g_return_if_fail (value != NULL);
477 node = _gtk_style_property_lookup (property);
481 g_warning ("Style property \"%s\" is not registered", property);
484 if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
486 g_warning ("Style property \"%s\" is not settable", property);
490 _gtk_style_property_assign (node, props, state, value);
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
499 * Sets several style properties on @props.
504 gtk_style_properties_set_valist (GtkStyleProperties *props,
508 const gchar *property_name;
510 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
512 property_name = va_arg (args, const gchar *);
514 while (property_name)
516 GtkStyleProperty *node;
519 GValue val = G_VALUE_INIT;
521 node = _gtk_style_property_lookup (property_name);
525 g_warning ("Style property \"%s\" is not registered", property_name);
529 val_type = _gtk_style_property_get_value_type (node);
530 if (val_type == G_TYPE_NONE)
532 g_warning ("Style property \"%s\" is not settable", property_name);
536 G_VALUE_COLLECT_INIT (&val, _gtk_style_property_get_value_type (node),
540 g_warning ("Could not set style property \"%s\": %s", property_name, error);
541 g_value_unset (&val);
546 _gtk_style_property_assign (node, props, state, &val);
547 g_value_unset (&val);
549 property_name = va_arg (args, const gchar *);
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
559 * Sets several style properties on @props.
564 gtk_style_properties_set (GtkStyleProperties *props,
570 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
572 va_start (args, state);
573 gtk_style_properties_set_valist (props, state, args);
578 _gtk_style_properties_peek_property (GtkStyleProperties *props,
579 GtkCssStyleProperty *property,
582 GtkStylePropertiesPrivate *priv;
585 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
586 g_return_val_if_fail (property != NULL, FALSE);
589 prop = g_hash_table_lookup (priv->properties, property);
593 return property_data_match_state (prop, state);
597 GtkStyleProperties *props;
602 style_query_func (guint id,
605 StyleQueryData *query = data;
607 return _gtk_style_properties_peek_property (query->props,
608 _gtk_css_style_property_lookup_by_id (id),
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.
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.
622 * Returns: %TRUE if the property exists in @props, %FALSE otherwise
627 gtk_style_properties_get_property (GtkStyleProperties *props,
628 const gchar *property,
632 StyleQueryData query = { props, state };
633 GtkStyleProperty *node;
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);
640 node = _gtk_style_property_lookup (property);
643 g_warning ("Style property \"%s\" is not registered", property);
646 if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
648 g_warning ("Style property \"%s\" is not gettable", property);
652 v = _gtk_style_property_query (node,
655 _gtk_css_value_init_gvalue (v, value);
656 _gtk_css_value_unref (v);
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
667 * Retrieves several style property values from @props for a given state.
672 gtk_style_properties_get_valist (GtkStyleProperties *props,
676 const gchar *property_name;
678 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
680 property_name = va_arg (args, const gchar *);
682 while (property_name)
685 GValue value = G_VALUE_INIT;
687 if (!gtk_style_properties_get_property (props,
693 G_VALUE_LCOPY (&value, args, 0, &error);
694 g_value_unset (&value);
698 g_warning ("Could not get style property \"%s\": %s", property_name, error);
703 property_name = va_arg (args, const gchar *);
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
713 * Retrieves several style property values from @props for a
719 gtk_style_properties_get (GtkStyleProperties *props,
725 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
727 va_start (args, state);
728 gtk_style_properties_get_valist (props, state, args);
733 * gtk_style_properties_unset_property:
734 * @props: a #GtkStyleProperties
735 * @property: property to unset
736 * @state: state to unset
738 * Unsets a style property in @props.
743 gtk_style_properties_unset_property (GtkStyleProperties *props,
744 const gchar *property,
747 GtkStylePropertiesPrivate *priv;
748 GtkStyleProperty *node;
752 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
753 g_return_if_fail (property != NULL);
755 node = _gtk_style_property_lookup (property);
759 g_warning ("Style property \"%s\" is not registered", property);
762 if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
764 g_warning ("Style property \"%s\" is not settable", property);
768 if (GTK_IS_CSS_SHORTHAND_PROPERTY (node))
770 GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (node);
772 for (pos = 0; pos < _gtk_css_shorthand_property_get_n_subproperties (shorthand); pos++)
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)),
783 prop = g_hash_table_lookup (priv->properties, node);
788 if (property_data_find_position (prop, state, &pos))
792 data = &g_array_index (prop->values, ValueData, pos);
794 _gtk_css_value_unref (data->value);
797 g_array_remove_index (prop->values, pos);
802 * gtk_style_properties_clear:
803 * @props: a #GtkStyleProperties
805 * Clears all style information from @props.
808 gtk_style_properties_clear (GtkStyleProperties *props)
810 GtkStylePropertiesPrivate *priv;
812 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
815 g_hash_table_remove_all (priv->properties);
819 * gtk_style_properties_merge:
820 * @props: a #GtkStyleProperties
821 * @props_to_merge: a second #GtkStyleProperties
822 * @replace: whether to replace values or not
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
832 gtk_style_properties_merge (GtkStyleProperties *props,
833 const GtkStyleProperties *props_to_merge,
836 GtkStylePropertiesPrivate *priv, *priv_to_merge;
840 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
841 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props_to_merge));
844 priv_to_merge = props_to_merge->priv;
846 /* Merge symbolic color map */
847 if (priv_to_merge->color_map)
849 g_hash_table_iter_init (&iter, priv_to_merge->color_map);
851 while (g_hash_table_iter_next (&iter, &key, &value))
854 GtkSymbolicColor *color;
860 g_hash_table_lookup (priv->color_map, name))
863 gtk_style_properties_map_color (props, name, color);
867 /* Merge symbolic style properties */
868 g_hash_table_iter_init (&iter, priv_to_merge->properties);
870 while (g_hash_table_iter_next (&iter, &key, &value))
872 PropertyData *prop_to_merge = value;
876 prop = g_hash_table_lookup (priv->properties, key);
880 prop = property_data_new ();
881 g_hash_table_insert (priv->properties, key, prop);
884 for (i = 0; i < prop_to_merge->values->len; i++)
889 data = &g_array_index (prop_to_merge->values, ValueData, i);
891 if (replace && data->state == GTK_STATE_FLAG_NORMAL &&
892 _gtk_css_value_holds (data->value, PANGO_TYPE_FONT_DESCRIPTION))
894 /* Let normal state override all states
895 * previously set in the original set
897 property_data_remove_values (prop);
900 value = property_data_get_value (prop, data->state);
902 if (_gtk_css_value_holds (data->value, PANGO_TYPE_FONT_DESCRIPTION) &&
903 value->value != NULL)
905 PangoFontDescription *font_desc;
906 PangoFontDescription *font_desc_to_merge;
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);
912 pango_font_description_merge (font_desc, font_desc_to_merge, replace);
914 else if (_gtk_css_value_holds (data->value, G_TYPE_PTR_ARRAY) &&
915 value->value != NULL)
917 GPtrArray *array, *array_to_merge;
920 /* Append the array, mainly thought
921 * for the gtk-key-bindings property
923 array = _gtk_css_value_get_boxed (value->value);
924 array_to_merge = _gtk_css_value_get_boxed (data->value);
926 for (i = 0; i < array_to_merge->len; i++)
927 g_ptr_array_add (array, g_ptr_array_index (array_to_merge, i));
929 else if (replace || value->value == NULL)
931 _gtk_css_value_unref (value->value);
932 value->value = _gtk_css_value_ref (data->value);