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 "gtkcssstylefuncsprivate.h"
29 #include <gdk-pixbuf/gdk-pixbuf.h>
30 #include <cairo-gobject.h>
32 #include "gtkanimationdescription.h"
33 #include "gtkcssimagegradientprivate.h"
34 #include "gtkcssprovider.h"
35 #include "gtkcsstypesprivate.h"
36 #include "gtkgradient.h"
37 #include "gtkprivatetypebuiltins.h"
38 #include "gtkshadowprivate.h"
39 #include "gtkstylecontextprivate.h"
40 #include "gtkthemingengine.h"
41 #include "gtktypebuiltins.h"
42 #include "gtkwin32themeprivate.h"
44 /* this is in case round() is not provided by the compiler,
45 * such as in the case of C89 compilers, like MSVC
47 #include "fallback-c89.c"
49 static GHashTable *parse_funcs = NULL;
50 static GHashTable *print_funcs = NULL;
51 static GHashTable *compute_funcs = NULL;
53 typedef gboolean (* GtkStyleParseFunc) (GtkCssParser *parser,
56 typedef void (* GtkStylePrintFunc) (const GValue *value,
58 typedef void (* GtkStylePrintFunc) (const GValue *value,
60 typedef void (* GtkStyleComputeFunc) (GValue *computed,
61 GtkStyleContext *context,
62 const GValue *specified);
65 register_conversion_function (GType type,
66 GtkStyleParseFunc parse,
67 GtkStylePrintFunc print,
68 GtkStyleComputeFunc compute)
71 g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse);
73 g_hash_table_insert (print_funcs, GSIZE_TO_POINTER (type), print);
75 g_hash_table_insert (compute_funcs, GSIZE_TO_POINTER (type), compute);
79 string_append_double (GString *string,
82 char buf[G_ASCII_DTOSTR_BUF_SIZE];
84 g_ascii_dtostr (buf, sizeof (buf), d);
85 g_string_append (string, buf);
89 string_append_string (GString *str,
94 g_string_append_c (str, '"');
97 len = strcspn (string, "\"\n\r\f");
98 g_string_append (str, string);
105 g_string_append (str, "\\A ");
108 g_string_append (str, "\\D ");
111 g_string_append (str, "\\C ");
114 g_string_append (str, "\\\"");
117 g_assert_not_reached ();
122 g_string_append_c (str, '"');
125 /*** IMPLEMENTATIONS ***/
128 enum_parse (GtkCssParser *parser,
134 if (_gtk_css_parser_try_enum (parser, type, res))
137 str = _gtk_css_parser_try_ident (parser, TRUE);
140 _gtk_css_parser_error (parser, "Expected an identifier");
144 _gtk_css_parser_error (parser,
145 "Unknown value '%s' for enum type '%s'",
146 str, g_type_name (type));
153 enum_print (int value,
157 GEnumClass *enum_class;
158 GEnumValue *enum_value;
160 enum_class = g_type_class_ref (type);
161 enum_value = g_enum_get_value (enum_class, value);
163 g_string_append (string, enum_value->value_nick);
165 g_type_class_unref (enum_class);
169 rgba_value_parse (GtkCssParser *parser,
173 GtkSymbolicColor *symbolic;
176 if (_gtk_css_parser_try (parser, "currentcolor", TRUE))
178 g_value_unset (value);
179 g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
180 g_value_set_enum (value, GTK_CSS_CURRENT_COLOR);
184 symbolic = _gtk_css_parser_read_symbolic_color (parser);
185 if (symbolic == NULL)
188 if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
190 g_value_set_boxed (value, &rgba);
191 gtk_symbolic_color_unref (symbolic);
195 g_value_unset (value);
196 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
197 g_value_take_boxed (value, symbolic);
204 rgba_value_print (const GValue *value,
207 const GdkRGBA *rgba = g_value_get_boxed (value);
210 g_string_append (string, "none");
213 char *s = gdk_rgba_to_string (rgba);
214 g_string_append (string, s);
220 rgba_value_compute (GValue *computed,
221 GtkStyleContext *context,
222 const GValue *specified)
224 GdkRGBA rgba, white = { 1, 1, 1, 1 };
226 if (G_VALUE_HOLDS (specified, GTK_TYPE_CSS_SPECIAL_VALUE))
228 g_assert (g_value_get_enum (specified) == GTK_CSS_CURRENT_COLOR);
229 g_value_copy (_gtk_style_context_peek_property (context, "color"), computed);
231 else if (G_VALUE_HOLDS (specified, GTK_TYPE_SYMBOLIC_COLOR))
233 if (_gtk_style_context_resolve_color (context,
234 g_value_get_boxed (specified),
236 g_value_set_boxed (computed, &rgba);
238 g_value_set_boxed (computed, &white);
241 g_value_copy (specified, computed);
245 color_value_parse (GtkCssParser *parser,
249 GtkSymbolicColor *symbolic;
252 symbolic = _gtk_css_parser_read_symbolic_color (parser);
253 if (symbolic == NULL)
256 if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
260 color.red = rgba.red * 65535. + 0.5;
261 color.green = rgba.green * 65535. + 0.5;
262 color.blue = rgba.blue * 65535. + 0.5;
264 g_value_set_boxed (value, &color);
268 g_value_unset (value);
269 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
270 g_value_take_boxed (value, symbolic);
277 color_value_print (const GValue *value,
280 const GdkColor *color = g_value_get_boxed (value);
283 g_string_append (string, "none");
286 char *s = gdk_color_to_string (color);
287 g_string_append (string, s);
293 color_value_compute (GValue *computed,
294 GtkStyleContext *context,
295 const GValue *specified)
298 GdkColor color = { 0, 65535, 65535, 65535 };
300 if (G_VALUE_HOLDS (specified, GTK_TYPE_SYMBOLIC_COLOR))
302 if (_gtk_style_context_resolve_color (context,
303 g_value_get_boxed (specified),
306 color.red = rgba.red * 65535. + 0.5;
307 color.green = rgba.green * 65535. + 0.5;
308 color.blue = rgba.blue * 65535. + 0.5;
311 g_value_set_boxed (computed, &color);
314 g_value_copy (specified, computed);
318 symbolic_color_value_parse (GtkCssParser *parser,
322 GtkSymbolicColor *symbolic;
324 symbolic = _gtk_css_parser_read_symbolic_color (parser);
325 if (symbolic == NULL)
328 g_value_take_boxed (value, symbolic);
333 symbolic_color_value_print (const GValue *value,
336 GtkSymbolicColor *symbolic = g_value_get_boxed (value);
338 if (symbolic == NULL)
339 g_string_append (string, "none");
342 char *s = gtk_symbolic_color_to_string (symbolic);
343 g_string_append (string, s);
349 font_description_value_parse (GtkCssParser *parser,
353 PangoFontDescription *font_desc;
357 str = _gtk_css_parser_read_value (parser);
361 font_desc = pango_font_description_from_string (str);
362 mask = pango_font_description_get_set_fields (font_desc);
363 /* These values are not really correct,
364 * but the fields must be set, so we set them to something */
365 if ((mask & PANGO_FONT_MASK_FAMILY) == 0)
366 pango_font_description_set_family_static (font_desc, "Sans");
367 if ((mask & PANGO_FONT_MASK_SIZE) == 0)
368 pango_font_description_set_size (font_desc, 10 * PANGO_SCALE);
370 g_value_take_boxed (value, font_desc);
375 font_description_value_print (const GValue *value,
378 const PangoFontDescription *desc = g_value_get_boxed (value);
381 g_string_append (string, "none");
384 char *s = pango_font_description_to_string (desc);
385 g_string_append (string, s);
391 boolean_value_parse (GtkCssParser *parser,
395 if (_gtk_css_parser_try (parser, "true", TRUE) ||
396 _gtk_css_parser_try (parser, "1", TRUE))
398 g_value_set_boolean (value, TRUE);
401 else if (_gtk_css_parser_try (parser, "false", TRUE) ||
402 _gtk_css_parser_try (parser, "0", TRUE))
404 g_value_set_boolean (value, FALSE);
409 _gtk_css_parser_error (parser, "Expected a boolean value");
415 boolean_value_print (const GValue *value,
418 if (g_value_get_boolean (value))
419 g_string_append (string, "true");
421 g_string_append (string, "false");
425 int_value_parse (GtkCssParser *parser,
431 if (_gtk_css_parser_begins_with (parser, '-'))
433 int res = _gtk_win32_theme_int_parse (parser, base, &i);
436 g_value_set_int (value, i);
439 /* < 0 => continue */
442 if (!_gtk_css_parser_try_int (parser, &i))
444 _gtk_css_parser_error (parser, "Expected a valid integer value");
448 g_value_set_int (value, i);
453 int_value_print (const GValue *value,
456 g_string_append_printf (string, "%d", g_value_get_int (value));
460 uint_value_parse (GtkCssParser *parser,
466 if (!_gtk_css_parser_try_uint (parser, &u))
468 _gtk_css_parser_error (parser, "Expected a valid unsigned value");
472 g_value_set_uint (value, u);
477 uint_value_print (const GValue *value,
480 g_string_append_printf (string, "%u", g_value_get_uint (value));
484 double_value_parse (GtkCssParser *parser,
490 if (!_gtk_css_parser_try_double (parser, &d))
492 _gtk_css_parser_error (parser, "Expected a number");
496 g_value_set_double (value, d);
501 double_value_print (const GValue *value,
504 string_append_double (string, g_value_get_double (value));
508 float_value_parse (GtkCssParser *parser,
514 if (!_gtk_css_parser_try_double (parser, &d))
516 _gtk_css_parser_error (parser, "Expected a number");
520 g_value_set_float (value, d);
525 float_value_print (const GValue *value,
528 string_append_double (string, g_value_get_float (value));
532 string_value_parse (GtkCssParser *parser,
536 char *str = _gtk_css_parser_read_string (parser);
541 g_value_take_string (value, str);
546 string_value_print (const GValue *value,
549 string_append_string (str, g_value_get_string (value));
553 theming_engine_value_parse (GtkCssParser *parser,
557 GtkThemingEngine *engine;
560 if (_gtk_css_parser_try (parser, "none", TRUE))
562 g_value_set_object (value, gtk_theming_engine_load (NULL));
566 str = _gtk_css_parser_try_ident (parser, TRUE);
569 _gtk_css_parser_error (parser, "Expected a valid theme name");
573 engine = gtk_theming_engine_load (str);
577 _gtk_css_parser_error (parser, "Themeing engine '%s' not found", str);
582 g_value_set_object (value, engine);
588 theming_engine_value_print (const GValue *value,
591 GtkThemingEngine *engine;
594 engine = g_value_get_object (value);
596 g_string_append (string, "none");
599 /* XXX: gtk_theming_engine_get_name()? */
600 g_object_get (engine, "name", &name, NULL);
601 g_string_append (string, name ? name : "none");
607 animation_description_value_parse (GtkCssParser *parser,
611 GtkAnimationDescription *desc;
614 str = _gtk_css_parser_read_value (parser);
618 desc = _gtk_animation_description_from_string (str);
623 _gtk_css_parser_error (parser, "Invalid animation description");
627 g_value_take_boxed (value, desc);
632 animation_description_value_print (const GValue *value,
635 GtkAnimationDescription *desc = g_value_get_boxed (value);
638 g_string_append (string, "none");
640 _gtk_animation_description_print (desc, string);
644 border_value_parse (GtkCssParser *parser,
648 GtkBorder border = { 0, };
651 for (i = 0; i < G_N_ELEMENTS (numbers); i++)
653 if (_gtk_css_parser_begins_with (parser, '-'))
655 /* These are strictly speaking signed, but we want to be able to use them
656 for unsigned types too, as the actual ranges of values make this safe */
657 int res = _gtk_win32_theme_int_parse (parser, base, (int *)&numbers[i]);
659 if (res == 0) /* Parse error, report */
662 if (res < 0) /* Nothing known to expand */
667 if (!_gtk_css_parser_try_uint (parser, &numbers[i]))
670 /* XXX: shouldn't allow spaces here? */
671 _gtk_css_parser_try (parser, "px", TRUE);
677 _gtk_css_parser_error (parser, "Expected valid border");
681 border.top = numbers[0];
683 border.right = numbers[1];
685 border.right = border.top;
687 border.bottom = numbers[2];
689 border.bottom = border.top;
691 border.left = numbers[3];
693 border.left = border.right;
695 g_value_set_boxed (value, &border);
700 border_value_print (const GValue *value, GString *string)
702 const GtkBorder *border = g_value_get_boxed (value);
705 g_string_append (string, "none");
706 else if (border->left != border->right)
707 g_string_append_printf (string, "%d %d %d %d", border->top, border->right, border->bottom, border->left);
708 else if (border->top != border->bottom)
709 g_string_append_printf (string, "%d %d %d", border->top, border->right, border->bottom);
710 else if (border->top != border->left)
711 g_string_append_printf (string, "%d %d", border->top, border->right);
713 g_string_append_printf (string, "%d", border->top);
717 gradient_value_parse (GtkCssParser *parser,
721 GtkGradient *gradient;
723 gradient = _gtk_gradient_parse (parser);
724 if (gradient == NULL)
727 g_value_take_boxed (value, gradient);
732 gradient_value_print (const GValue *value,
735 GtkGradient *gradient = g_value_get_boxed (value);
737 if (gradient == NULL)
738 g_string_append (string, "none");
741 char *s = gtk_gradient_to_string (gradient);
742 g_string_append (string, s);
748 gtk_css_parse_url (GtkCssParser *parser,
754 if (_gtk_css_parser_try (parser, "url", FALSE))
756 if (!_gtk_css_parser_try (parser, "(", TRUE))
758 _gtk_css_parser_skip_whitespace (parser);
759 if (_gtk_css_parser_try (parser, "(", TRUE))
763 error = g_error_new_literal (GTK_CSS_PROVIDER_ERROR,
764 GTK_CSS_PROVIDER_ERROR_DEPRECATED,
765 "Whitespace between 'url' and '(' is deprecated");
767 _gtk_css_parser_take_error (parser, error);
771 _gtk_css_parser_error (parser, "Expected '(' after 'url'");
776 path = _gtk_css_parser_read_string (parser);
780 if (!_gtk_css_parser_try (parser, ")", TRUE))
782 _gtk_css_parser_error (parser, "No closing ')' found for 'url'");
789 path = _gtk_css_parser_try_name (parser, TRUE);
792 _gtk_css_parser_error (parser, "Not a valid url");
797 file = g_file_resolve_relative_path (base, path);
804 pattern_value_parse (GtkCssParser *parser,
808 if (_gtk_css_parser_try (parser, "none", TRUE))
810 /* nothing to do here */
812 else if (_gtk_css_parser_begins_with (parser, '-'))
814 g_value_unset (value);
815 g_value_init (value, GTK_TYPE_GRADIENT);
816 return gradient_value_parse (parser, base, value);
820 GError *error = NULL;
824 cairo_surface_t *surface;
825 cairo_pattern_t *pattern;
827 cairo_matrix_t matrix;
829 file = gtk_css_parse_url (parser, base);
833 path = g_file_get_path (file);
834 g_object_unref (file);
836 pixbuf = gdk_pixbuf_new_from_file (path, &error);
840 _gtk_css_parser_take_error (parser, error);
844 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
845 gdk_pixbuf_get_width (pixbuf),
846 gdk_pixbuf_get_height (pixbuf));
847 cr = cairo_create (surface);
848 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
850 pattern = cairo_pattern_create_for_surface (surface);
852 cairo_matrix_init_scale (&matrix,
853 gdk_pixbuf_get_width (pixbuf),
854 gdk_pixbuf_get_height (pixbuf));
855 cairo_pattern_set_matrix (pattern, &matrix);
857 cairo_surface_destroy (surface);
859 g_object_unref (pixbuf);
861 g_value_take_boxed (value, pattern);
867 static cairo_status_t
868 surface_write (void *closure,
869 const unsigned char *data,
872 g_byte_array_append (closure, data, length);
874 return CAIRO_STATUS_SUCCESS;
878 surface_print (cairo_surface_t *surface,
881 #if CAIRO_HAS_PNG_FUNCTIONS
885 array = g_byte_array_new ();
886 cairo_surface_write_to_png_stream (surface, surface_write, array);
887 base64 = g_base64_encode (array->data, array->len);
888 g_byte_array_free (array, TRUE);
890 g_string_append (string, "url(\"data:image/png;base64,");
891 g_string_append (string, base64);
892 g_string_append (string, "\")");
896 g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
901 pattern_value_print (const GValue *value,
904 cairo_pattern_t *pattern;
905 cairo_surface_t *surface;
907 pattern = g_value_get_boxed (value);
911 g_string_append (string, "none");
915 switch (cairo_pattern_get_type (pattern))
917 case CAIRO_PATTERN_TYPE_SURFACE:
918 if (cairo_pattern_get_surface (pattern, &surface) != CAIRO_STATUS_SUCCESS)
920 g_assert_not_reached ();
922 surface_print (surface, string);
924 case CAIRO_PATTERN_TYPE_SOLID:
925 case CAIRO_PATTERN_TYPE_LINEAR:
926 case CAIRO_PATTERN_TYPE_RADIAL:
928 g_assert_not_reached ();
934 pattern_value_compute (GValue *computed,
935 GtkStyleContext *context,
936 const GValue *specified)
938 if (G_VALUE_HOLDS (specified, GTK_TYPE_GRADIENT))
940 cairo_pattern_t *gradient;
942 gradient = gtk_gradient_resolve_for_context (g_value_get_boxed (specified), context);
944 g_value_take_boxed (computed, gradient);
947 g_value_copy (specified, computed);
951 shadow_value_parse (GtkCssParser *parser,
955 gboolean have_inset, have_color, have_lengths;
956 gdouble hoffset, voffset, blur, spread;
957 GtkSymbolicColor *color;
961 shadow = _gtk_shadow_new ();
963 if (_gtk_css_parser_try (parser, "none", TRUE))
968 have_inset = have_lengths = have_color = FALSE;
970 for (i = 0; i < 3; i++)
973 _gtk_css_parser_try (parser, "inset", TRUE))
980 _gtk_css_parser_try_double (parser, &hoffset))
984 if (!_gtk_css_parser_try_double (parser, &voffset))
986 _gtk_css_parser_error (parser, "Horizontal and vertical offsets are required");
987 _gtk_shadow_unref (shadow);
991 if (!_gtk_css_parser_try_double (parser, &blur))
994 if (!_gtk_css_parser_try_double (parser, &spread))
1004 /* XXX: the color is optional and UA-defined if it's missing,
1005 * but it doesn't really make sense for us...
1007 color = _gtk_css_parser_read_symbolic_color (parser);
1011 _gtk_shadow_unref (shadow);
1017 if (!have_color || !have_lengths)
1019 _gtk_css_parser_error (parser, "Must specify at least color and offsets");
1020 _gtk_shadow_unref (shadow);
1024 _gtk_shadow_append (shadow,
1029 gtk_symbolic_color_unref (color);
1032 while (_gtk_css_parser_try (parser, ",", TRUE));
1034 g_value_take_boxed (value, shadow);
1039 shadow_value_print (const GValue *value,
1044 shadow = g_value_get_boxed (value);
1047 g_string_append (string, "none");
1049 _gtk_shadow_print (shadow, string);
1053 shadow_value_compute (GValue *computed,
1054 GtkStyleContext *context,
1055 const GValue *specified)
1059 shadow = g_value_get_boxed (specified);
1061 shadow = _gtk_shadow_resolve (shadow, context);
1063 g_value_take_boxed (computed, shadow);
1067 background_repeat_value_parse (GtkCssParser *parser,
1071 GtkCssBackgroundRepeat repeat;
1074 if (!enum_parse (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT_STYLE, &style))
1077 repeat.repeat = style;
1079 g_value_set_boxed (value, &repeat);
1085 background_repeat_value_print (const GValue *value,
1088 GtkCssBackgroundRepeat *repeat;
1090 repeat = g_value_get_boxed (value);
1092 enum_print (repeat->repeat, GTK_TYPE_CSS_BACKGROUND_REPEAT_STYLE, string);
1096 border_image_repeat_value_parse (GtkCssParser *parser,
1100 GtkCssBorderImageRepeat image_repeat;
1101 GtkCssBorderRepeatStyle styles[2];
1104 for (i = 0; i < 2; i++)
1106 if (_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, &v))
1110 styles[1] = styles[0] = GTK_CSS_REPEAT_STYLE_STRETCH;
1114 styles[i] = styles[0];
1117 image_repeat.hrepeat = styles[0];
1118 image_repeat.vrepeat = styles[1];
1120 g_value_set_boxed (value, &image_repeat);
1126 border_image_repeat_value_print (const GValue *value,
1129 GtkCssBorderImageRepeat *image_repeat;
1131 image_repeat = g_value_get_boxed (value);
1133 enum_print (image_repeat->hrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1134 if (image_repeat->hrepeat != image_repeat->vrepeat)
1136 g_string_append (string, " ");
1137 enum_print (image_repeat->vrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1142 enum_value_parse (GtkCssParser *parser,
1148 if (enum_parse (parser, G_VALUE_TYPE (value), &v))
1150 g_value_set_enum (value, v);
1158 enum_value_print (const GValue *value,
1161 enum_print (g_value_get_enum (value), G_VALUE_TYPE (value), string);
1165 flags_value_parse (GtkCssParser *parser,
1169 GFlagsClass *flags_class;
1170 GFlagsValue *flag_value;
1174 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1177 str = _gtk_css_parser_try_ident (parser, TRUE);
1180 _gtk_css_parser_error (parser, "Expected an identifier");
1181 g_type_class_unref (flags_class);
1185 flag_value = g_flags_get_value_by_nick (flags_class, str);
1188 _gtk_css_parser_error (parser,
1189 "Unknown flag value '%s' for type '%s'",
1190 str, g_type_name (G_VALUE_TYPE (value)));
1191 /* XXX Do we want to return FALSE here? We can get
1192 * forward-compatibility for new values this way
1195 g_type_class_unref (flags_class);
1201 while (_gtk_css_parser_try (parser, ",", FALSE));
1203 g_type_class_unref (flags_class);
1205 g_value_set_enum (value, flags);
1211 flags_value_print (const GValue *value,
1214 GFlagsClass *flags_class;
1217 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1218 flags = g_value_get_flags (value);
1220 for (i = 0; i < flags_class->n_values; i++)
1222 GFlagsValue *flags_value = &flags_class->values[i];
1224 if (flags & flags_value->value)
1226 if (string->len != 0)
1227 g_string_append (string, ", ");
1229 g_string_append (string, flags_value->value_nick);
1233 g_type_class_unref (flags_class);
1239 gtk_css_style_funcs_init (void)
1241 if (G_LIKELY (parse_funcs != NULL))
1244 parse_funcs = g_hash_table_new (NULL, NULL);
1245 print_funcs = g_hash_table_new (NULL, NULL);
1246 compute_funcs = g_hash_table_new (NULL, NULL);
1248 register_conversion_function (GDK_TYPE_RGBA,
1251 rgba_value_compute);
1252 register_conversion_function (GDK_TYPE_COLOR,
1255 color_value_compute);
1256 register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
1257 symbolic_color_value_parse,
1258 symbolic_color_value_print,
1260 register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
1261 font_description_value_parse,
1262 font_description_value_print,
1264 register_conversion_function (G_TYPE_BOOLEAN,
1265 boolean_value_parse,
1266 boolean_value_print,
1268 register_conversion_function (G_TYPE_INT,
1272 register_conversion_function (G_TYPE_UINT,
1276 register_conversion_function (G_TYPE_DOUBLE,
1280 register_conversion_function (G_TYPE_FLOAT,
1284 register_conversion_function (G_TYPE_STRING,
1288 register_conversion_function (GTK_TYPE_THEMING_ENGINE,
1289 theming_engine_value_parse,
1290 theming_engine_value_print,
1292 register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
1293 animation_description_value_parse,
1294 animation_description_value_print,
1296 register_conversion_function (GTK_TYPE_BORDER,
1300 register_conversion_function (GTK_TYPE_GRADIENT,
1301 gradient_value_parse,
1302 gradient_value_print,
1304 register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
1305 pattern_value_parse,
1306 pattern_value_print,
1307 pattern_value_compute);
1308 register_conversion_function (GTK_TYPE_CSS_BORDER_IMAGE_REPEAT,
1309 border_image_repeat_value_parse,
1310 border_image_repeat_value_print,
1312 register_conversion_function (GTK_TYPE_SHADOW,
1315 shadow_value_compute);
1316 register_conversion_function (G_TYPE_ENUM,
1320 register_conversion_function (G_TYPE_FLAGS,
1324 register_conversion_function (GTK_TYPE_CSS_BACKGROUND_REPEAT,
1325 background_repeat_value_parse,
1326 background_repeat_value_print,
1331 * _gtk_css_style_parse_value:
1332 * @value: the value to parse into. Must be a valid initialized #GValue
1333 * @parser: the parser to parse from
1334 * @base: the base URL for @parser
1336 * This is the generic parsing function used for CSS values. If the
1337 * function fails to parse a value, it will emit an error on @parser,
1338 * return %FALSE and not touch @value.
1340 * Returns: %TRUE if parsing succeeded.
1343 _gtk_css_style_parse_value (GValue *value,
1344 GtkCssParser *parser,
1347 GtkStyleParseFunc func;
1349 g_return_val_if_fail (value != NULL, FALSE);
1350 g_return_val_if_fail (parser != NULL, FALSE);
1352 gtk_css_style_funcs_init ();
1354 func = g_hash_table_lookup (parse_funcs,
1355 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
1357 func = g_hash_table_lookup (parse_funcs,
1358 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
1362 _gtk_css_parser_error (parser,
1363 "Cannot convert to type '%s'",
1364 g_type_name (G_VALUE_TYPE (value)));
1368 return (*func) (parser, base, value);
1372 * _gtk_css_style_print_value:
1373 * @value: an initialized GValue returned from _gtk_css_style_parse()
1374 * @string: the string to print into
1376 * Prints @value into @string as a CSS value. If @value is not a
1377 * valid value, a random string will be printed instead.
1380 _gtk_css_style_print_value (const GValue *value,
1383 GtkStylePrintFunc func;
1385 gtk_css_style_funcs_init ();
1387 func = g_hash_table_lookup (print_funcs,
1388 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
1390 func = g_hash_table_lookup (print_funcs,
1391 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
1395 char *s = g_strdup_value_contents (value);
1396 g_string_append (string, s);
1401 func (value, string);
1405 * _gtk_css_style_compute_value:
1406 * @computed: (out): a value to be filled with the result
1407 * @context: the context to use for computing the value
1408 * @specified: the value to use for the computation
1410 * Converts the @specified value into the @computed value using the
1411 * information in @context. The values must have matching types, ie
1412 * @specified must be a result of a call to
1413 * _gtk_css_style_parse_value() with the same type as @computed.
1416 _gtk_css_style_compute_value (GValue *computed,
1417 GtkStyleContext *context,
1418 const GValue *specified)
1420 GtkStyleComputeFunc func;
1422 g_return_if_fail (G_IS_VALUE (computed));
1423 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1424 g_return_if_fail (G_IS_VALUE (specified));
1426 gtk_css_style_funcs_init ();
1428 func = g_hash_table_lookup (compute_funcs,
1429 GSIZE_TO_POINTER (G_VALUE_TYPE (computed)));
1431 func = g_hash_table_lookup (compute_funcs,
1432 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (computed))));
1435 func (computed, context, specified);
1437 g_value_copy (specified, computed);