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"
29 #include <gdk-pixbuf/gdk-pixbuf.h>
30 #include <cairo-gobject.h>
32 #include "gtkcssprovider.h"
33 #include "gtkcssparserprivate.h"
34 #include "gtkcsstypesprivate.h"
35 #include "gtkprivatetypebuiltins.h"
37 /* the actual parsers we have */
38 #include "gtkanimationdescription.h"
39 #include "gtkbindings.h"
40 #include "gtkborderimageprivate.h"
41 #include "gtkgradient.h"
42 #include "gtkshadowprivate.h"
43 #include "gtkthemingengine.h"
44 #include "gtktypebuiltins.h"
45 #include "gtkwin32themeprivate.h"
47 /* this is in case round() is not provided by the compiler,
48 * such as in the case of C89 compilers, like MSVC
50 #include "fallback-c89.c"
52 static GHashTable *parse_funcs = NULL;
53 static GHashTable *print_funcs = NULL;
54 static GHashTable *properties = NULL;
57 register_conversion_function (GType type,
58 GtkStyleParseFunc parse,
59 GtkStylePrintFunc print)
62 g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse);
64 g_hash_table_insert (print_funcs, GSIZE_TO_POINTER (type), print);
68 string_append_double (GString *string,
71 char buf[G_ASCII_DTOSTR_BUF_SIZE];
73 g_ascii_dtostr (buf, sizeof (buf), d);
74 g_string_append (string, buf);
78 string_append_string (GString *str,
83 g_string_append_c (str, '"');
86 len = strcspn (string, "\"\n\r\f");
87 g_string_append (str, string);
94 g_string_append (str, "\\A ");
97 g_string_append (str, "\\D ");
100 g_string_append (str, "\\C ");
103 g_string_append (str, "\\\"");
106 g_assert_not_reached ();
111 g_string_append_c (str, '"');
114 /*** IMPLEMENTATIONS ***/
117 enum_parse (GtkCssParser *parser,
123 if (_gtk_css_parser_try_enum (parser, type, res))
126 str = _gtk_css_parser_try_ident (parser, TRUE);
129 _gtk_css_parser_error (parser, "Expected an identifier");
133 _gtk_css_parser_error (parser,
134 "Unknown value '%s' for enum type '%s'",
135 str, g_type_name (type));
142 enum_print (int value,
146 GEnumClass *enum_class;
147 GEnumValue *enum_value;
149 enum_class = g_type_class_ref (type);
150 enum_value = g_enum_get_value (enum_class, value);
152 g_string_append (string, enum_value->value_nick);
154 g_type_class_unref (enum_class);
158 rgba_value_parse (GtkCssParser *parser,
162 GtkSymbolicColor *symbolic;
165 symbolic = _gtk_css_parser_read_symbolic_color (parser);
166 if (symbolic == NULL)
169 if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
171 g_value_set_boxed (value, &rgba);
172 gtk_symbolic_color_unref (symbolic);
176 g_value_unset (value);
177 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
178 g_value_take_boxed (value, symbolic);
185 rgba_value_print (const GValue *value,
188 const GdkRGBA *rgba = g_value_get_boxed (value);
191 g_string_append (string, "none");
194 char *s = gdk_rgba_to_string (rgba);
195 g_string_append (string, s);
201 color_value_parse (GtkCssParser *parser,
205 GtkSymbolicColor *symbolic;
208 symbolic = _gtk_css_parser_read_symbolic_color (parser);
209 if (symbolic == NULL)
212 if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
216 color.red = rgba.red * 65535. + 0.5;
217 color.green = rgba.green * 65535. + 0.5;
218 color.blue = rgba.blue * 65535. + 0.5;
220 g_value_set_boxed (value, &color);
224 g_value_unset (value);
225 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
226 g_value_take_boxed (value, symbolic);
233 color_value_print (const GValue *value,
236 const GdkColor *color = g_value_get_boxed (value);
239 g_string_append (string, "none");
242 char *s = gdk_color_to_string (color);
243 g_string_append (string, s);
249 symbolic_color_value_parse (GtkCssParser *parser,
253 GtkSymbolicColor *symbolic;
255 symbolic = _gtk_css_parser_read_symbolic_color (parser);
256 if (symbolic == NULL)
259 g_value_take_boxed (value, symbolic);
264 symbolic_color_value_print (const GValue *value,
267 GtkSymbolicColor *symbolic = g_value_get_boxed (value);
269 if (symbolic == NULL)
270 g_string_append (string, "none");
273 char *s = gtk_symbolic_color_to_string (symbolic);
274 g_string_append (string, s);
280 font_family_parse (GtkCssParser *parser,
287 /* We don't special case generic families. Pango should do
290 names = g_ptr_array_new ();
293 name = _gtk_css_parser_try_ident (parser, TRUE);
296 GString *string = g_string_new (name);
298 while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
300 g_string_append_c (string, ' ');
301 g_string_append (string, name);
304 name = g_string_free (string, FALSE);
308 name = _gtk_css_parser_read_string (parser);
311 g_ptr_array_free (names, TRUE);
316 g_ptr_array_add (names, name);
317 } while (_gtk_css_parser_try (parser, ",", TRUE));
319 /* NULL-terminate array */
320 g_ptr_array_add (names, NULL);
321 g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
326 font_family_value_print (const GValue *value,
329 const char **names = g_value_get_boxed (value);
331 if (names == NULL || *names == NULL)
333 g_string_append (string, "none");
337 string_append_string (string, *names);
341 g_string_append (string, ", ");
342 string_append_string (string, *names);
348 font_description_value_parse (GtkCssParser *parser,
352 PangoFontDescription *font_desc;
356 str = _gtk_css_parser_read_value (parser);
360 font_desc = pango_font_description_from_string (str);
361 mask = pango_font_description_get_set_fields (font_desc);
362 /* These values are not really correct,
363 * but the fields must be set, so we set them to something */
364 if ((mask & PANGO_FONT_MASK_FAMILY) == 0)
365 pango_font_description_set_family_static (font_desc, "Sans");
366 if ((mask & PANGO_FONT_MASK_SIZE) == 0)
367 pango_font_description_set_size (font_desc, 10 * PANGO_SCALE);
369 g_value_take_boxed (value, font_desc);
374 font_description_value_print (const GValue *value,
377 const PangoFontDescription *desc = g_value_get_boxed (value);
380 g_string_append (string, "none");
383 char *s = pango_font_description_to_string (desc);
384 g_string_append (string, s);
390 boolean_value_parse (GtkCssParser *parser,
394 if (_gtk_css_parser_try (parser, "true", TRUE) ||
395 _gtk_css_parser_try (parser, "1", TRUE))
397 g_value_set_boolean (value, TRUE);
400 else if (_gtk_css_parser_try (parser, "false", TRUE) ||
401 _gtk_css_parser_try (parser, "0", TRUE))
403 g_value_set_boolean (value, FALSE);
408 _gtk_css_parser_error (parser, "Expected a boolean value");
414 boolean_value_print (const GValue *value,
417 if (g_value_get_boolean (value))
418 g_string_append (string, "true");
420 g_string_append (string, "false");
424 int_value_parse (GtkCssParser *parser,
430 if (_gtk_css_parser_begins_with (parser, '-'))
432 int res = _gtk_win32_theme_int_parse (parser, base, &i);
435 g_value_set_int (value, i);
438 /* < 0 => continue */
441 if (!_gtk_css_parser_try_int (parser, &i))
443 _gtk_css_parser_error (parser, "Expected a valid integer value");
447 g_value_set_int (value, i);
452 int_value_print (const GValue *value,
455 g_string_append_printf (string, "%d", g_value_get_int (value));
459 uint_value_parse (GtkCssParser *parser,
465 if (!_gtk_css_parser_try_uint (parser, &u))
467 _gtk_css_parser_error (parser, "Expected a valid unsigned value");
471 g_value_set_uint (value, u);
476 uint_value_print (const GValue *value,
479 g_string_append_printf (string, "%u", g_value_get_uint (value));
483 double_value_parse (GtkCssParser *parser,
489 if (!_gtk_css_parser_try_double (parser, &d))
491 _gtk_css_parser_error (parser, "Expected a number");
495 g_value_set_double (value, d);
500 double_value_print (const GValue *value,
503 string_append_double (string, g_value_get_double (value));
507 float_value_parse (GtkCssParser *parser,
513 if (!_gtk_css_parser_try_double (parser, &d))
515 _gtk_css_parser_error (parser, "Expected a number");
519 g_value_set_float (value, d);
524 float_value_print (const GValue *value,
527 string_append_double (string, g_value_get_float (value));
531 string_value_parse (GtkCssParser *parser,
535 char *str = _gtk_css_parser_read_string (parser);
540 g_value_take_string (value, str);
545 string_value_print (const GValue *value,
548 string_append_string (str, g_value_get_string (value));
552 theming_engine_value_parse (GtkCssParser *parser,
556 GtkThemingEngine *engine;
559 str = _gtk_css_parser_try_ident (parser, TRUE);
562 _gtk_css_parser_error (parser, "Expected a valid theme name");
566 engine = gtk_theming_engine_load (str);
569 _gtk_css_parser_error (parser, "Themeing engine '%s' not found", str);
574 g_value_set_object (value, engine);
580 theming_engine_value_print (const GValue *value,
583 GtkThemingEngine *engine;
586 engine = g_value_get_object (value);
588 g_string_append (string, "none");
591 /* XXX: gtk_theming_engine_get_name()? */
592 g_object_get (engine, "name", &name, NULL);
593 g_string_append (string, name ? name : "none");
599 animation_description_value_parse (GtkCssParser *parser,
603 GtkAnimationDescription *desc;
606 str = _gtk_css_parser_read_value (parser);
610 desc = _gtk_animation_description_from_string (str);
615 _gtk_css_parser_error (parser, "Invalid animation description");
619 g_value_take_boxed (value, desc);
624 animation_description_value_print (const GValue *value,
627 GtkAnimationDescription *desc = g_value_get_boxed (value);
630 g_string_append (string, "none");
632 _gtk_animation_description_print (desc, string);
636 border_value_parse (GtkCssParser *parser,
640 GtkBorder border = { 0, };
643 for (i = 0; i < G_N_ELEMENTS (numbers); i++)
645 if (_gtk_css_parser_begins_with (parser, '-'))
647 /* These are strictly speaking signed, but we want to be able to use them
648 for unsigned types too, as the actual ranges of values make this safe */
649 int res = _gtk_win32_theme_int_parse (parser, base, (int *)&numbers[i]);
651 if (res == 0) /* Parse error, report */
654 if (res < 0) /* Nothing known to expand */
659 if (!_gtk_css_parser_try_uint (parser, &numbers[i]))
662 /* XXX: shouldn't allow spaces here? */
663 _gtk_css_parser_try (parser, "px", TRUE);
669 _gtk_css_parser_error (parser, "Expected valid border");
673 border.top = numbers[0];
675 border.right = numbers[1];
677 border.right = border.top;
679 border.bottom = numbers[2];
681 border.bottom = border.top;
683 border.left = numbers[3];
685 border.left = border.right;
687 g_value_set_boxed (value, &border);
692 border_value_print (const GValue *value, GString *string)
694 const GtkBorder *border = g_value_get_boxed (value);
697 g_string_append (string, "none");
698 else if (border->left != border->right)
699 g_string_append_printf (string, "%d %d %d %d", border->top, border->right, border->bottom, border->left);
700 else if (border->top != border->bottom)
701 g_string_append_printf (string, "%d %d %d", border->top, border->right, border->bottom);
702 else if (border->top != border->left)
703 g_string_append_printf (string, "%d %d", border->top, border->right);
705 g_string_append_printf (string, "%d", border->top);
709 gradient_value_parse (GtkCssParser *parser,
713 GtkGradient *gradient;
714 cairo_pattern_type_t type;
718 if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
720 _gtk_css_parser_error (parser,
721 "Expected '-gtk-gradient'");
725 if (!_gtk_css_parser_try (parser, "(", TRUE))
727 _gtk_css_parser_error (parser,
728 "Expected '(' after '-gtk-gradient'");
732 /* Parse gradient type */
733 if (_gtk_css_parser_try (parser, "linear", TRUE))
734 type = CAIRO_PATTERN_TYPE_LINEAR;
735 else if (_gtk_css_parser_try (parser, "radial", TRUE))
736 type = CAIRO_PATTERN_TYPE_RADIAL;
739 _gtk_css_parser_error (parser,
740 "Gradient type must be 'radial' or 'linear'");
744 /* Parse start/stop position parameters */
745 for (i = 0; i < 2; i++)
747 if (! _gtk_css_parser_try (parser, ",", TRUE))
749 _gtk_css_parser_error (parser,
754 if (_gtk_css_parser_try (parser, "left", TRUE))
756 else if (_gtk_css_parser_try (parser, "right", TRUE))
758 else if (_gtk_css_parser_try (parser, "center", TRUE))
760 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3]))
762 _gtk_css_parser_error (parser,
763 "Expected a valid X coordinate");
767 if (_gtk_css_parser_try (parser, "top", TRUE))
768 coords[i * 3 + 1] = 0;
769 else if (_gtk_css_parser_try (parser, "bottom", TRUE))
770 coords[i * 3 + 1] = 1;
771 else if (_gtk_css_parser_try (parser, "center", TRUE))
772 coords[i * 3 + 1] = 0.5;
773 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3 + 1]))
775 _gtk_css_parser_error (parser,
776 "Expected a valid Y coordinate");
780 if (type == CAIRO_PATTERN_TYPE_RADIAL)
783 if (! _gtk_css_parser_try (parser, ",", TRUE))
785 _gtk_css_parser_error (parser,
790 if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
792 _gtk_css_parser_error (parser,
793 "Expected a numer for the radius");
799 if (type == CAIRO_PATTERN_TYPE_LINEAR)
800 gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
802 gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
803 coords[3], coords[4], coords[5]);
805 while (_gtk_css_parser_try (parser, ",", TRUE))
807 GtkSymbolicColor *color;
810 if (_gtk_css_parser_try (parser, "from", TRUE))
814 if (!_gtk_css_parser_try (parser, "(", TRUE))
816 gtk_gradient_unref (gradient);
817 _gtk_css_parser_error (parser,
823 else if (_gtk_css_parser_try (parser, "to", TRUE))
827 if (!_gtk_css_parser_try (parser, "(", TRUE))
829 gtk_gradient_unref (gradient);
830 _gtk_css_parser_error (parser,
836 else if (_gtk_css_parser_try (parser, "color-stop", TRUE))
838 if (!_gtk_css_parser_try (parser, "(", TRUE))
840 gtk_gradient_unref (gradient);
841 _gtk_css_parser_error (parser,
846 if (!_gtk_css_parser_try_double (parser, &position))
848 gtk_gradient_unref (gradient);
849 _gtk_css_parser_error (parser,
850 "Expected a valid number");
854 if (!_gtk_css_parser_try (parser, ",", TRUE))
856 gtk_gradient_unref (gradient);
857 _gtk_css_parser_error (parser,
864 gtk_gradient_unref (gradient);
865 _gtk_css_parser_error (parser,
866 "Not a valid color-stop definition");
870 color = _gtk_css_parser_read_symbolic_color (parser);
873 gtk_gradient_unref (gradient);
877 gtk_gradient_add_color_stop (gradient, position, color);
878 gtk_symbolic_color_unref (color);
880 if (!_gtk_css_parser_try (parser, ")", TRUE))
882 gtk_gradient_unref (gradient);
883 _gtk_css_parser_error (parser,
889 if (!_gtk_css_parser_try (parser, ")", TRUE))
891 gtk_gradient_unref (gradient);
892 _gtk_css_parser_error (parser,
897 g_value_take_boxed (value, gradient);
902 gradient_value_print (const GValue *value,
905 GtkGradient *gradient = g_value_get_boxed (value);
907 if (gradient == NULL)
908 g_string_append (string, "none");
911 char *s = gtk_gradient_to_string (gradient);
912 g_string_append (string, s);
918 gtk_css_parse_url (GtkCssParser *parser,
924 if (_gtk_css_parser_try (parser, "url", FALSE))
926 if (!_gtk_css_parser_try (parser, "(", TRUE))
928 _gtk_css_parser_skip_whitespace (parser);
929 if (_gtk_css_parser_try (parser, "(", TRUE))
933 error = g_error_new_literal (GTK_CSS_PROVIDER_ERROR,
934 GTK_CSS_PROVIDER_ERROR_DEPRECATED,
935 "Whitespace between 'url' and '(' is deprecated");
937 _gtk_css_parser_take_error (parser, error);
941 _gtk_css_parser_error (parser, "Expected '(' after 'url'");
946 path = _gtk_css_parser_read_string (parser);
950 if (!_gtk_css_parser_try (parser, ")", TRUE))
952 _gtk_css_parser_error (parser, "No closing ')' found for 'url'");
959 path = _gtk_css_parser_try_name (parser, TRUE);
962 _gtk_css_parser_error (parser, "Not a valid url");
967 file = g_file_resolve_relative_path (base, path);
974 pattern_value_parse (GtkCssParser *parser,
978 if (_gtk_css_parser_begins_with (parser, '-'))
981 res = _gtk_win32_theme_part_parse (parser, base, value);
984 /* < 0 => continue */
985 g_value_unset (value);
986 g_value_init (value, GTK_TYPE_GRADIENT);
987 return gradient_value_parse (parser, base, value);
991 GError *error = NULL;
995 cairo_surface_t *surface;
996 cairo_pattern_t *pattern;
998 cairo_matrix_t matrix;
1000 file = gtk_css_parse_url (parser, base);
1004 path = g_file_get_path (file);
1005 g_object_unref (file);
1007 pixbuf = gdk_pixbuf_new_from_file (path, &error);
1011 _gtk_css_parser_take_error (parser, error);
1015 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1016 gdk_pixbuf_get_width (pixbuf),
1017 gdk_pixbuf_get_height (pixbuf));
1018 cr = cairo_create (surface);
1019 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
1021 pattern = cairo_pattern_create_for_surface (surface);
1023 cairo_matrix_init_scale (&matrix,
1024 gdk_pixbuf_get_width (pixbuf),
1025 gdk_pixbuf_get_height (pixbuf));
1026 cairo_pattern_set_matrix (pattern, &matrix);
1028 cairo_surface_destroy (surface);
1030 g_object_unref (pixbuf);
1032 g_value_take_boxed (value, pattern);
1038 static cairo_status_t
1039 surface_write (void *closure,
1040 const unsigned char *data,
1041 unsigned int length)
1043 g_byte_array_append (closure, data, length);
1045 return CAIRO_STATUS_SUCCESS;
1049 surface_print (cairo_surface_t *surface,
1052 #if CAIRO_HAS_PNG_FUNCTIONS
1056 array = g_byte_array_new ();
1057 cairo_surface_write_to_png_stream (surface, surface_write, array);
1058 base64 = g_base64_encode (array->data, array->len);
1059 g_byte_array_free (array, TRUE);
1061 g_string_append (string, "url(\"data:image/png;base64,");
1062 g_string_append (string, base64);
1063 g_string_append (string, "\")");
1067 g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
1072 pattern_value_print (const GValue *value,
1075 cairo_pattern_t *pattern;
1076 cairo_surface_t *surface;
1078 pattern = g_value_get_boxed (value);
1080 if (pattern == NULL)
1082 g_string_append (string, "none");
1086 switch (cairo_pattern_get_type (pattern))
1088 case CAIRO_PATTERN_TYPE_SURFACE:
1089 if (cairo_pattern_get_surface (pattern, &surface) != CAIRO_STATUS_SUCCESS)
1091 g_assert_not_reached ();
1093 surface_print (surface, string);
1095 case CAIRO_PATTERN_TYPE_SOLID:
1096 case CAIRO_PATTERN_TYPE_LINEAR:
1097 case CAIRO_PATTERN_TYPE_RADIAL:
1099 g_assert_not_reached ();
1105 shadow_value_parse (GtkCssParser *parser,
1109 gboolean have_inset, have_color, have_lengths;
1110 gdouble hoffset, voffset, blur, spread;
1111 GtkSymbolicColor *color;
1115 shadow = _gtk_shadow_new ();
1119 have_inset = have_lengths = have_color = FALSE;
1121 for (i = 0; i < 3; i++)
1124 _gtk_css_parser_try (parser, "inset", TRUE))
1130 if (!have_lengths &&
1131 _gtk_css_parser_try_double (parser, &hoffset))
1133 have_lengths = TRUE;
1135 if (!_gtk_css_parser_try_double (parser, &voffset))
1137 _gtk_css_parser_error (parser, "Horizontal and vertical offsets are required");
1138 _gtk_shadow_unref (shadow);
1142 if (!_gtk_css_parser_try_double (parser, &blur))
1145 if (!_gtk_css_parser_try_double (parser, &spread))
1155 /* XXX: the color is optional and UA-defined if it's missing,
1156 * but it doesn't really make sense for us...
1158 color = _gtk_css_parser_read_symbolic_color (parser);
1162 _gtk_shadow_unref (shadow);
1168 if (!have_color || !have_lengths)
1170 _gtk_css_parser_error (parser, "Must specify at least color and offsets");
1171 _gtk_shadow_unref (shadow);
1175 _gtk_shadow_append (shadow,
1180 gtk_symbolic_color_unref (color);
1183 while (_gtk_css_parser_try (parser, ",", TRUE));
1185 g_value_take_boxed (value, shadow);
1190 shadow_value_print (const GValue *value,
1195 shadow = g_value_get_boxed (value);
1198 g_string_append (string, "none");
1200 _gtk_shadow_print (shadow, string);
1204 background_repeat_value_parse (GtkCssParser *parser,
1208 GtkCssBackgroundRepeat repeat;
1211 if (!enum_parse (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT_STYLE, &style))
1214 repeat.repeat = style;
1216 g_value_set_boxed (value, &repeat);
1222 background_repeat_value_print (const GValue *value,
1225 GtkCssBackgroundRepeat *repeat;
1227 repeat = g_value_get_boxed (value);
1229 enum_print (repeat->repeat, GTK_TYPE_CSS_BACKGROUND_REPEAT_STYLE, string);
1233 border_image_repeat_value_parse (GtkCssParser *parser,
1237 GtkCssBorderImageRepeat image_repeat;
1238 GtkCssBorderRepeatStyle styles[2];
1241 for (i = 0; i < 2; i++)
1243 if (_gtk_css_parser_try (parser, "stretch", TRUE))
1244 styles[i] = GTK_CSS_REPEAT_STYLE_STRETCH;
1245 else if (_gtk_css_parser_try (parser, "repeat", TRUE))
1246 styles[i] = GTK_CSS_REPEAT_STYLE_REPEAT;
1247 else if (_gtk_css_parser_try (parser, "round", TRUE))
1248 styles[i] = GTK_CSS_REPEAT_STYLE_ROUND;
1249 else if (_gtk_css_parser_try (parser, "space", TRUE))
1250 styles[i] = GTK_CSS_REPEAT_STYLE_SPACE;
1253 styles[1] = styles[0] = GTK_CSS_REPEAT_STYLE_STRETCH;
1257 styles[i] = styles[0];
1260 image_repeat.hrepeat = styles[0];
1261 image_repeat.vrepeat = styles[1];
1263 g_value_set_boxed (value, &image_repeat);
1268 static const gchar *
1269 border_image_repeat_style_to_string (GtkCssBorderRepeatStyle repeat)
1273 case GTK_CSS_REPEAT_STYLE_STRETCH:
1275 case GTK_CSS_REPEAT_STYLE_REPEAT:
1277 case GTK_CSS_REPEAT_STYLE_ROUND:
1279 case GTK_CSS_REPEAT_STYLE_SPACE:
1287 border_image_repeat_value_print (const GValue *value,
1290 GtkCssBorderImageRepeat *image_repeat;
1292 image_repeat = g_value_get_boxed (value);
1294 g_string_append (string, border_image_repeat_style_to_string (image_repeat->hrepeat));
1295 if (image_repeat->hrepeat != image_repeat->vrepeat)
1297 g_string_append (string, " ");
1298 g_string_append (string, border_image_repeat_style_to_string (image_repeat->vrepeat));
1303 border_image_value_parse (GtkCssParser *parser,
1307 GValue temp = G_VALUE_INIT;
1308 cairo_pattern_t *pattern = NULL;
1309 gconstpointer *boxed = NULL;
1311 GtkBorder slice, *width = NULL, *parsed_slice;
1312 GtkCssBorderImageRepeat repeat, *parsed_repeat;
1313 gboolean retval = FALSE;
1314 GtkBorderImage *image = NULL;
1316 g_value_init (&temp, CAIRO_GOBJECT_TYPE_PATTERN);
1318 if (!pattern_value_parse (parser, base, &temp))
1321 boxed_type = G_VALUE_TYPE (&temp);
1322 if (boxed_type != CAIRO_GOBJECT_TYPE_PATTERN)
1323 boxed = g_value_dup_boxed (&temp);
1325 pattern = g_value_dup_boxed (&temp);
1327 g_value_unset (&temp);
1328 g_value_init (&temp, GTK_TYPE_BORDER);
1330 if (!border_value_parse (parser, base, &temp))
1333 parsed_slice = g_value_get_boxed (&temp);
1334 slice = *parsed_slice;
1336 if (_gtk_css_parser_try (parser, "/", TRUE))
1338 g_value_unset (&temp);
1339 g_value_init (&temp, GTK_TYPE_BORDER);
1341 if (!border_value_parse (parser, base, &temp))
1344 width = g_value_dup_boxed (&temp);
1347 g_value_unset (&temp);
1348 g_value_init (&temp, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
1350 if (!border_image_repeat_value_parse (parser, base, &temp))
1353 parsed_repeat = g_value_get_boxed (&temp);
1354 repeat = *parsed_repeat;
1356 g_value_unset (&temp);
1359 image = _gtk_border_image_new_for_boxed (boxed_type, boxed, &slice, width, &repeat);
1360 else if (pattern != NULL)
1361 image = _gtk_border_image_new (pattern, &slice, width, &repeat);
1366 g_value_take_boxed (value, image);
1370 if (pattern != NULL)
1371 cairo_pattern_destroy (pattern);
1374 g_boxed_free (boxed_type, boxed);
1377 gtk_border_free (width);
1383 enum_value_parse (GtkCssParser *parser,
1389 if (enum_parse (parser, G_VALUE_TYPE (value), &v))
1391 g_value_set_enum (value, v);
1399 enum_value_print (const GValue *value,
1402 enum_print (g_value_get_enum (value), G_VALUE_TYPE (value), string);
1406 flags_value_parse (GtkCssParser *parser,
1410 GFlagsClass *flags_class;
1411 GFlagsValue *flag_value;
1415 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1418 str = _gtk_css_parser_try_ident (parser, TRUE);
1421 _gtk_css_parser_error (parser, "Expected an identifier");
1422 g_type_class_unref (flags_class);
1426 flag_value = g_flags_get_value_by_nick (flags_class, str);
1429 _gtk_css_parser_error (parser,
1430 "Unknown flag value '%s' for type '%s'",
1431 str, g_type_name (G_VALUE_TYPE (value)));
1432 /* XXX Do we want to return FALSE here? We can get
1433 * forward-compatibility for new values this way
1436 g_type_class_unref (flags_class);
1442 while (_gtk_css_parser_try (parser, ",", FALSE));
1444 g_type_class_unref (flags_class);
1446 g_value_set_enum (value, flags);
1452 flags_value_print (const GValue *value,
1455 GFlagsClass *flags_class;
1458 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1459 flags = g_value_get_flags (value);
1461 for (i = 0; i < flags_class->n_values; i++)
1463 GFlagsValue *flags_value = &flags_class->values[i];
1465 if (flags & flags_value->value)
1467 if (string->len != 0)
1468 g_string_append (string, ", ");
1470 g_string_append (string, flags_value->value_nick);
1474 g_type_class_unref (flags_class);
1478 bindings_value_parse (GtkCssParser *parser,
1483 GtkBindingSet *binding_set;
1486 array = g_ptr_array_new ();
1489 name = _gtk_css_parser_try_ident (parser, TRUE);
1492 _gtk_css_parser_error (parser, "Not a valid binding name");
1493 g_ptr_array_free (array, TRUE);
1497 binding_set = gtk_binding_set_find (name);
1501 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
1506 g_ptr_array_add (array, binding_set);
1509 while (_gtk_css_parser_try (parser, ",", TRUE));
1511 g_value_take_boxed (value, array);
1517 bindings_value_print (const GValue *value,
1523 array = g_value_get_boxed (value);
1525 for (i = 0; i < array->len; i++)
1527 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
1530 g_string_append (string, ", ");
1531 g_string_append (string, binding_set->set_name);
1536 border_corner_radius_value_parse (GtkCssParser *parser,
1540 GtkCssBorderCornerRadius corner;
1542 if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
1544 _gtk_css_parser_error (parser, "Expected a number");
1547 else if (corner.horizontal < 0)
1550 if (!_gtk_css_parser_try_double (parser, &corner.vertical))
1551 corner.vertical = corner.horizontal;
1552 else if (corner.vertical < 0)
1555 g_value_set_boxed (value, &corner);
1559 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1564 border_corner_radius_value_print (const GValue *value,
1567 GtkCssBorderCornerRadius *corner;
1569 corner = g_value_get_boxed (value);
1573 g_string_append (string, "none");
1577 string_append_double (string, corner->horizontal);
1578 if (corner->horizontal != corner->vertical)
1580 g_string_append_c (string, ' ');
1581 string_append_double (string, corner->vertical);
1586 border_radius_value_parse (GtkCssParser *parser,
1590 GtkCssBorderRadius border;
1592 if (!_gtk_css_parser_try_double (parser, &border.top_left.horizontal))
1594 _gtk_css_parser_error (parser, "Expected a number");
1597 else if (border.top_left.horizontal < 0)
1600 if (_gtk_css_parser_try_double (parser, &border.top_right.horizontal))
1602 if (border.top_right.horizontal < 0)
1604 if (_gtk_css_parser_try_double (parser, &border.bottom_right.horizontal))
1606 if (border.bottom_right.horizontal < 0)
1608 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.horizontal))
1609 border.bottom_left.horizontal = border.top_right.horizontal;
1610 else if (border.bottom_left.horizontal < 0)
1615 border.bottom_right.horizontal = border.top_left.horizontal;
1616 border.bottom_left.horizontal = border.top_right.horizontal;
1621 border.top_right.horizontal = border.top_left.horizontal;
1622 border.bottom_right.horizontal = border.top_left.horizontal;
1623 border.bottom_left.horizontal = border.top_left.horizontal;
1626 if (_gtk_css_parser_try (parser, "/", TRUE))
1628 if (!_gtk_css_parser_try_double (parser, &border.top_left.vertical))
1630 _gtk_css_parser_error (parser, "Expected a number");
1633 else if (border.top_left.vertical < 0)
1636 if (_gtk_css_parser_try_double (parser, &border.top_right.vertical))
1638 if (border.top_right.vertical < 0)
1640 if (_gtk_css_parser_try_double (parser, &border.bottom_right.vertical))
1642 if (border.bottom_right.vertical < 0)
1644 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.vertical))
1645 border.bottom_left.vertical = border.top_right.vertical;
1646 else if (border.bottom_left.vertical < 0)
1651 border.bottom_right.vertical = border.top_left.vertical;
1652 border.bottom_left.vertical = border.top_right.vertical;
1657 border.top_right.vertical = border.top_left.vertical;
1658 border.bottom_right.vertical = border.top_left.vertical;
1659 border.bottom_left.vertical = border.top_left.vertical;
1664 border.top_left.vertical = border.top_left.horizontal;
1665 border.top_right.vertical = border.top_right.horizontal;
1666 border.bottom_right.vertical = border.bottom_right.horizontal;
1667 border.bottom_left.vertical = border.bottom_left.horizontal;
1670 /* border-radius is an int property for backwards-compat reasons */
1671 g_value_unset (value);
1672 g_value_init (value, GTK_TYPE_CSS_BORDER_RADIUS);
1673 g_value_set_boxed (value, &border);
1678 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1683 border_radius_value_print (const GValue *value,
1686 GtkCssBorderRadius *border;
1688 border = g_value_get_boxed (value);
1692 g_string_append (string, "none");
1696 string_append_double (string, border->top_left.horizontal);
1697 if (border->top_left.horizontal != border->top_right.horizontal ||
1698 border->top_left.horizontal != border->bottom_right.horizontal ||
1699 border->top_left.horizontal != border->bottom_left.horizontal)
1701 g_string_append_c (string, ' ');
1702 string_append_double (string, border->top_right.horizontal);
1703 if (border->top_left.horizontal != border->bottom_right.horizontal ||
1704 border->top_right.horizontal != border->bottom_left.horizontal)
1706 g_string_append_c (string, ' ');
1707 string_append_double (string, border->bottom_right.horizontal);
1708 if (border->top_right.horizontal != border->bottom_left.horizontal)
1710 g_string_append_c (string, ' ');
1711 string_append_double (string, border->bottom_left.horizontal);
1716 if (border->top_left.horizontal != border->top_left.vertical ||
1717 border->top_right.horizontal != border->top_right.vertical ||
1718 border->bottom_right.horizontal != border->bottom_right.vertical ||
1719 border->bottom_left.horizontal != border->bottom_left.vertical)
1721 g_string_append (string, " / ");
1722 string_append_double (string, border->top_left.vertical);
1723 if (border->top_left.vertical != border->top_right.vertical ||
1724 border->top_left.vertical != border->bottom_right.vertical ||
1725 border->top_left.vertical != border->bottom_left.vertical)
1727 g_string_append_c (string, ' ');
1728 string_append_double (string, border->top_right.vertical);
1729 if (border->top_left.vertical != border->bottom_right.vertical ||
1730 border->top_right.vertical != border->bottom_left.vertical)
1732 g_string_append_c (string, ' ');
1733 string_append_double (string, border->bottom_right.vertical);
1734 if (border->top_right.vertical != border->bottom_left.vertical)
1736 g_string_append_c (string, ' ');
1737 string_append_double (string, border->bottom_left.vertical);
1746 transparent_color_value_parse (GtkCssParser *parser,
1750 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1752 GdkRGBA transparent = { 0, 0, 0, 0 };
1754 g_value_set_boxed (value, &transparent);
1759 return rgba_value_parse (parser, base, value);
1763 border_color_shorthand_value_parse (GtkCssParser *parser,
1767 GtkSymbolicColor *symbolic;
1770 array = g_ptr_array_new_with_free_func ((GDestroyNotify) gtk_symbolic_color_unref);
1774 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1776 GdkRGBA transparent = { 0, 0, 0, 0 };
1778 symbolic = gtk_symbolic_color_new_literal (&transparent);
1782 symbolic = _gtk_css_parser_read_symbolic_color (parser);
1784 if (symbolic == NULL)
1788 g_ptr_array_add (array, symbolic);
1790 while (array->len < 4 &&
1791 !_gtk_css_parser_is_eof (parser) &&
1792 !_gtk_css_parser_begins_with (parser, ';') &&
1793 !_gtk_css_parser_begins_with (parser, '}'));
1798 g_assert_not_reached ();
1801 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1804 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1807 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 1)));
1813 g_value_unset (value);
1814 g_value_init (value, G_TYPE_PTR_ARRAY);
1815 g_value_take_boxed (value, array);
1823 unpack_border (const GValue *value,
1830 GParameter *parameter = g_new0 (GParameter, 4);
1831 GtkBorder *border = g_value_get_boxed (value);
1833 parameter[0].name = top;
1834 g_value_init (¶meter[0].value, G_TYPE_INT);
1835 g_value_set_int (¶meter[0].value, border->top);
1836 parameter[1].name = left;
1837 g_value_init (¶meter[1].value, G_TYPE_INT);
1838 g_value_set_int (¶meter[1].value, border->left);
1839 parameter[2].name = bottom;
1840 g_value_init (¶meter[2].value, G_TYPE_INT);
1841 g_value_set_int (¶meter[2].value, border->bottom);
1842 parameter[3].name = right;
1843 g_value_init (¶meter[3].value, G_TYPE_INT);
1844 g_value_set_int (¶meter[3].value, border->right);
1851 pack_border (GValue *value,
1852 GtkStyleProperties *props,
1853 GtkStateFlags state,
1862 gtk_style_properties_get (props,
1875 g_value_set_boxed (value, &border);
1879 unpack_border_width (const GValue *value,
1882 return unpack_border (value, n_params,
1883 "border-top-width", "border-left-width",
1884 "border-bottom-width", "border-right-width");
1888 pack_border_width (GValue *value,
1889 GtkStyleProperties *props,
1890 GtkStateFlags state,
1891 GtkStylePropertyContext *context)
1893 pack_border (value, props, state,
1894 "border-top-width", "border-left-width",
1895 "border-bottom-width", "border-right-width");
1899 unpack_padding (const GValue *value,
1902 return unpack_border (value, n_params,
1903 "padding-top", "padding-left",
1904 "padding-bottom", "padding-right");
1908 pack_padding (GValue *value,
1909 GtkStyleProperties *props,
1910 GtkStateFlags state,
1911 GtkStylePropertyContext *context)
1913 pack_border (value, props, state,
1914 "padding-top", "padding-left",
1915 "padding-bottom", "padding-right");
1919 unpack_margin (const GValue *value,
1922 return unpack_border (value, n_params,
1923 "margin-top", "margin-left",
1924 "margin-bottom", "margin-right");
1928 pack_margin (GValue *value,
1929 GtkStyleProperties *props,
1930 GtkStateFlags state,
1931 GtkStylePropertyContext *context)
1933 pack_border (value, props, state,
1934 "margin-top", "margin-left",
1935 "margin-bottom", "margin-right");
1939 unpack_border_radius (const GValue *value,
1942 GParameter *parameter = g_new0 (GParameter, 4);
1943 GtkCssBorderRadius *border;
1945 if (G_VALUE_HOLDS_BOXED (value))
1946 border = g_value_get_boxed (value);
1950 parameter[0].name = "border-top-left-radius";
1951 g_value_init (¶meter[0].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1952 parameter[1].name = "border-top-right-radius";
1953 g_value_init (¶meter[1].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1954 parameter[2].name = "border-bottom-right-radius";
1955 g_value_init (¶meter[2].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1956 parameter[3].name = "border-bottom-left-radius";
1957 g_value_init (¶meter[3].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1960 g_value_set_boxed (¶meter[0].value, &border->top_left);
1961 g_value_set_boxed (¶meter[1].value, &border->top_right);
1962 g_value_set_boxed (¶meter[2].value, &border->bottom_right);
1963 g_value_set_boxed (¶meter[3].value, &border->bottom_left);
1971 pack_border_radius (GValue *value,
1972 GtkStyleProperties *props,
1973 GtkStateFlags state,
1974 GtkStylePropertyContext *context)
1976 GtkCssBorderCornerRadius *top_left;
1978 /* NB: We are an int property, so we have to resolve to an int here.
1979 * So we just resolve to an int. We pick one and stick to it.
1980 * Lesson learned: Don't query border-radius shorthand, query the
1981 * real properties instead. */
1982 gtk_style_properties_get (props,
1984 "border-top-left-radius", &top_left,
1988 g_value_set_int (value, top_left->horizontal);
1994 unpack_font_description (const GValue *value,
1997 GParameter *parameter = g_new0 (GParameter, 5);
1998 PangoFontDescription *description;
2002 /* For backwards compat, we only unpack values that are indeed set.
2003 * For strict CSS conformance we need to unpack all of them.
2004 * Note that we do set all of them in the parse function, so it
2005 * will not have effects when parsing CSS files. It will though
2006 * for custom style providers.
2009 description = g_value_get_boxed (value);
2013 mask = pango_font_description_get_set_fields (description);
2017 if (mask & PANGO_FONT_MASK_FAMILY)
2019 GPtrArray *strv = g_ptr_array_new ();
2021 g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
2022 g_ptr_array_add (strv, NULL);
2023 parameter[n].name = "font-family";
2024 g_value_init (¶meter[n].value, G_TYPE_STRV);
2025 g_value_take_boxed (¶meter[n].value,
2026 g_ptr_array_free (strv, FALSE));
2030 if (mask & PANGO_FONT_MASK_STYLE)
2032 parameter[n].name = "font-style";
2033 g_value_init (¶meter[n].value, PANGO_TYPE_STYLE);
2034 g_value_set_enum (¶meter[n].value,
2035 pango_font_description_get_style (description));
2039 if (mask & PANGO_FONT_MASK_VARIANT)
2041 parameter[n].name = "font-variant";
2042 g_value_init (¶meter[n].value, PANGO_TYPE_VARIANT);
2043 g_value_set_enum (¶meter[n].value,
2044 pango_font_description_get_variant (description));
2048 if (mask & PANGO_FONT_MASK_WEIGHT)
2050 parameter[n].name = "font-weight";
2051 g_value_init (¶meter[n].value, PANGO_TYPE_WEIGHT);
2052 g_value_set_enum (¶meter[n].value,
2053 pango_font_description_get_weight (description));
2057 if (mask & PANGO_FONT_MASK_SIZE)
2059 parameter[n].name = "font-size";
2060 g_value_init (¶meter[n].value, G_TYPE_DOUBLE);
2061 g_value_set_double (¶meter[n].value,
2062 (double) pango_font_description_get_size (description) / PANGO_SCALE);
2072 pack_font_description (GValue *value,
2073 GtkStyleProperties *props,
2074 GtkStateFlags state,
2075 GtkStylePropertyContext *context)
2077 PangoFontDescription *description;
2080 PangoVariant variant;
2084 gtk_style_properties_get (props,
2086 "font-family", &families,
2087 "font-style", &style,
2088 "font-variant", &variant,
2089 "font-weight", &weight,
2093 description = pango_font_description_new ();
2094 /* xxx: Can we set all the families here somehow? */
2096 pango_font_description_set_family (description, families[0]);
2097 pango_font_description_set_size (description, round (size * PANGO_SCALE));
2098 pango_font_description_set_style (description, style);
2099 pango_font_description_set_variant (description, variant);
2100 pango_font_description_set_weight (description, weight);
2102 g_strfreev (families);
2104 g_value_take_boxed (value, description);
2108 unpack_border_color (const GValue *value,
2111 GParameter *parameter = g_new0 (GParameter, 4);
2114 type = G_VALUE_TYPE (value);
2115 if (type == G_TYPE_PTR_ARRAY)
2116 type = GTK_TYPE_SYMBOLIC_COLOR;
2118 parameter[0].name = "border-top-color";
2119 g_value_init (¶meter[0].value, type);
2120 parameter[1].name = "border-right-color";
2121 g_value_init (¶meter[1].value, type);
2122 parameter[2].name = "border-bottom-color";
2123 g_value_init (¶meter[2].value, type);
2124 parameter[3].name = "border-left-color";
2125 g_value_init (¶meter[3].value, type);
2127 if (G_VALUE_TYPE (value) == G_TYPE_PTR_ARRAY)
2129 GPtrArray *array = g_value_get_boxed (value);
2132 for (i = 0; i < 4; i++)
2133 g_value_set_boxed (¶meter[i].value, g_ptr_array_index (array, i));
2137 /* can be RGBA or symbolic color */
2138 gpointer p = g_value_get_boxed (value);
2140 g_value_set_boxed (¶meter[0].value, p);
2141 g_value_set_boxed (¶meter[1].value, p);
2142 g_value_set_boxed (¶meter[2].value, p);
2143 g_value_set_boxed (¶meter[3].value, p);
2151 pack_border_color (GValue *value,
2152 GtkStyleProperties *props,
2153 GtkStateFlags state,
2154 GtkStylePropertyContext *context)
2156 /* NB: We are a color property, so we have to resolve to a color here.
2157 * So we just resolve to a color. We pick one and stick to it.
2158 * Lesson learned: Don't query border-color shorthand, query the
2159 * real properties instead. */
2160 g_value_unset (value);
2161 gtk_style_properties_get_property (props, "border-top-color", state, value);
2164 /*** UNSET FUNCS ***/
2167 unset_font_description (GtkStyleProperties *props,
2168 GtkStateFlags state)
2170 gtk_style_properties_unset_property (props, "font-family", state);
2171 gtk_style_properties_unset_property (props, "font-style", state);
2172 gtk_style_properties_unset_property (props, "font-variant", state);
2173 gtk_style_properties_unset_property (props, "font-weight", state);
2174 gtk_style_properties_unset_property (props, "font-size", state);
2178 unset_margin (GtkStyleProperties *props,
2179 GtkStateFlags state)
2181 gtk_style_properties_unset_property (props, "margin-top", state);
2182 gtk_style_properties_unset_property (props, "margin-right", state);
2183 gtk_style_properties_unset_property (props, "margin-bottom", state);
2184 gtk_style_properties_unset_property (props, "margin-left", state);
2188 unset_padding (GtkStyleProperties *props,
2189 GtkStateFlags state)
2191 gtk_style_properties_unset_property (props, "padding-top", state);
2192 gtk_style_properties_unset_property (props, "padding-right", state);
2193 gtk_style_properties_unset_property (props, "padding-bottom", state);
2194 gtk_style_properties_unset_property (props, "padding-left", state);
2198 unset_border_width (GtkStyleProperties *props,
2199 GtkStateFlags state)
2201 gtk_style_properties_unset_property (props, "border-top-width", state);
2202 gtk_style_properties_unset_property (props, "border-right-width", state);
2203 gtk_style_properties_unset_property (props, "border-bottom-width", state);
2204 gtk_style_properties_unset_property (props, "border-left-width", state);
2208 unset_border_radius (GtkStyleProperties *props,
2209 GtkStateFlags state)
2211 gtk_style_properties_unset_property (props, "border-top-right-radius", state);
2212 gtk_style_properties_unset_property (props, "border-bottom-right-radius", state);
2213 gtk_style_properties_unset_property (props, "border-bottom-left-radius", state);
2214 gtk_style_properties_unset_property (props, "border-top-left-radius", state);
2218 unset_border_color (GtkStyleProperties *props,
2219 GtkStateFlags state)
2221 gtk_style_properties_unset_property (props, "border-top-color", state);
2222 gtk_style_properties_unset_property (props, "border-right-color", state);
2223 gtk_style_properties_unset_property (props, "border-bottom-color", state);
2224 gtk_style_properties_unset_property (props, "border-left-color", state);
2228 unset_border_image (GtkStyleProperties *props,
2229 GtkStateFlags state)
2231 gtk_style_properties_unset_property (props, "border-image-source", state);
2232 gtk_style_properties_unset_property (props, "border-image-slice", state);
2233 gtk_style_properties_unset_property (props, "border-image-repeat", state);
2234 gtk_style_properties_unset_property (props, "border-image-width", state);
2237 /*** default values ***/
2240 border_image_width_default_value (GtkStyleProperties *props,
2241 GtkStateFlags state,
2247 background_color_default_value (GtkStyleProperties *props,
2248 GtkStateFlags state,
2251 GdkRGBA transparent_black = { 0, 0, 0, 0 };
2253 g_value_set_boxed (value, &transparent_black);
2257 border_color_default_value (GtkStyleProperties *props,
2258 GtkStateFlags state,
2261 g_value_unset (value);
2262 gtk_style_properties_get_property (props, "color", state, value);
2268 css_string_funcs_init (void)
2270 if (G_LIKELY (parse_funcs != NULL))
2273 parse_funcs = g_hash_table_new (NULL, NULL);
2274 print_funcs = g_hash_table_new (NULL, NULL);
2276 register_conversion_function (GDK_TYPE_RGBA,
2279 register_conversion_function (GDK_TYPE_COLOR,
2282 register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
2283 symbolic_color_value_parse,
2284 symbolic_color_value_print);
2285 register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
2286 font_description_value_parse,
2287 font_description_value_print);
2288 register_conversion_function (G_TYPE_BOOLEAN,
2289 boolean_value_parse,
2290 boolean_value_print);
2291 register_conversion_function (G_TYPE_INT,
2294 register_conversion_function (G_TYPE_UINT,
2297 register_conversion_function (G_TYPE_DOUBLE,
2299 double_value_print);
2300 register_conversion_function (G_TYPE_FLOAT,
2303 register_conversion_function (G_TYPE_STRING,
2305 string_value_print);
2306 register_conversion_function (GTK_TYPE_THEMING_ENGINE,
2307 theming_engine_value_parse,
2308 theming_engine_value_print);
2309 register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
2310 animation_description_value_parse,
2311 animation_description_value_print);
2312 register_conversion_function (GTK_TYPE_BORDER,
2314 border_value_print);
2315 register_conversion_function (GTK_TYPE_GRADIENT,
2316 gradient_value_parse,
2317 gradient_value_print);
2318 register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
2319 pattern_value_parse,
2320 pattern_value_print);
2321 register_conversion_function (GTK_TYPE_BORDER_IMAGE,
2322 border_image_value_parse,
2324 register_conversion_function (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
2325 border_image_repeat_value_parse,
2326 border_image_repeat_value_print);
2327 register_conversion_function (GTK_TYPE_SHADOW,
2329 shadow_value_print);
2330 register_conversion_function (G_TYPE_ENUM,
2333 register_conversion_function (G_TYPE_FLAGS,
2336 register_conversion_function (GTK_TYPE_CSS_BACKGROUND_REPEAT,
2337 background_repeat_value_parse,
2338 background_repeat_value_print);
2342 _gtk_style_property_parse_value (const GtkStyleProperty *property,
2344 GtkCssParser *parser,
2347 GtkStyleParseFunc func;
2349 g_return_val_if_fail (value != NULL, FALSE);
2350 g_return_val_if_fail (parser != NULL, FALSE);
2352 css_string_funcs_init ();
2356 if (_gtk_css_parser_try (parser, "none", TRUE))
2358 /* Insert the default value, so it has an opportunity
2359 * to override other style providers when merged
2361 g_param_value_set_default (property->pspec, value);
2364 else if (property->property_parse_func)
2366 GError *error = NULL;
2370 value_str = _gtk_css_parser_read_value (parser);
2371 if (value_str == NULL)
2374 success = (*property->property_parse_func) (value_str, value, &error);
2381 func = property->parse_func;
2387 func = g_hash_table_lookup (parse_funcs,
2388 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2390 func = g_hash_table_lookup (parse_funcs,
2391 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2395 _gtk_css_parser_error (parser,
2396 "Cannot convert to type '%s'",
2397 g_type_name (G_VALUE_TYPE (value)));
2401 return (*func) (parser, base, value);
2405 _gtk_style_property_print_value (const GtkStyleProperty *property,
2406 const GValue *value,
2409 GtkStylePrintFunc func;
2411 css_string_funcs_init ();
2414 func = property->print_func;
2419 func = g_hash_table_lookup (print_funcs,
2420 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2422 func = g_hash_table_lookup (print_funcs,
2423 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2427 char *s = g_strdup_value_contents (value);
2428 g_string_append (string, s);
2433 func (value, string);
2437 _gtk_style_property_default_value (const GtkStyleProperty *property,
2438 GtkStyleProperties *properties,
2439 GtkStateFlags state,
2442 if (property->default_value_func)
2443 property->default_value_func (properties, state, value);
2444 else if (property->pspec->value_type == GTK_TYPE_THEMING_ENGINE)
2445 g_value_set_object (value, gtk_theming_engine_load (NULL));
2446 else if (property->pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
2447 g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
2448 else if (property->pspec->value_type == GDK_TYPE_RGBA)
2451 gdk_rgba_parse (&color, "pink");
2452 g_value_set_boxed (value, &color);
2454 else if (property->pspec->value_type == GTK_TYPE_BORDER)
2456 g_value_take_boxed (value, gtk_border_new ());
2459 g_param_value_set_default (property->pspec, value);
2463 _gtk_style_property_is_inherit (const GtkStyleProperty *property)
2465 g_return_val_if_fail (property != NULL, FALSE);
2467 return property->flags & GTK_STYLE_PROPERTY_INHERIT ? TRUE : FALSE;
2471 resolve_color (GtkStyleProperties *props,
2476 /* Resolve symbolic color to GdkRGBA */
2477 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
2480 /* Store it back, this is where GdkRGBA caching happens */
2481 g_value_unset (value);
2482 g_value_init (value, GDK_TYPE_RGBA);
2483 g_value_set_boxed (value, &color);
2489 resolve_color_rgb (GtkStyleProperties *props,
2492 GdkColor color = { 0 };
2495 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
2498 color.red = rgba.red * 65535. + 0.5;
2499 color.green = rgba.green * 65535. + 0.5;
2500 color.blue = rgba.blue * 65535. + 0.5;
2502 g_value_unset (value);
2503 g_value_init (value, GDK_TYPE_COLOR);
2504 g_value_set_boxed (value, &color);
2510 resolve_win32_theme_part (GtkStyleProperties *props,
2513 GtkStylePropertyContext *context)
2515 GtkWin32ThemePart *part;
2516 cairo_pattern_t *pattern;
2518 part = g_value_get_boxed (value);
2522 pattern = _gtk_win32_theme_part_render (part, context->width, context->height);
2524 g_value_take_boxed (value_out, pattern);
2531 resolve_gradient (GtkStyleProperties *props,
2534 cairo_pattern_t *gradient;
2536 if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
2539 /* Store it back, this is where cairo_pattern_t caching happens */
2540 g_value_unset (value);
2541 g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
2542 g_value_take_boxed (value, gradient);
2548 resolve_shadow (GtkStyleProperties *props,
2551 GtkShadow *resolved, *base;
2553 base = g_value_get_boxed (value);
2558 if (_gtk_shadow_get_resolved (base))
2561 resolved = _gtk_shadow_resolve (base, props);
2562 if (resolved == NULL)
2565 g_value_take_boxed (value, resolved);
2571 _gtk_style_property_resolve (const GtkStyleProperty *property,
2572 GtkStyleProperties *props,
2573 GtkStateFlags state,
2574 GtkStylePropertyContext *context,
2578 if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
2580 if (property->pspec->value_type == GDK_TYPE_RGBA)
2582 if (resolve_color (props, val))
2585 else if (property->pspec->value_type == GDK_TYPE_COLOR)
2587 if (resolve_color_rgb (props, val))
2591 g_value_unset (val);
2592 g_value_init (val, property->pspec->value_type);
2593 _gtk_style_property_default_value (property, props, state, val);
2595 else if (G_VALUE_TYPE (val) == GDK_TYPE_RGBA)
2597 if (g_value_get_boxed (val) == NULL)
2598 _gtk_style_property_default_value (property, props, state, val);
2600 else if (G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
2602 g_return_if_fail (property->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN);
2604 if (!resolve_gradient (props, val))
2606 g_value_unset (val);
2607 g_value_init (val, CAIRO_GOBJECT_TYPE_PATTERN);
2608 _gtk_style_property_default_value (property, props, state, val);
2611 else if (G_VALUE_TYPE (val) == GTK_TYPE_SHADOW)
2613 if (!resolve_shadow (props, val))
2614 _gtk_style_property_default_value (property, props, state, val);
2616 else if (G_VALUE_TYPE (val) == GTK_TYPE_WIN32_THEME_PART)
2618 if (resolve_win32_theme_part (props, val, val_out, context))
2619 return; /* Don't copy val, this sets val_out */
2620 _gtk_style_property_default_value (property, props, state, val);
2624 g_value_copy (val, val_out);
2628 _gtk_style_property_is_shorthand (const GtkStyleProperty *property)
2630 g_return_val_if_fail (property != NULL, FALSE);
2632 return property->pack_func != NULL;
2636 _gtk_style_property_unpack (const GtkStyleProperty *property,
2637 const GValue *value,
2640 g_return_val_if_fail (property != NULL, NULL);
2641 g_return_val_if_fail (property->unpack_func != NULL, NULL);
2642 g_return_val_if_fail (value != NULL, NULL);
2643 g_return_val_if_fail (n_params != NULL, NULL);
2645 return property->unpack_func (value, n_params);
2649 _gtk_style_property_pack (const GtkStyleProperty *property,
2650 GtkStyleProperties *props,
2651 GtkStateFlags state,
2652 GtkStylePropertyContext *context,
2655 g_return_if_fail (property != NULL);
2656 g_return_if_fail (property->pack_func != NULL);
2657 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
2658 g_return_if_fail (G_IS_VALUE (value));
2660 property->pack_func (value, props, state, context);
2664 gtk_style_property_init (void)
2666 if (G_LIKELY (properties))
2669 /* stuff is never freed, so no need for free functions */
2670 properties = g_hash_table_new (g_str_hash, g_str_equal);
2672 /* note that gtk_style_properties_register_property() calls this function,
2673 * so make sure we're sanely inited to avoid infloops */
2675 _gtk_style_property_register (g_param_spec_boxed ("color",
2679 GTK_STYLE_PROPERTY_INHERIT,
2687 _gtk_style_property_register (g_param_spec_boxed ("background-color",
2695 transparent_color_value_parse,
2697 background_color_default_value,
2700 _gtk_style_property_register (g_param_spec_boxed ("font-family",
2704 GTK_STYLE_PROPERTY_INHERIT,
2709 font_family_value_print,
2712 _gtk_style_property_register (g_param_spec_enum ("font-style",
2716 PANGO_STYLE_NORMAL, 0),
2717 GTK_STYLE_PROPERTY_INHERIT,
2725 _gtk_style_property_register (g_param_spec_enum ("font-variant",
2729 PANGO_VARIANT_NORMAL, 0),
2730 GTK_STYLE_PROPERTY_INHERIT,
2738 /* xxx: need to parse this properly, ie parse the numbers */
2739 _gtk_style_property_register (g_param_spec_enum ("font-weight",
2743 PANGO_WEIGHT_NORMAL, 0),
2744 GTK_STYLE_PROPERTY_INHERIT,
2752 _gtk_style_property_register (g_param_spec_double ("font-size",
2755 0, G_MAXDOUBLE, 0, 0),
2756 GTK_STYLE_PROPERTY_INHERIT,
2764 _gtk_style_property_register (g_param_spec_boxed ("font",
2767 PANGO_TYPE_FONT_DESCRIPTION, 0),
2768 GTK_STYLE_PROPERTY_INHERIT,
2770 unpack_font_description,
2771 pack_font_description,
2772 font_description_value_parse,
2773 font_description_value_print,
2775 unset_font_description);
2777 _gtk_style_property_register (g_param_spec_boxed ("text-shadow",
2780 GTK_TYPE_SHADOW, 0),
2781 GTK_STYLE_PROPERTY_INHERIT,
2790 _gtk_style_property_register (g_param_spec_boxed ("icon-shadow",
2793 GTK_TYPE_SHADOW, 0),
2794 GTK_STYLE_PROPERTY_INHERIT,
2803 gtk_style_properties_register_property (NULL,
2804 g_param_spec_boxed ("box-shadow",
2807 GTK_TYPE_SHADOW, 0));
2808 gtk_style_properties_register_property (NULL,
2809 g_param_spec_int ("margin-top",
2812 0, G_MAXINT, 0, 0));
2813 gtk_style_properties_register_property (NULL,
2814 g_param_spec_int ("margin-left",
2817 0, G_MAXINT, 0, 0));
2818 gtk_style_properties_register_property (NULL,
2819 g_param_spec_int ("margin-bottom",
2822 0, G_MAXINT, 0, 0));
2823 gtk_style_properties_register_property (NULL,
2824 g_param_spec_int ("margin-right",
2827 0, G_MAXINT, 0, 0));
2828 _gtk_style_property_register (g_param_spec_boxed ("margin",
2831 GTK_TYPE_BORDER, 0),
2840 gtk_style_properties_register_property (NULL,
2841 g_param_spec_int ("padding-top",
2844 0, G_MAXINT, 0, 0));
2845 gtk_style_properties_register_property (NULL,
2846 g_param_spec_int ("padding-left",
2849 0, G_MAXINT, 0, 0));
2850 gtk_style_properties_register_property (NULL,
2851 g_param_spec_int ("padding-bottom",
2853 "Padding at bottom",
2854 0, G_MAXINT, 0, 0));
2855 gtk_style_properties_register_property (NULL,
2856 g_param_spec_int ("padding-right",
2859 0, G_MAXINT, 0, 0));
2860 _gtk_style_property_register (g_param_spec_boxed ("padding",
2863 GTK_TYPE_BORDER, 0),
2872 gtk_style_properties_register_property (NULL,
2873 g_param_spec_int ("border-top-width",
2875 "Border width at top",
2876 0, G_MAXINT, 0, 0));
2877 gtk_style_properties_register_property (NULL,
2878 g_param_spec_int ("border-left-width",
2879 "border left width",
2880 "Border width at left",
2881 0, G_MAXINT, 0, 0));
2882 gtk_style_properties_register_property (NULL,
2883 g_param_spec_int ("border-bottom-width",
2884 "border bottom width",
2885 "Border width at bottom",
2886 0, G_MAXINT, 0, 0));
2887 gtk_style_properties_register_property (NULL,
2888 g_param_spec_int ("border-right-width",
2889 "border right width",
2890 "Border width at right",
2891 0, G_MAXINT, 0, 0));
2892 _gtk_style_property_register (g_param_spec_boxed ("border-width",
2894 "Border width, in pixels",
2895 GTK_TYPE_BORDER, 0),
2898 unpack_border_width,
2903 unset_border_width);
2905 _gtk_style_property_register (g_param_spec_boxed ("border-top-left-radius",
2906 "Border top left radius",
2907 "Border radius of top left corner, in pixels",
2908 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2913 border_corner_radius_value_parse,
2914 border_corner_radius_value_print,
2917 _gtk_style_property_register (g_param_spec_boxed ("border-top-right-radius",
2918 "Border top right radius",
2919 "Border radius of top right corner, in pixels",
2920 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2925 border_corner_radius_value_parse,
2926 border_corner_radius_value_print,
2929 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-right-radius",
2930 "Border bottom right radius",
2931 "Border radius of bottom right corner, in pixels",
2932 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2937 border_corner_radius_value_parse,
2938 border_corner_radius_value_print,
2941 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-left-radius",
2942 "Border bottom left radius",
2943 "Border radius of bottom left corner, in pixels",
2944 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2949 border_corner_radius_value_parse,
2950 border_corner_radius_value_print,
2953 _gtk_style_property_register (g_param_spec_int ("border-radius",
2955 "Border radius, in pixels",
2959 unpack_border_radius,
2961 border_radius_value_parse,
2962 border_radius_value_print,
2964 unset_border_radius);
2966 gtk_style_properties_register_property (NULL,
2967 g_param_spec_enum ("border-style",
2970 GTK_TYPE_BORDER_STYLE,
2971 GTK_BORDER_STYLE_NONE, 0));
2972 _gtk_style_property_register (g_param_spec_boxed ("border-top-color",
2980 transparent_color_value_parse,
2982 border_color_default_value,
2984 _gtk_style_property_register (g_param_spec_boxed ("border-right-color",
2985 "Border right color",
2986 "Border right color",
2992 transparent_color_value_parse,
2994 border_color_default_value,
2996 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-color",
2997 "Border bottom color",
2998 "Border bottom color",
3004 transparent_color_value_parse,
3006 border_color_default_value,
3008 _gtk_style_property_register (g_param_spec_boxed ("border-left-color",
3009 "Border left color",
3010 "Border left color",
3016 transparent_color_value_parse,
3018 border_color_default_value,
3020 _gtk_style_property_register (g_param_spec_boxed ("border-color",
3026 unpack_border_color,
3028 border_color_shorthand_value_parse,
3031 unset_border_color);
3033 gtk_style_properties_register_property (NULL,
3034 g_param_spec_boxed ("background-image",
3037 CAIRO_GOBJECT_TYPE_PATTERN, 0));
3038 gtk_style_properties_register_property (NULL,
3039 g_param_spec_boxed ("background-repeat",
3040 "Background repeat",
3041 "Background repeat",
3042 GTK_TYPE_CSS_BACKGROUND_REPEAT, 0));
3044 gtk_style_properties_register_property (NULL,
3045 g_param_spec_boxed ("border-image-source",
3046 "Border image source",
3047 "Border image source",
3048 CAIRO_GOBJECT_TYPE_PATTERN, 0));
3049 gtk_style_properties_register_property (NULL,
3050 g_param_spec_boxed ("border-image-repeat",
3051 "Border image repeat",
3052 "Border image repeat",
3053 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
3054 gtk_style_properties_register_property (NULL,
3055 g_param_spec_boxed ("border-image-slice",
3056 "Border image slice",
3057 "Border image slice",
3058 GTK_TYPE_BORDER, 0));
3059 _gtk_style_property_register (g_param_spec_boxed ("border-image-width",
3060 "Border image width",
3061 "Border image width",
3062 GTK_TYPE_BORDER, 0),
3069 border_image_width_default_value,
3071 _gtk_style_property_register (g_param_spec_boxed ("border-image",
3074 GTK_TYPE_BORDER_IMAGE, 0),
3077 _gtk_border_image_unpack,
3078 _gtk_border_image_pack,
3082 unset_border_image);
3083 gtk_style_properties_register_property (NULL,
3084 g_param_spec_object ("engine",
3087 GTK_TYPE_THEMING_ENGINE, 0));
3088 gtk_style_properties_register_property (NULL,
3089 g_param_spec_boxed ("transition",
3090 "Transition animation description",
3091 "Transition animation description",
3092 GTK_TYPE_ANIMATION_DESCRIPTION, 0));
3094 /* Private property holding the binding sets */
3095 _gtk_style_property_register (g_param_spec_boxed ("gtk-key-bindings",
3098 G_TYPE_PTR_ARRAY, 0),
3103 bindings_value_parse,
3104 bindings_value_print,
3109 const GtkStyleProperty *
3110 _gtk_style_property_lookup (const char *name)
3112 gtk_style_property_init ();
3114 return g_hash_table_lookup (properties, name);
3118 _gtk_style_property_register (GParamSpec *pspec,
3119 GtkStylePropertyFlags flags,
3120 GtkStylePropertyParser property_parse_func,
3121 GtkStyleUnpackFunc unpack_func,
3122 GtkStylePackFunc pack_func,
3123 GtkStyleParseFunc parse_func,
3124 GtkStylePrintFunc print_func,
3125 GtkStyleDefaultValueFunc default_value_func,
3126 GtkStyleUnsetFunc unset_func)
3128 const GtkStyleProperty *existing;
3129 GtkStyleProperty *node;
3131 g_return_if_fail ((pack_func == NULL) == (unpack_func == NULL));
3133 gtk_style_property_init ();
3135 existing = _gtk_style_property_lookup (pspec->name);
3136 if (existing != NULL)
3138 g_warning ("Property \"%s\" was already registered with type %s",
3139 pspec->name, g_type_name (existing->pspec->value_type));
3143 node = g_slice_new0 (GtkStyleProperty);
3144 node->flags = flags;
3145 node->pspec = pspec;
3146 node->property_parse_func = property_parse_func;
3147 node->pack_func = pack_func;
3148 node->unpack_func = unpack_func;
3149 node->parse_func = parse_func;
3150 node->print_func = print_func;
3151 node->default_value_func = default_value_func;
3152 node->unset_func = unset_func;
3154 /* pspec owns name */
3155 g_hash_table_insert (properties, (gchar *)pspec->name, node);