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 gconstpointer *boxed = NULL;
1255 GtkBorder slice, *width = NULL, *parsed_slice;
1256 GtkCssBorderImageRepeat repeat, *parsed_repeat;
1257 gboolean retval = FALSE;
1258 GtkBorderImage *image = NULL;
1260 g_value_init (&temp, CAIRO_GOBJECT_TYPE_PATTERN);
1262 if (!pattern_value_parse (parser, base, &temp))
1265 boxed_type = G_VALUE_TYPE (&temp);
1266 if (boxed_type != CAIRO_GOBJECT_TYPE_PATTERN)
1267 boxed = g_value_dup_boxed (&temp);
1269 pattern = g_value_dup_boxed (&temp);
1271 g_value_unset (&temp);
1272 g_value_init (&temp, GTK_TYPE_BORDER);
1274 if (!border_value_parse (parser, base, &temp))
1277 parsed_slice = g_value_get_boxed (&temp);
1278 slice = *parsed_slice;
1280 if (_gtk_css_parser_try (parser, "/", TRUE))
1282 g_value_unset (&temp);
1283 g_value_init (&temp, GTK_TYPE_BORDER);
1285 if (!border_value_parse (parser, base, &temp))
1288 width = g_value_dup_boxed (&temp);
1291 g_value_unset (&temp);
1292 g_value_init (&temp, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
1294 if (!border_image_repeat_value_parse (parser, base, &temp))
1297 parsed_repeat = g_value_get_boxed (&temp);
1298 repeat = *parsed_repeat;
1300 g_value_unset (&temp);
1303 image = _gtk_border_image_new_for_boxed (boxed_type, boxed, &slice, width, &repeat);
1304 else if (pattern != NULL)
1305 image = _gtk_border_image_new (pattern, &slice, width, &repeat);
1310 g_value_take_boxed (value, image);
1314 if (pattern != NULL)
1315 cairo_pattern_destroy (pattern);
1318 g_boxed_free (boxed_type, boxed);
1321 gtk_border_free (width);
1327 enum_value_parse (GtkCssParser *parser,
1331 GEnumClass *enum_class;
1332 GEnumValue *enum_value;
1335 str = _gtk_css_parser_try_ident (parser, TRUE);
1338 _gtk_css_parser_error (parser, "Expected an identifier");
1342 enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1343 enum_value = g_enum_get_value_by_nick (enum_class, str);
1346 g_value_set_enum (value, enum_value->value);
1348 _gtk_css_parser_error (parser,
1349 "Unknown value '%s' for enum type '%s'",
1350 str, g_type_name (G_VALUE_TYPE (value)));
1352 g_type_class_unref (enum_class);
1355 return enum_value != NULL;
1359 enum_value_print (const GValue *value,
1362 GEnumClass *enum_class;
1363 GEnumValue *enum_value;
1365 enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1366 enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
1368 g_string_append (string, enum_value->value_nick);
1370 g_type_class_unref (enum_class);
1374 flags_value_parse (GtkCssParser *parser,
1378 GFlagsClass *flags_class;
1379 GFlagsValue *flag_value;
1383 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1386 str = _gtk_css_parser_try_ident (parser, TRUE);
1389 _gtk_css_parser_error (parser, "Expected an identifier");
1390 g_type_class_unref (flags_class);
1394 flag_value = g_flags_get_value_by_nick (flags_class, str);
1397 _gtk_css_parser_error (parser,
1398 "Unknown flag value '%s' for type '%s'",
1399 str, g_type_name (G_VALUE_TYPE (value)));
1400 /* XXX Do we want to return FALSE here? We can get
1401 * forward-compatibility for new values this way
1404 g_type_class_unref (flags_class);
1410 while (_gtk_css_parser_try (parser, ",", FALSE));
1412 g_type_class_unref (flags_class);
1414 g_value_set_enum (value, flags);
1420 flags_value_print (const GValue *value,
1423 GFlagsClass *flags_class;
1426 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1427 flags = g_value_get_flags (value);
1429 for (i = 0; i < flags_class->n_values; i++)
1431 GFlagsValue *flags_value = &flags_class->values[i];
1433 if (flags & flags_value->value)
1435 if (string->len != 0)
1436 g_string_append (string, ", ");
1438 g_string_append (string, flags_value->value_nick);
1442 g_type_class_unref (flags_class);
1446 bindings_value_parse (GtkCssParser *parser,
1451 GtkBindingSet *binding_set;
1454 array = g_ptr_array_new ();
1457 name = _gtk_css_parser_try_ident (parser, TRUE);
1460 _gtk_css_parser_error (parser, "Not a valid binding name");
1461 g_ptr_array_free (array, TRUE);
1465 binding_set = gtk_binding_set_find (name);
1469 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
1474 g_ptr_array_add (array, binding_set);
1477 while (_gtk_css_parser_try (parser, ",", TRUE));
1479 g_value_take_boxed (value, array);
1485 bindings_value_print (const GValue *value,
1491 array = g_value_get_boxed (value);
1493 for (i = 0; i < array->len; i++)
1495 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
1498 g_string_append (string, ", ");
1499 g_string_append (string, binding_set->set_name);
1504 border_corner_radius_value_parse (GtkCssParser *parser,
1508 GtkCssBorderCornerRadius corner;
1510 if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
1512 _gtk_css_parser_error (parser, "Expected a number");
1515 else if (corner.horizontal < 0)
1518 if (!_gtk_css_parser_try_double (parser, &corner.vertical))
1519 corner.vertical = corner.horizontal;
1520 else if (corner.vertical < 0)
1523 g_value_set_boxed (value, &corner);
1527 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1532 border_corner_radius_value_print (const GValue *value,
1535 GtkCssBorderCornerRadius *corner;
1537 corner = g_value_get_boxed (value);
1541 g_string_append (string, "none");
1545 string_append_double (string, corner->horizontal);
1546 if (corner->horizontal != corner->vertical)
1548 g_string_append_c (string, ' ');
1549 string_append_double (string, corner->vertical);
1554 border_radius_value_parse (GtkCssParser *parser,
1558 GtkCssBorderRadius border;
1560 if (!_gtk_css_parser_try_double (parser, &border.top_left.horizontal))
1562 _gtk_css_parser_error (parser, "Expected a number");
1565 else if (border.top_left.horizontal < 0)
1568 if (_gtk_css_parser_try_double (parser, &border.top_right.horizontal))
1570 if (border.top_right.horizontal < 0)
1572 if (_gtk_css_parser_try_double (parser, &border.bottom_right.horizontal))
1574 if (border.bottom_right.horizontal < 0)
1576 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.horizontal))
1577 border.bottom_left.horizontal = border.top_right.horizontal;
1578 else if (border.bottom_left.horizontal < 0)
1583 border.bottom_right.horizontal = border.top_left.horizontal;
1584 border.bottom_left.horizontal = border.top_right.horizontal;
1589 border.top_right.horizontal = border.top_left.horizontal;
1590 border.bottom_right.horizontal = border.top_left.horizontal;
1591 border.bottom_left.horizontal = border.top_left.horizontal;
1594 if (_gtk_css_parser_try (parser, "/", TRUE))
1596 if (!_gtk_css_parser_try_double (parser, &border.top_left.vertical))
1598 _gtk_css_parser_error (parser, "Expected a number");
1601 else if (border.top_left.vertical < 0)
1604 if (_gtk_css_parser_try_double (parser, &border.top_right.vertical))
1606 if (border.top_right.vertical < 0)
1608 if (_gtk_css_parser_try_double (parser, &border.bottom_right.vertical))
1610 if (border.bottom_right.vertical < 0)
1612 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.vertical))
1613 border.bottom_left.vertical = border.top_right.vertical;
1614 else if (border.bottom_left.vertical < 0)
1619 border.bottom_right.vertical = border.top_left.vertical;
1620 border.bottom_left.vertical = border.top_right.vertical;
1625 border.top_right.vertical = border.top_left.vertical;
1626 border.bottom_right.vertical = border.top_left.vertical;
1627 border.bottom_left.vertical = border.top_left.vertical;
1632 border.top_left.vertical = border.top_left.horizontal;
1633 border.top_right.vertical = border.top_right.horizontal;
1634 border.bottom_right.vertical = border.bottom_right.horizontal;
1635 border.bottom_left.vertical = border.bottom_left.horizontal;
1638 /* border-radius is an int property for backwards-compat reasons */
1639 g_value_unset (value);
1640 g_value_init (value, GTK_TYPE_CSS_BORDER_RADIUS);
1641 g_value_set_boxed (value, &border);
1646 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1651 border_radius_value_print (const GValue *value,
1654 GtkCssBorderRadius *border;
1656 border = g_value_get_boxed (value);
1660 g_string_append (string, "none");
1664 string_append_double (string, border->top_left.horizontal);
1665 if (border->top_left.horizontal != border->top_right.horizontal ||
1666 border->top_left.horizontal != border->bottom_right.horizontal ||
1667 border->top_left.horizontal != border->bottom_left.horizontal)
1669 g_string_append_c (string, ' ');
1670 string_append_double (string, border->top_right.horizontal);
1671 if (border->top_left.horizontal != border->bottom_right.horizontal ||
1672 border->top_right.horizontal != border->bottom_left.horizontal)
1674 g_string_append_c (string, ' ');
1675 string_append_double (string, border->bottom_right.horizontal);
1676 if (border->top_right.horizontal != border->bottom_left.horizontal)
1678 g_string_append_c (string, ' ');
1679 string_append_double (string, border->bottom_left.horizontal);
1684 if (border->top_left.horizontal != border->top_left.vertical ||
1685 border->top_right.horizontal != border->top_right.vertical ||
1686 border->bottom_right.horizontal != border->bottom_right.vertical ||
1687 border->bottom_left.horizontal != border->bottom_left.vertical)
1689 g_string_append (string, " / ");
1690 string_append_double (string, border->top_left.vertical);
1691 if (border->top_left.vertical != border->top_right.vertical ||
1692 border->top_left.vertical != border->bottom_right.vertical ||
1693 border->top_left.vertical != border->bottom_left.vertical)
1695 g_string_append_c (string, ' ');
1696 string_append_double (string, border->top_right.vertical);
1697 if (border->top_left.vertical != border->bottom_right.vertical ||
1698 border->top_right.vertical != border->bottom_left.vertical)
1700 g_string_append_c (string, ' ');
1701 string_append_double (string, border->bottom_right.vertical);
1702 if (border->top_right.vertical != border->bottom_left.vertical)
1704 g_string_append_c (string, ' ');
1705 string_append_double (string, border->bottom_left.vertical);
1714 transparent_color_value_parse (GtkCssParser *parser,
1718 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1720 GdkRGBA transparent = { 0, 0, 0, 0 };
1722 g_value_set_boxed (value, &transparent);
1727 return rgba_value_parse (parser, base, value);
1731 border_color_shorthand_value_parse (GtkCssParser *parser,
1735 GtkSymbolicColor *symbolic;
1738 array = g_ptr_array_new_with_free_func ((GDestroyNotify) gtk_symbolic_color_unref);
1742 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1744 GdkRGBA transparent = { 0, 0, 0, 0 };
1746 symbolic = gtk_symbolic_color_new_literal (&transparent);
1750 symbolic = _gtk_css_parser_read_symbolic_color (parser);
1752 if (symbolic == NULL)
1756 g_ptr_array_add (array, symbolic);
1758 while (array->len < 4 &&
1759 !_gtk_css_parser_is_eof (parser) &&
1760 !_gtk_css_parser_begins_with (parser, ';') &&
1761 !_gtk_css_parser_begins_with (parser, '}'));
1766 g_assert_not_reached ();
1769 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1772 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1775 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 1)));
1781 g_value_unset (value);
1782 g_value_init (value, G_TYPE_PTR_ARRAY);
1783 g_value_take_boxed (value, array);
1791 unpack_border (const GValue *value,
1798 GParameter *parameter = g_new0 (GParameter, 4);
1799 GtkBorder *border = g_value_get_boxed (value);
1801 parameter[0].name = top;
1802 g_value_init (¶meter[0].value, G_TYPE_INT);
1803 g_value_set_int (¶meter[0].value, border->top);
1804 parameter[1].name = left;
1805 g_value_init (¶meter[1].value, G_TYPE_INT);
1806 g_value_set_int (¶meter[1].value, border->left);
1807 parameter[2].name = bottom;
1808 g_value_init (¶meter[2].value, G_TYPE_INT);
1809 g_value_set_int (¶meter[2].value, border->bottom);
1810 parameter[3].name = right;
1811 g_value_init (¶meter[3].value, G_TYPE_INT);
1812 g_value_set_int (¶meter[3].value, border->right);
1819 pack_border (GValue *value,
1820 GtkStyleProperties *props,
1821 GtkStateFlags state,
1830 gtk_style_properties_get (props,
1843 g_value_set_boxed (value, &border);
1847 unpack_border_width (const GValue *value,
1850 return unpack_border (value, n_params,
1851 "border-top-width", "border-left-width",
1852 "border-bottom-width", "border-right-width");
1856 pack_border_width (GValue *value,
1857 GtkStyleProperties *props,
1858 GtkStateFlags state)
1860 pack_border (value, props, state,
1861 "border-top-width", "border-left-width",
1862 "border-bottom-width", "border-right-width");
1866 unpack_padding (const GValue *value,
1869 return unpack_border (value, n_params,
1870 "padding-top", "padding-left",
1871 "padding-bottom", "padding-right");
1875 pack_padding (GValue *value,
1876 GtkStyleProperties *props,
1877 GtkStateFlags state)
1879 pack_border (value, props, state,
1880 "padding-top", "padding-left",
1881 "padding-bottom", "padding-right");
1885 unpack_margin (const GValue *value,
1888 return unpack_border (value, n_params,
1889 "margin-top", "margin-left",
1890 "margin-bottom", "margin-right");
1894 pack_margin (GValue *value,
1895 GtkStyleProperties *props,
1896 GtkStateFlags state)
1898 pack_border (value, props, state,
1899 "margin-top", "margin-left",
1900 "margin-bottom", "margin-right");
1904 unpack_border_radius (const GValue *value,
1907 GParameter *parameter = g_new0 (GParameter, 4);
1908 GtkCssBorderRadius *border;
1910 if (G_VALUE_HOLDS_BOXED (value))
1911 border = g_value_get_boxed (value);
1915 parameter[0].name = "border-top-left-radius";
1916 g_value_init (¶meter[0].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1917 parameter[1].name = "border-top-right-radius";
1918 g_value_init (¶meter[1].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1919 parameter[2].name = "border-bottom-right-radius";
1920 g_value_init (¶meter[2].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1921 parameter[3].name = "border-bottom-left-radius";
1922 g_value_init (¶meter[3].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1925 g_value_set_boxed (¶meter[0].value, &border->top_left);
1926 g_value_set_boxed (¶meter[1].value, &border->top_right);
1927 g_value_set_boxed (¶meter[2].value, &border->bottom_right);
1928 g_value_set_boxed (¶meter[3].value, &border->bottom_left);
1936 pack_border_radius (GValue *value,
1937 GtkStyleProperties *props,
1938 GtkStateFlags state)
1940 GtkCssBorderCornerRadius *top_left;
1942 /* NB: We are an int property, so we have to resolve to an int here.
1943 * So we just resolve to an int. We pick one and stick to it.
1944 * Lesson learned: Don't query border-radius shorthand, query the
1945 * real properties instead. */
1946 gtk_style_properties_get (props,
1948 "border-top-left-radius", &top_left,
1952 g_value_set_int (value, top_left->horizontal);
1958 unpack_font_description (const GValue *value,
1961 GParameter *parameter = g_new0 (GParameter, 5);
1962 PangoFontDescription *description;
1966 /* For backwards compat, we only unpack values that are indeed set.
1967 * For strict CSS conformance we need to unpack all of them.
1968 * Note that we do set all of them in the parse function, so it
1969 * will not have effects when parsing CSS files. It will though
1970 * for custom style providers.
1973 description = g_value_get_boxed (value);
1977 mask = pango_font_description_get_set_fields (description);
1981 if (mask & PANGO_FONT_MASK_FAMILY)
1983 GPtrArray *strv = g_ptr_array_new ();
1985 g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
1986 g_ptr_array_add (strv, NULL);
1987 parameter[n].name = "font-family";
1988 g_value_init (¶meter[n].value, G_TYPE_STRV);
1989 g_value_take_boxed (¶meter[n].value,
1990 g_ptr_array_free (strv, FALSE));
1994 if (mask & PANGO_FONT_MASK_STYLE)
1996 parameter[n].name = "font-style";
1997 g_value_init (¶meter[n].value, PANGO_TYPE_STYLE);
1998 g_value_set_enum (¶meter[n].value,
1999 pango_font_description_get_style (description));
2003 if (mask & PANGO_FONT_MASK_VARIANT)
2005 parameter[n].name = "font-variant";
2006 g_value_init (¶meter[n].value, PANGO_TYPE_VARIANT);
2007 g_value_set_enum (¶meter[n].value,
2008 pango_font_description_get_variant (description));
2012 if (mask & PANGO_FONT_MASK_WEIGHT)
2014 parameter[n].name = "font-weight";
2015 g_value_init (¶meter[n].value, PANGO_TYPE_WEIGHT);
2016 g_value_set_enum (¶meter[n].value,
2017 pango_font_description_get_weight (description));
2021 if (mask & PANGO_FONT_MASK_SIZE)
2023 parameter[n].name = "font-size";
2024 g_value_init (¶meter[n].value, G_TYPE_DOUBLE);
2025 g_value_set_double (¶meter[n].value,
2026 (double) pango_font_description_get_size (description) / PANGO_SCALE);
2036 pack_font_description (GValue *value,
2037 GtkStyleProperties *props,
2038 GtkStateFlags state)
2040 PangoFontDescription *description;
2043 PangoVariant variant;
2047 gtk_style_properties_get (props,
2049 "font-family", &families,
2050 "font-style", &style,
2051 "font-variant", &variant,
2052 "font-weight", &weight,
2056 description = pango_font_description_new ();
2057 /* xxx: Can we set all the families here somehow? */
2059 pango_font_description_set_family (description, families[0]);
2060 pango_font_description_set_size (description, round (size * PANGO_SCALE));
2061 pango_font_description_set_style (description, style);
2062 pango_font_description_set_variant (description, variant);
2063 pango_font_description_set_weight (description, weight);
2065 g_strfreev (families);
2067 g_value_take_boxed (value, description);
2071 unpack_border_color (const GValue *value,
2074 GParameter *parameter = g_new0 (GParameter, 4);
2077 type = G_VALUE_TYPE (value);
2078 if (type == G_TYPE_PTR_ARRAY)
2079 type = GTK_TYPE_SYMBOLIC_COLOR;
2081 parameter[0].name = "border-top-color";
2082 g_value_init (¶meter[0].value, type);
2083 parameter[1].name = "border-right-color";
2084 g_value_init (¶meter[1].value, type);
2085 parameter[2].name = "border-bottom-color";
2086 g_value_init (¶meter[2].value, type);
2087 parameter[3].name = "border-left-color";
2088 g_value_init (¶meter[3].value, type);
2090 if (G_VALUE_TYPE (value) == G_TYPE_PTR_ARRAY)
2092 GPtrArray *array = g_value_get_boxed (value);
2095 for (i = 0; i < 4; i++)
2096 g_value_set_boxed (¶meter[i].value, g_ptr_array_index (array, i));
2100 /* can be RGBA or symbolic color */
2101 gpointer p = g_value_get_boxed (value);
2103 g_value_set_boxed (¶meter[0].value, p);
2104 g_value_set_boxed (¶meter[1].value, p);
2105 g_value_set_boxed (¶meter[2].value, p);
2106 g_value_set_boxed (¶meter[3].value, p);
2114 pack_border_color (GValue *value,
2115 GtkStyleProperties *props,
2116 GtkStateFlags state)
2118 /* NB: We are a color property, so we have to resolve to a color here.
2119 * So we just resolve to a color. We pick one and stick to it.
2120 * Lesson learned: Don't query border-color shorthand, query the
2121 * real properties instead. */
2122 g_value_unset (value);
2123 gtk_style_properties_get_property (props, "border-top-color", state, value);
2126 /*** UNSET FUNCS ***/
2129 unset_font_description (GtkStyleProperties *props,
2130 GtkStateFlags state)
2132 gtk_style_properties_unset_property (props, "font-family", state);
2133 gtk_style_properties_unset_property (props, "font-style", state);
2134 gtk_style_properties_unset_property (props, "font-variant", state);
2135 gtk_style_properties_unset_property (props, "font-weight", state);
2136 gtk_style_properties_unset_property (props, "font-size", state);
2140 unset_margin (GtkStyleProperties *props,
2141 GtkStateFlags state)
2143 gtk_style_properties_unset_property (props, "margin-top", state);
2144 gtk_style_properties_unset_property (props, "margin-right", state);
2145 gtk_style_properties_unset_property (props, "margin-bottom", state);
2146 gtk_style_properties_unset_property (props, "margin-left", state);
2150 unset_padding (GtkStyleProperties *props,
2151 GtkStateFlags state)
2153 gtk_style_properties_unset_property (props, "padding-top", state);
2154 gtk_style_properties_unset_property (props, "padding-right", state);
2155 gtk_style_properties_unset_property (props, "padding-bottom", state);
2156 gtk_style_properties_unset_property (props, "padding-left", state);
2160 unset_border_width (GtkStyleProperties *props,
2161 GtkStateFlags state)
2163 gtk_style_properties_unset_property (props, "border-top-width", state);
2164 gtk_style_properties_unset_property (props, "border-right-width", state);
2165 gtk_style_properties_unset_property (props, "border-bottom-width", state);
2166 gtk_style_properties_unset_property (props, "border-left-width", state);
2170 unset_border_radius (GtkStyleProperties *props,
2171 GtkStateFlags state)
2173 gtk_style_properties_unset_property (props, "border-top-right-radius", state);
2174 gtk_style_properties_unset_property (props, "border-bottom-right-radius", state);
2175 gtk_style_properties_unset_property (props, "border-bottom-left-radius", state);
2176 gtk_style_properties_unset_property (props, "border-top-left-radius", state);
2180 unset_border_color (GtkStyleProperties *props,
2181 GtkStateFlags state)
2183 gtk_style_properties_unset_property (props, "border-top-color", state);
2184 gtk_style_properties_unset_property (props, "border-right-color", state);
2185 gtk_style_properties_unset_property (props, "border-bottom-color", state);
2186 gtk_style_properties_unset_property (props, "border-left-color", state);
2190 unset_border_image (GtkStyleProperties *props,
2191 GtkStateFlags state)
2193 gtk_style_properties_unset_property (props, "border-image-source", state);
2194 gtk_style_properties_unset_property (props, "border-image-slice", state);
2195 gtk_style_properties_unset_property (props, "border-image-repeat", state);
2196 gtk_style_properties_unset_property (props, "border-image-width", state);
2199 /*** default values ***/
2202 border_image_width_default_value (GtkStyleProperties *props,
2203 GtkStateFlags state,
2209 background_color_default_value (GtkStyleProperties *props,
2210 GtkStateFlags state,
2213 GdkRGBA transparent_black = { 0, 0, 0, 0 };
2215 g_value_set_boxed (value, &transparent_black);
2219 border_color_default_value (GtkStyleProperties *props,
2220 GtkStateFlags state,
2223 g_value_unset (value);
2224 gtk_style_properties_get_property (props, "color", state, value);
2230 css_string_funcs_init (void)
2232 if (G_LIKELY (parse_funcs != NULL))
2235 parse_funcs = g_hash_table_new (NULL, NULL);
2236 print_funcs = g_hash_table_new (NULL, NULL);
2238 register_conversion_function (GDK_TYPE_RGBA,
2241 register_conversion_function (GDK_TYPE_COLOR,
2244 register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
2245 symbolic_color_value_parse,
2246 symbolic_color_value_print);
2247 register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
2248 font_description_value_parse,
2249 font_description_value_print);
2250 register_conversion_function (G_TYPE_BOOLEAN,
2251 boolean_value_parse,
2252 boolean_value_print);
2253 register_conversion_function (G_TYPE_INT,
2256 register_conversion_function (G_TYPE_UINT,
2259 register_conversion_function (G_TYPE_DOUBLE,
2261 double_value_print);
2262 register_conversion_function (G_TYPE_FLOAT,
2265 register_conversion_function (G_TYPE_STRING,
2267 string_value_print);
2268 register_conversion_function (GTK_TYPE_THEMING_ENGINE,
2269 theming_engine_value_parse,
2270 theming_engine_value_print);
2271 register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
2272 animation_description_value_parse,
2273 animation_description_value_print);
2274 register_conversion_function (GTK_TYPE_BORDER,
2276 border_value_print);
2277 register_conversion_function (GTK_TYPE_GRADIENT,
2278 gradient_value_parse,
2279 gradient_value_print);
2280 register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
2281 pattern_value_parse,
2282 pattern_value_print);
2283 register_conversion_function (GTK_TYPE_BORDER_IMAGE,
2284 border_image_value_parse,
2286 register_conversion_function (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
2287 border_image_repeat_value_parse,
2288 border_image_repeat_value_print);
2289 register_conversion_function (GTK_TYPE_SHADOW,
2291 shadow_value_print);
2292 register_conversion_function (G_TYPE_ENUM,
2295 register_conversion_function (G_TYPE_FLAGS,
2298 register_conversion_function (GTK_TYPE_CSS_BACKGROUND_REPEAT,
2299 background_repeat_value_parse,
2300 background_repeat_value_print);
2304 _gtk_style_property_parse_value (const GtkStyleProperty *property,
2306 GtkCssParser *parser,
2309 GtkStyleParseFunc func;
2311 g_return_val_if_fail (value != NULL, FALSE);
2312 g_return_val_if_fail (parser != NULL, FALSE);
2314 css_string_funcs_init ();
2318 if (_gtk_css_parser_try (parser, "none", TRUE))
2320 /* Insert the default value, so it has an opportunity
2321 * to override other style providers when merged
2323 g_param_value_set_default (property->pspec, value);
2326 else if (property->property_parse_func)
2328 GError *error = NULL;
2332 value_str = _gtk_css_parser_read_value (parser);
2333 if (value_str == NULL)
2336 success = (*property->property_parse_func) (value_str, value, &error);
2343 func = property->parse_func;
2349 func = g_hash_table_lookup (parse_funcs,
2350 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2352 func = g_hash_table_lookup (parse_funcs,
2353 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2357 _gtk_css_parser_error (parser,
2358 "Cannot convert to type '%s'",
2359 g_type_name (G_VALUE_TYPE (value)));
2363 return (*func) (parser, base, value);
2367 _gtk_style_property_print_value (const GtkStyleProperty *property,
2368 const GValue *value,
2371 GtkStylePrintFunc func;
2373 css_string_funcs_init ();
2376 func = property->print_func;
2381 func = g_hash_table_lookup (print_funcs,
2382 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2384 func = g_hash_table_lookup (print_funcs,
2385 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2389 char *s = g_strdup_value_contents (value);
2390 g_string_append (string, s);
2395 func (value, string);
2399 _gtk_style_property_default_value (const GtkStyleProperty *property,
2400 GtkStyleProperties *properties,
2401 GtkStateFlags state,
2404 if (property->default_value_func)
2405 property->default_value_func (properties, state, value);
2406 else if (property->pspec->value_type == GTK_TYPE_THEMING_ENGINE)
2407 g_value_set_object (value, gtk_theming_engine_load (NULL));
2408 else if (property->pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
2409 g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
2410 else if (property->pspec->value_type == GDK_TYPE_RGBA)
2413 gdk_rgba_parse (&color, "pink");
2414 g_value_set_boxed (value, &color);
2416 else if (property->pspec->value_type == GTK_TYPE_BORDER)
2418 g_value_take_boxed (value, gtk_border_new ());
2421 g_param_value_set_default (property->pspec, value);
2425 _gtk_style_property_is_inherit (const GtkStyleProperty *property)
2427 g_return_val_if_fail (property != NULL, FALSE);
2429 return property->flags & GTK_STYLE_PROPERTY_INHERIT ? TRUE : FALSE;
2433 resolve_color (GtkStyleProperties *props,
2438 /* Resolve symbolic color to GdkRGBA */
2439 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
2442 /* Store it back, this is where GdkRGBA caching happens */
2443 g_value_unset (value);
2444 g_value_init (value, GDK_TYPE_RGBA);
2445 g_value_set_boxed (value, &color);
2451 resolve_color_rgb (GtkStyleProperties *props,
2454 GdkColor color = { 0 };
2457 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
2460 color.red = rgba.red * 65535. + 0.5;
2461 color.green = rgba.green * 65535. + 0.5;
2462 color.blue = rgba.blue * 65535. + 0.5;
2464 g_value_unset (value);
2465 g_value_init (value, GDK_TYPE_COLOR);
2466 g_value_set_boxed (value, &color);
2472 resolve_gradient (GtkStyleProperties *props,
2475 cairo_pattern_t *gradient;
2477 if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
2480 /* Store it back, this is where cairo_pattern_t caching happens */
2481 g_value_unset (value);
2482 g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
2483 g_value_take_boxed (value, gradient);
2489 resolve_shadow (GtkStyleProperties *props,
2492 GtkShadow *resolved, *base;
2494 base = g_value_get_boxed (value);
2499 if (_gtk_shadow_get_resolved (base))
2502 resolved = _gtk_shadow_resolve (base, props);
2503 if (resolved == NULL)
2506 g_value_take_boxed (value, resolved);
2512 _gtk_style_property_resolve (const GtkStyleProperty *property,
2513 GtkStyleProperties *props,
2514 GtkStateFlags state,
2517 if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
2519 if (property->pspec->value_type == GDK_TYPE_RGBA)
2521 if (resolve_color (props, val))
2524 else if (property->pspec->value_type == GDK_TYPE_COLOR)
2526 if (resolve_color_rgb (props, val))
2530 g_value_unset (val);
2531 g_value_init (val, property->pspec->value_type);
2532 _gtk_style_property_default_value (property, props, state, val);
2534 else if (G_VALUE_TYPE (val) == GDK_TYPE_RGBA)
2536 if (g_value_get_boxed (val) == NULL)
2537 _gtk_style_property_default_value (property, props, state, val);
2539 else if (G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
2541 g_return_if_fail (property->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN);
2543 if (!resolve_gradient (props, val))
2545 g_value_unset (val);
2546 g_value_init (val, CAIRO_GOBJECT_TYPE_PATTERN);
2547 _gtk_style_property_default_value (property, props, state, val);
2550 else if (G_VALUE_TYPE (val) == GTK_TYPE_SHADOW)
2552 if (!resolve_shadow (props, val))
2553 _gtk_style_property_default_value (property, props, state, val);
2558 _gtk_style_property_is_shorthand (const GtkStyleProperty *property)
2560 g_return_val_if_fail (property != NULL, FALSE);
2562 return property->pack_func != NULL;
2566 _gtk_style_property_unpack (const GtkStyleProperty *property,
2567 const GValue *value,
2570 g_return_val_if_fail (property != NULL, NULL);
2571 g_return_val_if_fail (property->unpack_func != NULL, NULL);
2572 g_return_val_if_fail (value != NULL, NULL);
2573 g_return_val_if_fail (n_params != NULL, NULL);
2575 return property->unpack_func (value, n_params);
2579 _gtk_style_property_pack (const GtkStyleProperty *property,
2580 GtkStyleProperties *props,
2581 GtkStateFlags state,
2584 g_return_if_fail (property != NULL);
2585 g_return_if_fail (property->pack_func != NULL);
2586 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
2587 g_return_if_fail (G_IS_VALUE (value));
2589 property->pack_func (value, props, state);
2593 gtk_style_property_init (void)
2595 if (G_LIKELY (properties))
2598 /* stuff is never freed, so no need for free functions */
2599 properties = g_hash_table_new (g_str_hash, g_str_equal);
2601 /* note that gtk_style_properties_register_property() calls this function,
2602 * so make sure we're sanely inited to avoid infloops */
2604 _gtk_style_property_register (g_param_spec_boxed ("color",
2608 GTK_STYLE_PROPERTY_INHERIT,
2616 _gtk_style_property_register (g_param_spec_boxed ("background-color",
2624 transparent_color_value_parse,
2626 background_color_default_value,
2629 _gtk_style_property_register (g_param_spec_boxed ("font-family",
2633 GTK_STYLE_PROPERTY_INHERIT,
2638 font_family_value_print,
2641 _gtk_style_property_register (g_param_spec_enum ("font-style",
2645 PANGO_STYLE_NORMAL, 0),
2646 GTK_STYLE_PROPERTY_INHERIT,
2654 _gtk_style_property_register (g_param_spec_enum ("font-variant",
2658 PANGO_VARIANT_NORMAL, 0),
2659 GTK_STYLE_PROPERTY_INHERIT,
2667 /* xxx: need to parse this properly, ie parse the numbers */
2668 _gtk_style_property_register (g_param_spec_enum ("font-weight",
2672 PANGO_WEIGHT_NORMAL, 0),
2673 GTK_STYLE_PROPERTY_INHERIT,
2681 _gtk_style_property_register (g_param_spec_double ("font-size",
2684 0, G_MAXDOUBLE, 0, 0),
2685 GTK_STYLE_PROPERTY_INHERIT,
2693 _gtk_style_property_register (g_param_spec_boxed ("font",
2696 PANGO_TYPE_FONT_DESCRIPTION, 0),
2697 GTK_STYLE_PROPERTY_INHERIT,
2699 unpack_font_description,
2700 pack_font_description,
2701 font_description_value_parse,
2702 font_description_value_print,
2704 unset_font_description);
2706 _gtk_style_property_register (g_param_spec_boxed ("text-shadow",
2709 GTK_TYPE_SHADOW, 0),
2710 GTK_STYLE_PROPERTY_INHERIT,
2719 _gtk_style_property_register (g_param_spec_boxed ("icon-shadow",
2722 GTK_TYPE_SHADOW, 0),
2723 GTK_STYLE_PROPERTY_INHERIT,
2732 gtk_style_properties_register_property (NULL,
2733 g_param_spec_boxed ("box-shadow",
2736 GTK_TYPE_SHADOW, 0));
2737 gtk_style_properties_register_property (NULL,
2738 g_param_spec_int ("margin-top",
2741 0, G_MAXINT, 0, 0));
2742 gtk_style_properties_register_property (NULL,
2743 g_param_spec_int ("margin-left",
2746 0, G_MAXINT, 0, 0));
2747 gtk_style_properties_register_property (NULL,
2748 g_param_spec_int ("margin-bottom",
2751 0, G_MAXINT, 0, 0));
2752 gtk_style_properties_register_property (NULL,
2753 g_param_spec_int ("margin-right",
2756 0, G_MAXINT, 0, 0));
2757 _gtk_style_property_register (g_param_spec_boxed ("margin",
2760 GTK_TYPE_BORDER, 0),
2769 gtk_style_properties_register_property (NULL,
2770 g_param_spec_int ("padding-top",
2773 0, G_MAXINT, 0, 0));
2774 gtk_style_properties_register_property (NULL,
2775 g_param_spec_int ("padding-left",
2778 0, G_MAXINT, 0, 0));
2779 gtk_style_properties_register_property (NULL,
2780 g_param_spec_int ("padding-bottom",
2782 "Padding at bottom",
2783 0, G_MAXINT, 0, 0));
2784 gtk_style_properties_register_property (NULL,
2785 g_param_spec_int ("padding-right",
2788 0, G_MAXINT, 0, 0));
2789 _gtk_style_property_register (g_param_spec_boxed ("padding",
2792 GTK_TYPE_BORDER, 0),
2801 gtk_style_properties_register_property (NULL,
2802 g_param_spec_int ("border-top-width",
2804 "Border width at top",
2805 0, G_MAXINT, 0, 0));
2806 gtk_style_properties_register_property (NULL,
2807 g_param_spec_int ("border-left-width",
2808 "border left width",
2809 "Border width at left",
2810 0, G_MAXINT, 0, 0));
2811 gtk_style_properties_register_property (NULL,
2812 g_param_spec_int ("border-bottom-width",
2813 "border bottom width",
2814 "Border width at bottom",
2815 0, G_MAXINT, 0, 0));
2816 gtk_style_properties_register_property (NULL,
2817 g_param_spec_int ("border-right-width",
2818 "border right width",
2819 "Border width at right",
2820 0, G_MAXINT, 0, 0));
2821 _gtk_style_property_register (g_param_spec_boxed ("border-width",
2823 "Border width, in pixels",
2824 GTK_TYPE_BORDER, 0),
2827 unpack_border_width,
2832 unset_border_width);
2834 _gtk_style_property_register (g_param_spec_boxed ("border-top-left-radius",
2835 "Border top left radius",
2836 "Border radius of top left corner, in pixels",
2837 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2842 border_corner_radius_value_parse,
2843 border_corner_radius_value_print,
2846 _gtk_style_property_register (g_param_spec_boxed ("border-top-right-radius",
2847 "Border top right radius",
2848 "Border radius of top right corner, in pixels",
2849 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2854 border_corner_radius_value_parse,
2855 border_corner_radius_value_print,
2858 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-right-radius",
2859 "Border bottom right radius",
2860 "Border radius of bottom right corner, in pixels",
2861 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2866 border_corner_radius_value_parse,
2867 border_corner_radius_value_print,
2870 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-left-radius",
2871 "Border bottom left radius",
2872 "Border radius of bottom left corner, in pixels",
2873 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2878 border_corner_radius_value_parse,
2879 border_corner_radius_value_print,
2882 _gtk_style_property_register (g_param_spec_int ("border-radius",
2884 "Border radius, in pixels",
2888 unpack_border_radius,
2890 border_radius_value_parse,
2891 border_radius_value_print,
2893 unset_border_radius);
2895 gtk_style_properties_register_property (NULL,
2896 g_param_spec_enum ("border-style",
2899 GTK_TYPE_BORDER_STYLE,
2900 GTK_BORDER_STYLE_NONE, 0));
2901 _gtk_style_property_register (g_param_spec_boxed ("border-top-color",
2909 transparent_color_value_parse,
2911 border_color_default_value,
2913 _gtk_style_property_register (g_param_spec_boxed ("border-right-color",
2914 "Border right color",
2915 "Border right color",
2921 transparent_color_value_parse,
2923 border_color_default_value,
2925 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-color",
2926 "Border bottom color",
2927 "Border bottom color",
2933 transparent_color_value_parse,
2935 border_color_default_value,
2937 _gtk_style_property_register (g_param_spec_boxed ("border-left-color",
2938 "Border left color",
2939 "Border left color",
2945 transparent_color_value_parse,
2947 border_color_default_value,
2949 _gtk_style_property_register (g_param_spec_boxed ("border-color",
2955 unpack_border_color,
2957 border_color_shorthand_value_parse,
2960 unset_border_color);
2962 gtk_style_properties_register_property (NULL,
2963 g_param_spec_boxed ("background-image",
2966 CAIRO_GOBJECT_TYPE_PATTERN, 0));
2967 gtk_style_properties_register_property (NULL,
2968 g_param_spec_boxed ("background-repeat",
2969 "Background repeat",
2970 "Background repeat",
2971 GTK_TYPE_CSS_BACKGROUND_REPEAT, 0));
2973 gtk_style_properties_register_property (NULL,
2974 g_param_spec_boxed ("border-image-source",
2975 "Border image source",
2976 "Border image source",
2977 CAIRO_GOBJECT_TYPE_PATTERN, 0));
2978 gtk_style_properties_register_property (NULL,
2979 g_param_spec_boxed ("border-image-repeat",
2980 "Border image repeat",
2981 "Border image repeat",
2982 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
2983 gtk_style_properties_register_property (NULL,
2984 g_param_spec_boxed ("border-image-slice",
2985 "Border image slice",
2986 "Border image slice",
2987 GTK_TYPE_BORDER, 0));
2988 _gtk_style_property_register (g_param_spec_boxed ("border-image-width",
2989 "Border image width",
2990 "Border image width",
2991 GTK_TYPE_BORDER, 0),
2998 border_image_width_default_value,
3000 _gtk_style_property_register (g_param_spec_boxed ("border-image",
3003 GTK_TYPE_BORDER_IMAGE, 0),
3006 _gtk_border_image_unpack,
3007 _gtk_border_image_pack,
3011 unset_border_image);
3012 gtk_style_properties_register_property (NULL,
3013 g_param_spec_object ("engine",
3016 GTK_TYPE_THEMING_ENGINE, 0));
3017 gtk_style_properties_register_property (NULL,
3018 g_param_spec_boxed ("transition",
3019 "Transition animation description",
3020 "Transition animation description",
3021 GTK_TYPE_ANIMATION_DESCRIPTION, 0));
3023 /* Private property holding the binding sets */
3024 _gtk_style_property_register (g_param_spec_boxed ("gtk-key-bindings",
3027 G_TYPE_PTR_ARRAY, 0),
3032 bindings_value_parse,
3033 bindings_value_print,
3038 const GtkStyleProperty *
3039 _gtk_style_property_lookup (const char *name)
3041 gtk_style_property_init ();
3043 return g_hash_table_lookup (properties, name);
3047 _gtk_style_property_register (GParamSpec *pspec,
3048 GtkStylePropertyFlags flags,
3049 GtkStylePropertyParser property_parse_func,
3050 GtkStyleUnpackFunc unpack_func,
3051 GtkStylePackFunc pack_func,
3052 GtkStyleParseFunc parse_func,
3053 GtkStylePrintFunc print_func,
3054 GtkStyleDefaultValueFunc default_value_func,
3055 GtkStyleUnsetFunc unset_func)
3057 const GtkStyleProperty *existing;
3058 GtkStyleProperty *node;
3060 g_return_if_fail ((pack_func == NULL) == (unpack_func == NULL));
3062 gtk_style_property_init ();
3064 existing = _gtk_style_property_lookup (pspec->name);
3065 if (existing != NULL)
3067 g_warning ("Property \"%s\" was already registered with type %s",
3068 pspec->name, g_type_name (existing->pspec->value_type));
3072 node = g_slice_new0 (GtkStyleProperty);
3073 node->flags = flags;
3074 node->pspec = pspec;
3075 node->property_parse_func = property_parse_func;
3076 node->pack_func = pack_func;
3077 node->unpack_func = unpack_func;
3078 node->parse_func = parse_func;
3079 node->print_func = print_func;
3080 node->default_value_func = default_value_func;
3081 node->unset_func = unset_func;
3083 /* pspec owns name */
3084 g_hash_table_insert (properties, (gchar *)pspec->name, node);