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 _gtk_style_property_register (const char * name,
51 GtkStylePropertyFlags flags,
52 GtkCssStylePropertyParseFunc parse_value,
53 GtkCssStylePropertyPrintFunc print_value,
54 GtkCssStylePropertyComputeFunc compute_value,
55 const GValue * initial_value)
57 GtkCssStyleProperty *node;
59 node = g_object_new (GTK_TYPE_CSS_STYLE_PROPERTY,
60 "inherit", (flags & GTK_STYLE_PROPERTY_INHERIT) ? TRUE : FALSE,
61 "initial-value", initial_value,
63 "value-type", value_type,
67 node->parse_value = parse_value;
69 node->print_value = print_value;
71 node->compute_value = compute_value;
75 gtk_style_property_register (const char * name,
77 GtkStylePropertyFlags flags,
78 GtkCssStylePropertyParseFunc parse_value,
79 GtkCssStylePropertyPrintFunc print_value,
80 GtkCssStylePropertyComputeFunc compute_value,
83 GValue initial_value = G_VALUE_INIT;
87 va_start (args, compute_value);
88 G_VALUE_COLLECT_INIT (&initial_value, value_type,
92 g_error ("property `%s' initial value is broken: %s", name, error);
93 g_value_unset (&initial_value);
99 _gtk_style_property_register (name,
107 g_value_unset (&initial_value);
113 string_append_double (GString *string,
116 char buf[G_ASCII_DTOSTR_BUF_SIZE];
118 g_ascii_dtostr (buf, sizeof (buf), d);
119 g_string_append (string, buf);
123 string_append_string (GString *str,
128 g_string_append_c (str, '"');
131 len = strcspn (string, "\"\n\r\f");
132 g_string_append (str, string);
139 g_string_append (str, "\\A ");
142 g_string_append (str, "\\D ");
145 g_string_append (str, "\\C ");
148 g_string_append (str, "\\\"");
151 g_assert_not_reached ();
156 g_string_append_c (str, '"');
159 /*** IMPLEMENTATIONS ***/
162 font_family_parse (GtkCssStyleProperty *property,
164 GtkCssParser *parser,
170 /* We don't special case generic families. Pango should do
173 names = g_ptr_array_new ();
176 name = _gtk_css_parser_try_ident (parser, TRUE);
179 GString *string = g_string_new (name);
181 while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
183 g_string_append_c (string, ' ');
184 g_string_append (string, name);
187 name = g_string_free (string, FALSE);
191 name = _gtk_css_parser_read_string (parser);
194 g_ptr_array_free (names, TRUE);
199 g_ptr_array_add (names, name);
200 } while (_gtk_css_parser_try (parser, ",", TRUE));
202 /* NULL-terminate array */
203 g_ptr_array_add (names, NULL);
204 g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
209 font_family_value_print (GtkCssStyleProperty *property,
213 const char **names = g_value_get_boxed (value);
215 if (names == NULL || *names == NULL)
217 g_string_append (string, "none");
221 string_append_string (string, *names);
225 g_string_append (string, ", ");
226 string_append_string (string, *names);
232 bindings_value_parse (GtkCssStyleProperty *property,
234 GtkCssParser *parser,
238 GtkBindingSet *binding_set;
241 array = g_ptr_array_new ();
244 name = _gtk_css_parser_try_ident (parser, TRUE);
247 _gtk_css_parser_error (parser, "Not a valid binding name");
248 g_ptr_array_free (array, TRUE);
252 binding_set = gtk_binding_set_find (name);
256 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
261 g_ptr_array_add (array, binding_set);
264 while (_gtk_css_parser_try (parser, ",", TRUE));
266 g_value_take_boxed (value, array);
272 bindings_value_print (GtkCssStyleProperty *property,
279 array = g_value_get_boxed (value);
281 for (i = 0; i < array->len; i++)
283 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
286 g_string_append (string, ", ");
287 g_string_append (string, binding_set->set_name);
292 border_corner_radius_value_parse (GtkCssStyleProperty *property,
294 GtkCssParser *parser,
297 GtkCssBorderCornerRadius corner;
299 if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
301 _gtk_css_parser_error (parser, "Expected a number");
304 else if (corner.horizontal < 0)
307 if (!_gtk_css_parser_try_double (parser, &corner.vertical))
308 corner.vertical = corner.horizontal;
309 else if (corner.vertical < 0)
312 g_value_set_boxed (value, &corner);
316 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
321 border_corner_radius_value_print (GtkCssStyleProperty *property,
325 GtkCssBorderCornerRadius *corner;
327 corner = g_value_get_boxed (value);
331 g_string_append (string, "none");
335 string_append_double (string, corner->horizontal);
336 if (corner->horizontal != corner->vertical)
338 g_string_append_c (string, ' ');
339 string_append_double (string, corner->vertical);
344 css_image_value_parse (GtkCssStyleProperty *property,
346 GtkCssParser *parser,
351 if (_gtk_css_parser_try (parser, "none", TRUE))
355 image = _gtk_css_image_new_parse (parser, base);
360 g_value_unset (value);
361 g_value_init (value, GTK_TYPE_CSS_IMAGE);
362 g_value_take_object (value, image);
367 css_image_value_print (GtkCssStyleProperty *property,
371 GtkCssImage *image = g_value_get_object (value);
374 _gtk_css_image_print (image, string);
376 g_string_append (string, "none");
380 css_image_value_compute (GtkCssStyleProperty *property,
382 GtkStyleContext *context,
383 const GValue *specified)
385 GtkCssImage *image = g_value_get_object (specified);
388 image = _gtk_css_image_compute (image, context);
390 g_value_init (computed, GTK_TYPE_CSS_IMAGE);
391 g_value_take_object (computed, image);
394 /*** REGISTRATION ***/
396 #define rgba_init(rgba, r, g, b, a) G_STMT_START{ \
398 (rgba)->green = (g); \
399 (rgba)->blue = (b); \
400 (rgba)->alpha = (a); \
403 _gtk_css_style_property_init_properties (void)
405 GValue value = { 0, };
406 char *default_font_family[] = { "Sans", NULL };
408 GtkCssBorderCornerRadius no_corner_radius = { 0, };
409 GtkBorder border_of_ones = { 1, 1, 1, 1 };
410 GtkCssBackgroundRepeat background_repeat = { GTK_CSS_BACKGROUND_REPEAT_STYLE_REPEAT };
411 GtkCssBorderImageRepeat border_image_repeat = { GTK_CSS_REPEAT_STYLE_STRETCH, GTK_CSS_REPEAT_STYLE_STRETCH };
413 /* Initialize "color" and "font-size" first,
414 * so that when computing values later they are
415 * done first. That way, 'currentColor' and font
416 * sizes in em can be looked up properly */
417 rgba_init (&rgba, 1, 1, 1, 1);
418 gtk_style_property_register ("color",
420 GTK_STYLE_PROPERTY_INHERIT,
425 gtk_style_property_register ("font-size",
427 GTK_STYLE_PROPERTY_INHERIT,
433 /* properties that aren't referenced when computing values
435 rgba_init (&rgba, 0, 0, 0, 0);
436 gtk_style_property_register ("background-color",
444 gtk_style_property_register ("font-family",
446 GTK_STYLE_PROPERTY_INHERIT,
448 font_family_value_print,
450 default_font_family);
451 gtk_style_property_register ("font-style",
453 GTK_STYLE_PROPERTY_INHERIT,
458 gtk_style_property_register ("font-variant",
460 GTK_STYLE_PROPERTY_INHERIT,
464 PANGO_VARIANT_NORMAL);
465 /* xxx: need to parse this properly, ie parse the numbers */
466 gtk_style_property_register ("font-weight",
468 GTK_STYLE_PROPERTY_INHERIT,
472 PANGO_WEIGHT_NORMAL);
474 gtk_style_property_register ("text-shadow",
476 GTK_STYLE_PROPERTY_INHERIT,
482 gtk_style_property_register ("icon-shadow",
484 GTK_STYLE_PROPERTY_INHERIT,
490 gtk_style_property_register ("box-shadow",
498 gtk_style_property_register ("margin-top",
505 gtk_style_property_register ("margin-left",
512 gtk_style_property_register ("margin-bottom",
519 gtk_style_property_register ("margin-right",
526 gtk_style_property_register ("padding-top",
533 gtk_style_property_register ("padding-left",
540 gtk_style_property_register ("padding-bottom",
547 gtk_style_property_register ("padding-right",
554 gtk_style_property_register ("border-top-width",
561 gtk_style_property_register ("border-left-width",
568 gtk_style_property_register ("border-bottom-width",
575 gtk_style_property_register ("border-right-width",
583 gtk_style_property_register ("border-top-left-radius",
584 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
586 border_corner_radius_value_parse,
587 border_corner_radius_value_print,
590 gtk_style_property_register ("border-top-right-radius",
591 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
593 border_corner_radius_value_parse,
594 border_corner_radius_value_print,
597 gtk_style_property_register ("border-bottom-right-radius",
598 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
600 border_corner_radius_value_parse,
601 border_corner_radius_value_print,
604 gtk_style_property_register ("border-bottom-left-radius",
605 GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
607 border_corner_radius_value_parse,
608 border_corner_radius_value_print,
612 gtk_style_property_register ("border-style",
613 GTK_TYPE_BORDER_STYLE,
618 GTK_BORDER_STYLE_NONE);
619 gtk_style_property_register ("background-clip",
625 GTK_CSS_AREA_BORDER_BOX);
627 gtk_style_property_register ("background-origin",
633 GTK_CSS_AREA_PADDING_BOX);
635 g_value_init (&value, GTK_TYPE_CSS_SPECIAL_VALUE);
636 g_value_set_enum (&value, GTK_CSS_CURRENT_COLOR);
637 _gtk_style_property_register ("border-top-color",
644 _gtk_style_property_register ("border-right-color",
651 _gtk_style_property_register ("border-bottom-color",
658 _gtk_style_property_register ("border-left-color",
665 g_value_unset (&value);
667 gtk_style_property_register ("background-repeat",
668 GTK_TYPE_CSS_BACKGROUND_REPEAT,
674 g_value_init (&value, GTK_TYPE_CSS_IMAGE);
675 _gtk_style_property_register ("background-image",
676 CAIRO_GOBJECT_TYPE_PATTERN,
678 css_image_value_parse,
679 css_image_value_print,
680 css_image_value_compute,
683 _gtk_style_property_register ("border-image-source",
684 CAIRO_GOBJECT_TYPE_PATTERN,
686 css_image_value_parse,
687 css_image_value_print,
688 css_image_value_compute,
690 g_value_unset (&value);
691 gtk_style_property_register ("border-image-repeat",
692 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
697 &border_image_repeat);
699 /* XXX: The initial vaue is wrong, it should be 100% */
700 gtk_style_property_register ("border-image-slice",
707 gtk_style_property_register ("border-image-width",
714 gtk_style_property_register ("engine",
715 GTK_TYPE_THEMING_ENGINE,
720 gtk_theming_engine_load (NULL));
721 gtk_style_property_register ("transition",
722 GTK_TYPE_ANIMATION_DESCRIPTION,
729 /* Private property holding the binding sets */
730 gtk_style_property_register ("gtk-key-bindings",
733 bindings_value_parse,
734 bindings_value_print,