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 "gtkstylepropertyprivate.h"
29 #include <gdk-pixbuf/gdk-pixbuf.h>
30 #include <cairo-gobject.h>
32 #include "gtkcssprovider.h"
33 #include "gtkcssparserprivate.h"
34 #include "gtkcssshorthandpropertyprivate.h"
35 #include "gtkcssstylefuncsprivate.h"
36 #include "gtkcssstylepropertyprivate.h"
37 #include "gtkcsstypesprivate.h"
39 #include "gtkprivatetypebuiltins.h"
40 #include "gtkstylepropertiesprivate.h"
42 /* the actual parsers we have */
43 #include "gtkanimationdescription.h"
44 #include "gtkbindings.h"
45 #include "gtkgradient.h"
46 #include "gtkshadowprivate.h"
47 #include "gtkthemingengine.h"
48 #include "gtktypebuiltins.h"
49 #include "gtkwin32themeprivate.h"
51 /* this is in case round() is not provided by the compiler,
52 * such as in the case of C89 compilers, like MSVC
54 #include "fallback-c89.c"
62 G_DEFINE_ABSTRACT_TYPE (GtkStyleProperty, _gtk_style_property, G_TYPE_OBJECT)
65 gtk_style_property_finalize (GObject *object)
67 GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
69 g_warning ("finalizing %s `%s', how could this happen?", G_OBJECT_TYPE_NAME (object), property->name);
71 G_OBJECT_CLASS (_gtk_style_property_parent_class)->finalize (object);
75 gtk_style_property_set_property (GObject *object,
80 GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
81 GtkStylePropertyClass *klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
86 property->name = g_value_dup_string (value);
87 g_assert (property->name);
88 g_assert (g_hash_table_lookup (klass->properties, property->name) == NULL);
89 g_hash_table_insert (klass->properties, property->name, property);
92 property->value_type = g_value_get_gtype (value);
95 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
101 gtk_style_property_get_property (GObject *object,
106 GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
111 g_value_set_string (value, property->name);
113 case PROP_VALUE_TYPE:
114 g_value_set_gtype (value, property->value_type);
117 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
123 gtk_style_property_real_parse_value (GtkStyleProperty *property,
125 GtkCssParser *parser,
129 _gtk_style_property_class_init (GtkStylePropertyClass *klass)
131 GObjectClass *object_class = G_OBJECT_CLASS (klass);
133 object_class->finalize = gtk_style_property_finalize;
134 object_class->set_property = gtk_style_property_set_property;
135 object_class->get_property = gtk_style_property_get_property;
137 g_object_class_install_property (object_class,
139 g_param_spec_string ("name",
141 P_("The name of the property"),
143 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
144 g_object_class_install_property (object_class,
146 g_param_spec_gtype ("value-type",
148 P_("The value type returned by GtkStyleContext"),
150 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
152 klass->parse_value = gtk_style_property_real_parse_value;
154 klass->properties = g_hash_table_new (g_str_hash, g_str_equal);
158 _gtk_style_property_init (GtkStyleProperty *property)
160 property->value_type = G_TYPE_NONE;
164 string_append_double (GString *string,
167 char buf[G_ASCII_DTOSTR_BUF_SIZE];
169 g_ascii_dtostr (buf, sizeof (buf), d);
170 g_string_append (string, buf);
174 string_append_string (GString *str,
179 g_string_append_c (str, '"');
182 len = strcspn (string, "\"\n\r\f");
183 g_string_append (str, string);
190 g_string_append (str, "\\A ");
193 g_string_append (str, "\\D ");
196 g_string_append (str, "\\C ");
199 g_string_append (str, "\\\"");
202 g_assert_not_reached ();
207 g_string_append_c (str, '"');
210 /*** IMPLEMENTATIONS ***/
213 font_family_parse (GtkCssParser *parser,
220 /* We don't special case generic families. Pango should do
223 names = g_ptr_array_new ();
226 name = _gtk_css_parser_try_ident (parser, TRUE);
229 GString *string = g_string_new (name);
231 while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
233 g_string_append_c (string, ' ');
234 g_string_append (string, name);
237 name = g_string_free (string, FALSE);
241 name = _gtk_css_parser_read_string (parser);
244 g_ptr_array_free (names, TRUE);
249 g_ptr_array_add (names, name);
250 } while (_gtk_css_parser_try (parser, ",", TRUE));
252 /* NULL-terminate array */
253 g_ptr_array_add (names, NULL);
254 g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
259 font_family_value_print (const GValue *value,
262 const char **names = g_value_get_boxed (value);
264 if (names == NULL || *names == NULL)
266 g_string_append (string, "none");
270 string_append_string (string, *names);
274 g_string_append (string, ", ");
275 string_append_string (string, *names);
281 bindings_value_parse (GtkCssParser *parser,
286 GtkBindingSet *binding_set;
289 array = g_ptr_array_new ();
292 name = _gtk_css_parser_try_ident (parser, TRUE);
295 _gtk_css_parser_error (parser, "Not a valid binding name");
296 g_ptr_array_free (array, TRUE);
300 binding_set = gtk_binding_set_find (name);
304 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
309 g_ptr_array_add (array, binding_set);
312 while (_gtk_css_parser_try (parser, ",", TRUE));
314 g_value_take_boxed (value, array);
320 bindings_value_print (const GValue *value,
326 array = g_value_get_boxed (value);
328 for (i = 0; i < array->len; i++)
330 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
333 g_string_append (string, ", ");
334 g_string_append (string, binding_set->set_name);
339 border_corner_radius_value_parse (GtkCssParser *parser,
343 GtkCssBorderCornerRadius corner;
345 if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
347 _gtk_css_parser_error (parser, "Expected a number");
350 else if (corner.horizontal < 0)
353 if (!_gtk_css_parser_try_double (parser, &corner.vertical))
354 corner.vertical = corner.horizontal;
355 else if (corner.vertical < 0)
358 g_value_set_boxed (value, &corner);
362 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
367 border_corner_radius_value_print (const GValue *value,
370 GtkCssBorderCornerRadius *corner;
372 corner = g_value_get_boxed (value);
376 g_string_append (string, "none");
380 string_append_double (string, corner->horizontal);
381 if (corner->horizontal != corner->vertical)
383 g_string_append_c (string, ' ');
384 string_append_double (string, corner->vertical);
391 _gtk_style_property_parse_value (GtkStyleProperty *property,
393 GtkCssParser *parser,
396 GtkStylePropertyClass *klass;
398 g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), FALSE);
399 g_return_val_if_fail (value != NULL, FALSE);
400 g_return_val_if_fail (parser != NULL, FALSE);
402 klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
404 return klass->parse_value (property, value, parser, base);
408 gtk_style_property_real_parse_value (GtkStyleProperty *property,
410 GtkCssParser *parser,
413 if (_gtk_css_parser_try (parser, "initial", TRUE))
415 /* the initial value can be explicitly specified with the
416 * ‘initial’ keyword which all properties accept.
418 g_value_unset (value);
419 g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
420 g_value_set_enum (value, GTK_CSS_INITIAL);
423 else if (_gtk_css_parser_try (parser, "inherit", TRUE))
425 /* All properties accept the ‘inherit’ value which
426 * explicitly specifies that the value will be determined
427 * by inheritance. The ‘inherit’ value can be used to
428 * strengthen inherited values in the cascade, and it can
429 * also be used on properties that are not normally inherited.
431 g_value_unset (value);
432 g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
433 g_value_set_enum (value, GTK_CSS_INHERIT);
436 else if (property->property_parse_func)
438 GError *error = NULL;
442 value_str = _gtk_css_parser_read_value (parser);
443 if (value_str == NULL)
446 success = (*property->property_parse_func) (value_str, value, &error);
452 else if (property->parse_func)
453 return (* property->parse_func) (parser, base, value);
455 return _gtk_css_style_parse_value (value, parser, base);
459 _gtk_style_property_unpack (GtkStyleProperty *property,
463 g_return_val_if_fail (property != NULL, NULL);
464 g_return_val_if_fail (property->unpack_func != NULL, NULL);
465 g_return_val_if_fail (value != NULL, NULL);
466 g_return_val_if_fail (n_params != NULL, NULL);
468 return property->unpack_func (value, n_params);
472 * _gtk_style_property_assign:
473 * @property: the property
474 * @props: The properties to assign to
475 * @state: The state to assign
476 * @value: (out): the #GValue with the value to be
479 * This function is called by gtk_style_properties_set() and in
480 * turn gtk_style_context_set() and similar functions to set the
481 * value from code using old APIs.
484 _gtk_style_property_assign (GtkStyleProperty *property,
485 GtkStyleProperties *props,
489 GtkStylePropertyClass *klass;
491 g_return_if_fail (GTK_IS_STYLE_PROPERTY (property));
492 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
493 g_return_if_fail (value != NULL);
495 klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
497 klass->assign (property, props, state, value);
501 * _gtk_style_property_query:
502 * @property: the property
503 * @props: The properties to query
504 * @state: The state to query
505 * @value: (out): an uninitialized #GValue to be filled with the
506 * contents of the lookup
508 * This function is called by gtk_style_properties_get() and in
509 * turn gtk_style_context_get() and similar functions to get the
510 * value to return to code using old APIs.
513 _gtk_style_property_query (GtkStyleProperty *property,
514 GtkStyleProperties *props,
516 GtkStylePropertyContext *context,
519 GtkStylePropertyClass *klass;
521 g_return_if_fail (property != NULL);
522 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
523 g_return_if_fail (context != NULL);
524 g_return_if_fail (value != NULL);
526 klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
528 g_value_init (value, property->value_type);
530 klass->query (property, props, state, context, value);
533 #define rgba_init(rgba, r, g, b, a) G_STMT_START{ \
535 (rgba)->green = (g); \
536 (rgba)->blue = (b); \
537 (rgba)->alpha = (a); \
540 gtk_style_property_init_properties (void)
542 static gboolean initialized = FALSE;
543 GValue value = { 0, };
544 char *default_font_family[] = { "Sans", NULL };
547 if (G_LIKELY (initialized))
552 g_value_init (&value, GDK_TYPE_RGBA);
553 rgba_init (&rgba, 1, 1, 1, 1);
554 g_value_set_boxed (&value, &rgba);
555 _gtk_style_property_register (g_param_spec_boxed ("color",
559 GTK_STYLE_PROPERTY_INHERIT,
564 rgba_init (&rgba, 0, 0, 0, 0);
565 g_value_set_boxed (&value, &rgba);
566 _gtk_style_property_register (g_param_spec_boxed ("background-color",
575 g_value_unset (&value);
577 g_value_init (&value, G_TYPE_STRV);
578 g_value_set_boxed (&value, default_font_family);
579 _gtk_style_property_register (g_param_spec_boxed ("font-family",
583 GTK_STYLE_PROPERTY_INHERIT,
586 font_family_value_print,
588 g_value_unset (&value);
589 _gtk_style_property_register (g_param_spec_enum ("font-style",
593 PANGO_STYLE_NORMAL, 0),
594 GTK_STYLE_PROPERTY_INHERIT,
599 _gtk_style_property_register (g_param_spec_enum ("font-variant",
603 PANGO_VARIANT_NORMAL, 0),
604 GTK_STYLE_PROPERTY_INHERIT,
609 /* xxx: need to parse this properly, ie parse the numbers */
610 _gtk_style_property_register (g_param_spec_enum ("font-weight",
614 PANGO_WEIGHT_NORMAL, 0),
615 GTK_STYLE_PROPERTY_INHERIT,
620 g_value_init (&value, G_TYPE_DOUBLE);
621 g_value_set_double (&value, 10);
622 _gtk_style_property_register (g_param_spec_double ("font-size",
625 0, G_MAXDOUBLE, 0, 0),
626 GTK_STYLE_PROPERTY_INHERIT,
631 g_value_unset (&value);
633 _gtk_style_property_register (g_param_spec_boxed ("text-shadow",
637 GTK_STYLE_PROPERTY_INHERIT,
643 _gtk_style_property_register (g_param_spec_boxed ("icon-shadow",
647 GTK_STYLE_PROPERTY_INHERIT,
653 gtk_style_properties_register_property (NULL,
654 g_param_spec_boxed ("box-shadow",
657 GTK_TYPE_SHADOW, 0));
658 gtk_style_properties_register_property (NULL,
659 g_param_spec_int ("margin-top",
663 gtk_style_properties_register_property (NULL,
664 g_param_spec_int ("margin-left",
668 gtk_style_properties_register_property (NULL,
669 g_param_spec_int ("margin-bottom",
673 gtk_style_properties_register_property (NULL,
674 g_param_spec_int ("margin-right",
678 gtk_style_properties_register_property (NULL,
679 g_param_spec_int ("padding-top",
683 gtk_style_properties_register_property (NULL,
684 g_param_spec_int ("padding-left",
688 gtk_style_properties_register_property (NULL,
689 g_param_spec_int ("padding-bottom",
693 gtk_style_properties_register_property (NULL,
694 g_param_spec_int ("padding-right",
698 gtk_style_properties_register_property (NULL,
699 g_param_spec_int ("border-top-width",
701 "Border width at top",
703 gtk_style_properties_register_property (NULL,
704 g_param_spec_int ("border-left-width",
706 "Border width at left",
708 gtk_style_properties_register_property (NULL,
709 g_param_spec_int ("border-bottom-width",
710 "border bottom width",
711 "Border width at bottom",
713 gtk_style_properties_register_property (NULL,
714 g_param_spec_int ("border-right-width",
715 "border right width",
716 "Border width at right",
719 _gtk_style_property_register (g_param_spec_boxed ("border-top-left-radius",
720 "Border top left radius",
721 "Border radius of top left corner, in pixels",
722 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
725 border_corner_radius_value_parse,
726 border_corner_radius_value_print,
728 _gtk_style_property_register (g_param_spec_boxed ("border-top-right-radius",
729 "Border top right radius",
730 "Border radius of top right corner, in pixels",
731 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
734 border_corner_radius_value_parse,
735 border_corner_radius_value_print,
737 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-right-radius",
738 "Border bottom right radius",
739 "Border radius of bottom right corner, in pixels",
740 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
743 border_corner_radius_value_parse,
744 border_corner_radius_value_print,
746 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-left-radius",
747 "Border bottom left radius",
748 "Border radius of bottom left corner, in pixels",
749 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
752 border_corner_radius_value_parse,
753 border_corner_radius_value_print,
756 gtk_style_properties_register_property (NULL,
757 g_param_spec_enum ("border-style",
760 GTK_TYPE_BORDER_STYLE,
761 GTK_BORDER_STYLE_NONE, 0));
762 gtk_style_properties_register_property (NULL,
763 g_param_spec_enum ("background-clip",
767 GTK_CSS_AREA_BORDER_BOX, 0));
768 gtk_style_properties_register_property (NULL,
769 g_param_spec_enum ("background-origin",
773 GTK_CSS_AREA_PADDING_BOX, 0));
774 g_value_init (&value, GTK_TYPE_CSS_SPECIAL_VALUE);
775 g_value_set_enum (&value, GTK_CSS_CURRENT_COLOR);
776 _gtk_style_property_register (g_param_spec_boxed ("border-top-color",
785 _gtk_style_property_register (g_param_spec_boxed ("border-right-color",
786 "Border right color",
787 "Border right color",
794 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-color",
795 "Border bottom color",
796 "Border bottom color",
803 _gtk_style_property_register (g_param_spec_boxed ("border-left-color",
812 g_value_unset (&value);
814 gtk_style_properties_register_property (NULL,
815 g_param_spec_boxed ("background-image",
818 CAIRO_GOBJECT_TYPE_PATTERN, 0));
819 gtk_style_properties_register_property (NULL,
820 g_param_spec_boxed ("background-repeat",
823 GTK_TYPE_CSS_BACKGROUND_REPEAT, 0));
825 gtk_style_properties_register_property (NULL,
826 g_param_spec_boxed ("border-image-source",
827 "Border image source",
828 "Border image source",
829 CAIRO_GOBJECT_TYPE_PATTERN, 0));
830 gtk_style_properties_register_property (NULL,
831 g_param_spec_boxed ("border-image-repeat",
832 "Border image repeat",
833 "Border image repeat",
834 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
835 gtk_style_properties_register_property (NULL,
836 g_param_spec_boxed ("border-image-slice",
837 "Border image slice",
838 "Border image slice",
839 GTK_TYPE_BORDER, 0));
840 g_value_init (&value, GTK_TYPE_BORDER);
841 _gtk_style_property_register (g_param_spec_boxed ("border-image-width",
842 "Border image width",
843 "Border image width",
850 g_value_unset (&value);
851 gtk_style_properties_register_property (NULL,
852 g_param_spec_object ("engine",
855 GTK_TYPE_THEMING_ENGINE, 0));
856 gtk_style_properties_register_property (NULL,
857 g_param_spec_boxed ("transition",
858 "Transition animation description",
859 "Transition animation description",
860 GTK_TYPE_ANIMATION_DESCRIPTION, 0));
862 /* Private property holding the binding sets */
863 _gtk_style_property_register (g_param_spec_boxed ("gtk-key-bindings",
866 G_TYPE_PTR_ARRAY, 0),
869 bindings_value_parse,
870 bindings_value_print,
873 /* initialize shorthands last, they depend on the real properties existing */
874 _gtk_css_shorthand_property_init_properties ();
878 * _gtk_style_property_lookup:
879 * @name: name of the property to lookup
881 * Looks up the CSS property with the given @name. If no such
882 * property exists, %NULL is returned.
884 * Returns: (transfer none): The property or %NULL if no
885 * property with the given name exists.
888 _gtk_style_property_lookup (const char *name)
890 GtkStylePropertyClass *klass;
892 g_return_val_if_fail (name != NULL, NULL);
894 gtk_style_property_init_properties ();
896 klass = g_type_class_peek (GTK_TYPE_STYLE_PROPERTY);
898 return g_hash_table_lookup (klass->properties, name);
902 * _gtk_style_property_get_name:
903 * @property: the property to query
905 * Gets the name of the given property.
907 * Returns: the name of the property
910 _gtk_style_property_get_name (GtkStyleProperty *property)
912 g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), NULL);
914 return property->name;
918 * _gtk_style_property_get_value_type:
919 * @property: the property to query
921 * Gets the value type of the @property, if the property is usable
922 * in public API via _gtk_style_property_assign() and
923 * _gtk_style_property_query(). If the @property is not usable in that
924 * way, %G_TYPE_NONE is returned.
926 * Returns: the value type in use or %G_TYPE_NONE if none.
929 _gtk_style_property_get_value_type (GtkStyleProperty *property)
931 g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), G_TYPE_NONE);
933 return property->value_type;
938 _gtk_style_property_register (GParamSpec *pspec,
939 GtkStylePropertyFlags flags,
940 GtkStylePropertyParser property_parse_func,
941 GtkStyleParseFunc parse_func,
942 GtkStylePrintFunc print_func,
943 const GValue * initial_value)
945 GtkStyleProperty *node;
946 GValue initial_fallback = { 0, };
948 if (initial_value == NULL)
950 g_value_init (&initial_fallback, pspec->value_type);
951 if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
952 g_value_set_object (&initial_fallback, gtk_theming_engine_load (NULL));
953 else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
954 g_value_take_boxed (&initial_fallback, pango_font_description_from_string ("Sans 10"));
955 else if (pspec->value_type == GDK_TYPE_RGBA)
958 gdk_rgba_parse (&color, "pink");
959 g_value_set_boxed (&initial_fallback, &color);
961 else if (pspec->value_type == GTK_TYPE_BORDER)
963 g_value_take_boxed (&initial_fallback, gtk_border_new ());
966 g_param_value_set_default (pspec, &initial_fallback);
968 initial_value = &initial_fallback;
971 node = g_object_new (GTK_TYPE_CSS_STYLE_PROPERTY,
972 "inherit", (flags & GTK_STYLE_PROPERTY_INHERIT) ? TRUE : FALSE,
973 "initial-value", initial_value,
975 "value-type", pspec->value_type,
977 g_assert (node->value_type == pspec->value_type);
978 GTK_CSS_STYLE_PROPERTY (node)->pspec = pspec;
979 node->property_parse_func = property_parse_func;
980 node->parse_func = parse_func;
981 node->print_func = print_func;
983 if (G_IS_VALUE (&initial_fallback))
984 g_value_unset (&initial_fallback);