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;
55 static GPtrArray *__style_property_array = NULL;
58 register_conversion_function (GType type,
59 GtkStyleParseFunc parse,
60 GtkStylePrintFunc print)
63 g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse);
65 g_hash_table_insert (print_funcs, GSIZE_TO_POINTER (type), print);
69 string_append_double (GString *string,
72 char buf[G_ASCII_DTOSTR_BUF_SIZE];
74 g_ascii_dtostr (buf, sizeof (buf), d);
75 g_string_append (string, buf);
79 string_append_string (GString *str,
84 g_string_append_c (str, '"');
87 len = strcspn (string, "\"\n\r\f");
88 g_string_append (str, string);
95 g_string_append (str, "\\A ");
98 g_string_append (str, "\\D ");
101 g_string_append (str, "\\C ");
104 g_string_append (str, "\\\"");
107 g_assert_not_reached ();
112 g_string_append_c (str, '"');
115 /*** IMPLEMENTATIONS ***/
118 enum_parse (GtkCssParser *parser,
124 if (_gtk_css_parser_try_enum (parser, type, res))
127 str = _gtk_css_parser_try_ident (parser, TRUE);
130 _gtk_css_parser_error (parser, "Expected an identifier");
134 _gtk_css_parser_error (parser,
135 "Unknown value '%s' for enum type '%s'",
136 str, g_type_name (type));
143 enum_print (int value,
147 GEnumClass *enum_class;
148 GEnumValue *enum_value;
150 enum_class = g_type_class_ref (type);
151 enum_value = g_enum_get_value (enum_class, value);
153 g_string_append (string, enum_value->value_nick);
155 g_type_class_unref (enum_class);
159 rgba_value_parse (GtkCssParser *parser,
163 GtkSymbolicColor *symbolic;
166 symbolic = _gtk_css_parser_read_symbolic_color (parser);
167 if (symbolic == NULL)
170 if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
172 g_value_set_boxed (value, &rgba);
173 gtk_symbolic_color_unref (symbolic);
177 g_value_unset (value);
178 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
179 g_value_take_boxed (value, symbolic);
186 rgba_value_print (const GValue *value,
189 const GdkRGBA *rgba = g_value_get_boxed (value);
192 g_string_append (string, "none");
195 char *s = gdk_rgba_to_string (rgba);
196 g_string_append (string, s);
202 color_value_parse (GtkCssParser *parser,
206 GtkSymbolicColor *symbolic;
209 symbolic = _gtk_css_parser_read_symbolic_color (parser);
210 if (symbolic == NULL)
213 if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
217 color.red = rgba.red * 65535. + 0.5;
218 color.green = rgba.green * 65535. + 0.5;
219 color.blue = rgba.blue * 65535. + 0.5;
221 g_value_set_boxed (value, &color);
225 g_value_unset (value);
226 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
227 g_value_take_boxed (value, symbolic);
234 color_value_print (const GValue *value,
237 const GdkColor *color = g_value_get_boxed (value);
240 g_string_append (string, "none");
243 char *s = gdk_color_to_string (color);
244 g_string_append (string, s);
250 symbolic_color_value_parse (GtkCssParser *parser,
254 GtkSymbolicColor *symbolic;
256 symbolic = _gtk_css_parser_read_symbolic_color (parser);
257 if (symbolic == NULL)
260 g_value_take_boxed (value, symbolic);
265 symbolic_color_value_print (const GValue *value,
268 GtkSymbolicColor *symbolic = g_value_get_boxed (value);
270 if (symbolic == NULL)
271 g_string_append (string, "none");
274 char *s = gtk_symbolic_color_to_string (symbolic);
275 g_string_append (string, s);
281 font_family_parse (GtkCssParser *parser,
288 /* We don't special case generic families. Pango should do
291 names = g_ptr_array_new ();
294 name = _gtk_css_parser_try_ident (parser, TRUE);
297 GString *string = g_string_new (name);
299 while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
301 g_string_append_c (string, ' ');
302 g_string_append (string, name);
305 name = g_string_free (string, FALSE);
309 name = _gtk_css_parser_read_string (parser);
312 g_ptr_array_free (names, TRUE);
317 g_ptr_array_add (names, name);
318 } while (_gtk_css_parser_try (parser, ",", TRUE));
320 /* NULL-terminate array */
321 g_ptr_array_add (names, NULL);
322 g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
327 font_family_value_print (const GValue *value,
330 const char **names = g_value_get_boxed (value);
332 if (names == NULL || *names == NULL)
334 g_string_append (string, "none");
338 string_append_string (string, *names);
342 g_string_append (string, ", ");
343 string_append_string (string, *names);
349 font_description_value_parse (GtkCssParser *parser,
353 PangoFontDescription *font_desc;
357 str = _gtk_css_parser_read_value (parser);
361 font_desc = pango_font_description_from_string (str);
362 mask = pango_font_description_get_set_fields (font_desc);
363 /* These values are not really correct,
364 * but the fields must be set, so we set them to something */
365 if ((mask & PANGO_FONT_MASK_FAMILY) == 0)
366 pango_font_description_set_family_static (font_desc, "Sans");
367 if ((mask & PANGO_FONT_MASK_SIZE) == 0)
368 pango_font_description_set_size (font_desc, 10 * PANGO_SCALE);
370 g_value_take_boxed (value, font_desc);
375 font_description_value_print (const GValue *value,
378 const PangoFontDescription *desc = g_value_get_boxed (value);
381 g_string_append (string, "none");
384 char *s = pango_font_description_to_string (desc);
385 g_string_append (string, s);
391 boolean_value_parse (GtkCssParser *parser,
395 if (_gtk_css_parser_try (parser, "true", TRUE) ||
396 _gtk_css_parser_try (parser, "1", TRUE))
398 g_value_set_boolean (value, TRUE);
401 else if (_gtk_css_parser_try (parser, "false", TRUE) ||
402 _gtk_css_parser_try (parser, "0", TRUE))
404 g_value_set_boolean (value, FALSE);
409 _gtk_css_parser_error (parser, "Expected a boolean value");
415 boolean_value_print (const GValue *value,
418 if (g_value_get_boolean (value))
419 g_string_append (string, "true");
421 g_string_append (string, "false");
425 int_value_parse (GtkCssParser *parser,
431 if (_gtk_css_parser_begins_with (parser, '-'))
433 int res = _gtk_win32_theme_int_parse (parser, base, &i);
436 g_value_set_int (value, i);
439 /* < 0 => continue */
442 if (!_gtk_css_parser_try_int (parser, &i))
444 _gtk_css_parser_error (parser, "Expected a valid integer value");
448 g_value_set_int (value, i);
453 int_value_print (const GValue *value,
456 g_string_append_printf (string, "%d", g_value_get_int (value));
460 uint_value_parse (GtkCssParser *parser,
466 if (!_gtk_css_parser_try_uint (parser, &u))
468 _gtk_css_parser_error (parser, "Expected a valid unsigned value");
472 g_value_set_uint (value, u);
477 uint_value_print (const GValue *value,
480 g_string_append_printf (string, "%u", g_value_get_uint (value));
484 double_value_parse (GtkCssParser *parser,
490 if (!_gtk_css_parser_try_double (parser, &d))
492 _gtk_css_parser_error (parser, "Expected a number");
496 g_value_set_double (value, d);
501 double_value_print (const GValue *value,
504 string_append_double (string, g_value_get_double (value));
508 float_value_parse (GtkCssParser *parser,
514 if (!_gtk_css_parser_try_double (parser, &d))
516 _gtk_css_parser_error (parser, "Expected a number");
520 g_value_set_float (value, d);
525 float_value_print (const GValue *value,
528 string_append_double (string, g_value_get_float (value));
532 string_value_parse (GtkCssParser *parser,
536 char *str = _gtk_css_parser_read_string (parser);
541 g_value_take_string (value, str);
546 string_value_print (const GValue *value,
549 string_append_string (str, g_value_get_string (value));
553 theming_engine_value_parse (GtkCssParser *parser,
557 GtkThemingEngine *engine;
560 str = _gtk_css_parser_try_ident (parser, TRUE);
563 _gtk_css_parser_error (parser, "Expected a valid theme name");
567 engine = gtk_theming_engine_load (str);
570 _gtk_css_parser_error (parser, "Themeing engine '%s' not found", str);
575 g_value_set_object (value, engine);
581 theming_engine_value_print (const GValue *value,
584 GtkThemingEngine *engine;
587 engine = g_value_get_object (value);
589 g_string_append (string, "none");
592 /* XXX: gtk_theming_engine_get_name()? */
593 g_object_get (engine, "name", &name, NULL);
594 g_string_append (string, name ? name : "none");
600 animation_description_value_parse (GtkCssParser *parser,
604 GtkAnimationDescription *desc;
607 str = _gtk_css_parser_read_value (parser);
611 desc = _gtk_animation_description_from_string (str);
616 _gtk_css_parser_error (parser, "Invalid animation description");
620 g_value_take_boxed (value, desc);
625 animation_description_value_print (const GValue *value,
628 GtkAnimationDescription *desc = g_value_get_boxed (value);
631 g_string_append (string, "none");
633 _gtk_animation_description_print (desc, string);
637 border_value_parse (GtkCssParser *parser,
641 GtkBorder border = { 0, };
644 for (i = 0; i < G_N_ELEMENTS (numbers); i++)
646 if (_gtk_css_parser_begins_with (parser, '-'))
648 /* These are strictly speaking signed, but we want to be able to use them
649 for unsigned types too, as the actual ranges of values make this safe */
650 int res = _gtk_win32_theme_int_parse (parser, base, (int *)&numbers[i]);
652 if (res == 0) /* Parse error, report */
655 if (res < 0) /* Nothing known to expand */
660 if (!_gtk_css_parser_try_uint (parser, &numbers[i]))
663 /* XXX: shouldn't allow spaces here? */
664 _gtk_css_parser_try (parser, "px", TRUE);
670 _gtk_css_parser_error (parser, "Expected valid border");
674 border.top = numbers[0];
676 border.right = numbers[1];
678 border.right = border.top;
680 border.bottom = numbers[2];
682 border.bottom = border.top;
684 border.left = numbers[3];
686 border.left = border.right;
688 g_value_set_boxed (value, &border);
693 border_value_print (const GValue *value, GString *string)
695 const GtkBorder *border = g_value_get_boxed (value);
698 g_string_append (string, "none");
699 else if (border->left != border->right)
700 g_string_append_printf (string, "%d %d %d %d", border->top, border->right, border->bottom, border->left);
701 else if (border->top != border->bottom)
702 g_string_append_printf (string, "%d %d %d", border->top, border->right, border->bottom);
703 else if (border->top != border->left)
704 g_string_append_printf (string, "%d %d", border->top, border->right);
706 g_string_append_printf (string, "%d", border->top);
710 gradient_value_parse (GtkCssParser *parser,
714 GtkGradient *gradient;
715 cairo_pattern_type_t type;
719 if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
721 _gtk_css_parser_error (parser,
722 "Expected '-gtk-gradient'");
726 if (!_gtk_css_parser_try (parser, "(", TRUE))
728 _gtk_css_parser_error (parser,
729 "Expected '(' after '-gtk-gradient'");
733 /* Parse gradient type */
734 if (_gtk_css_parser_try (parser, "linear", TRUE))
735 type = CAIRO_PATTERN_TYPE_LINEAR;
736 else if (_gtk_css_parser_try (parser, "radial", TRUE))
737 type = CAIRO_PATTERN_TYPE_RADIAL;
740 _gtk_css_parser_error (parser,
741 "Gradient type must be 'radial' or 'linear'");
745 /* Parse start/stop position parameters */
746 for (i = 0; i < 2; i++)
748 if (! _gtk_css_parser_try (parser, ",", TRUE))
750 _gtk_css_parser_error (parser,
755 if (_gtk_css_parser_try (parser, "left", TRUE))
757 else if (_gtk_css_parser_try (parser, "right", TRUE))
759 else if (_gtk_css_parser_try (parser, "center", TRUE))
761 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3]))
763 _gtk_css_parser_error (parser,
764 "Expected a valid X coordinate");
768 if (_gtk_css_parser_try (parser, "top", TRUE))
769 coords[i * 3 + 1] = 0;
770 else if (_gtk_css_parser_try (parser, "bottom", TRUE))
771 coords[i * 3 + 1] = 1;
772 else if (_gtk_css_parser_try (parser, "center", TRUE))
773 coords[i * 3 + 1] = 0.5;
774 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3 + 1]))
776 _gtk_css_parser_error (parser,
777 "Expected a valid Y coordinate");
781 if (type == CAIRO_PATTERN_TYPE_RADIAL)
784 if (! _gtk_css_parser_try (parser, ",", TRUE))
786 _gtk_css_parser_error (parser,
791 if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
793 _gtk_css_parser_error (parser,
794 "Expected a numer for the radius");
800 if (type == CAIRO_PATTERN_TYPE_LINEAR)
801 gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
803 gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
804 coords[3], coords[4], coords[5]);
806 while (_gtk_css_parser_try (parser, ",", TRUE))
808 GtkSymbolicColor *color;
811 if (_gtk_css_parser_try (parser, "from", TRUE))
815 if (!_gtk_css_parser_try (parser, "(", TRUE))
817 gtk_gradient_unref (gradient);
818 _gtk_css_parser_error (parser,
824 else if (_gtk_css_parser_try (parser, "to", TRUE))
828 if (!_gtk_css_parser_try (parser, "(", TRUE))
830 gtk_gradient_unref (gradient);
831 _gtk_css_parser_error (parser,
837 else if (_gtk_css_parser_try (parser, "color-stop", TRUE))
839 if (!_gtk_css_parser_try (parser, "(", TRUE))
841 gtk_gradient_unref (gradient);
842 _gtk_css_parser_error (parser,
847 if (!_gtk_css_parser_try_double (parser, &position))
849 gtk_gradient_unref (gradient);
850 _gtk_css_parser_error (parser,
851 "Expected a valid number");
855 if (!_gtk_css_parser_try (parser, ",", TRUE))
857 gtk_gradient_unref (gradient);
858 _gtk_css_parser_error (parser,
865 gtk_gradient_unref (gradient);
866 _gtk_css_parser_error (parser,
867 "Not a valid color-stop definition");
871 color = _gtk_css_parser_read_symbolic_color (parser);
874 gtk_gradient_unref (gradient);
878 gtk_gradient_add_color_stop (gradient, position, color);
879 gtk_symbolic_color_unref (color);
881 if (!_gtk_css_parser_try (parser, ")", TRUE))
883 gtk_gradient_unref (gradient);
884 _gtk_css_parser_error (parser,
890 if (!_gtk_css_parser_try (parser, ")", TRUE))
892 gtk_gradient_unref (gradient);
893 _gtk_css_parser_error (parser,
898 g_value_take_boxed (value, gradient);
903 gradient_value_print (const GValue *value,
906 GtkGradient *gradient = g_value_get_boxed (value);
908 if (gradient == NULL)
909 g_string_append (string, "none");
912 char *s = gtk_gradient_to_string (gradient);
913 g_string_append (string, s);
919 gtk_css_parse_url (GtkCssParser *parser,
925 if (_gtk_css_parser_try (parser, "url", FALSE))
927 if (!_gtk_css_parser_try (parser, "(", TRUE))
929 _gtk_css_parser_skip_whitespace (parser);
930 if (_gtk_css_parser_try (parser, "(", TRUE))
934 error = g_error_new_literal (GTK_CSS_PROVIDER_ERROR,
935 GTK_CSS_PROVIDER_ERROR_DEPRECATED,
936 "Whitespace between 'url' and '(' is deprecated");
938 _gtk_css_parser_take_error (parser, error);
942 _gtk_css_parser_error (parser, "Expected '(' after 'url'");
947 path = _gtk_css_parser_read_string (parser);
951 if (!_gtk_css_parser_try (parser, ")", TRUE))
953 _gtk_css_parser_error (parser, "No closing ')' found for 'url'");
960 path = _gtk_css_parser_try_name (parser, TRUE);
963 _gtk_css_parser_error (parser, "Not a valid url");
968 file = g_file_resolve_relative_path (base, path);
975 pattern_value_parse (GtkCssParser *parser,
979 if (_gtk_css_parser_begins_with (parser, '-'))
982 res = _gtk_win32_theme_part_parse (parser, base, value);
985 /* < 0 => continue */
986 g_value_unset (value);
987 g_value_init (value, GTK_TYPE_GRADIENT);
988 return gradient_value_parse (parser, base, value);
992 GError *error = NULL;
996 cairo_surface_t *surface;
997 cairo_pattern_t *pattern;
999 cairo_matrix_t matrix;
1001 file = gtk_css_parse_url (parser, base);
1005 path = g_file_get_path (file);
1006 g_object_unref (file);
1008 pixbuf = gdk_pixbuf_new_from_file (path, &error);
1012 _gtk_css_parser_take_error (parser, error);
1016 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1017 gdk_pixbuf_get_width (pixbuf),
1018 gdk_pixbuf_get_height (pixbuf));
1019 cr = cairo_create (surface);
1020 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
1022 pattern = cairo_pattern_create_for_surface (surface);
1024 cairo_matrix_init_scale (&matrix,
1025 gdk_pixbuf_get_width (pixbuf),
1026 gdk_pixbuf_get_height (pixbuf));
1027 cairo_pattern_set_matrix (pattern, &matrix);
1029 cairo_surface_destroy (surface);
1031 g_object_unref (pixbuf);
1033 g_value_take_boxed (value, pattern);
1039 static cairo_status_t
1040 surface_write (void *closure,
1041 const unsigned char *data,
1042 unsigned int length)
1044 g_byte_array_append (closure, data, length);
1046 return CAIRO_STATUS_SUCCESS;
1050 surface_print (cairo_surface_t *surface,
1053 #if CAIRO_HAS_PNG_FUNCTIONS
1057 array = g_byte_array_new ();
1058 cairo_surface_write_to_png_stream (surface, surface_write, array);
1059 base64 = g_base64_encode (array->data, array->len);
1060 g_byte_array_free (array, TRUE);
1062 g_string_append (string, "url(\"data:image/png;base64,");
1063 g_string_append (string, base64);
1064 g_string_append (string, "\")");
1068 g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
1073 pattern_value_print (const GValue *value,
1076 cairo_pattern_t *pattern;
1077 cairo_surface_t *surface;
1079 pattern = g_value_get_boxed (value);
1081 if (pattern == NULL)
1083 g_string_append (string, "none");
1087 switch (cairo_pattern_get_type (pattern))
1089 case CAIRO_PATTERN_TYPE_SURFACE:
1090 if (cairo_pattern_get_surface (pattern, &surface) != CAIRO_STATUS_SUCCESS)
1092 g_assert_not_reached ();
1094 surface_print (surface, string);
1096 case CAIRO_PATTERN_TYPE_SOLID:
1097 case CAIRO_PATTERN_TYPE_LINEAR:
1098 case CAIRO_PATTERN_TYPE_RADIAL:
1100 g_assert_not_reached ();
1106 shadow_value_parse (GtkCssParser *parser,
1110 gboolean have_inset, have_color, have_lengths;
1111 gdouble hoffset, voffset, blur, spread;
1112 GtkSymbolicColor *color;
1116 shadow = _gtk_shadow_new ();
1120 have_inset = have_lengths = have_color = FALSE;
1122 for (i = 0; i < 3; i++)
1125 _gtk_css_parser_try (parser, "inset", TRUE))
1131 if (!have_lengths &&
1132 _gtk_css_parser_try_double (parser, &hoffset))
1134 have_lengths = TRUE;
1136 if (!_gtk_css_parser_try_double (parser, &voffset))
1138 _gtk_css_parser_error (parser, "Horizontal and vertical offsets are required");
1139 _gtk_shadow_unref (shadow);
1143 if (!_gtk_css_parser_try_double (parser, &blur))
1146 if (!_gtk_css_parser_try_double (parser, &spread))
1156 /* XXX: the color is optional and UA-defined if it's missing,
1157 * but it doesn't really make sense for us...
1159 color = _gtk_css_parser_read_symbolic_color (parser);
1163 _gtk_shadow_unref (shadow);
1169 if (!have_color || !have_lengths)
1171 _gtk_css_parser_error (parser, "Must specify at least color and offsets");
1172 _gtk_shadow_unref (shadow);
1176 _gtk_shadow_append (shadow,
1181 gtk_symbolic_color_unref (color);
1184 while (_gtk_css_parser_try (parser, ",", TRUE));
1186 g_value_take_boxed (value, shadow);
1191 shadow_value_print (const GValue *value,
1196 shadow = g_value_get_boxed (value);
1199 g_string_append (string, "none");
1201 _gtk_shadow_print (shadow, string);
1205 background_repeat_value_parse (GtkCssParser *parser,
1209 GtkCssBackgroundRepeat repeat;
1212 if (!enum_parse (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT_STYLE, &style))
1215 repeat.repeat = style;
1217 g_value_set_boxed (value, &repeat);
1223 background_repeat_value_print (const GValue *value,
1226 GtkCssBackgroundRepeat *repeat;
1228 repeat = g_value_get_boxed (value);
1230 enum_print (repeat->repeat, GTK_TYPE_CSS_BACKGROUND_REPEAT_STYLE, string);
1234 border_image_repeat_value_parse (GtkCssParser *parser,
1238 GtkCssBorderImageRepeat image_repeat;
1239 GtkCssBorderRepeatStyle styles[2];
1242 for (i = 0; i < 2; i++)
1244 if (_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, &v))
1248 styles[1] = styles[0] = GTK_CSS_REPEAT_STYLE_STRETCH;
1252 styles[i] = styles[0];
1255 image_repeat.hrepeat = styles[0];
1256 image_repeat.vrepeat = styles[1];
1258 g_value_set_boxed (value, &image_repeat);
1264 border_image_repeat_value_print (const GValue *value,
1267 GtkCssBorderImageRepeat *image_repeat;
1269 image_repeat = g_value_get_boxed (value);
1271 enum_print (image_repeat->hrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1272 if (image_repeat->hrepeat != image_repeat->vrepeat)
1274 g_string_append (string, " ");
1275 enum_print (image_repeat->vrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1280 border_image_value_parse (GtkCssParser *parser,
1284 GValue temp = G_VALUE_INIT;
1285 cairo_pattern_t *pattern = NULL;
1286 gconstpointer *boxed = NULL;
1288 GtkBorder slice, *width = NULL, *parsed_slice;
1289 GtkCssBorderImageRepeat repeat, *parsed_repeat;
1290 gboolean retval = FALSE;
1291 GtkBorderImage *image = NULL;
1293 g_value_init (&temp, CAIRO_GOBJECT_TYPE_PATTERN);
1295 if (!pattern_value_parse (parser, base, &temp))
1298 boxed_type = G_VALUE_TYPE (&temp);
1299 if (boxed_type != CAIRO_GOBJECT_TYPE_PATTERN)
1300 boxed = g_value_dup_boxed (&temp);
1302 pattern = g_value_dup_boxed (&temp);
1304 g_value_unset (&temp);
1305 g_value_init (&temp, GTK_TYPE_BORDER);
1307 if (!border_value_parse (parser, base, &temp))
1310 parsed_slice = g_value_get_boxed (&temp);
1311 slice = *parsed_slice;
1313 if (_gtk_css_parser_try (parser, "/", TRUE))
1315 g_value_unset (&temp);
1316 g_value_init (&temp, GTK_TYPE_BORDER);
1318 if (!border_value_parse (parser, base, &temp))
1321 width = g_value_dup_boxed (&temp);
1324 g_value_unset (&temp);
1325 g_value_init (&temp, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
1327 if (!border_image_repeat_value_parse (parser, base, &temp))
1330 parsed_repeat = g_value_get_boxed (&temp);
1331 repeat = *parsed_repeat;
1333 g_value_unset (&temp);
1336 image = _gtk_border_image_new_for_boxed (boxed_type, boxed, &slice, width, &repeat);
1337 else if (pattern != NULL)
1338 image = _gtk_border_image_new (pattern, &slice, width, &repeat);
1343 g_value_take_boxed (value, image);
1347 if (pattern != NULL)
1348 cairo_pattern_destroy (pattern);
1351 g_boxed_free (boxed_type, boxed);
1354 gtk_border_free (width);
1360 enum_value_parse (GtkCssParser *parser,
1366 if (enum_parse (parser, G_VALUE_TYPE (value), &v))
1368 g_value_set_enum (value, v);
1376 enum_value_print (const GValue *value,
1379 enum_print (g_value_get_enum (value), G_VALUE_TYPE (value), string);
1383 flags_value_parse (GtkCssParser *parser,
1387 GFlagsClass *flags_class;
1388 GFlagsValue *flag_value;
1392 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1395 str = _gtk_css_parser_try_ident (parser, TRUE);
1398 _gtk_css_parser_error (parser, "Expected an identifier");
1399 g_type_class_unref (flags_class);
1403 flag_value = g_flags_get_value_by_nick (flags_class, str);
1406 _gtk_css_parser_error (parser,
1407 "Unknown flag value '%s' for type '%s'",
1408 str, g_type_name (G_VALUE_TYPE (value)));
1409 /* XXX Do we want to return FALSE here? We can get
1410 * forward-compatibility for new values this way
1413 g_type_class_unref (flags_class);
1419 while (_gtk_css_parser_try (parser, ",", FALSE));
1421 g_type_class_unref (flags_class);
1423 g_value_set_enum (value, flags);
1429 flags_value_print (const GValue *value,
1432 GFlagsClass *flags_class;
1435 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1436 flags = g_value_get_flags (value);
1438 for (i = 0; i < flags_class->n_values; i++)
1440 GFlagsValue *flags_value = &flags_class->values[i];
1442 if (flags & flags_value->value)
1444 if (string->len != 0)
1445 g_string_append (string, ", ");
1447 g_string_append (string, flags_value->value_nick);
1451 g_type_class_unref (flags_class);
1455 bindings_value_parse (GtkCssParser *parser,
1460 GtkBindingSet *binding_set;
1463 array = g_ptr_array_new ();
1466 name = _gtk_css_parser_try_ident (parser, TRUE);
1469 _gtk_css_parser_error (parser, "Not a valid binding name");
1470 g_ptr_array_free (array, TRUE);
1474 binding_set = gtk_binding_set_find (name);
1478 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
1483 g_ptr_array_add (array, binding_set);
1486 while (_gtk_css_parser_try (parser, ",", TRUE));
1488 g_value_take_boxed (value, array);
1494 bindings_value_print (const GValue *value,
1500 array = g_value_get_boxed (value);
1502 for (i = 0; i < array->len; i++)
1504 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
1507 g_string_append (string, ", ");
1508 g_string_append (string, binding_set->set_name);
1513 border_corner_radius_value_parse (GtkCssParser *parser,
1517 GtkCssBorderCornerRadius corner;
1519 if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
1521 _gtk_css_parser_error (parser, "Expected a number");
1524 else if (corner.horizontal < 0)
1527 if (!_gtk_css_parser_try_double (parser, &corner.vertical))
1528 corner.vertical = corner.horizontal;
1529 else if (corner.vertical < 0)
1532 g_value_set_boxed (value, &corner);
1536 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1541 border_corner_radius_value_print (const GValue *value,
1544 GtkCssBorderCornerRadius *corner;
1546 corner = g_value_get_boxed (value);
1550 g_string_append (string, "none");
1554 string_append_double (string, corner->horizontal);
1555 if (corner->horizontal != corner->vertical)
1557 g_string_append_c (string, ' ');
1558 string_append_double (string, corner->vertical);
1563 border_radius_value_parse (GtkCssParser *parser,
1567 GtkCssBorderRadius border;
1569 if (!_gtk_css_parser_try_double (parser, &border.top_left.horizontal))
1571 _gtk_css_parser_error (parser, "Expected a number");
1574 else if (border.top_left.horizontal < 0)
1577 if (_gtk_css_parser_try_double (parser, &border.top_right.horizontal))
1579 if (border.top_right.horizontal < 0)
1581 if (_gtk_css_parser_try_double (parser, &border.bottom_right.horizontal))
1583 if (border.bottom_right.horizontal < 0)
1585 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.horizontal))
1586 border.bottom_left.horizontal = border.top_right.horizontal;
1587 else if (border.bottom_left.horizontal < 0)
1592 border.bottom_right.horizontal = border.top_left.horizontal;
1593 border.bottom_left.horizontal = border.top_right.horizontal;
1598 border.top_right.horizontal = border.top_left.horizontal;
1599 border.bottom_right.horizontal = border.top_left.horizontal;
1600 border.bottom_left.horizontal = border.top_left.horizontal;
1603 if (_gtk_css_parser_try (parser, "/", TRUE))
1605 if (!_gtk_css_parser_try_double (parser, &border.top_left.vertical))
1607 _gtk_css_parser_error (parser, "Expected a number");
1610 else if (border.top_left.vertical < 0)
1613 if (_gtk_css_parser_try_double (parser, &border.top_right.vertical))
1615 if (border.top_right.vertical < 0)
1617 if (_gtk_css_parser_try_double (parser, &border.bottom_right.vertical))
1619 if (border.bottom_right.vertical < 0)
1621 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.vertical))
1622 border.bottom_left.vertical = border.top_right.vertical;
1623 else if (border.bottom_left.vertical < 0)
1628 border.bottom_right.vertical = border.top_left.vertical;
1629 border.bottom_left.vertical = border.top_right.vertical;
1634 border.top_right.vertical = border.top_left.vertical;
1635 border.bottom_right.vertical = border.top_left.vertical;
1636 border.bottom_left.vertical = border.top_left.vertical;
1641 border.top_left.vertical = border.top_left.horizontal;
1642 border.top_right.vertical = border.top_right.horizontal;
1643 border.bottom_right.vertical = border.bottom_right.horizontal;
1644 border.bottom_left.vertical = border.bottom_left.horizontal;
1647 /* border-radius is an int property for backwards-compat reasons */
1648 g_value_unset (value);
1649 g_value_init (value, GTK_TYPE_CSS_BORDER_RADIUS);
1650 g_value_set_boxed (value, &border);
1655 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1660 border_radius_value_print (const GValue *value,
1663 GtkCssBorderRadius *border;
1665 border = g_value_get_boxed (value);
1669 g_string_append (string, "none");
1673 string_append_double (string, border->top_left.horizontal);
1674 if (border->top_left.horizontal != border->top_right.horizontal ||
1675 border->top_left.horizontal != border->bottom_right.horizontal ||
1676 border->top_left.horizontal != border->bottom_left.horizontal)
1678 g_string_append_c (string, ' ');
1679 string_append_double (string, border->top_right.horizontal);
1680 if (border->top_left.horizontal != border->bottom_right.horizontal ||
1681 border->top_right.horizontal != border->bottom_left.horizontal)
1683 g_string_append_c (string, ' ');
1684 string_append_double (string, border->bottom_right.horizontal);
1685 if (border->top_right.horizontal != border->bottom_left.horizontal)
1687 g_string_append_c (string, ' ');
1688 string_append_double (string, border->bottom_left.horizontal);
1693 if (border->top_left.horizontal != border->top_left.vertical ||
1694 border->top_right.horizontal != border->top_right.vertical ||
1695 border->bottom_right.horizontal != border->bottom_right.vertical ||
1696 border->bottom_left.horizontal != border->bottom_left.vertical)
1698 g_string_append (string, " / ");
1699 string_append_double (string, border->top_left.vertical);
1700 if (border->top_left.vertical != border->top_right.vertical ||
1701 border->top_left.vertical != border->bottom_right.vertical ||
1702 border->top_left.vertical != border->bottom_left.vertical)
1704 g_string_append_c (string, ' ');
1705 string_append_double (string, border->top_right.vertical);
1706 if (border->top_left.vertical != border->bottom_right.vertical ||
1707 border->top_right.vertical != border->bottom_left.vertical)
1709 g_string_append_c (string, ' ');
1710 string_append_double (string, border->bottom_right.vertical);
1711 if (border->top_right.vertical != border->bottom_left.vertical)
1713 g_string_append_c (string, ' ');
1714 string_append_double (string, border->bottom_left.vertical);
1723 transparent_color_value_parse (GtkCssParser *parser,
1727 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1729 GdkRGBA transparent = { 0, 0, 0, 0 };
1731 g_value_set_boxed (value, &transparent);
1736 return rgba_value_parse (parser, base, value);
1740 border_color_shorthand_value_parse (GtkCssParser *parser,
1744 GtkSymbolicColor *symbolic;
1747 array = g_ptr_array_new_with_free_func ((GDestroyNotify) gtk_symbolic_color_unref);
1751 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1753 GdkRGBA transparent = { 0, 0, 0, 0 };
1755 symbolic = gtk_symbolic_color_new_literal (&transparent);
1759 symbolic = _gtk_css_parser_read_symbolic_color (parser);
1761 if (symbolic == NULL)
1765 g_ptr_array_add (array, symbolic);
1767 while (array->len < 4 &&
1768 !_gtk_css_parser_is_eof (parser) &&
1769 !_gtk_css_parser_begins_with (parser, ';') &&
1770 !_gtk_css_parser_begins_with (parser, '}'));
1775 g_assert_not_reached ();
1778 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1781 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1784 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 1)));
1790 g_value_unset (value);
1791 g_value_init (value, G_TYPE_PTR_ARRAY);
1792 g_value_take_boxed (value, array);
1800 unpack_border (const GValue *value,
1807 GParameter *parameter = g_new0 (GParameter, 4);
1808 GtkBorder *border = g_value_get_boxed (value);
1810 parameter[0].name = top;
1811 g_value_init (¶meter[0].value, G_TYPE_INT);
1812 g_value_set_int (¶meter[0].value, border->top);
1813 parameter[1].name = left;
1814 g_value_init (¶meter[1].value, G_TYPE_INT);
1815 g_value_set_int (¶meter[1].value, border->left);
1816 parameter[2].name = bottom;
1817 g_value_init (¶meter[2].value, G_TYPE_INT);
1818 g_value_set_int (¶meter[2].value, border->bottom);
1819 parameter[3].name = right;
1820 g_value_init (¶meter[3].value, G_TYPE_INT);
1821 g_value_set_int (¶meter[3].value, border->right);
1828 pack_border (GValue *value,
1829 GtkStyleProperties *props,
1830 GtkStateFlags state,
1839 gtk_style_properties_get (props,
1852 g_value_set_boxed (value, &border);
1856 unpack_border_width (const GValue *value,
1859 return unpack_border (value, n_params,
1860 "border-top-width", "border-left-width",
1861 "border-bottom-width", "border-right-width");
1865 pack_border_width (GValue *value,
1866 GtkStyleProperties *props,
1867 GtkStateFlags state,
1868 GtkStylePropertyContext *context)
1870 pack_border (value, props, state,
1871 "border-top-width", "border-left-width",
1872 "border-bottom-width", "border-right-width");
1876 unpack_padding (const GValue *value,
1879 return unpack_border (value, n_params,
1880 "padding-top", "padding-left",
1881 "padding-bottom", "padding-right");
1885 pack_padding (GValue *value,
1886 GtkStyleProperties *props,
1887 GtkStateFlags state,
1888 GtkStylePropertyContext *context)
1890 pack_border (value, props, state,
1891 "padding-top", "padding-left",
1892 "padding-bottom", "padding-right");
1896 unpack_margin (const GValue *value,
1899 return unpack_border (value, n_params,
1900 "margin-top", "margin-left",
1901 "margin-bottom", "margin-right");
1905 pack_margin (GValue *value,
1906 GtkStyleProperties *props,
1907 GtkStateFlags state,
1908 GtkStylePropertyContext *context)
1910 pack_border (value, props, state,
1911 "margin-top", "margin-left",
1912 "margin-bottom", "margin-right");
1916 unpack_border_radius (const GValue *value,
1919 GParameter *parameter = g_new0 (GParameter, 4);
1920 GtkCssBorderRadius *border;
1922 if (G_VALUE_HOLDS_BOXED (value))
1923 border = g_value_get_boxed (value);
1927 parameter[0].name = "border-top-left-radius";
1928 g_value_init (¶meter[0].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1929 parameter[1].name = "border-top-right-radius";
1930 g_value_init (¶meter[1].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1931 parameter[2].name = "border-bottom-right-radius";
1932 g_value_init (¶meter[2].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1933 parameter[3].name = "border-bottom-left-radius";
1934 g_value_init (¶meter[3].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1937 g_value_set_boxed (¶meter[0].value, &border->top_left);
1938 g_value_set_boxed (¶meter[1].value, &border->top_right);
1939 g_value_set_boxed (¶meter[2].value, &border->bottom_right);
1940 g_value_set_boxed (¶meter[3].value, &border->bottom_left);
1948 pack_border_radius (GValue *value,
1949 GtkStyleProperties *props,
1950 GtkStateFlags state,
1951 GtkStylePropertyContext *context)
1953 GtkCssBorderCornerRadius *top_left;
1955 /* NB: We are an int property, so we have to resolve to an int here.
1956 * So we just resolve to an int. We pick one and stick to it.
1957 * Lesson learned: Don't query border-radius shorthand, query the
1958 * real properties instead. */
1959 gtk_style_properties_get (props,
1961 "border-top-left-radius", &top_left,
1965 g_value_set_int (value, top_left->horizontal);
1971 unpack_font_description (const GValue *value,
1974 GParameter *parameter = g_new0 (GParameter, 5);
1975 PangoFontDescription *description;
1979 /* For backwards compat, we only unpack values that are indeed set.
1980 * For strict CSS conformance we need to unpack all of them.
1981 * Note that we do set all of them in the parse function, so it
1982 * will not have effects when parsing CSS files. It will though
1983 * for custom style providers.
1986 description = g_value_get_boxed (value);
1990 mask = pango_font_description_get_set_fields (description);
1994 if (mask & PANGO_FONT_MASK_FAMILY)
1996 GPtrArray *strv = g_ptr_array_new ();
1998 g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
1999 g_ptr_array_add (strv, NULL);
2000 parameter[n].name = "font-family";
2001 g_value_init (¶meter[n].value, G_TYPE_STRV);
2002 g_value_take_boxed (¶meter[n].value,
2003 g_ptr_array_free (strv, FALSE));
2007 if (mask & PANGO_FONT_MASK_STYLE)
2009 parameter[n].name = "font-style";
2010 g_value_init (¶meter[n].value, PANGO_TYPE_STYLE);
2011 g_value_set_enum (¶meter[n].value,
2012 pango_font_description_get_style (description));
2016 if (mask & PANGO_FONT_MASK_VARIANT)
2018 parameter[n].name = "font-variant";
2019 g_value_init (¶meter[n].value, PANGO_TYPE_VARIANT);
2020 g_value_set_enum (¶meter[n].value,
2021 pango_font_description_get_variant (description));
2025 if (mask & PANGO_FONT_MASK_WEIGHT)
2027 parameter[n].name = "font-weight";
2028 g_value_init (¶meter[n].value, PANGO_TYPE_WEIGHT);
2029 g_value_set_enum (¶meter[n].value,
2030 pango_font_description_get_weight (description));
2034 if (mask & PANGO_FONT_MASK_SIZE)
2036 parameter[n].name = "font-size";
2037 g_value_init (¶meter[n].value, G_TYPE_DOUBLE);
2038 g_value_set_double (¶meter[n].value,
2039 (double) pango_font_description_get_size (description) / PANGO_SCALE);
2049 pack_font_description (GValue *value,
2050 GtkStyleProperties *props,
2051 GtkStateFlags state,
2052 GtkStylePropertyContext *context)
2054 PangoFontDescription *description;
2057 PangoVariant variant;
2061 gtk_style_properties_get (props,
2063 "font-family", &families,
2064 "font-style", &style,
2065 "font-variant", &variant,
2066 "font-weight", &weight,
2070 description = pango_font_description_new ();
2071 /* xxx: Can we set all the families here somehow? */
2073 pango_font_description_set_family (description, families[0]);
2074 pango_font_description_set_size (description, round (size * PANGO_SCALE));
2075 pango_font_description_set_style (description, style);
2076 pango_font_description_set_variant (description, variant);
2077 pango_font_description_set_weight (description, weight);
2079 g_strfreev (families);
2081 g_value_take_boxed (value, description);
2085 unpack_border_color (const GValue *value,
2088 GParameter *parameter = g_new0 (GParameter, 4);
2091 type = G_VALUE_TYPE (value);
2092 if (type == G_TYPE_PTR_ARRAY)
2093 type = GTK_TYPE_SYMBOLIC_COLOR;
2095 parameter[0].name = "border-top-color";
2096 g_value_init (¶meter[0].value, type);
2097 parameter[1].name = "border-right-color";
2098 g_value_init (¶meter[1].value, type);
2099 parameter[2].name = "border-bottom-color";
2100 g_value_init (¶meter[2].value, type);
2101 parameter[3].name = "border-left-color";
2102 g_value_init (¶meter[3].value, type);
2104 if (G_VALUE_TYPE (value) == G_TYPE_PTR_ARRAY)
2106 GPtrArray *array = g_value_get_boxed (value);
2109 for (i = 0; i < 4; i++)
2110 g_value_set_boxed (¶meter[i].value, g_ptr_array_index (array, i));
2114 /* can be RGBA or symbolic color */
2115 gpointer p = g_value_get_boxed (value);
2117 g_value_set_boxed (¶meter[0].value, p);
2118 g_value_set_boxed (¶meter[1].value, p);
2119 g_value_set_boxed (¶meter[2].value, p);
2120 g_value_set_boxed (¶meter[3].value, p);
2128 pack_border_color (GValue *value,
2129 GtkStyleProperties *props,
2130 GtkStateFlags state,
2131 GtkStylePropertyContext *context)
2133 /* NB: We are a color property, so we have to resolve to a color here.
2134 * So we just resolve to a color. We pick one and stick to it.
2135 * Lesson learned: Don't query border-color shorthand, query the
2136 * real properties instead. */
2137 g_value_unset (value);
2138 gtk_style_properties_get_property (props, "border-top-color", state, value);
2141 /*** UNSET FUNCS ***/
2144 unset_font_description (GtkStyleProperties *props,
2145 GtkStateFlags state)
2147 gtk_style_properties_unset_property (props, "font-family", state);
2148 gtk_style_properties_unset_property (props, "font-style", state);
2149 gtk_style_properties_unset_property (props, "font-variant", state);
2150 gtk_style_properties_unset_property (props, "font-weight", state);
2151 gtk_style_properties_unset_property (props, "font-size", state);
2155 unset_margin (GtkStyleProperties *props,
2156 GtkStateFlags state)
2158 gtk_style_properties_unset_property (props, "margin-top", state);
2159 gtk_style_properties_unset_property (props, "margin-right", state);
2160 gtk_style_properties_unset_property (props, "margin-bottom", state);
2161 gtk_style_properties_unset_property (props, "margin-left", state);
2165 unset_padding (GtkStyleProperties *props,
2166 GtkStateFlags state)
2168 gtk_style_properties_unset_property (props, "padding-top", state);
2169 gtk_style_properties_unset_property (props, "padding-right", state);
2170 gtk_style_properties_unset_property (props, "padding-bottom", state);
2171 gtk_style_properties_unset_property (props, "padding-left", state);
2175 unset_border_width (GtkStyleProperties *props,
2176 GtkStateFlags state)
2178 gtk_style_properties_unset_property (props, "border-top-width", state);
2179 gtk_style_properties_unset_property (props, "border-right-width", state);
2180 gtk_style_properties_unset_property (props, "border-bottom-width", state);
2181 gtk_style_properties_unset_property (props, "border-left-width", state);
2185 unset_border_radius (GtkStyleProperties *props,
2186 GtkStateFlags state)
2188 gtk_style_properties_unset_property (props, "border-top-right-radius", state);
2189 gtk_style_properties_unset_property (props, "border-bottom-right-radius", state);
2190 gtk_style_properties_unset_property (props, "border-bottom-left-radius", state);
2191 gtk_style_properties_unset_property (props, "border-top-left-radius", state);
2195 unset_border_color (GtkStyleProperties *props,
2196 GtkStateFlags state)
2198 gtk_style_properties_unset_property (props, "border-top-color", state);
2199 gtk_style_properties_unset_property (props, "border-right-color", state);
2200 gtk_style_properties_unset_property (props, "border-bottom-color", state);
2201 gtk_style_properties_unset_property (props, "border-left-color", state);
2205 unset_border_image (GtkStyleProperties *props,
2206 GtkStateFlags state)
2208 gtk_style_properties_unset_property (props, "border-image-source", state);
2209 gtk_style_properties_unset_property (props, "border-image-slice", state);
2210 gtk_style_properties_unset_property (props, "border-image-repeat", state);
2211 gtk_style_properties_unset_property (props, "border-image-width", state);
2217 _gtk_style_property_get_count (void)
2219 return __style_property_array ? __style_property_array->len : 0;
2222 const GtkStyleProperty *
2223 _gtk_style_property_get (guint id)
2225 g_assert (__style_property_array);
2227 return g_ptr_array_index (__style_property_array, id);
2231 _gtk_style_property_generate_id (GtkStyleProperty *node)
2233 if (__style_property_array == NULL)
2234 __style_property_array = g_ptr_array_new ();
2236 node->id = __style_property_array->len;
2237 g_ptr_array_add (__style_property_array, node);
2241 css_string_funcs_init (void)
2243 if (G_LIKELY (parse_funcs != NULL))
2246 parse_funcs = g_hash_table_new (NULL, NULL);
2247 print_funcs = g_hash_table_new (NULL, NULL);
2249 register_conversion_function (GDK_TYPE_RGBA,
2252 register_conversion_function (GDK_TYPE_COLOR,
2255 register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
2256 symbolic_color_value_parse,
2257 symbolic_color_value_print);
2258 register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
2259 font_description_value_parse,
2260 font_description_value_print);
2261 register_conversion_function (G_TYPE_BOOLEAN,
2262 boolean_value_parse,
2263 boolean_value_print);
2264 register_conversion_function (G_TYPE_INT,
2267 register_conversion_function (G_TYPE_UINT,
2270 register_conversion_function (G_TYPE_DOUBLE,
2272 double_value_print);
2273 register_conversion_function (G_TYPE_FLOAT,
2276 register_conversion_function (G_TYPE_STRING,
2278 string_value_print);
2279 register_conversion_function (GTK_TYPE_THEMING_ENGINE,
2280 theming_engine_value_parse,
2281 theming_engine_value_print);
2282 register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
2283 animation_description_value_parse,
2284 animation_description_value_print);
2285 register_conversion_function (GTK_TYPE_BORDER,
2287 border_value_print);
2288 register_conversion_function (GTK_TYPE_GRADIENT,
2289 gradient_value_parse,
2290 gradient_value_print);
2291 register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
2292 pattern_value_parse,
2293 pattern_value_print);
2294 register_conversion_function (GTK_TYPE_BORDER_IMAGE,
2295 border_image_value_parse,
2297 register_conversion_function (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
2298 border_image_repeat_value_parse,
2299 border_image_repeat_value_print);
2300 register_conversion_function (GTK_TYPE_SHADOW,
2302 shadow_value_print);
2303 register_conversion_function (G_TYPE_ENUM,
2306 register_conversion_function (G_TYPE_FLAGS,
2309 register_conversion_function (GTK_TYPE_CSS_BACKGROUND_REPEAT,
2310 background_repeat_value_parse,
2311 background_repeat_value_print);
2315 _gtk_style_property_parse_value (const GtkStyleProperty *property,
2317 GtkCssParser *parser,
2320 GtkStyleParseFunc func;
2322 g_return_val_if_fail (value != NULL, FALSE);
2323 g_return_val_if_fail (parser != NULL, FALSE);
2325 css_string_funcs_init ();
2329 if (_gtk_css_parser_try (parser, "none", TRUE))
2331 /* Insert the default value, so it has an opportunity
2332 * to override other style providers when merged
2334 g_param_value_set_default (property->pspec, value);
2337 else if (property->property_parse_func)
2339 GError *error = NULL;
2343 value_str = _gtk_css_parser_read_value (parser);
2344 if (value_str == NULL)
2347 success = (*property->property_parse_func) (value_str, value, &error);
2354 func = property->parse_func;
2360 func = g_hash_table_lookup (parse_funcs,
2361 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2363 func = g_hash_table_lookup (parse_funcs,
2364 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2368 _gtk_css_parser_error (parser,
2369 "Cannot convert to type '%s'",
2370 g_type_name (G_VALUE_TYPE (value)));
2374 return (*func) (parser, base, value);
2378 _gtk_style_property_print_value (const GtkStyleProperty *property,
2379 const GValue *value,
2382 GtkStylePrintFunc func;
2384 css_string_funcs_init ();
2387 func = property->print_func;
2392 func = g_hash_table_lookup (print_funcs,
2393 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2395 func = g_hash_table_lookup (print_funcs,
2396 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2400 char *s = g_strdup_value_contents (value);
2401 g_string_append (string, s);
2406 func (value, string);
2410 _gtk_style_property_default_value (const GtkStyleProperty *property,
2411 GtkStyleProperties *properties,
2412 GtkStateFlags state,
2415 g_value_copy (&property->initial_value, value);
2419 _gtk_style_property_is_inherit (const GtkStyleProperty *property)
2421 g_return_val_if_fail (property != NULL, FALSE);
2423 return property->flags & GTK_STYLE_PROPERTY_INHERIT ? TRUE : FALSE;
2427 _gtk_style_property_get_id (const GtkStyleProperty *property)
2429 g_return_val_if_fail (property != NULL, FALSE);
2431 return property->id;
2435 resolve_color (GtkStyleProperties *props,
2440 /* Resolve symbolic color to GdkRGBA */
2441 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
2444 /* Store it back, this is where GdkRGBA caching happens */
2445 g_value_unset (value);
2446 g_value_init (value, GDK_TYPE_RGBA);
2447 g_value_set_boxed (value, &color);
2453 resolve_color_rgb (GtkStyleProperties *props,
2456 GdkColor color = { 0 };
2459 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
2462 color.red = rgba.red * 65535. + 0.5;
2463 color.green = rgba.green * 65535. + 0.5;
2464 color.blue = rgba.blue * 65535. + 0.5;
2466 g_value_unset (value);
2467 g_value_init (value, GDK_TYPE_COLOR);
2468 g_value_set_boxed (value, &color);
2474 resolve_win32_theme_part (GtkStyleProperties *props,
2477 GtkStylePropertyContext *context)
2479 GtkWin32ThemePart *part;
2480 cairo_pattern_t *pattern;
2482 part = g_value_get_boxed (value);
2486 pattern = _gtk_win32_theme_part_render (part, context->width, context->height);
2488 g_value_take_boxed (value_out, pattern);
2495 resolve_gradient (GtkStyleProperties *props,
2498 cairo_pattern_t *gradient;
2500 if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
2503 /* Store it back, this is where cairo_pattern_t caching happens */
2504 g_value_unset (value);
2505 g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
2506 g_value_take_boxed (value, gradient);
2512 resolve_shadow (GtkStyleProperties *props,
2515 GtkShadow *resolved, *base;
2517 base = g_value_get_boxed (value);
2522 if (_gtk_shadow_get_resolved (base))
2525 resolved = _gtk_shadow_resolve (base, props);
2526 if (resolved == NULL)
2529 g_value_take_boxed (value, resolved);
2535 _gtk_style_property_resolve (const GtkStyleProperty *property,
2536 GtkStyleProperties *props,
2537 GtkStateFlags state,
2538 GtkStylePropertyContext *context,
2542 if (G_VALUE_TYPE (val) == GTK_TYPE_CSS_SPECIAL_VALUE)
2544 GtkCssSpecialValue special = g_value_get_enum (val);
2546 g_value_unset (val);
2549 case GTK_CSS_CURRENT_COLOR:
2550 g_assert (property->pspec->value_type == GDK_TYPE_RGBA);
2551 gtk_style_properties_get_property (props, "color", state, val);
2553 case GTK_CSS_INHERIT:
2554 case GTK_CSS_INITIAL:
2556 g_assert_not_reached ();
2559 else if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
2561 if (property->pspec->value_type == GDK_TYPE_RGBA)
2563 if (resolve_color (props, val))
2566 else if (property->pspec->value_type == GDK_TYPE_COLOR)
2568 if (resolve_color_rgb (props, val))
2572 g_value_unset (val);
2573 g_value_init (val, property->pspec->value_type);
2574 _gtk_style_property_default_value (property, props, state, val);
2576 else if (G_VALUE_TYPE (val) == GDK_TYPE_RGBA)
2578 if (g_value_get_boxed (val) == NULL)
2579 _gtk_style_property_default_value (property, props, state, val);
2581 else if (G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
2583 g_return_if_fail (property->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN);
2585 if (!resolve_gradient (props, val))
2587 g_value_unset (val);
2588 g_value_init (val, CAIRO_GOBJECT_TYPE_PATTERN);
2589 _gtk_style_property_default_value (property, props, state, val);
2592 else if (G_VALUE_TYPE (val) == GTK_TYPE_SHADOW)
2594 if (!resolve_shadow (props, val))
2595 _gtk_style_property_default_value (property, props, state, val);
2597 else if (G_VALUE_TYPE (val) == GTK_TYPE_WIN32_THEME_PART)
2599 if (resolve_win32_theme_part (props, val, val_out, context))
2600 return; /* Don't copy val, this sets val_out */
2601 _gtk_style_property_default_value (property, props, state, val);
2605 g_value_copy (val, val_out);
2609 _gtk_style_property_is_shorthand (const GtkStyleProperty *property)
2611 g_return_val_if_fail (property != NULL, FALSE);
2613 return property->pack_func != NULL;
2617 _gtk_style_property_unpack (const GtkStyleProperty *property,
2618 const GValue *value,
2621 g_return_val_if_fail (property != NULL, NULL);
2622 g_return_val_if_fail (property->unpack_func != NULL, NULL);
2623 g_return_val_if_fail (value != NULL, NULL);
2624 g_return_val_if_fail (n_params != NULL, NULL);
2626 return property->unpack_func (value, n_params);
2630 _gtk_style_property_pack (const GtkStyleProperty *property,
2631 GtkStyleProperties *props,
2632 GtkStateFlags state,
2633 GtkStylePropertyContext *context,
2636 g_return_if_fail (property != NULL);
2637 g_return_if_fail (property->pack_func != NULL);
2638 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
2639 g_return_if_fail (G_IS_VALUE (value));
2641 property->pack_func (value, props, state, context);
2644 #define rgba_init(rgba, r, g, b, a) G_STMT_START{ \
2645 (rgba)->red = (r); \
2646 (rgba)->green = (g); \
2647 (rgba)->blue = (b); \
2648 (rgba)->alpha = (a); \
2651 gtk_style_property_init (void)
2653 GValue value = { 0, };
2654 char *default_font_family[] = { "Sans", NULL };
2657 if (G_LIKELY (properties))
2660 /* stuff is never freed, so no need for free functions */
2661 properties = g_hash_table_new (g_str_hash, g_str_equal);
2663 /* note that gtk_style_properties_register_property() calls this function,
2664 * so make sure we're sanely inited to avoid infloops */
2666 g_value_init (&value, GDK_TYPE_RGBA);
2667 rgba_init (&rgba, 1, 1, 1, 1);
2668 g_value_set_boxed (&value, &rgba);
2669 _gtk_style_property_register (g_param_spec_boxed ("color",
2673 GTK_STYLE_PROPERTY_INHERIT,
2681 rgba_init (&rgba, 0, 0, 0, 0);
2682 g_value_set_boxed (&value, &rgba);
2683 _gtk_style_property_register (g_param_spec_boxed ("background-color",
2691 transparent_color_value_parse,
2695 g_value_unset (&value);
2697 g_value_init (&value, G_TYPE_STRV);
2698 g_value_set_boxed (&value, default_font_family);
2699 _gtk_style_property_register (g_param_spec_boxed ("font-family",
2703 GTK_STYLE_PROPERTY_INHERIT,
2708 font_family_value_print,
2711 g_value_unset (&value);
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 g_value_init (&value, G_TYPE_DOUBLE);
2753 g_value_set_double (&value, 10);
2754 _gtk_style_property_register (g_param_spec_double ("font-size",
2757 0, G_MAXDOUBLE, 0, 0),
2758 GTK_STYLE_PROPERTY_INHERIT,
2766 g_value_unset (&value);
2767 _gtk_style_property_register (g_param_spec_boxed ("font",
2770 PANGO_TYPE_FONT_DESCRIPTION, 0),
2771 GTK_STYLE_PROPERTY_INHERIT,
2773 unpack_font_description,
2774 pack_font_description,
2775 font_description_value_parse,
2776 font_description_value_print,
2778 unset_font_description);
2780 _gtk_style_property_register (g_param_spec_boxed ("text-shadow",
2783 GTK_TYPE_SHADOW, 0),
2784 GTK_STYLE_PROPERTY_INHERIT,
2793 _gtk_style_property_register (g_param_spec_boxed ("icon-shadow",
2796 GTK_TYPE_SHADOW, 0),
2797 GTK_STYLE_PROPERTY_INHERIT,
2806 gtk_style_properties_register_property (NULL,
2807 g_param_spec_boxed ("box-shadow",
2810 GTK_TYPE_SHADOW, 0));
2811 gtk_style_properties_register_property (NULL,
2812 g_param_spec_int ("margin-top",
2815 0, G_MAXINT, 0, 0));
2816 gtk_style_properties_register_property (NULL,
2817 g_param_spec_int ("margin-left",
2820 0, G_MAXINT, 0, 0));
2821 gtk_style_properties_register_property (NULL,
2822 g_param_spec_int ("margin-bottom",
2825 0, G_MAXINT, 0, 0));
2826 gtk_style_properties_register_property (NULL,
2827 g_param_spec_int ("margin-right",
2830 0, G_MAXINT, 0, 0));
2831 _gtk_style_property_register (g_param_spec_boxed ("margin",
2834 GTK_TYPE_BORDER, 0),
2843 gtk_style_properties_register_property (NULL,
2844 g_param_spec_int ("padding-top",
2847 0, G_MAXINT, 0, 0));
2848 gtk_style_properties_register_property (NULL,
2849 g_param_spec_int ("padding-left",
2852 0, G_MAXINT, 0, 0));
2853 gtk_style_properties_register_property (NULL,
2854 g_param_spec_int ("padding-bottom",
2856 "Padding at bottom",
2857 0, G_MAXINT, 0, 0));
2858 gtk_style_properties_register_property (NULL,
2859 g_param_spec_int ("padding-right",
2862 0, G_MAXINT, 0, 0));
2863 _gtk_style_property_register (g_param_spec_boxed ("padding",
2866 GTK_TYPE_BORDER, 0),
2875 gtk_style_properties_register_property (NULL,
2876 g_param_spec_int ("border-top-width",
2878 "Border width at top",
2879 0, G_MAXINT, 0, 0));
2880 gtk_style_properties_register_property (NULL,
2881 g_param_spec_int ("border-left-width",
2882 "border left width",
2883 "Border width at left",
2884 0, G_MAXINT, 0, 0));
2885 gtk_style_properties_register_property (NULL,
2886 g_param_spec_int ("border-bottom-width",
2887 "border bottom width",
2888 "Border width at bottom",
2889 0, G_MAXINT, 0, 0));
2890 gtk_style_properties_register_property (NULL,
2891 g_param_spec_int ("border-right-width",
2892 "border right width",
2893 "Border width at right",
2894 0, G_MAXINT, 0, 0));
2895 _gtk_style_property_register (g_param_spec_boxed ("border-width",
2897 "Border width, in pixels",
2898 GTK_TYPE_BORDER, 0),
2901 unpack_border_width,
2906 unset_border_width);
2908 _gtk_style_property_register (g_param_spec_boxed ("border-top-left-radius",
2909 "Border top left radius",
2910 "Border radius of top left corner, in pixels",
2911 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2916 border_corner_radius_value_parse,
2917 border_corner_radius_value_print,
2920 _gtk_style_property_register (g_param_spec_boxed ("border-top-right-radius",
2921 "Border top right radius",
2922 "Border radius of top right corner, in pixels",
2923 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2928 border_corner_radius_value_parse,
2929 border_corner_radius_value_print,
2932 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-right-radius",
2933 "Border bottom right radius",
2934 "Border radius of bottom right corner, in pixels",
2935 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2940 border_corner_radius_value_parse,
2941 border_corner_radius_value_print,
2944 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-left-radius",
2945 "Border bottom left radius",
2946 "Border radius of bottom left corner, in pixels",
2947 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2952 border_corner_radius_value_parse,
2953 border_corner_radius_value_print,
2956 _gtk_style_property_register (g_param_spec_int ("border-radius",
2958 "Border radius, in pixels",
2962 unpack_border_radius,
2964 border_radius_value_parse,
2965 border_radius_value_print,
2967 unset_border_radius);
2969 gtk_style_properties_register_property (NULL,
2970 g_param_spec_enum ("border-style",
2973 GTK_TYPE_BORDER_STYLE,
2974 GTK_BORDER_STYLE_NONE, 0));
2975 gtk_style_properties_register_property (NULL,
2976 g_param_spec_enum ("background-clip",
2980 GTK_CSS_AREA_BORDER_BOX, 0));
2981 gtk_style_properties_register_property (NULL,
2982 g_param_spec_enum ("background-origin",
2983 "Background origin",
2984 "Background origin",
2986 GTK_CSS_AREA_PADDING_BOX, 0));
2987 g_value_init (&value, GTK_TYPE_CSS_SPECIAL_VALUE);
2988 g_value_set_enum (&value, GTK_CSS_CURRENT_COLOR);
2989 _gtk_style_property_register (g_param_spec_boxed ("border-top-color",
2997 transparent_color_value_parse,
3001 _gtk_style_property_register (g_param_spec_boxed ("border-right-color",
3002 "Border right color",
3003 "Border right color",
3009 transparent_color_value_parse,
3013 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-color",
3014 "Border bottom color",
3015 "Border bottom color",
3021 transparent_color_value_parse,
3025 _gtk_style_property_register (g_param_spec_boxed ("border-left-color",
3026 "Border left color",
3027 "Border left color",
3033 transparent_color_value_parse,
3037 g_value_unset (&value);
3038 _gtk_style_property_register (g_param_spec_boxed ("border-color",
3044 unpack_border_color,
3046 border_color_shorthand_value_parse,
3049 unset_border_color);
3051 gtk_style_properties_register_property (NULL,
3052 g_param_spec_boxed ("background-image",
3055 CAIRO_GOBJECT_TYPE_PATTERN, 0));
3056 gtk_style_properties_register_property (NULL,
3057 g_param_spec_boxed ("background-repeat",
3058 "Background repeat",
3059 "Background repeat",
3060 GTK_TYPE_CSS_BACKGROUND_REPEAT, 0));
3062 gtk_style_properties_register_property (NULL,
3063 g_param_spec_boxed ("border-image-source",
3064 "Border image source",
3065 "Border image source",
3066 CAIRO_GOBJECT_TYPE_PATTERN, 0));
3067 gtk_style_properties_register_property (NULL,
3068 g_param_spec_boxed ("border-image-repeat",
3069 "Border image repeat",
3070 "Border image repeat",
3071 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
3072 gtk_style_properties_register_property (NULL,
3073 g_param_spec_boxed ("border-image-slice",
3074 "Border image slice",
3075 "Border image slice",
3076 GTK_TYPE_BORDER, 0));
3077 g_value_init (&value, GTK_TYPE_BORDER);
3078 _gtk_style_property_register (g_param_spec_boxed ("border-image-width",
3079 "Border image width",
3080 "Border image width",
3081 GTK_TYPE_BORDER, 0),
3090 g_value_unset (&value);
3091 _gtk_style_property_register (g_param_spec_boxed ("border-image",
3094 GTK_TYPE_BORDER_IMAGE, 0),
3097 _gtk_border_image_unpack,
3098 _gtk_border_image_pack,
3102 unset_border_image);
3103 gtk_style_properties_register_property (NULL,
3104 g_param_spec_object ("engine",
3107 GTK_TYPE_THEMING_ENGINE, 0));
3108 gtk_style_properties_register_property (NULL,
3109 g_param_spec_boxed ("transition",
3110 "Transition animation description",
3111 "Transition animation description",
3112 GTK_TYPE_ANIMATION_DESCRIPTION, 0));
3114 /* Private property holding the binding sets */
3115 _gtk_style_property_register (g_param_spec_boxed ("gtk-key-bindings",
3118 G_TYPE_PTR_ARRAY, 0),
3123 bindings_value_parse,
3124 bindings_value_print,
3129 const GtkStyleProperty *
3130 _gtk_style_property_lookup (const char *name)
3132 gtk_style_property_init ();
3134 return g_hash_table_lookup (properties, name);
3138 _gtk_style_property_register (GParamSpec *pspec,
3139 GtkStylePropertyFlags flags,
3140 GtkStylePropertyParser property_parse_func,
3141 GtkStyleUnpackFunc unpack_func,
3142 GtkStylePackFunc pack_func,
3143 GtkStyleParseFunc parse_func,
3144 GtkStylePrintFunc print_func,
3145 const GValue * initial_value,
3146 GtkStyleUnsetFunc unset_func)
3148 const GtkStyleProperty *existing;
3149 GtkStyleProperty *node;
3151 g_return_if_fail ((pack_func == NULL) == (unpack_func == NULL));
3153 gtk_style_property_init ();
3155 existing = _gtk_style_property_lookup (pspec->name);
3156 if (existing != NULL)
3158 g_warning ("Property \"%s\" was already registered with type %s",
3159 pspec->name, g_type_name (existing->pspec->value_type));
3163 node = g_slice_new0 (GtkStyleProperty);
3164 node->flags = flags;
3165 node->pspec = pspec;
3166 node->property_parse_func = property_parse_func;
3167 node->pack_func = pack_func;
3168 node->unpack_func = unpack_func;
3169 node->parse_func = parse_func;
3170 node->print_func = print_func;
3171 node->unset_func = unset_func;
3173 if (!_gtk_style_property_is_shorthand (node))
3175 _gtk_style_property_generate_id (node);
3177 /* initialize the initial value */
3180 g_value_init (&node->initial_value, G_VALUE_TYPE (initial_value));
3181 g_value_copy (initial_value, &node->initial_value);
3185 g_value_init (&node->initial_value, pspec->value_type);
3186 if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
3187 g_value_set_object (&node->initial_value, gtk_theming_engine_load (NULL));
3188 else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
3189 g_value_take_boxed (&node->initial_value, pango_font_description_from_string ("Sans 10"));
3190 else if (pspec->value_type == GDK_TYPE_RGBA)
3193 gdk_rgba_parse (&color, "pink");
3194 g_value_set_boxed (&node->initial_value, &color);
3196 else if (pspec->value_type == GTK_TYPE_BORDER)
3198 g_value_take_boxed (&node->initial_value, gtk_border_new ());
3201 g_param_value_set_default (pspec, &node->initial_value);
3205 /* pspec owns name */
3206 g_hash_table_insert (properties, (gchar *)pspec->name, node);