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.
23 #include <gobject/gvaluecollector.h>
25 #include "gtkstyleprovider.h"
26 #include "gtkstyleset.h"
27 #include "gtkprivate.h"
28 #include "gtkthemingengine.h"
29 #include "gtkanimationdescription.h"
32 typedef struct GtkStyleSetPrivate GtkStyleSetPrivate;
33 typedef struct PropertyData PropertyData;
34 typedef struct PropertyNode PropertyNode;
35 typedef struct ValueData ValueData;
39 GQuark property_quark;
42 GtkStylePropertyParser parse_func;
56 struct GtkStyleSetPrivate
58 GHashTable *color_map;
59 GHashTable *properties;
62 static GArray *properties = NULL;
64 static void gtk_style_set_provider_init (GtkStyleProviderIface *iface);
65 static void gtk_style_set_finalize (GObject *object);
68 G_DEFINE_TYPE_EXTENDED (GtkStyleSet, gtk_style_set, G_TYPE_OBJECT, 0,
69 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
70 gtk_style_set_provider_init));
73 gtk_style_set_class_init (GtkStyleSetClass *klass)
75 GObjectClass *object_class = G_OBJECT_CLASS (klass);
78 object_class->finalize = gtk_style_set_finalize;
80 /* Initialize default property set */
81 gtk_style_set_register_property ("foreground-color", GDK_TYPE_COLOR, NULL, NULL);
82 gtk_style_set_register_property ("background-color", GDK_TYPE_COLOR, NULL, NULL);
83 gtk_style_set_register_property ("text-color", GDK_TYPE_COLOR, NULL, NULL);
84 gtk_style_set_register_property ("base-color", GDK_TYPE_COLOR, NULL, NULL);
86 gtk_style_set_register_property ("font", PANGO_TYPE_FONT_DESCRIPTION, NULL, NULL);
88 gtk_style_set_register_property ("padding", GTK_TYPE_BORDER, NULL, NULL);
89 gtk_style_set_register_property ("border", G_TYPE_INT, NULL, NULL);
91 g_value_init (&val, GTK_TYPE_THEMING_ENGINE);
92 g_value_set_object (&val, (GObject *) gtk_theming_engine_load (NULL));
93 gtk_style_set_register_property ("engine", GTK_TYPE_THEMING_ENGINE, &val, NULL);
96 g_value_init (&val, GTK_TYPE_ANIMATION_DESCRIPTION);
97 g_value_take_boxed (&val, gtk_animation_description_new (0, GTK_TIMELINE_PROGRESS_LINEAR));
98 gtk_style_set_register_property ("transition", GTK_TYPE_ANIMATION_DESCRIPTION, &val, NULL);
101 g_type_class_add_private (object_class, sizeof (GtkStyleSetPrivate));
104 static PropertyData *
105 property_data_new (void)
109 data = g_slice_new0 (PropertyData);
110 data->values = g_array_new (FALSE, FALSE, sizeof (ValueData));
116 property_data_free (PropertyData *data)
120 for (i = 0; i < data->values->len; i++)
122 ValueData *value_data;
124 value_data = &g_array_index (data->values, ValueData, i);
126 if (G_IS_VALUE (&value_data->value))
127 g_value_unset (&value_data->value);
130 g_array_free (data->values, TRUE);
131 g_slice_free (PropertyData, data);
135 property_data_find_position (PropertyData *data,
140 gboolean found = FALSE;
146 if (data->values->len == 0)
149 /* Find position for the given state, or the position where
150 * it would be if not found, the array is ordered by the
154 max = data->values->len - 1;
158 ValueData *value_data;
160 mid = (min + max) / 2;
161 value_data = &g_array_index (data->values, ValueData, mid);
163 if (value_data->state == state)
168 else if (value_data->state < state)
169 position = min = mid + 1;
176 while (!found && min <= max);
185 property_data_get_value (PropertyData *data,
191 if (!property_data_find_position (data, state, &pos))
193 ValueData new = { 0 };
195 //val_data = &g_array_index (data->values, ValueData, pos);
197 g_array_insert_val (data->values, pos, new);
200 val_data = &g_array_index (data->values, ValueData, pos);
202 return &val_data->value;
206 property_data_match_state (PropertyData *data,
212 if (property_data_find_position (data, state, &pos))
217 val_data = &g_array_index (data->values, ValueData, pos);
218 return &val_data->value;
221 if (pos >= data->values->len)
222 pos = data->values->len - 1;
224 /* No exact match, go downwards the list to find
225 * the closest match to the given state flags, as
226 * a side effect, there is an implicit precedence
227 * of higher flags over the smaller ones.
229 for (i = pos; i >= 0; i--)
233 val_data = &g_array_index (data->values, ValueData, i);
235 /* Check whether any of the requested
236 * flags are set, and no other flags are.
238 * Also, no flags acts as a wildcard, such
239 * value should be always in the first position
240 * in the array (if present) anyways.
242 if (val_data->state == 0 ||
243 ((val_data->state & state) != 0 &&
244 (val_data->state & ~state) == 0))
245 return &val_data->value;
252 gtk_style_set_init (GtkStyleSet *set)
254 GtkStyleSetPrivate *priv;
256 priv = set->priv = G_TYPE_INSTANCE_GET_PRIVATE (set,
260 priv->properties = g_hash_table_new_full (NULL, NULL, NULL,
261 (GDestroyNotify) property_data_free);
265 gtk_style_set_finalize (GObject *object)
267 GtkStyleSetPrivate *priv;
270 set = GTK_STYLE_SET (object);
272 g_hash_table_destroy (priv->properties);
275 g_hash_table_destroy (priv->color_map);
277 G_OBJECT_CLASS (gtk_style_set_parent_class)->finalize (object);
281 gtk_style_set_get_style (GtkStyleProvider *provider,
284 /* Return style set itself */
285 return g_object_ref (provider);
289 gtk_style_set_provider_init (GtkStyleProviderIface *iface)
291 iface->get_style = gtk_style_set_get_style;
295 compare_property (gconstpointer p1,
298 PropertyNode *key = (PropertyNode *) p1;
299 PropertyNode *node = (PropertyNode *) p2;
301 return (int) key->property_quark - node->property_quark;
304 static PropertyNode *
305 property_node_lookup (GQuark quark)
307 PropertyNode key = { 0 };
315 key.property_quark = quark;
317 return bsearch (&key, properties->data, properties->len,
318 sizeof (PropertyNode), compare_property);
321 /* Property registration functions */
323 gtk_style_set_register_property (const gchar *property_name,
325 const GValue *default_value,
326 GtkStylePropertyParser parse_func)
328 PropertyNode *node, new = { 0 };
332 g_return_if_fail (property_name != NULL);
333 g_return_if_fail (type != 0);
335 if (G_UNLIKELY (!properties))
336 properties = g_array_new (FALSE, TRUE, sizeof (PropertyNode));
338 quark = g_quark_try_string (property_name);
340 if ((node = property_node_lookup (quark)) != NULL)
342 g_warning ("Property \"%s\" was already registered with type %s",
343 property_name, g_type_name (node->property_type));
347 quark = g_quark_from_string (property_name);
349 new.property_quark = quark;
350 new.property_type = type;
354 g_value_init (&new.default_value, G_VALUE_TYPE (default_value));
355 g_value_copy (default_value, &new.default_value);
359 new.parse_func = parse_func;
361 for (i = 0; i < properties->len; i++)
363 node = &g_array_index (properties, PropertyNode, i);
365 if (node->property_quark > quark)
369 g_array_insert_val (properties, i, new);
373 gtk_style_set_lookup_property (const gchar *property_name,
375 GtkStylePropertyParser *parse_func)
378 GtkStyleSetClass *klass;
379 gboolean found = FALSE;
383 g_return_val_if_fail (property_name != NULL, FALSE);
385 klass = g_type_class_ref (GTK_TYPE_STYLE_SET);
386 quark = g_quark_try_string (property_name);
390 g_type_class_unref (klass);
394 for (i = 0; i < properties->len; i++)
396 node = &g_array_index (properties, PropertyNode, i);
398 if (node->property_quark == quark)
401 *type = node->property_type;
404 *parse_func = node->parse_func;
409 else if (node->property_quark > quark)
413 g_type_class_unref (klass);
418 /* GtkStyleSet methods */
421 gtk_style_set_new (void)
423 return g_object_new (GTK_TYPE_STYLE_SET, NULL);
427 gtk_style_set_map_color (GtkStyleSet *set,
429 GtkSymbolicColor *color)
431 GtkStyleSetPrivate *priv;
433 g_return_if_fail (GTK_IS_STYLE_SET (set));
434 g_return_if_fail (name != NULL);
435 g_return_if_fail (color != NULL);
439 if (G_UNLIKELY (!priv->color_map))
440 priv->color_map = g_hash_table_new_full (g_str_hash,
442 (GDestroyNotify) g_free,
443 (GDestroyNotify) gtk_symbolic_color_unref);
445 g_hash_table_replace (priv->color_map,
447 gtk_symbolic_color_ref (color));
451 gtk_style_set_lookup_color (GtkStyleSet *set,
454 GtkStyleSetPrivate *priv;
456 g_return_val_if_fail (GTK_IS_STYLE_SET (set), NULL);
457 g_return_val_if_fail (name != NULL, NULL);
461 if (!priv->color_map)
464 return g_hash_table_lookup (priv->color_map, name);
468 gtk_style_set_set_property (GtkStyleSet *set,
469 const gchar *property,
473 GtkStyleSetPrivate *priv;
479 g_return_if_fail (GTK_IS_STYLE_SET (set));
480 g_return_if_fail (property != NULL);
481 g_return_if_fail (value != NULL);
483 value_type = G_VALUE_TYPE (value);
484 node = property_node_lookup (g_quark_try_string (property));
488 g_warning ("Style property \"%s\" is not registered", property);
492 if (node->property_type == GDK_TYPE_COLOR)
494 /* Allow GtkSymbolicColor as well */
495 g_return_if_fail (value_type == GDK_TYPE_COLOR || value_type == GTK_TYPE_SYMBOLIC_COLOR);
498 g_return_if_fail (node->property_type == value_type);
501 prop = g_hash_table_lookup (priv->properties,
502 GINT_TO_POINTER (node->property_quark));
506 prop = property_data_new ();
507 g_hash_table_insert (priv->properties,
508 GINT_TO_POINTER (node->property_quark),
512 val = property_data_get_value (prop, state);
514 if (G_VALUE_TYPE (val) == value_type)
518 if (G_IS_VALUE (val))
521 g_value_init (val, value_type);
524 g_value_copy (value, val);
528 gtk_style_set_set_valist (GtkStyleSet *set,
532 GtkStyleSetPrivate *priv;
533 const gchar *property_name;
535 g_return_if_fail (GTK_IS_STYLE_SET (set));
538 property_name = va_arg (args, const gchar *);
540 while (property_name)
547 node = property_node_lookup (g_quark_try_string (property_name));
551 g_warning ("Style property \"%s\" is not registered", property_name);
555 prop = g_hash_table_lookup (priv->properties,
556 GINT_TO_POINTER (node->property_quark));
560 prop = property_data_new ();
561 g_hash_table_insert (priv->properties,
562 GINT_TO_POINTER (node->property_quark),
566 val = property_data_get_value (prop, state);
568 if (G_IS_VALUE (val))
571 g_value_init (val, node->property_type);
572 G_VALUE_COLLECT (val, args, 0, &error);
576 g_warning ("Could not set style property \"%s\": %s", property_name, error);
582 property_name = va_arg (args, const gchar *);
587 gtk_style_set_set (GtkStyleSet *set,
593 g_return_if_fail (GTK_IS_STYLE_SET (set));
595 va_start (args, state);
596 gtk_style_set_set_valist (set, state, args);
601 resolve_color (GtkStyleSet *set,
606 /* Resolve symbolic color to GdkColor */
607 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), set, &color))
610 /* Store it back, this is where GdkColor caching happens */
611 g_value_unset (value);
612 g_value_init (value, GDK_TYPE_COLOR);
613 g_value_set_boxed (value, &color);
619 gtk_style_set_get_property (GtkStyleSet *set,
620 const gchar *property,
624 GtkStyleSetPrivate *priv;
629 g_return_val_if_fail (GTK_IS_STYLE_SET (set), FALSE);
630 g_return_val_if_fail (property != NULL, FALSE);
631 g_return_val_if_fail (value != NULL, FALSE);
633 node = property_node_lookup (g_quark_try_string (property));
637 g_warning ("Style property \"%s\" is not registered", property);
642 prop = g_hash_table_lookup (priv->properties,
643 GINT_TO_POINTER (node->property_quark));
648 g_value_init (value, node->property_type);
650 val = property_data_match_state (prop, state);
652 if (!val && G_IS_VALUE (&node->default_value))
653 val = &node->default_value;
655 g_return_val_if_fail (G_IS_VALUE (val), FALSE);
657 if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
659 g_return_val_if_fail (node->property_type == GDK_TYPE_COLOR, FALSE);
661 if (!resolve_color (set, val))
665 g_value_copy (val, value);
671 gtk_style_set_get_valist (GtkStyleSet *set,
675 GtkStyleSetPrivate *priv;
676 const gchar *property_name;
678 g_return_if_fail (GTK_IS_STYLE_SET (set));
681 property_name = va_arg (args, const gchar *);
683 while (property_name)
689 node = property_node_lookup (g_quark_try_string (property_name));
693 g_warning ("Style property \"%s\" is not registered", property_name);
697 prop = g_hash_table_lookup (priv->properties,
698 GINT_TO_POINTER (node->property_quark));
700 if (!prop && !G_IS_VALUE (&node->default_value))
702 GValue *empty_value = { 0 };
704 g_warning ("No value for style property \"%s\"", property_name);
706 g_value_init (&empty_value, node->property_type);
707 G_VALUE_LCOPY (&empty_value, args, 0, &error);
714 val = property_data_match_state (prop, state);
716 if (!val && G_IS_VALUE (&node->default_value))
717 val = &node->default_value;
719 if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
721 g_return_if_fail (node->property_type == GDK_TYPE_COLOR);
723 if (!resolve_color (set, val))
727 G_VALUE_LCOPY (val, args, 0, &error);
732 g_warning ("Could not get style property \"%s\": %s", property_name, error);
737 property_name = va_arg (args, const gchar *);
742 gtk_style_set_get (GtkStyleSet *set,
748 g_return_if_fail (GTK_IS_STYLE_SET (set));
750 va_start (args, state);
751 gtk_style_set_get_valist (set, state, args);
756 gtk_style_set_unset_property (GtkStyleSet *set,
757 const gchar *property,
760 GtkStyleSetPrivate *priv;
765 g_return_if_fail (GTK_IS_STYLE_SET (set));
766 g_return_if_fail (property != NULL);
768 node = property_node_lookup (g_quark_try_string (property));
772 g_warning ("Style property \"%s\" is not registered", property);
777 prop = g_hash_table_lookup (priv->properties,
778 GINT_TO_POINTER (node->property_quark));
783 if (property_data_find_position (prop, state, &pos))
787 data = &g_array_index (prop->values, ValueData, pos);
789 if (G_IS_VALUE (&data->value))
790 g_value_unset (&data->value);
792 g_array_remove_index (prop->values, pos);
797 gtk_style_set_clear (GtkStyleSet *set)
799 GtkStyleSetPrivate *priv;
801 g_return_if_fail (GTK_IS_STYLE_SET (set));
804 g_hash_table_remove_all (priv->properties);
808 gtk_style_set_merge (GtkStyleSet *set,
809 const GtkStyleSet *set_to_merge,
812 GtkStyleSetPrivate *priv, *priv_to_merge;
816 g_return_if_fail (GTK_IS_STYLE_SET (set));
817 g_return_if_fail (GTK_IS_STYLE_SET (set_to_merge));
820 priv_to_merge = set_to_merge->priv;
822 /* Merge symbolic color map */
823 if (priv_to_merge->color_map)
825 g_hash_table_iter_init (&iter, priv_to_merge->color_map);
827 while (g_hash_table_iter_next (&iter, &key, &value))
830 GtkSymbolicColor *color;
836 g_hash_table_lookup (priv->color_map, name))
839 gtk_style_set_map_color (set, name, color);
843 /* Merge symbolic style properties */
844 g_hash_table_iter_init (&iter, priv_to_merge->properties);
846 while (g_hash_table_iter_next (&iter, &key, &value))
848 PropertyData *prop_to_merge = value;
852 prop = g_hash_table_lookup (priv->properties, key);
856 prop = property_data_new ();
857 g_hash_table_insert (priv->properties, key, prop);
860 for (i = 0; i < prop_to_merge->values->len; i++)
865 data = &g_array_index (prop_to_merge->values, ValueData, i);
866 value = property_data_get_value (prop, data->state);
868 if (replace || !G_IS_VALUE (value))
870 if (!G_IS_VALUE (value))
871 g_value_init (value, G_VALUE_TYPE (&data->value));
872 else if (G_VALUE_TYPE (value) != G_VALUE_TYPE (&data->value))
874 g_value_unset (value);
875 g_value_init (value, G_VALUE_TYPE (&data->value));
878 g_value_copy (&data->value, value);