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 background_repeat_value_parse (GtkCssParser *parser,
1134 GtkCssBackgroundRepeat repeat;
1135 GtkCssBackgroundRepeatStyle style;
1137 if (_gtk_css_parser_try (parser, "repeat", TRUE))
1138 style = GTK_CSS_BACKGROUND_REPEAT_STYLE_REPEAT;
1139 else if (_gtk_css_parser_try (parser, "no-repeat", TRUE))
1140 style = GTK_CSS_BACKGROUND_REPEAT_STYLE_NO_REPEAT;
1142 style = GTK_CSS_BACKGROUND_REPEAT_STYLE_NONE;
1144 repeat.repeat = style;
1146 g_value_set_boxed (value, &repeat);
1151 static const gchar *
1152 background_repeat_style_to_string (GtkCssBackgroundRepeatStyle repeat)
1156 case GTK_CSS_BACKGROUND_REPEAT_STYLE_REPEAT:
1158 case GTK_CSS_BACKGROUND_REPEAT_STYLE_NO_REPEAT:
1166 background_repeat_value_print (const GValue *value,
1169 GtkCssBackgroundRepeat *repeat;
1171 repeat = g_value_get_boxed (value);
1173 g_string_append (string, background_repeat_style_to_string (repeat->repeat));
1177 border_image_repeat_value_parse (GtkCssParser *parser,
1181 GtkCssBorderImageRepeat image_repeat;
1182 GtkCssBorderRepeatStyle styles[2];
1185 for (i = 0; i < 2; i++)
1187 if (_gtk_css_parser_try (parser, "stretch", TRUE))
1188 styles[i] = GTK_CSS_REPEAT_STYLE_NONE;
1189 else if (_gtk_css_parser_try (parser, "repeat", TRUE))
1190 styles[i] = GTK_CSS_REPEAT_STYLE_REPEAT;
1191 else if (_gtk_css_parser_try (parser, "round", TRUE))
1192 styles[i] = GTK_CSS_REPEAT_STYLE_ROUND;
1193 else if (_gtk_css_parser_try (parser, "space", TRUE))
1194 styles[i] = GTK_CSS_REPEAT_STYLE_SPACE;
1197 styles[1] = styles[0] = GTK_CSS_REPEAT_STYLE_NONE;
1201 styles[i] = styles[0];
1204 image_repeat.hrepeat = styles[0];
1205 image_repeat.vrepeat = styles[1];
1207 g_value_set_boxed (value, &image_repeat);
1212 static const gchar *
1213 border_image_repeat_style_to_string (GtkCssBorderRepeatStyle repeat)
1217 case GTK_CSS_REPEAT_STYLE_NONE:
1219 case GTK_CSS_REPEAT_STYLE_REPEAT:
1221 case GTK_CSS_REPEAT_STYLE_ROUND:
1223 case GTK_CSS_REPEAT_STYLE_SPACE:
1231 border_image_repeat_value_print (const GValue *value,
1234 GtkCssBorderImageRepeat *image_repeat;
1236 image_repeat = g_value_get_boxed (value);
1238 g_string_append (string, border_image_repeat_style_to_string (image_repeat->hrepeat));
1239 if (image_repeat->hrepeat != image_repeat->vrepeat)
1241 g_string_append (string, " ");
1242 g_string_append (string, border_image_repeat_style_to_string (image_repeat->vrepeat));
1247 border_image_value_parse (GtkCssParser *parser,
1251 GValue temp = G_VALUE_INIT;
1252 cairo_pattern_t *pattern = NULL;
1253 GtkGradient *gradient = NULL;
1254 GtkBorder slice, *width = NULL, *parsed_slice;
1255 GtkCssBorderImageRepeat repeat, *parsed_repeat;
1256 gboolean retval = FALSE;
1257 GtkBorderImage *image = NULL;
1259 g_value_init (&temp, CAIRO_GOBJECT_TYPE_PATTERN);
1261 if (!pattern_value_parse (parser, base, &temp))
1264 if (G_VALUE_TYPE (&temp) == GTK_TYPE_GRADIENT)
1265 gradient = g_value_dup_boxed (&temp);
1267 pattern = g_value_dup_boxed (&temp);
1269 g_value_unset (&temp);
1270 g_value_init (&temp, GTK_TYPE_BORDER);
1272 if (!border_value_parse (parser, base, &temp))
1275 parsed_slice = g_value_get_boxed (&temp);
1276 slice = *parsed_slice;
1278 if (_gtk_css_parser_try (parser, "/", TRUE))
1280 g_value_unset (&temp);
1281 g_value_init (&temp, GTK_TYPE_BORDER);
1283 if (!border_value_parse (parser, base, &temp))
1286 width = g_value_dup_boxed (&temp);
1289 g_value_unset (&temp);
1290 g_value_init (&temp, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
1292 if (!border_image_repeat_value_parse (parser, base, &temp))
1295 parsed_repeat = g_value_get_boxed (&temp);
1296 repeat = *parsed_repeat;
1298 g_value_unset (&temp);
1300 if (gradient != NULL)
1301 image = _gtk_border_image_new_for_gradient (gradient, &slice, width, &repeat);
1302 else if (pattern != NULL)
1303 image = _gtk_border_image_new (pattern, &slice, width, &repeat);
1308 g_value_take_boxed (value, image);
1312 if (pattern != NULL)
1313 cairo_pattern_destroy (pattern);
1315 if (gradient != NULL)
1316 gtk_gradient_unref (gradient);
1319 gtk_border_free (width);
1325 enum_value_parse (GtkCssParser *parser,
1329 GEnumClass *enum_class;
1330 GEnumValue *enum_value;
1333 str = _gtk_css_parser_try_ident (parser, TRUE);
1336 _gtk_css_parser_error (parser, "Expected an identifier");
1340 enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1341 enum_value = g_enum_get_value_by_nick (enum_class, str);
1344 g_value_set_enum (value, enum_value->value);
1346 _gtk_css_parser_error (parser,
1347 "Unknown value '%s' for enum type '%s'",
1348 str, g_type_name (G_VALUE_TYPE (value)));
1350 g_type_class_unref (enum_class);
1353 return enum_value != NULL;
1357 enum_value_print (const GValue *value,
1360 GEnumClass *enum_class;
1361 GEnumValue *enum_value;
1363 enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1364 enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
1366 g_string_append (string, enum_value->value_nick);
1368 g_type_class_unref (enum_class);
1372 flags_value_parse (GtkCssParser *parser,
1376 GFlagsClass *flags_class;
1377 GFlagsValue *flag_value;
1381 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1384 str = _gtk_css_parser_try_ident (parser, TRUE);
1387 _gtk_css_parser_error (parser, "Expected an identifier");
1388 g_type_class_unref (flags_class);
1392 flag_value = g_flags_get_value_by_nick (flags_class, str);
1395 _gtk_css_parser_error (parser,
1396 "Unknown flag value '%s' for type '%s'",
1397 str, g_type_name (G_VALUE_TYPE (value)));
1398 /* XXX Do we want to return FALSE here? We can get
1399 * forward-compatibility for new values this way
1402 g_type_class_unref (flags_class);
1408 while (_gtk_css_parser_try (parser, ",", FALSE));
1410 g_type_class_unref (flags_class);
1412 g_value_set_enum (value, flags);
1418 flags_value_print (const GValue *value,
1421 GFlagsClass *flags_class;
1424 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1425 flags = g_value_get_flags (value);
1427 for (i = 0; i < flags_class->n_values; i++)
1429 GFlagsValue *flags_value = &flags_class->values[i];
1431 if (flags & flags_value->value)
1433 if (string->len != 0)
1434 g_string_append (string, ", ");
1436 g_string_append (string, flags_value->value_nick);
1440 g_type_class_unref (flags_class);
1444 bindings_value_parse (GtkCssParser *parser,
1449 GtkBindingSet *binding_set;
1452 array = g_ptr_array_new ();
1455 name = _gtk_css_parser_try_ident (parser, TRUE);
1458 _gtk_css_parser_error (parser, "Not a valid binding name");
1459 g_ptr_array_free (array, TRUE);
1463 binding_set = gtk_binding_set_find (name);
1467 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
1472 g_ptr_array_add (array, binding_set);
1475 while (_gtk_css_parser_try (parser, ",", TRUE));
1477 g_value_take_boxed (value, array);
1483 bindings_value_print (const GValue *value,
1489 array = g_value_get_boxed (value);
1491 for (i = 0; i < array->len; i++)
1493 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
1496 g_string_append (string, ", ");
1497 g_string_append (string, binding_set->set_name);
1502 border_corner_radius_value_parse (GtkCssParser *parser,
1506 GtkCssBorderCornerRadius corner;
1508 if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
1510 _gtk_css_parser_error (parser, "Expected a number");
1513 else if (corner.horizontal < 0)
1516 if (!_gtk_css_parser_try_double (parser, &corner.vertical))
1517 corner.vertical = corner.horizontal;
1518 else if (corner.vertical < 0)
1521 g_value_set_boxed (value, &corner);
1525 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1530 border_corner_radius_value_print (const GValue *value,
1533 GtkCssBorderCornerRadius *corner;
1535 corner = g_value_get_boxed (value);
1539 g_string_append (string, "none");
1543 string_append_double (string, corner->horizontal);
1544 if (corner->horizontal != corner->vertical)
1546 g_string_append_c (string, ' ');
1547 string_append_double (string, corner->vertical);
1552 border_radius_value_parse (GtkCssParser *parser,
1556 GtkCssBorderRadius border;
1558 if (!_gtk_css_parser_try_double (parser, &border.top_left.horizontal))
1560 _gtk_css_parser_error (parser, "Expected a number");
1563 else if (border.top_left.horizontal < 0)
1566 if (_gtk_css_parser_try_double (parser, &border.top_right.horizontal))
1568 if (border.top_right.horizontal < 0)
1570 if (_gtk_css_parser_try_double (parser, &border.bottom_right.horizontal))
1572 if (border.bottom_right.horizontal < 0)
1574 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.horizontal))
1575 border.bottom_left.horizontal = border.top_right.horizontal;
1576 else if (border.bottom_left.horizontal < 0)
1581 border.bottom_right.horizontal = border.top_left.horizontal;
1582 border.bottom_left.horizontal = border.top_right.horizontal;
1587 border.top_right.horizontal = border.top_left.horizontal;
1588 border.bottom_right.horizontal = border.top_left.horizontal;
1589 border.bottom_left.horizontal = border.top_left.horizontal;
1592 if (_gtk_css_parser_try (parser, "/", TRUE))
1594 if (!_gtk_css_parser_try_double (parser, &border.top_left.vertical))
1596 _gtk_css_parser_error (parser, "Expected a number");
1599 else if (border.top_left.vertical < 0)
1602 if (_gtk_css_parser_try_double (parser, &border.top_right.vertical))
1604 if (border.top_right.vertical < 0)
1606 if (_gtk_css_parser_try_double (parser, &border.bottom_right.vertical))
1608 if (border.bottom_right.vertical < 0)
1610 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.vertical))
1611 border.bottom_left.vertical = border.top_right.vertical;
1612 else if (border.bottom_left.vertical < 0)
1617 border.bottom_right.vertical = border.top_left.vertical;
1618 border.bottom_left.vertical = border.top_right.vertical;
1623 border.top_right.vertical = border.top_left.vertical;
1624 border.bottom_right.vertical = border.top_left.vertical;
1625 border.bottom_left.vertical = border.top_left.vertical;
1630 border.top_left.vertical = border.top_left.horizontal;
1631 border.top_right.vertical = border.top_right.horizontal;
1632 border.bottom_right.vertical = border.bottom_right.horizontal;
1633 border.bottom_left.vertical = border.bottom_left.horizontal;
1636 /* border-radius is an int property for backwards-compat reasons */
1637 g_value_unset (value);
1638 g_value_init (value, GTK_TYPE_CSS_BORDER_RADIUS);
1639 g_value_set_boxed (value, &border);
1644 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1649 border_radius_value_print (const GValue *value,
1652 GtkCssBorderRadius *border;
1654 border = g_value_get_boxed (value);
1658 g_string_append (string, "none");
1662 string_append_double (string, border->top_left.horizontal);
1663 if (border->top_left.horizontal != border->top_right.horizontal ||
1664 border->top_left.horizontal != border->bottom_right.horizontal ||
1665 border->top_left.horizontal != border->bottom_left.horizontal)
1667 g_string_append_c (string, ' ');
1668 string_append_double (string, border->top_right.horizontal);
1669 if (border->top_left.horizontal != border->bottom_right.horizontal ||
1670 border->top_right.horizontal != border->bottom_left.horizontal)
1672 g_string_append_c (string, ' ');
1673 string_append_double (string, border->bottom_right.horizontal);
1674 if (border->top_right.horizontal != border->bottom_left.horizontal)
1676 g_string_append_c (string, ' ');
1677 string_append_double (string, border->bottom_left.horizontal);
1682 if (border->top_left.horizontal != border->top_left.vertical ||
1683 border->top_right.horizontal != border->top_right.vertical ||
1684 border->bottom_right.horizontal != border->bottom_right.vertical ||
1685 border->bottom_left.horizontal != border->bottom_left.vertical)
1687 g_string_append (string, " / ");
1688 string_append_double (string, border->top_left.vertical);
1689 if (border->top_left.vertical != border->top_right.vertical ||
1690 border->top_left.vertical != border->bottom_right.vertical ||
1691 border->top_left.vertical != border->bottom_left.vertical)
1693 g_string_append_c (string, ' ');
1694 string_append_double (string, border->top_right.vertical);
1695 if (border->top_left.vertical != border->bottom_right.vertical ||
1696 border->top_right.vertical != border->bottom_left.vertical)
1698 g_string_append_c (string, ' ');
1699 string_append_double (string, border->bottom_right.vertical);
1700 if (border->top_right.vertical != border->bottom_left.vertical)
1702 g_string_append_c (string, ' ');
1703 string_append_double (string, border->bottom_left.vertical);
1712 transparent_color_value_parse (GtkCssParser *parser,
1716 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1718 GdkRGBA transparent = { 0, 0, 0, 0 };
1720 g_value_set_boxed (value, &transparent);
1725 return rgba_value_parse (parser, base, value);
1729 border_color_shorthand_value_parse (GtkCssParser *parser,
1733 GtkSymbolicColor *symbolic;
1736 array = g_ptr_array_new_with_free_func ((GDestroyNotify) gtk_symbolic_color_unref);
1740 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1742 GdkRGBA transparent = { 0, 0, 0, 0 };
1744 symbolic = gtk_symbolic_color_new_literal (&transparent);
1748 symbolic = _gtk_css_parser_read_symbolic_color (parser);
1750 if (symbolic == NULL)
1754 g_ptr_array_add (array, symbolic);
1756 while (array->len < 4 &&
1757 !_gtk_css_parser_is_eof (parser) &&
1758 !_gtk_css_parser_begins_with (parser, ';') &&
1759 !_gtk_css_parser_begins_with (parser, '}'));
1764 g_assert_not_reached ();
1767 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1770 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1773 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 1)));
1779 g_value_unset (value);
1780 g_value_init (value, G_TYPE_PTR_ARRAY);
1781 g_value_take_boxed (value, array);
1789 unpack_border (const GValue *value,
1796 GParameter *parameter = g_new0 (GParameter, 4);
1797 GtkBorder *border = g_value_get_boxed (value);
1799 parameter[0].name = top;
1800 g_value_init (¶meter[0].value, G_TYPE_INT);
1801 g_value_set_int (¶meter[0].value, border->top);
1802 parameter[1].name = left;
1803 g_value_init (¶meter[1].value, G_TYPE_INT);
1804 g_value_set_int (¶meter[1].value, border->left);
1805 parameter[2].name = bottom;
1806 g_value_init (¶meter[2].value, G_TYPE_INT);
1807 g_value_set_int (¶meter[2].value, border->bottom);
1808 parameter[3].name = right;
1809 g_value_init (¶meter[3].value, G_TYPE_INT);
1810 g_value_set_int (¶meter[3].value, border->right);
1817 pack_border (GValue *value,
1818 GtkStyleProperties *props,
1819 GtkStateFlags state,
1828 gtk_style_properties_get (props,
1841 g_value_set_boxed (value, &border);
1845 unpack_border_width (const GValue *value,
1848 return unpack_border (value, n_params,
1849 "border-top-width", "border-left-width",
1850 "border-bottom-width", "border-right-width");
1854 pack_border_width (GValue *value,
1855 GtkStyleProperties *props,
1856 GtkStateFlags state)
1858 pack_border (value, props, state,
1859 "border-top-width", "border-left-width",
1860 "border-bottom-width", "border-right-width");
1864 unpack_padding (const GValue *value,
1867 return unpack_border (value, n_params,
1868 "padding-top", "padding-left",
1869 "padding-bottom", "padding-right");
1873 pack_padding (GValue *value,
1874 GtkStyleProperties *props,
1875 GtkStateFlags state)
1877 pack_border (value, props, state,
1878 "padding-top", "padding-left",
1879 "padding-bottom", "padding-right");
1883 unpack_margin (const GValue *value,
1886 return unpack_border (value, n_params,
1887 "margin-top", "margin-left",
1888 "margin-bottom", "margin-right");
1892 pack_margin (GValue *value,
1893 GtkStyleProperties *props,
1894 GtkStateFlags state)
1896 pack_border (value, props, state,
1897 "margin-top", "margin-left",
1898 "margin-bottom", "margin-right");
1902 unpack_border_radius (const GValue *value,
1905 GParameter *parameter = g_new0 (GParameter, 4);
1906 GtkCssBorderRadius *border;
1908 if (G_VALUE_HOLDS_BOXED (value))
1909 border = g_value_get_boxed (value);
1913 parameter[0].name = "border-top-left-radius";
1914 g_value_init (¶meter[0].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1915 parameter[1].name = "border-top-right-radius";
1916 g_value_init (¶meter[1].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1917 parameter[2].name = "border-bottom-right-radius";
1918 g_value_init (¶meter[2].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1919 parameter[3].name = "border-bottom-left-radius";
1920 g_value_init (¶meter[3].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1923 g_value_set_boxed (¶meter[0].value, &border->top_left);
1924 g_value_set_boxed (¶meter[1].value, &border->top_right);
1925 g_value_set_boxed (¶meter[2].value, &border->bottom_right);
1926 g_value_set_boxed (¶meter[3].value, &border->bottom_left);
1934 pack_border_radius (GValue *value,
1935 GtkStyleProperties *props,
1936 GtkStateFlags state)
1938 GtkCssBorderCornerRadius *top_left;
1940 /* NB: We are an int property, so we have to resolve to an int here.
1941 * So we just resolve to an int. We pick one and stick to it.
1942 * Lesson learned: Don't query border-radius shorthand, query the
1943 * real properties instead. */
1944 gtk_style_properties_get (props,
1946 "border-top-left-radius", &top_left,
1950 g_value_set_int (value, top_left->horizontal);
1956 unpack_font_description (const GValue *value,
1959 GParameter *parameter = g_new0 (GParameter, 5);
1960 PangoFontDescription *description;
1964 /* For backwards compat, we only unpack values that are indeed set.
1965 * For strict CSS conformance we need to unpack all of them.
1966 * Note that we do set all of them in the parse function, so it
1967 * will not have effects when parsing CSS files. It will though
1968 * for custom style providers.
1971 description = g_value_get_boxed (value);
1975 mask = pango_font_description_get_set_fields (description);
1979 if (mask & PANGO_FONT_MASK_FAMILY)
1981 GPtrArray *strv = g_ptr_array_new ();
1983 g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
1984 g_ptr_array_add (strv, NULL);
1985 parameter[n].name = "font-family";
1986 g_value_init (¶meter[n].value, G_TYPE_STRV);
1987 g_value_take_boxed (¶meter[n].value,
1988 g_ptr_array_free (strv, FALSE));
1992 if (mask & PANGO_FONT_MASK_STYLE)
1994 parameter[n].name = "font-style";
1995 g_value_init (¶meter[n].value, PANGO_TYPE_STYLE);
1996 g_value_set_enum (¶meter[n].value,
1997 pango_font_description_get_style (description));
2001 if (mask & PANGO_FONT_MASK_VARIANT)
2003 parameter[n].name = "font-variant";
2004 g_value_init (¶meter[n].value, PANGO_TYPE_VARIANT);
2005 g_value_set_enum (¶meter[n].value,
2006 pango_font_description_get_variant (description));
2010 if (mask & PANGO_FONT_MASK_WEIGHT)
2012 parameter[n].name = "font-weight";
2013 g_value_init (¶meter[n].value, PANGO_TYPE_WEIGHT);
2014 g_value_set_enum (¶meter[n].value,
2015 pango_font_description_get_weight (description));
2019 if (mask & PANGO_FONT_MASK_SIZE)
2021 parameter[n].name = "font-size";
2022 g_value_init (¶meter[n].value, G_TYPE_DOUBLE);
2023 g_value_set_double (¶meter[n].value,
2024 (double) pango_font_description_get_size (description) / PANGO_SCALE);
2034 pack_font_description (GValue *value,
2035 GtkStyleProperties *props,
2036 GtkStateFlags state)
2038 PangoFontDescription *description;
2041 PangoVariant variant;
2045 gtk_style_properties_get (props,
2047 "font-family", &families,
2048 "font-style", &style,
2049 "font-variant", &variant,
2050 "font-weight", &weight,
2054 description = pango_font_description_new ();
2055 /* xxx: Can we set all the families here somehow? */
2057 pango_font_description_set_family (description, families[0]);
2058 pango_font_description_set_size (description, round (size * PANGO_SCALE));
2059 pango_font_description_set_style (description, style);
2060 pango_font_description_set_variant (description, variant);
2061 pango_font_description_set_weight (description, weight);
2063 g_strfreev (families);
2065 g_value_take_boxed (value, description);
2069 unpack_border_color (const GValue *value,
2072 GParameter *parameter = g_new0 (GParameter, 4);
2075 type = G_VALUE_TYPE (value);
2076 if (type == G_TYPE_PTR_ARRAY)
2077 type = GTK_TYPE_SYMBOLIC_COLOR;
2079 parameter[0].name = "border-top-color";
2080 g_value_init (¶meter[0].value, type);
2081 parameter[1].name = "border-right-color";
2082 g_value_init (¶meter[1].value, type);
2083 parameter[2].name = "border-bottom-color";
2084 g_value_init (¶meter[2].value, type);
2085 parameter[3].name = "border-left-color";
2086 g_value_init (¶meter[3].value, type);
2088 if (G_VALUE_TYPE (value) == G_TYPE_PTR_ARRAY)
2090 GPtrArray *array = g_value_get_boxed (value);
2093 for (i = 0; i < 4; i++)
2094 g_value_set_boxed (¶meter[i].value, g_ptr_array_index (array, i));
2098 /* can be RGBA or symbolic color */
2099 gpointer p = g_value_get_boxed (value);
2101 g_value_set_boxed (¶meter[0].value, p);
2102 g_value_set_boxed (¶meter[1].value, p);
2103 g_value_set_boxed (¶meter[2].value, p);
2104 g_value_set_boxed (¶meter[3].value, p);
2112 pack_border_color (GValue *value,
2113 GtkStyleProperties *props,
2114 GtkStateFlags state)
2116 /* NB: We are a color property, so we have to resolve to a color here.
2117 * So we just resolve to a color. We pick one and stick to it.
2118 * Lesson learned: Don't query border-color shorthand, query the
2119 * real properties instead. */
2120 g_value_unset (value);
2121 gtk_style_properties_get_property (props, "border-top-color", state, value);
2124 /*** UNSET FUNCS ***/
2127 unset_font_description (GtkStyleProperties *props,
2128 GtkStateFlags state)
2130 gtk_style_properties_unset_property (props, "font-family", state);
2131 gtk_style_properties_unset_property (props, "font-style", state);
2132 gtk_style_properties_unset_property (props, "font-variant", state);
2133 gtk_style_properties_unset_property (props, "font-weight", state);
2134 gtk_style_properties_unset_property (props, "font-size", state);
2138 unset_margin (GtkStyleProperties *props,
2139 GtkStateFlags state)
2141 gtk_style_properties_unset_property (props, "margin-top", state);
2142 gtk_style_properties_unset_property (props, "margin-right", state);
2143 gtk_style_properties_unset_property (props, "margin-bottom", state);
2144 gtk_style_properties_unset_property (props, "margin-left", state);
2148 unset_padding (GtkStyleProperties *props,
2149 GtkStateFlags state)
2151 gtk_style_properties_unset_property (props, "padding-top", state);
2152 gtk_style_properties_unset_property (props, "padding-right", state);
2153 gtk_style_properties_unset_property (props, "padding-bottom", state);
2154 gtk_style_properties_unset_property (props, "padding-left", state);
2158 unset_border_width (GtkStyleProperties *props,
2159 GtkStateFlags state)
2161 gtk_style_properties_unset_property (props, "border-top-width", state);
2162 gtk_style_properties_unset_property (props, "border-right-width", state);
2163 gtk_style_properties_unset_property (props, "border-bottom-width", state);
2164 gtk_style_properties_unset_property (props, "border-left-width", state);
2168 unset_border_radius (GtkStyleProperties *props,
2169 GtkStateFlags state)
2171 gtk_style_properties_unset_property (props, "border-top-right-radius", state);
2172 gtk_style_properties_unset_property (props, "border-bottom-right-radius", state);
2173 gtk_style_properties_unset_property (props, "border-bottom-left-radius", state);
2174 gtk_style_properties_unset_property (props, "border-top-left-radius", state);
2178 unset_border_color (GtkStyleProperties *props,
2179 GtkStateFlags state)
2181 gtk_style_properties_unset_property (props, "border-top-color", state);
2182 gtk_style_properties_unset_property (props, "border-right-color", state);
2183 gtk_style_properties_unset_property (props, "border-bottom-color", state);
2184 gtk_style_properties_unset_property (props, "border-left-color", state);
2188 unset_border_image (GtkStyleProperties *props,
2189 GtkStateFlags state)
2191 gtk_style_properties_unset_property (props, "border-image-source", state);
2192 gtk_style_properties_unset_property (props, "border-image-slice", state);
2193 gtk_style_properties_unset_property (props, "border-image-repeat", state);
2194 gtk_style_properties_unset_property (props, "border-image-width", state);
2197 /*** default values ***/
2200 border_image_width_default_value (GtkStyleProperties *props,
2201 GtkStateFlags state,
2207 background_color_default_value (GtkStyleProperties *props,
2208 GtkStateFlags state,
2211 GdkRGBA transparent_black = { 0, 0, 0, 0 };
2213 g_value_set_boxed (value, &transparent_black);
2217 border_color_default_value (GtkStyleProperties *props,
2218 GtkStateFlags state,
2221 g_value_unset (value);
2222 gtk_style_properties_get_property (props, "color", state, value);
2228 css_string_funcs_init (void)
2230 if (G_LIKELY (parse_funcs != NULL))
2233 parse_funcs = g_hash_table_new (NULL, NULL);
2234 print_funcs = g_hash_table_new (NULL, NULL);
2236 register_conversion_function (GDK_TYPE_RGBA,
2239 register_conversion_function (GDK_TYPE_COLOR,
2242 register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
2243 symbolic_color_value_parse,
2244 symbolic_color_value_print);
2245 register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
2246 font_description_value_parse,
2247 font_description_value_print);
2248 register_conversion_function (G_TYPE_BOOLEAN,
2249 boolean_value_parse,
2250 boolean_value_print);
2251 register_conversion_function (G_TYPE_INT,
2254 register_conversion_function (G_TYPE_UINT,
2257 register_conversion_function (G_TYPE_DOUBLE,
2259 double_value_print);
2260 register_conversion_function (G_TYPE_FLOAT,
2263 register_conversion_function (G_TYPE_STRING,
2265 string_value_print);
2266 register_conversion_function (GTK_TYPE_THEMING_ENGINE,
2267 theming_engine_value_parse,
2268 theming_engine_value_print);
2269 register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
2270 animation_description_value_parse,
2271 animation_description_value_print);
2272 register_conversion_function (GTK_TYPE_BORDER,
2274 border_value_print);
2275 register_conversion_function (GTK_TYPE_GRADIENT,
2276 gradient_value_parse,
2277 gradient_value_print);
2278 register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
2279 pattern_value_parse,
2280 pattern_value_print);
2281 register_conversion_function (GTK_TYPE_BORDER_IMAGE,
2282 border_image_value_parse,
2284 register_conversion_function (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
2285 border_image_repeat_value_parse,
2286 border_image_repeat_value_print);
2287 register_conversion_function (GTK_TYPE_SHADOW,
2289 shadow_value_print);
2290 register_conversion_function (G_TYPE_ENUM,
2293 register_conversion_function (G_TYPE_FLAGS,
2296 register_conversion_function (GTK_TYPE_CSS_BACKGROUND_REPEAT,
2297 background_repeat_value_parse,
2298 background_repeat_value_print);
2302 _gtk_style_property_parse_value (const GtkStyleProperty *property,
2304 GtkCssParser *parser,
2307 GtkStyleParseFunc func;
2309 g_return_val_if_fail (value != NULL, FALSE);
2310 g_return_val_if_fail (parser != NULL, FALSE);
2312 css_string_funcs_init ();
2316 if (_gtk_css_parser_try (parser, "none", TRUE))
2318 /* Insert the default value, so it has an opportunity
2319 * to override other style providers when merged
2321 g_param_value_set_default (property->pspec, value);
2324 else if (property->property_parse_func)
2326 GError *error = NULL;
2330 value_str = _gtk_css_parser_read_value (parser);
2331 if (value_str == NULL)
2334 success = (*property->property_parse_func) (value_str, value, &error);
2341 func = property->parse_func;
2347 func = g_hash_table_lookup (parse_funcs,
2348 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2350 func = g_hash_table_lookup (parse_funcs,
2351 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2355 _gtk_css_parser_error (parser,
2356 "Cannot convert to type '%s'",
2357 g_type_name (G_VALUE_TYPE (value)));
2361 return (*func) (parser, base, value);
2365 _gtk_style_property_print_value (const GtkStyleProperty *property,
2366 const GValue *value,
2369 GtkStylePrintFunc func;
2371 css_string_funcs_init ();
2374 func = property->print_func;
2379 func = g_hash_table_lookup (print_funcs,
2380 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2382 func = g_hash_table_lookup (print_funcs,
2383 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2387 char *s = g_strdup_value_contents (value);
2388 g_string_append (string, s);
2393 func (value, string);
2397 _gtk_style_property_default_value (const GtkStyleProperty *property,
2398 GtkStyleProperties *properties,
2399 GtkStateFlags state,
2402 if (property->default_value_func)
2403 property->default_value_func (properties, state, value);
2404 else if (property->pspec->value_type == GTK_TYPE_THEMING_ENGINE)
2405 g_value_set_object (value, gtk_theming_engine_load (NULL));
2406 else if (property->pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
2407 g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
2408 else if (property->pspec->value_type == GDK_TYPE_RGBA)
2411 gdk_rgba_parse (&color, "pink");
2412 g_value_set_boxed (value, &color);
2414 else if (property->pspec->value_type == GTK_TYPE_BORDER)
2416 g_value_take_boxed (value, gtk_border_new ());
2419 g_param_value_set_default (property->pspec, value);
2423 _gtk_style_property_is_inherit (const GtkStyleProperty *property)
2425 g_return_val_if_fail (property != NULL, FALSE);
2427 return property->flags & GTK_STYLE_PROPERTY_INHERIT ? TRUE : FALSE;
2431 resolve_color (GtkStyleProperties *props,
2436 /* Resolve symbolic color to GdkRGBA */
2437 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
2440 /* Store it back, this is where GdkRGBA caching happens */
2441 g_value_unset (value);
2442 g_value_init (value, GDK_TYPE_RGBA);
2443 g_value_set_boxed (value, &color);
2449 resolve_color_rgb (GtkStyleProperties *props,
2452 GdkColor color = { 0 };
2455 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
2458 color.red = rgba.red * 65535. + 0.5;
2459 color.green = rgba.green * 65535. + 0.5;
2460 color.blue = rgba.blue * 65535. + 0.5;
2462 g_value_unset (value);
2463 g_value_init (value, GDK_TYPE_COLOR);
2464 g_value_set_boxed (value, &color);
2470 resolve_gradient (GtkStyleProperties *props,
2473 cairo_pattern_t *gradient;
2475 if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
2478 /* Store it back, this is where cairo_pattern_t caching happens */
2479 g_value_unset (value);
2480 g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
2481 g_value_take_boxed (value, gradient);
2487 resolve_shadow (GtkStyleProperties *props,
2490 GtkShadow *resolved, *base;
2492 base = g_value_get_boxed (value);
2497 if (_gtk_shadow_get_resolved (base))
2500 resolved = _gtk_shadow_resolve (base, props);
2501 if (resolved == NULL)
2504 g_value_take_boxed (value, resolved);
2510 _gtk_style_property_resolve (const GtkStyleProperty *property,
2511 GtkStyleProperties *props,
2512 GtkStateFlags state,
2515 if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
2517 if (property->pspec->value_type == GDK_TYPE_RGBA)
2519 if (resolve_color (props, val))
2522 else if (property->pspec->value_type == GDK_TYPE_COLOR)
2524 if (resolve_color_rgb (props, val))
2528 g_value_unset (val);
2529 g_value_init (val, property->pspec->value_type);
2530 _gtk_style_property_default_value (property, props, state, val);
2532 else if (G_VALUE_TYPE (val) == GDK_TYPE_RGBA)
2534 if (g_value_get_boxed (val) == NULL)
2535 _gtk_style_property_default_value (property, props, state, val);
2537 else if (G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
2539 g_return_if_fail (property->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN);
2541 if (!resolve_gradient (props, val))
2543 g_value_unset (val);
2544 g_value_init (val, CAIRO_GOBJECT_TYPE_PATTERN);
2545 _gtk_style_property_default_value (property, props, state, val);
2548 else if (G_VALUE_TYPE (val) == GTK_TYPE_SHADOW)
2550 if (!resolve_shadow (props, val))
2551 _gtk_style_property_default_value (property, props, state, val);
2556 _gtk_style_property_is_shorthand (const GtkStyleProperty *property)
2558 g_return_val_if_fail (property != NULL, FALSE);
2560 return property->pack_func != NULL;
2564 _gtk_style_property_unpack (const GtkStyleProperty *property,
2565 const GValue *value,
2568 g_return_val_if_fail (property != NULL, NULL);
2569 g_return_val_if_fail (property->unpack_func != NULL, NULL);
2570 g_return_val_if_fail (value != NULL, NULL);
2571 g_return_val_if_fail (n_params != NULL, NULL);
2573 return property->unpack_func (value, n_params);
2577 _gtk_style_property_pack (const GtkStyleProperty *property,
2578 GtkStyleProperties *props,
2579 GtkStateFlags state,
2582 g_return_if_fail (property != NULL);
2583 g_return_if_fail (property->pack_func != NULL);
2584 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
2585 g_return_if_fail (G_IS_VALUE (value));
2587 property->pack_func (value, props, state);
2591 gtk_style_property_init (void)
2593 if (G_LIKELY (properties))
2596 /* stuff is never freed, so no need for free functions */
2597 properties = g_hash_table_new (g_str_hash, g_str_equal);
2599 /* note that gtk_style_properties_register_property() calls this function,
2600 * so make sure we're sanely inited to avoid infloops */
2602 _gtk_style_property_register (g_param_spec_boxed ("color",
2606 GTK_STYLE_PROPERTY_INHERIT,
2614 _gtk_style_property_register (g_param_spec_boxed ("background-color",
2622 transparent_color_value_parse,
2624 background_color_default_value,
2627 _gtk_style_property_register (g_param_spec_boxed ("font-family",
2631 GTK_STYLE_PROPERTY_INHERIT,
2636 font_family_value_print,
2639 _gtk_style_property_register (g_param_spec_enum ("font-style",
2643 PANGO_STYLE_NORMAL, 0),
2644 GTK_STYLE_PROPERTY_INHERIT,
2652 _gtk_style_property_register (g_param_spec_enum ("font-variant",
2656 PANGO_VARIANT_NORMAL, 0),
2657 GTK_STYLE_PROPERTY_INHERIT,
2665 /* xxx: need to parse this properly, ie parse the numbers */
2666 _gtk_style_property_register (g_param_spec_enum ("font-weight",
2670 PANGO_WEIGHT_NORMAL, 0),
2671 GTK_STYLE_PROPERTY_INHERIT,
2679 _gtk_style_property_register (g_param_spec_double ("font-size",
2682 0, G_MAXDOUBLE, 0, 0),
2683 GTK_STYLE_PROPERTY_INHERIT,
2691 _gtk_style_property_register (g_param_spec_boxed ("font",
2694 PANGO_TYPE_FONT_DESCRIPTION, 0),
2695 GTK_STYLE_PROPERTY_INHERIT,
2697 unpack_font_description,
2698 pack_font_description,
2699 font_description_value_parse,
2700 font_description_value_print,
2702 unset_font_description);
2704 _gtk_style_property_register (g_param_spec_boxed ("text-shadow",
2707 GTK_TYPE_SHADOW, 0),
2708 GTK_STYLE_PROPERTY_INHERIT,
2717 _gtk_style_property_register (g_param_spec_boxed ("icon-shadow",
2720 GTK_TYPE_SHADOW, 0),
2721 GTK_STYLE_PROPERTY_INHERIT,
2730 gtk_style_properties_register_property (NULL,
2731 g_param_spec_boxed ("box-shadow",
2734 GTK_TYPE_SHADOW, 0));
2735 gtk_style_properties_register_property (NULL,
2736 g_param_spec_int ("margin-top",
2739 0, G_MAXINT, 0, 0));
2740 gtk_style_properties_register_property (NULL,
2741 g_param_spec_int ("margin-left",
2744 0, G_MAXINT, 0, 0));
2745 gtk_style_properties_register_property (NULL,
2746 g_param_spec_int ("margin-bottom",
2749 0, G_MAXINT, 0, 0));
2750 gtk_style_properties_register_property (NULL,
2751 g_param_spec_int ("margin-right",
2754 0, G_MAXINT, 0, 0));
2755 _gtk_style_property_register (g_param_spec_boxed ("margin",
2758 GTK_TYPE_BORDER, 0),
2767 gtk_style_properties_register_property (NULL,
2768 g_param_spec_int ("padding-top",
2771 0, G_MAXINT, 0, 0));
2772 gtk_style_properties_register_property (NULL,
2773 g_param_spec_int ("padding-left",
2776 0, G_MAXINT, 0, 0));
2777 gtk_style_properties_register_property (NULL,
2778 g_param_spec_int ("padding-bottom",
2780 "Padding at bottom",
2781 0, G_MAXINT, 0, 0));
2782 gtk_style_properties_register_property (NULL,
2783 g_param_spec_int ("padding-right",
2786 0, G_MAXINT, 0, 0));
2787 _gtk_style_property_register (g_param_spec_boxed ("padding",
2790 GTK_TYPE_BORDER, 0),
2799 gtk_style_properties_register_property (NULL,
2800 g_param_spec_int ("border-top-width",
2802 "Border width at top",
2803 0, G_MAXINT, 0, 0));
2804 gtk_style_properties_register_property (NULL,
2805 g_param_spec_int ("border-left-width",
2806 "border left width",
2807 "Border width at left",
2808 0, G_MAXINT, 0, 0));
2809 gtk_style_properties_register_property (NULL,
2810 g_param_spec_int ("border-bottom-width",
2811 "border bottom width",
2812 "Border width at bottom",
2813 0, G_MAXINT, 0, 0));
2814 gtk_style_properties_register_property (NULL,
2815 g_param_spec_int ("border-right-width",
2816 "border right width",
2817 "Border width at right",
2818 0, G_MAXINT, 0, 0));
2819 _gtk_style_property_register (g_param_spec_boxed ("border-width",
2821 "Border width, in pixels",
2822 GTK_TYPE_BORDER, 0),
2825 unpack_border_width,
2830 unset_border_width);
2832 _gtk_style_property_register (g_param_spec_boxed ("border-top-left-radius",
2833 "Border top left radius",
2834 "Border radius of top left corner, in pixels",
2835 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2840 border_corner_radius_value_parse,
2841 border_corner_radius_value_print,
2844 _gtk_style_property_register (g_param_spec_boxed ("border-top-right-radius",
2845 "Border top right radius",
2846 "Border radius of top right corner, in pixels",
2847 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2852 border_corner_radius_value_parse,
2853 border_corner_radius_value_print,
2856 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-right-radius",
2857 "Border bottom right radius",
2858 "Border radius of bottom right corner, in pixels",
2859 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2864 border_corner_radius_value_parse,
2865 border_corner_radius_value_print,
2868 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-left-radius",
2869 "Border bottom left radius",
2870 "Border radius of bottom left corner, in pixels",
2871 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2876 border_corner_radius_value_parse,
2877 border_corner_radius_value_print,
2880 _gtk_style_property_register (g_param_spec_int ("border-radius",
2882 "Border radius, in pixels",
2886 unpack_border_radius,
2888 border_radius_value_parse,
2889 border_radius_value_print,
2891 unset_border_radius);
2893 gtk_style_properties_register_property (NULL,
2894 g_param_spec_enum ("border-style",
2897 GTK_TYPE_BORDER_STYLE,
2898 GTK_BORDER_STYLE_NONE, 0));
2899 _gtk_style_property_register (g_param_spec_boxed ("border-top-color",
2907 transparent_color_value_parse,
2909 border_color_default_value,
2911 _gtk_style_property_register (g_param_spec_boxed ("border-right-color",
2912 "Border right color",
2913 "Border right color",
2919 transparent_color_value_parse,
2921 border_color_default_value,
2923 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-color",
2924 "Border bottom color",
2925 "Border bottom color",
2931 transparent_color_value_parse,
2933 border_color_default_value,
2935 _gtk_style_property_register (g_param_spec_boxed ("border-left-color",
2936 "Border left color",
2937 "Border left color",
2943 transparent_color_value_parse,
2945 border_color_default_value,
2947 _gtk_style_property_register (g_param_spec_boxed ("border-color",
2953 unpack_border_color,
2955 border_color_shorthand_value_parse,
2958 unset_border_color);
2960 gtk_style_properties_register_property (NULL,
2961 g_param_spec_boxed ("background-image",
2964 CAIRO_GOBJECT_TYPE_PATTERN, 0));
2965 gtk_style_properties_register_property (NULL,
2966 g_param_spec_boxed ("background-repeat",
2967 "Background repeat",
2968 "Background repeat",
2969 GTK_TYPE_CSS_BACKGROUND_REPEAT, 0));
2971 gtk_style_properties_register_property (NULL,
2972 g_param_spec_boxed ("border-image-source",
2973 "Border image source",
2974 "Border image source",
2975 CAIRO_GOBJECT_TYPE_PATTERN, 0));
2976 gtk_style_properties_register_property (NULL,
2977 g_param_spec_boxed ("border-image-repeat",
2978 "Border image repeat",
2979 "Border image repeat",
2980 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
2981 gtk_style_properties_register_property (NULL,
2982 g_param_spec_boxed ("border-image-slice",
2983 "Border image slice",
2984 "Border image slice",
2985 GTK_TYPE_BORDER, 0));
2986 _gtk_style_property_register (g_param_spec_boxed ("border-image-width",
2987 "Border image width",
2988 "Border image width",
2989 GTK_TYPE_BORDER, 0),
2996 border_image_width_default_value,
2998 _gtk_style_property_register (g_param_spec_boxed ("border-image",
3001 GTK_TYPE_BORDER_IMAGE, 0),
3004 _gtk_border_image_unpack,
3005 _gtk_border_image_pack,
3009 unset_border_image);
3010 gtk_style_properties_register_property (NULL,
3011 g_param_spec_object ("engine",
3014 GTK_TYPE_THEMING_ENGINE, 0));
3015 gtk_style_properties_register_property (NULL,
3016 g_param_spec_boxed ("transition",
3017 "Transition animation description",
3018 "Transition animation description",
3019 GTK_TYPE_ANIMATION_DESCRIPTION, 0));
3021 /* Private property holding the binding sets */
3022 _gtk_style_property_register (g_param_spec_boxed ("gtk-key-bindings",
3025 G_TYPE_PTR_ARRAY, 0),
3030 bindings_value_parse,
3031 bindings_value_print,
3036 const GtkStyleProperty *
3037 _gtk_style_property_lookup (const char *name)
3039 gtk_style_property_init ();
3041 return g_hash_table_lookup (properties, name);
3045 _gtk_style_property_register (GParamSpec *pspec,
3046 GtkStylePropertyFlags flags,
3047 GtkStylePropertyParser property_parse_func,
3048 GtkStyleUnpackFunc unpack_func,
3049 GtkStylePackFunc pack_func,
3050 GtkStyleParseFunc parse_func,
3051 GtkStylePrintFunc print_func,
3052 GtkStyleDefaultValueFunc default_value_func,
3053 GtkStyleUnsetFunc unset_func)
3055 const GtkStyleProperty *existing;
3056 GtkStyleProperty *node;
3058 g_return_if_fail ((pack_func == NULL) == (unpack_func == NULL));
3060 gtk_style_property_init ();
3062 existing = _gtk_style_property_lookup (pspec->name);
3063 if (existing != NULL)
3065 g_warning ("Property \"%s\" was already registered with type %s",
3066 pspec->name, g_type_name (existing->pspec->value_type));
3070 node = g_slice_new0 (GtkStyleProperty);
3071 node->flags = flags;
3072 node->pspec = pspec;
3073 node->property_parse_func = property_parse_func;
3074 node->pack_func = pack_func;
3075 node->unpack_func = unpack_func;
3076 node->parse_func = parse_func;
3077 node->print_func = print_func;
3078 node->default_value_func = default_value_func;
3079 node->unset_func = unset_func;
3081 /* pspec owns name */
3082 g_hash_table_insert (properties, (gchar *)pspec->name, node);