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 static GHashTable *parse_funcs = NULL;
46 static GHashTable *print_funcs = NULL;
47 static GHashTable *properties = NULL;
50 register_conversion_function (GType type,
51 GtkStyleParseFunc parse,
52 GtkStylePrintFunc print)
55 g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse);
57 g_hash_table_insert (print_funcs, GSIZE_TO_POINTER (type), print);
61 string_append_double (GString *string,
64 char buf[G_ASCII_DTOSTR_BUF_SIZE];
66 g_ascii_dtostr (buf, sizeof (buf), d);
67 g_string_append (string, buf);
71 string_append_string (GString *str,
76 g_string_append_c (str, '"');
79 len = strcspn (string, "\"\n\r\f");
80 g_string_append (str, string);
87 g_string_append (str, "\\A ");
90 g_string_append (str, "\\D ");
93 g_string_append (str, "\\C ");
96 g_string_append (str, "\\\"");
99 g_assert_not_reached ();
104 g_string_append_c (str, '"');
107 /*** IMPLEMENTATIONS ***/
110 rgba_value_parse (GtkCssParser *parser,
114 GtkSymbolicColor *symbolic;
117 symbolic = _gtk_css_parser_read_symbolic_color (parser);
118 if (symbolic == NULL)
121 if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
123 g_value_set_boxed (value, &rgba);
124 gtk_symbolic_color_unref (symbolic);
128 g_value_unset (value);
129 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
130 g_value_take_boxed (value, symbolic);
137 rgba_value_print (const GValue *value,
140 const GdkRGBA *rgba = g_value_get_boxed (value);
143 g_string_append (string, "none");
146 char *s = gdk_rgba_to_string (rgba);
147 g_string_append (string, s);
153 color_value_parse (GtkCssParser *parser,
157 GtkSymbolicColor *symbolic;
160 symbolic = _gtk_css_parser_read_symbolic_color (parser);
161 if (symbolic == NULL)
164 if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
168 color.red = rgba.red * 65535. + 0.5;
169 color.green = rgba.green * 65535. + 0.5;
170 color.blue = rgba.blue * 65535. + 0.5;
172 g_value_set_boxed (value, &color);
176 g_value_unset (value);
177 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
178 g_value_take_boxed (value, symbolic);
185 color_value_print (const GValue *value,
188 const GdkColor *color = g_value_get_boxed (value);
191 g_string_append (string, "none");
194 char *s = gdk_color_to_string (color);
195 g_string_append (string, s);
201 symbolic_color_value_parse (GtkCssParser *parser,
205 GtkSymbolicColor *symbolic;
207 symbolic = _gtk_css_parser_read_symbolic_color (parser);
208 if (symbolic == NULL)
211 g_value_take_boxed (value, symbolic);
216 symbolic_color_value_print (const GValue *value,
219 GtkSymbolicColor *symbolic = g_value_get_boxed (value);
221 if (symbolic == NULL)
222 g_string_append (string, "none");
225 char *s = gtk_symbolic_color_to_string (symbolic);
226 g_string_append (string, s);
232 font_family_parse (GtkCssParser *parser,
239 /* We don't special case generic families. Pango should do
242 names = g_ptr_array_new ();
245 name = _gtk_css_parser_try_ident (parser, TRUE);
248 GString *string = g_string_new (name);
250 while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
252 g_string_append_c (string, ' ');
253 g_string_append (string, name);
256 name = g_string_free (string, FALSE);
260 name = _gtk_css_parser_read_string (parser);
263 g_ptr_array_free (names, TRUE);
268 g_ptr_array_add (names, name);
269 } while (_gtk_css_parser_try (parser, ",", TRUE));
271 /* NULL-terminate array */
272 g_ptr_array_add (names, NULL);
273 g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
278 font_family_value_print (const GValue *value,
281 const char **names = g_value_get_boxed (value);
283 if (names == NULL || *names == NULL)
285 g_string_append (string, "none");
289 string_append_string (string, *names);
293 g_string_append (string, ", ");
294 string_append_string (string, *names);
300 font_description_value_parse (GtkCssParser *parser,
304 PangoFontDescription *font_desc;
308 str = _gtk_css_parser_read_value (parser);
312 font_desc = pango_font_description_from_string (str);
313 mask = pango_font_description_get_set_fields (font_desc);
314 /* These values are not really correct,
315 * but the fields must be set, so we set them to something */
316 if ((mask & PANGO_FONT_MASK_FAMILY) == 0)
317 pango_font_description_set_family_static (font_desc, "Sans");
318 if ((mask & PANGO_FONT_MASK_SIZE) == 0)
319 pango_font_description_set_size (font_desc, 10 * PANGO_SCALE);
321 g_value_take_boxed (value, font_desc);
326 font_description_value_print (const GValue *value,
329 const PangoFontDescription *desc = g_value_get_boxed (value);
332 g_string_append (string, "none");
335 char *s = pango_font_description_to_string (desc);
336 g_string_append (string, s);
342 boolean_value_parse (GtkCssParser *parser,
346 if (_gtk_css_parser_try (parser, "true", TRUE) ||
347 _gtk_css_parser_try (parser, "1", TRUE))
349 g_value_set_boolean (value, TRUE);
352 else if (_gtk_css_parser_try (parser, "false", TRUE) ||
353 _gtk_css_parser_try (parser, "0", TRUE))
355 g_value_set_boolean (value, FALSE);
360 _gtk_css_parser_error (parser, "Expected a boolean value");
366 boolean_value_print (const GValue *value,
369 if (g_value_get_boolean (value))
370 g_string_append (string, "true");
372 g_string_append (string, "false");
376 int_value_parse (GtkCssParser *parser,
382 if (!_gtk_css_parser_try_int (parser, &i))
384 _gtk_css_parser_error (parser, "Expected a valid integer value");
388 g_value_set_int (value, i);
393 int_value_print (const GValue *value,
396 g_string_append_printf (string, "%d", g_value_get_int (value));
400 uint_value_parse (GtkCssParser *parser,
406 if (!_gtk_css_parser_try_uint (parser, &u))
408 _gtk_css_parser_error (parser, "Expected a valid unsigned value");
412 g_value_set_uint (value, u);
417 uint_value_print (const GValue *value,
420 g_string_append_printf (string, "%u", g_value_get_uint (value));
424 double_value_parse (GtkCssParser *parser,
430 if (!_gtk_css_parser_try_double (parser, &d))
432 _gtk_css_parser_error (parser, "Expected a number");
436 g_value_set_double (value, d);
441 double_value_print (const GValue *value,
444 string_append_double (string, g_value_get_double (value));
448 float_value_parse (GtkCssParser *parser,
454 if (!_gtk_css_parser_try_double (parser, &d))
456 _gtk_css_parser_error (parser, "Expected a number");
460 g_value_set_float (value, d);
465 float_value_print (const GValue *value,
468 string_append_double (string, g_value_get_float (value));
472 string_value_parse (GtkCssParser *parser,
476 char *str = _gtk_css_parser_read_string (parser);
481 g_value_take_string (value, str);
486 string_value_print (const GValue *value,
489 string_append_string (str, g_value_get_string (value));
493 theming_engine_value_parse (GtkCssParser *parser,
497 GtkThemingEngine *engine;
500 str = _gtk_css_parser_try_ident (parser, TRUE);
503 _gtk_css_parser_error (parser, "Expected a valid theme name");
507 engine = gtk_theming_engine_load (str);
510 _gtk_css_parser_error (parser, "Themeing engine '%s' not found", str);
515 g_value_set_object (value, engine);
521 theming_engine_value_print (const GValue *value,
524 GtkThemingEngine *engine;
527 engine = g_value_get_object (value);
529 g_string_append (string, "none");
532 /* XXX: gtk_theming_engine_get_name()? */
533 g_object_get (engine, "name", &name, NULL);
534 g_string_append (string, name ? name : "none");
540 animation_description_value_parse (GtkCssParser *parser,
544 GtkAnimationDescription *desc;
547 str = _gtk_css_parser_read_value (parser);
551 desc = _gtk_animation_description_from_string (str);
556 _gtk_css_parser_error (parser, "Invalid animation description");
560 g_value_take_boxed (value, desc);
565 animation_description_value_print (const GValue *value,
568 GtkAnimationDescription *desc = g_value_get_boxed (value);
571 g_string_append (string, "none");
573 _gtk_animation_description_print (desc, string);
577 border_value_parse (GtkCssParser *parser,
581 GtkBorder border = { 0, };
584 for (i = 0; i < G_N_ELEMENTS (numbers); i++)
586 if (!_gtk_css_parser_try_uint (parser, &numbers[i]))
589 /* XXX: shouldn't allow spaces here? */
590 _gtk_css_parser_try (parser, "px", TRUE);
595 _gtk_css_parser_error (parser, "Expected valid border");
599 border.top = numbers[0];
601 border.right = numbers[1];
603 border.right = border.top;
605 border.bottom = numbers[2];
607 border.bottom = border.top;
609 border.left = numbers[3];
611 border.left = border.right;
613 g_value_set_boxed (value, &border);
618 border_value_print (const GValue *value, GString *string)
620 const GtkBorder *border = g_value_get_boxed (value);
623 g_string_append (string, "none");
624 else if (border->left != border->right)
625 g_string_append_printf (string, "%d %d %d %d", border->top, border->right, border->bottom, border->left);
626 else if (border->top != border->bottom)
627 g_string_append_printf (string, "%d %d %d", border->top, border->right, border->bottom);
628 else if (border->top != border->left)
629 g_string_append_printf (string, "%d %d", border->top, border->right);
631 g_string_append_printf (string, "%d", border->top);
635 gradient_value_parse (GtkCssParser *parser,
639 GtkGradient *gradient;
640 cairo_pattern_type_t type;
644 if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
646 _gtk_css_parser_error (parser,
647 "Expected '-gtk-gradient'");
651 if (!_gtk_css_parser_try (parser, "(", TRUE))
653 _gtk_css_parser_error (parser,
654 "Expected '(' after '-gtk-gradient'");
658 /* Parse gradient type */
659 if (_gtk_css_parser_try (parser, "linear", TRUE))
660 type = CAIRO_PATTERN_TYPE_LINEAR;
661 else if (_gtk_css_parser_try (parser, "radial", TRUE))
662 type = CAIRO_PATTERN_TYPE_RADIAL;
665 _gtk_css_parser_error (parser,
666 "Gradient type must be 'radial' or 'linear'");
670 /* Parse start/stop position parameters */
671 for (i = 0; i < 2; i++)
673 if (! _gtk_css_parser_try (parser, ",", TRUE))
675 _gtk_css_parser_error (parser,
680 if (_gtk_css_parser_try (parser, "left", TRUE))
682 else if (_gtk_css_parser_try (parser, "right", TRUE))
684 else if (_gtk_css_parser_try (parser, "center", TRUE))
686 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3]))
688 _gtk_css_parser_error (parser,
689 "Expected a valid X coordinate");
693 if (_gtk_css_parser_try (parser, "top", TRUE))
694 coords[i * 3 + 1] = 0;
695 else if (_gtk_css_parser_try (parser, "bottom", TRUE))
696 coords[i * 3 + 1] = 1;
697 else if (_gtk_css_parser_try (parser, "center", TRUE))
698 coords[i * 3 + 1] = 0.5;
699 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3 + 1]))
701 _gtk_css_parser_error (parser,
702 "Expected a valid Y coordinate");
706 if (type == CAIRO_PATTERN_TYPE_RADIAL)
709 if (! _gtk_css_parser_try (parser, ",", TRUE))
711 _gtk_css_parser_error (parser,
716 if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
718 _gtk_css_parser_error (parser,
719 "Expected a numer for the radius");
725 if (type == CAIRO_PATTERN_TYPE_LINEAR)
726 gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
728 gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
729 coords[3], coords[4], coords[5]);
731 while (_gtk_css_parser_try (parser, ",", TRUE))
733 GtkSymbolicColor *color;
736 if (_gtk_css_parser_try (parser, "from", TRUE))
740 if (!_gtk_css_parser_try (parser, "(", TRUE))
742 gtk_gradient_unref (gradient);
743 _gtk_css_parser_error (parser,
749 else if (_gtk_css_parser_try (parser, "to", TRUE))
753 if (!_gtk_css_parser_try (parser, "(", TRUE))
755 gtk_gradient_unref (gradient);
756 _gtk_css_parser_error (parser,
762 else if (_gtk_css_parser_try (parser, "color-stop", TRUE))
764 if (!_gtk_css_parser_try (parser, "(", TRUE))
766 gtk_gradient_unref (gradient);
767 _gtk_css_parser_error (parser,
772 if (!_gtk_css_parser_try_double (parser, &position))
774 gtk_gradient_unref (gradient);
775 _gtk_css_parser_error (parser,
776 "Expected a valid number");
780 if (!_gtk_css_parser_try (parser, ",", TRUE))
782 gtk_gradient_unref (gradient);
783 _gtk_css_parser_error (parser,
790 gtk_gradient_unref (gradient);
791 _gtk_css_parser_error (parser,
792 "Not a valid color-stop definition");
796 color = _gtk_css_parser_read_symbolic_color (parser);
799 gtk_gradient_unref (gradient);
803 gtk_gradient_add_color_stop (gradient, position, color);
804 gtk_symbolic_color_unref (color);
806 if (!_gtk_css_parser_try (parser, ")", TRUE))
808 gtk_gradient_unref (gradient);
809 _gtk_css_parser_error (parser,
815 if (!_gtk_css_parser_try (parser, ")", TRUE))
817 gtk_gradient_unref (gradient);
818 _gtk_css_parser_error (parser,
823 g_value_take_boxed (value, gradient);
828 gradient_value_print (const GValue *value,
831 GtkGradient *gradient = g_value_get_boxed (value);
833 if (gradient == NULL)
834 g_string_append (string, "none");
837 char *s = gtk_gradient_to_string (gradient);
838 g_string_append (string, s);
844 gtk_css_parse_url (GtkCssParser *parser,
850 if (_gtk_css_parser_try (parser, "url", FALSE))
852 if (!_gtk_css_parser_try (parser, "(", TRUE))
854 _gtk_css_parser_skip_whitespace (parser);
855 if (_gtk_css_parser_try (parser, "(", TRUE))
859 error = g_error_new_literal (GTK_CSS_PROVIDER_ERROR,
860 GTK_CSS_PROVIDER_ERROR_DEPRECATED,
861 "Whitespace between 'url' and '(' is not allowed");
863 _gtk_css_parser_take_error (parser, error);
867 _gtk_css_parser_error (parser, "Expected '(' after 'url'");
872 path = _gtk_css_parser_read_string (parser);
876 if (!_gtk_css_parser_try (parser, ")", TRUE))
878 _gtk_css_parser_error (parser, "No closing ')' found for 'url'");
885 path = _gtk_css_parser_try_name (parser, TRUE);
888 _gtk_css_parser_error (parser, "Not a valid url");
893 file = g_file_resolve_relative_path (base, path);
900 pattern_value_parse (GtkCssParser *parser,
904 if (_gtk_css_parser_begins_with (parser, '-'))
906 g_value_unset (value);
907 g_value_init (value, GTK_TYPE_GRADIENT);
908 return gradient_value_parse (parser, base, value);
912 GError *error = NULL;
916 cairo_surface_t *surface;
917 cairo_pattern_t *pattern;
919 cairo_matrix_t matrix;
921 file = gtk_css_parse_url (parser, base);
925 path = g_file_get_path (file);
926 g_object_unref (file);
928 pixbuf = gdk_pixbuf_new_from_file (path, &error);
932 _gtk_css_parser_take_error (parser, error);
936 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
937 gdk_pixbuf_get_width (pixbuf),
938 gdk_pixbuf_get_height (pixbuf));
939 cr = cairo_create (surface);
940 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
942 pattern = cairo_pattern_create_for_surface (surface);
944 cairo_matrix_init_scale (&matrix,
945 gdk_pixbuf_get_width (pixbuf),
946 gdk_pixbuf_get_height (pixbuf));
947 cairo_pattern_set_matrix (pattern, &matrix);
949 cairo_surface_destroy (surface);
951 g_object_unref (pixbuf);
953 g_value_take_boxed (value, pattern);
959 static cairo_status_t
960 surface_write (void *closure,
961 const unsigned char *data,
964 g_byte_array_append (closure, data, length);
966 return CAIRO_STATUS_SUCCESS;
970 surface_print (cairo_surface_t *surface,
973 #if CAIRO_HAS_PNG_FUNCTIONS
977 array = g_byte_array_new ();
978 cairo_surface_write_to_png_stream (surface, surface_write, array);
979 base64 = g_base64_encode (array->data, array->len);
980 g_byte_array_free (array, TRUE);
982 g_string_append (string, "url(\"data:image/png;base64,");
983 g_string_append (string, base64);
984 g_string_append (string, "\")");
988 g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
993 pattern_value_print (const GValue *value,
996 cairo_pattern_t *pattern;
997 cairo_surface_t *surface;
999 pattern = g_value_get_boxed (value);
1001 if (pattern == NULL)
1003 g_string_append (string, "none");
1007 switch (cairo_pattern_get_type (pattern))
1009 case CAIRO_PATTERN_TYPE_SURFACE:
1010 if (cairo_pattern_get_surface (pattern, &surface) != CAIRO_STATUS_SUCCESS)
1012 g_assert_not_reached ();
1014 surface_print (surface, string);
1016 case CAIRO_PATTERN_TYPE_SOLID:
1017 case CAIRO_PATTERN_TYPE_LINEAR:
1018 case CAIRO_PATTERN_TYPE_RADIAL:
1020 g_assert_not_reached ();
1026 shadow_value_parse (GtkCssParser *parser,
1030 gboolean have_inset, have_color, have_lengths;
1031 gdouble hoffset, voffset, blur, spread;
1032 GtkSymbolicColor *color;
1036 shadow = _gtk_shadow_new ();
1040 have_inset = have_lengths = have_color = FALSE;
1042 for (i = 0; i < 3; i++)
1045 _gtk_css_parser_try (parser, "inset", TRUE))
1051 if (!have_lengths &&
1052 _gtk_css_parser_try_double (parser, &hoffset))
1054 have_lengths = TRUE;
1056 if (!_gtk_css_parser_try_double (parser, &voffset))
1058 _gtk_css_parser_error (parser, "Horizontal and vertical offsets are required");
1059 _gtk_shadow_unref (shadow);
1063 if (!_gtk_css_parser_try_double (parser, &blur))
1066 if (!_gtk_css_parser_try_double (parser, &spread))
1076 /* XXX: the color is optional and UA-defined if it's missing,
1077 * but it doesn't really make sense for us...
1079 color = _gtk_css_parser_read_symbolic_color (parser);
1083 _gtk_shadow_unref (shadow);
1089 if (!have_color || !have_lengths)
1091 _gtk_css_parser_error (parser, "Must specify at least color and offsets");
1092 _gtk_shadow_unref (shadow);
1096 _gtk_shadow_append (shadow,
1101 gtk_symbolic_color_unref (color);
1104 while (_gtk_css_parser_try (parser, ",", TRUE));
1106 g_value_take_boxed (value, shadow);
1111 shadow_value_print (const GValue *value,
1116 shadow = g_value_get_boxed (value);
1119 g_string_append (string, "none");
1121 _gtk_shadow_print (shadow, string);
1125 border_image_repeat_value_parse (GtkCssParser *parser,
1129 GtkCssBorderImageRepeat image_repeat;
1130 GtkCssRepeatStyle styles[2];
1133 for (i = 0; i < 2; i++)
1135 if (_gtk_css_parser_try (parser, "stretch", TRUE))
1136 styles[i] = GTK_CSS_REPEAT_STYLE_NONE;
1137 else if (_gtk_css_parser_try (parser, "repeat", TRUE))
1138 styles[i] = GTK_CSS_REPEAT_STYLE_REPEAT;
1139 else if (_gtk_css_parser_try (parser, "round", TRUE))
1140 styles[i] = GTK_CSS_REPEAT_STYLE_ROUND;
1141 else if (_gtk_css_parser_try (parser, "space", TRUE))
1142 styles[i] = GTK_CSS_REPEAT_STYLE_SPACE;
1145 styles[1] = styles[0] = GTK_CSS_REPEAT_STYLE_NONE;
1149 styles[i] = styles[0];
1152 image_repeat.hrepeat = styles[0];
1153 image_repeat.vrepeat = styles[1];
1155 g_value_set_boxed (value, &image_repeat);
1160 static const gchar *
1161 border_image_repeat_style_to_string (GtkCssRepeatStyle repeat)
1165 case GTK_CSS_REPEAT_STYLE_NONE:
1167 case GTK_CSS_REPEAT_STYLE_REPEAT:
1169 case GTK_CSS_REPEAT_STYLE_ROUND:
1171 case GTK_CSS_REPEAT_STYLE_SPACE:
1179 border_image_repeat_value_print (const GValue *value,
1182 GtkCssBorderImageRepeat *image_repeat;
1184 image_repeat = g_value_get_boxed (value);
1186 g_string_append_printf (string, "%s %s",
1187 border_image_repeat_style_to_string (image_repeat->hrepeat),
1188 border_image_repeat_style_to_string (image_repeat->vrepeat));
1192 border_image_value_parse (GtkCssParser *parser,
1196 GValue temp = { 0, };
1197 cairo_pattern_t *pattern = NULL;
1198 GtkGradient *gradient = NULL;
1199 GtkBorder slice, *width = NULL, *parsed_slice;
1200 GtkCssBorderImageRepeat repeat, *parsed_repeat;
1201 gboolean retval = FALSE;
1202 GtkBorderImage *image = NULL;
1204 g_value_init (&temp, CAIRO_GOBJECT_TYPE_PATTERN);
1206 if (!pattern_value_parse (parser, base, &temp))
1209 if (G_VALUE_TYPE (&temp) == GTK_TYPE_GRADIENT)
1210 gradient = g_value_dup_boxed (&temp);
1212 pattern = g_value_dup_boxed (&temp);
1214 g_value_unset (&temp);
1215 g_value_init (&temp, GTK_TYPE_BORDER);
1217 if (!border_value_parse (parser, base, &temp))
1220 parsed_slice = g_value_get_boxed (&temp);
1221 slice = *parsed_slice;
1223 if (_gtk_css_parser_try (parser, "/", TRUE))
1225 g_value_unset (&temp);
1226 g_value_init (&temp, GTK_TYPE_BORDER);
1228 if (!border_value_parse (parser, base, &temp))
1231 width = g_value_dup_boxed (&temp);
1234 g_value_unset (&temp);
1235 g_value_init (&temp, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
1237 if (!border_image_repeat_value_parse (parser, base, &temp))
1240 parsed_repeat = g_value_get_boxed (&temp);
1241 repeat = *parsed_repeat;
1243 g_value_unset (&temp);
1245 if (gradient != NULL)
1246 image = _gtk_border_image_new_for_gradient (gradient, &slice, width, &repeat);
1247 else if (pattern != NULL)
1248 image = _gtk_border_image_new (pattern, &slice, width, &repeat);
1253 g_value_take_boxed (value, image);
1257 if (pattern != NULL)
1258 cairo_pattern_destroy (pattern);
1260 if (gradient != NULL)
1261 gtk_gradient_unref (gradient);
1264 gtk_border_free (width);
1270 enum_value_parse (GtkCssParser *parser,
1274 GEnumClass *enum_class;
1275 GEnumValue *enum_value;
1278 str = _gtk_css_parser_try_ident (parser, TRUE);
1281 _gtk_css_parser_error (parser, "Expected an identifier");
1285 enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1286 enum_value = g_enum_get_value_by_nick (enum_class, str);
1289 g_value_set_enum (value, enum_value->value);
1291 _gtk_css_parser_error (parser,
1292 "Unknown value '%s' for enum type '%s'",
1293 str, g_type_name (G_VALUE_TYPE (value)));
1295 g_type_class_unref (enum_class);
1298 return enum_value != NULL;
1302 enum_value_print (const GValue *value,
1305 GEnumClass *enum_class;
1306 GEnumValue *enum_value;
1308 enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1309 enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
1311 g_string_append (string, enum_value->value_nick);
1313 g_type_class_unref (enum_class);
1317 flags_value_parse (GtkCssParser *parser,
1321 GFlagsClass *flags_class;
1322 GFlagsValue *flag_value;
1326 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1329 str = _gtk_css_parser_try_ident (parser, TRUE);
1332 _gtk_css_parser_error (parser, "Expected an identifier");
1333 g_type_class_unref (flags_class);
1337 flag_value = g_flags_get_value_by_nick (flags_class, str);
1340 _gtk_css_parser_error (parser,
1341 "Unknown flag value '%s' for type '%s'",
1342 str, g_type_name (G_VALUE_TYPE (value)));
1343 /* XXX Do we want to return FALSE here? We can get
1344 * forward-compatibility for new values this way
1347 g_type_class_unref (flags_class);
1353 while (_gtk_css_parser_try (parser, ",", FALSE));
1355 g_type_class_unref (flags_class);
1357 g_value_set_enum (value, flags);
1363 flags_value_print (const GValue *value,
1366 GFlagsClass *flags_class;
1369 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1370 flags = g_value_get_flags (value);
1372 for (i = 0; i < flags_class->n_values; i++)
1374 GFlagsValue *flags_value = &flags_class->values[i];
1376 if (flags & flags_value->value)
1378 if (string->len != 0)
1379 g_string_append (string, ", ");
1381 g_string_append (string, flags_value->value_nick);
1385 g_type_class_unref (flags_class);
1389 bindings_value_parse (GtkCssParser *parser,
1394 GtkBindingSet *binding_set;
1397 array = g_ptr_array_new ();
1400 name = _gtk_css_parser_try_ident (parser, TRUE);
1403 _gtk_css_parser_error (parser, "Not a valid binding name");
1404 g_ptr_array_free (array, TRUE);
1408 binding_set = gtk_binding_set_find (name);
1412 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
1417 g_ptr_array_add (array, binding_set);
1420 while (_gtk_css_parser_try (parser, ",", TRUE));
1422 g_value_take_boxed (value, array);
1428 bindings_value_print (const GValue *value,
1434 array = g_value_get_boxed (value);
1436 for (i = 0; i < array->len; i++)
1438 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
1441 g_string_append (string, ", ");
1442 g_string_append (string, binding_set->set_name);
1447 border_corner_radius_value_parse (GtkCssParser *parser,
1451 GtkCssBorderCornerRadius corner;
1453 if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
1455 _gtk_css_parser_error (parser, "Expected a number");
1458 else if (corner.horizontal < 0)
1461 if (!_gtk_css_parser_try_double (parser, &corner.vertical))
1462 corner.vertical = corner.horizontal;
1463 else if (corner.vertical < 0)
1466 g_value_set_boxed (value, &corner);
1470 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1475 border_corner_radius_value_print (const GValue *value,
1478 GtkCssBorderCornerRadius *corner;
1480 corner = g_value_get_boxed (value);
1484 g_string_append (string, "none");
1488 string_append_double (string, corner->horizontal);
1489 if (corner->horizontal != corner->vertical)
1491 g_string_append_c (string, ' ');
1492 string_append_double (string, corner->vertical);
1497 border_radius_value_parse (GtkCssParser *parser,
1501 GtkCssBorderRadius border;
1503 if (!_gtk_css_parser_try_double (parser, &border.top_left.horizontal))
1505 _gtk_css_parser_error (parser, "Expected a number");
1508 else if (border.top_left.horizontal < 0)
1511 if (_gtk_css_parser_try_double (parser, &border.top_right.horizontal))
1513 if (border.top_right.horizontal < 0)
1515 if (_gtk_css_parser_try_double (parser, &border.bottom_right.horizontal))
1517 if (border.bottom_right.horizontal < 0)
1519 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.horizontal))
1520 border.bottom_left.horizontal = border.top_right.horizontal;
1521 else if (border.bottom_left.horizontal < 0)
1526 border.bottom_right.horizontal = border.top_left.horizontal;
1527 border.bottom_left.horizontal = border.top_right.horizontal;
1532 border.top_right.horizontal = border.top_left.horizontal;
1533 border.bottom_right.horizontal = border.top_left.horizontal;
1534 border.bottom_left.horizontal = border.top_left.horizontal;
1537 if (_gtk_css_parser_try (parser, "/", TRUE))
1539 if (!_gtk_css_parser_try_double (parser, &border.top_left.vertical))
1541 _gtk_css_parser_error (parser, "Expected a number");
1544 else if (border.top_left.vertical < 0)
1547 if (_gtk_css_parser_try_double (parser, &border.top_right.vertical))
1549 if (border.top_right.vertical < 0)
1551 if (_gtk_css_parser_try_double (parser, &border.bottom_right.vertical))
1553 if (border.bottom_right.vertical < 0)
1555 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.vertical))
1556 border.bottom_left.vertical = border.top_right.vertical;
1557 else if (border.bottom_left.vertical < 0)
1562 border.bottom_right.vertical = border.top_left.vertical;
1563 border.bottom_left.vertical = border.top_right.vertical;
1568 border.top_right.vertical = border.top_left.vertical;
1569 border.bottom_right.vertical = border.top_left.vertical;
1570 border.bottom_left.vertical = border.top_left.vertical;
1575 border.top_left.vertical = border.top_left.horizontal;
1576 border.top_right.vertical = border.top_right.horizontal;
1577 border.bottom_right.vertical = border.bottom_right.horizontal;
1578 border.bottom_left.vertical = border.bottom_left.horizontal;
1581 /* border-radius is an int property for backwards-compat reasons */
1582 g_value_unset (value);
1583 g_value_init (value, GTK_TYPE_CSS_BORDER_RADIUS);
1584 g_value_set_boxed (value, &border);
1589 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1594 border_radius_value_print (const GValue *value,
1597 GtkCssBorderRadius *border;
1599 border = g_value_get_boxed (value);
1603 g_string_append (string, "none");
1607 string_append_double (string, border->top_left.horizontal);
1608 if (border->top_left.horizontal != border->top_right.horizontal ||
1609 border->top_left.horizontal != border->bottom_right.horizontal ||
1610 border->top_left.horizontal != border->bottom_left.horizontal)
1612 g_string_append_c (string, ' ');
1613 string_append_double (string, border->top_right.horizontal);
1614 if (border->top_left.horizontal != border->bottom_right.horizontal ||
1615 border->top_right.horizontal != border->bottom_left.horizontal)
1617 g_string_append_c (string, ' ');
1618 string_append_double (string, border->bottom_right.horizontal);
1619 if (border->top_right.horizontal != border->bottom_left.horizontal)
1621 g_string_append_c (string, ' ');
1622 string_append_double (string, border->bottom_left.horizontal);
1627 if (border->top_left.horizontal != border->top_left.vertical ||
1628 border->top_right.horizontal != border->top_right.vertical ||
1629 border->bottom_right.horizontal != border->bottom_right.vertical ||
1630 border->bottom_left.horizontal != border->bottom_left.vertical)
1632 g_string_append (string, " / ");
1633 string_append_double (string, border->top_left.vertical);
1634 if (border->top_left.vertical != border->top_right.vertical ||
1635 border->top_left.vertical != border->bottom_right.vertical ||
1636 border->top_left.vertical != border->bottom_left.vertical)
1638 g_string_append_c (string, ' ');
1639 string_append_double (string, border->top_right.vertical);
1640 if (border->top_left.vertical != border->bottom_right.vertical ||
1641 border->top_right.vertical != border->bottom_left.vertical)
1643 g_string_append_c (string, ' ');
1644 string_append_double (string, border->bottom_right.vertical);
1645 if (border->top_right.vertical != border->bottom_left.vertical)
1647 g_string_append_c (string, ' ');
1648 string_append_double (string, border->bottom_left.vertical);
1657 border_color_value_parse (GtkCssParser *parser,
1661 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1663 GdkRGBA transparent = { 0, 0, 0, 0 };
1665 g_value_set_boxed (value, &transparent);
1670 return rgba_value_parse (parser, base, value);
1674 border_color_shorthand_value_parse (GtkCssParser *parser,
1678 GtkSymbolicColor *symbolic;
1681 array = g_ptr_array_new_with_free_func ((GDestroyNotify) gtk_symbolic_color_unref);
1685 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1687 GdkRGBA transparent = { 0, 0, 0, 0 };
1689 symbolic = gtk_symbolic_color_new_literal (&transparent);
1693 symbolic = _gtk_css_parser_read_symbolic_color (parser);
1695 if (symbolic == NULL)
1699 g_ptr_array_add (array, symbolic);
1701 while (array->len < 4 &&
1702 !_gtk_css_parser_is_eof (parser) &&
1703 !_gtk_css_parser_begins_with (parser, ';') &&
1704 !_gtk_css_parser_begins_with (parser, '}'));
1709 g_assert_not_reached ();
1712 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1715 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
1718 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 1)));
1724 g_value_unset (value);
1725 g_value_init (value, G_TYPE_PTR_ARRAY);
1726 g_value_take_boxed (value, array);
1734 unpack_border (const GValue *value,
1741 GParameter *parameter = g_new0 (GParameter, 4);
1742 GtkBorder *border = g_value_get_boxed (value);
1744 parameter[0].name = top;
1745 g_value_init (¶meter[0].value, G_TYPE_INT);
1746 g_value_set_int (¶meter[0].value, border->top);
1747 parameter[1].name = left;
1748 g_value_init (¶meter[1].value, G_TYPE_INT);
1749 g_value_set_int (¶meter[1].value, border->left);
1750 parameter[2].name = bottom;
1751 g_value_init (¶meter[2].value, G_TYPE_INT);
1752 g_value_set_int (¶meter[2].value, border->bottom);
1753 parameter[3].name = right;
1754 g_value_init (¶meter[3].value, G_TYPE_INT);
1755 g_value_set_int (¶meter[3].value, border->right);
1762 pack_border (GValue *value,
1763 GtkStyleProperties *props,
1764 GtkStateFlags state,
1773 gtk_style_properties_get (props,
1786 g_value_set_boxed (value, &border);
1790 unpack_border_width (const GValue *value,
1793 return unpack_border (value, n_params,
1794 "border-top-width", "border-left-width",
1795 "border-bottom-width", "border-right-width");
1799 pack_border_width (GValue *value,
1800 GtkStyleProperties *props,
1801 GtkStateFlags state)
1803 pack_border (value, props, state,
1804 "border-top-width", "border-left-width",
1805 "border-bottom-width", "border-right-width");
1809 unpack_padding (const GValue *value,
1812 return unpack_border (value, n_params,
1813 "padding-top", "padding-left",
1814 "padding-bottom", "padding-right");
1818 pack_padding (GValue *value,
1819 GtkStyleProperties *props,
1820 GtkStateFlags state)
1822 pack_border (value, props, state,
1823 "padding-top", "padding-left",
1824 "padding-bottom", "padding-right");
1828 unpack_margin (const GValue *value,
1831 return unpack_border (value, n_params,
1832 "margin-top", "margin-left",
1833 "margin-bottom", "margin-right");
1837 pack_margin (GValue *value,
1838 GtkStyleProperties *props,
1839 GtkStateFlags state)
1841 pack_border (value, props, state,
1842 "margin-top", "margin-left",
1843 "margin-bottom", "margin-right");
1847 unpack_border_radius (const GValue *value,
1850 GParameter *parameter = g_new0 (GParameter, 4);
1851 GtkCssBorderRadius *border;
1853 if (G_VALUE_HOLDS_BOXED (value))
1854 border = g_value_get_boxed (value);
1858 parameter[0].name = "border-top-left-radius";
1859 g_value_init (¶meter[0].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1860 parameter[1].name = "border-top-right-radius";
1861 g_value_init (¶meter[1].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1862 parameter[2].name = "border-bottom-right-radius";
1863 g_value_init (¶meter[2].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1864 parameter[3].name = "border-bottom-left-radius";
1865 g_value_init (¶meter[3].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1868 g_value_set_boxed (¶meter[0].value, &border->top_left);
1869 g_value_set_boxed (¶meter[1].value, &border->top_right);
1870 g_value_set_boxed (¶meter[2].value, &border->bottom_right);
1871 g_value_set_boxed (¶meter[3].value, &border->bottom_left);
1879 pack_border_radius (GValue *value,
1880 GtkStyleProperties *props,
1881 GtkStateFlags state)
1883 GtkCssBorderCornerRadius *top_left;
1885 /* NB: We are an int property, so we have to resolve to an int here.
1886 * So we just resolve to an int. We pick one and stick to it.
1887 * Lesson learned: Don't query border-radius shorthand, query the
1888 * real properties instead. */
1889 gtk_style_properties_get (props,
1891 "border-top-left-radius", &top_left,
1895 g_value_set_int (value, top_left->horizontal);
1901 unpack_font_description (const GValue *value,
1904 GParameter *parameter = g_new0 (GParameter, 5);
1905 PangoFontDescription *description;
1909 /* For backwards compat, we only unpack values that are indeed set.
1910 * For strict CSS conformance we need to unpack all of them.
1911 * Note that we do set all of them in the parse function, so it
1912 * will not have effects when parsing CSS files. It will though
1913 * for custom style providers.
1916 description = g_value_get_boxed (value);
1920 mask = pango_font_description_get_set_fields (description);
1924 if (mask & PANGO_FONT_MASK_FAMILY)
1926 GPtrArray *strv = g_ptr_array_new ();
1928 g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
1929 g_ptr_array_add (strv, NULL);
1930 parameter[n].name = "font-family";
1931 g_value_init (¶meter[n].value, G_TYPE_STRV);
1932 g_value_take_boxed (¶meter[n].value,
1933 g_ptr_array_free (strv, FALSE));
1937 if (mask & PANGO_FONT_MASK_STYLE)
1939 parameter[n].name = "font-style";
1940 g_value_init (¶meter[n].value, PANGO_TYPE_STYLE);
1941 g_value_set_enum (¶meter[n].value,
1942 pango_font_description_get_style (description));
1946 if (mask & PANGO_FONT_MASK_VARIANT)
1948 parameter[n].name = "font-variant";
1949 g_value_init (¶meter[n].value, PANGO_TYPE_VARIANT);
1950 g_value_set_enum (¶meter[n].value,
1951 pango_font_description_get_variant (description));
1955 if (mask & PANGO_FONT_MASK_WEIGHT)
1957 parameter[n].name = "font-weight";
1958 g_value_init (¶meter[n].value, PANGO_TYPE_WEIGHT);
1959 g_value_set_enum (¶meter[n].value,
1960 pango_font_description_get_weight (description));
1964 if (mask & PANGO_FONT_MASK_SIZE)
1966 parameter[n].name = "font-size";
1967 g_value_init (¶meter[n].value, G_TYPE_DOUBLE);
1968 g_value_set_double (¶meter[n].value,
1969 (double) pango_font_description_get_size (description) / PANGO_SCALE);
1979 pack_font_description (GValue *value,
1980 GtkStyleProperties *props,
1981 GtkStateFlags state)
1983 PangoFontDescription *description;
1986 PangoVariant variant;
1990 gtk_style_properties_get (props,
1992 "font-family", &families,
1993 "font-style", &style,
1994 "font-variant", &variant,
1995 "font-weight", &weight,
1999 description = pango_font_description_new ();
2000 /* xxx: Can we set all the families here somehow? */
2002 pango_font_description_set_family (description, families[0]);
2003 pango_font_description_set_size (description, round (size * PANGO_SCALE));
2004 pango_font_description_set_style (description, style);
2005 pango_font_description_set_variant (description, variant);
2006 pango_font_description_set_weight (description, weight);
2010 g_value_take_boxed (value, description);
2014 unpack_border_color (const GValue *value,
2017 GParameter *parameter = g_new0 (GParameter, 4);
2020 type = G_VALUE_TYPE (value);
2021 if (type == G_TYPE_PTR_ARRAY)
2022 type = GTK_TYPE_SYMBOLIC_COLOR;
2024 parameter[0].name = "border-top-color";
2025 g_value_init (¶meter[0].value, type);
2026 parameter[1].name = "border-right-color";
2027 g_value_init (¶meter[1].value, type);
2028 parameter[2].name = "border-bottom-color";
2029 g_value_init (¶meter[2].value, type);
2030 parameter[3].name = "border-left-color";
2031 g_value_init (¶meter[3].value, type);
2033 if (G_VALUE_TYPE (value) == G_TYPE_PTR_ARRAY)
2035 GPtrArray *array = g_value_get_boxed (value);
2038 for (i = 0; i < 4; i++)
2039 g_value_set_boxed (¶meter[i].value, g_ptr_array_index (array, i));
2043 /* can be RGBA or symbolic color */
2044 gpointer p = g_value_get_boxed (value);
2046 g_value_set_boxed (¶meter[0].value, p);
2047 g_value_set_boxed (¶meter[1].value, p);
2048 g_value_set_boxed (¶meter[2].value, p);
2049 g_value_set_boxed (¶meter[3].value, p);
2057 pack_border_color (GValue *value,
2058 GtkStyleProperties *props,
2059 GtkStateFlags state)
2061 /* NB: We are a color property, so we have to resolve to a color here.
2062 * So we just resolve to a color. We pick one and stick to it.
2063 * Lesson learned: Don't query border-color shorthand, query the
2064 * real properties instead. */
2065 g_value_unset (value);
2066 gtk_style_properties_get_property (props, "border-top-color", state, value);
2069 /*** UNSET FUNCS ***/
2072 unset_font_description (GtkStyleProperties *props,
2073 GtkStateFlags state)
2075 gtk_style_properties_unset_property (props, "font-family", state);
2076 gtk_style_properties_unset_property (props, "font-style", state);
2077 gtk_style_properties_unset_property (props, "font-variant", state);
2078 gtk_style_properties_unset_property (props, "font-weight", state);
2079 gtk_style_properties_unset_property (props, "font-size", state);
2083 unset_margin (GtkStyleProperties *props,
2084 GtkStateFlags state)
2086 gtk_style_properties_unset_property (props, "margin-top", state);
2087 gtk_style_properties_unset_property (props, "margin-right", state);
2088 gtk_style_properties_unset_property (props, "margin-bottom", state);
2089 gtk_style_properties_unset_property (props, "margin-left", state);
2093 unset_padding (GtkStyleProperties *props,
2094 GtkStateFlags state)
2096 gtk_style_properties_unset_property (props, "padding-top", state);
2097 gtk_style_properties_unset_property (props, "padding-right", state);
2098 gtk_style_properties_unset_property (props, "padding-bottom", state);
2099 gtk_style_properties_unset_property (props, "padding-left", state);
2103 unset_border_width (GtkStyleProperties *props,
2104 GtkStateFlags state)
2106 gtk_style_properties_unset_property (props, "border-top-width", state);
2107 gtk_style_properties_unset_property (props, "border-right-width", state);
2108 gtk_style_properties_unset_property (props, "border-bottom-width", state);
2109 gtk_style_properties_unset_property (props, "border-left-width", state);
2113 unset_border_radius (GtkStyleProperties *props,
2114 GtkStateFlags state)
2116 gtk_style_properties_unset_property (props, "border-top-right-radius", state);
2117 gtk_style_properties_unset_property (props, "border-bottom-right-radius", state);
2118 gtk_style_properties_unset_property (props, "border-bottom-left-radius", state);
2119 gtk_style_properties_unset_property (props, "border-top-left-radius", state);
2123 unset_border_color (GtkStyleProperties *props,
2124 GtkStateFlags state)
2126 gtk_style_properties_unset_property (props, "border-top-color", state);
2127 gtk_style_properties_unset_property (props, "border-right-color", state);
2128 gtk_style_properties_unset_property (props, "border-bottom-color", state);
2129 gtk_style_properties_unset_property (props, "border-left-color", state);
2133 unset_border_image (GtkStyleProperties *props,
2134 GtkStateFlags state)
2136 gtk_style_properties_unset_property (props, "border-image-source", state);
2137 gtk_style_properties_unset_property (props, "border-image-slice", state);
2138 gtk_style_properties_unset_property (props, "border-image-repeat", state);
2139 gtk_style_properties_unset_property (props, "border-image-width", state);
2142 /*** default values ***/
2145 border_image_width_default_value (GtkStyleProperties *props,
2146 GtkStateFlags state,
2152 border_color_default_value (GtkStyleProperties *props,
2153 GtkStateFlags state,
2156 g_value_unset (value);
2157 gtk_style_properties_get_property (props, "color", state, value);
2163 css_string_funcs_init (void)
2165 if (G_LIKELY (parse_funcs != NULL))
2168 parse_funcs = g_hash_table_new (NULL, NULL);
2169 print_funcs = g_hash_table_new (NULL, NULL);
2171 register_conversion_function (GDK_TYPE_RGBA,
2174 register_conversion_function (GDK_TYPE_COLOR,
2177 register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
2178 symbolic_color_value_parse,
2179 symbolic_color_value_print);
2180 register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
2181 font_description_value_parse,
2182 font_description_value_print);
2183 register_conversion_function (G_TYPE_BOOLEAN,
2184 boolean_value_parse,
2185 boolean_value_print);
2186 register_conversion_function (G_TYPE_INT,
2189 register_conversion_function (G_TYPE_UINT,
2192 register_conversion_function (G_TYPE_DOUBLE,
2194 double_value_print);
2195 register_conversion_function (G_TYPE_FLOAT,
2198 register_conversion_function (G_TYPE_STRING,
2200 string_value_print);
2201 register_conversion_function (GTK_TYPE_THEMING_ENGINE,
2202 theming_engine_value_parse,
2203 theming_engine_value_print);
2204 register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
2205 animation_description_value_parse,
2206 animation_description_value_print);
2207 register_conversion_function (GTK_TYPE_BORDER,
2209 border_value_print);
2210 register_conversion_function (GTK_TYPE_GRADIENT,
2211 gradient_value_parse,
2212 gradient_value_print);
2213 register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
2214 pattern_value_parse,
2215 pattern_value_print);
2216 register_conversion_function (GTK_TYPE_BORDER_IMAGE,
2217 border_image_value_parse,
2219 register_conversion_function (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
2220 border_image_repeat_value_parse,
2221 border_image_repeat_value_print);
2222 register_conversion_function (GTK_TYPE_SHADOW,
2224 shadow_value_print);
2225 register_conversion_function (G_TYPE_ENUM,
2228 register_conversion_function (G_TYPE_FLAGS,
2234 _gtk_style_property_parse_value (const GtkStyleProperty *property,
2236 GtkCssParser *parser,
2239 GtkStyleParseFunc func;
2241 g_return_val_if_fail (value != NULL, FALSE);
2242 g_return_val_if_fail (parser != NULL, FALSE);
2244 css_string_funcs_init ();
2248 if (_gtk_css_parser_try (parser, "none", TRUE))
2250 /* Insert the default value, so it has an opportunity
2251 * to override other style providers when merged
2253 g_param_value_set_default (property->pspec, value);
2256 else if (property->property_parse_func)
2258 GError *error = NULL;
2262 value_str = _gtk_css_parser_read_value (parser);
2263 if (value_str == NULL)
2266 success = (*property->property_parse_func) (value_str, value, &error);
2273 func = property->parse_func;
2279 func = g_hash_table_lookup (parse_funcs,
2280 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2282 func = g_hash_table_lookup (parse_funcs,
2283 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2287 _gtk_css_parser_error (parser,
2288 "Cannot convert to type '%s'",
2289 g_type_name (G_VALUE_TYPE (value)));
2293 return (*func) (parser, base, value);
2297 _gtk_style_property_print_value (const GtkStyleProperty *property,
2298 const GValue *value,
2301 GtkStylePrintFunc func;
2303 css_string_funcs_init ();
2306 func = property->print_func;
2311 func = g_hash_table_lookup (print_funcs,
2312 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2314 func = g_hash_table_lookup (print_funcs,
2315 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2319 char *s = g_strdup_value_contents (value);
2320 g_string_append (string, s);
2325 func (value, string);
2329 _gtk_style_property_default_value (const GtkStyleProperty *property,
2330 GtkStyleProperties *properties,
2331 GtkStateFlags state,
2334 if (property->default_value_func)
2335 property->default_value_func (properties, state, value);
2336 else if (property->pspec->value_type == GTK_TYPE_THEMING_ENGINE)
2337 g_value_set_object (value, gtk_theming_engine_load (NULL));
2338 else if (property->pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
2339 g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
2340 else if (property->pspec->value_type == GDK_TYPE_RGBA)
2343 gdk_rgba_parse (&color, "pink");
2344 g_value_set_boxed (value, &color);
2346 else if (property->pspec->value_type == GTK_TYPE_BORDER)
2348 g_value_take_boxed (value, gtk_border_new ());
2351 g_param_value_set_default (property->pspec, value);
2355 _gtk_style_property_is_inherit (const GtkStyleProperty *property)
2357 g_return_val_if_fail (property != NULL, FALSE);
2359 return property->flags & GTK_STYLE_PROPERTY_INHERIT ? TRUE : FALSE;
2363 resolve_color (GtkStyleProperties *props,
2368 /* Resolve symbolic color to GdkRGBA */
2369 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
2372 /* Store it back, this is where GdkRGBA caching happens */
2373 g_value_unset (value);
2374 g_value_init (value, GDK_TYPE_RGBA);
2375 g_value_set_boxed (value, &color);
2381 resolve_color_rgb (GtkStyleProperties *props,
2384 GdkColor color = { 0 };
2387 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
2390 color.red = rgba.red * 65535. + 0.5;
2391 color.green = rgba.green * 65535. + 0.5;
2392 color.blue = rgba.blue * 65535. + 0.5;
2394 g_value_unset (value);
2395 g_value_init (value, GDK_TYPE_COLOR);
2396 g_value_set_boxed (value, &color);
2402 resolve_gradient (GtkStyleProperties *props,
2405 cairo_pattern_t *gradient;
2407 if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
2410 /* Store it back, this is where cairo_pattern_t caching happens */
2411 g_value_unset (value);
2412 g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
2413 g_value_take_boxed (value, gradient);
2419 resolve_shadow (GtkStyleProperties *props,
2422 GtkShadow *resolved, *base;
2424 base = g_value_get_boxed (value);
2429 if (_gtk_shadow_get_resolved (base))
2432 resolved = _gtk_shadow_resolve (base, props);
2433 if (resolved == NULL)
2436 g_value_take_boxed (value, resolved);
2442 _gtk_style_property_resolve (const GtkStyleProperty *property,
2443 GtkStyleProperties *props,
2444 GtkStateFlags state,
2447 if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
2449 if (property->pspec->value_type == GDK_TYPE_RGBA)
2451 if (resolve_color (props, val))
2454 else if (property->pspec->value_type == GDK_TYPE_COLOR)
2456 if (resolve_color_rgb (props, val))
2460 g_value_unset (val);
2461 g_value_init (val, property->pspec->value_type);
2462 _gtk_style_property_default_value (property, props, state, val);
2464 else if (G_VALUE_TYPE (val) == GDK_TYPE_RGBA)
2466 if (g_value_get_boxed (val) == NULL)
2467 _gtk_style_property_default_value (property, props, state, val);
2469 else if (G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
2471 g_return_if_fail (property->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN);
2473 if (!resolve_gradient (props, val))
2475 g_value_unset (val);
2476 g_value_init (val, CAIRO_GOBJECT_TYPE_PATTERN);
2477 _gtk_style_property_default_value (property, props, state, val);
2480 else if (G_VALUE_TYPE (val) == GTK_TYPE_SHADOW)
2482 if (!resolve_shadow (props, val))
2483 _gtk_style_property_default_value (property, props, state, val);
2488 _gtk_style_property_is_shorthand (const GtkStyleProperty *property)
2490 g_return_val_if_fail (property != NULL, FALSE);
2492 return property->pack_func != NULL;
2496 _gtk_style_property_unpack (const GtkStyleProperty *property,
2497 const GValue *value,
2500 g_return_val_if_fail (property != NULL, NULL);
2501 g_return_val_if_fail (property->unpack_func != NULL, NULL);
2502 g_return_val_if_fail (value != NULL, NULL);
2503 g_return_val_if_fail (n_params != NULL, NULL);
2505 return property->unpack_func (value, n_params);
2509 _gtk_style_property_pack (const GtkStyleProperty *property,
2510 GtkStyleProperties *props,
2511 GtkStateFlags state,
2514 g_return_if_fail (property != NULL);
2515 g_return_if_fail (property->pack_func != NULL);
2516 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
2517 g_return_if_fail (G_IS_VALUE (value));
2519 property->pack_func (value, props, state);
2523 gtk_style_property_init (void)
2525 if (G_LIKELY (properties))
2528 /* stuff is never freed, so no need for free functions */
2529 properties = g_hash_table_new (g_str_hash, g_str_equal);
2531 /* note that gtk_style_properties_register_property() calls this function,
2532 * so make sure we're sanely inited to avoid infloops */
2534 _gtk_style_property_register (g_param_spec_boxed ("color",
2538 GTK_STYLE_PROPERTY_INHERIT,
2547 gtk_style_properties_register_property (NULL,
2548 g_param_spec_boxed ("background-color",
2553 _gtk_style_property_register (g_param_spec_boxed ("font-family",
2557 GTK_STYLE_PROPERTY_INHERIT,
2562 font_family_value_print,
2565 _gtk_style_property_register (g_param_spec_enum ("font-style",
2569 PANGO_STYLE_NORMAL, 0),
2570 GTK_STYLE_PROPERTY_INHERIT,
2578 _gtk_style_property_register (g_param_spec_enum ("font-variant",
2582 PANGO_VARIANT_NORMAL, 0),
2583 GTK_STYLE_PROPERTY_INHERIT,
2591 /* xxx: need to parse this properly, ie parse the numbers */
2592 _gtk_style_property_register (g_param_spec_enum ("font-weight",
2596 PANGO_WEIGHT_NORMAL, 0),
2597 GTK_STYLE_PROPERTY_INHERIT,
2605 _gtk_style_property_register (g_param_spec_double ("font-size",
2608 0, G_MAXDOUBLE, 0, 0),
2609 GTK_STYLE_PROPERTY_INHERIT,
2617 _gtk_style_property_register (g_param_spec_boxed ("font",
2620 PANGO_TYPE_FONT_DESCRIPTION, 0),
2621 GTK_STYLE_PROPERTY_INHERIT,
2623 unpack_font_description,
2624 pack_font_description,
2625 font_description_value_parse,
2626 font_description_value_print,
2628 unset_font_description);
2630 _gtk_style_property_register (g_param_spec_boxed ("text-shadow",
2633 GTK_TYPE_SHADOW, 0),
2634 GTK_STYLE_PROPERTY_INHERIT,
2643 _gtk_style_property_register (g_param_spec_boxed ("icon-shadow",
2646 GTK_TYPE_SHADOW, 0),
2647 GTK_STYLE_PROPERTY_INHERIT,
2656 gtk_style_properties_register_property (NULL,
2657 g_param_spec_boxed ("box-shadow",
2660 GTK_TYPE_SHADOW, 0));
2661 gtk_style_properties_register_property (NULL,
2662 g_param_spec_int ("margin-top",
2665 0, G_MAXINT, 0, 0));
2666 gtk_style_properties_register_property (NULL,
2667 g_param_spec_int ("margin-left",
2670 0, G_MAXINT, 0, 0));
2671 gtk_style_properties_register_property (NULL,
2672 g_param_spec_int ("margin-bottom",
2675 0, G_MAXINT, 0, 0));
2676 gtk_style_properties_register_property (NULL,
2677 g_param_spec_int ("margin-right",
2680 0, G_MAXINT, 0, 0));
2681 _gtk_style_property_register (g_param_spec_boxed ("margin",
2684 GTK_TYPE_BORDER, 0),
2693 gtk_style_properties_register_property (NULL,
2694 g_param_spec_int ("padding-top",
2697 0, G_MAXINT, 0, 0));
2698 gtk_style_properties_register_property (NULL,
2699 g_param_spec_int ("padding-left",
2702 0, G_MAXINT, 0, 0));
2703 gtk_style_properties_register_property (NULL,
2704 g_param_spec_int ("padding-bottom",
2706 "Padding at bottom",
2707 0, G_MAXINT, 0, 0));
2708 gtk_style_properties_register_property (NULL,
2709 g_param_spec_int ("padding-right",
2712 0, G_MAXINT, 0, 0));
2713 _gtk_style_property_register (g_param_spec_boxed ("padding",
2716 GTK_TYPE_BORDER, 0),
2725 gtk_style_properties_register_property (NULL,
2726 g_param_spec_int ("border-top-width",
2728 "Border width at top",
2729 0, G_MAXINT, 0, 0));
2730 gtk_style_properties_register_property (NULL,
2731 g_param_spec_int ("border-left-width",
2732 "border left width",
2733 "Border width at left",
2734 0, G_MAXINT, 0, 0));
2735 gtk_style_properties_register_property (NULL,
2736 g_param_spec_int ("border-bottom-width",
2737 "border bottom width",
2738 "Border width at bottom",
2739 0, G_MAXINT, 0, 0));
2740 gtk_style_properties_register_property (NULL,
2741 g_param_spec_int ("border-right-width",
2742 "border right width",
2743 "Border width at right",
2744 0, G_MAXINT, 0, 0));
2745 _gtk_style_property_register (g_param_spec_boxed ("border-width",
2747 "Border width, in pixels",
2748 GTK_TYPE_BORDER, 0),
2751 unpack_border_width,
2756 unset_border_width);
2758 _gtk_style_property_register (g_param_spec_boxed ("border-top-left-radius",
2759 "Border top left radius",
2760 "Border radius of top left corner, in pixels",
2761 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2766 border_corner_radius_value_parse,
2767 border_corner_radius_value_print,
2770 _gtk_style_property_register (g_param_spec_boxed ("border-top-right-radius",
2771 "Border top right radius",
2772 "Border radius of top right corner, in pixels",
2773 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2778 border_corner_radius_value_parse,
2779 border_corner_radius_value_print,
2782 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-right-radius",
2783 "Border bottom right radius",
2784 "Border radius of bottom right corner, in pixels",
2785 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2790 border_corner_radius_value_parse,
2791 border_corner_radius_value_print,
2794 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-left-radius",
2795 "Border bottom left radius",
2796 "Border radius of bottom left corner, in pixels",
2797 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2802 border_corner_radius_value_parse,
2803 border_corner_radius_value_print,
2806 _gtk_style_property_register (g_param_spec_int ("border-radius",
2808 "Border radius, in pixels",
2812 unpack_border_radius,
2814 border_radius_value_parse,
2815 border_radius_value_print,
2817 unset_border_radius);
2819 gtk_style_properties_register_property (NULL,
2820 g_param_spec_enum ("border-style",
2823 GTK_TYPE_BORDER_STYLE,
2824 GTK_BORDER_STYLE_NONE, 0));
2825 _gtk_style_property_register (g_param_spec_boxed ("border-top-color",
2833 border_color_value_parse,
2835 border_color_default_value,
2837 _gtk_style_property_register (g_param_spec_boxed ("border-right-color",
2838 "Border right color",
2839 "Border right color",
2845 border_color_value_parse,
2847 border_color_default_value,
2849 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-color",
2850 "Border bottom color",
2851 "Border bottom color",
2857 border_color_value_parse,
2859 border_color_default_value,
2861 _gtk_style_property_register (g_param_spec_boxed ("border-left-color",
2862 "Border left color",
2863 "Border left color",
2869 border_color_value_parse,
2871 border_color_default_value,
2873 _gtk_style_property_register (g_param_spec_boxed ("border-color",
2879 unpack_border_color,
2881 border_color_shorthand_value_parse,
2884 unset_border_color);
2886 gtk_style_properties_register_property (NULL,
2887 g_param_spec_boxed ("background-image",
2890 CAIRO_GOBJECT_TYPE_PATTERN, 0));
2891 gtk_style_properties_register_property (NULL,
2892 g_param_spec_boxed ("border-image-source",
2893 "Border image source",
2894 "Border image source",
2895 CAIRO_GOBJECT_TYPE_PATTERN, 0));
2896 gtk_style_properties_register_property (NULL,
2897 g_param_spec_boxed ("border-image-repeat",
2898 "Border image repeat",
2899 "Border image repeat",
2900 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
2901 gtk_style_properties_register_property (NULL,
2902 g_param_spec_boxed ("border-image-slice",
2903 "Border image slice",
2904 "Border image slice",
2905 GTK_TYPE_BORDER, 0));
2906 _gtk_style_property_register (g_param_spec_boxed ("border-image-width",
2907 "Border image width",
2908 "Border image width",
2909 GTK_TYPE_BORDER, 0),
2916 border_image_width_default_value,
2918 _gtk_style_property_register (g_param_spec_boxed ("border-image",
2921 GTK_TYPE_BORDER_IMAGE, 0),
2924 _gtk_border_image_unpack,
2925 _gtk_border_image_pack,
2929 unset_border_image);
2930 gtk_style_properties_register_property (NULL,
2931 g_param_spec_object ("engine",
2934 GTK_TYPE_THEMING_ENGINE, 0));
2935 gtk_style_properties_register_property (NULL,
2936 g_param_spec_boxed ("transition",
2937 "Transition animation description",
2938 "Transition animation description",
2939 GTK_TYPE_ANIMATION_DESCRIPTION, 0));
2941 /* Private property holding the binding sets */
2942 _gtk_style_property_register (g_param_spec_boxed ("gtk-key-bindings",
2945 G_TYPE_PTR_ARRAY, 0),
2950 bindings_value_parse,
2951 bindings_value_print,
2956 const GtkStyleProperty *
2957 _gtk_style_property_lookup (const char *name)
2959 gtk_style_property_init ();
2961 return g_hash_table_lookup (properties, name);
2965 _gtk_style_property_register (GParamSpec *pspec,
2966 GtkStylePropertyFlags flags,
2967 GtkStylePropertyParser property_parse_func,
2968 GtkStyleUnpackFunc unpack_func,
2969 GtkStylePackFunc pack_func,
2970 GtkStyleParseFunc parse_func,
2971 GtkStylePrintFunc print_func,
2972 GtkStyleDefaultValueFunc default_value_func,
2973 GtkStyleUnsetFunc unset_func)
2975 const GtkStyleProperty *existing;
2976 GtkStyleProperty *node;
2978 g_return_if_fail ((pack_func == NULL) == (unpack_func == NULL));
2980 gtk_style_property_init ();
2982 existing = _gtk_style_property_lookup (pspec->name);
2983 if (existing != NULL)
2985 g_warning ("Property \"%s\" was already registered with type %s",
2986 pspec->name, g_type_name (existing->pspec->value_type));
2990 node = g_slice_new0 (GtkStyleProperty);
2991 node->flags = flags;
2992 node->pspec = pspec;
2993 node->property_parse_func = property_parse_func;
2994 node->pack_func = pack_func;
2995 node->unpack_func = unpack_func;
2996 node->parse_func = parse_func;
2997 node->print_func = print_func;
2998 node->default_value_func = default_value_func;
2999 node->unset_func = unset_func;
3001 g_hash_table_insert (properties, pspec->name, node);