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 "gtkgradient.h"
30 #include "gtkcssshorthandpropertyprivate.h"
31 #include "gtkcsstypedvalueprivate.h"
32 #include "gtkcsstypesprivate.h"
33 #include "gtkborderimageprivate.h"
35 #include "gtkprivatetypebuiltins.h"
36 #include "gtkstylepropertyprivate.h"
37 #include "gtkstyleproviderprivate.h"
40 #include "gtkwin32themeprivate.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 return _gtk_symbolic_color_get_css_value (gtk_style_properties_lookup_color (GTK_STYLE_PROPERTIES (provider), name));
295 gtk_style_properties_provider_lookup (GtkStyleProviderPrivate *provider,
296 const GtkCssMatcher *matcher,
297 GtkCssLookup *lookup)
299 GtkStyleProperties *props;
300 GtkStylePropertiesPrivate *priv;
304 props = GTK_STYLE_PROPERTIES (provider);
307 /* Merge symbolic style properties */
308 g_hash_table_iter_init (&iter, priv->properties);
310 while (g_hash_table_iter_next (&iter, &key, &value))
312 GtkCssStyleProperty *prop = key;
313 PropertyData *data = value;
317 id = _gtk_css_style_property_get_id (prop);
319 if (!_gtk_css_lookup_is_missing (lookup, id))
322 value = property_data_match_state (data, _gtk_css_matcher_get_state (matcher));
326 _gtk_css_lookup_set_computed (lookup, id, NULL, value);
331 gtk_style_properties_provider_get_change (GtkStyleProviderPrivate *provider,
332 const GtkCssMatcher *matcher)
334 return GTK_CSS_CHANGE_STATE;
338 gtk_style_properties_provider_private_init (GtkStyleProviderPrivateInterface *iface)
340 iface->get_color = gtk_style_properties_provider_get_color;
341 iface->lookup = gtk_style_properties_provider_lookup;
342 iface->get_change = gtk_style_properties_provider_get_change;
345 /* GtkStyleProperties methods */
348 * gtk_style_properties_new:
350 * Returns a newly created #GtkStyleProperties
352 * Returns: a new #GtkStyleProperties
355 gtk_style_properties_new (void)
357 return g_object_new (GTK_TYPE_STYLE_PROPERTIES, NULL);
361 * gtk_style_properties_map_color:
362 * @props: a #GtkStyleProperties
364 * @color: #GtkSymbolicColor to map @name to
366 * Maps @color so it can be referenced by @name. See
367 * gtk_style_properties_lookup_color()
372 gtk_style_properties_map_color (GtkStyleProperties *props,
374 GtkSymbolicColor *color)
376 GtkStylePropertiesPrivate *priv;
378 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
379 g_return_if_fail (name != NULL);
380 g_return_if_fail (color != NULL);
384 if (G_UNLIKELY (!priv->color_map))
385 priv->color_map = g_hash_table_new_full (g_str_hash,
387 (GDestroyNotify) g_free,
388 (GDestroyNotify) gtk_symbolic_color_unref);
390 g_hash_table_replace (priv->color_map,
392 gtk_symbolic_color_ref (color));
394 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (props));
398 * gtk_style_properties_lookup_color:
399 * @props: a #GtkStyleProperties
400 * @name: color name to lookup
402 * Returns the symbolic color that is mapped
405 * Returns: (transfer none): The mapped color
410 gtk_style_properties_lookup_color (GtkStyleProperties *props,
413 GtkStylePropertiesPrivate *priv;
415 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
416 g_return_val_if_fail (name != NULL, NULL);
420 if (!priv->color_map)
423 return g_hash_table_lookup (priv->color_map, name);
427 _gtk_style_properties_set_property_by_property (GtkStyleProperties *props,
428 GtkCssStyleProperty *style_prop,
432 GtkStylePropertiesPrivate *priv;
437 prop = g_hash_table_lookup (priv->properties, style_prop);
441 prop = property_data_new ();
442 g_hash_table_insert (priv->properties, (gpointer) style_prop, prop);
445 val = property_data_get_value (prop, state);
447 _gtk_css_value_unref (val->value);
448 val->value = _gtk_css_value_ref (value);
450 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (props));
454 * gtk_style_properties_set_property:
455 * @props: a #GtkStyleProperties
456 * @property: styling property to set
457 * @state: state to set the value for
458 * @value: new value for the property
460 * Sets a styling property in @props.
465 gtk_style_properties_set_property (GtkStyleProperties *props,
466 const gchar *property,
470 GtkStyleProperty *node;
472 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
473 g_return_if_fail (property != NULL);
474 g_return_if_fail (value != NULL);
476 node = _gtk_style_property_lookup (property);
480 g_warning ("Style property \"%s\" is not registered", property);
483 if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
485 g_warning ("Style property \"%s\" is not settable", property);
489 _gtk_style_property_assign (node, props, state, value);
493 * gtk_style_properties_set_valist:
494 * @props: a #GtkStyleProperties
495 * @state: state to set the values for
496 * @args: va_list of property name/value pairs, followed by %NULL
498 * Sets several style properties on @props.
503 gtk_style_properties_set_valist (GtkStyleProperties *props,
507 const gchar *property_name;
509 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
511 property_name = va_arg (args, const gchar *);
513 while (property_name)
515 GtkStyleProperty *node;
518 GValue val = G_VALUE_INIT;
520 node = _gtk_style_property_lookup (property_name);
524 g_warning ("Style property \"%s\" is not registered", property_name);
528 val_type = _gtk_style_property_get_value_type (node);
529 if (val_type == G_TYPE_NONE)
531 g_warning ("Style property \"%s\" is not settable", property_name);
535 G_VALUE_COLLECT_INIT (&val, _gtk_style_property_get_value_type (node),
539 g_warning ("Could not set style property \"%s\": %s", property_name, error);
540 g_value_unset (&val);
545 _gtk_style_property_assign (node, props, state, &val);
546 g_value_unset (&val);
548 property_name = va_arg (args, const gchar *);
553 * gtk_style_properties_set:
554 * @props: a #GtkStyleProperties
555 * @state: state to set the values for
556 * @...: property name/value pairs, followed by %NULL
558 * Sets several style properties on @props.
563 gtk_style_properties_set (GtkStyleProperties *props,
569 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
571 va_start (args, state);
572 gtk_style_properties_set_valist (props, state, args);
577 _gtk_style_properties_peek_property (GtkStyleProperties *props,
578 GtkCssStyleProperty *property,
581 GtkStylePropertiesPrivate *priv;
584 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
585 g_return_val_if_fail (property != NULL, FALSE);
588 prop = g_hash_table_lookup (priv->properties, property);
592 return property_data_match_state (prop, state);
596 GtkStyleProperties *props;
601 style_query_func (guint id,
604 StyleQueryData *query = data;
606 return _gtk_style_properties_peek_property (query->props,
607 _gtk_css_style_property_lookup_by_id (id),
612 * gtk_style_properties_get_property:
613 * @props: a #GtkStyleProperties
614 * @property: style property name
615 * @state: state to retrieve the property value for
616 * @value: (out) (transfer full): return location for the style property value.
618 * Gets a style property from @props for the given state. When done with @value,
619 * g_value_unset() needs to be called to free any allocated memory.
621 * Returns: %TRUE if the property exists in @props, %FALSE otherwise
626 gtk_style_properties_get_property (GtkStyleProperties *props,
627 const gchar *property,
631 StyleQueryData query = { props, state };
632 GtkStyleProperty *node;
634 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
635 g_return_val_if_fail (property != NULL, FALSE);
636 g_return_val_if_fail (value != NULL, FALSE);
638 node = _gtk_style_property_lookup (property);
641 g_warning ("Style property \"%s\" is not registered", property);
644 if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
646 g_warning ("Style property \"%s\" is not gettable", property);
650 _gtk_style_property_query (node,
659 * gtk_style_properties_get_valist:
660 * @props: a #GtkStyleProperties
661 * @state: state to retrieve the property values for
662 * @args: va_list of property name/return location pairs, followed by %NULL
664 * Retrieves several style property values from @props for a given state.
669 gtk_style_properties_get_valist (GtkStyleProperties *props,
673 const gchar *property_name;
675 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
677 property_name = va_arg (args, const gchar *);
679 while (property_name)
682 GValue value = G_VALUE_INIT;
684 if (!gtk_style_properties_get_property (props,
690 G_VALUE_LCOPY (&value, args, 0, &error);
691 g_value_unset (&value);
695 g_warning ("Could not get style property \"%s\": %s", property_name, error);
700 property_name = va_arg (args, const gchar *);
705 * gtk_style_properties_get:
706 * @props: a #GtkStyleProperties
707 * @state: state to retrieve the property values for
708 * @...: property name /return value pairs, followed by %NULL
710 * Retrieves several style property values from @props for a
716 gtk_style_properties_get (GtkStyleProperties *props,
722 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
724 va_start (args, state);
725 gtk_style_properties_get_valist (props, state, args);
730 * gtk_style_properties_unset_property:
731 * @props: a #GtkStyleProperties
732 * @property: property to unset
733 * @state: state to unset
735 * Unsets a style property in @props.
740 gtk_style_properties_unset_property (GtkStyleProperties *props,
741 const gchar *property,
744 GtkStylePropertiesPrivate *priv;
745 GtkStyleProperty *node;
749 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
750 g_return_if_fail (property != NULL);
752 node = _gtk_style_property_lookup (property);
756 g_warning ("Style property \"%s\" is not registered", property);
759 if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
761 g_warning ("Style property \"%s\" is not settable", property);
765 if (GTK_IS_CSS_SHORTHAND_PROPERTY (node))
767 GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (node);
769 for (pos = 0; pos < _gtk_css_shorthand_property_get_n_subproperties (shorthand); pos++)
771 GtkCssStyleProperty *sub = _gtk_css_shorthand_property_get_subproperty (shorthand, pos);
772 gtk_style_properties_unset_property (props,
773 _gtk_style_property_get_name (GTK_STYLE_PROPERTY (sub)),
780 prop = g_hash_table_lookup (priv->properties, node);
785 if (property_data_find_position (prop, state, &pos))
789 data = &g_array_index (prop->values, ValueData, pos);
791 _gtk_css_value_unref (data->value);
794 g_array_remove_index (prop->values, pos);
796 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (props));
801 * gtk_style_properties_clear:
802 * @props: a #GtkStyleProperties
804 * Clears all style information from @props.
807 gtk_style_properties_clear (GtkStyleProperties *props)
809 GtkStylePropertiesPrivate *priv;
811 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
814 g_hash_table_remove_all (priv->properties);
816 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (props));
820 * gtk_style_properties_merge:
821 * @props: a #GtkStyleProperties
822 * @props_to_merge: a second #GtkStyleProperties
823 * @replace: whether to replace values or not
825 * Merges into @props all the style information contained
826 * in @props_to_merge. If @replace is %TRUE, the values
827 * will be overwritten, if it is %FALSE, the older values
833 gtk_style_properties_merge (GtkStyleProperties *props,
834 const GtkStyleProperties *props_to_merge,
837 GtkStylePropertiesPrivate *priv, *priv_to_merge;
841 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
842 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props_to_merge));
845 priv_to_merge = props_to_merge->priv;
847 /* Merge symbolic color map */
848 if (priv_to_merge->color_map)
850 g_hash_table_iter_init (&iter, priv_to_merge->color_map);
852 while (g_hash_table_iter_next (&iter, &key, &value))
855 GtkSymbolicColor *color;
861 g_hash_table_lookup (priv->color_map, name))
864 gtk_style_properties_map_color (props, name, color);
868 /* Merge symbolic style properties */
869 g_hash_table_iter_init (&iter, priv_to_merge->properties);
871 while (g_hash_table_iter_next (&iter, &key, &value))
873 PropertyData *prop_to_merge = value;
877 prop = g_hash_table_lookup (priv->properties, key);
881 prop = property_data_new ();
882 g_hash_table_insert (priv->properties, key, prop);
885 for (i = 0; i < prop_to_merge->values->len; i++)
890 data = &g_array_index (prop_to_merge->values, ValueData, i);
892 if (replace && data->state == GTK_STATE_FLAG_NORMAL &&
893 _gtk_is_css_typed_value_of_type (data->value, PANGO_TYPE_FONT_DESCRIPTION))
895 /* Let normal state override all states
896 * previously set in the original set
898 property_data_remove_values (prop);
901 value = property_data_get_value (prop, data->state);
903 if (_gtk_is_css_typed_value_of_type (data->value, PANGO_TYPE_FONT_DESCRIPTION) &&
904 value->value != NULL)
906 PangoFontDescription *font_desc;
907 PangoFontDescription *font_desc_to_merge;
909 /* Handle merging of font descriptions */
910 font_desc = g_value_get_boxed (_gtk_css_typed_value_get (value->value));
911 font_desc_to_merge = g_value_get_boxed (_gtk_css_typed_value_get (data->value));
913 pango_font_description_merge (font_desc, font_desc_to_merge, replace);
915 else if (_gtk_is_css_typed_value_of_type (data->value, G_TYPE_PTR_ARRAY) &&
916 value->value != NULL)
918 GPtrArray *array, *array_to_merge;
921 /* Append the array, mainly thought
922 * for the gtk-key-bindings property
924 array = g_value_get_boxed (_gtk_css_typed_value_get (value->value));
925 array_to_merge = g_value_get_boxed (_gtk_css_typed_value_get (data->value));
927 for (i = 0; i < array_to_merge->len; i++)
928 g_ptr_array_add (array, g_ptr_array_index (array_to_merge, i));
930 else if (replace || value->value == NULL)
932 _gtk_css_value_unref (value->value);
933 value->value = _gtk_css_value_ref (data->value);
938 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (props));