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 "gtkcsstypesprivate.h"
35 #include "gtkborderimageprivate.h"
37 #include "gtkstylepropertyprivate.h"
41 * SECTION:gtkstyleproperties
42 * @Short_description: Store for style property information
43 * @Title: GtkStyleProperties
45 * GtkStyleProperties provides the storage for style information
46 * that is used by #GtkStyleContext and other #GtkStyleProvider
49 * Before style properties can be stored in GtkStyleProperties, they
50 * must be registered with gtk_style_properties_register_property().
52 * Unless you are writing a #GtkStyleProvider implementation, you
53 * are unlikely to use this API directly, as gtk_style_context_get()
54 * and its variants are the preferred way to access styling information
55 * from widget implementations and theming engine implementations
56 * should use the APIs provided by #GtkThemingEngine instead.
59 typedef struct GtkStylePropertiesPrivate GtkStylePropertiesPrivate;
60 typedef struct PropertyData PropertyData;
61 typedef struct ValueData ValueData;
74 struct GtkStylePropertiesPrivate
76 GHashTable *color_map;
77 GHashTable *properties;
80 static void gtk_style_properties_provider_init (GtkStyleProviderIface *iface);
81 static void gtk_style_properties_finalize (GObject *object);
84 G_DEFINE_TYPE_EXTENDED (GtkStyleProperties, gtk_style_properties, G_TYPE_OBJECT, 0,
85 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
86 gtk_style_properties_provider_init));
89 gtk_style_properties_class_init (GtkStylePropertiesClass *klass)
91 GObjectClass *object_class = G_OBJECT_CLASS (klass);
93 object_class->finalize = gtk_style_properties_finalize;
95 g_type_class_add_private (object_class, sizeof (GtkStylePropertiesPrivate));
99 property_data_new (void)
103 data = g_slice_new0 (PropertyData);
104 data->values = g_array_new (FALSE, FALSE, sizeof (ValueData));
110 property_data_remove_values (PropertyData *data)
114 for (i = 0; i < data->values->len; i++)
116 ValueData *value_data;
118 value_data = &g_array_index (data->values, ValueData, i);
120 if (G_IS_VALUE (&value_data->value))
121 g_value_unset (&value_data->value);
124 if (data->values->len > 0)
125 g_array_remove_range (data->values, 0, data->values->len);
129 property_data_free (PropertyData *data)
131 property_data_remove_values (data);
132 g_array_free (data->values, TRUE);
133 g_slice_free (PropertyData, data);
137 property_data_find_position (PropertyData *data,
142 gboolean found = FALSE;
148 if (data->values->len == 0)
151 /* Find position for the given state, or the position where
152 * it would be if not found, the array is ordered by the
156 max = data->values->len - 1;
160 ValueData *value_data;
162 mid = (min + max) / 2;
163 value_data = &g_array_index (data->values, ValueData, mid);
165 if (value_data->state == state)
170 else if (value_data->state < state)
171 position = min = mid + 1;
178 while (!found && min <= max);
187 property_data_get_value (PropertyData *data,
193 if (!property_data_find_position (data, state, &pos))
195 ValueData new = { 0 };
198 g_array_insert_val (data->values, pos, new);
201 val_data = &g_array_index (data->values, ValueData, pos);
203 return &val_data->value;
207 property_data_match_state (PropertyData *data,
213 if (property_data_find_position (data, state, &pos))
218 val_data = &g_array_index (data->values, ValueData, pos);
219 return &val_data->value;
222 if (pos >= data->values->len)
223 pos = data->values->len - 1;
225 /* No exact match, go downwards the list to find
226 * the closest match to the given state flags, as
227 * a side effect, there is an implicit precedence
228 * of higher flags over the smaller ones.
230 for (i = pos; i >= 0; i--)
234 val_data = &g_array_index (data->values, ValueData, i);
236 /* Check whether any of the requested
237 * flags are set, and no other flags are.
239 * Also, no flags acts as a wildcard, such
240 * value should be always in the first position
241 * in the array (if present) anyways.
243 if (val_data->state == 0 ||
244 ((val_data->state & state) != 0 &&
245 (val_data->state & ~state) == 0))
246 return &val_data->value;
253 gtk_style_properties_init (GtkStyleProperties *props)
255 GtkStylePropertiesPrivate *priv;
257 priv = props->priv = G_TYPE_INSTANCE_GET_PRIVATE (props,
258 GTK_TYPE_STYLE_PROPERTIES,
259 GtkStylePropertiesPrivate);
261 priv->properties = g_hash_table_new_full (NULL, NULL, NULL,
262 (GDestroyNotify) property_data_free);
266 gtk_style_properties_finalize (GObject *object)
268 GtkStylePropertiesPrivate *priv;
269 GtkStyleProperties *props;
271 props = GTK_STYLE_PROPERTIES (object);
273 g_hash_table_destroy (priv->properties);
276 g_hash_table_destroy (priv->color_map);
278 G_OBJECT_CLASS (gtk_style_properties_parent_class)->finalize (object);
282 gtk_style_properties_get_style (GtkStyleProvider *provider,
285 /* Return style set itself */
286 return g_object_ref (provider);
290 gtk_style_properties_provider_init (GtkStyleProviderIface *iface)
292 iface->get_style = gtk_style_properties_get_style;
295 /* Property registration functions */
298 * gtk_style_properties_register_property: (skip)
299 * @parse_func: parsing function to use, or %NULL
300 * @pspec: the #GParamSpec for the new property
302 * Registers a property so it can be used in the CSS file format.
303 * This function is the low-level equivalent of
304 * gtk_theming_engine_register_property(), if you are implementing
305 * a theming engine, you want to use that function instead.
310 gtk_style_properties_register_property (GtkStylePropertyParser parse_func,
313 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
315 _gtk_style_property_register (pspec,
327 * gtk_style_properties_lookup_property: (skip)
328 * @property_name: property name to look up
329 * @parse_func: (out): return location for the parse function
330 * @pspec: (out) (transfer none): return location for the #GParamSpec
332 * Returns %TRUE if a property has been registered, if @pspec or
333 * @parse_func are not %NULL, the #GParamSpec and parsing function
334 * will be respectively returned.
336 * Returns: %TRUE if the property is registered, %FALSE otherwise
341 gtk_style_properties_lookup_property (const gchar *property_name,
342 GtkStylePropertyParser *parse_func,
345 const GtkStyleProperty *node;
346 gboolean found = FALSE;
348 g_return_val_if_fail (property_name != NULL, FALSE);
350 node = _gtk_style_property_lookup (property_name);
355 *pspec = node->pspec;
358 *parse_func = node->property_parse_func;
366 /* GtkStyleProperties methods */
369 * gtk_style_properties_new:
371 * Returns a newly created #GtkStyleProperties
373 * Returns: a new #GtkStyleProperties
376 gtk_style_properties_new (void)
378 return g_object_new (GTK_TYPE_STYLE_PROPERTIES, NULL);
382 * gtk_style_properties_map_color:
383 * @props: a #GtkStyleProperties
385 * @color: #GtkSymbolicColor to map @name to
387 * Maps @color so it can be referenced by @name. See
388 * gtk_style_properties_lookup_color()
393 gtk_style_properties_map_color (GtkStyleProperties *props,
395 GtkSymbolicColor *color)
397 GtkStylePropertiesPrivate *priv;
399 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
400 g_return_if_fail (name != NULL);
401 g_return_if_fail (color != NULL);
405 if (G_UNLIKELY (!priv->color_map))
406 priv->color_map = g_hash_table_new_full (g_str_hash,
408 (GDestroyNotify) g_free,
409 (GDestroyNotify) gtk_symbolic_color_unref);
411 g_hash_table_replace (priv->color_map,
413 gtk_symbolic_color_ref (color));
417 * gtk_style_properties_lookup_color:
418 * @props: a #GtkStyleProperties
419 * @name: color name to lookup
421 * Returns the symbolic color that is mapped
424 * Returns: (transfer none): The mapped color
429 gtk_style_properties_lookup_color (GtkStyleProperties *props,
432 GtkStylePropertiesPrivate *priv;
434 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
435 g_return_val_if_fail (name != NULL, NULL);
439 if (!priv->color_map)
442 return g_hash_table_lookup (priv->color_map, name);
446 _gtk_style_properties_set_property_by_property (GtkStyleProperties *props,
447 const GtkStyleProperty *style_prop,
451 GtkStylePropertiesPrivate *priv;
456 value_type = G_VALUE_TYPE (value);
458 if (style_prop->pspec->value_type == GDK_TYPE_RGBA ||
459 style_prop->pspec->value_type == GDK_TYPE_COLOR)
461 /* Allow GtkSymbolicColor as well */
462 g_return_if_fail (value_type == GDK_TYPE_RGBA ||
463 value_type == GDK_TYPE_COLOR ||
464 value_type == GTK_TYPE_SYMBOLIC_COLOR);
466 else if (style_prop->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN)
468 /* Allow GtkGradient as a substitute */
469 g_return_if_fail (value_type == CAIRO_GOBJECT_TYPE_PATTERN ||
470 value_type == GTK_TYPE_GRADIENT);
472 else if (style_prop->pspec->value_type == G_TYPE_INT)
474 g_return_if_fail (value_type == G_TYPE_INT ||
475 value_type == GTK_TYPE_CSS_BORDER_RADIUS);
478 g_return_if_fail (style_prop->pspec->value_type == value_type);
480 if (_gtk_style_property_is_shorthand (style_prop))
482 GParameter *parameters;
483 guint i, n_parameters;
485 parameters = _gtk_style_property_unpack (style_prop, value, &n_parameters);
487 for (i = 0; i < n_parameters; i++)
489 gtk_style_properties_set_property (props,
492 ¶meters[i].value);
493 g_value_unset (¶meters[i].value);
500 prop = g_hash_table_lookup (priv->properties, style_prop);
504 prop = property_data_new ();
505 g_hash_table_insert (priv->properties, (gpointer) style_prop, prop);
508 val = property_data_get_value (prop, state);
510 if (G_VALUE_TYPE (val) == value_type)
514 if (G_IS_VALUE (val))
517 g_value_init (val, value_type);
520 g_value_copy (value, val);
521 if (style_prop->pspec->value_type == value_type)
522 g_param_value_validate (style_prop->pspec, val);
526 * gtk_style_properties_set_property:
527 * @props: a #GtkStyleProperties
528 * @property: styling property to set
529 * @state: state to set the value for
530 * @value: new value for the property
532 * Sets a styling property in @props.
537 gtk_style_properties_set_property (GtkStyleProperties *props,
538 const gchar *property,
542 const GtkStyleProperty *node;
544 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
545 g_return_if_fail (property != NULL);
546 g_return_if_fail (value != NULL);
548 node = _gtk_style_property_lookup (property);
552 g_warning ("Style property \"%s\" is not registered", property);
556 _gtk_style_properties_set_property_by_property (props,
563 * gtk_style_properties_set_valist:
564 * @props: a #GtkStyleProperties
565 * @state: state to set the values for
566 * @args: va_list of property name/value pairs, followed by %NULL
568 * Sets several style properties on @props.
573 gtk_style_properties_set_valist (GtkStyleProperties *props,
577 const gchar *property_name;
579 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
581 property_name = va_arg (args, const gchar *);
583 while (property_name)
585 const GtkStyleProperty *node;
587 GValue val = G_VALUE_INIT;
589 node = _gtk_style_property_lookup (property_name);
593 g_warning ("Style property \"%s\" is not registered", property_name);
597 G_VALUE_COLLECT_INIT (&val, node->pspec->value_type,
601 g_warning ("Could not set style property \"%s\": %s", property_name, error);
602 g_value_unset (&val);
607 _gtk_style_properties_set_property_by_property (props, node, state, &val);
608 g_value_unset (&val);
610 property_name = va_arg (args, const gchar *);
615 * gtk_style_properties_set:
616 * @props: a #GtkStyleProperties
617 * @state: state to set the values for
618 * @...: property name/value pairs, followed by %NULL
620 * Sets several style properties on @props.
625 gtk_style_properties_set (GtkStyleProperties *props,
631 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
633 va_start (args, state);
634 gtk_style_properties_set_valist (props, state, args);
638 /* NB: Will return NULL for shorthands */
640 _gtk_style_properties_peek_property (GtkStyleProperties *props,
641 const gchar *prop_name,
643 const GtkStyleProperty **property)
645 GtkStylePropertiesPrivate *priv;
646 const GtkStyleProperty *node;
650 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
651 g_return_val_if_fail (prop_name != NULL, NULL);
653 node = _gtk_style_property_lookup (prop_name);
659 g_warning ("Style property \"%s\" is not registered", prop_name);
664 prop = g_hash_table_lookup (priv->properties, node);
669 val = property_data_match_state (prop, state);
673 _gtk_style_property_resolve (node, props, state, val);
679 * gtk_style_properties_get_property:
680 * @props: a #GtkStyleProperties
681 * @property: style property name
682 * @state: state to retrieve the property value for
683 * @value: (out) (transfer full): return location for the style property value.
685 * Gets a style property from @props for the given state. When done with @value,
686 * g_value_unset() needs to be called to free any allocated memory.
688 * Returns: %TRUE if the property exists in @props, %FALSE otherwise
693 gtk_style_properties_get_property (GtkStyleProperties *props,
694 const gchar *property,
698 const GtkStyleProperty *node;
701 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
702 g_return_val_if_fail (property != NULL, FALSE);
703 g_return_val_if_fail (value != NULL, FALSE);
705 val = _gtk_style_properties_peek_property (props, property, state, &node);
710 g_value_init (value, node->pspec->value_type);
713 g_value_copy (val, value);
714 else if (_gtk_style_property_is_shorthand (node))
715 _gtk_style_property_pack (node, props, state, value);
717 _gtk_style_property_default_value (node, props, state, value);
723 * gtk_style_properties_get_valist:
724 * @props: a #GtkStyleProperties
725 * @state: state to retrieve the property values for
726 * @args: va_list of property name/return location pairs, followed by %NULL
728 * Retrieves several style property values from @props for a given state.
733 gtk_style_properties_get_valist (GtkStyleProperties *props,
737 const gchar *property_name;
739 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
741 property_name = va_arg (args, const gchar *);
743 while (property_name)
745 const GtkStyleProperty *node;
749 val = _gtk_style_properties_peek_property (props, property_name, state, &node);
755 G_VALUE_LCOPY (val, args, 0, &error);
757 else if (_gtk_style_property_is_shorthand (node))
759 GValue packed = G_VALUE_INIT;
761 g_value_init (&packed, node->pspec->value_type);
762 _gtk_style_property_pack (node, props, state, &packed);
763 G_VALUE_LCOPY (&packed, args, 0, &error);
764 g_value_unset (&packed);
768 GValue default_value = G_VALUE_INIT;
770 g_value_init (&default_value, node->pspec->value_type);
771 _gtk_style_property_default_value (node, props, state, &default_value);
772 G_VALUE_LCOPY (&default_value, args, 0, &error);
773 g_value_unset (&default_value);
778 g_warning ("Could not get style property \"%s\": %s", property_name, error);
783 property_name = va_arg (args, const gchar *);
788 * gtk_style_properties_get:
789 * @props: a #GtkStyleProperties
790 * @state: state to retrieve the property values for
791 * @...: property name /return value pairs, followed by %NULL
793 * Retrieves several style property values from @props for a
799 gtk_style_properties_get (GtkStyleProperties *props,
805 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
807 va_start (args, state);
808 gtk_style_properties_get_valist (props, state, args);
813 * gtk_style_properties_unset_property:
814 * @props: a #GtkStyleProperties
815 * @property: property to unset
816 * @state: state to unset
818 * Unsets a style property in @props.
823 gtk_style_properties_unset_property (GtkStyleProperties *props,
824 const gchar *property,
827 GtkStylePropertiesPrivate *priv;
828 const GtkStyleProperty *node;
832 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
833 g_return_if_fail (property != NULL);
835 node = _gtk_style_property_lookup (property);
839 g_warning ("Style property \"%s\" is not registered", property);
843 if (node->unset_func)
845 node->unset_func (props, state);
850 prop = g_hash_table_lookup (priv->properties, node);
855 if (property_data_find_position (prop, state, &pos))
859 data = &g_array_index (prop->values, ValueData, pos);
861 if (G_IS_VALUE (&data->value))
862 g_value_unset (&data->value);
864 g_array_remove_index (prop->values, pos);
869 * gtk_style_properties_clear:
870 * @props: a #GtkStyleProperties
872 * Clears all style information from @props.
875 gtk_style_properties_clear (GtkStyleProperties *props)
877 GtkStylePropertiesPrivate *priv;
879 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
882 g_hash_table_remove_all (priv->properties);
886 * gtk_style_properties_merge:
887 * @props: a #GtkStyleProperties
888 * @props_to_merge: a second #GtkStyleProperties
889 * @replace: whether to replace values or not
891 * Merges into @props all the style information contained
892 * in @props_to_merge. If @replace is %TRUE, the values
893 * will be overwritten, if it is %FALSE, the older values
899 gtk_style_properties_merge (GtkStyleProperties *props,
900 const GtkStyleProperties *props_to_merge,
903 GtkStylePropertiesPrivate *priv, *priv_to_merge;
907 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
908 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props_to_merge));
911 priv_to_merge = props_to_merge->priv;
913 /* Merge symbolic color map */
914 if (priv_to_merge->color_map)
916 g_hash_table_iter_init (&iter, priv_to_merge->color_map);
918 while (g_hash_table_iter_next (&iter, &key, &value))
921 GtkSymbolicColor *color;
927 g_hash_table_lookup (priv->color_map, name))
930 gtk_style_properties_map_color (props, name, color);
934 /* Merge symbolic style properties */
935 g_hash_table_iter_init (&iter, priv_to_merge->properties);
937 while (g_hash_table_iter_next (&iter, &key, &value))
939 PropertyData *prop_to_merge = value;
943 prop = g_hash_table_lookup (priv->properties, key);
947 prop = property_data_new ();
948 g_hash_table_insert (priv->properties, key, prop);
951 for (i = 0; i < prop_to_merge->values->len; i++)
956 data = &g_array_index (prop_to_merge->values, ValueData, i);
958 if (replace && data->state == GTK_STATE_FLAG_NORMAL &&
959 G_VALUE_TYPE (&data->value) != PANGO_TYPE_FONT_DESCRIPTION)
961 /* Let normal state override all states
962 * previously set in the original set
964 property_data_remove_values (prop);
967 value = property_data_get_value (prop, data->state);
969 if (G_VALUE_TYPE (&data->value) == PANGO_TYPE_FONT_DESCRIPTION &&
972 PangoFontDescription *font_desc;
973 PangoFontDescription *font_desc_to_merge;
975 /* Handle merging of font descriptions */
976 font_desc = g_value_get_boxed (value);
977 font_desc_to_merge = g_value_get_boxed (&data->value);
979 pango_font_description_merge (font_desc, font_desc_to_merge, replace);
981 else if (G_VALUE_TYPE (&data->value) == G_TYPE_PTR_ARRAY &&
984 GPtrArray *array, *array_to_merge;
987 /* Append the array, mainly thought
988 * for the gtk-key-bindings property
990 array = g_value_get_boxed (value);
991 array_to_merge = g_value_get_boxed (&data->value);
993 for (i = 0; i < array_to_merge->len; i++)
994 g_ptr_array_add (array, g_ptr_array_index (array_to_merge, i));
996 else if (replace || !G_IS_VALUE (value))
998 if (!G_IS_VALUE (value))
999 g_value_init (value, G_VALUE_TYPE (&data->value));
1000 else if (G_VALUE_TYPE (value) != G_VALUE_TYPE (&data->value))
1002 g_value_unset (value);
1003 g_value_init (value, G_VALUE_TYPE (&data->value));
1006 g_value_copy (&data->value, value);