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>
29 #include "gtkcssparserprivate.h"
30 #include "gtkcssstylefuncsprivate.h"
31 #include "gtkcssstylepropertyprivate.h"
32 #include "gtkcsstypesprivate.h"
34 #include "gtkprivatetypebuiltins.h"
35 #include "gtkstylepropertiesprivate.h"
37 /* this is in case round() is not provided by the compiler,
38 * such as in the case of C89 compilers, like MSVC
40 #include "fallback-c89.c"
42 /* the actual parsers we have */
43 #include "gtkanimationdescription.h"
44 #include "gtkbindings.h"
45 #include "gtkcssimageprivate.h"
46 #include "gtkgradient.h"
47 #include "gtkshadowprivate.h"
48 #include "gtksymboliccolorprivate.h"
49 #include "gtkthemingengine.h"
50 #include "gtktypebuiltins.h"
51 #include "gtkwin32themeprivate.h"
53 /*** REGISTRATION ***/
56 gtk_css_style_property_register (const char * name,
60 GtkStylePropertyFlags flags,
61 GtkCssStylePropertyParseFunc parse_value,
62 GtkCssStylePropertyPrintFunc print_value,
63 GtkCssStylePropertyComputeFunc compute_value,
66 GtkCssStyleProperty *node;
67 GValue initial_value = G_VALUE_INIT;
71 va_start (args, compute_value);
72 G_VALUE_COLLECT_INIT (&initial_value, specified_type,
76 g_error ("property `%s' initial value is broken: %s", name, error);
77 g_value_unset (&initial_value);
83 node = g_object_new (GTK_TYPE_CSS_STYLE_PROPERTY,
84 "value-type", value_type,
85 "computed-type", computed_type,
86 "inherit", (flags & GTK_STYLE_PROPERTY_INHERIT) ? TRUE : FALSE,
87 "initial-value", &initial_value,
92 node->parse_value = parse_value;
94 node->print_value = print_value;
96 node->compute_value = compute_value;
98 g_value_unset (&initial_value);
104 string_append_double (GString *string,
107 char buf[G_ASCII_DTOSTR_BUF_SIZE];
109 g_ascii_dtostr (buf, sizeof (buf), d);
110 g_string_append (string, buf);
114 string_append_string (GString *str,
119 g_string_append_c (str, '"');
122 len = strcspn (string, "\"\n\r\f");
123 g_string_append (str, string);
130 g_string_append (str, "\\A ");
133 g_string_append (str, "\\D ");
136 g_string_append (str, "\\C ");
139 g_string_append (str, "\\\"");
142 g_assert_not_reached ();
147 g_string_append_c (str, '"');
150 /*** IMPLEMENTATIONS ***/
153 color_compute (GtkCssStyleProperty *property,
155 GtkStyleContext *context,
156 const GValue *specified)
158 GtkSymbolicColor *symbolic = g_value_get_boxed (specified);
161 if (symbolic == _gtk_symbolic_color_get_current_color ())
163 /* The computed value of the ‘currentColor’ keyword is the computed
164 * value of the ‘color’ property. If the ‘currentColor’ keyword is
165 * set on the ‘color’ property itself, it is treated as ‘color: inherit’.
167 if (g_str_equal (_gtk_style_property_get_name (GTK_STYLE_PROPERTY (property)), "color"))
169 GtkStyleContext *parent = gtk_style_context_get_parent (context);
172 g_value_copy (_gtk_style_context_peek_property (parent, "color"), computed);
174 _gtk_css_style_compute_value (computed,
176 _gtk_css_style_property_get_initial_value (property));
180 g_value_copy (_gtk_style_context_peek_property (context, "color"), computed);
183 else if (_gtk_style_context_resolve_color (context,
187 g_value_set_boxed (computed, &rgba);
191 color_compute (property,
194 _gtk_css_style_property_get_initial_value (property));
199 font_family_parse (GtkCssStyleProperty *property,
201 GtkCssParser *parser,
207 /* We don't special case generic families. Pango should do
210 names = g_ptr_array_new ();
213 name = _gtk_css_parser_try_ident (parser, TRUE);
216 GString *string = g_string_new (name);
218 while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
220 g_string_append_c (string, ' ');
221 g_string_append (string, name);
224 name = g_string_free (string, FALSE);
228 name = _gtk_css_parser_read_string (parser);
231 g_ptr_array_free (names, TRUE);
236 g_ptr_array_add (names, name);
237 } while (_gtk_css_parser_try (parser, ",", TRUE));
239 /* NULL-terminate array */
240 g_ptr_array_add (names, NULL);
241 g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
246 font_family_value_print (GtkCssStyleProperty *property,
250 const char **names = g_value_get_boxed (value);
252 if (names == NULL || *names == NULL)
254 g_string_append (string, "none");
258 string_append_string (string, *names);
262 g_string_append (string, ", ");
263 string_append_string (string, *names);
269 bindings_value_parse (GtkCssStyleProperty *property,
271 GtkCssParser *parser,
275 GtkBindingSet *binding_set;
278 array = g_ptr_array_new ();
281 name = _gtk_css_parser_try_ident (parser, TRUE);
284 _gtk_css_parser_error (parser, "Not a valid binding name");
285 g_ptr_array_free (array, TRUE);
289 binding_set = gtk_binding_set_find (name);
293 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
298 g_ptr_array_add (array, binding_set);
301 while (_gtk_css_parser_try (parser, ",", TRUE));
303 g_value_take_boxed (value, array);
309 bindings_value_print (GtkCssStyleProperty *property,
316 array = g_value_get_boxed (value);
318 for (i = 0; i < array->len; i++)
320 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
323 g_string_append (string, ", ");
324 g_string_append (string, binding_set->set_name);
329 border_corner_radius_value_parse (GtkCssStyleProperty *property,
331 GtkCssParser *parser,
334 GtkCssBorderCornerRadius corner;
336 if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
338 _gtk_css_parser_error (parser, "Expected a number");
341 else if (corner.horizontal < 0)
344 if (!_gtk_css_parser_try_double (parser, &corner.vertical))
345 corner.vertical = corner.horizontal;
346 else if (corner.vertical < 0)
349 g_value_set_boxed (value, &corner);
353 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
358 border_corner_radius_value_print (GtkCssStyleProperty *property,
362 GtkCssBorderCornerRadius *corner;
364 corner = g_value_get_boxed (value);
368 g_string_append (string, "none");
372 string_append_double (string, corner->horizontal);
373 if (corner->horizontal != corner->vertical)
375 g_string_append_c (string, ' ');
376 string_append_double (string, corner->vertical);
381 css_image_value_parse (GtkCssStyleProperty *property,
383 GtkCssParser *parser,
388 if (_gtk_css_parser_try (parser, "none", TRUE))
392 image = _gtk_css_image_new_parse (parser, base);
397 g_value_take_object (value, image);
402 css_image_value_print (GtkCssStyleProperty *property,
406 GtkCssImage *image = g_value_get_object (value);
409 _gtk_css_image_print (image, string);
411 g_string_append (string, "none");
415 css_image_value_compute (GtkCssStyleProperty *property,
417 GtkStyleContext *context,
418 const GValue *specified)
420 GtkCssImage *image = g_value_get_object (specified);
423 image = _gtk_css_image_compute (image, context);
425 g_value_take_object (computed, image);
429 parse_border_width (GtkCssStyleProperty *property,
431 GtkCssParser *parser,
436 if (!_gtk_css_parser_read_number (parser,
438 GTK_CSS_POSITIVE_ONLY
439 | GTK_CSS_NUMBER_AS_PIXELS
440 | GTK_CSS_PARSE_LENGTH))
443 g_value_set_boxed (value, &number);
448 compute_border_width (GtkCssStyleProperty *property,
450 GtkStyleContext *context,
451 const GValue *specified)
453 GtkCssStyleProperty *style;
454 GtkBorderStyle border_style;
457 /* The -1 is magic that is only true because we register the style
458 * properties directly after the width properties.
460 style = _gtk_css_style_property_lookup_by_id (_gtk_css_style_property_get_id (property) - 1);
461 border_style = g_value_get_enum (_gtk_style_context_peek_property (context, _gtk_style_property_get_name (GTK_STYLE_PROPERTY (style))));
463 if (border_style == GTK_BORDER_STYLE_NONE ||
464 border_style == GTK_BORDER_STYLE_HIDDEN)
466 g_value_set_int (computed, 0);
470 _gtk_css_number_compute (&number,
471 g_value_get_boxed (specified),
473 g_value_set_int (computed, round (number.value));
477 background_repeat_value_parse (GtkCssStyleProperty *property,
479 GtkCssParser *parser,
482 int repeat, vertical;
484 if (!_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &repeat))
486 _gtk_css_parser_error (parser, "Not a valid value");
490 if (repeat <= GTK_CSS_BACKGROUND_REPEAT_MASK)
492 if (_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &vertical))
494 if (vertical >= GTK_CSS_BACKGROUND_REPEAT_MASK)
496 _gtk_css_parser_error (parser, "Not a valid 2nd value");
500 repeat |= vertical << GTK_CSS_BACKGROUND_REPEAT_SHIFT;
503 repeat |= repeat << GTK_CSS_BACKGROUND_REPEAT_SHIFT;
506 g_value_set_enum (value, repeat);
511 background_repeat_value_print (GtkCssStyleProperty *property,
515 GEnumClass *enum_class;
516 GEnumValue *enum_value;
517 GtkCssBackgroundRepeat repeat;
519 repeat = g_value_get_enum (value);
520 enum_class = g_type_class_ref (GTK_TYPE_CSS_BACKGROUND_REPEAT);
521 enum_value = g_enum_get_value (enum_class, repeat);
523 /* only triggers for 'repeat-x' and 'repeat-y' */
525 g_string_append (string, enum_value->value_nick);
528 enum_value = g_enum_get_value (enum_class, GTK_CSS_BACKGROUND_HORIZONTAL (repeat));
529 g_string_append (string, enum_value->value_nick);
531 if (GTK_CSS_BACKGROUND_HORIZONTAL (repeat) != GTK_CSS_BACKGROUND_VERTICAL (repeat))
533 enum_value = g_enum_get_value (enum_class, GTK_CSS_BACKGROUND_VERTICAL (repeat));
534 g_string_append (string, " ");
535 g_string_append (string, enum_value->value_nick);
539 g_type_class_unref (enum_class);
542 /*** REGISTRATION ***/
544 static GtkSymbolicColor *
545 gtk_symbolic_color_new_rgba (double red,
550 GdkRGBA rgba = { red, green, blue, alpha };
552 return gtk_symbolic_color_new_literal (&rgba);
556 _gtk_css_style_property_init_properties (void)
558 char *default_font_family[] = { "Sans", NULL };
560 GtkSymbolicColor *symbolic;
561 GtkCssBorderCornerRadius no_corner_radius = { 0, };
562 GtkBorder border_of_ones = { 1, 1, 1, 1 };
563 GtkCssBorderImageRepeat border_image_repeat = { GTK_CSS_REPEAT_STYLE_STRETCH, GTK_CSS_REPEAT_STYLE_STRETCH };
565 /* Initialize "color" and "font-size" first,
566 * so that when computing values later they are
567 * done first. That way, 'currentColor' and font
568 * sizes in em can be looked up properly */
569 symbolic = gtk_symbolic_color_new_rgba (1, 1, 1, 1);
570 gtk_css_style_property_register ("color",
571 GTK_TYPE_SYMBOLIC_COLOR,
574 GTK_STYLE_PROPERTY_INHERIT,
579 gtk_symbolic_color_unref (symbolic);
580 gtk_css_style_property_register ("font-size",
584 GTK_STYLE_PROPERTY_INHERIT,
590 /* properties that aren't referenced when computing values
592 symbolic = gtk_symbolic_color_new_rgba (0, 0, 0, 0);
593 gtk_css_style_property_register ("background-color",
594 GTK_TYPE_SYMBOLIC_COLOR,
602 gtk_symbolic_color_unref (symbolic);
604 gtk_css_style_property_register ("font-family",
608 GTK_STYLE_PROPERTY_INHERIT,
610 font_family_value_print,
612 default_font_family);
613 gtk_css_style_property_register ("font-style",
617 GTK_STYLE_PROPERTY_INHERIT,
622 gtk_css_style_property_register ("font-variant",
626 GTK_STYLE_PROPERTY_INHERIT,
630 PANGO_VARIANT_NORMAL);
631 /* xxx: need to parse this properly, ie parse the numbers */
632 gtk_css_style_property_register ("font-weight",
636 GTK_STYLE_PROPERTY_INHERIT,
640 PANGO_WEIGHT_NORMAL);
642 gtk_css_style_property_register ("text-shadow",
646 GTK_STYLE_PROPERTY_INHERIT,
652 gtk_css_style_property_register ("icon-shadow",
656 GTK_STYLE_PROPERTY_INHERIT,
662 gtk_css_style_property_register ("box-shadow",
672 gtk_css_style_property_register ("margin-top",
681 gtk_css_style_property_register ("margin-left",
690 gtk_css_style_property_register ("margin-bottom",
699 gtk_css_style_property_register ("margin-right",
708 gtk_css_style_property_register ("padding-top",
717 gtk_css_style_property_register ("padding-left",
726 gtk_css_style_property_register ("padding-bottom",
735 gtk_css_style_property_register ("padding-right",
744 _gtk_css_number_init (&number, 0, GTK_CSS_PX);
745 /* IMPORTANT: compute_border_width() requires that the border-width
746 * properties be immeditaly followed by the border-style properties
748 gtk_css_style_property_register ("border-top-style",
749 GTK_TYPE_BORDER_STYLE,
750 GTK_TYPE_BORDER_STYLE,
751 GTK_TYPE_BORDER_STYLE,
756 GTK_BORDER_STYLE_NONE);
757 gtk_css_style_property_register ("border-top-width",
764 compute_border_width,
766 gtk_css_style_property_register ("border-left-style",
767 GTK_TYPE_BORDER_STYLE,
768 GTK_TYPE_BORDER_STYLE,
769 GTK_TYPE_BORDER_STYLE,
774 GTK_BORDER_STYLE_NONE);
775 gtk_css_style_property_register ("border-left-width",
782 compute_border_width,
784 gtk_css_style_property_register ("border-bottom-style",
785 GTK_TYPE_BORDER_STYLE,
786 GTK_TYPE_BORDER_STYLE,
787 GTK_TYPE_BORDER_STYLE,
792 GTK_BORDER_STYLE_NONE);
793 gtk_css_style_property_register ("border-bottom-width",
800 compute_border_width,
802 gtk_css_style_property_register ("border-right-style",
803 GTK_TYPE_BORDER_STYLE,
804 GTK_TYPE_BORDER_STYLE,
805 GTK_TYPE_BORDER_STYLE,
810 GTK_BORDER_STYLE_NONE);
811 gtk_css_style_property_register ("border-right-width",
818 compute_border_width,
821 gtk_css_style_property_register ("border-top-left-radius",
822 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
823 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
824 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
826 border_corner_radius_value_parse,
827 border_corner_radius_value_print,
830 gtk_css_style_property_register ("border-top-right-radius",
831 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
832 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
833 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
835 border_corner_radius_value_parse,
836 border_corner_radius_value_print,
839 gtk_css_style_property_register ("border-bottom-right-radius",
840 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
841 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
842 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
844 border_corner_radius_value_parse,
845 border_corner_radius_value_print,
848 gtk_css_style_property_register ("border-bottom-left-radius",
849 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
850 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
851 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
853 border_corner_radius_value_parse,
854 border_corner_radius_value_print,
858 gtk_css_style_property_register ("outline-style",
859 GTK_TYPE_BORDER_STYLE,
860 GTK_TYPE_BORDER_STYLE,
861 GTK_TYPE_BORDER_STYLE,
866 GTK_BORDER_STYLE_NONE);
867 gtk_css_style_property_register ("outline-width",
874 compute_border_width,
876 gtk_css_style_property_register ("outline-offset",
886 gtk_css_style_property_register ("background-clip",
894 GTK_CSS_AREA_BORDER_BOX);
896 gtk_css_style_property_register ("background-origin",
904 GTK_CSS_AREA_PADDING_BOX);
906 gtk_css_style_property_register ("border-top-color",
907 GTK_TYPE_SYMBOLIC_COLOR,
914 _gtk_symbolic_color_get_current_color ());
915 gtk_css_style_property_register ("border-right-color",
916 GTK_TYPE_SYMBOLIC_COLOR,
923 _gtk_symbolic_color_get_current_color ());
924 gtk_css_style_property_register ("border-bottom-color",
925 GTK_TYPE_SYMBOLIC_COLOR,
932 _gtk_symbolic_color_get_current_color ());
933 gtk_css_style_property_register ("border-left-color",
934 GTK_TYPE_SYMBOLIC_COLOR,
941 _gtk_symbolic_color_get_current_color ());
942 gtk_css_style_property_register ("outline-color",
943 GTK_TYPE_SYMBOLIC_COLOR,
950 _gtk_symbolic_color_get_current_color ());
952 gtk_css_style_property_register ("background-repeat",
953 GTK_TYPE_CSS_BACKGROUND_REPEAT,
954 GTK_TYPE_CSS_BACKGROUND_REPEAT,
955 GTK_TYPE_CSS_BACKGROUND_REPEAT,
957 background_repeat_value_parse,
958 background_repeat_value_print,
960 GTK_CSS_BACKGROUND_REPEAT | (GTK_CSS_BACKGROUND_REPEAT << GTK_CSS_BACKGROUND_REPEAT_SHIFT));
961 gtk_css_style_property_register ("background-image",
964 CAIRO_GOBJECT_TYPE_PATTERN,
966 css_image_value_parse,
967 css_image_value_print,
968 css_image_value_compute,
971 gtk_css_style_property_register ("border-image-source",
974 CAIRO_GOBJECT_TYPE_PATTERN,
976 css_image_value_parse,
977 css_image_value_print,
978 css_image_value_compute,
980 gtk_css_style_property_register ("border-image-repeat",
981 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
982 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
983 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
988 &border_image_repeat);
990 /* XXX: The initial value is wrong, it should be 100% */
991 gtk_css_style_property_register ("border-image-slice",
1000 gtk_css_style_property_register ("border-image-width",
1009 gtk_css_style_property_register ("engine",
1010 GTK_TYPE_THEMING_ENGINE,
1011 GTK_TYPE_THEMING_ENGINE,
1012 GTK_TYPE_THEMING_ENGINE,
1017 gtk_theming_engine_load (NULL));
1018 gtk_css_style_property_register ("transition",
1019 GTK_TYPE_ANIMATION_DESCRIPTION,
1020 GTK_TYPE_ANIMATION_DESCRIPTION,
1021 GTK_TYPE_ANIMATION_DESCRIPTION,
1028 /* Private property holding the binding sets */
1029 gtk_css_style_property_register ("gtk-key-bindings",
1034 bindings_value_parse,
1035 bindings_value_print,