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 void (* GtkStyleComputeFunc) (GValue *computed,
58 GtkStyleContext *context,
59 const GValue *specified);
62 register_conversion_function (GType type,
63 GtkStyleParseFunc parse,
64 GtkStylePrintFunc print,
65 GtkStyleComputeFunc compute)
68 g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse);
70 g_hash_table_insert (print_funcs, GSIZE_TO_POINTER (type), print);
72 g_hash_table_insert (compute_funcs, GSIZE_TO_POINTER (type), compute);
76 string_append_double (GString *string,
79 char buf[G_ASCII_DTOSTR_BUF_SIZE];
81 g_ascii_dtostr (buf, sizeof (buf), d);
82 g_string_append (string, buf);
86 string_append_string (GString *str,
91 g_string_append_c (str, '"');
94 len = strcspn (string, "\"\n\r\f");
95 g_string_append (str, string);
102 g_string_append (str, "\\A ");
105 g_string_append (str, "\\D ");
108 g_string_append (str, "\\C ");
111 g_string_append (str, "\\\"");
114 g_assert_not_reached ();
119 g_string_append_c (str, '"');
122 /*** IMPLEMENTATIONS ***/
125 enum_parse (GtkCssParser *parser,
131 if (_gtk_css_parser_try_enum (parser, type, res))
134 str = _gtk_css_parser_try_ident (parser, TRUE);
137 _gtk_css_parser_error (parser, "Expected an identifier");
141 _gtk_css_parser_error (parser,
142 "Unknown value '%s' for enum type '%s'",
143 str, g_type_name (type));
150 enum_print (int value,
154 GEnumClass *enum_class;
155 GEnumValue *enum_value;
157 enum_class = g_type_class_ref (type);
158 enum_value = g_enum_get_value (enum_class, value);
160 g_string_append (string, enum_value->value_nick);
162 g_type_class_unref (enum_class);
166 rgba_value_parse (GtkCssParser *parser,
170 GtkSymbolicColor *symbolic;
173 if (_gtk_css_parser_try (parser, "currentcolor", TRUE))
175 symbolic = gtk_symbolic_color_ref (_gtk_symbolic_color_get_current_color ());
179 symbolic = _gtk_css_parser_read_symbolic_color (parser);
180 if (symbolic == NULL)
184 if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
186 g_value_set_boxed (value, &rgba);
187 gtk_symbolic_color_unref (symbolic);
191 g_value_unset (value);
192 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
193 g_value_take_boxed (value, symbolic);
200 rgba_value_print (const GValue *value,
203 const GdkRGBA *rgba = g_value_get_boxed (value);
206 g_string_append (string, "none");
209 char *s = gdk_rgba_to_string (rgba);
210 g_string_append (string, s);
216 rgba_value_compute (GValue *computed,
217 GtkStyleContext *context,
218 const GValue *specified)
220 GdkRGBA rgba, white = { 1, 1, 1, 1 };
222 if (G_VALUE_HOLDS (specified, GTK_TYPE_CSS_SPECIAL_VALUE))
225 else if (G_VALUE_HOLDS (specified, GTK_TYPE_SYMBOLIC_COLOR))
227 GtkSymbolicColor *symbolic = g_value_get_boxed (specified);
229 if (symbolic == _gtk_symbolic_color_get_current_color ())
230 g_value_copy (_gtk_style_context_peek_property (context, "color"), computed);
231 else if (_gtk_style_context_resolve_color (context,
234 g_value_set_boxed (computed, &rgba);
236 g_value_set_boxed (computed, &white);
239 g_value_copy (specified, computed);
243 color_value_parse (GtkCssParser *parser,
247 GtkSymbolicColor *symbolic;
250 symbolic = _gtk_css_parser_read_symbolic_color (parser);
251 if (symbolic == NULL)
254 if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
258 color.red = rgba.red * 65535. + 0.5;
259 color.green = rgba.green * 65535. + 0.5;
260 color.blue = rgba.blue * 65535. + 0.5;
262 g_value_set_boxed (value, &color);
266 g_value_unset (value);
267 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
268 g_value_take_boxed (value, symbolic);
275 color_value_print (const GValue *value,
278 const GdkColor *color = g_value_get_boxed (value);
281 g_string_append (string, "none");
284 char *s = gdk_color_to_string (color);
285 g_string_append (string, s);
291 color_value_compute (GValue *computed,
292 GtkStyleContext *context,
293 const GValue *specified)
296 GdkColor color = { 0, 65535, 65535, 65535 };
298 if (G_VALUE_HOLDS (specified, GTK_TYPE_SYMBOLIC_COLOR))
300 if (_gtk_style_context_resolve_color (context,
301 g_value_get_boxed (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 g_value_set_boxed (computed, &color);
312 g_value_copy (specified, computed);
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 (GValue *computed,
882 GtkStyleContext *context,
883 const GValue *specified)
885 if (G_VALUE_HOLDS (specified, GTK_TYPE_GRADIENT))
887 cairo_pattern_t *gradient;
889 gradient = gtk_gradient_resolve_for_context (g_value_get_boxed (specified), context);
891 g_value_take_boxed (computed, gradient);
894 g_value_copy (specified, computed);
898 shadow_value_parse (GtkCssParser *parser,
902 gboolean have_inset, have_color, have_lengths;
903 gdouble hoffset, voffset, blur, spread;
904 GtkSymbolicColor *color;
908 if (_gtk_css_parser_try (parser, "none", TRUE))
911 shadow = _gtk_shadow_new ();
915 have_inset = have_lengths = have_color = FALSE;
917 for (i = 0; i < 3; i++)
920 _gtk_css_parser_try (parser, "inset", TRUE))
927 _gtk_css_parser_try_double (parser, &hoffset))
931 if (!_gtk_css_parser_try_double (parser, &voffset))
933 _gtk_css_parser_error (parser, "Horizontal and vertical offsets are required");
934 _gtk_shadow_unref (shadow);
938 if (!_gtk_css_parser_try_double (parser, &blur))
941 if (!_gtk_css_parser_try_double (parser, &spread))
951 /* XXX: the color is optional and UA-defined if it's missing,
952 * but it doesn't really make sense for us...
954 color = _gtk_css_parser_read_symbolic_color (parser);
958 _gtk_shadow_unref (shadow);
964 if (!have_color || !have_lengths)
966 _gtk_css_parser_error (parser, "Must specify at least color and offsets");
967 _gtk_shadow_unref (shadow);
971 _gtk_shadow_append (shadow,
976 gtk_symbolic_color_unref (color);
979 while (_gtk_css_parser_try (parser, ",", TRUE));
981 g_value_take_boxed (value, shadow);
986 shadow_value_print (const GValue *value,
991 shadow = g_value_get_boxed (value);
994 g_string_append (string, "none");
996 _gtk_shadow_print (shadow, string);
1000 shadow_value_compute (GValue *computed,
1001 GtkStyleContext *context,
1002 const GValue *specified)
1006 shadow = g_value_get_boxed (specified);
1008 shadow = _gtk_shadow_resolve (shadow, context);
1010 g_value_take_boxed (computed, shadow);
1014 border_image_repeat_value_parse (GtkCssParser *parser,
1018 GtkCssBorderImageRepeat image_repeat;
1019 GtkCssBorderRepeatStyle styles[2];
1022 for (i = 0; i < 2; i++)
1024 if (_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, &v))
1028 styles[1] = styles[0] = GTK_CSS_REPEAT_STYLE_STRETCH;
1032 styles[i] = styles[0];
1035 image_repeat.hrepeat = styles[0];
1036 image_repeat.vrepeat = styles[1];
1038 g_value_set_boxed (value, &image_repeat);
1044 border_image_repeat_value_print (const GValue *value,
1047 GtkCssBorderImageRepeat *image_repeat;
1049 image_repeat = g_value_get_boxed (value);
1051 enum_print (image_repeat->hrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1052 if (image_repeat->hrepeat != image_repeat->vrepeat)
1054 g_string_append (string, " ");
1055 enum_print (image_repeat->vrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1060 css_number_print (const GValue *value,
1063 _gtk_css_number_print (g_value_get_boxed (value), string);
1067 enum_value_parse (GtkCssParser *parser,
1073 if (enum_parse (parser, G_VALUE_TYPE (value), &v))
1075 g_value_set_enum (value, v);
1083 enum_value_print (const GValue *value,
1086 enum_print (g_value_get_enum (value), G_VALUE_TYPE (value), string);
1090 flags_value_parse (GtkCssParser *parser,
1094 GFlagsClass *flags_class;
1095 GFlagsValue *flag_value;
1099 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1102 str = _gtk_css_parser_try_ident (parser, TRUE);
1105 _gtk_css_parser_error (parser, "Expected an identifier");
1106 g_type_class_unref (flags_class);
1110 flag_value = g_flags_get_value_by_nick (flags_class, str);
1113 _gtk_css_parser_error (parser,
1114 "Unknown flag value '%s' for type '%s'",
1115 str, g_type_name (G_VALUE_TYPE (value)));
1116 /* XXX Do we want to return FALSE here? We can get
1117 * forward-compatibility for new values this way
1120 g_type_class_unref (flags_class);
1126 while (_gtk_css_parser_try (parser, ",", FALSE));
1128 g_type_class_unref (flags_class);
1130 g_value_set_enum (value, flags);
1136 flags_value_print (const GValue *value,
1139 GFlagsClass *flags_class;
1142 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1143 flags = g_value_get_flags (value);
1145 for (i = 0; i < flags_class->n_values; i++)
1147 GFlagsValue *flags_value = &flags_class->values[i];
1149 if (flags & flags_value->value)
1151 if (string->len != 0)
1152 g_string_append (string, ", ");
1154 g_string_append (string, flags_value->value_nick);
1158 g_type_class_unref (flags_class);
1164 gtk_css_style_funcs_init (void)
1166 if (G_LIKELY (parse_funcs != NULL))
1169 parse_funcs = g_hash_table_new (NULL, NULL);
1170 print_funcs = g_hash_table_new (NULL, NULL);
1171 compute_funcs = g_hash_table_new (NULL, NULL);
1173 register_conversion_function (GDK_TYPE_RGBA,
1176 rgba_value_compute);
1177 register_conversion_function (GDK_TYPE_COLOR,
1180 color_value_compute);
1181 register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
1182 symbolic_color_value_parse,
1183 symbolic_color_value_print,
1185 register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
1186 font_description_value_parse,
1187 font_description_value_print,
1189 register_conversion_function (G_TYPE_BOOLEAN,
1190 boolean_value_parse,
1191 boolean_value_print,
1193 register_conversion_function (G_TYPE_INT,
1197 register_conversion_function (G_TYPE_UINT,
1201 register_conversion_function (G_TYPE_DOUBLE,
1205 register_conversion_function (G_TYPE_FLOAT,
1209 register_conversion_function (G_TYPE_STRING,
1213 register_conversion_function (GTK_TYPE_THEMING_ENGINE,
1214 theming_engine_value_parse,
1215 theming_engine_value_print,
1217 register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
1218 animation_description_value_parse,
1219 animation_description_value_print,
1221 register_conversion_function (GTK_TYPE_BORDER,
1225 register_conversion_function (GTK_TYPE_GRADIENT,
1226 gradient_value_parse,
1227 gradient_value_print,
1229 register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
1230 pattern_value_parse,
1231 pattern_value_print,
1232 pattern_value_compute);
1233 register_conversion_function (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
1234 border_image_repeat_value_parse,
1235 border_image_repeat_value_print,
1237 register_conversion_function (GTK_TYPE_SHADOW,
1240 shadow_value_compute);
1241 register_conversion_function (GTK_TYPE_CSS_NUMBER,
1245 register_conversion_function (G_TYPE_ENUM,
1249 register_conversion_function (G_TYPE_FLAGS,
1256 * _gtk_css_style_parse_value:
1257 * @value: the value to parse into. Must be a valid initialized #GValue
1258 * @parser: the parser to parse from
1259 * @base: the base URL for @parser
1261 * This is the generic parsing function used for CSS values. If the
1262 * function fails to parse a value, it will emit an error on @parser,
1263 * return %FALSE and not touch @value.
1265 * Returns: %TRUE if parsing succeeded.
1268 _gtk_css_style_parse_value (GValue *value,
1269 GtkCssParser *parser,
1272 GtkStyleParseFunc func;
1274 g_return_val_if_fail (value != NULL, FALSE);
1275 g_return_val_if_fail (parser != NULL, FALSE);
1277 gtk_css_style_funcs_init ();
1279 func = g_hash_table_lookup (parse_funcs,
1280 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
1282 func = g_hash_table_lookup (parse_funcs,
1283 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
1287 _gtk_css_parser_error (parser,
1288 "Cannot convert to type '%s'",
1289 g_type_name (G_VALUE_TYPE (value)));
1293 return (*func) (parser, base, value);
1297 * _gtk_css_style_print_value:
1298 * @value: an initialized GValue returned from _gtk_css_style_parse()
1299 * @string: the string to print into
1301 * Prints @value into @string as a CSS value. If @value is not a
1302 * valid value, a random string will be printed instead.
1305 _gtk_css_style_print_value (const GValue *value,
1308 GtkStylePrintFunc func;
1310 gtk_css_style_funcs_init ();
1312 func = g_hash_table_lookup (print_funcs,
1313 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
1315 func = g_hash_table_lookup (print_funcs,
1316 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
1320 char *s = g_strdup_value_contents (value);
1321 g_string_append (string, s);
1326 func (value, string);
1330 * _gtk_css_style_compute_value:
1331 * @computed: (out): a value to be filled with the result
1332 * @context: the context to use for computing the value
1333 * @specified: the value to use for the computation
1335 * Converts the @specified value into the @computed value using the
1336 * information in @context. The values must have matching types, ie
1337 * @specified must be a result of a call to
1338 * _gtk_css_style_parse_value() with the same type as @computed.
1341 _gtk_css_style_compute_value (GValue *computed,
1342 GtkStyleContext *context,
1343 const GValue *specified)
1345 GtkStyleComputeFunc func;
1347 g_return_if_fail (G_IS_VALUE (computed));
1348 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1349 g_return_if_fail (G_IS_VALUE (specified));
1351 gtk_css_style_funcs_init ();
1353 func = g_hash_table_lookup (compute_funcs,
1354 GSIZE_TO_POINTER (G_VALUE_TYPE (computed)));
1356 func = g_hash_table_lookup (compute_funcs,
1357 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (computed))));
1360 func (computed, context, specified);
1362 g_value_copy (specified, computed);