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_get_style (GtkStyleProvider *provider,
286 /* Return style set itself */
287 return g_object_ref (provider);
291 gtk_style_properties_provider_init (GtkStyleProviderIface *iface)
293 iface->get_style = gtk_style_properties_get_style;
296 static GtkSymbolicColor *
297 gtk_style_properties_provider_get_color (GtkStyleProviderPrivate *provider,
300 return gtk_style_properties_lookup_color (GTK_STYLE_PROPERTIES (provider), name);
304 gtk_style_properties_provider_lookup (GtkStyleProviderPrivate *provider,
305 const GtkCssMatcher *matcher,
306 GtkCssLookup *lookup)
308 GtkStyleProperties *props;
309 GtkStylePropertiesPrivate *priv;
313 props = GTK_STYLE_PROPERTIES (provider);
316 /* Merge symbolic style properties */
317 g_hash_table_iter_init (&iter, priv->properties);
319 while (g_hash_table_iter_next (&iter, &key, &value))
321 GtkCssStyleProperty *prop = key;
322 PropertyData *data = value;
326 id = _gtk_css_style_property_get_id (prop);
328 if (!_gtk_css_lookup_is_missing (lookup, id))
331 value = property_data_match_state (data, _gtk_css_matcher_get_state (matcher));
335 _gtk_css_lookup_set_computed (lookup, id, NULL, value);
340 gtk_style_properties_provider_get_change (GtkStyleProviderPrivate *provider,
341 const GtkCssMatcher *matcher)
343 return GTK_CSS_CHANGE_STATE;
347 gtk_style_properties_provider_private_init (GtkStyleProviderPrivateInterface *iface)
349 iface->get_color = gtk_style_properties_provider_get_color;
350 iface->lookup = gtk_style_properties_provider_lookup;
351 iface->get_change = gtk_style_properties_provider_get_change;
354 /* GtkStyleProperties methods */
357 * gtk_style_properties_new:
359 * Returns a newly created #GtkStyleProperties
361 * Returns: a new #GtkStyleProperties
364 gtk_style_properties_new (void)
366 return g_object_new (GTK_TYPE_STYLE_PROPERTIES, NULL);
370 * gtk_style_properties_map_color:
371 * @props: a #GtkStyleProperties
373 * @color: #GtkSymbolicColor to map @name to
375 * Maps @color so it can be referenced by @name. See
376 * gtk_style_properties_lookup_color()
381 gtk_style_properties_map_color (GtkStyleProperties *props,
383 GtkSymbolicColor *color)
385 GtkStylePropertiesPrivate *priv;
387 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
388 g_return_if_fail (name != NULL);
389 g_return_if_fail (color != NULL);
393 if (G_UNLIKELY (!priv->color_map))
394 priv->color_map = g_hash_table_new_full (g_str_hash,
396 (GDestroyNotify) g_free,
397 (GDestroyNotify) gtk_symbolic_color_unref);
399 g_hash_table_replace (priv->color_map,
401 gtk_symbolic_color_ref (color));
403 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (props));
407 * gtk_style_properties_lookup_color:
408 * @props: a #GtkStyleProperties
409 * @name: color name to lookup
411 * Returns the symbolic color that is mapped
414 * Returns: (transfer none): The mapped color
419 gtk_style_properties_lookup_color (GtkStyleProperties *props,
422 GtkStylePropertiesPrivate *priv;
424 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
425 g_return_val_if_fail (name != NULL, NULL);
429 if (!priv->color_map)
432 return g_hash_table_lookup (priv->color_map, name);
436 _gtk_style_properties_set_property_by_property (GtkStyleProperties *props,
437 GtkCssStyleProperty *style_prop,
441 GtkStylePropertiesPrivate *priv;
446 prop = g_hash_table_lookup (priv->properties, style_prop);
450 prop = property_data_new ();
451 g_hash_table_insert (priv->properties, (gpointer) style_prop, prop);
454 val = property_data_get_value (prop, state);
456 _gtk_css_value_unref (val->value);
457 val->value = _gtk_css_value_ref (value);
459 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (props));
463 * gtk_style_properties_set_property:
464 * @props: a #GtkStyleProperties
465 * @property: styling property to set
466 * @state: state to set the value for
467 * @value: new value for the property
469 * Sets a styling property in @props.
474 gtk_style_properties_set_property (GtkStyleProperties *props,
475 const gchar *property,
479 GtkStyleProperty *node;
481 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
482 g_return_if_fail (property != NULL);
483 g_return_if_fail (value != NULL);
485 node = _gtk_style_property_lookup (property);
489 g_warning ("Style property \"%s\" is not registered", property);
492 if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
494 g_warning ("Style property \"%s\" is not settable", property);
498 _gtk_style_property_assign (node, props, state, value);
502 * gtk_style_properties_set_valist:
503 * @props: a #GtkStyleProperties
504 * @state: state to set the values for
505 * @args: va_list of property name/value pairs, followed by %NULL
507 * Sets several style properties on @props.
512 gtk_style_properties_set_valist (GtkStyleProperties *props,
516 const gchar *property_name;
518 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
520 property_name = va_arg (args, const gchar *);
522 while (property_name)
524 GtkStyleProperty *node;
527 GValue val = G_VALUE_INIT;
529 node = _gtk_style_property_lookup (property_name);
533 g_warning ("Style property \"%s\" is not registered", property_name);
537 val_type = _gtk_style_property_get_value_type (node);
538 if (val_type == G_TYPE_NONE)
540 g_warning ("Style property \"%s\" is not settable", property_name);
544 G_VALUE_COLLECT_INIT (&val, _gtk_style_property_get_value_type (node),
548 g_warning ("Could not set style property \"%s\": %s", property_name, error);
549 g_value_unset (&val);
554 _gtk_style_property_assign (node, props, state, &val);
555 g_value_unset (&val);
557 property_name = va_arg (args, const gchar *);
562 * gtk_style_properties_set:
563 * @props: a #GtkStyleProperties
564 * @state: state to set the values for
565 * @...: property name/value pairs, followed by %NULL
567 * Sets several style properties on @props.
572 gtk_style_properties_set (GtkStyleProperties *props,
578 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
580 va_start (args, state);
581 gtk_style_properties_set_valist (props, state, args);
586 _gtk_style_properties_peek_property (GtkStyleProperties *props,
587 GtkCssStyleProperty *property,
590 GtkStylePropertiesPrivate *priv;
593 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
594 g_return_val_if_fail (property != NULL, FALSE);
597 prop = g_hash_table_lookup (priv->properties, property);
601 return property_data_match_state (prop, state);
605 GtkStyleProperties *props;
610 style_query_func (guint id,
613 StyleQueryData *query = data;
615 return _gtk_style_properties_peek_property (query->props,
616 _gtk_css_style_property_lookup_by_id (id),
621 * gtk_style_properties_get_property:
622 * @props: a #GtkStyleProperties
623 * @property: style property name
624 * @state: state to retrieve the property value for
625 * @value: (out) (transfer full): return location for the style property value.
627 * Gets a style property from @props for the given state. When done with @value,
628 * g_value_unset() needs to be called to free any allocated memory.
630 * Returns: %TRUE if the property exists in @props, %FALSE otherwise
635 gtk_style_properties_get_property (GtkStyleProperties *props,
636 const gchar *property,
640 StyleQueryData query = { props, state };
641 GtkStyleProperty *node;
643 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
644 g_return_val_if_fail (property != NULL, FALSE);
645 g_return_val_if_fail (value != NULL, FALSE);
647 node = _gtk_style_property_lookup (property);
650 g_warning ("Style property \"%s\" is not registered", property);
653 if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
655 g_warning ("Style property \"%s\" is not gettable", property);
659 _gtk_style_property_query (node,
668 * gtk_style_properties_get_valist:
669 * @props: a #GtkStyleProperties
670 * @state: state to retrieve the property values for
671 * @args: va_list of property name/return location pairs, followed by %NULL
673 * Retrieves several style property values from @props for a given state.
678 gtk_style_properties_get_valist (GtkStyleProperties *props,
682 const gchar *property_name;
684 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
686 property_name = va_arg (args, const gchar *);
688 while (property_name)
691 GValue value = G_VALUE_INIT;
693 if (!gtk_style_properties_get_property (props,
699 G_VALUE_LCOPY (&value, args, 0, &error);
700 g_value_unset (&value);
704 g_warning ("Could not get style property \"%s\": %s", property_name, error);
709 property_name = va_arg (args, const gchar *);
714 * gtk_style_properties_get:
715 * @props: a #GtkStyleProperties
716 * @state: state to retrieve the property values for
717 * @...: property name /return value pairs, followed by %NULL
719 * Retrieves several style property values from @props for a
725 gtk_style_properties_get (GtkStyleProperties *props,
731 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
733 va_start (args, state);
734 gtk_style_properties_get_valist (props, state, args);
739 * gtk_style_properties_unset_property:
740 * @props: a #GtkStyleProperties
741 * @property: property to unset
742 * @state: state to unset
744 * Unsets a style property in @props.
749 gtk_style_properties_unset_property (GtkStyleProperties *props,
750 const gchar *property,
753 GtkStylePropertiesPrivate *priv;
754 GtkStyleProperty *node;
758 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
759 g_return_if_fail (property != NULL);
761 node = _gtk_style_property_lookup (property);
765 g_warning ("Style property \"%s\" is not registered", property);
768 if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
770 g_warning ("Style property \"%s\" is not settable", property);
774 if (GTK_IS_CSS_SHORTHAND_PROPERTY (node))
776 GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (node);
778 for (pos = 0; pos < _gtk_css_shorthand_property_get_n_subproperties (shorthand); pos++)
780 GtkCssStyleProperty *sub = _gtk_css_shorthand_property_get_subproperty (shorthand, pos);
781 gtk_style_properties_unset_property (props,
782 _gtk_style_property_get_name (GTK_STYLE_PROPERTY (sub)),
789 prop = g_hash_table_lookup (priv->properties, node);
794 if (property_data_find_position (prop, state, &pos))
798 data = &g_array_index (prop->values, ValueData, pos);
800 _gtk_css_value_unref (data->value);
803 g_array_remove_index (prop->values, pos);
805 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (props));
810 * gtk_style_properties_clear:
811 * @props: a #GtkStyleProperties
813 * Clears all style information from @props.
816 gtk_style_properties_clear (GtkStyleProperties *props)
818 GtkStylePropertiesPrivate *priv;
820 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
823 g_hash_table_remove_all (priv->properties);
825 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (props));
829 * gtk_style_properties_merge:
830 * @props: a #GtkStyleProperties
831 * @props_to_merge: a second #GtkStyleProperties
832 * @replace: whether to replace values or not
834 * Merges into @props all the style information contained
835 * in @props_to_merge. If @replace is %TRUE, the values
836 * will be overwritten, if it is %FALSE, the older values
842 gtk_style_properties_merge (GtkStyleProperties *props,
843 const GtkStyleProperties *props_to_merge,
846 GtkStylePropertiesPrivate *priv, *priv_to_merge;
850 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
851 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props_to_merge));
854 priv_to_merge = props_to_merge->priv;
856 /* Merge symbolic color map */
857 if (priv_to_merge->color_map)
859 g_hash_table_iter_init (&iter, priv_to_merge->color_map);
861 while (g_hash_table_iter_next (&iter, &key, &value))
864 GtkSymbolicColor *color;
870 g_hash_table_lookup (priv->color_map, name))
873 gtk_style_properties_map_color (props, name, color);
877 /* Merge symbolic style properties */
878 g_hash_table_iter_init (&iter, priv_to_merge->properties);
880 while (g_hash_table_iter_next (&iter, &key, &value))
882 PropertyData *prop_to_merge = value;
886 prop = g_hash_table_lookup (priv->properties, key);
890 prop = property_data_new ();
891 g_hash_table_insert (priv->properties, key, prop);
894 for (i = 0; i < prop_to_merge->values->len; i++)
899 data = &g_array_index (prop_to_merge->values, ValueData, i);
901 if (replace && data->state == GTK_STATE_FLAG_NORMAL &&
902 _gtk_is_css_typed_value_of_type (data->value, PANGO_TYPE_FONT_DESCRIPTION))
904 /* Let normal state override all states
905 * previously set in the original set
907 property_data_remove_values (prop);
910 value = property_data_get_value (prop, data->state);
912 if (_gtk_is_css_typed_value_of_type (data->value, PANGO_TYPE_FONT_DESCRIPTION) &&
913 value->value != NULL)
915 PangoFontDescription *font_desc;
916 PangoFontDescription *font_desc_to_merge;
918 /* Handle merging of font descriptions */
919 font_desc = g_value_get_boxed (_gtk_css_typed_value_get (value->value));
920 font_desc_to_merge = g_value_get_boxed (_gtk_css_typed_value_get (data->value));
922 pango_font_description_merge (font_desc, font_desc_to_merge, replace);
924 else if (_gtk_is_css_typed_value_of_type (data->value, G_TYPE_PTR_ARRAY) &&
925 value->value != NULL)
927 GPtrArray *array, *array_to_merge;
930 /* Append the array, mainly thought
931 * for the gtk-key-bindings property
933 array = g_value_get_boxed (_gtk_css_typed_value_get (value->value));
934 array_to_merge = g_value_get_boxed (_gtk_css_typed_value_get (data->value));
936 for (i = 0; i < array_to_merge->len; i++)
937 g_ptr_array_add (array, g_ptr_array_index (array_to_merge, i));
939 else if (replace || value->value == NULL)
941 _gtk_css_value_unref (value->value);
942 value->value = _gtk_css_value_ref (data->value);
947 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (props));