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 _gtk_style_property_register (const char * name,
53 GtkStylePropertyFlags flags,
54 GtkCssStylePropertyParseFunc parse_value,
55 GtkCssStylePropertyPrintFunc print_value,
56 GtkCssStylePropertyComputeFunc compute_value,
57 const GValue * initial_value)
59 GtkCssStyleProperty *node;
61 node = g_object_new (GTK_TYPE_CSS_STYLE_PROPERTY,
62 "value-type", value_type,
63 "computed-type", computed_type,
64 "inherit", (flags & GTK_STYLE_PROPERTY_INHERIT) ? TRUE : FALSE,
65 "initial-value", initial_value,
70 node->parse_value = parse_value;
72 node->print_value = print_value;
74 node->compute_value = compute_value;
78 gtk_style_property_register (const char * name,
82 GtkStylePropertyFlags flags,
83 GtkCssStylePropertyParseFunc parse_value,
84 GtkCssStylePropertyPrintFunc print_value,
85 GtkCssStylePropertyComputeFunc compute_value,
88 GValue initial_value = G_VALUE_INIT;
92 va_start (args, compute_value);
93 G_VALUE_COLLECT_INIT (&initial_value, specified_type,
97 g_error ("property `%s' initial value is broken: %s", name, error);
98 g_value_unset (&initial_value);
104 _gtk_style_property_register (name,
113 g_value_unset (&initial_value);
119 string_append_double (GString *string,
122 char buf[G_ASCII_DTOSTR_BUF_SIZE];
124 g_ascii_dtostr (buf, sizeof (buf), d);
125 g_string_append (string, buf);
129 string_append_string (GString *str,
134 g_string_append_c (str, '"');
137 len = strcspn (string, "\"\n\r\f");
138 g_string_append (str, string);
145 g_string_append (str, "\\A ");
148 g_string_append (str, "\\D ");
151 g_string_append (str, "\\C ");
154 g_string_append (str, "\\\"");
157 g_assert_not_reached ();
162 g_string_append_c (str, '"');
165 /*** IMPLEMENTATIONS ***/
168 color_compute (GtkCssStyleProperty *property,
170 GtkStyleContext *context,
171 const GValue *specified)
173 GtkSymbolicColor *symbolic = g_value_get_boxed (specified);
176 if (symbolic == _gtk_symbolic_color_get_current_color ())
178 /* The computed value of the ‘currentColor’ keyword is the computed
179 * value of the ‘color’ property. If the ‘currentColor’ keyword is
180 * set on the ‘color’ property itself, it is treated as ‘color: inherit’.
182 if (g_str_equal (_gtk_style_property_get_name (GTK_STYLE_PROPERTY (property)), "color"))
184 GtkStyleContext *parent = gtk_style_context_get_parent (context);
187 g_value_copy (_gtk_style_context_peek_property (parent, "color"), computed);
189 _gtk_css_style_compute_value (computed,
191 _gtk_css_style_property_get_initial_value (property));
195 g_value_copy (_gtk_style_context_peek_property (context, "color"), computed);
198 else if (_gtk_style_context_resolve_color (context,
202 g_value_set_boxed (computed, &rgba);
206 color_compute (property,
209 _gtk_css_style_property_get_initial_value (property));
214 font_family_parse (GtkCssStyleProperty *property,
216 GtkCssParser *parser,
222 /* We don't special case generic families. Pango should do
225 names = g_ptr_array_new ();
228 name = _gtk_css_parser_try_ident (parser, TRUE);
231 GString *string = g_string_new (name);
233 while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
235 g_string_append_c (string, ' ');
236 g_string_append (string, name);
239 name = g_string_free (string, FALSE);
243 name = _gtk_css_parser_read_string (parser);
246 g_ptr_array_free (names, TRUE);
251 g_ptr_array_add (names, name);
252 } while (_gtk_css_parser_try (parser, ",", TRUE));
254 /* NULL-terminate array */
255 g_ptr_array_add (names, NULL);
256 g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
261 font_family_value_print (GtkCssStyleProperty *property,
265 const char **names = g_value_get_boxed (value);
267 if (names == NULL || *names == NULL)
269 g_string_append (string, "none");
273 string_append_string (string, *names);
277 g_string_append (string, ", ");
278 string_append_string (string, *names);
284 bindings_value_parse (GtkCssStyleProperty *property,
286 GtkCssParser *parser,
290 GtkBindingSet *binding_set;
293 array = g_ptr_array_new ();
296 name = _gtk_css_parser_try_ident (parser, TRUE);
299 _gtk_css_parser_error (parser, "Not a valid binding name");
300 g_ptr_array_free (array, TRUE);
304 binding_set = gtk_binding_set_find (name);
308 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
313 g_ptr_array_add (array, binding_set);
316 while (_gtk_css_parser_try (parser, ",", TRUE));
318 g_value_take_boxed (value, array);
324 bindings_value_print (GtkCssStyleProperty *property,
331 array = g_value_get_boxed (value);
333 for (i = 0; i < array->len; i++)
335 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
338 g_string_append (string, ", ");
339 g_string_append (string, binding_set->set_name);
344 border_corner_radius_value_parse (GtkCssStyleProperty *property,
346 GtkCssParser *parser,
349 GtkCssBorderCornerRadius corner;
351 if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
353 _gtk_css_parser_error (parser, "Expected a number");
356 else if (corner.horizontal < 0)
359 if (!_gtk_css_parser_try_double (parser, &corner.vertical))
360 corner.vertical = corner.horizontal;
361 else if (corner.vertical < 0)
364 g_value_set_boxed (value, &corner);
368 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
373 border_corner_radius_value_print (GtkCssStyleProperty *property,
377 GtkCssBorderCornerRadius *corner;
379 corner = g_value_get_boxed (value);
383 g_string_append (string, "none");
387 string_append_double (string, corner->horizontal);
388 if (corner->horizontal != corner->vertical)
390 g_string_append_c (string, ' ');
391 string_append_double (string, corner->vertical);
396 css_image_value_parse (GtkCssStyleProperty *property,
398 GtkCssParser *parser,
403 if (_gtk_css_parser_try (parser, "none", TRUE))
407 image = _gtk_css_image_new_parse (parser, base);
412 g_value_take_object (value, image);
417 css_image_value_print (GtkCssStyleProperty *property,
421 GtkCssImage *image = g_value_get_object (value);
424 _gtk_css_image_print (image, string);
426 g_string_append (string, "none");
430 css_image_value_compute (GtkCssStyleProperty *property,
432 GtkStyleContext *context,
433 const GValue *specified)
435 GtkCssImage *image = g_value_get_object (specified);
438 image = _gtk_css_image_compute (image, context);
440 g_value_take_object (computed, image);
444 compute_border_width (GtkCssStyleProperty *property,
446 GtkStyleContext *context,
447 const GValue *specified)
449 GtkCssStyleProperty *style;
450 GtkBorderStyle border_style;
452 /* The -1 is magic that is only true because we register the style
453 * properties directly after the width properties.
455 style = _gtk_css_style_property_lookup_by_id (_gtk_css_style_property_get_id (property) - 1);
456 border_style = g_value_get_enum (_gtk_style_context_peek_property (context, _gtk_style_property_get_name (GTK_STYLE_PROPERTY (style))));
458 if (border_style == GTK_BORDER_STYLE_NONE ||
459 border_style == GTK_BORDER_STYLE_HIDDEN)
460 g_value_set_int (computed, 0);
462 g_value_copy (specified, computed);
466 background_repeat_value_parse (GtkCssStyleProperty *property,
468 GtkCssParser *parser,
471 int repeat, vertical;
473 if (!_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &repeat))
475 _gtk_css_parser_error (parser, "Not a valid value");
479 if (repeat <= GTK_CSS_BACKGROUND_REPEAT_MASK)
481 if (_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &vertical))
483 if (vertical >= GTK_CSS_BACKGROUND_REPEAT_MASK)
485 _gtk_css_parser_error (parser, "Not a valid 2nd value");
489 repeat |= vertical << GTK_CSS_BACKGROUND_REPEAT_SHIFT;
492 repeat |= repeat << GTK_CSS_BACKGROUND_REPEAT_SHIFT;
495 g_value_set_enum (value, repeat);
500 background_repeat_value_print (GtkCssStyleProperty *property,
504 GEnumClass *enum_class;
505 GEnumValue *enum_value;
506 GtkCssBackgroundRepeat repeat;
508 repeat = g_value_get_enum (value);
509 enum_class = g_type_class_ref (GTK_TYPE_CSS_BACKGROUND_REPEAT);
510 enum_value = g_enum_get_value (enum_class, repeat);
512 /* only triggers for 'repeat-x' and 'repeat-y' */
514 g_string_append (string, enum_value->value_nick);
517 enum_value = g_enum_get_value (enum_class, GTK_CSS_BACKGROUND_HORIZONTAL (repeat));
518 g_string_append (string, enum_value->value_nick);
520 if (GTK_CSS_BACKGROUND_HORIZONTAL (repeat) != GTK_CSS_BACKGROUND_VERTICAL (repeat))
522 enum_value = g_enum_get_value (enum_class, GTK_CSS_BACKGROUND_VERTICAL (repeat));
523 g_string_append (string, " ");
524 g_string_append (string, enum_value->value_nick);
528 g_type_class_unref (enum_class);
531 /*** REGISTRATION ***/
533 static GtkSymbolicColor *
534 gtk_symbolic_color_new_rgba (double red,
539 GdkRGBA rgba = { red, green, blue, alpha };
541 return gtk_symbolic_color_new_literal (&rgba);
545 _gtk_css_style_property_init_properties (void)
547 char *default_font_family[] = { "Sans", NULL };
548 GtkSymbolicColor *symbolic;
549 GtkCssBorderCornerRadius no_corner_radius = { 0, };
550 GtkBorder border_of_ones = { 1, 1, 1, 1 };
551 GtkCssBorderImageRepeat border_image_repeat = { GTK_CSS_REPEAT_STYLE_STRETCH, GTK_CSS_REPEAT_STYLE_STRETCH };
553 /* Initialize "color" and "font-size" first,
554 * so that when computing values later they are
555 * done first. That way, 'currentColor' and font
556 * sizes in em can be looked up properly */
557 symbolic = gtk_symbolic_color_new_rgba (1, 1, 1, 1);
558 gtk_style_property_register ("color",
559 GTK_TYPE_SYMBOLIC_COLOR,
562 GTK_STYLE_PROPERTY_INHERIT,
567 gtk_symbolic_color_unref (symbolic);
568 gtk_style_property_register ("font-size",
572 GTK_STYLE_PROPERTY_INHERIT,
578 /* properties that aren't referenced when computing values
580 symbolic = gtk_symbolic_color_new_rgba (0, 0, 0, 0);
581 gtk_style_property_register ("background-color",
582 GTK_TYPE_SYMBOLIC_COLOR,
590 gtk_symbolic_color_unref (symbolic);
592 gtk_style_property_register ("font-family",
596 GTK_STYLE_PROPERTY_INHERIT,
598 font_family_value_print,
600 default_font_family);
601 gtk_style_property_register ("font-style",
605 GTK_STYLE_PROPERTY_INHERIT,
610 gtk_style_property_register ("font-variant",
614 GTK_STYLE_PROPERTY_INHERIT,
618 PANGO_VARIANT_NORMAL);
619 /* xxx: need to parse this properly, ie parse the numbers */
620 gtk_style_property_register ("font-weight",
624 GTK_STYLE_PROPERTY_INHERIT,
628 PANGO_WEIGHT_NORMAL);
630 gtk_style_property_register ("text-shadow",
634 GTK_STYLE_PROPERTY_INHERIT,
640 gtk_style_property_register ("icon-shadow",
644 GTK_STYLE_PROPERTY_INHERIT,
650 gtk_style_property_register ("box-shadow",
660 gtk_style_property_register ("margin-top",
669 gtk_style_property_register ("margin-left",
678 gtk_style_property_register ("margin-bottom",
687 gtk_style_property_register ("margin-right",
696 gtk_style_property_register ("padding-top",
705 gtk_style_property_register ("padding-left",
714 gtk_style_property_register ("padding-bottom",
723 gtk_style_property_register ("padding-right",
732 /* IMPORTANT: compute_border_width() requires that the border-width
733 * properties be immeditaly followed by the border-style properties
735 gtk_style_property_register ("border-top-style",
736 GTK_TYPE_BORDER_STYLE,
737 GTK_TYPE_BORDER_STYLE,
738 GTK_TYPE_BORDER_STYLE,
743 GTK_BORDER_STYLE_NONE);
744 gtk_style_property_register ("border-top-width",
751 compute_border_width,
753 gtk_style_property_register ("border-left-style",
754 GTK_TYPE_BORDER_STYLE,
755 GTK_TYPE_BORDER_STYLE,
756 GTK_TYPE_BORDER_STYLE,
761 GTK_BORDER_STYLE_NONE);
762 gtk_style_property_register ("border-left-width",
769 compute_border_width,
771 gtk_style_property_register ("border-bottom-style",
772 GTK_TYPE_BORDER_STYLE,
773 GTK_TYPE_BORDER_STYLE,
774 GTK_TYPE_BORDER_STYLE,
779 GTK_BORDER_STYLE_NONE);
780 gtk_style_property_register ("border-bottom-width",
787 compute_border_width,
789 gtk_style_property_register ("border-right-style",
790 GTK_TYPE_BORDER_STYLE,
791 GTK_TYPE_BORDER_STYLE,
792 GTK_TYPE_BORDER_STYLE,
797 GTK_BORDER_STYLE_NONE);
798 gtk_style_property_register ("border-right-width",
805 compute_border_width,
808 gtk_style_property_register ("border-top-left-radius",
809 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
810 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
811 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
813 border_corner_radius_value_parse,
814 border_corner_radius_value_print,
817 gtk_style_property_register ("border-top-right-radius",
818 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
819 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
820 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
822 border_corner_radius_value_parse,
823 border_corner_radius_value_print,
826 gtk_style_property_register ("border-bottom-right-radius",
827 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
828 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
829 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
831 border_corner_radius_value_parse,
832 border_corner_radius_value_print,
835 gtk_style_property_register ("border-bottom-left-radius",
836 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
837 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
838 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
840 border_corner_radius_value_parse,
841 border_corner_radius_value_print,
845 gtk_style_property_register ("outline-style",
846 GTK_TYPE_BORDER_STYLE,
847 GTK_TYPE_BORDER_STYLE,
848 GTK_TYPE_BORDER_STYLE,
853 GTK_BORDER_STYLE_NONE);
854 gtk_style_property_register ("outline-width",
861 compute_border_width,
863 gtk_style_property_register ("outline-offset",
873 gtk_style_property_register ("background-clip",
881 GTK_CSS_AREA_BORDER_BOX);
883 gtk_style_property_register ("background-origin",
891 GTK_CSS_AREA_PADDING_BOX);
893 gtk_style_property_register ("border-top-color",
894 GTK_TYPE_SYMBOLIC_COLOR,
901 _gtk_symbolic_color_get_current_color ());
902 gtk_style_property_register ("border-right-color",
903 GTK_TYPE_SYMBOLIC_COLOR,
910 _gtk_symbolic_color_get_current_color ());
911 gtk_style_property_register ("border-bottom-color",
912 GTK_TYPE_SYMBOLIC_COLOR,
919 _gtk_symbolic_color_get_current_color ());
920 gtk_style_property_register ("border-left-color",
921 GTK_TYPE_SYMBOLIC_COLOR,
928 _gtk_symbolic_color_get_current_color ());
929 gtk_style_property_register ("outline-color",
930 GTK_TYPE_SYMBOLIC_COLOR,
937 _gtk_symbolic_color_get_current_color ());
939 gtk_style_property_register ("background-repeat",
940 GTK_TYPE_CSS_BACKGROUND_REPEAT,
941 GTK_TYPE_CSS_BACKGROUND_REPEAT,
942 GTK_TYPE_CSS_BACKGROUND_REPEAT,
944 background_repeat_value_parse,
945 background_repeat_value_print,
947 GTK_CSS_BACKGROUND_REPEAT | (GTK_CSS_BACKGROUND_REPEAT << GTK_CSS_BACKGROUND_REPEAT_SHIFT));
948 gtk_style_property_register ("background-image",
951 CAIRO_GOBJECT_TYPE_PATTERN,
953 css_image_value_parse,
954 css_image_value_print,
955 css_image_value_compute,
958 gtk_style_property_register ("border-image-source",
961 CAIRO_GOBJECT_TYPE_PATTERN,
963 css_image_value_parse,
964 css_image_value_print,
965 css_image_value_compute,
967 gtk_style_property_register ("border-image-repeat",
968 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
969 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
970 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
975 &border_image_repeat);
977 /* XXX: The initial value is wrong, it should be 100% */
978 gtk_style_property_register ("border-image-slice",
987 gtk_style_property_register ("border-image-width",
996 gtk_style_property_register ("engine",
997 GTK_TYPE_THEMING_ENGINE,
998 GTK_TYPE_THEMING_ENGINE,
999 GTK_TYPE_THEMING_ENGINE,
1004 gtk_theming_engine_load (NULL));
1005 gtk_style_property_register ("transition",
1006 GTK_TYPE_ANIMATION_DESCRIPTION,
1007 GTK_TYPE_ANIMATION_DESCRIPTION,
1008 GTK_TYPE_ANIMATION_DESCRIPTION,
1015 /* Private property holding the binding sets */
1016 gtk_style_property_register ("gtk-key-bindings",
1021 bindings_value_parse,
1022 bindings_value_print,