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"
36 /* the actual parsers we have */
37 #include "gtkanimationdescription.h"
38 #include "gtkbindings.h"
39 #include "gtkborderimageprivate.h"
40 #include "gtkgradient.h"
41 #include "gtkshadowprivate.h"
42 #include "gtkthemingengine.h"
43 #include "gtktypebuiltins.h"
45 /* this is in case round() is not provided by the compiler,
46 * such as in the case of C89 compilers, like MSVC
48 #include "fallback-c89.c"
50 static GHashTable *parse_funcs = NULL;
51 static GHashTable *print_funcs = NULL;
52 static GHashTable *properties = NULL;
55 register_conversion_function (GType type,
56 GtkStyleParseFunc parse,
57 GtkStylePrintFunc print)
60 g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse);
62 g_hash_table_insert (print_funcs, GSIZE_TO_POINTER (type), print);
66 string_append_double (GString *string,
69 char buf[G_ASCII_DTOSTR_BUF_SIZE];
71 g_ascii_dtostr (buf, sizeof (buf), d);
72 g_string_append (string, buf);
76 string_append_string (GString *str,
81 g_string_append_c (str, '"');
84 len = strcspn (string, "\"\n\r\f");
85 g_string_append (str, string);
92 g_string_append (str, "\\A ");
95 g_string_append (str, "\\D ");
98 g_string_append (str, "\\C ");
101 g_string_append (str, "\\\"");
104 g_assert_not_reached ();
109 g_string_append_c (str, '"');
112 /*** IMPLEMENTATIONS ***/
115 rgba_value_parse (GtkCssParser *parser,
119 GtkSymbolicColor *symbolic;
122 symbolic = _gtk_css_parser_read_symbolic_color (parser);
123 if (symbolic == NULL)
126 if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
128 g_value_set_boxed (value, &rgba);
129 gtk_symbolic_color_unref (symbolic);
133 g_value_unset (value);
134 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
135 g_value_take_boxed (value, symbolic);
142 rgba_value_print (const GValue *value,
145 const GdkRGBA *rgba = g_value_get_boxed (value);
148 g_string_append (string, "none");
151 char *s = gdk_rgba_to_string (rgba);
152 g_string_append (string, s);
158 color_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))
173 color.red = rgba.red * 65535. + 0.5;
174 color.green = rgba.green * 65535. + 0.5;
175 color.blue = rgba.blue * 65535. + 0.5;
177 g_value_set_boxed (value, &color);
181 g_value_unset (value);
182 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
183 g_value_take_boxed (value, symbolic);
190 color_value_print (const GValue *value,
193 const GdkColor *color = g_value_get_boxed (value);
196 g_string_append (string, "none");
199 char *s = gdk_color_to_string (color);
200 g_string_append (string, s);
206 symbolic_color_value_parse (GtkCssParser *parser,
210 GtkSymbolicColor *symbolic;
212 symbolic = _gtk_css_parser_read_symbolic_color (parser);
213 if (symbolic == NULL)
216 g_value_take_boxed (value, symbolic);
221 symbolic_color_value_print (const GValue *value,
224 GtkSymbolicColor *symbolic = g_value_get_boxed (value);
226 if (symbolic == NULL)
227 g_string_append (string, "none");
230 char *s = gtk_symbolic_color_to_string (symbolic);
231 g_string_append (string, s);
237 font_family_parse (GtkCssParser *parser,
244 /* We don't special case generic families. Pango should do
247 names = g_ptr_array_new ();
250 name = _gtk_css_parser_try_ident (parser, TRUE);
253 GString *string = g_string_new (name);
255 while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
257 g_string_append_c (string, ' ');
258 g_string_append (string, name);
261 name = g_string_free (string, FALSE);
265 name = _gtk_css_parser_read_string (parser);
268 g_ptr_array_free (names, TRUE);
273 g_ptr_array_add (names, name);
274 } while (_gtk_css_parser_try (parser, ",", TRUE));
276 /* NULL-terminate array */
277 g_ptr_array_add (names, NULL);
278 g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
283 font_family_value_print (const GValue *value,
286 const char **names = g_value_get_boxed (value);
288 if (names == NULL || *names == NULL)
290 g_string_append (string, "none");
294 string_append_string (string, *names);
298 g_string_append (string, ", ");
299 string_append_string (string, *names);
305 font_description_value_parse (GtkCssParser *parser,
309 PangoFontDescription *font_desc;
313 str = _gtk_css_parser_read_value (parser);
317 font_desc = pango_font_description_from_string (str);
318 mask = pango_font_description_get_set_fields (font_desc);
319 /* These values are not really correct,
320 * but the fields must be set, so we set them to something */
321 if ((mask & PANGO_FONT_MASK_FAMILY) == 0)
322 pango_font_description_set_family_static (font_desc, "Sans");
323 if ((mask & PANGO_FONT_MASK_SIZE) == 0)
324 pango_font_description_set_size (font_desc, 10 * PANGO_SCALE);
326 g_value_take_boxed (value, font_desc);
331 font_description_value_print (const GValue *value,
334 const PangoFontDescription *desc = g_value_get_boxed (value);
337 g_string_append (string, "none");
340 char *s = pango_font_description_to_string (desc);
341 g_string_append (string, s);
347 boolean_value_parse (GtkCssParser *parser,
351 if (_gtk_css_parser_try (parser, "true", TRUE) ||
352 _gtk_css_parser_try (parser, "1", TRUE))
354 g_value_set_boolean (value, TRUE);
357 else if (_gtk_css_parser_try (parser, "false", TRUE) ||
358 _gtk_css_parser_try (parser, "0", TRUE))
360 g_value_set_boolean (value, FALSE);
365 _gtk_css_parser_error (parser, "Expected a boolean value");
371 boolean_value_print (const GValue *value,
374 if (g_value_get_boolean (value))
375 g_string_append (string, "true");
377 g_string_append (string, "false");
381 int_value_parse (GtkCssParser *parser,
387 if (!_gtk_css_parser_try_int (parser, &i))
389 _gtk_css_parser_error (parser, "Expected a valid integer value");
393 g_value_set_int (value, i);
398 int_value_print (const GValue *value,
401 g_string_append_printf (string, "%d", g_value_get_int (value));
405 uint_value_parse (GtkCssParser *parser,
411 if (!_gtk_css_parser_try_uint (parser, &u))
413 _gtk_css_parser_error (parser, "Expected a valid unsigned value");
417 g_value_set_uint (value, u);
422 uint_value_print (const GValue *value,
425 g_string_append_printf (string, "%u", g_value_get_uint (value));
429 double_value_parse (GtkCssParser *parser,
435 if (!_gtk_css_parser_try_double (parser, &d))
437 _gtk_css_parser_error (parser, "Expected a number");
441 g_value_set_double (value, d);
446 double_value_print (const GValue *value,
449 string_append_double (string, g_value_get_double (value));
453 float_value_parse (GtkCssParser *parser,
459 if (!_gtk_css_parser_try_double (parser, &d))
461 _gtk_css_parser_error (parser, "Expected a number");
465 g_value_set_float (value, d);
470 float_value_print (const GValue *value,
473 string_append_double (string, g_value_get_float (value));
477 string_value_parse (GtkCssParser *parser,
481 char *str = _gtk_css_parser_read_string (parser);
486 g_value_take_string (value, str);
491 string_value_print (const GValue *value,
494 string_append_string (str, g_value_get_string (value));
498 theming_engine_value_parse (GtkCssParser *parser,
502 GtkThemingEngine *engine;
505 str = _gtk_css_parser_try_ident (parser, TRUE);
508 _gtk_css_parser_error (parser, "Expected a valid theme name");
512 engine = gtk_theming_engine_load (str);
515 _gtk_css_parser_error (parser, "Themeing engine '%s' not found", str);
520 g_value_set_object (value, engine);
526 theming_engine_value_print (const GValue *value,
529 GtkThemingEngine *engine;
532 engine = g_value_get_object (value);
534 g_string_append (string, "none");
537 /* XXX: gtk_theming_engine_get_name()? */
538 g_object_get (engine, "name", &name, NULL);
539 g_string_append (string, name ? name : "none");
545 animation_description_value_parse (GtkCssParser *parser,
549 GtkAnimationDescription *desc;
552 str = _gtk_css_parser_read_value (parser);
556 desc = _gtk_animation_description_from_string (str);
561 _gtk_css_parser_error (parser, "Invalid animation description");
565 g_value_take_boxed (value, desc);
570 animation_description_value_print (const GValue *value,
573 GtkAnimationDescription *desc = g_value_get_boxed (value);
576 g_string_append (string, "none");
578 _gtk_animation_description_print (desc, string);
582 border_value_parse (GtkCssParser *parser,
586 GtkBorder border = { 0, };
589 for (i = 0; i < G_N_ELEMENTS (numbers); i++)
591 if (!_gtk_css_parser_try_uint (parser, &numbers[i]))
594 /* XXX: shouldn't allow spaces here? */
595 _gtk_css_parser_try (parser, "px", TRUE);
600 _gtk_css_parser_error (parser, "Expected valid border");
604 border.top = numbers[0];
606 border.right = numbers[1];
608 border.right = border.top;
610 border.bottom = numbers[2];
612 border.bottom = border.top;
614 border.left = numbers[3];
616 border.left = border.right;
618 g_value_set_boxed (value, &border);
623 border_value_print (const GValue *value, GString *string)
625 const GtkBorder *border = g_value_get_boxed (value);
628 g_string_append (string, "none");
629 else if (border->left != border->right)
630 g_string_append_printf (string, "%d %d %d %d", border->top, border->right, border->bottom, border->left);
631 else if (border->top != border->bottom)
632 g_string_append_printf (string, "%d %d %d", border->top, border->right, border->bottom);
633 else if (border->top != border->left)
634 g_string_append_printf (string, "%d %d", border->top, border->right);
636 g_string_append_printf (string, "%d", border->top);
640 gradient_value_parse (GtkCssParser *parser,
644 GtkGradient *gradient;
645 cairo_pattern_type_t type;
649 if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
651 _gtk_css_parser_error (parser,
652 "Expected '-gtk-gradient'");
656 if (!_gtk_css_parser_try (parser, "(", TRUE))
658 _gtk_css_parser_error (parser,
659 "Expected '(' after '-gtk-gradient'");
663 /* Parse gradient type */
664 if (_gtk_css_parser_try (parser, "linear", TRUE))
665 type = CAIRO_PATTERN_TYPE_LINEAR;
666 else if (_gtk_css_parser_try (parser, "radial", TRUE))
667 type = CAIRO_PATTERN_TYPE_RADIAL;
670 _gtk_css_parser_error (parser,
671 "Gradient type must be 'radial' or 'linear'");
675 /* Parse start/stop position parameters */
676 for (i = 0; i < 2; i++)
678 if (! _gtk_css_parser_try (parser, ",", TRUE))
680 _gtk_css_parser_error (parser,
685 if (_gtk_css_parser_try (parser, "left", TRUE))
687 else if (_gtk_css_parser_try (parser, "right", TRUE))
689 else if (_gtk_css_parser_try (parser, "center", TRUE))
691 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3]))
693 _gtk_css_parser_error (parser,
694 "Expected a valid X coordinate");
698 if (_gtk_css_parser_try (parser, "top", TRUE))
699 coords[i * 3 + 1] = 0;
700 else if (_gtk_css_parser_try (parser, "bottom", TRUE))
701 coords[i * 3 + 1] = 1;
702 else if (_gtk_css_parser_try (parser, "center", TRUE))
703 coords[i * 3 + 1] = 0.5;
704 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3 + 1]))
706 _gtk_css_parser_error (parser,
707 "Expected a valid Y coordinate");
711 if (type == CAIRO_PATTERN_TYPE_RADIAL)
714 if (! _gtk_css_parser_try (parser, ",", TRUE))
716 _gtk_css_parser_error (parser,
721 if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
723 _gtk_css_parser_error (parser,
724 "Expected a numer for the radius");
730 if (type == CAIRO_PATTERN_TYPE_LINEAR)
731 gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
733 gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
734 coords[3], coords[4], coords[5]);
736 while (_gtk_css_parser_try (parser, ",", TRUE))
738 GtkSymbolicColor *color;
741 if (_gtk_css_parser_try (parser, "from", TRUE))
745 if (!_gtk_css_parser_try (parser, "(", TRUE))
747 gtk_gradient_unref (gradient);
748 _gtk_css_parser_error (parser,
754 else if (_gtk_css_parser_try (parser, "to", TRUE))
758 if (!_gtk_css_parser_try (parser, "(", TRUE))
760 gtk_gradient_unref (gradient);
761 _gtk_css_parser_error (parser,
767 else if (_gtk_css_parser_try (parser, "color-stop", TRUE))
769 if (!_gtk_css_parser_try (parser, "(", TRUE))
771 gtk_gradient_unref (gradient);
772 _gtk_css_parser_error (parser,
777 if (!_gtk_css_parser_try_double (parser, &position))
779 gtk_gradient_unref (gradient);
780 _gtk_css_parser_error (parser,
781 "Expected a valid number");
785 if (!_gtk_css_parser_try (parser, ",", TRUE))
787 gtk_gradient_unref (gradient);
788 _gtk_css_parser_error (parser,
795 gtk_gradient_unref (gradient);
796 _gtk_css_parser_error (parser,
797 "Not a valid color-stop definition");
801 color = _gtk_css_parser_read_symbolic_color (parser);
804 gtk_gradient_unref (gradient);
808 gtk_gradient_add_color_stop (gradient, position, color);
809 gtk_symbolic_color_unref (color);
811 if (!_gtk_css_parser_try (parser, ")", TRUE))
813 gtk_gradient_unref (gradient);
814 _gtk_css_parser_error (parser,
820 if (!_gtk_css_parser_try (parser, ")", TRUE))
822 gtk_gradient_unref (gradient);
823 _gtk_css_parser_error (parser,
828 g_value_take_boxed (value, gradient);
833 gradient_value_print (const GValue *value,
836 GtkGradient *gradient = g_value_get_boxed (value);
838 if (gradient == NULL)
839 g_string_append (string, "none");
842 char *s = gtk_gradient_to_string (gradient);
843 g_string_append (string, s);
849 gtk_css_parse_url (GtkCssParser *parser,
855 if (_gtk_css_parser_try (parser, "url", FALSE))
857 if (!_gtk_css_parser_try (parser, "(", TRUE))
859 _gtk_css_parser_skip_whitespace (parser);
860 if (_gtk_css_parser_try (parser, "(", TRUE))
864 error = g_error_new_literal (GTK_CSS_PROVIDER_ERROR,
865 GTK_CSS_PROVIDER_ERROR_DEPRECATED,
866 "Whitespace between 'url' and '(' is deprecated");
868 _gtk_css_parser_take_error (parser, error);
872 _gtk_css_parser_error (parser, "Expected '(' after 'url'");
877 path = _gtk_css_parser_read_string (parser);
881 if (!_gtk_css_parser_try (parser, ")", TRUE))
883 _gtk_css_parser_error (parser, "No closing ')' found for 'url'");
890 path = _gtk_css_parser_try_name (parser, TRUE);
893 _gtk_css_parser_error (parser, "Not a valid url");
898 file = g_file_resolve_relative_path (base, path);
905 pattern_value_parse (GtkCssParser *parser,
909 if (_gtk_css_parser_begins_with (parser, '-'))
911 g_value_unset (value);
912 g_value_init (value, GTK_TYPE_GRADIENT);
913 return gradient_value_parse (parser, base, value);
917 GError *error = NULL;
921 cairo_surface_t *surface;
922 cairo_pattern_t *pattern;
924 cairo_matrix_t matrix;
926 file = gtk_css_parse_url (parser, base);
930 path = g_file_get_path (file);
931 g_object_unref (file);
933 pixbuf = gdk_pixbuf_new_from_file (path, &error);
937 _gtk_css_parser_take_error (parser, error);
941 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
942 gdk_pixbuf_get_width (pixbuf),
943 gdk_pixbuf_get_height (pixbuf));
944 cr = cairo_create (surface);
945 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
947 pattern = cairo_pattern_create_for_surface (surface);
949 cairo_matrix_init_scale (&matrix,
950 gdk_pixbuf_get_width (pixbuf),
951 gdk_pixbuf_get_height (pixbuf));
952 cairo_pattern_set_matrix (pattern, &matrix);
954 cairo_surface_destroy (surface);
956 g_object_unref (pixbuf);
958 g_value_take_boxed (value, pattern);
964 static cairo_status_t
965 surface_write (void *closure,
966 const unsigned char *data,
969 g_byte_array_append (closure, data, length);
971 return CAIRO_STATUS_SUCCESS;
975 surface_print (cairo_surface_t *surface,
978 #if CAIRO_HAS_PNG_FUNCTIONS
982 array = g_byte_array_new ();
983 cairo_surface_write_to_png_stream (surface, surface_write, array);
984 base64 = g_base64_encode (array->data, array->len);
985 g_byte_array_free (array, TRUE);
987 g_string_append (string, "url(\"data:image/png;base64,");
988 g_string_append (string, base64);
989 g_string_append (string, "\")");
993 g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
998 pattern_value_print (const GValue *value,
1001 cairo_pattern_t *pattern;
1002 cairo_surface_t *surface;
1004 pattern = g_value_get_boxed (value);
1006 if (pattern == NULL)
1008 g_string_append (string, "none");
1012 switch (cairo_pattern_get_type (pattern))
1014 case CAIRO_PATTERN_TYPE_SURFACE:
1015 if (cairo_pattern_get_surface (pattern, &surface) != CAIRO_STATUS_SUCCESS)
1017 g_assert_not_reached ();
1019 surface_print (surface, string);
1021 case CAIRO_PATTERN_TYPE_SOLID:
1022 case CAIRO_PATTERN_TYPE_LINEAR:
1023 case CAIRO_PATTERN_TYPE_RADIAL:
1025 g_assert_not_reached ();
1031 shadow_value_parse (GtkCssParser *parser,
1035 gboolean have_inset, have_color, have_lengths;
1036 gdouble hoffset, voffset, blur, spread;
1037 GtkSymbolicColor *color;
1041 shadow = _gtk_shadow_new ();
1045 have_inset = have_lengths = have_color = FALSE;
1047 for (i = 0; i < 3; i++)
1050 _gtk_css_parser_try (parser, "inset", TRUE))
1056 if (!have_lengths &&
1057 _gtk_css_parser_try_double (parser, &hoffset))
1059 have_lengths = TRUE;
1061 if (!_gtk_css_parser_try_double (parser, &voffset))
1063 _gtk_css_parser_error (parser, "Horizontal and vertical offsets are required");
1064 _gtk_shadow_unref (shadow);
1068 if (!_gtk_css_parser_try_double (parser, &blur))
1071 if (!_gtk_css_parser_try_double (parser, &spread))
1081 /* XXX: the color is optional and UA-defined if it's missing,
1082 * but it doesn't really make sense for us...
1084 color = _gtk_css_parser_read_symbolic_color (parser);
1088 _gtk_shadow_unref (shadow);
1094 if (!have_color || !have_lengths)
1096 _gtk_css_parser_error (parser, "Must specify at least color and offsets");
1097 _gtk_shadow_unref (shadow);
1101 _gtk_shadow_append (shadow,
1106 gtk_symbolic_color_unref (color);
1109 while (_gtk_css_parser_try (parser, ",", TRUE));
1111 g_value_take_boxed (value, shadow);
1116 shadow_value_print (const GValue *value,
1121 shadow = g_value_get_boxed (value);
1124 g_string_append (string, "none");
1126 _gtk_shadow_print (shadow, string);
1130 border_image_repeat_value_parse (GtkCssParser *parser,
1134 GtkCssBorderImageRepeat image_repeat;
1135 GtkCssRepeatStyle styles[2];
1138 for (i = 0; i < 2; i++)
1140 if (_gtk_css_parser_try (parser, "stretch", TRUE))
1141 styles[i] = GTK_CSS_REPEAT_STYLE_NONE;
1142 else if (_gtk_css_parser_try (parser, "repeat", TRUE))
1143 styles[i] = GTK_CSS_REPEAT_STYLE_REPEAT;
1144 else if (_gtk_css_parser_try (parser, "round", TRUE))
1145 styles[i] = GTK_CSS_REPEAT_STYLE_ROUND;
1146 else if (_gtk_css_parser_try (parser, "space", TRUE))
1147 styles[i] = GTK_CSS_REPEAT_STYLE_SPACE;
1150 styles[1] = styles[0] = GTK_CSS_REPEAT_STYLE_NONE;
1154 styles[i] = styles[0];
1157 image_repeat.hrepeat = styles[0];
1158 image_repeat.vrepeat = styles[1];
1160 g_value_set_boxed (value, &image_repeat);
1165 static const gchar *
1166 border_image_repeat_style_to_string (GtkCssRepeatStyle repeat)
1170 case GTK_CSS_REPEAT_STYLE_NONE:
1172 case GTK_CSS_REPEAT_STYLE_REPEAT:
1174 case GTK_CSS_REPEAT_STYLE_ROUND:
1176 case GTK_CSS_REPEAT_STYLE_SPACE:
1184 border_image_repeat_value_print (const GValue *value,
1187 GtkCssBorderImageRepeat *image_repeat;
1189 image_repeat = g_value_get_boxed (value);
1191 g_string_append (string, border_image_repeat_style_to_string (image_repeat->hrepeat));
1192 if (image_repeat->hrepeat != image_repeat->vrepeat)
1194 g_string_append (string, " ");
1195 g_string_append (string, border_image_repeat_style_to_string (image_repeat->vrepeat));
1200 border_image_value_parse (GtkCssParser *parser,
1204 GValue temp = { 0, };
1205 cairo_pattern_t *pattern = NULL;
1206 GtkGradient *gradient = NULL;
1207 GtkBorder slice, *width = NULL, *parsed_slice;
1208 GtkCssBorderImageRepeat repeat, *parsed_repeat;
1209 gboolean retval = FALSE;
1210 GtkBorderImage *image = NULL;
1212 g_value_init (&temp, CAIRO_GOBJECT_TYPE_PATTERN);
1214 if (!pattern_value_parse (parser, base, &temp))
1217 if (G_VALUE_TYPE (&temp) == GTK_TYPE_GRADIENT)
1218 gradient = g_value_dup_boxed (&temp);
1220 pattern = g_value_dup_boxed (&temp);
1222 g_value_unset (&temp);
1223 g_value_init (&temp, GTK_TYPE_BORDER);
1225 if (!border_value_parse (parser, base, &temp))
1228 parsed_slice = g_value_get_boxed (&temp);
1229 slice = *parsed_slice;
1231 if (_gtk_css_parser_try (parser, "/", TRUE))
1233 g_value_unset (&temp);
1234 g_value_init (&temp, GTK_TYPE_BORDER);
1236 if (!border_value_parse (parser, base, &temp))
1239 width = g_value_dup_boxed (&temp);
1242 g_value_unset (&temp);
1243 g_value_init (&temp, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
1245 if (!border_image_repeat_value_parse (parser, base, &temp))
1248 parsed_repeat = g_value_get_boxed (&temp);
1249 repeat = *parsed_repeat;
1251 g_value_unset (&temp);
1253 if (gradient != NULL)
1254 image = _gtk_border_image_new_for_gradient (gradient, &slice, width, &repeat);
1255 else if (pattern != NULL)
1256 image = _gtk_border_image_new (pattern, &slice, width, &repeat);
1261 g_value_take_boxed (value, image);
1265 if (pattern != NULL)
1266 cairo_pattern_destroy (pattern);
1268 if (gradient != NULL)
1269 gtk_gradient_unref (gradient);
1272 gtk_border_free (width);
1278 enum_value_parse (GtkCssParser *parser,
1282 GEnumClass *enum_class;
1283 GEnumValue *enum_value;
1286 str = _gtk_css_parser_try_ident (parser, TRUE);
1289 _gtk_css_parser_error (parser, "Expected an identifier");
1293 enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1294 enum_value = g_enum_get_value_by_nick (enum_class, str);
1297 g_value_set_enum (value, enum_value->value);
1299 _gtk_css_parser_error (parser,
1300 "Unknown value '%s' for enum type '%s'",
1301 str, g_type_name (G_VALUE_TYPE (value)));
1303 g_type_class_unref (enum_class);
1306 return enum_value != NULL;
1310 enum_value_print (const GValue *value,
1313 GEnumClass *enum_class;
1314 GEnumValue *enum_value;
1316 enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1317 enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
1319 g_string_append (string, enum_value->value_nick);
1321 g_type_class_unref (enum_class);
1325 flags_value_parse (GtkCssParser *parser,
1329 GFlagsClass *flags_class;
1330 GFlagsValue *flag_value;
1334 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1337 str = _gtk_css_parser_try_ident (parser, TRUE);
1340 _gtk_css_parser_error (parser, "Expected an identifier");
1341 g_type_class_unref (flags_class);
1345 flag_value = g_flags_get_value_by_nick (flags_class, str);
1348 _gtk_css_parser_error (parser,
1349 "Unknown flag value '%s' for type '%s'",
1350 str, g_type_name (G_VALUE_TYPE (value)));
1351 /* XXX Do we want to return FALSE here? We can get
1352 * forward-compatibility for new values this way
1355 g_type_class_unref (flags_class);
1361 while (_gtk_css_parser_try (parser, ",", FALSE));
1363 g_type_class_unref (flags_class);
1365 g_value_set_enum (value, flags);
1371 flags_value_print (const GValue *value,
1374 GFlagsClass *flags_class;
1377 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1378 flags = g_value_get_flags (value);
1380 for (i = 0; i < flags_class->n_values; i++)
1382 GFlagsValue *flags_value = &flags_class->values[i];
1384 if (flags & flags_value->value)
1386 if (string->len != 0)
1387 g_string_append (string, ", ");
1389 g_string_append (string, flags_value->value_nick);
1393 g_type_class_unref (flags_class);
1397 bindings_value_parse (GtkCssParser *parser,
1402 GtkBindingSet *binding_set;
1405 array = g_ptr_array_new ();
1408 name = _gtk_css_parser_try_ident (parser, TRUE);
1411 _gtk_css_parser_error (parser, "Not a valid binding name");
1412 g_ptr_array_free (array, TRUE);
1416 binding_set = gtk_binding_set_find (name);
1420 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
1425 g_ptr_array_add (array, binding_set);
1428 while (_gtk_css_parser_try (parser, ",", TRUE));
1430 g_value_take_boxed (value, array);
1436 bindings_value_print (const GValue *value,
1442 array = g_value_get_boxed (value);
1444 for (i = 0; i < array->len; i++)
1446 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
1449 g_string_append (string, ", ");
1450 g_string_append (string, binding_set->set_name);
1455 border_corner_radius_value_parse (GtkCssParser *parser,
1459 GtkCssBorderCornerRadius corner;
1461 if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
1463 _gtk_css_parser_error (parser, "Expected a number");
1466 else if (corner.horizontal < 0)
1469 if (!_gtk_css_parser_try_double (parser, &corner.vertical))
1470 corner.vertical = corner.horizontal;
1471 else if (corner.vertical < 0)
1474 g_value_set_boxed (value, &corner);
1478 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1483 border_corner_radius_value_print (const GValue *value,
1486 GtkCssBorderCornerRadius *corner;
1488 corner = g_value_get_boxed (value);
1492 g_string_append (string, "none");
1496 string_append_double (string, corner->horizontal);
1497 if (corner->horizontal != corner->vertical)
1499 g_string_append_c (string, ' ');
1500 string_append_double (string, corner->vertical);
1505 border_radius_value_parse (GtkCssParser *parser,
1509 GtkCssBorderRadius border;
1511 if (!_gtk_css_parser_try_double (parser, &border.top_left.horizontal))
1513 _gtk_css_parser_error (parser, "Expected a number");
1516 else if (border.top_left.horizontal < 0)
1519 if (_gtk_css_parser_try_double (parser, &border.top_right.horizontal))
1521 if (border.top_right.horizontal < 0)
1523 if (_gtk_css_parser_try_double (parser, &border.bottom_right.horizontal))
1525 if (border.bottom_right.horizontal < 0)
1527 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.horizontal))
1528 border.bottom_left.horizontal = border.top_right.horizontal;
1529 else if (border.bottom_left.horizontal < 0)
1534 border.bottom_right.horizontal = border.top_left.horizontal;
1535 border.bottom_left.horizontal = border.top_right.horizontal;
1540 border.top_right.horizontal = border.top_left.horizontal;
1541 border.bottom_right.horizontal = border.top_left.horizontal;
1542 border.bottom_left.horizontal = border.top_left.horizontal;
1545 if (_gtk_css_parser_try (parser, "/", TRUE))
1547 if (!_gtk_css_parser_try_double (parser, &border.top_left.vertical))
1549 _gtk_css_parser_error (parser, "Expected a number");
1552 else if (border.top_left.vertical < 0)
1555 if (_gtk_css_parser_try_double (parser, &border.top_right.vertical))
1557 if (border.top_right.vertical < 0)
1559 if (_gtk_css_parser_try_double (parser, &border.bottom_right.vertical))
1561 if (border.bottom_right.vertical < 0)
1563 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.vertical))
1564 border.bottom_left.vertical = border.top_right.vertical;
1565 else if (border.bottom_left.vertical < 0)
1570 border.bottom_right.vertical = border.top_left.vertical;
1571 border.bottom_left.vertical = border.top_right.vertical;
1576 border.top_right.vertical = border.top_left.vertical;
1577 border.bottom_right.vertical = border.top_left.vertical;
1578 border.bottom_left.vertical = border.top_left.vertical;
1583 border.top_left.vertical = border.top_left.horizontal;
1584 border.top_right.vertical = border.top_right.horizontal;
1585 border.bottom_right.vertical = border.bottom_right.horizontal;
1586 border.bottom_left.vertical = border.bottom_left.horizontal;
1589 /* border-radius is an int property for backwards-compat reasons */
1590 g_value_unset (value);
1591 g_value_init (value, GTK_TYPE_CSS_BORDER_RADIUS);
1592 g_value_set_boxed (value, &border);
1597 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1602 border_radius_value_print (const GValue *value,
1605 GtkCssBorderRadius *border;
1607 border = g_value_get_boxed (value);
1611 g_string_append (string, "none");
1615 string_append_double (string, border->top_left.horizontal);
1616 if (border->top_left.horizontal != border->top_right.horizontal ||
1617 border->top_left.horizontal != border->bottom_right.horizontal ||
1618 border->top_left.horizontal != border->bottom_left.horizontal)
1620 g_string_append_c (string, ' ');
1621 string_append_double (string, border->top_right.horizontal);
1622 if (border->top_left.horizontal != border->bottom_right.horizontal ||
1623 border->top_right.horizontal != border->bottom_left.horizontal)
1625 g_string_append_c (string, ' ');
1626 string_append_double (string, border->bottom_right.horizontal);
1627 if (border->top_right.horizontal != border->bottom_left.horizontal)
1629 g_string_append_c (string, ' ');
1630 string_append_double (string, border->bottom_left.horizontal);
1635 if (border->top_left.horizontal != border->top_left.vertical ||
1636 border->top_right.horizontal != border->top_right.vertical ||
1637 border->bottom_right.horizontal != border->bottom_right.vertical ||
1638 border->bottom_left.horizontal != border->bottom_left.vertical)
1640 g_string_append (string, " / ");
1641 string_append_double (string, border->top_left.vertical);
1642 if (border->top_left.vertical != border->top_right.vertical ||
1643 border->top_left.vertical != border->bottom_right.vertical ||
1644 border->top_left.vertical != border->bottom_left.vertical)
1646 g_string_append_c (string, ' ');
1647 string_append_double (string, border->top_right.vertical);
1648 if (border->top_left.vertical != border->bottom_right.vertical ||
1649 border->top_right.vertical != border->bottom_left.vertical)
1651 g_string_append_c (string, ' ');
1652 string_append_double (string, border->bottom_right.vertical);
1653 if (border->top_right.vertical != border->bottom_left.vertical)
1655 g_string_append_c (string, ' ');
1656 string_append_double (string, border->bottom_left.vertical);
1665 border_color_value_parse (GtkCssParser *parser,
1669 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1671 GdkRGBA transparent = { 0, 0, 0, 0 };
1673 g_value_set_boxed (value, &transparent);
1678 return rgba_value_parse (parser, base, value);
1682 border_color_shorthand_value_parse (GtkCssParser *parser,
1686 GtkSymbolicColor *symbolic;
1689 array = g_ptr_array_new_with_free_func ((GDestroyNotify) gtk_symbolic_color_unref);
1693 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1695 GdkRGBA transparent = { 0, 0, 0, 0 };
1697 symbolic = gtk_symbolic_color_new_literal (&transparent);
1701 symbolic = _gtk_css_parser_read_symbolic_color (parser);
1703 if (symbolic == NULL)
1707 g_ptr_array_add (array, symbolic);
1709 while (array->len < 4 &&
1710 !_gtk_css_parser_is_eof (parser) &&
1711 !_gtk_css_parser_begins_with (parser, ';') &&
1712 !_gtk_css_parser_begins_with (parser, '}'));
1717 g_assert_not_reached ();
1720 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1723 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1726 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 1)));
1732 g_value_unset (value);
1733 g_value_init (value, G_TYPE_PTR_ARRAY);
1734 g_value_take_boxed (value, array);
1742 unpack_border (const GValue *value,
1749 GParameter *parameter = g_new0 (GParameter, 4);
1750 GtkBorder *border = g_value_get_boxed (value);
1752 parameter[0].name = top;
1753 g_value_init (¶meter[0].value, G_TYPE_INT);
1754 g_value_set_int (¶meter[0].value, border->top);
1755 parameter[1].name = left;
1756 g_value_init (¶meter[1].value, G_TYPE_INT);
1757 g_value_set_int (¶meter[1].value, border->left);
1758 parameter[2].name = bottom;
1759 g_value_init (¶meter[2].value, G_TYPE_INT);
1760 g_value_set_int (¶meter[2].value, border->bottom);
1761 parameter[3].name = right;
1762 g_value_init (¶meter[3].value, G_TYPE_INT);
1763 g_value_set_int (¶meter[3].value, border->right);
1770 pack_border (GValue *value,
1771 GtkStyleProperties *props,
1772 GtkStateFlags state,
1781 gtk_style_properties_get (props,
1794 g_value_set_boxed (value, &border);
1798 unpack_border_width (const GValue *value,
1801 return unpack_border (value, n_params,
1802 "border-top-width", "border-left-width",
1803 "border-bottom-width", "border-right-width");
1807 pack_border_width (GValue *value,
1808 GtkStyleProperties *props,
1809 GtkStateFlags state)
1811 pack_border (value, props, state,
1812 "border-top-width", "border-left-width",
1813 "border-bottom-width", "border-right-width");
1817 unpack_padding (const GValue *value,
1820 return unpack_border (value, n_params,
1821 "padding-top", "padding-left",
1822 "padding-bottom", "padding-right");
1826 pack_padding (GValue *value,
1827 GtkStyleProperties *props,
1828 GtkStateFlags state)
1830 pack_border (value, props, state,
1831 "padding-top", "padding-left",
1832 "padding-bottom", "padding-right");
1836 unpack_margin (const GValue *value,
1839 return unpack_border (value, n_params,
1840 "margin-top", "margin-left",
1841 "margin-bottom", "margin-right");
1845 pack_margin (GValue *value,
1846 GtkStyleProperties *props,
1847 GtkStateFlags state)
1849 pack_border (value, props, state,
1850 "margin-top", "margin-left",
1851 "margin-bottom", "margin-right");
1855 unpack_border_radius (const GValue *value,
1858 GParameter *parameter = g_new0 (GParameter, 4);
1859 GtkCssBorderRadius *border;
1861 if (G_VALUE_HOLDS_BOXED (value))
1862 border = g_value_get_boxed (value);
1866 parameter[0].name = "border-top-left-radius";
1867 g_value_init (¶meter[0].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1868 parameter[1].name = "border-top-right-radius";
1869 g_value_init (¶meter[1].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1870 parameter[2].name = "border-bottom-right-radius";
1871 g_value_init (¶meter[2].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1872 parameter[3].name = "border-bottom-left-radius";
1873 g_value_init (¶meter[3].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1876 g_value_set_boxed (¶meter[0].value, &border->top_left);
1877 g_value_set_boxed (¶meter[1].value, &border->top_right);
1878 g_value_set_boxed (¶meter[2].value, &border->bottom_right);
1879 g_value_set_boxed (¶meter[3].value, &border->bottom_left);
1887 pack_border_radius (GValue *value,
1888 GtkStyleProperties *props,
1889 GtkStateFlags state)
1891 GtkCssBorderCornerRadius *top_left;
1893 /* NB: We are an int property, so we have to resolve to an int here.
1894 * So we just resolve to an int. We pick one and stick to it.
1895 * Lesson learned: Don't query border-radius shorthand, query the
1896 * real properties instead. */
1897 gtk_style_properties_get (props,
1899 "border-top-left-radius", &top_left,
1903 g_value_set_int (value, top_left->horizontal);
1909 unpack_font_description (const GValue *value,
1912 GParameter *parameter = g_new0 (GParameter, 5);
1913 PangoFontDescription *description;
1917 /* For backwards compat, we only unpack values that are indeed set.
1918 * For strict CSS conformance we need to unpack all of them.
1919 * Note that we do set all of them in the parse function, so it
1920 * will not have effects when parsing CSS files. It will though
1921 * for custom style providers.
1924 description = g_value_get_boxed (value);
1928 mask = pango_font_description_get_set_fields (description);
1932 if (mask & PANGO_FONT_MASK_FAMILY)
1934 GPtrArray *strv = g_ptr_array_new ();
1936 g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
1937 g_ptr_array_add (strv, NULL);
1938 parameter[n].name = "font-family";
1939 g_value_init (¶meter[n].value, G_TYPE_STRV);
1940 g_value_take_boxed (¶meter[n].value,
1941 g_ptr_array_free (strv, FALSE));
1945 if (mask & PANGO_FONT_MASK_STYLE)
1947 parameter[n].name = "font-style";
1948 g_value_init (¶meter[n].value, PANGO_TYPE_STYLE);
1949 g_value_set_enum (¶meter[n].value,
1950 pango_font_description_get_style (description));
1954 if (mask & PANGO_FONT_MASK_VARIANT)
1956 parameter[n].name = "font-variant";
1957 g_value_init (¶meter[n].value, PANGO_TYPE_VARIANT);
1958 g_value_set_enum (¶meter[n].value,
1959 pango_font_description_get_variant (description));
1963 if (mask & PANGO_FONT_MASK_WEIGHT)
1965 parameter[n].name = "font-weight";
1966 g_value_init (¶meter[n].value, PANGO_TYPE_WEIGHT);
1967 g_value_set_enum (¶meter[n].value,
1968 pango_font_description_get_weight (description));
1972 if (mask & PANGO_FONT_MASK_SIZE)
1974 parameter[n].name = "font-size";
1975 g_value_init (¶meter[n].value, G_TYPE_DOUBLE);
1976 g_value_set_double (¶meter[n].value,
1977 (double) pango_font_description_get_size (description) / PANGO_SCALE);
1987 pack_font_description (GValue *value,
1988 GtkStyleProperties *props,
1989 GtkStateFlags state)
1991 PangoFontDescription *description;
1994 PangoVariant variant;
1998 gtk_style_properties_get (props,
2000 "font-family", &families,
2001 "font-style", &style,
2002 "font-variant", &variant,
2003 "font-weight", &weight,
2007 description = pango_font_description_new ();
2008 /* xxx: Can we set all the families here somehow? */
2010 pango_font_description_set_family (description, families[0]);
2011 pango_font_description_set_size (description, round (size * PANGO_SCALE));
2012 pango_font_description_set_style (description, style);
2013 pango_font_description_set_variant (description, variant);
2014 pango_font_description_set_weight (description, weight);
2016 g_strfreev (families);
2018 g_value_take_boxed (value, description);
2022 unpack_border_color (const GValue *value,
2025 GParameter *parameter = g_new0 (GParameter, 4);
2028 type = G_VALUE_TYPE (value);
2029 if (type == G_TYPE_PTR_ARRAY)
2030 type = GTK_TYPE_SYMBOLIC_COLOR;
2032 parameter[0].name = "border-top-color";
2033 g_value_init (¶meter[0].value, type);
2034 parameter[1].name = "border-right-color";
2035 g_value_init (¶meter[1].value, type);
2036 parameter[2].name = "border-bottom-color";
2037 g_value_init (¶meter[2].value, type);
2038 parameter[3].name = "border-left-color";
2039 g_value_init (¶meter[3].value, type);
2041 if (G_VALUE_TYPE (value) == G_TYPE_PTR_ARRAY)
2043 GPtrArray *array = g_value_get_boxed (value);
2046 for (i = 0; i < 4; i++)
2047 g_value_set_boxed (¶meter[i].value, g_ptr_array_index (array, i));
2051 /* can be RGBA or symbolic color */
2052 gpointer p = g_value_get_boxed (value);
2054 g_value_set_boxed (¶meter[0].value, p);
2055 g_value_set_boxed (¶meter[1].value, p);
2056 g_value_set_boxed (¶meter[2].value, p);
2057 g_value_set_boxed (¶meter[3].value, p);
2065 pack_border_color (GValue *value,
2066 GtkStyleProperties *props,
2067 GtkStateFlags state)
2069 /* NB: We are a color property, so we have to resolve to a color here.
2070 * So we just resolve to a color. We pick one and stick to it.
2071 * Lesson learned: Don't query border-color shorthand, query the
2072 * real properties instead. */
2073 g_value_unset (value);
2074 gtk_style_properties_get_property (props, "border-top-color", state, value);
2077 /*** UNSET FUNCS ***/
2080 unset_font_description (GtkStyleProperties *props,
2081 GtkStateFlags state)
2083 gtk_style_properties_unset_property (props, "font-family", state);
2084 gtk_style_properties_unset_property (props, "font-style", state);
2085 gtk_style_properties_unset_property (props, "font-variant", state);
2086 gtk_style_properties_unset_property (props, "font-weight", state);
2087 gtk_style_properties_unset_property (props, "font-size", state);
2091 unset_margin (GtkStyleProperties *props,
2092 GtkStateFlags state)
2094 gtk_style_properties_unset_property (props, "margin-top", state);
2095 gtk_style_properties_unset_property (props, "margin-right", state);
2096 gtk_style_properties_unset_property (props, "margin-bottom", state);
2097 gtk_style_properties_unset_property (props, "margin-left", state);
2101 unset_padding (GtkStyleProperties *props,
2102 GtkStateFlags state)
2104 gtk_style_properties_unset_property (props, "padding-top", state);
2105 gtk_style_properties_unset_property (props, "padding-right", state);
2106 gtk_style_properties_unset_property (props, "padding-bottom", state);
2107 gtk_style_properties_unset_property (props, "padding-left", state);
2111 unset_border_width (GtkStyleProperties *props,
2112 GtkStateFlags state)
2114 gtk_style_properties_unset_property (props, "border-top-width", state);
2115 gtk_style_properties_unset_property (props, "border-right-width", state);
2116 gtk_style_properties_unset_property (props, "border-bottom-width", state);
2117 gtk_style_properties_unset_property (props, "border-left-width", state);
2121 unset_border_radius (GtkStyleProperties *props,
2122 GtkStateFlags state)
2124 gtk_style_properties_unset_property (props, "border-top-right-radius", state);
2125 gtk_style_properties_unset_property (props, "border-bottom-right-radius", state);
2126 gtk_style_properties_unset_property (props, "border-bottom-left-radius", state);
2127 gtk_style_properties_unset_property (props, "border-top-left-radius", state);
2131 unset_border_color (GtkStyleProperties *props,
2132 GtkStateFlags state)
2134 gtk_style_properties_unset_property (props, "border-top-color", state);
2135 gtk_style_properties_unset_property (props, "border-right-color", state);
2136 gtk_style_properties_unset_property (props, "border-bottom-color", state);
2137 gtk_style_properties_unset_property (props, "border-left-color", state);
2141 unset_border_image (GtkStyleProperties *props,
2142 GtkStateFlags state)
2144 gtk_style_properties_unset_property (props, "border-image-source", state);
2145 gtk_style_properties_unset_property (props, "border-image-slice", state);
2146 gtk_style_properties_unset_property (props, "border-image-repeat", state);
2147 gtk_style_properties_unset_property (props, "border-image-width", state);
2150 /*** default values ***/
2153 border_image_width_default_value (GtkStyleProperties *props,
2154 GtkStateFlags state,
2160 border_color_default_value (GtkStyleProperties *props,
2161 GtkStateFlags state,
2164 g_value_unset (value);
2165 gtk_style_properties_get_property (props, "color", state, value);
2171 css_string_funcs_init (void)
2173 if (G_LIKELY (parse_funcs != NULL))
2176 parse_funcs = g_hash_table_new (NULL, NULL);
2177 print_funcs = g_hash_table_new (NULL, NULL);
2179 register_conversion_function (GDK_TYPE_RGBA,
2182 register_conversion_function (GDK_TYPE_COLOR,
2185 register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
2186 symbolic_color_value_parse,
2187 symbolic_color_value_print);
2188 register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
2189 font_description_value_parse,
2190 font_description_value_print);
2191 register_conversion_function (G_TYPE_BOOLEAN,
2192 boolean_value_parse,
2193 boolean_value_print);
2194 register_conversion_function (G_TYPE_INT,
2197 register_conversion_function (G_TYPE_UINT,
2200 register_conversion_function (G_TYPE_DOUBLE,
2202 double_value_print);
2203 register_conversion_function (G_TYPE_FLOAT,
2206 register_conversion_function (G_TYPE_STRING,
2208 string_value_print);
2209 register_conversion_function (GTK_TYPE_THEMING_ENGINE,
2210 theming_engine_value_parse,
2211 theming_engine_value_print);
2212 register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
2213 animation_description_value_parse,
2214 animation_description_value_print);
2215 register_conversion_function (GTK_TYPE_BORDER,
2217 border_value_print);
2218 register_conversion_function (GTK_TYPE_GRADIENT,
2219 gradient_value_parse,
2220 gradient_value_print);
2221 register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
2222 pattern_value_parse,
2223 pattern_value_print);
2224 register_conversion_function (GTK_TYPE_BORDER_IMAGE,
2225 border_image_value_parse,
2227 register_conversion_function (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
2228 border_image_repeat_value_parse,
2229 border_image_repeat_value_print);
2230 register_conversion_function (GTK_TYPE_SHADOW,
2232 shadow_value_print);
2233 register_conversion_function (G_TYPE_ENUM,
2236 register_conversion_function (G_TYPE_FLAGS,
2242 _gtk_style_property_parse_value (const GtkStyleProperty *property,
2244 GtkCssParser *parser,
2247 GtkStyleParseFunc func;
2249 g_return_val_if_fail (value != NULL, FALSE);
2250 g_return_val_if_fail (parser != NULL, FALSE);
2252 css_string_funcs_init ();
2256 if (_gtk_css_parser_try (parser, "none", TRUE))
2258 /* Insert the default value, so it has an opportunity
2259 * to override other style providers when merged
2261 g_param_value_set_default (property->pspec, value);
2264 else if (property->property_parse_func)
2266 GError *error = NULL;
2270 value_str = _gtk_css_parser_read_value (parser);
2271 if (value_str == NULL)
2274 success = (*property->property_parse_func) (value_str, value, &error);
2281 func = property->parse_func;
2287 func = g_hash_table_lookup (parse_funcs,
2288 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2290 func = g_hash_table_lookup (parse_funcs,
2291 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2295 _gtk_css_parser_error (parser,
2296 "Cannot convert to type '%s'",
2297 g_type_name (G_VALUE_TYPE (value)));
2301 return (*func) (parser, base, value);
2305 _gtk_style_property_print_value (const GtkStyleProperty *property,
2306 const GValue *value,
2309 GtkStylePrintFunc func;
2311 css_string_funcs_init ();
2314 func = property->print_func;
2319 func = g_hash_table_lookup (print_funcs,
2320 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2322 func = g_hash_table_lookup (print_funcs,
2323 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2327 char *s = g_strdup_value_contents (value);
2328 g_string_append (string, s);
2333 func (value, string);
2337 _gtk_style_property_default_value (const GtkStyleProperty *property,
2338 GtkStyleProperties *properties,
2339 GtkStateFlags state,
2342 if (property->default_value_func)
2343 property->default_value_func (properties, state, value);
2344 else if (property->pspec->value_type == GTK_TYPE_THEMING_ENGINE)
2345 g_value_set_object (value, gtk_theming_engine_load (NULL));
2346 else if (property->pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
2347 g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
2348 else if (property->pspec->value_type == GDK_TYPE_RGBA)
2351 gdk_rgba_parse (&color, "pink");
2352 g_value_set_boxed (value, &color);
2354 else if (property->pspec->value_type == GTK_TYPE_BORDER)
2356 g_value_take_boxed (value, gtk_border_new ());
2359 g_param_value_set_default (property->pspec, value);
2363 _gtk_style_property_is_inherit (const GtkStyleProperty *property)
2365 g_return_val_if_fail (property != NULL, FALSE);
2367 return property->flags & GTK_STYLE_PROPERTY_INHERIT ? TRUE : FALSE;
2371 resolve_color (GtkStyleProperties *props,
2376 /* Resolve symbolic color to GdkRGBA */
2377 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
2380 /* Store it back, this is where GdkRGBA caching happens */
2381 g_value_unset (value);
2382 g_value_init (value, GDK_TYPE_RGBA);
2383 g_value_set_boxed (value, &color);
2389 resolve_color_rgb (GtkStyleProperties *props,
2392 GdkColor color = { 0 };
2395 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
2398 color.red = rgba.red * 65535. + 0.5;
2399 color.green = rgba.green * 65535. + 0.5;
2400 color.blue = rgba.blue * 65535. + 0.5;
2402 g_value_unset (value);
2403 g_value_init (value, GDK_TYPE_COLOR);
2404 g_value_set_boxed (value, &color);
2410 resolve_gradient (GtkStyleProperties *props,
2413 cairo_pattern_t *gradient;
2415 if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
2418 /* Store it back, this is where cairo_pattern_t caching happens */
2419 g_value_unset (value);
2420 g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
2421 g_value_take_boxed (value, gradient);
2427 resolve_shadow (GtkStyleProperties *props,
2430 GtkShadow *resolved, *base;
2432 base = g_value_get_boxed (value);
2437 if (_gtk_shadow_get_resolved (base))
2440 resolved = _gtk_shadow_resolve (base, props);
2441 if (resolved == NULL)
2444 g_value_take_boxed (value, resolved);
2450 _gtk_style_property_resolve (const GtkStyleProperty *property,
2451 GtkStyleProperties *props,
2452 GtkStateFlags state,
2455 if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
2457 if (property->pspec->value_type == GDK_TYPE_RGBA)
2459 if (resolve_color (props, val))
2462 else if (property->pspec->value_type == GDK_TYPE_COLOR)
2464 if (resolve_color_rgb (props, val))
2468 g_value_unset (val);
2469 g_value_init (val, property->pspec->value_type);
2470 _gtk_style_property_default_value (property, props, state, val);
2472 else if (G_VALUE_TYPE (val) == GDK_TYPE_RGBA)
2474 if (g_value_get_boxed (val) == NULL)
2475 _gtk_style_property_default_value (property, props, state, val);
2477 else if (G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
2479 g_return_if_fail (property->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN);
2481 if (!resolve_gradient (props, val))
2483 g_value_unset (val);
2484 g_value_init (val, CAIRO_GOBJECT_TYPE_PATTERN);
2485 _gtk_style_property_default_value (property, props, state, val);
2488 else if (G_VALUE_TYPE (val) == GTK_TYPE_SHADOW)
2490 if (!resolve_shadow (props, val))
2491 _gtk_style_property_default_value (property, props, state, val);
2496 _gtk_style_property_is_shorthand (const GtkStyleProperty *property)
2498 g_return_val_if_fail (property != NULL, FALSE);
2500 return property->pack_func != NULL;
2504 _gtk_style_property_unpack (const GtkStyleProperty *property,
2505 const GValue *value,
2508 g_return_val_if_fail (property != NULL, NULL);
2509 g_return_val_if_fail (property->unpack_func != NULL, NULL);
2510 g_return_val_if_fail (value != NULL, NULL);
2511 g_return_val_if_fail (n_params != NULL, NULL);
2513 return property->unpack_func (value, n_params);
2517 _gtk_style_property_pack (const GtkStyleProperty *property,
2518 GtkStyleProperties *props,
2519 GtkStateFlags state,
2522 g_return_if_fail (property != NULL);
2523 g_return_if_fail (property->pack_func != NULL);
2524 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
2525 g_return_if_fail (G_IS_VALUE (value));
2527 property->pack_func (value, props, state);
2531 gtk_style_property_init (void)
2533 if (G_LIKELY (properties))
2536 /* stuff is never freed, so no need for free functions */
2537 properties = g_hash_table_new (g_str_hash, g_str_equal);
2539 /* note that gtk_style_properties_register_property() calls this function,
2540 * so make sure we're sanely inited to avoid infloops */
2542 _gtk_style_property_register (g_param_spec_boxed ("color",
2546 GTK_STYLE_PROPERTY_INHERIT,
2555 gtk_style_properties_register_property (NULL,
2556 g_param_spec_boxed ("background-color",
2561 _gtk_style_property_register (g_param_spec_boxed ("font-family",
2565 GTK_STYLE_PROPERTY_INHERIT,
2570 font_family_value_print,
2573 _gtk_style_property_register (g_param_spec_enum ("font-style",
2577 PANGO_STYLE_NORMAL, 0),
2578 GTK_STYLE_PROPERTY_INHERIT,
2586 _gtk_style_property_register (g_param_spec_enum ("font-variant",
2590 PANGO_VARIANT_NORMAL, 0),
2591 GTK_STYLE_PROPERTY_INHERIT,
2599 /* xxx: need to parse this properly, ie parse the numbers */
2600 _gtk_style_property_register (g_param_spec_enum ("font-weight",
2604 PANGO_WEIGHT_NORMAL, 0),
2605 GTK_STYLE_PROPERTY_INHERIT,
2613 _gtk_style_property_register (g_param_spec_double ("font-size",
2616 0, G_MAXDOUBLE, 0, 0),
2617 GTK_STYLE_PROPERTY_INHERIT,
2625 _gtk_style_property_register (g_param_spec_boxed ("font",
2628 PANGO_TYPE_FONT_DESCRIPTION, 0),
2629 GTK_STYLE_PROPERTY_INHERIT,
2631 unpack_font_description,
2632 pack_font_description,
2633 font_description_value_parse,
2634 font_description_value_print,
2636 unset_font_description);
2638 _gtk_style_property_register (g_param_spec_boxed ("text-shadow",
2641 GTK_TYPE_SHADOW, 0),
2642 GTK_STYLE_PROPERTY_INHERIT,
2651 _gtk_style_property_register (g_param_spec_boxed ("icon-shadow",
2654 GTK_TYPE_SHADOW, 0),
2655 GTK_STYLE_PROPERTY_INHERIT,
2664 gtk_style_properties_register_property (NULL,
2665 g_param_spec_boxed ("box-shadow",
2668 GTK_TYPE_SHADOW, 0));
2669 gtk_style_properties_register_property (NULL,
2670 g_param_spec_int ("margin-top",
2673 0, G_MAXINT, 0, 0));
2674 gtk_style_properties_register_property (NULL,
2675 g_param_spec_int ("margin-left",
2678 0, G_MAXINT, 0, 0));
2679 gtk_style_properties_register_property (NULL,
2680 g_param_spec_int ("margin-bottom",
2683 0, G_MAXINT, 0, 0));
2684 gtk_style_properties_register_property (NULL,
2685 g_param_spec_int ("margin-right",
2688 0, G_MAXINT, 0, 0));
2689 _gtk_style_property_register (g_param_spec_boxed ("margin",
2692 GTK_TYPE_BORDER, 0),
2701 gtk_style_properties_register_property (NULL,
2702 g_param_spec_int ("padding-top",
2705 0, G_MAXINT, 0, 0));
2706 gtk_style_properties_register_property (NULL,
2707 g_param_spec_int ("padding-left",
2710 0, G_MAXINT, 0, 0));
2711 gtk_style_properties_register_property (NULL,
2712 g_param_spec_int ("padding-bottom",
2714 "Padding at bottom",
2715 0, G_MAXINT, 0, 0));
2716 gtk_style_properties_register_property (NULL,
2717 g_param_spec_int ("padding-right",
2720 0, G_MAXINT, 0, 0));
2721 _gtk_style_property_register (g_param_spec_boxed ("padding",
2724 GTK_TYPE_BORDER, 0),
2733 gtk_style_properties_register_property (NULL,
2734 g_param_spec_int ("border-top-width",
2736 "Border width at top",
2737 0, G_MAXINT, 0, 0));
2738 gtk_style_properties_register_property (NULL,
2739 g_param_spec_int ("border-left-width",
2740 "border left width",
2741 "Border width at left",
2742 0, G_MAXINT, 0, 0));
2743 gtk_style_properties_register_property (NULL,
2744 g_param_spec_int ("border-bottom-width",
2745 "border bottom width",
2746 "Border width at bottom",
2747 0, G_MAXINT, 0, 0));
2748 gtk_style_properties_register_property (NULL,
2749 g_param_spec_int ("border-right-width",
2750 "border right width",
2751 "Border width at right",
2752 0, G_MAXINT, 0, 0));
2753 _gtk_style_property_register (g_param_spec_boxed ("border-width",
2755 "Border width, in pixels",
2756 GTK_TYPE_BORDER, 0),
2759 unpack_border_width,
2764 unset_border_width);
2766 _gtk_style_property_register (g_param_spec_boxed ("border-top-left-radius",
2767 "Border top left radius",
2768 "Border radius of top left corner, in pixels",
2769 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2774 border_corner_radius_value_parse,
2775 border_corner_radius_value_print,
2778 _gtk_style_property_register (g_param_spec_boxed ("border-top-right-radius",
2779 "Border top right radius",
2780 "Border radius of top right corner, in pixels",
2781 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2786 border_corner_radius_value_parse,
2787 border_corner_radius_value_print,
2790 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-right-radius",
2791 "Border bottom right radius",
2792 "Border radius of bottom right corner, in pixels",
2793 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2798 border_corner_radius_value_parse,
2799 border_corner_radius_value_print,
2802 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-left-radius",
2803 "Border bottom left radius",
2804 "Border radius of bottom left corner, in pixels",
2805 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2810 border_corner_radius_value_parse,
2811 border_corner_radius_value_print,
2814 _gtk_style_property_register (g_param_spec_int ("border-radius",
2816 "Border radius, in pixels",
2820 unpack_border_radius,
2822 border_radius_value_parse,
2823 border_radius_value_print,
2825 unset_border_radius);
2827 gtk_style_properties_register_property (NULL,
2828 g_param_spec_enum ("border-style",
2831 GTK_TYPE_BORDER_STYLE,
2832 GTK_BORDER_STYLE_NONE, 0));
2833 _gtk_style_property_register (g_param_spec_boxed ("border-top-color",
2841 border_color_value_parse,
2843 border_color_default_value,
2845 _gtk_style_property_register (g_param_spec_boxed ("border-right-color",
2846 "Border right color",
2847 "Border right color",
2853 border_color_value_parse,
2855 border_color_default_value,
2857 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-color",
2858 "Border bottom color",
2859 "Border bottom color",
2865 border_color_value_parse,
2867 border_color_default_value,
2869 _gtk_style_property_register (g_param_spec_boxed ("border-left-color",
2870 "Border left color",
2871 "Border left color",
2877 border_color_value_parse,
2879 border_color_default_value,
2881 _gtk_style_property_register (g_param_spec_boxed ("border-color",
2887 unpack_border_color,
2889 border_color_shorthand_value_parse,
2892 unset_border_color);
2894 gtk_style_properties_register_property (NULL,
2895 g_param_spec_boxed ("background-image",
2898 CAIRO_GOBJECT_TYPE_PATTERN, 0));
2899 gtk_style_properties_register_property (NULL,
2900 g_param_spec_boxed ("border-image-source",
2901 "Border image source",
2902 "Border image source",
2903 CAIRO_GOBJECT_TYPE_PATTERN, 0));
2904 gtk_style_properties_register_property (NULL,
2905 g_param_spec_boxed ("border-image-repeat",
2906 "Border image repeat",
2907 "Border image repeat",
2908 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
2909 gtk_style_properties_register_property (NULL,
2910 g_param_spec_boxed ("border-image-slice",
2911 "Border image slice",
2912 "Border image slice",
2913 GTK_TYPE_BORDER, 0));
2914 _gtk_style_property_register (g_param_spec_boxed ("border-image-width",
2915 "Border image width",
2916 "Border image width",
2917 GTK_TYPE_BORDER, 0),
2924 border_image_width_default_value,
2926 _gtk_style_property_register (g_param_spec_boxed ("border-image",
2929 GTK_TYPE_BORDER_IMAGE, 0),
2932 _gtk_border_image_unpack,
2933 _gtk_border_image_pack,
2937 unset_border_image);
2938 gtk_style_properties_register_property (NULL,
2939 g_param_spec_object ("engine",
2942 GTK_TYPE_THEMING_ENGINE, 0));
2943 gtk_style_properties_register_property (NULL,
2944 g_param_spec_boxed ("transition",
2945 "Transition animation description",
2946 "Transition animation description",
2947 GTK_TYPE_ANIMATION_DESCRIPTION, 0));
2949 /* Private property holding the binding sets */
2950 _gtk_style_property_register (g_param_spec_boxed ("gtk-key-bindings",
2953 G_TYPE_PTR_ARRAY, 0),
2958 bindings_value_parse,
2959 bindings_value_print,
2964 const GtkStyleProperty *
2965 _gtk_style_property_lookup (const char *name)
2967 gtk_style_property_init ();
2969 return g_hash_table_lookup (properties, name);
2973 _gtk_style_property_register (GParamSpec *pspec,
2974 GtkStylePropertyFlags flags,
2975 GtkStylePropertyParser property_parse_func,
2976 GtkStyleUnpackFunc unpack_func,
2977 GtkStylePackFunc pack_func,
2978 GtkStyleParseFunc parse_func,
2979 GtkStylePrintFunc print_func,
2980 GtkStyleDefaultValueFunc default_value_func,
2981 GtkStyleUnsetFunc unset_func)
2983 const GtkStyleProperty *existing;
2984 GtkStyleProperty *node;
2986 g_return_if_fail ((pack_func == NULL) == (unpack_func == NULL));
2988 gtk_style_property_init ();
2990 existing = _gtk_style_property_lookup (pspec->name);
2991 if (existing != NULL)
2993 g_warning ("Property \"%s\" was already registered with type %s",
2994 pspec->name, g_type_name (existing->pspec->value_type));
2998 node = g_slice_new0 (GtkStyleProperty);
2999 node->flags = flags;
3000 node->pspec = pspec;
3001 node->property_parse_func = property_parse_func;
3002 node->pack_func = pack_func;
3003 node->unpack_func = unpack_func;
3004 node->parse_func = parse_func;
3005 node->print_func = print_func;
3006 node->default_value_func = default_value_func;
3007 node->unset_func = unset_func;
3009 /* pspec owns name */
3010 g_hash_table_insert (properties, (gchar *)pspec->name, node);