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 "gtkthemingengine.h"
28 #include "gtkcssshorthandpropertyprivate.h"
29 #include "gtkcsstypedvalueprivate.h"
30 #include "gtkcsstypesprivate.h"
31 #include "gtkborderimageprivate.h"
33 #include "gtkprivatetypebuiltins.h"
34 #include "gtkstylepropertyprivate.h"
35 #include "gtkstyleproviderprivate.h"
37 #include "gtkwin32themeprivate.h"
39 #include "deprecated/gtkgradient.h"
40 #include "deprecated/gtksymboliccolorprivate.h"
43 * SECTION:gtkstyleproperties
44 * @Short_description: Store for style property information
45 * @Title: GtkStyleProperties
47 * GtkStyleProperties provides the storage for style information
48 * that is used by #GtkStyleContext and other #GtkStyleProvider
51 * Before style properties can be stored in GtkStyleProperties, they
52 * must be registered with gtk_style_properties_register_property().
54 * Unless you are writing a #GtkStyleProvider implementation, you
55 * are unlikely to use this API directly, as gtk_style_context_get()
56 * and its variants are the preferred way to access styling information
57 * from widget implementations and theming engine implementations
58 * should use the APIs provided by #GtkThemingEngine instead.
61 typedef struct PropertyData PropertyData;
62 typedef struct ValueData ValueData;
75 struct _GtkStylePropertiesPrivate
77 GHashTable *color_map;
78 GHashTable *properties;
81 static void gtk_style_properties_provider_init (GtkStyleProviderIface *iface);
82 static void gtk_style_properties_provider_private_init (GtkStyleProviderPrivateInterface *iface);
83 static void gtk_style_properties_finalize (GObject *object);
86 G_DEFINE_TYPE_EXTENDED (GtkStyleProperties, gtk_style_properties, G_TYPE_OBJECT, 0,
87 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
88 gtk_style_properties_provider_init)
89 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER_PRIVATE,
90 gtk_style_properties_provider_private_init));
93 gtk_style_properties_class_init (GtkStylePropertiesClass *klass)
95 GObjectClass *object_class = G_OBJECT_CLASS (klass);
97 object_class->finalize = gtk_style_properties_finalize;
99 g_type_class_add_private (object_class, sizeof (GtkStylePropertiesPrivate));
102 static PropertyData *
103 property_data_new (void)
107 data = g_slice_new0 (PropertyData);
108 data->values = g_array_new (FALSE, FALSE, sizeof (ValueData));
114 property_data_remove_values (PropertyData *data)
118 for (i = 0; i < data->values->len; i++)
120 ValueData *value_data;
122 value_data = &g_array_index (data->values, ValueData, i);
124 _gtk_css_value_unref (value_data->value);
125 value_data->value = NULL;
128 if (data->values->len > 0)
129 g_array_remove_range (data->values, 0, data->values->len);
133 property_data_free (PropertyData *data)
135 property_data_remove_values (data);
136 g_array_free (data->values, TRUE);
137 g_slice_free (PropertyData, data);
141 property_data_find_position (PropertyData *data,
146 gboolean found = FALSE;
152 if (data->values->len == 0)
155 /* Find position for the given state, or the position where
156 * it would be if not found, the array is ordered by the
160 max = data->values->len - 1;
164 ValueData *value_data;
166 mid = (min + max) / 2;
167 value_data = &g_array_index (data->values, ValueData, mid);
169 if (value_data->state == state)
174 else if (value_data->state < state)
175 position = min = mid + 1;
182 while (!found && min <= max);
191 property_data_get_value (PropertyData *data,
196 if (!property_data_find_position (data, state, &pos))
198 ValueData new = { 0 };
201 g_array_insert_val (data->values, pos, new);
204 return &g_array_index (data->values, ValueData, pos);
208 property_data_match_state (PropertyData *data,
214 if (property_data_find_position (data, state, &pos))
219 val_data = &g_array_index (data->values, ValueData, pos);
220 return val_data->value;
223 if (pos >= data->values->len)
224 pos = data->values->len - 1;
226 /* No exact match, go downwards the list to find
227 * the closest match to the given state flags, as
228 * a side effect, there is an implicit precedence
229 * of higher flags over the smaller ones.
231 for (i = pos; i >= 0; i--)
235 val_data = &g_array_index (data->values, ValueData, i);
237 /* Check whether any of the requested
238 * flags are set, and no other flags are.
240 * Also, no flags acts as a wildcard, such
241 * value should be always in the first position
242 * in the array (if present) anyways.
244 if (val_data->state == 0 ||
245 ((val_data->state & state) != 0 &&
246 (val_data->state & ~state) == 0))
247 return val_data->value;
254 gtk_style_properties_init (GtkStyleProperties *props)
256 GtkStylePropertiesPrivate *priv;
258 priv = props->priv = G_TYPE_INSTANCE_GET_PRIVATE (props,
259 GTK_TYPE_STYLE_PROPERTIES,
260 GtkStylePropertiesPrivate);
262 priv->properties = g_hash_table_new_full (NULL, NULL, NULL,
263 (GDestroyNotify) property_data_free);
267 gtk_style_properties_finalize (GObject *object)
269 GtkStylePropertiesPrivate *priv;
270 GtkStyleProperties *props;
272 props = GTK_STYLE_PROPERTIES (object);
274 g_hash_table_destroy (priv->properties);
277 g_hash_table_destroy (priv->color_map);
279 G_OBJECT_CLASS (gtk_style_properties_parent_class)->finalize (object);
283 gtk_style_properties_provider_init (GtkStyleProviderIface *iface)
288 gtk_style_properties_provider_get_color (GtkStyleProviderPrivate *provider,
291 GtkSymbolicColor *symbolic;
293 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
295 symbolic = gtk_style_properties_lookup_color (GTK_STYLE_PROPERTIES (provider), name);
296 if (symbolic == NULL)
299 return _gtk_symbolic_color_get_css_value (symbolic);
301 G_GNUC_END_IGNORE_DEPRECATIONS;
305 gtk_style_properties_provider_lookup (GtkStyleProviderPrivate *provider,
306 const GtkCssMatcher *matcher,
307 GtkCssLookup *lookup)
309 GtkStyleProperties *props;
310 GtkStylePropertiesPrivate *priv;
314 props = GTK_STYLE_PROPERTIES (provider);
317 /* Merge symbolic style properties */
318 g_hash_table_iter_init (&iter, priv->properties);
320 while (g_hash_table_iter_next (&iter, &key, &value))
322 GtkCssStyleProperty *prop = key;
323 PropertyData *data = value;
327 id = _gtk_css_style_property_get_id (prop);
329 if (!_gtk_css_lookup_is_missing (lookup, id))
332 value = property_data_match_state (data, _gtk_css_matcher_get_state (matcher));
336 _gtk_css_lookup_set_computed (lookup, id, NULL, value);
341 gtk_style_properties_provider_get_change (GtkStyleProviderPrivate *provider,
342 const GtkCssMatcher *matcher)
344 return GTK_CSS_CHANGE_STATE;
348 gtk_style_properties_provider_private_init (GtkStyleProviderPrivateInterface *iface)
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;
355 /* GtkStyleProperties methods */
358 * gtk_style_properties_new:
360 * Returns a newly created #GtkStyleProperties
362 * Returns: a new #GtkStyleProperties
365 gtk_style_properties_new (void)
367 return g_object_new (GTK_TYPE_STYLE_PROPERTIES, NULL);
371 * gtk_style_properties_map_color:
372 * @props: a #GtkStyleProperties
374 * @color: #GtkSymbolicColor to map @name to
376 * Maps @color so it can be referenced by @name. See
377 * gtk_style_properties_lookup_color()
381 * Deprecated: 3.8: #GtkSymbolicColor is deprecated.
384 gtk_style_properties_map_color (GtkStyleProperties *props,
386 GtkSymbolicColor *color)
388 GtkStylePropertiesPrivate *priv;
390 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
391 g_return_if_fail (name != NULL);
392 g_return_if_fail (color != NULL);
396 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
398 if (G_UNLIKELY (!priv->color_map))
399 priv->color_map = g_hash_table_new_full (g_str_hash,
401 (GDestroyNotify) g_free,
402 (GDestroyNotify) gtk_symbolic_color_unref);
404 g_hash_table_replace (priv->color_map,
406 gtk_symbolic_color_ref (color));
408 G_GNUC_END_IGNORE_DEPRECATIONS;
410 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (props));
414 * gtk_style_properties_lookup_color:
415 * @props: a #GtkStyleProperties
416 * @name: color name to lookup
418 * Returns the symbolic color that is mapped
421 * Returns: (transfer none): The mapped color
425 * Deprecated: 3.8: #GtkSymbolicColor is deprecated.
428 gtk_style_properties_lookup_color (GtkStyleProperties *props,
431 GtkStylePropertiesPrivate *priv;
433 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
434 g_return_val_if_fail (name != NULL, NULL);
438 if (!priv->color_map)
441 return g_hash_table_lookup (priv->color_map, name);
445 _gtk_style_properties_set_property_by_property (GtkStyleProperties *props,
446 GtkCssStyleProperty *style_prop,
450 GtkStylePropertiesPrivate *priv;
455 prop = g_hash_table_lookup (priv->properties, style_prop);
459 prop = property_data_new ();
460 g_hash_table_insert (priv->properties, (gpointer) style_prop, prop);
463 val = property_data_get_value (prop, state);
465 _gtk_css_value_unref (val->value);
466 val->value = _gtk_css_value_ref (value);
468 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (props));
472 * gtk_style_properties_set_property:
473 * @props: a #GtkStyleProperties
474 * @property: styling property to set
475 * @state: state to set the value for
476 * @value: new value for the property
478 * Sets a styling property in @props.
483 gtk_style_properties_set_property (GtkStyleProperties *props,
484 const gchar *property,
488 GtkStyleProperty *node;
490 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
491 g_return_if_fail (property != NULL);
492 g_return_if_fail (value != NULL);
494 node = _gtk_style_property_lookup (property);
498 g_warning ("Style property \"%s\" is not registered", property);
501 if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
503 g_warning ("Style property \"%s\" is not settable", property);
507 _gtk_style_property_assign (node, props, state, value);
511 * gtk_style_properties_set_valist:
512 * @props: a #GtkStyleProperties
513 * @state: state to set the values for
514 * @args: va_list of property name/value pairs, followed by %NULL
516 * Sets several style properties on @props.
521 gtk_style_properties_set_valist (GtkStyleProperties *props,
525 const gchar *property_name;
527 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
529 property_name = va_arg (args, const gchar *);
531 while (property_name)
533 GtkStyleProperty *node;
536 GValue val = G_VALUE_INIT;
538 node = _gtk_style_property_lookup (property_name);
542 g_warning ("Style property \"%s\" is not registered", property_name);
546 val_type = _gtk_style_property_get_value_type (node);
547 if (val_type == G_TYPE_NONE)
549 g_warning ("Style property \"%s\" is not settable", property_name);
553 G_VALUE_COLLECT_INIT (&val, _gtk_style_property_get_value_type (node),
557 g_warning ("Could not set style property \"%s\": %s", property_name, error);
558 g_value_unset (&val);
563 _gtk_style_property_assign (node, props, state, &val);
564 g_value_unset (&val);
566 property_name = va_arg (args, const gchar *);
571 * gtk_style_properties_set:
572 * @props: a #GtkStyleProperties
573 * @state: state to set the values for
574 * @...: property name/value pairs, followed by %NULL
576 * Sets several style properties on @props.
581 gtk_style_properties_set (GtkStyleProperties *props,
587 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
589 va_start (args, state);
590 gtk_style_properties_set_valist (props, state, args);
595 _gtk_style_properties_peek_property (GtkStyleProperties *props,
596 GtkCssStyleProperty *property,
599 GtkStylePropertiesPrivate *priv;
602 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
603 g_return_val_if_fail (property != NULL, FALSE);
606 prop = g_hash_table_lookup (priv->properties, property);
610 return property_data_match_state (prop, state);
614 GtkStyleProperties *props;
619 style_query_func (guint id,
622 StyleQueryData *query = data;
624 return _gtk_style_properties_peek_property (query->props,
625 _gtk_css_style_property_lookup_by_id (id),
630 * gtk_style_properties_get_property:
631 * @props: a #GtkStyleProperties
632 * @property: style property name
633 * @state: state to retrieve the property value for
634 * @value: (out) (transfer full): return location for the style property value.
636 * Gets a style property from @props for the given state. When done with @value,
637 * g_value_unset() needs to be called to free any allocated memory.
639 * Returns: %TRUE if the property exists in @props, %FALSE otherwise
644 gtk_style_properties_get_property (GtkStyleProperties *props,
645 const gchar *property,
649 StyleQueryData query = { props, state };
650 GtkStyleProperty *node;
652 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
653 g_return_val_if_fail (property != NULL, FALSE);
654 g_return_val_if_fail (value != NULL, FALSE);
656 node = _gtk_style_property_lookup (property);
659 g_warning ("Style property \"%s\" is not registered", property);
662 if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
664 g_warning ("Style property \"%s\" is not gettable", property);
668 _gtk_style_property_query (node,
677 * gtk_style_properties_get_valist:
678 * @props: a #GtkStyleProperties
679 * @state: state to retrieve the property values for
680 * @args: va_list of property name/return location pairs, followed by %NULL
682 * Retrieves several style property values from @props for a given state.
687 gtk_style_properties_get_valist (GtkStyleProperties *props,
691 const gchar *property_name;
693 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
695 property_name = va_arg (args, const gchar *);
697 while (property_name)
700 GValue value = G_VALUE_INIT;
702 if (!gtk_style_properties_get_property (props,
708 G_VALUE_LCOPY (&value, args, 0, &error);
709 g_value_unset (&value);
713 g_warning ("Could not get style property \"%s\": %s", property_name, error);
718 property_name = va_arg (args, const gchar *);
723 * gtk_style_properties_get:
724 * @props: a #GtkStyleProperties
725 * @state: state to retrieve the property values for
726 * @...: property name /return value pairs, followed by %NULL
728 * Retrieves several style property values from @props for a
734 gtk_style_properties_get (GtkStyleProperties *props,
740 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
742 va_start (args, state);
743 gtk_style_properties_get_valist (props, state, args);
748 * gtk_style_properties_unset_property:
749 * @props: a #GtkStyleProperties
750 * @property: property to unset
751 * @state: state to unset
753 * Unsets a style property in @props.
758 gtk_style_properties_unset_property (GtkStyleProperties *props,
759 const gchar *property,
762 GtkStylePropertiesPrivate *priv;
763 GtkStyleProperty *node;
767 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
768 g_return_if_fail (property != NULL);
770 node = _gtk_style_property_lookup (property);
774 g_warning ("Style property \"%s\" is not registered", property);
777 if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
779 g_warning ("Style property \"%s\" is not settable", property);
783 if (GTK_IS_CSS_SHORTHAND_PROPERTY (node))
785 GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (node);
787 for (pos = 0; pos < _gtk_css_shorthand_property_get_n_subproperties (shorthand); pos++)
789 GtkCssStyleProperty *sub = _gtk_css_shorthand_property_get_subproperty (shorthand, pos);
790 gtk_style_properties_unset_property (props,
791 _gtk_style_property_get_name (GTK_STYLE_PROPERTY (sub)),
798 prop = g_hash_table_lookup (priv->properties, node);
803 if (property_data_find_position (prop, state, &pos))
807 data = &g_array_index (prop->values, ValueData, pos);
809 _gtk_css_value_unref (data->value);
812 g_array_remove_index (prop->values, pos);
814 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (props));
819 * gtk_style_properties_clear:
820 * @props: a #GtkStyleProperties
822 * Clears all style information from @props.
825 gtk_style_properties_clear (GtkStyleProperties *props)
827 GtkStylePropertiesPrivate *priv;
829 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
832 g_hash_table_remove_all (priv->properties);
834 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (props));
838 * gtk_style_properties_merge:
839 * @props: a #GtkStyleProperties
840 * @props_to_merge: a second #GtkStyleProperties
841 * @replace: whether to replace values or not
843 * Merges into @props all the style information contained
844 * in @props_to_merge. If @replace is %TRUE, the values
845 * will be overwritten, if it is %FALSE, the older values
851 gtk_style_properties_merge (GtkStyleProperties *props,
852 const GtkStyleProperties *props_to_merge,
855 GtkStylePropertiesPrivate *priv, *priv_to_merge;
859 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
860 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props_to_merge));
863 priv_to_merge = props_to_merge->priv;
865 /* Merge symbolic color map */
866 if (priv_to_merge->color_map)
868 g_hash_table_iter_init (&iter, priv_to_merge->color_map);
870 while (g_hash_table_iter_next (&iter, &key, &value))
873 GtkSymbolicColor *color;
879 g_hash_table_lookup (priv->color_map, name))
882 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
883 gtk_style_properties_map_color (props, name, color);
884 G_GNUC_END_IGNORE_DEPRECATIONS;
888 /* Merge symbolic style properties */
889 g_hash_table_iter_init (&iter, priv_to_merge->properties);
891 while (g_hash_table_iter_next (&iter, &key, &value))
893 PropertyData *prop_to_merge = value;
897 prop = g_hash_table_lookup (priv->properties, key);
901 prop = property_data_new ();
902 g_hash_table_insert (priv->properties, key, prop);
905 for (i = 0; i < prop_to_merge->values->len; i++)
910 data = &g_array_index (prop_to_merge->values, ValueData, i);
912 if (replace && data->state == GTK_STATE_FLAG_NORMAL &&
913 _gtk_is_css_typed_value_of_type (data->value, PANGO_TYPE_FONT_DESCRIPTION))
915 /* Let normal state override all states
916 * previously set in the original set
918 property_data_remove_values (prop);
921 value = property_data_get_value (prop, data->state);
923 if (_gtk_is_css_typed_value_of_type (data->value, PANGO_TYPE_FONT_DESCRIPTION) &&
924 value->value != NULL)
926 PangoFontDescription *font_desc;
927 PangoFontDescription *font_desc_to_merge;
929 /* Handle merging of font descriptions */
930 font_desc = g_value_get_boxed (_gtk_css_typed_value_get (value->value));
931 font_desc_to_merge = g_value_get_boxed (_gtk_css_typed_value_get (data->value));
933 pango_font_description_merge (font_desc, font_desc_to_merge, replace);
935 else if (_gtk_is_css_typed_value_of_type (data->value, G_TYPE_PTR_ARRAY) &&
936 value->value != NULL)
938 GPtrArray *array, *array_to_merge;
941 /* Append the array, mainly thought
942 * for the gtk-key-bindings property
944 array = g_value_get_boxed (_gtk_css_typed_value_get (value->value));
945 array_to_merge = g_value_get_boxed (_gtk_css_typed_value_get (data->value));
947 for (i = 0; i < array_to_merge->len; i++)
948 g_ptr_array_add (array, g_ptr_array_index (array_to_merge, i));
950 else if (replace || value->value == NULL)
952 _gtk_css_value_unref (value->value);
953 value->value = _gtk_css_value_ref (data->value);
958 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (props));