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,
326 * gtk_style_properties_lookup_property: (skip)
327 * @property_name: property name to look up
328 * @parse_func: (out): return location for the parse function
329 * @pspec: (out) (transfer none): return location for the #GParamSpec
331 * Returns %TRUE if a property has been registered, if @pspec or
332 * @parse_func are not %NULL, the #GParamSpec and parsing function
333 * will be respectively returned.
335 * Returns: %TRUE if the property is registered, %FALSE otherwise
340 gtk_style_properties_lookup_property (const gchar *property_name,
341 GtkStylePropertyParser *parse_func,
344 const GtkStyleProperty *node;
345 gboolean found = FALSE;
347 g_return_val_if_fail (property_name != NULL, FALSE);
349 node = _gtk_style_property_lookup (property_name);
354 *pspec = node->pspec;
357 *parse_func = node->property_parse_func;
365 /* GtkStyleProperties methods */
368 * gtk_style_properties_new:
370 * Returns a newly created #GtkStyleProperties
372 * Returns: a new #GtkStyleProperties
375 gtk_style_properties_new (void)
377 return g_object_new (GTK_TYPE_STYLE_PROPERTIES, NULL);
381 * gtk_style_properties_map_color:
382 * @props: a #GtkStyleProperties
384 * @color: #GtkSymbolicColor to map @name to
386 * Maps @color so it can be referenced by @name. See
387 * gtk_style_properties_lookup_color()
392 gtk_style_properties_map_color (GtkStyleProperties *props,
394 GtkSymbolicColor *color)
396 GtkStylePropertiesPrivate *priv;
398 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
399 g_return_if_fail (name != NULL);
400 g_return_if_fail (color != NULL);
404 if (G_UNLIKELY (!priv->color_map))
405 priv->color_map = g_hash_table_new_full (g_str_hash,
407 (GDestroyNotify) g_free,
408 (GDestroyNotify) gtk_symbolic_color_unref);
410 g_hash_table_replace (priv->color_map,
412 gtk_symbolic_color_ref (color));
416 * gtk_style_properties_lookup_color:
417 * @props: a #GtkStyleProperties
418 * @name: color name to lookup
420 * Returns the symbolic color that is mapped
423 * Returns: (transfer none): The mapped color
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 const GtkStyleProperty *style_prop,
450 GtkStylePropertiesPrivate *priv;
455 value_type = G_VALUE_TYPE (value);
457 if (style_prop->pspec->value_type == GDK_TYPE_RGBA ||
458 style_prop->pspec->value_type == GDK_TYPE_COLOR)
460 /* Allow GtkSymbolicColor as well */
461 g_return_if_fail (value_type == GDK_TYPE_RGBA ||
462 value_type == GDK_TYPE_COLOR ||
463 value_type == GTK_TYPE_SYMBOLIC_COLOR);
465 else if (style_prop->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN)
467 /* Allow GtkGradient as a substitute */
468 g_return_if_fail (value_type == CAIRO_GOBJECT_TYPE_PATTERN ||
469 value_type == GTK_TYPE_GRADIENT);
471 else if (style_prop->pspec->value_type == G_TYPE_INT)
473 g_return_if_fail (value_type == G_TYPE_INT ||
474 value_type == GTK_TYPE_CSS_BORDER_RADIUS);
477 g_return_if_fail (style_prop->pspec->value_type == value_type);
479 if (_gtk_style_property_is_shorthand (style_prop))
481 GParameter *parameters;
482 guint i, n_parameters;
484 parameters = _gtk_style_property_unpack (style_prop, value, &n_parameters);
486 for (i = 0; i < n_parameters; i++)
488 gtk_style_properties_set_property (props,
491 ¶meters[i].value);
492 g_value_unset (¶meters[i].value);
499 prop = g_hash_table_lookup (priv->properties, style_prop);
503 prop = property_data_new ();
504 g_hash_table_insert (priv->properties, (gpointer) style_prop, prop);
507 val = property_data_get_value (prop, state);
509 if (G_VALUE_TYPE (val) == value_type)
513 if (G_IS_VALUE (val))
516 g_value_init (val, value_type);
519 g_value_copy (value, val);
520 if (style_prop->pspec->value_type == value_type)
521 g_param_value_validate (style_prop->pspec, val);
525 * gtk_style_properties_set_property:
526 * @props: a #GtkStyleProperties
527 * @property: styling property to set
528 * @state: state to set the value for
529 * @value: new value for the property
531 * Sets a styling property in @props.
536 gtk_style_properties_set_property (GtkStyleProperties *props,
537 const gchar *property,
541 const GtkStyleProperty *node;
543 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
544 g_return_if_fail (property != NULL);
545 g_return_if_fail (value != NULL);
547 node = _gtk_style_property_lookup (property);
551 g_warning ("Style property \"%s\" is not registered", property);
555 _gtk_style_properties_set_property_by_property (props,
562 * gtk_style_properties_set_valist:
563 * @props: a #GtkStyleProperties
564 * @state: state to set the values for
565 * @args: va_list of property name/value pairs, followed by %NULL
567 * Sets several style properties on @props.
572 gtk_style_properties_set_valist (GtkStyleProperties *props,
576 const gchar *property_name;
578 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
580 property_name = va_arg (args, const gchar *);
582 while (property_name)
584 const GtkStyleProperty *node;
588 node = _gtk_style_property_lookup (property_name);
592 g_warning ("Style property \"%s\" is not registered", property_name);
596 G_VALUE_COLLECT_INIT (&val, node->pspec->value_type,
600 g_warning ("Could not set style property \"%s\": %s", property_name, error);
601 g_value_unset (&val);
606 _gtk_style_properties_set_property_by_property (props, node, state, &val);
607 g_value_unset (&val);
609 property_name = va_arg (args, const gchar *);
614 * gtk_style_properties_set:
615 * @props: a #GtkStyleProperties
616 * @state: state to set the values for
617 * @...: property name/value pairs, followed by %NULL
619 * Sets several style properties on @props.
624 gtk_style_properties_set (GtkStyleProperties *props,
630 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
632 va_start (args, state);
633 gtk_style_properties_set_valist (props, state, args);
637 /* NB: Will return NULL for shorthands */
639 _gtk_style_properties_peek_property (GtkStyleProperties *props,
640 const gchar *prop_name,
642 const GtkStyleProperty **property)
644 GtkStylePropertiesPrivate *priv;
645 const GtkStyleProperty *node;
649 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
650 g_return_val_if_fail (prop_name != NULL, NULL);
652 node = _gtk_style_property_lookup (prop_name);
658 g_warning ("Style property \"%s\" is not registered", prop_name);
663 prop = g_hash_table_lookup (priv->properties, node);
668 val = property_data_match_state (prop, state);
672 _gtk_style_property_resolve (node, props, val);
678 * gtk_style_properties_get_property:
679 * @props: a #GtkStyleProperties
680 * @property: style property name
681 * @state: state to retrieve the property value for
682 * @value: (out) (transfer full): return location for the style property value.
684 * Gets a style property from @props for the given state. When done with @value,
685 * g_value_unset() needs to be called to free any allocated memory.
687 * Returns: %TRUE if the property exists in @props, %FALSE otherwise
692 gtk_style_properties_get_property (GtkStyleProperties *props,
693 const gchar *property,
697 const GtkStyleProperty *node;
700 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
701 g_return_val_if_fail (property != NULL, FALSE);
702 g_return_val_if_fail (value != NULL, FALSE);
704 val = _gtk_style_properties_peek_property (props, property, state, &node);
709 g_value_init (value, node->pspec->value_type);
712 g_value_copy (val, value);
713 else if (_gtk_style_property_is_shorthand (node))
714 _gtk_style_property_pack (node, props, state, value);
716 _gtk_style_property_default_value (node, props, value);
722 * gtk_style_properties_get_valist:
723 * @props: a #GtkStyleProperties
724 * @state: state to retrieve the property values for
725 * @args: va_list of property name/return location pairs, followed by %NULL
727 * Retrieves several style property values from @props for a given state.
732 gtk_style_properties_get_valist (GtkStyleProperties *props,
736 const gchar *property_name;
738 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
740 property_name = va_arg (args, const gchar *);
742 while (property_name)
744 const GtkStyleProperty *node;
748 val = _gtk_style_properties_peek_property (props, property_name, state, &node);
754 G_VALUE_LCOPY (val, args, 0, &error);
756 else if (_gtk_style_property_is_shorthand (node))
758 GValue packed = { 0 };
760 g_value_init (&packed, node->pspec->value_type);
761 _gtk_style_property_pack (node, props, state, &packed);
762 G_VALUE_LCOPY (&packed, args, 0, &error);
763 g_value_unset (&packed);
767 GValue default_value = { 0 };
769 g_value_init (&default_value, node->pspec->value_type);
770 _gtk_style_property_default_value (node, props, &default_value);
771 G_VALUE_LCOPY (&default_value, args, 0, &error);
772 g_value_unset (&default_value);
777 g_warning ("Could not get style property \"%s\": %s", property_name, error);
782 property_name = va_arg (args, const gchar *);
787 * gtk_style_properties_get:
788 * @props: a #GtkStyleProperties
789 * @state: state to retrieve the property values for
790 * @...: property name /return value pairs, followed by %NULL
792 * Retrieves several style property values from @props for a
798 gtk_style_properties_get (GtkStyleProperties *props,
804 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
806 va_start (args, state);
807 gtk_style_properties_get_valist (props, state, args);
812 * gtk_style_properties_unset_property:
813 * @props: a #GtkStyleProperties
814 * @property: property to unset
815 * @state: state to unset
817 * Unsets a style property in @props.
822 gtk_style_properties_unset_property (GtkStyleProperties *props,
823 const gchar *property,
826 GtkStylePropertiesPrivate *priv;
827 const GtkStyleProperty *node;
831 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
832 g_return_if_fail (property != NULL);
834 node = _gtk_style_property_lookup (property);
838 g_warning ("Style property \"%s\" is not registered", property);
843 prop = g_hash_table_lookup (priv->properties, node);
848 if (property_data_find_position (prop, state, &pos))
852 data = &g_array_index (prop->values, ValueData, pos);
854 if (G_IS_VALUE (&data->value))
855 g_value_unset (&data->value);
857 g_array_remove_index (prop->values, pos);
862 * gtk_style_properties_clear:
863 * @props: a #GtkStyleProperties
865 * Clears all style information from @props.
868 gtk_style_properties_clear (GtkStyleProperties *props)
870 GtkStylePropertiesPrivate *priv;
872 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
875 g_hash_table_remove_all (priv->properties);
879 * gtk_style_properties_merge:
880 * @props: a #GtkStyleProperties
881 * @props_to_merge: a second #GtkStyleProperties
882 * @replace: whether to replace values or not
884 * Merges into @props all the style information contained
885 * in @props_to_merge. If @replace is %TRUE, the values
886 * will be overwritten, if it is %FALSE, the older values
892 gtk_style_properties_merge (GtkStyleProperties *props,
893 const GtkStyleProperties *props_to_merge,
896 GtkStylePropertiesPrivate *priv, *priv_to_merge;
900 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
901 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props_to_merge));
904 priv_to_merge = props_to_merge->priv;
906 /* Merge symbolic color map */
907 if (priv_to_merge->color_map)
909 g_hash_table_iter_init (&iter, priv_to_merge->color_map);
911 while (g_hash_table_iter_next (&iter, &key, &value))
914 GtkSymbolicColor *color;
920 g_hash_table_lookup (priv->color_map, name))
923 gtk_style_properties_map_color (props, name, color);
927 /* Merge symbolic style properties */
928 g_hash_table_iter_init (&iter, priv_to_merge->properties);
930 while (g_hash_table_iter_next (&iter, &key, &value))
932 PropertyData *prop_to_merge = value;
936 prop = g_hash_table_lookup (priv->properties, key);
940 prop = property_data_new ();
941 g_hash_table_insert (priv->properties, key, prop);
944 for (i = 0; i < prop_to_merge->values->len; i++)
949 data = &g_array_index (prop_to_merge->values, ValueData, i);
951 if (replace && data->state == GTK_STATE_FLAG_NORMAL &&
952 G_VALUE_TYPE (&data->value) != PANGO_TYPE_FONT_DESCRIPTION)
954 /* Let normal state override all states
955 * previously set in the original set
957 property_data_remove_values (prop);
960 value = property_data_get_value (prop, data->state);
962 if (G_VALUE_TYPE (&data->value) == PANGO_TYPE_FONT_DESCRIPTION &&
965 PangoFontDescription *font_desc;
966 PangoFontDescription *font_desc_to_merge;
968 /* Handle merging of font descriptions */
969 font_desc = g_value_get_boxed (value);
970 font_desc_to_merge = g_value_get_boxed (&data->value);
972 pango_font_description_merge (font_desc, font_desc_to_merge, replace);
974 else if (G_VALUE_TYPE (&data->value) == G_TYPE_PTR_ARRAY &&
977 GPtrArray *array, *array_to_merge;
980 /* Append the array, mainly thought
981 * for the gtk-key-bindings property
983 array = g_value_get_boxed (value);
984 array_to_merge = g_value_get_boxed (&data->value);
986 for (i = 0; i < array_to_merge->len; i++)
987 g_ptr_array_add (array, g_ptr_array_index (array_to_merge, i));
989 else if (replace || !G_IS_VALUE (value))
991 if (!G_IS_VALUE (value))
992 g_value_init (value, G_VALUE_TYPE (&data->value));
993 else if (G_VALUE_TYPE (value) != G_VALUE_TYPE (&data->value))
995 g_value_unset (value);
996 g_value_init (value, G_VALUE_TYPE (&data->value));
999 g_value_copy (&data->value, value);