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 (string, border_image_repeat_style_to_string (image_repeat->hrepeat));
1187 if (image_repeat->hrepeat != image_repeat->vrepeat)
1189 g_string_append (string, " ");
1190 g_string_append (string, border_image_repeat_style_to_string (image_repeat->vrepeat));
1195 border_image_value_parse (GtkCssParser *parser,
1199 GValue temp = { 0, };
1200 cairo_pattern_t *pattern = NULL;
1201 GtkGradient *gradient = NULL;
1202 GtkBorder slice, *width = NULL, *parsed_slice;
1203 GtkCssBorderImageRepeat repeat, *parsed_repeat;
1204 gboolean retval = FALSE;
1205 GtkBorderImage *image = NULL;
1207 g_value_init (&temp, CAIRO_GOBJECT_TYPE_PATTERN);
1209 if (!pattern_value_parse (parser, base, &temp))
1212 if (G_VALUE_TYPE (&temp) == GTK_TYPE_GRADIENT)
1213 gradient = g_value_dup_boxed (&temp);
1215 pattern = g_value_dup_boxed (&temp);
1217 g_value_unset (&temp);
1218 g_value_init (&temp, GTK_TYPE_BORDER);
1220 if (!border_value_parse (parser, base, &temp))
1223 parsed_slice = g_value_get_boxed (&temp);
1224 slice = *parsed_slice;
1226 if (_gtk_css_parser_try (parser, "/", TRUE))
1228 g_value_unset (&temp);
1229 g_value_init (&temp, GTK_TYPE_BORDER);
1231 if (!border_value_parse (parser, base, &temp))
1234 width = g_value_dup_boxed (&temp);
1237 g_value_unset (&temp);
1238 g_value_init (&temp, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
1240 if (!border_image_repeat_value_parse (parser, base, &temp))
1243 parsed_repeat = g_value_get_boxed (&temp);
1244 repeat = *parsed_repeat;
1246 g_value_unset (&temp);
1248 if (gradient != NULL)
1249 image = _gtk_border_image_new_for_gradient (gradient, &slice, width, &repeat);
1250 else if (pattern != NULL)
1251 image = _gtk_border_image_new (pattern, &slice, width, &repeat);
1256 g_value_take_boxed (value, image);
1260 if (pattern != NULL)
1261 cairo_pattern_destroy (pattern);
1263 if (gradient != NULL)
1264 gtk_gradient_unref (gradient);
1267 gtk_border_free (width);
1273 enum_value_parse (GtkCssParser *parser,
1277 GEnumClass *enum_class;
1278 GEnumValue *enum_value;
1281 str = _gtk_css_parser_try_ident (parser, TRUE);
1284 _gtk_css_parser_error (parser, "Expected an identifier");
1288 enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1289 enum_value = g_enum_get_value_by_nick (enum_class, str);
1292 g_value_set_enum (value, enum_value->value);
1294 _gtk_css_parser_error (parser,
1295 "Unknown value '%s' for enum type '%s'",
1296 str, g_type_name (G_VALUE_TYPE (value)));
1298 g_type_class_unref (enum_class);
1301 return enum_value != NULL;
1305 enum_value_print (const GValue *value,
1308 GEnumClass *enum_class;
1309 GEnumValue *enum_value;
1311 enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1312 enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
1314 g_string_append (string, enum_value->value_nick);
1316 g_type_class_unref (enum_class);
1320 flags_value_parse (GtkCssParser *parser,
1324 GFlagsClass *flags_class;
1325 GFlagsValue *flag_value;
1329 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1332 str = _gtk_css_parser_try_ident (parser, TRUE);
1335 _gtk_css_parser_error (parser, "Expected an identifier");
1336 g_type_class_unref (flags_class);
1340 flag_value = g_flags_get_value_by_nick (flags_class, str);
1343 _gtk_css_parser_error (parser,
1344 "Unknown flag value '%s' for type '%s'",
1345 str, g_type_name (G_VALUE_TYPE (value)));
1346 /* XXX Do we want to return FALSE here? We can get
1347 * forward-compatibility for new values this way
1350 g_type_class_unref (flags_class);
1356 while (_gtk_css_parser_try (parser, ",", FALSE));
1358 g_type_class_unref (flags_class);
1360 g_value_set_enum (value, flags);
1366 flags_value_print (const GValue *value,
1369 GFlagsClass *flags_class;
1372 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1373 flags = g_value_get_flags (value);
1375 for (i = 0; i < flags_class->n_values; i++)
1377 GFlagsValue *flags_value = &flags_class->values[i];
1379 if (flags & flags_value->value)
1381 if (string->len != 0)
1382 g_string_append (string, ", ");
1384 g_string_append (string, flags_value->value_nick);
1388 g_type_class_unref (flags_class);
1392 bindings_value_parse (GtkCssParser *parser,
1397 GtkBindingSet *binding_set;
1400 array = g_ptr_array_new ();
1403 name = _gtk_css_parser_try_ident (parser, TRUE);
1406 _gtk_css_parser_error (parser, "Not a valid binding name");
1407 g_ptr_array_free (array, TRUE);
1411 binding_set = gtk_binding_set_find (name);
1415 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
1420 g_ptr_array_add (array, binding_set);
1423 while (_gtk_css_parser_try (parser, ",", TRUE));
1425 g_value_take_boxed (value, array);
1431 bindings_value_print (const GValue *value,
1437 array = g_value_get_boxed (value);
1439 for (i = 0; i < array->len; i++)
1441 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
1444 g_string_append (string, ", ");
1445 g_string_append (string, binding_set->set_name);
1450 border_corner_radius_value_parse (GtkCssParser *parser,
1454 GtkCssBorderCornerRadius corner;
1456 if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
1458 _gtk_css_parser_error (parser, "Expected a number");
1461 else if (corner.horizontal < 0)
1464 if (!_gtk_css_parser_try_double (parser, &corner.vertical))
1465 corner.vertical = corner.horizontal;
1466 else if (corner.vertical < 0)
1469 g_value_set_boxed (value, &corner);
1473 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1478 border_corner_radius_value_print (const GValue *value,
1481 GtkCssBorderCornerRadius *corner;
1483 corner = g_value_get_boxed (value);
1487 g_string_append (string, "none");
1491 string_append_double (string, corner->horizontal);
1492 if (corner->horizontal != corner->vertical)
1494 g_string_append_c (string, ' ');
1495 string_append_double (string, corner->vertical);
1500 border_radius_value_parse (GtkCssParser *parser,
1504 GtkCssBorderRadius border;
1506 if (!_gtk_css_parser_try_double (parser, &border.top_left.horizontal))
1508 _gtk_css_parser_error (parser, "Expected a number");
1511 else if (border.top_left.horizontal < 0)
1514 if (_gtk_css_parser_try_double (parser, &border.top_right.horizontal))
1516 if (border.top_right.horizontal < 0)
1518 if (_gtk_css_parser_try_double (parser, &border.bottom_right.horizontal))
1520 if (border.bottom_right.horizontal < 0)
1522 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.horizontal))
1523 border.bottom_left.horizontal = border.top_right.horizontal;
1524 else if (border.bottom_left.horizontal < 0)
1529 border.bottom_right.horizontal = border.top_left.horizontal;
1530 border.bottom_left.horizontal = border.top_right.horizontal;
1535 border.top_right.horizontal = border.top_left.horizontal;
1536 border.bottom_right.horizontal = border.top_left.horizontal;
1537 border.bottom_left.horizontal = border.top_left.horizontal;
1540 if (_gtk_css_parser_try (parser, "/", TRUE))
1542 if (!_gtk_css_parser_try_double (parser, &border.top_left.vertical))
1544 _gtk_css_parser_error (parser, "Expected a number");
1547 else if (border.top_left.vertical < 0)
1550 if (_gtk_css_parser_try_double (parser, &border.top_right.vertical))
1552 if (border.top_right.vertical < 0)
1554 if (_gtk_css_parser_try_double (parser, &border.bottom_right.vertical))
1556 if (border.bottom_right.vertical < 0)
1558 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.vertical))
1559 border.bottom_left.vertical = border.top_right.vertical;
1560 else if (border.bottom_left.vertical < 0)
1565 border.bottom_right.vertical = border.top_left.vertical;
1566 border.bottom_left.vertical = border.top_right.vertical;
1571 border.top_right.vertical = border.top_left.vertical;
1572 border.bottom_right.vertical = border.top_left.vertical;
1573 border.bottom_left.vertical = border.top_left.vertical;
1578 border.top_left.vertical = border.top_left.horizontal;
1579 border.top_right.vertical = border.top_right.horizontal;
1580 border.bottom_right.vertical = border.bottom_right.horizontal;
1581 border.bottom_left.vertical = border.bottom_left.horizontal;
1584 /* border-radius is an int property for backwards-compat reasons */
1585 g_value_unset (value);
1586 g_value_init (value, GTK_TYPE_CSS_BORDER_RADIUS);
1587 g_value_set_boxed (value, &border);
1592 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
1597 border_radius_value_print (const GValue *value,
1600 GtkCssBorderRadius *border;
1602 border = g_value_get_boxed (value);
1606 g_string_append (string, "none");
1610 string_append_double (string, border->top_left.horizontal);
1611 if (border->top_left.horizontal != border->top_right.horizontal ||
1612 border->top_left.horizontal != border->bottom_right.horizontal ||
1613 border->top_left.horizontal != border->bottom_left.horizontal)
1615 g_string_append_c (string, ' ');
1616 string_append_double (string, border->top_right.horizontal);
1617 if (border->top_left.horizontal != border->bottom_right.horizontal ||
1618 border->top_right.horizontal != border->bottom_left.horizontal)
1620 g_string_append_c (string, ' ');
1621 string_append_double (string, border->bottom_right.horizontal);
1622 if (border->top_right.horizontal != border->bottom_left.horizontal)
1624 g_string_append_c (string, ' ');
1625 string_append_double (string, border->bottom_left.horizontal);
1630 if (border->top_left.horizontal != border->top_left.vertical ||
1631 border->top_right.horizontal != border->top_right.vertical ||
1632 border->bottom_right.horizontal != border->bottom_right.vertical ||
1633 border->bottom_left.horizontal != border->bottom_left.vertical)
1635 g_string_append (string, " / ");
1636 string_append_double (string, border->top_left.vertical);
1637 if (border->top_left.vertical != border->top_right.vertical ||
1638 border->top_left.vertical != border->bottom_right.vertical ||
1639 border->top_left.vertical != border->bottom_left.vertical)
1641 g_string_append_c (string, ' ');
1642 string_append_double (string, border->top_right.vertical);
1643 if (border->top_left.vertical != border->bottom_right.vertical ||
1644 border->top_right.vertical != border->bottom_left.vertical)
1646 g_string_append_c (string, ' ');
1647 string_append_double (string, border->bottom_right.vertical);
1648 if (border->top_right.vertical != border->bottom_left.vertical)
1650 g_string_append_c (string, ' ');
1651 string_append_double (string, border->bottom_left.vertical);
1660 border_color_value_parse (GtkCssParser *parser,
1664 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1666 GdkRGBA transparent = { 0, 0, 0, 0 };
1668 g_value_set_boxed (value, &transparent);
1673 return rgba_value_parse (parser, base, value);
1677 border_color_shorthand_value_parse (GtkCssParser *parser,
1681 GtkSymbolicColor *symbolic;
1684 array = g_ptr_array_new_with_free_func ((GDestroyNotify) gtk_symbolic_color_unref);
1688 if (_gtk_css_parser_try (parser, "transparent", TRUE))
1690 GdkRGBA transparent = { 0, 0, 0, 0 };
1692 symbolic = gtk_symbolic_color_new_literal (&transparent);
1696 symbolic = _gtk_css_parser_read_symbolic_color (parser);
1698 if (symbolic == NULL)
1702 g_ptr_array_add (array, symbolic);
1704 while (array->len < 4 &&
1705 !_gtk_css_parser_is_eof (parser) &&
1706 !_gtk_css_parser_begins_with (parser, ';') &&
1707 !_gtk_css_parser_begins_with (parser, '}'));
1712 g_assert_not_reached ();
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, 0)));
1721 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 1)));
1727 g_value_unset (value);
1728 g_value_init (value, G_TYPE_PTR_ARRAY);
1729 g_value_take_boxed (value, array);
1737 unpack_border (const GValue *value,
1744 GParameter *parameter = g_new0 (GParameter, 4);
1745 GtkBorder *border = g_value_get_boxed (value);
1747 parameter[0].name = top;
1748 g_value_init (¶meter[0].value, G_TYPE_INT);
1749 g_value_set_int (¶meter[0].value, border->top);
1750 parameter[1].name = left;
1751 g_value_init (¶meter[1].value, G_TYPE_INT);
1752 g_value_set_int (¶meter[1].value, border->left);
1753 parameter[2].name = bottom;
1754 g_value_init (¶meter[2].value, G_TYPE_INT);
1755 g_value_set_int (¶meter[2].value, border->bottom);
1756 parameter[3].name = right;
1757 g_value_init (¶meter[3].value, G_TYPE_INT);
1758 g_value_set_int (¶meter[3].value, border->right);
1765 pack_border (GValue *value,
1766 GtkStyleProperties *props,
1767 GtkStateFlags state,
1776 gtk_style_properties_get (props,
1789 g_value_set_boxed (value, &border);
1793 unpack_border_width (const GValue *value,
1796 return unpack_border (value, n_params,
1797 "border-top-width", "border-left-width",
1798 "border-bottom-width", "border-right-width");
1802 pack_border_width (GValue *value,
1803 GtkStyleProperties *props,
1804 GtkStateFlags state)
1806 pack_border (value, props, state,
1807 "border-top-width", "border-left-width",
1808 "border-bottom-width", "border-right-width");
1812 unpack_padding (const GValue *value,
1815 return unpack_border (value, n_params,
1816 "padding-top", "padding-left",
1817 "padding-bottom", "padding-right");
1821 pack_padding (GValue *value,
1822 GtkStyleProperties *props,
1823 GtkStateFlags state)
1825 pack_border (value, props, state,
1826 "padding-top", "padding-left",
1827 "padding-bottom", "padding-right");
1831 unpack_margin (const GValue *value,
1834 return unpack_border (value, n_params,
1835 "margin-top", "margin-left",
1836 "margin-bottom", "margin-right");
1840 pack_margin (GValue *value,
1841 GtkStyleProperties *props,
1842 GtkStateFlags state)
1844 pack_border (value, props, state,
1845 "margin-top", "margin-left",
1846 "margin-bottom", "margin-right");
1850 unpack_border_radius (const GValue *value,
1853 GParameter *parameter = g_new0 (GParameter, 4);
1854 GtkCssBorderRadius *border;
1856 if (G_VALUE_HOLDS_BOXED (value))
1857 border = g_value_get_boxed (value);
1861 parameter[0].name = "border-top-left-radius";
1862 g_value_init (¶meter[0].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1863 parameter[1].name = "border-top-right-radius";
1864 g_value_init (¶meter[1].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1865 parameter[2].name = "border-bottom-right-radius";
1866 g_value_init (¶meter[2].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1867 parameter[3].name = "border-bottom-left-radius";
1868 g_value_init (¶meter[3].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
1871 g_value_set_boxed (¶meter[0].value, &border->top_left);
1872 g_value_set_boxed (¶meter[1].value, &border->top_right);
1873 g_value_set_boxed (¶meter[2].value, &border->bottom_right);
1874 g_value_set_boxed (¶meter[3].value, &border->bottom_left);
1882 pack_border_radius (GValue *value,
1883 GtkStyleProperties *props,
1884 GtkStateFlags state)
1886 GtkCssBorderCornerRadius *top_left;
1888 /* NB: We are an int property, so we have to resolve to an int here.
1889 * So we just resolve to an int. We pick one and stick to it.
1890 * Lesson learned: Don't query border-radius shorthand, query the
1891 * real properties instead. */
1892 gtk_style_properties_get (props,
1894 "border-top-left-radius", &top_left,
1898 g_value_set_int (value, top_left->horizontal);
1904 unpack_font_description (const GValue *value,
1907 GParameter *parameter = g_new0 (GParameter, 5);
1908 PangoFontDescription *description;
1912 /* For backwards compat, we only unpack values that are indeed set.
1913 * For strict CSS conformance we need to unpack all of them.
1914 * Note that we do set all of them in the parse function, so it
1915 * will not have effects when parsing CSS files. It will though
1916 * for custom style providers.
1919 description = g_value_get_boxed (value);
1923 mask = pango_font_description_get_set_fields (description);
1927 if (mask & PANGO_FONT_MASK_FAMILY)
1929 GPtrArray *strv = g_ptr_array_new ();
1931 g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
1932 g_ptr_array_add (strv, NULL);
1933 parameter[n].name = "font-family";
1934 g_value_init (¶meter[n].value, G_TYPE_STRV);
1935 g_value_take_boxed (¶meter[n].value,
1936 g_ptr_array_free (strv, FALSE));
1940 if (mask & PANGO_FONT_MASK_STYLE)
1942 parameter[n].name = "font-style";
1943 g_value_init (¶meter[n].value, PANGO_TYPE_STYLE);
1944 g_value_set_enum (¶meter[n].value,
1945 pango_font_description_get_style (description));
1949 if (mask & PANGO_FONT_MASK_VARIANT)
1951 parameter[n].name = "font-variant";
1952 g_value_init (¶meter[n].value, PANGO_TYPE_VARIANT);
1953 g_value_set_enum (¶meter[n].value,
1954 pango_font_description_get_variant (description));
1958 if (mask & PANGO_FONT_MASK_WEIGHT)
1960 parameter[n].name = "font-weight";
1961 g_value_init (¶meter[n].value, PANGO_TYPE_WEIGHT);
1962 g_value_set_enum (¶meter[n].value,
1963 pango_font_description_get_weight (description));
1967 if (mask & PANGO_FONT_MASK_SIZE)
1969 parameter[n].name = "font-size";
1970 g_value_init (¶meter[n].value, G_TYPE_DOUBLE);
1971 g_value_set_double (¶meter[n].value,
1972 (double) pango_font_description_get_size (description) / PANGO_SCALE);
1982 pack_font_description (GValue *value,
1983 GtkStyleProperties *props,
1984 GtkStateFlags state)
1986 PangoFontDescription *description;
1989 PangoVariant variant;
1993 gtk_style_properties_get (props,
1995 "font-family", &families,
1996 "font-style", &style,
1997 "font-variant", &variant,
1998 "font-weight", &weight,
2002 description = pango_font_description_new ();
2003 /* xxx: Can we set all the families here somehow? */
2005 pango_font_description_set_family (description, families[0]);
2006 pango_font_description_set_size (description, round (size * PANGO_SCALE));
2007 pango_font_description_set_style (description, style);
2008 pango_font_description_set_variant (description, variant);
2009 pango_font_description_set_weight (description, weight);
2011 g_strfreev (families);
2013 g_value_take_boxed (value, description);
2017 unpack_border_color (const GValue *value,
2020 GParameter *parameter = g_new0 (GParameter, 4);
2023 type = G_VALUE_TYPE (value);
2024 if (type == G_TYPE_PTR_ARRAY)
2025 type = GTK_TYPE_SYMBOLIC_COLOR;
2027 parameter[0].name = "border-top-color";
2028 g_value_init (¶meter[0].value, type);
2029 parameter[1].name = "border-right-color";
2030 g_value_init (¶meter[1].value, type);
2031 parameter[2].name = "border-bottom-color";
2032 g_value_init (¶meter[2].value, type);
2033 parameter[3].name = "border-left-color";
2034 g_value_init (¶meter[3].value, type);
2036 if (G_VALUE_TYPE (value) == G_TYPE_PTR_ARRAY)
2038 GPtrArray *array = g_value_get_boxed (value);
2041 for (i = 0; i < 4; i++)
2042 g_value_set_boxed (¶meter[i].value, g_ptr_array_index (array, i));
2046 /* can be RGBA or symbolic color */
2047 gpointer p = g_value_get_boxed (value);
2049 g_value_set_boxed (¶meter[0].value, p);
2050 g_value_set_boxed (¶meter[1].value, p);
2051 g_value_set_boxed (¶meter[2].value, p);
2052 g_value_set_boxed (¶meter[3].value, p);
2060 pack_border_color (GValue *value,
2061 GtkStyleProperties *props,
2062 GtkStateFlags state)
2064 /* NB: We are a color property, so we have to resolve to a color here.
2065 * So we just resolve to a color. We pick one and stick to it.
2066 * Lesson learned: Don't query border-color shorthand, query the
2067 * real properties instead. */
2068 g_value_unset (value);
2069 gtk_style_properties_get_property (props, "border-top-color", state, value);
2072 /*** UNSET FUNCS ***/
2075 unset_font_description (GtkStyleProperties *props,
2076 GtkStateFlags state)
2078 gtk_style_properties_unset_property (props, "font-family", state);
2079 gtk_style_properties_unset_property (props, "font-style", state);
2080 gtk_style_properties_unset_property (props, "font-variant", state);
2081 gtk_style_properties_unset_property (props, "font-weight", state);
2082 gtk_style_properties_unset_property (props, "font-size", state);
2086 unset_margin (GtkStyleProperties *props,
2087 GtkStateFlags state)
2089 gtk_style_properties_unset_property (props, "margin-top", state);
2090 gtk_style_properties_unset_property (props, "margin-right", state);
2091 gtk_style_properties_unset_property (props, "margin-bottom", state);
2092 gtk_style_properties_unset_property (props, "margin-left", state);
2096 unset_padding (GtkStyleProperties *props,
2097 GtkStateFlags state)
2099 gtk_style_properties_unset_property (props, "padding-top", state);
2100 gtk_style_properties_unset_property (props, "padding-right", state);
2101 gtk_style_properties_unset_property (props, "padding-bottom", state);
2102 gtk_style_properties_unset_property (props, "padding-left", state);
2106 unset_border_width (GtkStyleProperties *props,
2107 GtkStateFlags state)
2109 gtk_style_properties_unset_property (props, "border-top-width", state);
2110 gtk_style_properties_unset_property (props, "border-right-width", state);
2111 gtk_style_properties_unset_property (props, "border-bottom-width", state);
2112 gtk_style_properties_unset_property (props, "border-left-width", state);
2116 unset_border_radius (GtkStyleProperties *props,
2117 GtkStateFlags state)
2119 gtk_style_properties_unset_property (props, "border-top-right-radius", state);
2120 gtk_style_properties_unset_property (props, "border-bottom-right-radius", state);
2121 gtk_style_properties_unset_property (props, "border-bottom-left-radius", state);
2122 gtk_style_properties_unset_property (props, "border-top-left-radius", state);
2126 unset_border_color (GtkStyleProperties *props,
2127 GtkStateFlags state)
2129 gtk_style_properties_unset_property (props, "border-top-color", state);
2130 gtk_style_properties_unset_property (props, "border-right-color", state);
2131 gtk_style_properties_unset_property (props, "border-bottom-color", state);
2132 gtk_style_properties_unset_property (props, "border-left-color", state);
2136 unset_border_image (GtkStyleProperties *props,
2137 GtkStateFlags state)
2139 gtk_style_properties_unset_property (props, "border-image-source", state);
2140 gtk_style_properties_unset_property (props, "border-image-slice", state);
2141 gtk_style_properties_unset_property (props, "border-image-repeat", state);
2142 gtk_style_properties_unset_property (props, "border-image-width", state);
2145 /*** default values ***/
2148 border_image_width_default_value (GtkStyleProperties *props,
2149 GtkStateFlags state,
2155 border_color_default_value (GtkStyleProperties *props,
2156 GtkStateFlags state,
2159 g_value_unset (value);
2160 gtk_style_properties_get_property (props, "color", state, value);
2166 css_string_funcs_init (void)
2168 if (G_LIKELY (parse_funcs != NULL))
2171 parse_funcs = g_hash_table_new (NULL, NULL);
2172 print_funcs = g_hash_table_new (NULL, NULL);
2174 register_conversion_function (GDK_TYPE_RGBA,
2177 register_conversion_function (GDK_TYPE_COLOR,
2180 register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
2181 symbolic_color_value_parse,
2182 symbolic_color_value_print);
2183 register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
2184 font_description_value_parse,
2185 font_description_value_print);
2186 register_conversion_function (G_TYPE_BOOLEAN,
2187 boolean_value_parse,
2188 boolean_value_print);
2189 register_conversion_function (G_TYPE_INT,
2192 register_conversion_function (G_TYPE_UINT,
2195 register_conversion_function (G_TYPE_DOUBLE,
2197 double_value_print);
2198 register_conversion_function (G_TYPE_FLOAT,
2201 register_conversion_function (G_TYPE_STRING,
2203 string_value_print);
2204 register_conversion_function (GTK_TYPE_THEMING_ENGINE,
2205 theming_engine_value_parse,
2206 theming_engine_value_print);
2207 register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
2208 animation_description_value_parse,
2209 animation_description_value_print);
2210 register_conversion_function (GTK_TYPE_BORDER,
2212 border_value_print);
2213 register_conversion_function (GTK_TYPE_GRADIENT,
2214 gradient_value_parse,
2215 gradient_value_print);
2216 register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
2217 pattern_value_parse,
2218 pattern_value_print);
2219 register_conversion_function (GTK_TYPE_BORDER_IMAGE,
2220 border_image_value_parse,
2222 register_conversion_function (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
2223 border_image_repeat_value_parse,
2224 border_image_repeat_value_print);
2225 register_conversion_function (GTK_TYPE_SHADOW,
2227 shadow_value_print);
2228 register_conversion_function (G_TYPE_ENUM,
2231 register_conversion_function (G_TYPE_FLAGS,
2237 _gtk_style_property_parse_value (const GtkStyleProperty *property,
2239 GtkCssParser *parser,
2242 GtkStyleParseFunc func;
2244 g_return_val_if_fail (value != NULL, FALSE);
2245 g_return_val_if_fail (parser != NULL, FALSE);
2247 css_string_funcs_init ();
2251 if (_gtk_css_parser_try (parser, "none", TRUE))
2253 /* Insert the default value, so it has an opportunity
2254 * to override other style providers when merged
2256 g_param_value_set_default (property->pspec, value);
2259 else if (property->property_parse_func)
2261 GError *error = NULL;
2265 value_str = _gtk_css_parser_read_value (parser);
2266 if (value_str == NULL)
2269 success = (*property->property_parse_func) (value_str, value, &error);
2276 func = property->parse_func;
2282 func = g_hash_table_lookup (parse_funcs,
2283 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2285 func = g_hash_table_lookup (parse_funcs,
2286 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2290 _gtk_css_parser_error (parser,
2291 "Cannot convert to type '%s'",
2292 g_type_name (G_VALUE_TYPE (value)));
2296 return (*func) (parser, base, value);
2300 _gtk_style_property_print_value (const GtkStyleProperty *property,
2301 const GValue *value,
2304 GtkStylePrintFunc func;
2306 css_string_funcs_init ();
2309 func = property->print_func;
2314 func = g_hash_table_lookup (print_funcs,
2315 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
2317 func = g_hash_table_lookup (print_funcs,
2318 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
2322 char *s = g_strdup_value_contents (value);
2323 g_string_append (string, s);
2328 func (value, string);
2332 _gtk_style_property_default_value (const GtkStyleProperty *property,
2333 GtkStyleProperties *properties,
2334 GtkStateFlags state,
2337 if (property->default_value_func)
2338 property->default_value_func (properties, state, value);
2339 else if (property->pspec->value_type == GTK_TYPE_THEMING_ENGINE)
2340 g_value_set_object (value, gtk_theming_engine_load (NULL));
2341 else if (property->pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
2342 g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
2343 else if (property->pspec->value_type == GDK_TYPE_RGBA)
2346 gdk_rgba_parse (&color, "pink");
2347 g_value_set_boxed (value, &color);
2349 else if (property->pspec->value_type == GTK_TYPE_BORDER)
2351 g_value_take_boxed (value, gtk_border_new ());
2354 g_param_value_set_default (property->pspec, value);
2358 _gtk_style_property_is_inherit (const GtkStyleProperty *property)
2360 g_return_val_if_fail (property != NULL, FALSE);
2362 return property->flags & GTK_STYLE_PROPERTY_INHERIT ? TRUE : FALSE;
2366 resolve_color (GtkStyleProperties *props,
2371 /* Resolve symbolic color to GdkRGBA */
2372 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &color))
2375 /* Store it back, this is where GdkRGBA caching happens */
2376 g_value_unset (value);
2377 g_value_init (value, GDK_TYPE_RGBA);
2378 g_value_set_boxed (value, &color);
2384 resolve_color_rgb (GtkStyleProperties *props,
2387 GdkColor color = { 0 };
2390 if (!gtk_symbolic_color_resolve (g_value_get_boxed (value), props, &rgba))
2393 color.red = rgba.red * 65535. + 0.5;
2394 color.green = rgba.green * 65535. + 0.5;
2395 color.blue = rgba.blue * 65535. + 0.5;
2397 g_value_unset (value);
2398 g_value_init (value, GDK_TYPE_COLOR);
2399 g_value_set_boxed (value, &color);
2405 resolve_gradient (GtkStyleProperties *props,
2408 cairo_pattern_t *gradient;
2410 if (!gtk_gradient_resolve (g_value_get_boxed (value), props, &gradient))
2413 /* Store it back, this is where cairo_pattern_t caching happens */
2414 g_value_unset (value);
2415 g_value_init (value, CAIRO_GOBJECT_TYPE_PATTERN);
2416 g_value_take_boxed (value, gradient);
2422 resolve_shadow (GtkStyleProperties *props,
2425 GtkShadow *resolved, *base;
2427 base = g_value_get_boxed (value);
2432 if (_gtk_shadow_get_resolved (base))
2435 resolved = _gtk_shadow_resolve (base, props);
2436 if (resolved == NULL)
2439 g_value_take_boxed (value, resolved);
2445 _gtk_style_property_resolve (const GtkStyleProperty *property,
2446 GtkStyleProperties *props,
2447 GtkStateFlags state,
2450 if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
2452 if (property->pspec->value_type == GDK_TYPE_RGBA)
2454 if (resolve_color (props, val))
2457 else if (property->pspec->value_type == GDK_TYPE_COLOR)
2459 if (resolve_color_rgb (props, val))
2463 g_value_unset (val);
2464 g_value_init (val, property->pspec->value_type);
2465 _gtk_style_property_default_value (property, props, state, val);
2467 else if (G_VALUE_TYPE (val) == GDK_TYPE_RGBA)
2469 if (g_value_get_boxed (val) == NULL)
2470 _gtk_style_property_default_value (property, props, state, val);
2472 else if (G_VALUE_TYPE (val) == GTK_TYPE_GRADIENT)
2474 g_return_if_fail (property->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN);
2476 if (!resolve_gradient (props, val))
2478 g_value_unset (val);
2479 g_value_init (val, CAIRO_GOBJECT_TYPE_PATTERN);
2480 _gtk_style_property_default_value (property, props, state, val);
2483 else if (G_VALUE_TYPE (val) == GTK_TYPE_SHADOW)
2485 if (!resolve_shadow (props, val))
2486 _gtk_style_property_default_value (property, props, state, val);
2491 _gtk_style_property_is_shorthand (const GtkStyleProperty *property)
2493 g_return_val_if_fail (property != NULL, FALSE);
2495 return property->pack_func != NULL;
2499 _gtk_style_property_unpack (const GtkStyleProperty *property,
2500 const GValue *value,
2503 g_return_val_if_fail (property != NULL, NULL);
2504 g_return_val_if_fail (property->unpack_func != NULL, NULL);
2505 g_return_val_if_fail (value != NULL, NULL);
2506 g_return_val_if_fail (n_params != NULL, NULL);
2508 return property->unpack_func (value, n_params);
2512 _gtk_style_property_pack (const GtkStyleProperty *property,
2513 GtkStyleProperties *props,
2514 GtkStateFlags state,
2517 g_return_if_fail (property != NULL);
2518 g_return_if_fail (property->pack_func != NULL);
2519 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
2520 g_return_if_fail (G_IS_VALUE (value));
2522 property->pack_func (value, props, state);
2526 gtk_style_property_init (void)
2528 if (G_LIKELY (properties))
2531 /* stuff is never freed, so no need for free functions */
2532 properties = g_hash_table_new (g_str_hash, g_str_equal);
2534 /* note that gtk_style_properties_register_property() calls this function,
2535 * so make sure we're sanely inited to avoid infloops */
2537 _gtk_style_property_register (g_param_spec_boxed ("color",
2541 GTK_STYLE_PROPERTY_INHERIT,
2550 gtk_style_properties_register_property (NULL,
2551 g_param_spec_boxed ("background-color",
2556 _gtk_style_property_register (g_param_spec_boxed ("font-family",
2560 GTK_STYLE_PROPERTY_INHERIT,
2565 font_family_value_print,
2568 _gtk_style_property_register (g_param_spec_enum ("font-style",
2572 PANGO_STYLE_NORMAL, 0),
2573 GTK_STYLE_PROPERTY_INHERIT,
2581 _gtk_style_property_register (g_param_spec_enum ("font-variant",
2585 PANGO_VARIANT_NORMAL, 0),
2586 GTK_STYLE_PROPERTY_INHERIT,
2594 /* xxx: need to parse this properly, ie parse the numbers */
2595 _gtk_style_property_register (g_param_spec_enum ("font-weight",
2599 PANGO_WEIGHT_NORMAL, 0),
2600 GTK_STYLE_PROPERTY_INHERIT,
2608 _gtk_style_property_register (g_param_spec_double ("font-size",
2611 0, G_MAXDOUBLE, 0, 0),
2612 GTK_STYLE_PROPERTY_INHERIT,
2620 _gtk_style_property_register (g_param_spec_boxed ("font",
2623 PANGO_TYPE_FONT_DESCRIPTION, 0),
2624 GTK_STYLE_PROPERTY_INHERIT,
2626 unpack_font_description,
2627 pack_font_description,
2628 font_description_value_parse,
2629 font_description_value_print,
2631 unset_font_description);
2633 _gtk_style_property_register (g_param_spec_boxed ("text-shadow",
2636 GTK_TYPE_SHADOW, 0),
2637 GTK_STYLE_PROPERTY_INHERIT,
2646 _gtk_style_property_register (g_param_spec_boxed ("icon-shadow",
2649 GTK_TYPE_SHADOW, 0),
2650 GTK_STYLE_PROPERTY_INHERIT,
2659 gtk_style_properties_register_property (NULL,
2660 g_param_spec_boxed ("box-shadow",
2663 GTK_TYPE_SHADOW, 0));
2664 gtk_style_properties_register_property (NULL,
2665 g_param_spec_int ("margin-top",
2668 0, G_MAXINT, 0, 0));
2669 gtk_style_properties_register_property (NULL,
2670 g_param_spec_int ("margin-left",
2673 0, G_MAXINT, 0, 0));
2674 gtk_style_properties_register_property (NULL,
2675 g_param_spec_int ("margin-bottom",
2678 0, G_MAXINT, 0, 0));
2679 gtk_style_properties_register_property (NULL,
2680 g_param_spec_int ("margin-right",
2683 0, G_MAXINT, 0, 0));
2684 _gtk_style_property_register (g_param_spec_boxed ("margin",
2687 GTK_TYPE_BORDER, 0),
2696 gtk_style_properties_register_property (NULL,
2697 g_param_spec_int ("padding-top",
2700 0, G_MAXINT, 0, 0));
2701 gtk_style_properties_register_property (NULL,
2702 g_param_spec_int ("padding-left",
2705 0, G_MAXINT, 0, 0));
2706 gtk_style_properties_register_property (NULL,
2707 g_param_spec_int ("padding-bottom",
2709 "Padding at bottom",
2710 0, G_MAXINT, 0, 0));
2711 gtk_style_properties_register_property (NULL,
2712 g_param_spec_int ("padding-right",
2715 0, G_MAXINT, 0, 0));
2716 _gtk_style_property_register (g_param_spec_boxed ("padding",
2719 GTK_TYPE_BORDER, 0),
2728 gtk_style_properties_register_property (NULL,
2729 g_param_spec_int ("border-top-width",
2731 "Border width at top",
2732 0, G_MAXINT, 0, 0));
2733 gtk_style_properties_register_property (NULL,
2734 g_param_spec_int ("border-left-width",
2735 "border left width",
2736 "Border width at left",
2737 0, G_MAXINT, 0, 0));
2738 gtk_style_properties_register_property (NULL,
2739 g_param_spec_int ("border-bottom-width",
2740 "border bottom width",
2741 "Border width at bottom",
2742 0, G_MAXINT, 0, 0));
2743 gtk_style_properties_register_property (NULL,
2744 g_param_spec_int ("border-right-width",
2745 "border right width",
2746 "Border width at right",
2747 0, G_MAXINT, 0, 0));
2748 _gtk_style_property_register (g_param_spec_boxed ("border-width",
2750 "Border width, in pixels",
2751 GTK_TYPE_BORDER, 0),
2754 unpack_border_width,
2759 unset_border_width);
2761 _gtk_style_property_register (g_param_spec_boxed ("border-top-left-radius",
2762 "Border top left radius",
2763 "Border radius of top left corner, in pixels",
2764 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2769 border_corner_radius_value_parse,
2770 border_corner_radius_value_print,
2773 _gtk_style_property_register (g_param_spec_boxed ("border-top-right-radius",
2774 "Border top right radius",
2775 "Border radius of top right corner, in pixels",
2776 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2781 border_corner_radius_value_parse,
2782 border_corner_radius_value_print,
2785 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-right-radius",
2786 "Border bottom right radius",
2787 "Border radius of bottom right corner, in pixels",
2788 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2793 border_corner_radius_value_parse,
2794 border_corner_radius_value_print,
2797 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-left-radius",
2798 "Border bottom left radius",
2799 "Border radius of bottom left corner, in pixels",
2800 GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
2805 border_corner_radius_value_parse,
2806 border_corner_radius_value_print,
2809 _gtk_style_property_register (g_param_spec_int ("border-radius",
2811 "Border radius, in pixels",
2815 unpack_border_radius,
2817 border_radius_value_parse,
2818 border_radius_value_print,
2820 unset_border_radius);
2822 gtk_style_properties_register_property (NULL,
2823 g_param_spec_enum ("border-style",
2826 GTK_TYPE_BORDER_STYLE,
2827 GTK_BORDER_STYLE_NONE, 0));
2828 _gtk_style_property_register (g_param_spec_boxed ("border-top-color",
2836 border_color_value_parse,
2838 border_color_default_value,
2840 _gtk_style_property_register (g_param_spec_boxed ("border-right-color",
2841 "Border right color",
2842 "Border right color",
2848 border_color_value_parse,
2850 border_color_default_value,
2852 _gtk_style_property_register (g_param_spec_boxed ("border-bottom-color",
2853 "Border bottom color",
2854 "Border bottom color",
2860 border_color_value_parse,
2862 border_color_default_value,
2864 _gtk_style_property_register (g_param_spec_boxed ("border-left-color",
2865 "Border left color",
2866 "Border left color",
2872 border_color_value_parse,
2874 border_color_default_value,
2876 _gtk_style_property_register (g_param_spec_boxed ("border-color",
2882 unpack_border_color,
2884 border_color_shorthand_value_parse,
2887 unset_border_color);
2889 gtk_style_properties_register_property (NULL,
2890 g_param_spec_boxed ("background-image",
2893 CAIRO_GOBJECT_TYPE_PATTERN, 0));
2894 gtk_style_properties_register_property (NULL,
2895 g_param_spec_boxed ("border-image-source",
2896 "Border image source",
2897 "Border image source",
2898 CAIRO_GOBJECT_TYPE_PATTERN, 0));
2899 gtk_style_properties_register_property (NULL,
2900 g_param_spec_boxed ("border-image-repeat",
2901 "Border image repeat",
2902 "Border image repeat",
2903 GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
2904 gtk_style_properties_register_property (NULL,
2905 g_param_spec_boxed ("border-image-slice",
2906 "Border image slice",
2907 "Border image slice",
2908 GTK_TYPE_BORDER, 0));
2909 _gtk_style_property_register (g_param_spec_boxed ("border-image-width",
2910 "Border image width",
2911 "Border image width",
2912 GTK_TYPE_BORDER, 0),
2919 border_image_width_default_value,
2921 _gtk_style_property_register (g_param_spec_boxed ("border-image",
2924 GTK_TYPE_BORDER_IMAGE, 0),
2927 _gtk_border_image_unpack,
2928 _gtk_border_image_pack,
2932 unset_border_image);
2933 gtk_style_properties_register_property (NULL,
2934 g_param_spec_object ("engine",
2937 GTK_TYPE_THEMING_ENGINE, 0));
2938 gtk_style_properties_register_property (NULL,
2939 g_param_spec_boxed ("transition",
2940 "Transition animation description",
2941 "Transition animation description",
2942 GTK_TYPE_ANIMATION_DESCRIPTION, 0));
2944 /* Private property holding the binding sets */
2945 _gtk_style_property_register (g_param_spec_boxed ("gtk-key-bindings",
2948 G_TYPE_PTR_ARRAY, 0),
2953 bindings_value_parse,
2954 bindings_value_print,
2959 const GtkStyleProperty *
2960 _gtk_style_property_lookup (const char *name)
2962 gtk_style_property_init ();
2964 return g_hash_table_lookup (properties, name);
2968 _gtk_style_property_register (GParamSpec *pspec,
2969 GtkStylePropertyFlags flags,
2970 GtkStylePropertyParser property_parse_func,
2971 GtkStyleUnpackFunc unpack_func,
2972 GtkStylePackFunc pack_func,
2973 GtkStyleParseFunc parse_func,
2974 GtkStylePrintFunc print_func,
2975 GtkStyleDefaultValueFunc default_value_func,
2976 GtkStyleUnsetFunc unset_func)
2978 const GtkStyleProperty *existing;
2979 GtkStyleProperty *node;
2981 g_return_if_fail ((pack_func == NULL) == (unpack_func == NULL));
2983 gtk_style_property_init ();
2985 existing = _gtk_style_property_lookup (pspec->name);
2986 if (existing != NULL)
2988 g_warning ("Property \"%s\" was already registered with type %s",
2989 pspec->name, g_type_name (existing->pspec->value_type));
2993 node = g_slice_new0 (GtkStyleProperty);
2994 node->flags = flags;
2995 node->pspec = pspec;
2996 node->property_parse_func = property_parse_func;
2997 node->pack_func = pack_func;
2998 node->unpack_func = unpack_func;
2999 node->parse_func = parse_func;
3000 node->print_func = print_func;
3001 node->default_value_func = default_value_func;
3002 node->unset_func = unset_func;
3004 g_hash_table_insert (properties, pspec->name, node);