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_enum (parser, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, &v))
1247 styles[1] = styles[0] = GTK_CSS_REPEAT_STYLE_STRETCH;
1251 styles[i] = styles[0];
1254 image_repeat.hrepeat = styles[0];
1255 image_repeat.vrepeat = styles[1];
1257 g_value_set_boxed (value, &image_repeat);
1263 border_image_repeat_value_print (const GValue *value,
1266 GtkCssBorderImageRepeat *image_repeat;
1268 image_repeat = g_value_get_boxed (value);
1270 enum_print (image_repeat->hrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1271 if (image_repeat->hrepeat != image_repeat->vrepeat)
1273 g_string_append (string, " ");
1274 enum_print (image_repeat->vrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1279 border_image_value_parse (GtkCssParser *parser,
1283 GValue temp = G_VALUE_INIT;
1284 cairo_pattern_t *pattern = NULL;
1285 gconstpointer *boxed = NULL;
1287 GtkBorder slice, *width = NULL, *parsed_slice;
1288 GtkCssBorderImageRepeat repeat, *parsed_repeat;
1289 gboolean retval = FALSE;
1290 GtkBorderImage *image = NULL;
1292 g_value_init (&temp, CAIRO_GOBJECT_TYPE_PATTERN);
1294 if (!pattern_value_parse (parser, base, &temp))
1297 boxed_type = G_VALUE_TYPE (&temp);
1298 if (boxed_type != CAIRO_GOBJECT_TYPE_PATTERN)
1299 boxed = g_value_dup_boxed (&temp);
1301 pattern = g_value_dup_boxed (&temp);
1303 g_value_unset (&temp);
1304 g_value_init (&temp, GTK_TYPE_BORDER);
1306 if (!border_value_parse (parser, base, &temp))
1309 parsed_slice = g_value_get_boxed (&temp);
1310 slice = *parsed_slice;
1312 if (_gtk_css_parser_try (parser, "/", TRUE))
1314 g_value_unset (&temp);
1315 g_value_init (&temp, GTK_TYPE_BORDER);
1317 if (!border_value_parse (parser, base, &temp))
1320 width = g_value_dup_boxed (&temp);
1323 g_value_unset (&temp);
1324 g_value_init (&temp, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
1326 if (!border_image_repeat_value_parse (parser, base, &temp))
1329 parsed_repeat = g_value_get_boxed (&temp);
1330 repeat = *parsed_repeat;
1332 g_value_unset (&temp);
1335 image = _gtk_border_image_new_for_boxed (boxed_type, boxed, &slice, width, &repeat);
1336 else if (pattern != NULL)
1337 image = _gtk_border_image_new (pattern, &slice, width, &repeat);
1342 g_value_take_boxed (value, image);
1346 if (pattern != NULL)
1347 cairo_pattern_destroy (pattern);
1350 g_boxed_free (boxed_type, boxed);
1353 gtk_border_free (width);
1359 enum_value_parse (GtkCssParser *parser,
1365 if (enum_parse (parser, G_VALUE_TYPE (value), &v))
1367 g_value_set_enum (value, v);
1375 enum_value_print (const GValue *value,
1378 enum_print (g_value_get_enum (value), G_VALUE_TYPE (value), string);
1382 flags_value_parse (GtkCssParser *parser,
1386 GFlagsClass *flags_class;
1387 GFlagsValue *flag_value;
1391 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1394 str = _gtk_css_parser_try_ident (parser, TRUE);
1397 _gtk_css_parser_error (parser, "Expected an identifier");
1398 g_type_class_unref (flags_class);
1402 flag_value = g_flags_get_value_by_nick (flags_class, str);
1405 _gtk_css_parser_error (parser,
1406 "Unknown flag value '%s' for type '%s'",
1407 str, g_type_name (G_VALUE_TYPE (value)));
1408 /* XXX Do we want to return FALSE here? We can get
1409 * forward-compatibility for new values this way
1412 g_type_class_unref (flags_class);
1418 while (_gtk_css_parser_try (parser, ",", FALSE));
1420 g_type_class_unref (flags_class);
1422 g_value_set_enum (value, flags);
1428 flags_value_print (const GValue *value,
1431 GFlagsClass *flags_class;
1434 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1435 flags = g_value_get_flags (value);
1437 for (i = 0; i < flags_class->n_values; i++)
1439 GFlagsValue *flags_value = &flags_class->values[i];
1441 if (flags & flags_value->value)
1443 if (string->len != 0)
1444 g_string_append (string, ", ");
1446 g_string_append (string, flags_value->value_nick);
1450 g_type_class_unref (flags_class);
1454 bindings_value_parse (GtkCssParser *parser,
1459 GtkBindingSet *binding_set;
1462 array = g_ptr_array_new ();
1465 name = _gtk_css_parser_try_ident (parser, TRUE);
1468 _gtk_css_parser_error (parser, "Not a valid binding name");
1469 g_ptr_array_free (array, TRUE);
1473 binding_set = gtk_binding_set_find (name);
1477 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
1482 g_ptr_array_add (array, binding_set);
1485 while (_gtk_css_parser_try (parser, ",", TRUE));
1487 g_value_take_boxed (value, array);
1493 bindings_value_print (const GValue *value,
1499 array = g_value_get_boxed (value);
1501 for (i = 0; i < array->len; i++)
1503 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
1506 g_string_append (string, ", ");
1507 g_string_append (string, binding_set->set_name);
1512 border_corner_radius_value_parse (GtkCssParser *parser,
1516 GtkCssBorderCornerRadius corner;
1518 if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
1520 _gtk_css_parser_error (parser, "Expected a number");
1523 else if (corner.horizontal < 0)
1526 if (!_gtk_css_parser_try_double (parser, &corner.vertical))
1527 corner.vertical = corner.horizontal;
1528 else if (corner.vertical < 0)
1531 g_value_set_boxed (value, &corner);
1535 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1540 border_corner_radius_value_print (const GValue *value,
1543 GtkCssBorderCornerRadius *corner;
1545 corner = g_value_get_boxed (value);
1549 g_string_append (string, "none");
1553 string_append_double (string, corner->horizontal);
1554 if (corner->horizontal != corner->vertical)
1556 g_string_append_c (string, ' ');
1557 string_append_double (string, corner->vertical);
1562 border_radius_value_parse (GtkCssParser *parser,
1566 GtkCssBorderRadius border;
1568 if (!_gtk_css_parser_try_double (parser, &border.top_left.horizontal))
1570 _gtk_css_parser_error (parser, "Expected a number");
1573 else if (border.top_left.horizontal < 0)
1576 if (_gtk_css_parser_try_double (parser, &border.top_right.horizontal))
1578 if (border.top_right.horizontal < 0)
1580 if (_gtk_css_parser_try_double (parser, &border.bottom_right.horizontal))
1582 if (border.bottom_right.horizontal < 0)
1584 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.horizontal))
1585 border.bottom_left.horizontal = border.top_right.horizontal;
1586 else if (border.bottom_left.horizontal < 0)
1591 border.bottom_right.horizontal = border.top_left.horizontal;
1592 border.bottom_left.horizontal = border.top_right.horizontal;
1597 border.top_right.horizontal = border.top_left.horizontal;
1598 border.bottom_right.horizontal = border.top_left.horizontal;
1599 border.bottom_left.horizontal = border.top_left.horizontal;
1602 if (_gtk_css_parser_try (parser, "/", TRUE))
1604 if (!_gtk_css_parser_try_double (parser, &border.top_left.vertical))
1606 _gtk_css_parser_error (parser, "Expected a number");
1609 else if (border.top_left.vertical < 0)
1612 if (_gtk_css_parser_try_double (parser, &border.top_right.vertical))
1614 if (border.top_right.vertical < 0)
1616 if (_gtk_css_parser_try_double (parser, &border.bottom_right.vertical))
1618 if (border.bottom_right.vertical < 0)
1620 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.vertical))
1621 border.bottom_left.vertical = border.top_right.vertical;
1622 else if (border.bottom_left.vertical < 0)
1627 border.bottom_right.vertical = border.top_left.vertical;
1628 border.bottom_left.vertical = border.top_right.vertical;
1633 border.top_right.vertical = border.top_left.vertical;
1634 border.bottom_right.vertical = border.top_left.vertical;
1635 border.bottom_left.vertical = border.top_left.vertical;
1640 border.top_left.vertical = border.top_left.horizontal;
1641 border.top_right.vertical = border.top_right.horizontal;
1642 border.bottom_right.vertical = border.bottom_right.horizontal;
1643 border.bottom_left.vertical = border.bottom_left.horizontal;
1646 /* border-radius is an int property for backwards-compat reasons */
1647 g_value_unset (value);
1648 g_value_init (value, GTK_TYPE_CSS_BORDER_RADIUS);
1649 g_value_set_boxed (value, &border);
1654 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1659 border_radius_value_print (const GValue *value,
1662 GtkCssBorderRadius *border;
1664 border = g_value_get_boxed (value);
1668 g_string_append (string, "none");
1672 string_append_double (string, border->top_left.horizontal);
1673 if (border->top_left.horizontal != border->top_right.horizontal ||
1674 border->top_left.horizontal != border->bottom_right.horizontal ||
1675 border->top_left.horizontal != border->bottom_left.horizontal)
1677 g_string_append_c (string, ' ');
1678 string_append_double (string, border->top_right.horizontal);
1679 if (border->top_left.horizontal != border->bottom_right.horizontal ||
1680 border->top_right.horizontal != border->bottom_left.horizontal)
1682 g_string_append_c (string, ' ');
1683 string_append_double (string, border->bottom_right.horizontal);
1684 if (border->top_right.horizontal != border->bottom_left.horizontal)
1686 g_string_append_c (string, ' ');
1687 string_append_double (string, border->bottom_left.horizontal);
1692 if (border->top_left.horizontal != border->top_left.vertical ||
1693 border->top_right.horizontal != border->top_right.vertical ||
1694 border->bottom_right.horizontal != border->bottom_right.vertical ||
1695 border->bottom_left.horizontal != border->bottom_left.vertical)
1697 g_string_append (string, " / ");
1698 string_append_double (string, border->top_left.vertical);
1699 if (border->top_left.vertical != border->top_right.vertical ||
1700 border->top_left.vertical != border->bottom_right.vertical ||
1701 border->top_left.vertical != border->bottom_left.vertical)
1703 g_string_append_c (string, ' ');
1704 string_append_double (string, border->top_right.vertical);
1705 if (border->top_left.vertical != border->bottom_right.vertical ||
1706 border->top_right.vertical != border->bottom_left.vertical)
1708 g_string_append_c (string, ' ');
1709 string_append_double (string, border->bottom_right.vertical);
1710 if (border->top_right.vertical != border->bottom_left.vertical)
1712 g_string_append_c (string, ' ');
1713 string_append_double (string, border->bottom_left.vertical);
1722 transparent_color_value_parse (GtkCssParser *parser,
1726 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1728 GdkRGBA transparent = { 0, 0, 0, 0 };
1730 g_value_set_boxed (value, &transparent);
1735 return rgba_value_parse (parser, base, value);
1739 border_color_shorthand_value_parse (GtkCssParser *parser,
1743 GtkSymbolicColor *symbolic;
1746 array = g_ptr_array_new_with_free_func ((GDestroyNotify) gtk_symbolic_color_unref);
1750 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1752 GdkRGBA transparent = { 0, 0, 0, 0 };
1754 symbolic = gtk_symbolic_color_new_literal (&transparent);
1758 symbolic = _gtk_css_parser_read_symbolic_color (parser);
1760 if (symbolic == NULL)
1764 g_ptr_array_add (array, symbolic);
1766 while (array->len < 4 &&
1767 !_gtk_css_parser_is_eof (parser) &&
1768 !_gtk_css_parser_begins_with (parser, ';') &&
1769 !_gtk_css_parser_begins_with (parser, '}'));
1774 g_assert_not_reached ();
1777 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1780 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1783 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 1)));
1789 g_value_unset (value);
1790 g_value_init (value, G_TYPE_PTR_ARRAY);
1791 g_value_take_boxed (value, array);
1799 unpack_border (const GValue *value,
1806 GParameter *parameter = g_new0 (GParameter, 4);
1807 GtkBorder *border = g_value_get_boxed (value);
1809 parameter[0].name = top;
1810 g_value_init (¶meter[0].value, G_TYPE_INT);
1811 g_value_set_int (¶meter[0].value, border->top);
1812 parameter[1].name = left;
1813 g_value_init (¶meter[1].value, G_TYPE_INT);
1814 g_value_set_int (¶meter[1].value, border->left);
1815 parameter[2].name = bottom;
1816 g_value_init (¶meter[2].value, G_TYPE_INT);
1817 g_value_set_int (¶meter[2].value, border->bottom);
1818 parameter[3].name = right;
1819 g_value_init (¶meter[3].value, G_TYPE_INT);
1820 g_value_set_int (¶meter[3].value, border->right);
1827 pack_border (GValue *value,
1828 GtkStyleProperties *props,
1829 GtkStateFlags state,
1838 gtk_style_properties_get (props,
1851 g_value_set_boxed (value, &border);
1855 unpack_border_width (const GValue *value,
1858 return unpack_border (value, n_params,
1859 "border-top-width", "border-left-width",
1860 "border-bottom-width", "border-right-width");
1864 pack_border_width (GValue *value,
1865 GtkStyleProperties *props,
1866 GtkStateFlags state,
1867 GtkStylePropertyContext *context)
1869 pack_border (value, props, state,
1870 "border-top-width", "border-left-width",
1871 "border-bottom-width", "border-right-width");
1875 unpack_padding (const GValue *value,
1878 return unpack_border (value, n_params,
1879 "padding-top", "padding-left",
1880 "padding-bottom", "padding-right");
1884 pack_padding (GValue *value,
1885 GtkStyleProperties *props,
1886 GtkStateFlags state,
1887 GtkStylePropertyContext *context)
1889 pack_border (value, props, state,
1890 "padding-top", "padding-left",
1891 "padding-bottom", "padding-right");
1895 unpack_margin (const GValue *value,
1898 return unpack_border (value, n_params,
1899 "margin-top", "margin-left",
1900 "margin-bottom", "margin-right");
1904 pack_margin (GValue *value,
1905 GtkStyleProperties *props,
1906 GtkStateFlags state,
1907 GtkStylePropertyContext *context)
1909 pack_border (value, props, state,
1910 "margin-top", "margin-left",
1911 "margin-bottom", "margin-right");
1915 unpack_border_radius (const GValue *value,
1918 GParameter *parameter = g_new0 (GParameter, 4);
1919 GtkCssBorderRadius *border;
1921 if (G_VALUE_HOLDS_BOXED (value))
1922 border = g_value_get_boxed (value);
1926 parameter[0].name = "border-top-left-radius";
1927 g_value_init (¶meter[0].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1928 parameter[1].name = "border-top-right-radius";
1929 g_value_init (¶meter[1].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1930 parameter[2].name = "border-bottom-right-radius";
1931 g_value_init (¶meter[2].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1932 parameter[3].name = "border-bottom-left-radius";
1933 g_value_init (¶meter[3].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1936 g_value_set_boxed (¶meter[0].value, &border->top_left);
1937 g_value_set_boxed (¶meter[1].value, &border->top_right);
1938 g_value_set_boxed (¶meter[2].value, &border->bottom_right);
1939 g_value_set_boxed (¶meter[3].value, &border->bottom_left);
1947 pack_border_radius (GValue *value,
1948 GtkStyleProperties *props,
1949 GtkStateFlags state,
1950 GtkStylePropertyContext *context)
1952 GtkCssBorderCornerRadius *top_left;
1954 /* NB: We are an int property, so we have to resolve to an int here.
1955 * So we just resolve to an int. We pick one and stick to it.
1956 * Lesson learned: Don't query border-radius shorthand, query the
1957 * real properties instead. */
1958 gtk_style_properties_get (props,
1960 "border-top-left-radius", &top_left,
1964 g_value_set_int (value, top_left->horizontal);
1970 unpack_font_description (const GValue *value,
1973 GParameter *parameter = g_new0 (GParameter, 5);
1974 PangoFontDescription *description;
1978 /* For backwards compat, we only unpack values that are indeed set.
1979 * For strict CSS conformance we need to unpack all of them.
1980 * Note that we do set all of them in the parse function, so it
1981 * will not have effects when parsing CSS files. It will though
1982 * for custom style providers.
1985 description = g_value_get_boxed (value);
1989 mask = pango_font_description_get_set_fields (description);
1993 if (mask & PANGO_FONT_MASK_FAMILY)
1995 GPtrArray *strv = g_ptr_array_new ();
1997 g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
1998 g_ptr_array_add (strv, NULL);
1999 parameter[n].name = "font-family";
2000 g_value_init (¶meter[n].value, G_TYPE_STRV);
2001 g_value_take_boxed (¶meter[n].value,
2002 g_ptr_array_free (strv, FALSE));
2006 if (mask & PANGO_FONT_MASK_STYLE)
2008 parameter[n].name = "font-style";
2009 g_value_init (¶meter[n].value, PANGO_TYPE_STYLE);
2010 g_value_set_enum (¶meter[n].value,
2011 pango_font_description_get_style (description));
2015 if (mask & PANGO_FONT_MASK_VARIANT)
2017 parameter[n].name = "font-variant";
2018 g_value_init (¶meter[n].value, PANGO_TYPE_VARIANT);
2019 g_value_set_enum (¶meter[n].value,
2020 pango_font_description_get_variant (description));
2024 if (mask & PANGO_FONT_MASK_WEIGHT)
2026 parameter[n].name = "font-weight";
2027 g_value_init (¶meter[n].value, PANGO_TYPE_WEIGHT);
2028 g_value_set_enum (¶meter[n].value,
2029 pango_font_description_get_weight (description));
2033 if (mask & PANGO_FONT_MASK_SIZE)
2035 parameter[n].name = "font-size";
2036 g_value_init (¶meter[n].value, G_TYPE_DOUBLE);
2037 g_value_set_double (¶meter[n].value,
2038 (double) pango_font_description_get_size (description) / PANGO_SCALE);
2048 pack_font_description (GValue *value,
2049 GtkStyleProperties *props,
2050 GtkStateFlags state,
2051 GtkStylePropertyContext *context)
2053 PangoFontDescription *description;
2056 PangoVariant variant;
2060 gtk_style_properties_get (props,
2062 "font-family", &families,
2063 "font-style", &style,
2064 "font-variant", &variant,
2065 "font-weight", &weight,
2069 description = pango_font_description_new ();
2070 /* xxx: Can we set all the families here somehow? */
2072 pango_font_description_set_family (description, families[0]);
2073 pango_font_description_set_size (description, round (size * PANGO_SCALE));
2074 pango_font_description_set_style (description, style);
2075 pango_font_description_set_variant (description, variant);
2076 pango_font_description_set_weight (description, weight);
2078 g_strfreev (families);
2080 g_value_take_boxed (value, description);
2084 unpack_border_color (const GValue *value,
2087 GParameter *parameter = g_new0 (GParameter, 4);
2090 type = G_VALUE_TYPE (value);
2091 if (type == G_TYPE_PTR_ARRAY)
2092 type = GTK_TYPE_SYMBOLIC_COLOR;
2094 parameter[0].name = "border-top-color";
2095 g_value_init (¶meter[0].value, type);
2096 parameter[1].name = "border-right-color";
2097 g_value_init (¶meter[1].value, type);
2098 parameter[2].name = "border-bottom-color";
2099 g_value_init (¶meter[2].value, type);
2100 parameter[3].name = "border-left-color";
2101 g_value_init (¶meter[3].value, type);
2103 if (G_VALUE_TYPE (value) == G_TYPE_PTR_ARRAY)
2105 GPtrArray *array = g_value_get_boxed (value);
2108 for (i = 0; i < 4; i++)
2109 g_value_set_boxed (¶meter[i].value, g_ptr_array_index (array, i));
2113 /* can be RGBA or symbolic color */
2114 gpointer p = g_value_get_boxed (value);
2116 g_value_set_boxed (¶meter[0].value, p);
2117 g_value_set_boxed (¶meter[1].value, p);
2118 g_value_set_boxed (¶meter[2].value, p);
2119 g_value_set_boxed (¶meter[3].value, p);
2127 pack_border_color (GValue *value,
2128 GtkStyleProperties *props,
2129 GtkStateFlags state,
2130 GtkStylePropertyContext *context)
2132 /* NB: We are a color property, so we have to resolve to a color here.
2133 * So we just resolve to a color. We pick one and stick to it.
2134 * Lesson learned: Don't query border-color shorthand, query the
2135 * real properties instead. */
2136 g_value_unset (value);
2137 gtk_style_properties_get_property (props, "border-top-color", state, value);
2140 /*** UNSET FUNCS ***/
2143 unset_font_description (GtkStyleProperties *props,
2144 GtkStateFlags state)
2146 gtk_style_properties_unset_property (props, "font-family", state);
2147 gtk_style_properties_unset_property (props, "font-style", state);
2148 gtk_style_properties_unset_property (props, "font-variant", state);
2149 gtk_style_properties_unset_property (props, "font-weight", state);
2150 gtk_style_properties_unset_property (props, "font-size", state);
2154 unset_margin (GtkStyleProperties *props,
2155 GtkStateFlags state)
2157 gtk_style_properties_unset_property (props, "margin-top", state);
2158 gtk_style_properties_unset_property (props, "margin-right", state);
2159 gtk_style_properties_unset_property (props, "margin-bottom", state);
2160 gtk_style_properties_unset_property (props, "margin-left", state);
2164 unset_padding (GtkStyleProperties *props,
2165 GtkStateFlags state)
2167 gtk_style_properties_unset_property (props, "padding-top", state);
2168 gtk_style_properties_unset_property (props, "padding-right", state);
2169 gtk_style_properties_unset_property (props, "padding-bottom", state);
2170 gtk_style_properties_unset_property (props, "padding-left", state);
2174 unset_border_width (GtkStyleProperties *props,
2175 GtkStateFlags state)
2177 gtk_style_properties_unset_property (props, "border-top-width", state);
2178 gtk_style_properties_unset_property (props, "border-right-width", state);
2179 gtk_style_properties_unset_property (props, "border-bottom-width", state);
2180 gtk_style_properties_unset_property (props, "border-left-width", state);
2184 unset_border_radius (GtkStyleProperties *props,
2185 GtkStateFlags state)
2187 gtk_style_properties_unset_property (props, "border-top-right-radius", state);
2188 gtk_style_properties_unset_property (props, "border-bottom-right-radius", state);
2189 gtk_style_properties_unset_property (props, "border-bottom-left-radius", state);
2190 gtk_style_properties_unset_property (props, "border-top-left-radius", state);
2194 unset_border_color (GtkStyleProperties *props,
2195 GtkStateFlags state)
2197 gtk_style_properties_unset_property (props, "border-top-color", state);
2198 gtk_style_properties_unset_property (props, "border-right-color", state);
2199 gtk_style_properties_unset_property (props, "border-bottom-color", state);
2200 gtk_style_properties_unset_property (props, "border-left-color", state);
2204 unset_border_image (GtkStyleProperties *props,
2205 GtkStateFlags state)
2207 gtk_style_properties_unset_property (props, "border-image-source", state);
2208 gtk_style_properties_unset_property (props, "border-image-slice", state);
2209 gtk_style_properties_unset_property (props, "border-image-repeat", state);
2210 gtk_style_properties_unset_property (props, "border-image-width", state);
2213 /*** default values ***/
2216 border_image_width_default_value (GtkStyleProperties *props,
2217 GtkStateFlags state,
2223 background_color_default_value (GtkStyleProperties *props,
2224 GtkStateFlags state,
2227 GdkRGBA transparent_black = { 0, 0, 0, 0 };
2229 g_value_set_boxed (value, &transparent_black);
2233 border_color_default_value (GtkStyleProperties *props,
2234 GtkStateFlags state,
2237 g_value_unset (value);
2238 gtk_style_properties_get_property (props, "color", state, value);
2244 css_string_funcs_init (void)
2246 if (G_LIKELY (parse_funcs != NULL))
2249 parse_funcs = g_hash_table_new (NULL, NULL);
2250 print_funcs = g_hash_table_new (NULL, NULL);
2252 register_conversion_function (GDK_TYPE_RGBA,
2255 register_conversion_function (GDK_TYPE_COLOR,
2258 register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
2259 symbolic_color_value_parse,
2260 symbolic_color_value_print);
2261 register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
2262 font_description_value_parse,
2263 font_description_value_print);
2264 register_conversion_function (G_TYPE_BOOLEAN,
2265 boolean_value_parse,
2266 boolean_value_print);
2267 register_conversion_function (G_TYPE_INT,
2270 register_conversion_function (G_TYPE_UINT,
2273 register_conversion_function (G_TYPE_DOUBLE,
2275 double_value_print);
2276 register_conversion_function (G_TYPE_FLOAT,
2279 register_conversion_function (G_TYPE_STRING,
2281 string_value_print);
2282 register_conversion_function (GTK_TYPE_THEMING_ENGINE,
2283 theming_engine_value_parse,
2284 theming_engine_value_print);
2285 register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
2286 animation_description_value_parse,
2287 animation_description_value_print);
2288 register_conversion_function (GTK_TYPE_BORDER,
2290 border_value_print);
2291 register_conversion_function (GTK_TYPE_GRADIENT,
2292 gradient_value_parse,
2293 gradient_value_print);
2294 register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
2295 pattern_value_parse,
2296 pattern_value_print);
2297 register_conversion_function (GTK_TYPE_BORDER_IMAGE,
2298 border_image_value_parse,
2300 register_conversion_function (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
2301 border_image_repeat_value_parse,
2302 border_image_repeat_value_print);
2303 register_conversion_function (GTK_TYPE_SHADOW,
2305 shadow_value_print);
2306 register_conversion_function (G_TYPE_ENUM,
2309 register_conversion_function (G_TYPE_FLAGS,
2312 register_conversion_function (GTK_TYPE_CSS_BACKGROUND_REPEAT,
2313 background_repeat_value_parse,
2314 background_repeat_value_print);
2318 _gtk_style_property_parse_value (const GtkStyleProperty *property,
2320 GtkCssParser *parser,
2323 GtkStyleParseFunc func;
2325 g_return_val_if_fail (value != NULL, FALSE);
2326 g_return_val_if_fail (parser != NULL, FALSE);
2328 css_string_funcs_init ();
2332 if (_gtk_css_parser_try (parser, "none", TRUE))
2334 /* Insert the default value, so it has an opportunity
2335 * to override other style providers when merged
2337 g_param_value_set_default (property->pspec, value);
2340 else if (property->property_parse_func)
2342 GError *error = NULL;
2346 value_str = _gtk_css_parser_read_value (parser);
2347 if (value_str == NULL)
2350 success = (*property->property_parse_func) (value_str, value, &error);
2357 func = property->parse_func;
2363 func = g_hash_table_lookup (parse_funcs,
2364 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2366 func = g_hash_table_lookup (parse_funcs,
2367 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2371 _gtk_css_parser_error (parser,
2372 "Cannot convert to type '%s'",
2373 g_type_name (G_VALUE_TYPE (value)));
2377 return (*func) (parser, base, value);
2381 _gtk_style_property_print_value (const GtkStyleProperty *property,
2382 const GValue *value,
2385 GtkStylePrintFunc func;
2387 css_string_funcs_init ();
2390 func = property->print_func;
2395 func = g_hash_table_lookup (print_funcs,
2396 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2398 func = g_hash_table_lookup (print_funcs,
2399 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2403 char *s = g_strdup_value_contents (value);
2404 g_string_append (string, s);
2409 func (value, string);
2413 _gtk_style_property_default_value (const GtkStyleProperty *property,
2414 GtkStyleProperties *properties,
2415 GtkStateFlags state,
2418 if (property->default_value_func)
2419 property->default_value_func (properties, state, value);
2420 else if (property->pspec->value_type == GTK_TYPE_THEMING_ENGINE)
2421 g_value_set_object (value, gtk_theming_engine_load (NULL));
2422 else if (property->pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
2423 g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
2424 else if (property->pspec->value_type == GDK_TYPE_RGBA)
2427 gdk_rgba_parse (&color, "pink");
2428 g_value_set_boxed (value, &color);
2430 else if (property->pspec->value_type == GTK_TYPE_BORDER)
2432 g_value_take_boxed (value, gtk_border_new ());
2435 g_param_value_set_default (property->pspec, value);
2439 _gtk_style_property_is_inherit (const GtkStyleProperty *property)
2441 g_return_val_if_fail (property != NULL, FALSE);
2443 return property->flags & GTK_STYLE_PROPERTY_INHERIT ? TRUE : FALSE;
2447 resolve_color (GtkStyleProperties *props,
2452 /* Resolve symbolic color to GdkRGBA */
2453 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
2456 /* Store it back, this is where GdkRGBA caching happens */
2457 g_value_unset (value);
2458 g_value_init (value, GDK_TYPE_RGBA);
2459 g_value_set_boxed (value, &color);
2465 resolve_color_rgb (GtkStyleProperties *props,
2468 GdkColor color = { 0 };
2471 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
2474 color.red = rgba.red * 65535. + 0.5;
2475 color.green = rgba.green * 65535. + 0.5;
2476 color.blue = rgba.blue * 65535. + 0.5;
2478 g_value_unset (value);
2479 g_value_init (value, GDK_TYPE_COLOR);
2480 g_value_set_boxed (value, &color);
2486 resolve_win32_theme_part (GtkStyleProperties *props,
2489 GtkStylePropertyContext *context)
2491 GtkWin32ThemePart *part;
2492 cairo_pattern_t *pattern;
2494 part = g_value_get_boxed (value);
2498 pattern = _gtk_win32_theme_part_render (part, context->width, context->height);
2500 g_value_take_boxed (value_out, pattern);
2507 resolve_gradient (GtkStyleProperties *props,
2510 cairo_pattern_t *gradient;
2512 if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
2515 /* Store it back, this is where cairo_pattern_t caching happens */
2516 g_value_unset (value);
2517 g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
2518 g_value_take_boxed (value, gradient);
2524 resolve_shadow (GtkStyleProperties *props,
2527 GtkShadow *resolved, *base;
2529 base = g_value_get_boxed (value);
2534 if (_gtk_shadow_get_resolved (base))
2537 resolved = _gtk_shadow_resolve (base, props);
2538 if (resolved == NULL)
2541 g_value_take_boxed (value, resolved);
2547 _gtk_style_property_resolve (const GtkStyleProperty *property,
2548 GtkStyleProperties *props,
2549 GtkStateFlags state,
2550 GtkStylePropertyContext *context,
2554 if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
2556 if (property->pspec->value_type == GDK_TYPE_RGBA)
2558 if (resolve_color (props, val))
2561 else if (property->pspec->value_type == GDK_TYPE_COLOR)
2563 if (resolve_color_rgb (props, val))
2567 g_value_unset (val);
2568 g_value_init (val, property->pspec->value_type);
2569 _gtk_style_property_default_value (property, props, state, val);
2571 else if (G_VALUE_TYPE (val) == GDK_TYPE_RGBA)
2573 if (g_value_get_boxed (val) == NULL)
2574 _gtk_style_property_default_value (property, props, state, val);
2576 else if (G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
2578 g_return_if_fail (property->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN);
2580 if (!resolve_gradient (props, val))
2582 g_value_unset (val);
2583 g_value_init (val, CAIRO_GOBJECT_TYPE_PATTERN);
2584 _gtk_style_property_default_value (property, props, state, val);
2587 else if (G_VALUE_TYPE (val) == GTK_TYPE_SHADOW)
2589 if (!resolve_shadow (props, val))
2590 _gtk_style_property_default_value (property, props, state, val);
2592 else if (G_VALUE_TYPE (val) == GTK_TYPE_WIN32_THEME_PART)
2594 if (resolve_win32_theme_part (props, val, val_out, context))
2595 return; /* Don't copy val, this sets val_out */
2596 _gtk_style_property_default_value (property, props, state, val);
2600 g_value_copy (val, val_out);
2604 _gtk_style_property_is_shorthand (const GtkStyleProperty *property)
2606 g_return_val_if_fail (property != NULL, FALSE);
2608 return property->pack_func != NULL;
2612 _gtk_style_property_unpack (const GtkStyleProperty *property,
2613 const GValue *value,
2616 g_return_val_if_fail (property != NULL, NULL);
2617 g_return_val_if_fail (property->unpack_func != NULL, NULL);
2618 g_return_val_if_fail (value != NULL, NULL);
2619 g_return_val_if_fail (n_params != NULL, NULL);
2621 return property->unpack_func (value, n_params);
2625 _gtk_style_property_pack (const GtkStyleProperty *property,
2626 GtkStyleProperties *props,
2627 GtkStateFlags state,
2628 GtkStylePropertyContext *context,
2631 g_return_if_fail (property != NULL);
2632 g_return_if_fail (property->pack_func != NULL);
2633 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
2634 g_return_if_fail (G_IS_VALUE (value));
2636 property->pack_func (value, props, state, context);
2640 gtk_style_property_init (void)
2642 if (G_LIKELY (properties))
2645 /* stuff is never freed, so no need for free functions */
2646 properties = g_hash_table_new (g_str_hash, g_str_equal);
2648 /* note that gtk_style_properties_register_property() calls this function,
2649 * so make sure we're sanely inited to avoid infloops */
2651 _gtk_style_property_register (g_param_spec_boxed ("color",
2655 GTK_STYLE_PROPERTY_INHERIT,
2663 _gtk_style_property_register (g_param_spec_boxed ("background-color",
2671 transparent_color_value_parse,
2673 background_color_default_value,
2676 _gtk_style_property_register (g_param_spec_boxed ("font-family",
2680 GTK_STYLE_PROPERTY_INHERIT,
2685 font_family_value_print,
2688 _gtk_style_property_register (g_param_spec_enum ("font-style",
2692 PANGO_STYLE_NORMAL, 0),
2693 GTK_STYLE_PROPERTY_INHERIT,
2701 _gtk_style_property_register (g_param_spec_enum ("font-variant",
2705 PANGO_VARIANT_NORMAL, 0),
2706 GTK_STYLE_PROPERTY_INHERIT,
2714 /* xxx: need to parse this properly, ie parse the numbers */
2715 _gtk_style_property_register (g_param_spec_enum ("font-weight",
2719 PANGO_WEIGHT_NORMAL, 0),
2720 GTK_STYLE_PROPERTY_INHERIT,
2728 _gtk_style_property_register (g_param_spec_double ("font-size",
2731 0, G_MAXDOUBLE, 0, 0),
2732 GTK_STYLE_PROPERTY_INHERIT,
2740 _gtk_style_property_register (g_param_spec_boxed ("font",
2743 PANGO_TYPE_FONT_DESCRIPTION, 0),
2744 GTK_STYLE_PROPERTY_INHERIT,
2746 unpack_font_description,
2747 pack_font_description,
2748 font_description_value_parse,
2749 font_description_value_print,
2751 unset_font_description);
2753 _gtk_style_property_register (g_param_spec_boxed ("text-shadow",
2756 GTK_TYPE_SHADOW, 0),
2757 GTK_STYLE_PROPERTY_INHERIT,
2766 _gtk_style_property_register (g_param_spec_boxed ("icon-shadow",
2769 GTK_TYPE_SHADOW, 0),
2770 GTK_STYLE_PROPERTY_INHERIT,
2779 gtk_style_properties_register_property (NULL,
2780 g_param_spec_boxed ("box-shadow",
2783 GTK_TYPE_SHADOW, 0));
2784 gtk_style_properties_register_property (NULL,
2785 g_param_spec_int ("margin-top",
2788 0, G_MAXINT, 0, 0));
2789 gtk_style_properties_register_property (NULL,
2790 g_param_spec_int ("margin-left",
2793 0, G_MAXINT, 0, 0));
2794 gtk_style_properties_register_property (NULL,
2795 g_param_spec_int ("margin-bottom",
2798 0, G_MAXINT, 0, 0));
2799 gtk_style_properties_register_property (NULL,
2800 g_param_spec_int ("margin-right",
2803 0, G_MAXINT, 0, 0));
2804 _gtk_style_property_register (g_param_spec_boxed ("margin",
2807 GTK_TYPE_BORDER, 0),
2816 gtk_style_properties_register_property (NULL,
2817 g_param_spec_int ("padding-top",
2820 0, G_MAXINT, 0, 0));
2821 gtk_style_properties_register_property (NULL,
2822 g_param_spec_int ("padding-left",
2825 0, G_MAXINT, 0, 0));
2826 gtk_style_properties_register_property (NULL,
2827 g_param_spec_int ("padding-bottom",
2829 "Padding at bottom",
2830 0, G_MAXINT, 0, 0));
2831 gtk_style_properties_register_property (NULL,
2832 g_param_spec_int ("padding-right",
2835 0, G_MAXINT, 0, 0));
2836 _gtk_style_property_register (g_param_spec_boxed ("padding",
2839 GTK_TYPE_BORDER, 0),
2848 gtk_style_properties_register_property (NULL,
2849 g_param_spec_int ("border-top-width",
2851 "Border width at top",
2852 0, G_MAXINT, 0, 0));
2853 gtk_style_properties_register_property (NULL,
2854 g_param_spec_int ("border-left-width",
2855 "border left width",
2856 "Border width at left",
2857 0, G_MAXINT, 0, 0));
2858 gtk_style_properties_register_property (NULL,
2859 g_param_spec_int ("border-bottom-width",
2860 "border bottom width",
2861 "Border width at bottom",
2862 0, G_MAXINT, 0, 0));
2863 gtk_style_properties_register_property (NULL,
2864 g_param_spec_int ("border-right-width",
2865 "border right width",
2866 "Border width at right",
2867 0, G_MAXINT, 0, 0));
2868 _gtk_style_property_register (g_param_spec_boxed ("border-width",
2870 "Border width, in pixels",
2871 GTK_TYPE_BORDER, 0),
2874 unpack_border_width,
2879 unset_border_width);
2881 _gtk_style_property_register (g_param_spec_boxed ("border-top-left-radius",
2882 "Border top left radius",
2883 "Border radius of top left corner, in pixels",
2884 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2889 border_corner_radius_value_parse,
2890 border_corner_radius_value_print,
2893 _gtk_style_property_register (g_param_spec_boxed ("border-top-right-radius",
2894 "Border top right radius",
2895 "Border radius of top right corner, in pixels",
2896 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2901 border_corner_radius_value_parse,
2902 border_corner_radius_value_print,
2905 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-right-radius",
2906 "Border bottom right radius",
2907 "Border radius of bottom right 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-bottom-left-radius",
2918 "Border bottom left radius",
2919 "Border radius of bottom left 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_int ("border-radius",
2931 "Border radius, in pixels",
2935 unpack_border_radius,
2937 border_radius_value_parse,
2938 border_radius_value_print,
2940 unset_border_radius);
2942 gtk_style_properties_register_property (NULL,
2943 g_param_spec_enum ("border-style",
2946 GTK_TYPE_BORDER_STYLE,
2947 GTK_BORDER_STYLE_NONE, 0));
2948 _gtk_style_property_register (g_param_spec_boxed ("border-top-color",
2956 transparent_color_value_parse,
2958 border_color_default_value,
2960 _gtk_style_property_register (g_param_spec_boxed ("border-right-color",
2961 "Border right color",
2962 "Border right color",
2968 transparent_color_value_parse,
2970 border_color_default_value,
2972 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-color",
2973 "Border bottom color",
2974 "Border bottom color",
2980 transparent_color_value_parse,
2982 border_color_default_value,
2984 _gtk_style_property_register (g_param_spec_boxed ("border-left-color",
2985 "Border left color",
2986 "Border left color",
2992 transparent_color_value_parse,
2994 border_color_default_value,
2996 _gtk_style_property_register (g_param_spec_boxed ("border-color",
3002 unpack_border_color,
3004 border_color_shorthand_value_parse,
3007 unset_border_color);
3009 gtk_style_properties_register_property (NULL,
3010 g_param_spec_boxed ("background-image",
3013 CAIRO_GOBJECT_TYPE_PATTERN, 0));
3014 gtk_style_properties_register_property (NULL,
3015 g_param_spec_boxed ("background-repeat",
3016 "Background repeat",
3017 "Background repeat",
3018 GTK_TYPE_CSS_BACKGROUND_REPEAT, 0));
3020 gtk_style_properties_register_property (NULL,
3021 g_param_spec_boxed ("border-image-source",
3022 "Border image source",
3023 "Border image source",
3024 CAIRO_GOBJECT_TYPE_PATTERN, 0));
3025 gtk_style_properties_register_property (NULL,
3026 g_param_spec_boxed ("border-image-repeat",
3027 "Border image repeat",
3028 "Border image repeat",
3029 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
3030 gtk_style_properties_register_property (NULL,
3031 g_param_spec_boxed ("border-image-slice",
3032 "Border image slice",
3033 "Border image slice",
3034 GTK_TYPE_BORDER, 0));
3035 _gtk_style_property_register (g_param_spec_boxed ("border-image-width",
3036 "Border image width",
3037 "Border image width",
3038 GTK_TYPE_BORDER, 0),
3045 border_image_width_default_value,
3047 _gtk_style_property_register (g_param_spec_boxed ("border-image",
3050 GTK_TYPE_BORDER_IMAGE, 0),
3053 _gtk_border_image_unpack,
3054 _gtk_border_image_pack,
3058 unset_border_image);
3059 gtk_style_properties_register_property (NULL,
3060 g_param_spec_object ("engine",
3063 GTK_TYPE_THEMING_ENGINE, 0));
3064 gtk_style_properties_register_property (NULL,
3065 g_param_spec_boxed ("transition",
3066 "Transition animation description",
3067 "Transition animation description",
3068 GTK_TYPE_ANIMATION_DESCRIPTION, 0));
3070 /* Private property holding the binding sets */
3071 _gtk_style_property_register (g_param_spec_boxed ("gtk-key-bindings",
3074 G_TYPE_PTR_ARRAY, 0),
3079 bindings_value_parse,
3080 bindings_value_print,
3085 const GtkStyleProperty *
3086 _gtk_style_property_lookup (const char *name)
3088 gtk_style_property_init ();
3090 return g_hash_table_lookup (properties, name);
3094 _gtk_style_property_register (GParamSpec *pspec,
3095 GtkStylePropertyFlags flags,
3096 GtkStylePropertyParser property_parse_func,
3097 GtkStyleUnpackFunc unpack_func,
3098 GtkStylePackFunc pack_func,
3099 GtkStyleParseFunc parse_func,
3100 GtkStylePrintFunc print_func,
3101 GtkStyleDefaultValueFunc default_value_func,
3102 GtkStyleUnsetFunc unset_func)
3104 const GtkStyleProperty *existing;
3105 GtkStyleProperty *node;
3107 g_return_if_fail ((pack_func == NULL) == (unpack_func == NULL));
3109 gtk_style_property_init ();
3111 existing = _gtk_style_property_lookup (pspec->name);
3112 if (existing != NULL)
3114 g_warning ("Property \"%s\" was already registered with type %s",
3115 pspec->name, g_type_name (existing->pspec->value_type));
3119 node = g_slice_new0 (GtkStyleProperty);
3120 node->flags = flags;
3121 node->pspec = pspec;
3122 node->property_parse_func = property_parse_func;
3123 node->pack_func = pack_func;
3124 node->unpack_func = unpack_func;
3125 node->parse_func = parse_func;
3126 node->print_func = print_func;
3127 node->default_value_func = default_value_func;
3128 node->unset_func = unset_func;
3130 /* pspec owns name */
3131 g_hash_table_insert (properties, (gchar *)pspec->name, node);