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 "gtkstylepropertiesprivate.h"
25 #include <gobject/gvaluecollector.h>
26 #include <cairo-gobject.h>
28 #include "gtkstyleprovider.h"
29 #include "gtksymboliccolor.h"
30 #include "gtkthemingengine.h"
31 #include "gtkanimationdescription.h"
32 #include "gtkgradient.h"
33 #include "gtkshadowprivate.h"
34 #include "gtkcssshorthandpropertyprivate.h"
35 #include "gtkcsstypesprivate.h"
36 #include "gtkborderimageprivate.h"
38 #include "gtkprivatetypebuiltins.h"
39 #include "gtkstylepropertyprivate.h"
40 #include "gtkstyleproviderprivate.h"
43 #include "gtkwin32themeprivate.h"
46 * SECTION:gtkstyleproperties
47 * @Short_description: Store for style property information
48 * @Title: GtkStyleProperties
50 * GtkStyleProperties provides the storage for style information
51 * that is used by #GtkStyleContext and other #GtkStyleProvider
54 * Before style properties can be stored in GtkStyleProperties, they
55 * must be registered with gtk_style_properties_register_property().
57 * Unless you are writing a #GtkStyleProvider implementation, you
58 * are unlikely to use this API directly, as gtk_style_context_get()
59 * and its variants are the preferred way to access styling information
60 * from widget implementations and theming engine implementations
61 * should use the APIs provided by #GtkThemingEngine instead.
64 typedef struct PropertyData PropertyData;
65 typedef struct ValueData ValueData;
78 struct _GtkStylePropertiesPrivate
80 GHashTable *color_map;
81 GHashTable *properties;
82 GtkSymbolicColorLookupFunc color_lookup_func;
83 gpointer color_lookup_data;
86 static void gtk_style_properties_provider_init (GtkStyleProviderIface *iface);
87 static void gtk_style_properties_provider_private_init (GtkStyleProviderPrivateInterface *iface);
88 static void gtk_style_properties_finalize (GObject *object);
91 G_DEFINE_TYPE_EXTENDED (GtkStyleProperties, gtk_style_properties, G_TYPE_OBJECT, 0,
92 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
93 gtk_style_properties_provider_init)
94 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER_PRIVATE,
95 gtk_style_properties_provider_private_init));
98 gtk_style_properties_class_init (GtkStylePropertiesClass *klass)
100 GObjectClass *object_class = G_OBJECT_CLASS (klass);
102 object_class->finalize = gtk_style_properties_finalize;
104 g_type_class_add_private (object_class, sizeof (GtkStylePropertiesPrivate));
107 static PropertyData *
108 property_data_new (void)
112 data = g_slice_new0 (PropertyData);
113 data->values = g_array_new (FALSE, FALSE, sizeof (ValueData));
119 property_data_remove_values (PropertyData *data)
123 for (i = 0; i < data->values->len; i++)
125 ValueData *value_data;
127 value_data = &g_array_index (data->values, ValueData, i);
129 if (G_IS_VALUE (&value_data->value))
130 g_value_unset (&value_data->value);
133 if (data->values->len > 0)
134 g_array_remove_range (data->values, 0, data->values->len);
138 property_data_free (PropertyData *data)
140 property_data_remove_values (data);
141 g_array_free (data->values, TRUE);
142 g_slice_free (PropertyData, data);
146 property_data_find_position (PropertyData *data,
151 gboolean found = FALSE;
157 if (data->values->len == 0)
160 /* Find position for the given state, or the position where
161 * it would be if not found, the array is ordered by the
165 max = data->values->len - 1;
169 ValueData *value_data;
171 mid = (min + max) / 2;
172 value_data = &g_array_index (data->values, ValueData, mid);
174 if (value_data->state == state)
179 else if (value_data->state < state)
180 position = min = mid + 1;
187 while (!found && min <= max);
196 property_data_get_value (PropertyData *data,
202 if (!property_data_find_position (data, state, &pos))
204 ValueData new = { 0 };
207 g_array_insert_val (data->values, pos, new);
210 val_data = &g_array_index (data->values, ValueData, pos);
212 return &val_data->value;
216 property_data_match_state (PropertyData *data,
222 if (property_data_find_position (data, state, &pos))
227 val_data = &g_array_index (data->values, ValueData, pos);
228 return &val_data->value;
231 if (pos >= data->values->len)
232 pos = data->values->len - 1;
234 /* No exact match, go downwards the list to find
235 * the closest match to the given state flags, as
236 * a side effect, there is an implicit precedence
237 * of higher flags over the smaller ones.
239 for (i = pos; i >= 0; i--)
243 val_data = &g_array_index (data->values, ValueData, i);
245 /* Check whether any of the requested
246 * flags are set, and no other flags are.
248 * Also, no flags acts as a wildcard, such
249 * value should be always in the first position
250 * in the array (if present) anyways.
252 if (val_data->state == 0 ||
253 ((val_data->state & state) != 0 &&
254 (val_data->state & ~state) == 0))
255 return &val_data->value;
262 gtk_style_properties_init (GtkStyleProperties *props)
264 GtkStylePropertiesPrivate *priv;
266 priv = props->priv = G_TYPE_INSTANCE_GET_PRIVATE (props,
267 GTK_TYPE_STYLE_PROPERTIES,
268 GtkStylePropertiesPrivate);
270 priv->properties = g_hash_table_new_full (NULL, NULL, NULL,
271 (GDestroyNotify) property_data_free);
275 gtk_style_properties_finalize (GObject *object)
277 GtkStylePropertiesPrivate *priv;
278 GtkStyleProperties *props;
280 props = GTK_STYLE_PROPERTIES (object);
282 g_hash_table_destroy (priv->properties);
285 g_hash_table_destroy (priv->color_map);
287 G_OBJECT_CLASS (gtk_style_properties_parent_class)->finalize (object);
291 gtk_style_properties_get_style (GtkStyleProvider *provider,
294 /* Return style set itself */
295 return g_object_ref (provider);
299 gtk_style_properties_provider_init (GtkStyleProviderIface *iface)
301 iface->get_style = gtk_style_properties_get_style;
304 static GtkSymbolicColor *
305 gtk_style_properties_provider_get_color (GtkStyleProviderPrivate *provider,
308 return gtk_style_properties_lookup_color (GTK_STYLE_PROPERTIES (provider), name);
312 gtk_style_properties_provider_lookup (GtkStyleProviderPrivate *provider,
315 GtkCssLookup *lookup)
317 GtkStyleProperties *props;
318 GtkStylePropertiesPrivate *priv;
322 props = GTK_STYLE_PROPERTIES (provider);
325 /* Merge symbolic style properties */
326 g_hash_table_iter_init (&iter, priv->properties);
328 while (g_hash_table_iter_next (&iter, &key, &value))
330 GtkCssStyleProperty *prop = key;
331 PropertyData *data = value;
335 id = _gtk_css_style_property_get_id (prop);
337 if (!_gtk_css_lookup_is_missing (lookup, id))
340 value = property_data_match_state (data, state);
344 _gtk_css_lookup_set (lookup, id, NULL, value);
349 gtk_style_properties_provider_private_init (GtkStyleProviderPrivateInterface *iface)
351 iface->get_color = gtk_style_properties_provider_get_color;
352 iface->lookup = gtk_style_properties_provider_lookup;
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_set_color_lookup_func (GtkStyleProperties *props,
372 GtkSymbolicColorLookupFunc func,
375 GtkStylePropertiesPrivate *priv;
377 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
378 g_return_if_fail (func != NULL);
381 g_return_if_fail (priv->color_map == NULL);
383 priv->color_lookup_func = func;
384 priv->color_lookup_data = data;
388 * gtk_style_properties_map_color:
389 * @props: a #GtkStyleProperties
391 * @color: #GtkSymbolicColor to map @name to
393 * Maps @color so it can be referenced by @name. See
394 * gtk_style_properties_lookup_color()
399 gtk_style_properties_map_color (GtkStyleProperties *props,
401 GtkSymbolicColor *color)
403 GtkStylePropertiesPrivate *priv;
405 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
406 g_return_if_fail (name != NULL);
407 g_return_if_fail (color != NULL);
410 g_return_if_fail (priv->color_lookup_func == NULL);
412 if (G_UNLIKELY (!priv->color_map))
413 priv->color_map = g_hash_table_new_full (g_str_hash,
415 (GDestroyNotify) g_free,
416 (GDestroyNotify) gtk_symbolic_color_unref);
418 g_hash_table_replace (priv->color_map,
420 gtk_symbolic_color_ref (color));
424 * gtk_style_properties_lookup_color:
425 * @props: a #GtkStyleProperties
426 * @name: color name to lookup
428 * Returns the symbolic color that is mapped
431 * Returns: (transfer none): The mapped color
436 gtk_style_properties_lookup_color (GtkStyleProperties *props,
439 GtkStylePropertiesPrivate *priv;
441 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
442 g_return_val_if_fail (name != NULL, NULL);
446 if (priv->color_lookup_func)
447 return priv->color_lookup_func (priv->color_lookup_data, name);
449 if (!priv->color_map)
452 return g_hash_table_lookup (priv->color_map, name);
456 _gtk_style_properties_set_property_by_property (GtkStyleProperties *props,
457 GtkCssStyleProperty *style_prop,
461 GtkStylePropertiesPrivate *priv;
466 prop = g_hash_table_lookup (priv->properties, style_prop);
470 prop = property_data_new ();
471 g_hash_table_insert (priv->properties, (gpointer) style_prop, prop);
474 val = property_data_get_value (prop, state);
476 if (G_VALUE_TYPE (val) == G_VALUE_TYPE (value))
480 if (G_IS_VALUE (val))
483 g_value_init (val, G_VALUE_TYPE (value));
486 g_value_copy (value, val);
490 * gtk_style_properties_set_property:
491 * @props: a #GtkStyleProperties
492 * @property: styling property to set
493 * @state: state to set the value for
494 * @value: new value for the property
496 * Sets a styling property in @props.
501 gtk_style_properties_set_property (GtkStyleProperties *props,
502 const gchar *property,
506 GtkStyleProperty *node;
508 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
509 g_return_if_fail (property != NULL);
510 g_return_if_fail (value != NULL);
512 node = _gtk_style_property_lookup (property);
516 g_warning ("Style property \"%s\" is not registered", property);
519 if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
521 g_warning ("Style property \"%s\" is not settable", property);
525 _gtk_style_property_assign (node, props, state, value);
529 * gtk_style_properties_set_valist:
530 * @props: a #GtkStyleProperties
531 * @state: state to set the values for
532 * @args: va_list of property name/value pairs, followed by %NULL
534 * Sets several style properties on @props.
539 gtk_style_properties_set_valist (GtkStyleProperties *props,
543 const gchar *property_name;
545 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
547 property_name = va_arg (args, const gchar *);
549 while (property_name)
551 GtkStyleProperty *node;
554 GValue val = G_VALUE_INIT;
556 node = _gtk_style_property_lookup (property_name);
560 g_warning ("Style property \"%s\" is not registered", property_name);
564 val_type = _gtk_style_property_get_value_type (node);
565 if (val_type == G_TYPE_NONE)
567 g_warning ("Style property \"%s\" is not settable", property_name);
571 G_VALUE_COLLECT_INIT (&val, _gtk_style_property_get_value_type (node),
575 g_warning ("Could not set style property \"%s\": %s", property_name, error);
576 g_value_unset (&val);
581 _gtk_style_property_assign (node, props, state, &val);
582 g_value_unset (&val);
584 property_name = va_arg (args, const gchar *);
589 * gtk_style_properties_set:
590 * @props: a #GtkStyleProperties
591 * @state: state to set the values for
592 * @...: property name/value pairs, followed by %NULL
594 * Sets several style properties on @props.
599 gtk_style_properties_set (GtkStyleProperties *props,
605 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
607 va_start (args, state);
608 gtk_style_properties_set_valist (props, state, args);
613 _gtk_style_properties_peek_property (GtkStyleProperties *props,
614 GtkCssStyleProperty *property,
617 GtkStylePropertiesPrivate *priv;
620 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
621 g_return_val_if_fail (property != NULL, FALSE);
624 prop = g_hash_table_lookup (priv->properties, property);
628 return property_data_match_state (prop, state);
632 _gtk_style_properties_get_property (GtkStyleProperties *props,
633 const gchar *property,
635 GtkStylePropertyContext *context,
638 GtkStyleProperty *node;
640 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
641 g_return_val_if_fail (property != NULL, FALSE);
642 g_return_val_if_fail (value != NULL, FALSE);
644 node = _gtk_style_property_lookup (property);
647 g_warning ("Style property \"%s\" is not registered", property);
650 if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
652 g_warning ("Style property \"%s\" is not gettable", property);
656 _gtk_style_property_query (node, props, state, context, value);
661 * gtk_style_properties_get_property:
662 * @props: a #GtkStyleProperties
663 * @property: style property name
664 * @state: state to retrieve the property value for
665 * @value: (out) (transfer full): return location for the style property value.
667 * Gets a style property from @props for the given state. When done with @value,
668 * g_value_unset() needs to be called to free any allocated memory.
670 * Returns: %TRUE if the property exists in @props, %FALSE otherwise
675 gtk_style_properties_get_property (GtkStyleProperties *props,
676 const gchar *property,
680 GtkStylePropertyContext context = { 100, 100};
682 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
683 g_return_val_if_fail (property != NULL, FALSE);
684 g_return_val_if_fail (value != NULL, FALSE);
686 return _gtk_style_properties_get_property (props,
688 state, &context, value);
692 _gtk_style_properties_get_valist (GtkStyleProperties *props,
694 GtkStylePropertyContext *context,
697 const gchar *property_name;
699 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
701 property_name = va_arg (args, const gchar *);
703 while (property_name)
706 GValue value = G_VALUE_INIT;
708 if (!_gtk_style_properties_get_property (props,
715 G_VALUE_LCOPY (&value, args, 0, &error);
716 g_value_unset (&value);
720 g_warning ("Could not get style property \"%s\": %s", property_name, error);
725 property_name = va_arg (args, const gchar *);
730 * gtk_style_properties_get_valist:
731 * @props: a #GtkStyleProperties
732 * @state: state to retrieve the property values for
733 * @args: va_list of property name/return location pairs, followed by %NULL
735 * Retrieves several style property values from @props for a given state.
740 gtk_style_properties_get_valist (GtkStyleProperties *props,
744 GtkStylePropertyContext context = { 100, 100};
746 return _gtk_style_properties_get_valist (props, state, &context, args);
750 _gtk_style_properties_get (GtkStyleProperties *props,
752 GtkStylePropertyContext *context,
757 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
759 va_start (args, context);
760 _gtk_style_properties_get_valist (props, state, context, args);
765 * gtk_style_properties_get:
766 * @props: a #GtkStyleProperties
767 * @state: state to retrieve the property values for
768 * @...: property name /return value pairs, followed by %NULL
770 * Retrieves several style property values from @props for a
776 gtk_style_properties_get (GtkStyleProperties *props,
782 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
784 va_start (args, state);
785 gtk_style_properties_get_valist (props, state, args);
790 * gtk_style_properties_unset_property:
791 * @props: a #GtkStyleProperties
792 * @property: property to unset
793 * @state: state to unset
795 * Unsets a style property in @props.
800 gtk_style_properties_unset_property (GtkStyleProperties *props,
801 const gchar *property,
804 GtkStylePropertiesPrivate *priv;
805 GtkStyleProperty *node;
809 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
810 g_return_if_fail (property != NULL);
812 node = _gtk_style_property_lookup (property);
816 g_warning ("Style property \"%s\" is not registered", property);
819 if (_gtk_style_property_get_value_type (node) == G_TYPE_NONE)
821 g_warning ("Style property \"%s\" is not settable", property);
825 if (GTK_IS_CSS_SHORTHAND_PROPERTY (node))
827 GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (node);
829 for (pos = 0; pos < _gtk_css_shorthand_property_get_n_subproperties (shorthand); pos++)
831 GtkCssStyleProperty *sub = _gtk_css_shorthand_property_get_subproperty (shorthand, pos);
832 gtk_style_properties_unset_property (props,
833 _gtk_style_property_get_name (GTK_STYLE_PROPERTY (sub)),
840 prop = g_hash_table_lookup (priv->properties, node);
845 if (property_data_find_position (prop, state, &pos))
849 data = &g_array_index (prop->values, ValueData, pos);
851 if (G_IS_VALUE (&data->value))
852 g_value_unset (&data->value);
854 g_array_remove_index (prop->values, pos);
859 * gtk_style_properties_clear:
860 * @props: a #GtkStyleProperties
862 * Clears all style information from @props.
865 gtk_style_properties_clear (GtkStyleProperties *props)
867 GtkStylePropertiesPrivate *priv;
869 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
872 g_hash_table_remove_all (priv->properties);
876 * gtk_style_properties_merge:
877 * @props: a #GtkStyleProperties
878 * @props_to_merge: a second #GtkStyleProperties
879 * @replace: whether to replace values or not
881 * Merges into @props all the style information contained
882 * in @props_to_merge. If @replace is %TRUE, the values
883 * will be overwritten, if it is %FALSE, the older values
889 gtk_style_properties_merge (GtkStyleProperties *props,
890 const GtkStyleProperties *props_to_merge,
893 GtkStylePropertiesPrivate *priv, *priv_to_merge;
897 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
898 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props_to_merge));
901 priv_to_merge = props_to_merge->priv;
903 /* Merge symbolic color map */
904 if (priv_to_merge->color_map)
906 g_hash_table_iter_init (&iter, priv_to_merge->color_map);
908 while (g_hash_table_iter_next (&iter, &key, &value))
911 GtkSymbolicColor *color;
917 g_hash_table_lookup (priv->color_map, name))
920 gtk_style_properties_map_color (props, name, color);
924 /* Merge symbolic style properties */
925 g_hash_table_iter_init (&iter, priv_to_merge->properties);
927 while (g_hash_table_iter_next (&iter, &key, &value))
929 PropertyData *prop_to_merge = value;
933 prop = g_hash_table_lookup (priv->properties, key);
937 prop = property_data_new ();
938 g_hash_table_insert (priv->properties, key, prop);
941 for (i = 0; i < prop_to_merge->values->len; i++)
946 data = &g_array_index (prop_to_merge->values, ValueData, i);
948 if (replace && data->state == GTK_STATE_FLAG_NORMAL &&
949 G_VALUE_TYPE (&data->value) != PANGO_TYPE_FONT_DESCRIPTION)
951 /* Let normal state override all states
952 * previously set in the original set
954 property_data_remove_values (prop);
957 value = property_data_get_value (prop, data->state);
959 if (G_VALUE_TYPE (&data->value) == PANGO_TYPE_FONT_DESCRIPTION &&
962 PangoFontDescription *font_desc;
963 PangoFontDescription *font_desc_to_merge;
965 /* Handle merging of font descriptions */
966 font_desc = g_value_get_boxed (value);
967 font_desc_to_merge = g_value_get_boxed (&data->value);
969 pango_font_description_merge (font_desc, font_desc_to_merge, replace);
971 else if (G_VALUE_TYPE (&data->value) == G_TYPE_PTR_ARRAY &&
974 GPtrArray *array, *array_to_merge;
977 /* Append the array, mainly thought
978 * for the gtk-key-bindings property
980 array = g_value_get_boxed (value);
981 array_to_merge = g_value_get_boxed (&data->value);
983 for (i = 0; i < array_to_merge->len; i++)
984 g_ptr_array_add (array, g_ptr_array_index (array_to_merge, i));
986 else if (replace || !G_IS_VALUE (value))
988 if (!G_IS_VALUE (value))
989 g_value_init (value, G_VALUE_TYPE (&data->value));
990 else if (G_VALUE_TYPE (value) != G_VALUE_TYPE (&data->value))
992 g_value_unset (value);
993 g_value_init (value, G_VALUE_TYPE (&data->value));
996 g_value_copy (&data->value, value);