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, see <http://www.gnu.org/licenses/>.
20 #include "gtkcssstylefuncsprivate.h"
27 #include <gdk-pixbuf/gdk-pixbuf.h>
28 #include <cairo-gobject.h>
30 #include "gtkanimationdescription.h"
31 #include "gtkcssimagegradientprivate.h"
32 #include "gtkcssprovider.h"
33 #include "gtkcsstypesprivate.h"
34 #include "gtkgradient.h"
35 #include "gtkprivatetypebuiltins.h"
36 #include "gtkshadowprivate.h"
37 #include "gtkstylecontextprivate.h"
38 #include "gtksymboliccolorprivate.h"
39 #include "gtkthemingengine.h"
40 #include "gtktypebuiltins.h"
41 #include "gtkwin32themeprivate.h"
43 /* this is in case round() is not provided by the compiler,
44 * such as in the case of C89 compilers, like MSVC
46 #include "fallback-c89.c"
48 static GHashTable *parse_funcs = NULL;
49 static GHashTable *print_funcs = NULL;
50 static GHashTable *compute_funcs = NULL;
52 typedef gboolean (* GtkStyleParseFunc) (GtkCssParser *parser,
55 typedef void (* GtkStylePrintFunc) (const GValue *value,
57 typedef GtkCssValue * (* GtkStyleComputeFunc) (GtkStyleContext *context,
58 GtkCssValue *specified);
61 register_conversion_function (GType type,
62 GtkStyleParseFunc parse,
63 GtkStylePrintFunc print,
64 GtkStyleComputeFunc compute)
67 g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse);
69 g_hash_table_insert (print_funcs, GSIZE_TO_POINTER (type), print);
71 g_hash_table_insert (compute_funcs, GSIZE_TO_POINTER (type), compute);
75 string_append_double (GString *string,
78 char buf[G_ASCII_DTOSTR_BUF_SIZE];
80 g_ascii_dtostr (buf, sizeof (buf), d);
81 g_string_append (string, buf);
85 string_append_string (GString *str,
90 g_string_append_c (str, '"');
93 len = strcspn (string, "\"\n\r\f");
94 g_string_append (str, string);
101 g_string_append (str, "\\A ");
104 g_string_append (str, "\\D ");
107 g_string_append (str, "\\C ");
110 g_string_append (str, "\\\"");
113 g_assert_not_reached ();
118 g_string_append_c (str, '"');
121 /*** IMPLEMENTATIONS ***/
124 enum_parse (GtkCssParser *parser,
130 if (_gtk_css_parser_try_enum (parser, type, res))
133 str = _gtk_css_parser_try_ident (parser, TRUE);
136 _gtk_css_parser_error (parser, "Expected an identifier");
140 _gtk_css_parser_error (parser,
141 "Unknown value '%s' for enum type '%s'",
142 str, g_type_name (type));
149 enum_print (int value,
153 GEnumClass *enum_class;
154 GEnumValue *enum_value;
156 enum_class = g_type_class_ref (type);
157 enum_value = g_enum_get_value (enum_class, value);
159 g_string_append (string, enum_value->value_nick);
161 g_type_class_unref (enum_class);
165 rgba_value_parse (GtkCssParser *parser,
169 GtkSymbolicColor *symbolic;
172 if (_gtk_css_parser_try (parser, "currentcolor", TRUE))
174 symbolic = gtk_symbolic_color_ref (_gtk_symbolic_color_get_current_color ());
178 symbolic = _gtk_css_parser_read_symbolic_color (parser);
179 if (symbolic == NULL)
183 if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
185 g_value_set_boxed (value, &rgba);
186 gtk_symbolic_color_unref (symbolic);
190 g_value_unset (value);
191 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
192 g_value_take_boxed (value, symbolic);
199 rgba_value_print (const GValue *value,
202 const GdkRGBA *rgba = g_value_get_boxed (value);
205 g_string_append (string, "none");
208 char *s = gdk_rgba_to_string (rgba);
209 g_string_append (string, s);
215 rgba_value_compute (GtkStyleContext *context,
216 GtkCssValue *specified)
218 GdkRGBA white = { 1, 1, 1, 1 };
221 if (_gtk_css_value_holds (specified, GTK_TYPE_CSS_SPECIAL_VALUE))
223 return _gtk_css_value_new_from_rgba (NULL);
225 else if (_gtk_css_value_holds (specified, GTK_TYPE_SYMBOLIC_COLOR))
227 GtkSymbolicColor *symbolic = _gtk_css_value_get_symbolic_color (specified);
229 if (symbolic == _gtk_symbolic_color_get_current_color ())
230 return _gtk_css_value_ref (_gtk_style_context_peek_property (context, "color"));
232 res = _gtk_style_context_resolve_color_value (context, symbolic);
236 return _gtk_css_value_new_from_rgba (&white);
240 return _gtk_css_value_ref (specified);
244 color_value_parse (GtkCssParser *parser,
248 GtkSymbolicColor *symbolic;
251 symbolic = _gtk_css_parser_read_symbolic_color (parser);
252 if (symbolic == NULL)
255 if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
259 color.red = rgba.red * 65535. + 0.5;
260 color.green = rgba.green * 65535. + 0.5;
261 color.blue = rgba.blue * 65535. + 0.5;
263 g_value_set_boxed (value, &color);
267 g_value_unset (value);
268 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
269 g_value_take_boxed (value, symbolic);
276 color_value_print (const GValue *value,
279 const GdkColor *color = g_value_get_boxed (value);
282 g_string_append (string, "none");
285 char *s = gdk_color_to_string (color);
286 g_string_append (string, s);
292 color_value_compute (GtkStyleContext *context,
293 GtkCssValue *specified)
296 GdkColor color = { 0, 65535, 65535, 65535 };
298 if (_gtk_css_value_holds (specified, GTK_TYPE_SYMBOLIC_COLOR))
300 if (_gtk_style_context_resolve_color (context,
301 _gtk_css_value_get_symbolic_color (specified),
304 color.red = rgba.red * 65535. + 0.5;
305 color.green = rgba.green * 65535. + 0.5;
306 color.blue = rgba.blue * 65535. + 0.5;
309 return _gtk_css_value_new_from_color (&color);
312 return _gtk_css_value_ref (specified);
316 symbolic_color_value_parse (GtkCssParser *parser,
320 GtkSymbolicColor *symbolic;
322 if (_gtk_css_parser_try (parser, "currentcolor", TRUE))
324 symbolic = gtk_symbolic_color_ref (_gtk_symbolic_color_get_current_color ());
328 symbolic = _gtk_css_parser_read_symbolic_color (parser);
329 if (symbolic == NULL)
333 g_value_take_boxed (value, symbolic);
338 symbolic_color_value_print (const GValue *value,
341 GtkSymbolicColor *symbolic = g_value_get_boxed (value);
343 if (symbolic == NULL)
344 g_string_append (string, "none");
347 char *s = gtk_symbolic_color_to_string (symbolic);
348 g_string_append (string, s);
354 font_description_value_parse (GtkCssParser *parser,
358 PangoFontDescription *font_desc;
362 str = _gtk_css_parser_read_value (parser);
366 font_desc = pango_font_description_from_string (str);
367 mask = pango_font_description_get_set_fields (font_desc);
368 /* These values are not really correct,
369 * but the fields must be set, so we set them to something */
370 if ((mask & PANGO_FONT_MASK_FAMILY) == 0)
371 pango_font_description_set_family_static (font_desc, "Sans");
372 if ((mask & PANGO_FONT_MASK_SIZE) == 0)
373 pango_font_description_set_size (font_desc, 10 * PANGO_SCALE);
375 g_value_take_boxed (value, font_desc);
380 font_description_value_print (const GValue *value,
383 const PangoFontDescription *desc = g_value_get_boxed (value);
386 g_string_append (string, "none");
389 char *s = pango_font_description_to_string (desc);
390 g_string_append (string, s);
396 boolean_value_parse (GtkCssParser *parser,
400 if (_gtk_css_parser_try (parser, "true", TRUE) ||
401 _gtk_css_parser_try (parser, "1", TRUE))
403 g_value_set_boolean (value, TRUE);
406 else if (_gtk_css_parser_try (parser, "false", TRUE) ||
407 _gtk_css_parser_try (parser, "0", TRUE))
409 g_value_set_boolean (value, FALSE);
414 _gtk_css_parser_error (parser, "Expected a boolean value");
420 boolean_value_print (const GValue *value,
423 if (g_value_get_boolean (value))
424 g_string_append (string, "true");
426 g_string_append (string, "false");
430 int_value_parse (GtkCssParser *parser,
436 if (_gtk_css_parser_begins_with (parser, '-'))
438 int res = _gtk_win32_theme_int_parse (parser, base, &i);
441 g_value_set_int (value, i);
444 /* < 0 => continue */
447 if (!_gtk_css_parser_try_int (parser, &i))
449 _gtk_css_parser_error (parser, "Expected a valid integer value");
453 g_value_set_int (value, i);
458 int_value_print (const GValue *value,
461 g_string_append_printf (string, "%d", g_value_get_int (value));
465 uint_value_parse (GtkCssParser *parser,
471 if (!_gtk_css_parser_try_uint (parser, &u))
473 _gtk_css_parser_error (parser, "Expected a valid unsigned value");
477 g_value_set_uint (value, u);
482 uint_value_print (const GValue *value,
485 g_string_append_printf (string, "%u", g_value_get_uint (value));
489 double_value_parse (GtkCssParser *parser,
495 if (!_gtk_css_parser_try_double (parser, &d))
497 _gtk_css_parser_error (parser, "Expected a number");
501 g_value_set_double (value, d);
506 double_value_print (const GValue *value,
509 string_append_double (string, g_value_get_double (value));
513 float_value_parse (GtkCssParser *parser,
519 if (!_gtk_css_parser_try_double (parser, &d))
521 _gtk_css_parser_error (parser, "Expected a number");
525 g_value_set_float (value, d);
530 float_value_print (const GValue *value,
533 string_append_double (string, g_value_get_float (value));
537 string_value_parse (GtkCssParser *parser,
541 char *str = _gtk_css_parser_read_string (parser);
546 g_value_take_string (value, str);
551 string_value_print (const GValue *value,
554 string_append_string (str, g_value_get_string (value));
558 theming_engine_value_parse (GtkCssParser *parser,
562 GtkThemingEngine *engine;
565 if (_gtk_css_parser_try (parser, "none", TRUE))
567 g_value_set_object (value, gtk_theming_engine_load (NULL));
571 str = _gtk_css_parser_try_ident (parser, TRUE);
574 _gtk_css_parser_error (parser, "Expected a valid theme name");
578 engine = gtk_theming_engine_load (str);
582 _gtk_css_parser_error (parser, "Theming engine '%s' not found", str);
587 g_value_set_object (value, engine);
593 theming_engine_value_print (const GValue *value,
596 GtkThemingEngine *engine;
599 engine = g_value_get_object (value);
601 g_string_append (string, "none");
604 /* XXX: gtk_theming_engine_get_name()? */
605 g_object_get (engine, "name", &name, NULL);
606 g_string_append (string, name ? name : "none");
612 animation_description_value_parse (GtkCssParser *parser,
616 GtkAnimationDescription *desc;
619 str = _gtk_css_parser_read_value (parser);
623 desc = _gtk_animation_description_from_string (str);
628 _gtk_css_parser_error (parser, "Invalid animation description");
632 g_value_take_boxed (value, desc);
637 animation_description_value_print (const GValue *value,
640 GtkAnimationDescription *desc = g_value_get_boxed (value);
643 g_string_append (string, "none");
645 _gtk_animation_description_print (desc, string);
649 border_value_parse (GtkCssParser *parser,
653 GtkBorder border = { 0, };
657 for (i = 0; i < G_N_ELEMENTS (numbers); i++)
659 if (_gtk_css_parser_begins_with (parser, '-'))
661 /* These are strictly speaking signed, but we want to be able to use them
662 for unsigned types too, as the actual ranges of values make this safe */
663 int res = _gtk_win32_theme_int_parse (parser, base, &numbers[i]);
665 if (res == 0) /* Parse error, report */
668 if (res < 0) /* Nothing known to expand */
673 if (!_gtk_css_parser_try_length (parser, &numbers[i]))
680 _gtk_css_parser_error (parser, "Expected valid border");
684 border.top = numbers[0];
686 border.right = numbers[1];
688 border.right = border.top;
690 border.bottom = numbers[2];
692 border.bottom = border.top;
694 border.left = numbers[3];
696 border.left = border.right;
698 g_value_set_boxed (value, &border);
703 border_value_print (const GValue *value, GString *string)
705 const GtkBorder *border = g_value_get_boxed (value);
708 g_string_append (string, "none");
709 else if (border->left != border->right)
710 g_string_append_printf (string, "%d %d %d %d", border->top, border->right, border->bottom, border->left);
711 else if (border->top != border->bottom)
712 g_string_append_printf (string, "%d %d %d", border->top, border->right, border->bottom);
713 else if (border->top != border->left)
714 g_string_append_printf (string, "%d %d", border->top, border->right);
716 g_string_append_printf (string, "%d", border->top);
720 gradient_value_parse (GtkCssParser *parser,
724 GtkGradient *gradient;
726 gradient = _gtk_gradient_parse (parser);
727 if (gradient == NULL)
730 g_value_take_boxed (value, gradient);
735 gradient_value_print (const GValue *value,
738 GtkGradient *gradient = g_value_get_boxed (value);
740 if (gradient == NULL)
741 g_string_append (string, "none");
744 char *s = gtk_gradient_to_string (gradient);
745 g_string_append (string, s);
751 pattern_value_parse (GtkCssParser *parser,
755 if (_gtk_css_parser_try (parser, "none", TRUE))
757 /* nothing to do here */
759 else if (_gtk_css_parser_begins_with (parser, '-'))
761 g_value_unset (value);
762 g_value_init (value, GTK_TYPE_GRADIENT);
763 return gradient_value_parse (parser, base, value);
767 GError *error = NULL;
771 cairo_surface_t *surface;
772 cairo_pattern_t *pattern;
774 cairo_matrix_t matrix;
776 file = _gtk_css_parser_read_url (parser, base);
780 path = g_file_get_path (file);
781 g_object_unref (file);
783 pixbuf = gdk_pixbuf_new_from_file (path, &error);
787 _gtk_css_parser_take_error (parser, error);
791 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
792 gdk_pixbuf_get_width (pixbuf),
793 gdk_pixbuf_get_height (pixbuf));
794 cr = cairo_create (surface);
795 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
797 pattern = cairo_pattern_create_for_surface (surface);
799 cairo_matrix_init_scale (&matrix,
800 gdk_pixbuf_get_width (pixbuf),
801 gdk_pixbuf_get_height (pixbuf));
802 cairo_pattern_set_matrix (pattern, &matrix);
804 cairo_surface_destroy (surface);
806 g_object_unref (pixbuf);
808 g_value_take_boxed (value, pattern);
814 static cairo_status_t
815 surface_write (void *closure,
816 const unsigned char *data,
819 g_byte_array_append (closure, data, length);
821 return CAIRO_STATUS_SUCCESS;
825 surface_print (cairo_surface_t *surface,
828 #if CAIRO_HAS_PNG_FUNCTIONS
832 array = g_byte_array_new ();
833 cairo_surface_write_to_png_stream (surface, surface_write, array);
834 base64 = g_base64_encode (array->data, array->len);
835 g_byte_array_free (array, TRUE);
837 g_string_append (string, "url(\"data:image/png;base64,");
838 g_string_append (string, base64);
839 g_string_append (string, "\")");
843 g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
848 pattern_value_print (const GValue *value,
851 cairo_pattern_t *pattern;
852 cairo_surface_t *surface;
854 pattern = g_value_get_boxed (value);
858 g_string_append (string, "none");
862 switch (cairo_pattern_get_type (pattern))
864 case CAIRO_PATTERN_TYPE_SURFACE:
865 if (cairo_pattern_get_surface (pattern, &surface) != CAIRO_STATUS_SUCCESS)
867 g_assert_not_reached ();
869 surface_print (surface, string);
871 case CAIRO_PATTERN_TYPE_SOLID:
872 case CAIRO_PATTERN_TYPE_LINEAR:
873 case CAIRO_PATTERN_TYPE_RADIAL:
875 g_assert_not_reached ();
881 pattern_value_compute (GtkStyleContext *context,
882 GtkCssValue *specified)
884 if (_gtk_css_value_holds (specified, GTK_TYPE_GRADIENT))
886 cairo_pattern_t *gradient;
888 gradient = gtk_gradient_resolve_for_context (_gtk_css_value_get_gradient (specified), context);
890 return _gtk_css_value_new_take_pattern (gradient);
893 return _gtk_css_value_ref (specified);
897 shadow_value_parse (GtkCssParser *parser,
901 gboolean have_inset, have_color, have_lengths;
902 gdouble hoffset, voffset, blur, spread;
903 GtkSymbolicColor *color;
907 if (_gtk_css_parser_try (parser, "none", TRUE))
910 shadow = _gtk_shadow_new ();
914 have_inset = have_lengths = have_color = FALSE;
916 for (i = 0; i < 3; i++)
919 _gtk_css_parser_try (parser, "inset", TRUE))
926 _gtk_css_parser_try_double (parser, &hoffset))
930 if (!_gtk_css_parser_try_double (parser, &voffset))
932 _gtk_css_parser_error (parser, "Horizontal and vertical offsets are required");
933 _gtk_shadow_unref (shadow);
937 if (!_gtk_css_parser_try_double (parser, &blur))
940 if (!_gtk_css_parser_try_double (parser, &spread))
950 /* XXX: the color is optional and UA-defined if it's missing,
951 * but it doesn't really make sense for us...
953 color = _gtk_css_parser_read_symbolic_color (parser);
957 _gtk_shadow_unref (shadow);
963 if (!have_color || !have_lengths)
965 _gtk_css_parser_error (parser, "Must specify at least color and offsets");
966 _gtk_shadow_unref (shadow);
970 _gtk_shadow_append (shadow,
975 gtk_symbolic_color_unref (color);
978 while (_gtk_css_parser_try (parser, ",", TRUE));
980 g_value_take_boxed (value, shadow);
985 shadow_value_print (const GValue *value,
990 shadow = g_value_get_boxed (value);
993 g_string_append (string, "none");
995 _gtk_shadow_print (shadow, string);
999 shadow_value_compute (GtkStyleContext *context,
1000 GtkCssValue *specified)
1004 shadow = _gtk_css_value_get_shadow (specified);
1006 shadow = _gtk_shadow_resolve (shadow, context);
1008 return _gtk_css_value_new_take_shadow (shadow);
1012 border_image_repeat_value_parse (GtkCssParser *parser,
1016 GtkCssBorderImageRepeat image_repeat;
1017 GtkCssBorderRepeatStyle styles[2];
1020 for (i = 0; i < 2; i++)
1022 if (_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, &v))
1026 styles[1] = styles[0] = GTK_CSS_REPEAT_STYLE_STRETCH;
1030 styles[i] = styles[0];
1033 image_repeat.hrepeat = styles[0];
1034 image_repeat.vrepeat = styles[1];
1036 g_value_set_boxed (value, &image_repeat);
1042 border_image_repeat_value_print (const GValue *value,
1045 GtkCssBorderImageRepeat *image_repeat;
1047 image_repeat = g_value_get_boxed (value);
1049 enum_print (image_repeat->hrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1050 if (image_repeat->hrepeat != image_repeat->vrepeat)
1052 g_string_append (string, " ");
1053 enum_print (image_repeat->vrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1058 css_number_print (const GValue *value,
1061 _gtk_css_number_print (g_value_get_boxed (value), string);
1065 enum_value_parse (GtkCssParser *parser,
1071 if (enum_parse (parser, G_VALUE_TYPE (value), &v))
1073 g_value_set_enum (value, v);
1081 enum_value_print (const GValue *value,
1084 enum_print (g_value_get_enum (value), G_VALUE_TYPE (value), string);
1088 flags_value_parse (GtkCssParser *parser,
1092 GFlagsClass *flags_class;
1093 GFlagsValue *flag_value;
1097 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1100 str = _gtk_css_parser_try_ident (parser, TRUE);
1103 _gtk_css_parser_error (parser, "Expected an identifier");
1104 g_type_class_unref (flags_class);
1108 flag_value = g_flags_get_value_by_nick (flags_class, str);
1111 _gtk_css_parser_error (parser,
1112 "Unknown flag value '%s' for type '%s'",
1113 str, g_type_name (G_VALUE_TYPE (value)));
1114 /* XXX Do we want to return FALSE here? We can get
1115 * forward-compatibility for new values this way
1118 g_type_class_unref (flags_class);
1124 while (_gtk_css_parser_try (parser, ",", FALSE));
1126 g_type_class_unref (flags_class);
1128 g_value_set_enum (value, flags);
1134 flags_value_print (const GValue *value,
1137 GFlagsClass *flags_class;
1140 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1141 flags = g_value_get_flags (value);
1143 for (i = 0; i < flags_class->n_values; i++)
1145 GFlagsValue *flags_value = &flags_class->values[i];
1147 if (flags & flags_value->value)
1149 if (string->len != 0)
1150 g_string_append (string, ", ");
1152 g_string_append (string, flags_value->value_nick);
1156 g_type_class_unref (flags_class);
1162 gtk_css_style_funcs_init (void)
1164 if (G_LIKELY (parse_funcs != NULL))
1167 parse_funcs = g_hash_table_new (NULL, NULL);
1168 print_funcs = g_hash_table_new (NULL, NULL);
1169 compute_funcs = g_hash_table_new (NULL, NULL);
1171 register_conversion_function (GDK_TYPE_RGBA,
1174 rgba_value_compute);
1175 register_conversion_function (GDK_TYPE_COLOR,
1178 color_value_compute);
1179 register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
1180 symbolic_color_value_parse,
1181 symbolic_color_value_print,
1183 register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
1184 font_description_value_parse,
1185 font_description_value_print,
1187 register_conversion_function (G_TYPE_BOOLEAN,
1188 boolean_value_parse,
1189 boolean_value_print,
1191 register_conversion_function (G_TYPE_INT,
1195 register_conversion_function (G_TYPE_UINT,
1199 register_conversion_function (G_TYPE_DOUBLE,
1203 register_conversion_function (G_TYPE_FLOAT,
1207 register_conversion_function (G_TYPE_STRING,
1211 register_conversion_function (GTK_TYPE_THEMING_ENGINE,
1212 theming_engine_value_parse,
1213 theming_engine_value_print,
1215 register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
1216 animation_description_value_parse,
1217 animation_description_value_print,
1219 register_conversion_function (GTK_TYPE_BORDER,
1223 register_conversion_function (GTK_TYPE_GRADIENT,
1224 gradient_value_parse,
1225 gradient_value_print,
1227 register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
1228 pattern_value_parse,
1229 pattern_value_print,
1230 pattern_value_compute);
1231 register_conversion_function (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
1232 border_image_repeat_value_parse,
1233 border_image_repeat_value_print,
1235 register_conversion_function (GTK_TYPE_SHADOW,
1238 shadow_value_compute);
1239 register_conversion_function (GTK_TYPE_CSS_NUMBER,
1243 register_conversion_function (G_TYPE_ENUM,
1247 register_conversion_function (G_TYPE_FLAGS,
1254 * _gtk_css_style_parse_value:
1255 * @value: the value to parse into. Must be a valid initialized #GValue
1256 * @parser: the parser to parse from
1257 * @base: the base URL for @parser
1259 * This is the generic parsing function used for CSS values. If the
1260 * function fails to parse a value, it will emit an error on @parser,
1261 * return %FALSE and not touch @value.
1263 * Returns: %TRUE if parsing succeeded.
1266 _gtk_css_style_parse_value (GValue *value,
1267 GtkCssParser *parser,
1270 GtkStyleParseFunc func;
1272 g_return_val_if_fail (value != NULL, FALSE);
1273 g_return_val_if_fail (parser != NULL, FALSE);
1275 gtk_css_style_funcs_init ();
1277 func = g_hash_table_lookup (parse_funcs,
1278 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
1280 func = g_hash_table_lookup (parse_funcs,
1281 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
1285 _gtk_css_parser_error (parser,
1286 "Cannot convert to type '%s'",
1287 g_type_name (G_VALUE_TYPE (value)));
1291 return (*func) (parser, base, value);
1295 * _gtk_css_style_print_value:
1296 * @value: an initialized GValue returned from _gtk_css_style_parse()
1297 * @string: the string to print into
1299 * Prints @value into @string as a CSS value. If @value is not a
1300 * valid value, a random string will be printed instead.
1303 _gtk_css_style_print_value (const GValue *value,
1306 GtkStylePrintFunc func;
1308 gtk_css_style_funcs_init ();
1310 func = g_hash_table_lookup (print_funcs,
1311 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
1313 func = g_hash_table_lookup (print_funcs,
1314 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
1318 char *s = g_strdup_value_contents (value);
1319 g_string_append (string, s);
1324 func (value, string);
1328 * _gtk_css_style_compute_value:
1329 * @computed: (out): a value to be filled with the result
1330 * @context: the context to use for computing the value
1331 * @specified: the value to use for the computation
1333 * Converts the @specified value into the @computed value using the
1334 * information in @context. The values must have matching types, ie
1335 * @specified must be a result of a call to
1336 * _gtk_css_style_parse_value() with the same type as @computed.
1339 _gtk_css_style_compute_value (GtkStyleContext *context,
1341 GtkCssValue *specified)
1343 GtkStyleComputeFunc func;
1345 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1347 gtk_css_style_funcs_init ();
1349 func = g_hash_table_lookup (compute_funcs,
1350 GSIZE_TO_POINTER (target_type));
1352 func = g_hash_table_lookup (compute_funcs,
1353 GSIZE_TO_POINTER (g_type_fundamental (target_type)));
1356 return func (context, specified);
1358 return _gtk_css_value_ref (specified);