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"
24 #include <gobject/gvaluecollector.h>
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <cairo-gobject.h>
28 #include "gtkcssparserprivate.h"
29 #include "gtkcssstylefuncsprivate.h"
30 #include "gtkcssstylepropertyprivate.h"
31 #include "gtkcsstypesprivate.h"
33 #include "gtkprivatetypebuiltins.h"
34 #include "gtkstylepropertiesprivate.h"
36 /* the actual parsers we have */
37 #include "gtkanimationdescription.h"
38 #include "gtkbindings.h"
39 #include "gtkcssimageprivate.h"
40 #include "gtkgradient.h"
41 #include "gtkshadowprivate.h"
42 #include "gtksymboliccolorprivate.h"
43 #include "gtkthemingengine.h"
44 #include "gtktypebuiltins.h"
45 #include "gtkwin32themeprivate.h"
47 /*** REGISTRATION ***/
50 color_compute (GtkCssStyleProperty *property,
52 GtkStyleContext *context,
53 const GValue *specified)
55 if (G_VALUE_HOLDS (specified, GTK_TYPE_SYMBOLIC_COLOR))
57 GtkSymbolicColor *symbolic = g_value_get_boxed (specified);
60 if (symbolic == _gtk_symbolic_color_get_current_color ())
62 /* The computed value of the ‘currentColor’ keyword is the computed
63 * value of the ‘color’ property. If the ‘currentColor’ keyword is
64 * set on the ‘color’ property itself, it is treated as ‘color: inherit’.
66 if (g_str_equal (_gtk_style_property_get_name (GTK_STYLE_PROPERTY (property)), "color"))
68 GtkStyleContext *parent = gtk_style_context_get_parent (context);
71 g_value_copy (_gtk_style_context_peek_property (parent, "color"), computed);
73 _gtk_css_style_compute_value (computed,
75 _gtk_css_style_property_get_initial_value (property));
79 g_value_copy (_gtk_style_context_peek_property (context, "color"), computed);
82 else if (_gtk_style_context_resolve_color (context,
86 g_value_set_boxed (computed, &rgba);
90 color_compute (property,
93 _gtk_css_style_property_get_initial_value (property));
98 g_value_copy (specified, computed);
102 _gtk_style_property_register (const char * name,
105 GtkStylePropertyFlags flags,
106 GtkCssStylePropertyParseFunc parse_value,
107 GtkCssStylePropertyPrintFunc print_value,
108 GtkCssStylePropertyComputeFunc compute_value,
109 const GValue * initial_value)
111 GtkCssStyleProperty *node;
113 node = g_object_new (GTK_TYPE_CSS_STYLE_PROPERTY,
114 "value-type", value_type,
115 "computed-type", computed_type,
116 "inherit", (flags & GTK_STYLE_PROPERTY_INHERIT) ? TRUE : FALSE,
117 "initial-value", initial_value,
122 node->parse_value = parse_value;
124 node->print_value = print_value;
126 node->compute_value = compute_value;
130 gtk_style_property_register (const char * name,
131 GType specified_type,
134 GtkStylePropertyFlags flags,
135 GtkCssStylePropertyParseFunc parse_value,
136 GtkCssStylePropertyPrintFunc print_value,
137 GtkCssStylePropertyComputeFunc compute_value,
140 GValue initial_value = G_VALUE_INIT;
144 va_start (args, compute_value);
145 G_VALUE_COLLECT_INIT (&initial_value, specified_type,
149 g_error ("property `%s' initial value is broken: %s", name, error);
150 g_value_unset (&initial_value);
156 _gtk_style_property_register (name,
165 g_value_unset (&initial_value);
171 string_append_double (GString *string,
174 char buf[G_ASCII_DTOSTR_BUF_SIZE];
176 g_ascii_dtostr (buf, sizeof (buf), d);
177 g_string_append (string, buf);
181 string_append_string (GString *str,
186 g_string_append_c (str, '"');
189 len = strcspn (string, "\"\n\r\f");
190 g_string_append (str, string);
197 g_string_append (str, "\\A ");
200 g_string_append (str, "\\D ");
203 g_string_append (str, "\\C ");
206 g_string_append (str, "\\\"");
209 g_assert_not_reached ();
214 g_string_append_c (str, '"');
217 /*** IMPLEMENTATIONS ***/
220 font_family_parse (GtkCssStyleProperty *property,
222 GtkCssParser *parser,
228 /* We don't special case generic families. Pango should do
231 names = g_ptr_array_new ();
234 name = _gtk_css_parser_try_ident (parser, TRUE);
237 GString *string = g_string_new (name);
239 while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
241 g_string_append_c (string, ' ');
242 g_string_append (string, name);
245 name = g_string_free (string, FALSE);
249 name = _gtk_css_parser_read_string (parser);
252 g_ptr_array_free (names, TRUE);
257 g_ptr_array_add (names, name);
258 } while (_gtk_css_parser_try (parser, ",", TRUE));
260 /* NULL-terminate array */
261 g_ptr_array_add (names, NULL);
262 g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
267 font_family_value_print (GtkCssStyleProperty *property,
271 const char **names = g_value_get_boxed (value);
273 if (names == NULL || *names == NULL)
275 g_string_append (string, "none");
279 string_append_string (string, *names);
283 g_string_append (string, ", ");
284 string_append_string (string, *names);
290 bindings_value_parse (GtkCssStyleProperty *property,
292 GtkCssParser *parser,
296 GtkBindingSet *binding_set;
299 array = g_ptr_array_new ();
302 name = _gtk_css_parser_try_ident (parser, TRUE);
305 _gtk_css_parser_error (parser, "Not a valid binding name");
306 g_ptr_array_free (array, TRUE);
310 binding_set = gtk_binding_set_find (name);
314 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
319 g_ptr_array_add (array, binding_set);
322 while (_gtk_css_parser_try (parser, ",", TRUE));
324 g_value_take_boxed (value, array);
330 bindings_value_print (GtkCssStyleProperty *property,
337 array = g_value_get_boxed (value);
339 for (i = 0; i < array->len; i++)
341 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
344 g_string_append (string, ", ");
345 g_string_append (string, binding_set->set_name);
350 border_corner_radius_value_parse (GtkCssStyleProperty *property,
352 GtkCssParser *parser,
355 GtkCssBorderCornerRadius corner;
357 if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
359 _gtk_css_parser_error (parser, "Expected a number");
362 else if (corner.horizontal < 0)
365 if (!_gtk_css_parser_try_double (parser, &corner.vertical))
366 corner.vertical = corner.horizontal;
367 else if (corner.vertical < 0)
370 g_value_set_boxed (value, &corner);
374 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
379 border_corner_radius_value_print (GtkCssStyleProperty *property,
383 GtkCssBorderCornerRadius *corner;
385 corner = g_value_get_boxed (value);
389 g_string_append (string, "none");
393 string_append_double (string, corner->horizontal);
394 if (corner->horizontal != corner->vertical)
396 g_string_append_c (string, ' ');
397 string_append_double (string, corner->vertical);
402 css_image_value_parse (GtkCssStyleProperty *property,
404 GtkCssParser *parser,
409 if (_gtk_css_parser_try (parser, "none", TRUE))
413 image = _gtk_css_image_new_parse (parser, base);
418 g_value_take_object (value, image);
423 css_image_value_print (GtkCssStyleProperty *property,
427 GtkCssImage *image = g_value_get_object (value);
430 _gtk_css_image_print (image, string);
432 g_string_append (string, "none");
436 css_image_value_compute (GtkCssStyleProperty *property,
438 GtkStyleContext *context,
439 const GValue *specified)
441 GtkCssImage *image = g_value_get_object (specified);
444 image = _gtk_css_image_compute (image, context);
446 g_value_take_object (computed, image);
450 compute_border_width (GtkCssStyleProperty *property,
452 GtkStyleContext *context,
453 const GValue *specified)
455 GtkCssStyleProperty *style;
456 GtkBorderStyle border_style;
458 /* The -1 is magic that is only true because we register the style
459 * properties directly after the width properties.
461 style = _gtk_css_style_property_lookup_by_id (_gtk_css_style_property_get_id (property) - 1);
462 border_style = g_value_get_enum (_gtk_style_context_peek_property (context, _gtk_style_property_get_name (GTK_STYLE_PROPERTY (style))));
464 if (border_style == GTK_BORDER_STYLE_NONE ||
465 border_style == GTK_BORDER_STYLE_HIDDEN)
466 g_value_set_int (computed, 0);
468 g_value_copy (specified, computed);
472 background_repeat_value_parse (GtkCssStyleProperty *property,
474 GtkCssParser *parser,
477 int repeat, vertical;
479 if (!_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &repeat))
481 _gtk_css_parser_error (parser, "Not a valid value");
485 if (repeat <= GTK_CSS_BACKGROUND_REPEAT_MASK)
487 if (_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &vertical))
489 if (vertical >= GTK_CSS_BACKGROUND_REPEAT_MASK)
491 _gtk_css_parser_error (parser, "Not a valid 2nd value");
495 repeat |= vertical << GTK_CSS_BACKGROUND_REPEAT_SHIFT;
498 repeat |= repeat << GTK_CSS_BACKGROUND_REPEAT_SHIFT;
501 g_value_set_enum (value, repeat);
506 background_repeat_value_print (GtkCssStyleProperty *property,
510 GEnumClass *enum_class;
511 GEnumValue *enum_value;
512 GtkCssBackgroundRepeat repeat;
514 repeat = g_value_get_enum (value);
515 enum_class = g_type_class_ref (GTK_TYPE_CSS_BACKGROUND_REPEAT);
516 enum_value = g_enum_get_value (enum_class, repeat);
518 /* only triggers for 'repeat-x' and 'repeat-y' */
520 g_string_append (string, enum_value->value_nick);
523 enum_value = g_enum_get_value (enum_class, GTK_CSS_BACKGROUND_HORIZONTAL (repeat));
524 g_string_append (string, enum_value->value_nick);
526 if (GTK_CSS_BACKGROUND_HORIZONTAL (repeat) != GTK_CSS_BACKGROUND_VERTICAL (repeat))
528 enum_value = g_enum_get_value (enum_class, GTK_CSS_BACKGROUND_VERTICAL (repeat));
529 g_string_append (string, " ");
530 g_string_append (string, enum_value->value_nick);
534 g_type_class_unref (enum_class);
537 /*** REGISTRATION ***/
539 static GtkSymbolicColor *
540 gtk_symbolic_color_new_rgba (double red,
545 GdkRGBA rgba = { red, green, blue, alpha };
547 return gtk_symbolic_color_new_literal (&rgba);
551 _gtk_css_style_property_init_properties (void)
553 char *default_font_family[] = { "Sans", NULL };
554 GtkSymbolicColor *symbolic;
555 GtkCssBorderCornerRadius no_corner_radius = { 0, };
556 GtkBorder border_of_ones = { 1, 1, 1, 1 };
557 GtkCssBorderImageRepeat border_image_repeat = { GTK_CSS_REPEAT_STYLE_STRETCH, GTK_CSS_REPEAT_STYLE_STRETCH };
559 /* Initialize "color" and "font-size" first,
560 * so that when computing values later they are
561 * done first. That way, 'currentColor' and font
562 * sizes in em can be looked up properly */
563 symbolic = gtk_symbolic_color_new_rgba (1, 1, 1, 1);
564 gtk_style_property_register ("color",
565 GTK_TYPE_SYMBOLIC_COLOR,
568 GTK_STYLE_PROPERTY_INHERIT,
573 gtk_symbolic_color_unref (symbolic);
574 gtk_style_property_register ("font-size",
578 GTK_STYLE_PROPERTY_INHERIT,
584 /* properties that aren't referenced when computing values
586 symbolic = gtk_symbolic_color_new_rgba (0, 0, 0, 0);
587 gtk_style_property_register ("background-color",
588 GTK_TYPE_SYMBOLIC_COLOR,
596 gtk_symbolic_color_unref (symbolic);
598 gtk_style_property_register ("font-family",
602 GTK_STYLE_PROPERTY_INHERIT,
604 font_family_value_print,
606 default_font_family);
607 gtk_style_property_register ("font-style",
611 GTK_STYLE_PROPERTY_INHERIT,
616 gtk_style_property_register ("font-variant",
620 GTK_STYLE_PROPERTY_INHERIT,
624 PANGO_VARIANT_NORMAL);
625 /* xxx: need to parse this properly, ie parse the numbers */
626 gtk_style_property_register ("font-weight",
630 GTK_STYLE_PROPERTY_INHERIT,
634 PANGO_WEIGHT_NORMAL);
636 gtk_style_property_register ("text-shadow",
640 GTK_STYLE_PROPERTY_INHERIT,
646 gtk_style_property_register ("icon-shadow",
650 GTK_STYLE_PROPERTY_INHERIT,
656 gtk_style_property_register ("box-shadow",
666 gtk_style_property_register ("margin-top",
675 gtk_style_property_register ("margin-left",
684 gtk_style_property_register ("margin-bottom",
693 gtk_style_property_register ("margin-right",
702 gtk_style_property_register ("padding-top",
711 gtk_style_property_register ("padding-left",
720 gtk_style_property_register ("padding-bottom",
729 gtk_style_property_register ("padding-right",
738 /* IMPORTANT: compute_border_width() requires that the border-width
739 * properties be immeditaly followed by the border-style properties
741 gtk_style_property_register ("border-top-style",
742 GTK_TYPE_BORDER_STYLE,
743 GTK_TYPE_BORDER_STYLE,
744 GTK_TYPE_BORDER_STYLE,
749 GTK_BORDER_STYLE_NONE);
750 gtk_style_property_register ("border-top-width",
757 compute_border_width,
759 gtk_style_property_register ("border-left-style",
760 GTK_TYPE_BORDER_STYLE,
761 GTK_TYPE_BORDER_STYLE,
762 GTK_TYPE_BORDER_STYLE,
767 GTK_BORDER_STYLE_NONE);
768 gtk_style_property_register ("border-left-width",
775 compute_border_width,
777 gtk_style_property_register ("border-bottom-style",
778 GTK_TYPE_BORDER_STYLE,
779 GTK_TYPE_BORDER_STYLE,
780 GTK_TYPE_BORDER_STYLE,
785 GTK_BORDER_STYLE_NONE);
786 gtk_style_property_register ("border-bottom-width",
793 compute_border_width,
795 gtk_style_property_register ("border-right-style",
796 GTK_TYPE_BORDER_STYLE,
797 GTK_TYPE_BORDER_STYLE,
798 GTK_TYPE_BORDER_STYLE,
803 GTK_BORDER_STYLE_NONE);
804 gtk_style_property_register ("border-right-width",
811 compute_border_width,
814 gtk_style_property_register ("border-top-left-radius",
815 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
816 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
817 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
819 border_corner_radius_value_parse,
820 border_corner_radius_value_print,
823 gtk_style_property_register ("border-top-right-radius",
824 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
825 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
826 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
828 border_corner_radius_value_parse,
829 border_corner_radius_value_print,
832 gtk_style_property_register ("border-bottom-right-radius",
833 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
834 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
835 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
837 border_corner_radius_value_parse,
838 border_corner_radius_value_print,
841 gtk_style_property_register ("border-bottom-left-radius",
842 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
843 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
844 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
846 border_corner_radius_value_parse,
847 border_corner_radius_value_print,
851 gtk_style_property_register ("outline-style",
852 GTK_TYPE_BORDER_STYLE,
853 GTK_TYPE_BORDER_STYLE,
854 GTK_TYPE_BORDER_STYLE,
859 GTK_BORDER_STYLE_NONE);
860 gtk_style_property_register ("outline-width",
867 compute_border_width,
869 gtk_style_property_register ("outline-offset",
879 gtk_style_property_register ("background-clip",
887 GTK_CSS_AREA_BORDER_BOX);
889 gtk_style_property_register ("background-origin",
897 GTK_CSS_AREA_PADDING_BOX);
899 gtk_style_property_register ("border-top-color",
900 GTK_TYPE_SYMBOLIC_COLOR,
907 _gtk_symbolic_color_get_current_color ());
908 gtk_style_property_register ("border-right-color",
909 GTK_TYPE_SYMBOLIC_COLOR,
916 _gtk_symbolic_color_get_current_color ());
917 gtk_style_property_register ("border-bottom-color",
918 GTK_TYPE_SYMBOLIC_COLOR,
925 _gtk_symbolic_color_get_current_color ());
926 gtk_style_property_register ("border-left-color",
927 GTK_TYPE_SYMBOLIC_COLOR,
934 _gtk_symbolic_color_get_current_color ());
935 gtk_style_property_register ("outline-color",
936 GTK_TYPE_SYMBOLIC_COLOR,
943 _gtk_symbolic_color_get_current_color ());
945 gtk_style_property_register ("background-repeat",
946 GTK_TYPE_CSS_BACKGROUND_REPEAT,
947 GTK_TYPE_CSS_BACKGROUND_REPEAT,
948 GTK_TYPE_CSS_BACKGROUND_REPEAT,
950 background_repeat_value_parse,
951 background_repeat_value_print,
953 GTK_CSS_BACKGROUND_REPEAT | (GTK_CSS_BACKGROUND_REPEAT << GTK_CSS_BACKGROUND_REPEAT_SHIFT));
954 gtk_style_property_register ("background-image",
957 CAIRO_GOBJECT_TYPE_PATTERN,
959 css_image_value_parse,
960 css_image_value_print,
961 css_image_value_compute,
964 gtk_style_property_register ("border-image-source",
967 CAIRO_GOBJECT_TYPE_PATTERN,
969 css_image_value_parse,
970 css_image_value_print,
971 css_image_value_compute,
973 gtk_style_property_register ("border-image-repeat",
974 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
975 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
976 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
981 &border_image_repeat);
983 /* XXX: The initial value is wrong, it should be 100% */
984 gtk_style_property_register ("border-image-slice",
993 gtk_style_property_register ("border-image-width",
1002 gtk_style_property_register ("engine",
1003 GTK_TYPE_THEMING_ENGINE,
1004 GTK_TYPE_THEMING_ENGINE,
1005 GTK_TYPE_THEMING_ENGINE,
1010 gtk_theming_engine_load (NULL));
1011 gtk_style_property_register ("transition",
1012 GTK_TYPE_ANIMATION_DESCRIPTION,
1013 GTK_TYPE_ANIMATION_DESCRIPTION,
1014 GTK_TYPE_ANIMATION_DESCRIPTION,
1021 /* Private property holding the binding sets */
1022 gtk_style_property_register ("gtk-key-bindings",
1027 bindings_value_parse,
1028 bindings_value_print,