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 "gtkthemingengine.h"
43 #include "gtktypebuiltins.h"
44 #include "gtkwin32themeprivate.h"
46 /*** REGISTRATION ***/
49 color_compute (GtkCssStyleProperty *property,
51 GtkStyleContext *context,
52 const GValue *specified)
54 g_value_init (computed, GDK_TYPE_RGBA);
56 /* for when resolvage fails */
59 if (G_VALUE_HOLDS (specified, GTK_TYPE_CSS_SPECIAL_VALUE))
61 GtkStyleContext *parent = gtk_style_context_get_parent (context);
62 g_assert (g_value_get_enum (specified) == GTK_CSS_CURRENT_COLOR);
65 g_value_copy (_gtk_style_context_peek_property (parent, "color"), computed);
67 _gtk_css_style_compute_value (computed,
69 _gtk_css_style_property_get_initial_value (property));
71 else if (G_VALUE_HOLDS (specified, GTK_TYPE_SYMBOLIC_COLOR))
75 if (!_gtk_style_context_resolve_color (context,
76 g_value_get_boxed (specified),
79 specified = _gtk_css_style_property_get_initial_value (property);
83 g_value_set_boxed (computed, &rgba);
86 g_value_copy (specified, computed);
90 _gtk_style_property_register (const char * name,
92 GtkStylePropertyFlags flags,
93 GtkCssStylePropertyParseFunc parse_value,
94 GtkCssStylePropertyPrintFunc print_value,
95 GtkCssStylePropertyComputeFunc compute_value,
96 const GValue * initial_value)
98 GtkCssStyleProperty *node;
100 node = g_object_new (GTK_TYPE_CSS_STYLE_PROPERTY,
101 "inherit", (flags & GTK_STYLE_PROPERTY_INHERIT) ? TRUE : FALSE,
102 "initial-value", initial_value,
104 "value-type", value_type,
108 node->parse_value = parse_value;
110 node->print_value = print_value;
112 node->compute_value = compute_value;
116 gtk_style_property_register (const char * name,
118 GtkStylePropertyFlags flags,
119 GtkCssStylePropertyParseFunc parse_value,
120 GtkCssStylePropertyPrintFunc print_value,
121 GtkCssStylePropertyComputeFunc compute_value,
124 GValue initial_value = G_VALUE_INIT;
128 va_start (args, compute_value);
129 G_VALUE_COLLECT_INIT (&initial_value, value_type,
133 g_error ("property `%s' initial value is broken: %s", name, error);
134 g_value_unset (&initial_value);
140 _gtk_style_property_register (name,
148 g_value_unset (&initial_value);
154 string_append_double (GString *string,
157 char buf[G_ASCII_DTOSTR_BUF_SIZE];
159 g_ascii_dtostr (buf, sizeof (buf), d);
160 g_string_append (string, buf);
164 string_append_string (GString *str,
169 g_string_append_c (str, '"');
172 len = strcspn (string, "\"\n\r\f");
173 g_string_append (str, string);
180 g_string_append (str, "\\A ");
183 g_string_append (str, "\\D ");
186 g_string_append (str, "\\C ");
189 g_string_append (str, "\\\"");
192 g_assert_not_reached ();
197 g_string_append_c (str, '"');
200 /*** IMPLEMENTATIONS ***/
203 font_family_parse (GtkCssStyleProperty *property,
205 GtkCssParser *parser,
211 /* We don't special case generic families. Pango should do
214 names = g_ptr_array_new ();
217 name = _gtk_css_parser_try_ident (parser, TRUE);
220 GString *string = g_string_new (name);
222 while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
224 g_string_append_c (string, ' ');
225 g_string_append (string, name);
228 name = g_string_free (string, FALSE);
232 name = _gtk_css_parser_read_string (parser);
235 g_ptr_array_free (names, TRUE);
240 g_ptr_array_add (names, name);
241 } while (_gtk_css_parser_try (parser, ",", TRUE));
243 /* NULL-terminate array */
244 g_ptr_array_add (names, NULL);
245 g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
250 font_family_value_print (GtkCssStyleProperty *property,
254 const char **names = g_value_get_boxed (value);
256 if (names == NULL || *names == NULL)
258 g_string_append (string, "none");
262 string_append_string (string, *names);
266 g_string_append (string, ", ");
267 string_append_string (string, *names);
273 bindings_value_parse (GtkCssStyleProperty *property,
275 GtkCssParser *parser,
279 GtkBindingSet *binding_set;
282 array = g_ptr_array_new ();
285 name = _gtk_css_parser_try_ident (parser, TRUE);
288 _gtk_css_parser_error (parser, "Not a valid binding name");
289 g_ptr_array_free (array, TRUE);
293 binding_set = gtk_binding_set_find (name);
297 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
302 g_ptr_array_add (array, binding_set);
305 while (_gtk_css_parser_try (parser, ",", TRUE));
307 g_value_take_boxed (value, array);
313 bindings_value_print (GtkCssStyleProperty *property,
320 array = g_value_get_boxed (value);
322 for (i = 0; i < array->len; i++)
324 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
327 g_string_append (string, ", ");
328 g_string_append (string, binding_set->set_name);
333 border_corner_radius_value_parse (GtkCssStyleProperty *property,
335 GtkCssParser *parser,
338 GtkCssBorderCornerRadius corner;
340 if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
342 _gtk_css_parser_error (parser, "Expected a number");
345 else if (corner.horizontal < 0)
348 if (!_gtk_css_parser_try_double (parser, &corner.vertical))
349 corner.vertical = corner.horizontal;
350 else if (corner.vertical < 0)
353 g_value_set_boxed (value, &corner);
357 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
362 border_corner_radius_value_print (GtkCssStyleProperty *property,
366 GtkCssBorderCornerRadius *corner;
368 corner = g_value_get_boxed (value);
372 g_string_append (string, "none");
376 string_append_double (string, corner->horizontal);
377 if (corner->horizontal != corner->vertical)
379 g_string_append_c (string, ' ');
380 string_append_double (string, corner->vertical);
385 css_image_value_parse (GtkCssStyleProperty *property,
387 GtkCssParser *parser,
392 if (_gtk_css_parser_try (parser, "none", TRUE))
396 image = _gtk_css_image_new_parse (parser, base);
401 g_value_unset (value);
402 g_value_init (value, GTK_TYPE_CSS_IMAGE);
403 g_value_take_object (value, image);
408 css_image_value_print (GtkCssStyleProperty *property,
412 GtkCssImage *image = g_value_get_object (value);
415 _gtk_css_image_print (image, string);
417 g_string_append (string, "none");
421 css_image_value_compute (GtkCssStyleProperty *property,
423 GtkStyleContext *context,
424 const GValue *specified)
426 GtkCssImage *image = g_value_get_object (specified);
429 image = _gtk_css_image_compute (image, context);
431 g_value_init (computed, GTK_TYPE_CSS_IMAGE);
432 g_value_take_object (computed, image);
436 compute_border_width (GtkCssStyleProperty *property,
438 GtkStyleContext *context,
439 const GValue *specified)
441 GtkCssStyleProperty *style;
442 GtkBorderStyle border_style;
444 /* The -1 is magic that is only true because we register the style
445 * properties directly after the width properties.
447 style = _gtk_css_style_property_lookup_by_id (_gtk_css_style_property_get_id (property) - 1);
448 border_style = g_value_get_enum (_gtk_style_context_peek_property (context, _gtk_style_property_get_name (GTK_STYLE_PROPERTY (style))));
450 g_value_init (computed, G_TYPE_INT);
451 if (border_style == GTK_BORDER_STYLE_NONE ||
452 border_style == GTK_BORDER_STYLE_HIDDEN)
453 g_value_set_int (computed, 0);
455 g_value_copy (specified, computed);
459 background_repeat_value_parse (GtkCssStyleProperty *property,
461 GtkCssParser *parser,
464 int repeat, vertical;
466 if (!_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &repeat))
468 _gtk_css_parser_error (parser, "Not a valid value");
472 if (repeat <= GTK_CSS_BACKGROUND_REPEAT_MASK)
474 if (_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &vertical))
476 if (vertical >= GTK_CSS_BACKGROUND_REPEAT_MASK)
478 _gtk_css_parser_error (parser, "Not a valid 2nd value");
482 repeat |= vertical << GTK_CSS_BACKGROUND_REPEAT_SHIFT;
485 repeat |= repeat << GTK_CSS_BACKGROUND_REPEAT_SHIFT;
488 g_value_set_enum (value, repeat);
493 background_repeat_value_print (GtkCssStyleProperty *property,
497 GEnumClass *enum_class;
498 GEnumValue *enum_value;
499 GtkCssBackgroundRepeat repeat;
501 repeat = g_value_get_enum (value);
502 enum_class = g_type_class_ref (GTK_TYPE_CSS_BACKGROUND_REPEAT);
503 enum_value = g_enum_get_value (enum_class, repeat);
505 /* only triggers for 'repeat-x' and 'repeat-y' */
507 g_string_append (string, enum_value->value_nick);
510 enum_value = g_enum_get_value (enum_class, GTK_CSS_BACKGROUND_HORIZONTAL (repeat));
511 g_string_append (string, enum_value->value_nick);
513 if (GTK_CSS_BACKGROUND_HORIZONTAL (repeat) != GTK_CSS_BACKGROUND_VERTICAL (repeat))
515 enum_value = g_enum_get_value (enum_class, GTK_CSS_BACKGROUND_VERTICAL (repeat));
516 g_string_append (string, " ");
517 g_string_append (string, enum_value->value_nick);
521 g_type_class_unref (enum_class);
524 /*** REGISTRATION ***/
526 #define rgba_init(rgba, r, g, b, a) G_STMT_START{ \
528 (rgba)->green = (g); \
529 (rgba)->blue = (b); \
530 (rgba)->alpha = (a); \
533 _gtk_css_style_property_init_properties (void)
535 GValue value = { 0, };
536 char *default_font_family[] = { "Sans", NULL };
538 GtkCssBorderCornerRadius no_corner_radius = { 0, };
539 GtkBorder border_of_ones = { 1, 1, 1, 1 };
540 GtkCssBorderImageRepeat border_image_repeat = { GTK_CSS_REPEAT_STYLE_STRETCH, GTK_CSS_REPEAT_STYLE_STRETCH };
542 /* Initialize "color" and "font-size" first,
543 * so that when computing values later they are
544 * done first. That way, 'currentColor' and font
545 * sizes in em can be looked up properly */
546 rgba_init (&rgba, 1, 1, 1, 1);
547 gtk_style_property_register ("color",
549 GTK_STYLE_PROPERTY_INHERIT,
554 gtk_style_property_register ("font-size",
556 GTK_STYLE_PROPERTY_INHERIT,
562 /* properties that aren't referenced when computing values
564 rgba_init (&rgba, 0, 0, 0, 0);
565 gtk_style_property_register ("background-color",
573 gtk_style_property_register ("font-family",
575 GTK_STYLE_PROPERTY_INHERIT,
577 font_family_value_print,
579 default_font_family);
580 gtk_style_property_register ("font-style",
582 GTK_STYLE_PROPERTY_INHERIT,
587 gtk_style_property_register ("font-variant",
589 GTK_STYLE_PROPERTY_INHERIT,
593 PANGO_VARIANT_NORMAL);
594 /* xxx: need to parse this properly, ie parse the numbers */
595 gtk_style_property_register ("font-weight",
597 GTK_STYLE_PROPERTY_INHERIT,
601 PANGO_WEIGHT_NORMAL);
603 gtk_style_property_register ("text-shadow",
605 GTK_STYLE_PROPERTY_INHERIT,
611 gtk_style_property_register ("icon-shadow",
613 GTK_STYLE_PROPERTY_INHERIT,
619 gtk_style_property_register ("box-shadow",
627 gtk_style_property_register ("margin-top",
634 gtk_style_property_register ("margin-left",
641 gtk_style_property_register ("margin-bottom",
648 gtk_style_property_register ("margin-right",
655 gtk_style_property_register ("padding-top",
662 gtk_style_property_register ("padding-left",
669 gtk_style_property_register ("padding-bottom",
676 gtk_style_property_register ("padding-right",
683 /* IMPORTANT: compute_border_width() requires that the border-width
684 * properties be immeditaly followed by the border-style properties
686 gtk_style_property_register ("border-top-style",
687 GTK_TYPE_BORDER_STYLE,
692 GTK_BORDER_STYLE_NONE);
693 gtk_style_property_register ("border-top-width",
698 compute_border_width,
700 gtk_style_property_register ("border-left-style",
701 GTK_TYPE_BORDER_STYLE,
706 GTK_BORDER_STYLE_NONE);
707 gtk_style_property_register ("border-left-width",
712 compute_border_width,
714 gtk_style_property_register ("border-bottom-style",
715 GTK_TYPE_BORDER_STYLE,
720 GTK_BORDER_STYLE_NONE);
721 gtk_style_property_register ("border-bottom-width",
726 compute_border_width,
728 gtk_style_property_register ("border-right-style",
729 GTK_TYPE_BORDER_STYLE,
734 GTK_BORDER_STYLE_NONE);
735 gtk_style_property_register ("border-right-width",
740 compute_border_width,
743 gtk_style_property_register ("border-top-left-radius",
744 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
746 border_corner_radius_value_parse,
747 border_corner_radius_value_print,
750 gtk_style_property_register ("border-top-right-radius",
751 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
753 border_corner_radius_value_parse,
754 border_corner_radius_value_print,
757 gtk_style_property_register ("border-bottom-right-radius",
758 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
760 border_corner_radius_value_parse,
761 border_corner_radius_value_print,
764 gtk_style_property_register ("border-bottom-left-radius",
765 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
767 border_corner_radius_value_parse,
768 border_corner_radius_value_print,
772 gtk_style_property_register ("background-clip",
778 GTK_CSS_AREA_BORDER_BOX);
780 gtk_style_property_register ("background-origin",
786 GTK_CSS_AREA_PADDING_BOX);
788 g_value_init (&value, GTK_TYPE_CSS_SPECIAL_VALUE);
789 g_value_set_enum (&value, GTK_CSS_CURRENT_COLOR);
790 _gtk_style_property_register ("border-top-color",
797 _gtk_style_property_register ("border-right-color",
804 _gtk_style_property_register ("border-bottom-color",
811 _gtk_style_property_register ("border-left-color",
818 g_value_unset (&value);
820 gtk_style_property_register ("background-repeat",
821 GTK_TYPE_CSS_BACKGROUND_REPEAT,
823 background_repeat_value_parse,
824 background_repeat_value_print,
826 GTK_CSS_BACKGROUND_REPEAT | (GTK_CSS_BACKGROUND_REPEAT << GTK_CSS_BACKGROUND_REPEAT_SHIFT));
827 g_value_init (&value, GTK_TYPE_CSS_IMAGE);
828 _gtk_style_property_register ("background-image",
829 CAIRO_GOBJECT_TYPE_PATTERN,
831 css_image_value_parse,
832 css_image_value_print,
833 css_image_value_compute,
836 _gtk_style_property_register ("border-image-source",
837 CAIRO_GOBJECT_TYPE_PATTERN,
839 css_image_value_parse,
840 css_image_value_print,
841 css_image_value_compute,
843 g_value_unset (&value);
844 gtk_style_property_register ("border-image-repeat",
845 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
850 &border_image_repeat);
852 /* XXX: The initial vaue is wrong, it should be 100% */
853 gtk_style_property_register ("border-image-slice",
860 gtk_style_property_register ("border-image-width",
867 gtk_style_property_register ("engine",
868 GTK_TYPE_THEMING_ENGINE,
873 gtk_theming_engine_load (NULL));
874 gtk_style_property_register ("transition",
875 GTK_TYPE_ANIMATION_DESCRIPTION,
882 /* Private property holding the binding sets */
883 gtk_style_property_register ("gtk-key-bindings",
886 bindings_value_parse,
887 bindings_value_print,