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 "gtkgradient.h"
40 #include "gtkshadowprivate.h"
41 #include "gtkthemingengine.h"
42 #include "gtktypebuiltins.h"
43 #include "gtkwin32themeprivate.h"
45 /*** REGISTRATION ***/
48 _gtk_style_property_register (GParamSpec *pspec,
49 GtkStylePropertyFlags flags,
50 GtkStyleParseFunc parse_func,
51 GtkStylePrintFunc print_func,
52 const GValue * initial_value)
54 GtkStyleProperty *node;
56 node = g_object_new (GTK_TYPE_CSS_STYLE_PROPERTY,
57 "inherit", (flags & GTK_STYLE_PROPERTY_INHERIT) ? TRUE : FALSE,
58 "initial-value", initial_value,
60 "value-type", pspec->value_type,
62 g_assert (node->value_type == pspec->value_type);
63 GTK_CSS_STYLE_PROPERTY (node)->pspec = pspec;
64 node->parse_func = parse_func;
65 node->print_func = print_func;
69 gtk_style_property_register (GParamSpec *pspec,
70 GtkStylePropertyFlags flags,
71 GtkStyleParseFunc parse_func,
72 GtkStylePrintFunc print_func,
75 GValue initial_value = G_VALUE_INIT;
79 va_start (args, print_func);
80 G_VALUE_COLLECT_INIT (&initial_value, pspec->value_type,
84 g_error ("property `%s' initial value is broken: %s", pspec->name, error);
85 g_value_unset (&initial_value);
91 _gtk_style_property_register (pspec, flags, parse_func, print_func, &initial_value);
93 g_value_unset (&initial_value);
99 string_append_double (GString *string,
102 char buf[G_ASCII_DTOSTR_BUF_SIZE];
104 g_ascii_dtostr (buf, sizeof (buf), d);
105 g_string_append (string, buf);
109 string_append_string (GString *str,
114 g_string_append_c (str, '"');
117 len = strcspn (string, "\"\n\r\f");
118 g_string_append (str, string);
125 g_string_append (str, "\\A ");
128 g_string_append (str, "\\D ");
131 g_string_append (str, "\\C ");
134 g_string_append (str, "\\\"");
137 g_assert_not_reached ();
142 g_string_append_c (str, '"');
145 /*** IMPLEMENTATIONS ***/
148 font_family_parse (GtkCssParser *parser,
155 /* We don't special case generic families. Pango should do
158 names = g_ptr_array_new ();
161 name = _gtk_css_parser_try_ident (parser, TRUE);
164 GString *string = g_string_new (name);
166 while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
168 g_string_append_c (string, ' ');
169 g_string_append (string, name);
172 name = g_string_free (string, FALSE);
176 name = _gtk_css_parser_read_string (parser);
179 g_ptr_array_free (names, TRUE);
184 g_ptr_array_add (names, name);
185 } while (_gtk_css_parser_try (parser, ",", TRUE));
187 /* NULL-terminate array */
188 g_ptr_array_add (names, NULL);
189 g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
194 font_family_value_print (const GValue *value,
197 const char **names = g_value_get_boxed (value);
199 if (names == NULL || *names == NULL)
201 g_string_append (string, "none");
205 string_append_string (string, *names);
209 g_string_append (string, ", ");
210 string_append_string (string, *names);
216 bindings_value_parse (GtkCssParser *parser,
221 GtkBindingSet *binding_set;
224 array = g_ptr_array_new ();
227 name = _gtk_css_parser_try_ident (parser, TRUE);
230 _gtk_css_parser_error (parser, "Not a valid binding name");
231 g_ptr_array_free (array, TRUE);
235 binding_set = gtk_binding_set_find (name);
239 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
244 g_ptr_array_add (array, binding_set);
247 while (_gtk_css_parser_try (parser, ",", TRUE));
249 g_value_take_boxed (value, array);
255 bindings_value_print (const GValue *value,
261 array = g_value_get_boxed (value);
263 for (i = 0; i < array->len; i++)
265 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
268 g_string_append (string, ", ");
269 g_string_append (string, binding_set->set_name);
274 border_corner_radius_value_parse (GtkCssParser *parser,
278 GtkCssBorderCornerRadius corner;
280 if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
282 _gtk_css_parser_error (parser, "Expected a number");
285 else if (corner.horizontal < 0)
288 if (!_gtk_css_parser_try_double (parser, &corner.vertical))
289 corner.vertical = corner.horizontal;
290 else if (corner.vertical < 0)
293 g_value_set_boxed (value, &corner);
297 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
302 border_corner_radius_value_print (const GValue *value,
305 GtkCssBorderCornerRadius *corner;
307 corner = g_value_get_boxed (value);
311 g_string_append (string, "none");
315 string_append_double (string, corner->horizontal);
316 if (corner->horizontal != corner->vertical)
318 g_string_append_c (string, ' ');
319 string_append_double (string, corner->vertical);
323 /*** REGISTRATION ***/
325 #define rgba_init(rgba, r, g, b, a) G_STMT_START{ \
327 (rgba)->green = (g); \
328 (rgba)->blue = (b); \
329 (rgba)->alpha = (a); \
332 _gtk_css_style_property_init_properties (void)
334 GValue value = { 0, };
335 char *default_font_family[] = { "Sans", NULL };
337 GtkCssBorderCornerRadius no_corner_radius = { 0, };
338 GtkBorder border_of_ones = { 1, 1, 1, 1 };
339 GtkCssBackgroundRepeat background_repeat = { GTK_CSS_BACKGROUND_REPEAT_STYLE_REPEAT };
340 GtkCssBorderImageRepeat border_image_repeat = { GTK_CSS_REPEAT_STYLE_STRETCH, GTK_CSS_REPEAT_STYLE_STRETCH };
342 /* note that gtk_style_properties_register_property() calls this function,
343 * so make sure we're sanely inited to avoid infloops */
345 rgba_init (&rgba, 1, 1, 1, 1);
346 gtk_style_property_register (g_param_spec_boxed ("color",
350 GTK_STYLE_PROPERTY_INHERIT,
354 rgba_init (&rgba, 0, 0, 0, 0);
355 gtk_style_property_register (g_param_spec_boxed ("background-color",
364 gtk_style_property_register (g_param_spec_boxed ("font-family",
368 GTK_STYLE_PROPERTY_INHERIT,
370 font_family_value_print,
371 default_font_family);
372 gtk_style_property_register (g_param_spec_enum ("font-style",
376 PANGO_STYLE_NORMAL, 0),
377 GTK_STYLE_PROPERTY_INHERIT,
381 gtk_style_property_register (g_param_spec_enum ("font-variant",
385 PANGO_VARIANT_NORMAL, 0),
386 GTK_STYLE_PROPERTY_INHERIT,
389 PANGO_VARIANT_NORMAL);
390 /* xxx: need to parse this properly, ie parse the numbers */
391 gtk_style_property_register (g_param_spec_enum ("font-weight",
395 PANGO_WEIGHT_NORMAL, 0),
396 GTK_STYLE_PROPERTY_INHERIT,
399 PANGO_WEIGHT_NORMAL);
400 gtk_style_property_register (g_param_spec_double ("font-size",
403 0, G_MAXDOUBLE, 0, 0),
404 GTK_STYLE_PROPERTY_INHERIT,
409 gtk_style_property_register (g_param_spec_boxed ("text-shadow",
413 GTK_STYLE_PROPERTY_INHERIT,
418 gtk_style_property_register (g_param_spec_boxed ("icon-shadow",
422 GTK_STYLE_PROPERTY_INHERIT,
427 gtk_style_property_register (g_param_spec_boxed ("box-shadow",
436 gtk_style_property_register (g_param_spec_int ("margin-top",
444 gtk_style_property_register (g_param_spec_int ("margin-left",
452 gtk_style_property_register (g_param_spec_int ("margin-bottom",
460 gtk_style_property_register (g_param_spec_int ("margin-right",
468 gtk_style_property_register (g_param_spec_int ("padding-top",
476 gtk_style_property_register (g_param_spec_int ("padding-left",
484 gtk_style_property_register (g_param_spec_int ("padding-bottom",
492 gtk_style_property_register (g_param_spec_int ("padding-right",
500 gtk_style_property_register (g_param_spec_int ("border-top-width",
502 "Border width at top",
508 gtk_style_property_register (g_param_spec_int ("border-left-width",
510 "Border width at left",
516 gtk_style_property_register (g_param_spec_int ("border-bottom-width",
517 "border bottom width",
518 "Border width at bottom",
524 gtk_style_property_register (g_param_spec_int ("border-right-width",
525 "border right width",
526 "Border width at right",
533 gtk_style_property_register (g_param_spec_boxed ("border-top-left-radius",
534 "Border top left radius",
535 "Border radius of top left corner, in pixels",
536 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
538 border_corner_radius_value_parse,
539 border_corner_radius_value_print,
541 gtk_style_property_register (g_param_spec_boxed ("border-top-right-radius",
542 "Border top right radius",
543 "Border radius of top right corner, in pixels",
544 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
546 border_corner_radius_value_parse,
547 border_corner_radius_value_print,
549 gtk_style_property_register (g_param_spec_boxed ("border-bottom-right-radius",
550 "Border bottom right radius",
551 "Border radius of bottom right corner, in pixels",
552 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
554 border_corner_radius_value_parse,
555 border_corner_radius_value_print,
557 gtk_style_property_register (g_param_spec_boxed ("border-bottom-left-radius",
558 "Border bottom left radius",
559 "Border radius of bottom left corner, in pixels",
560 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
562 border_corner_radius_value_parse,
563 border_corner_radius_value_print,
566 gtk_style_property_register (g_param_spec_enum ("border-style",
569 GTK_TYPE_BORDER_STYLE,
570 GTK_BORDER_STYLE_NONE, 0),
574 GTK_BORDER_STYLE_NONE);
575 gtk_style_property_register (g_param_spec_enum ("background-clip",
579 GTK_CSS_AREA_BORDER_BOX, 0),
583 GTK_CSS_AREA_BORDER_BOX);
585 gtk_style_property_register (g_param_spec_enum ("background-origin",
589 GTK_CSS_AREA_PADDING_BOX, 0),
593 GTK_CSS_AREA_PADDING_BOX);
595 g_value_init (&value, GTK_TYPE_CSS_SPECIAL_VALUE);
596 g_value_set_enum (&value, GTK_CSS_CURRENT_COLOR);
597 _gtk_style_property_register (g_param_spec_boxed ("border-top-color",
605 _gtk_style_property_register (g_param_spec_boxed ("border-right-color",
606 "Border right color",
607 "Border right color",
613 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-color",
614 "Border bottom color",
615 "Border bottom color",
621 _gtk_style_property_register (g_param_spec_boxed ("border-left-color",
629 g_value_unset (&value);
631 gtk_style_property_register (g_param_spec_boxed ("background-image",
634 CAIRO_GOBJECT_TYPE_PATTERN, 0),
639 gtk_style_property_register (g_param_spec_boxed ("background-repeat",
642 GTK_TYPE_CSS_BACKGROUND_REPEAT, 0),
648 gtk_style_property_register (g_param_spec_boxed ("border-image-source",
649 "Border image source",
650 "Border image source",
651 CAIRO_GOBJECT_TYPE_PATTERN, 0),
656 gtk_style_property_register (g_param_spec_boxed ("border-image-repeat",
657 "Border image repeat",
658 "Border image repeat",
659 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0),
663 &border_image_repeat);
665 /* XXX: The initial vaue is wrong, it should be 100% */
666 gtk_style_property_register (g_param_spec_boxed ("border-image-slice",
667 "Border image slice",
668 "Border image slice",
674 gtk_style_property_register (g_param_spec_boxed ("border-image-width",
675 "Border image width",
676 "Border image width",
682 gtk_style_property_register (g_param_spec_object ("engine",
685 GTK_TYPE_THEMING_ENGINE, 0),
689 gtk_theming_engine_load (NULL));
690 gtk_style_property_register (g_param_spec_boxed ("transition",
691 "Transition animation description",
692 "Transition animation description",
693 GTK_TYPE_ANIMATION_DESCRIPTION, 0),
699 /* Private property holding the binding sets */
700 gtk_style_property_register (g_param_spec_boxed ("gtk-key-bindings",
703 G_TYPE_PTR_ARRAY, 0),
705 bindings_value_parse,
706 bindings_value_print,