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"
28 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include <cairo-gobject.h>
31 #include "gtkcssprovider.h"
32 #include "gtkcssparserprivate.h"
34 /* the actual parsers we have */
35 #include "gtkanimationdescription.h"
36 #include "gtkbindings.h"
37 #include "gtk9slice.h"
38 #include "gtkgradient.h"
39 #include "gtkshadowprivate.h"
40 #include "gtkthemingengine.h"
41 #include "gtktypebuiltins.h"
43 static GHashTable *parse_funcs = NULL;
44 static GHashTable *print_funcs = NULL;
45 static GHashTable *properties = NULL;
48 register_conversion_function (GType type,
49 GtkStyleParseFunc parse,
50 GtkStylePrintFunc print)
53 g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse);
55 g_hash_table_insert (print_funcs, GSIZE_TO_POINTER (type), print);
58 /*** IMPLEMENTATIONS ***/
61 rgba_value_parse (GtkCssParser *parser,
65 GtkSymbolicColor *symbolic;
68 symbolic = _gtk_css_parser_read_symbolic_color (parser);
72 if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
74 g_value_set_boxed (value, &rgba);
75 gtk_symbolic_color_unref (symbolic);
79 g_value_unset (value);
80 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
81 g_value_take_boxed (value, symbolic);
88 rgba_value_print (const GValue *value,
91 const GdkRGBA *rgba = g_value_get_boxed (value);
94 g_string_append (string, "none");
97 char *s = gdk_rgba_to_string (rgba);
98 g_string_append (string, s);
104 color_value_parse (GtkCssParser *parser,
108 GtkSymbolicColor *symbolic;
111 symbolic = _gtk_css_parser_read_symbolic_color (parser);
112 if (symbolic == NULL)
115 if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
119 color.red = rgba.red * 65535. + 0.5;
120 color.green = rgba.green * 65535. + 0.5;
121 color.blue = rgba.blue * 65535. + 0.5;
123 g_value_set_boxed (value, &color);
127 g_value_unset (value);
128 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
129 g_value_take_boxed (value, symbolic);
136 color_value_print (const GValue *value,
139 const GdkColor *color = g_value_get_boxed (value);
142 g_string_append (string, "none");
145 char *s = gdk_color_to_string (color);
146 g_string_append (string, s);
152 symbolic_color_value_parse (GtkCssParser *parser,
156 GtkSymbolicColor *symbolic;
158 symbolic = _gtk_css_parser_read_symbolic_color (parser);
159 if (symbolic == NULL)
162 g_value_take_boxed (value, symbolic);
167 symbolic_color_value_print (const GValue *value,
170 GtkSymbolicColor *symbolic = g_value_get_boxed (value);
172 if (symbolic == NULL)
173 g_string_append (string, "none");
176 char *s = gtk_symbolic_color_to_string (symbolic);
177 g_string_append (string, s);
183 font_description_value_parse (GtkCssParser *parser,
187 PangoFontDescription *font_desc;
190 str = _gtk_css_parser_read_value (parser);
194 font_desc = pango_font_description_from_string (str);
196 g_value_take_boxed (value, font_desc);
201 font_description_value_print (const GValue *value,
204 const PangoFontDescription *desc = g_value_get_boxed (value);
207 g_string_append (string, "none");
210 char *s = pango_font_description_to_string (desc);
211 g_string_append (string, s);
217 boolean_value_parse (GtkCssParser *parser,
221 if (_gtk_css_parser_try (parser, "true", TRUE) ||
222 _gtk_css_parser_try (parser, "1", TRUE))
224 g_value_set_boolean (value, TRUE);
227 else if (_gtk_css_parser_try (parser, "false", TRUE) ||
228 _gtk_css_parser_try (parser, "0", TRUE))
230 g_value_set_boolean (value, FALSE);
235 _gtk_css_parser_error (parser, "Expected a boolean value");
241 boolean_value_print (const GValue *value,
244 if (g_value_get_boolean (value))
245 g_string_append (string, "true");
247 g_string_append (string, "false");
251 int_value_parse (GtkCssParser *parser,
257 if (!_gtk_css_parser_try_int (parser, &i))
259 _gtk_css_parser_error (parser, "Expected a valid integer value");
263 g_value_set_int (value, i);
268 int_value_print (const GValue *value,
271 g_string_append_printf (string, "%d", g_value_get_int (value));
275 uint_value_parse (GtkCssParser *parser,
281 if (!_gtk_css_parser_try_uint (parser, &u))
283 _gtk_css_parser_error (parser, "Expected a valid unsigned value");
287 g_value_set_uint (value, u);
292 uint_value_print (const GValue *value,
295 g_string_append_printf (string, "%u", g_value_get_uint (value));
299 double_value_parse (GtkCssParser *parser,
305 if (!_gtk_css_parser_try_double (parser, &d))
307 _gtk_css_parser_error (parser, "Expected a number");
311 g_value_set_double (value, d);
316 string_append_double (GString *string,
319 char buf[G_ASCII_DTOSTR_BUF_SIZE];
321 g_ascii_dtostr (buf, sizeof (buf), d);
322 g_string_append (string, buf);
326 double_value_print (const GValue *value,
329 string_append_double (string, g_value_get_double (value));
333 float_value_parse (GtkCssParser *parser,
339 if (!_gtk_css_parser_try_double (parser, &d))
341 _gtk_css_parser_error (parser, "Expected a number");
345 g_value_set_float (value, d);
350 float_value_print (const GValue *value,
353 string_append_double (string, g_value_get_float (value));
357 string_value_parse (GtkCssParser *parser,
361 char *str = _gtk_css_parser_read_string (parser);
366 g_value_take_string (value, str);
371 string_value_print (const GValue *value,
377 string = g_value_get_string (value);
378 g_string_append_c (str, '"');
381 len = strcspn (string, "\"\n\r\f");
382 g_string_append (str, string);
389 g_string_append (str, "\\A ");
392 g_string_append (str, "\\D ");
395 g_string_append (str, "\\C ");
398 g_string_append (str, "\\\"");
401 g_assert_not_reached ();
406 g_string_append_c (str, '"');
410 theming_engine_value_parse (GtkCssParser *parser,
414 GtkThemingEngine *engine;
417 str = _gtk_css_parser_try_ident (parser, TRUE);
420 _gtk_css_parser_error (parser, "Expected a valid theme name");
424 engine = gtk_theming_engine_load (str);
427 _gtk_css_parser_error (parser, "Themeing engine '%s' not found", str);
432 g_value_set_object (value, engine);
438 theming_engine_value_print (const GValue *value,
441 GtkThemingEngine *engine;
444 engine = g_value_get_object (value);
446 g_string_append (string, "none");
449 /* XXX: gtk_theming_engine_get_name()? */
450 g_object_get (engine, "name", &name, NULL);
451 g_string_append (string, name);
457 animation_description_value_parse (GtkCssParser *parser,
461 GtkAnimationDescription *desc;
464 str = _gtk_css_parser_read_value (parser);
468 desc = _gtk_animation_description_from_string (str);
473 _gtk_css_parser_error (parser, "Invalid animation description");
477 g_value_take_boxed (value, desc);
482 animation_description_value_print (const GValue *value,
485 GtkAnimationDescription *desc = g_value_get_boxed (value);
488 g_string_append (string, "none");
490 _gtk_animation_description_print (desc, string);
494 border_value_parse (GtkCssParser *parser,
498 GtkBorder border = { 0, };
501 for (i = 0; i < G_N_ELEMENTS (numbers); i++)
503 if (!_gtk_css_parser_try_uint (parser, &numbers[i]))
506 /* XXX: shouldn't allow spaces here? */
507 _gtk_css_parser_try (parser, "px", TRUE);
512 _gtk_css_parser_error (parser, "Expected valid border");
516 border.top = numbers[0];
518 border.right = numbers[1];
520 border.right = border.top;
522 border.bottom = numbers[2];
524 border.bottom = border.top;
526 border.left = numbers[3];
528 border.left = border.right;
530 g_value_set_boxed (value, &border);
535 border_value_print (const GValue *value, GString *string)
537 const GtkBorder *border = g_value_get_boxed (value);
540 g_string_append (string, "none");
541 else if (border->left != border->right)
542 g_string_append_printf (string, "%d %d %d %d", border->top, border->right, border->bottom, border->left);
543 else if (border->top != border->bottom)
544 g_string_append_printf (string, "%d %d %d", border->top, border->right, border->bottom);
545 else if (border->top != border->left)
546 g_string_append_printf (string, "%d %d", border->top, border->right);
548 g_string_append_printf (string, "%d", border->top);
552 gradient_value_parse (GtkCssParser *parser,
556 GtkGradient *gradient;
557 cairo_pattern_type_t type;
561 if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
563 _gtk_css_parser_error (parser,
564 "Expected '-gtk-gradient'");
568 if (!_gtk_css_parser_try (parser, "(", TRUE))
570 _gtk_css_parser_error (parser,
571 "Expected '(' after '-gtk-gradient'");
575 /* Parse gradient type */
576 if (_gtk_css_parser_try (parser, "linear", TRUE))
577 type = CAIRO_PATTERN_TYPE_LINEAR;
578 else if (_gtk_css_parser_try (parser, "radial", TRUE))
579 type = CAIRO_PATTERN_TYPE_RADIAL;
582 _gtk_css_parser_error (parser,
583 "Gradient type must be 'radial' or 'linear'");
587 /* Parse start/stop position parameters */
588 for (i = 0; i < 2; i++)
590 if (! _gtk_css_parser_try (parser, ",", TRUE))
592 _gtk_css_parser_error (parser,
597 if (_gtk_css_parser_try (parser, "left", TRUE))
599 else if (_gtk_css_parser_try (parser, "right", TRUE))
601 else if (_gtk_css_parser_try (parser, "center", TRUE))
603 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3]))
605 _gtk_css_parser_error (parser,
606 "Expected a valid X coordinate");
610 if (_gtk_css_parser_try (parser, "top", TRUE))
611 coords[i * 3 + 1] = 0;
612 else if (_gtk_css_parser_try (parser, "bottom", TRUE))
613 coords[i * 3 + 1] = 1;
614 else if (_gtk_css_parser_try (parser, "center", TRUE))
615 coords[i * 3 + 1] = 0.5;
616 else if (!_gtk_css_parser_try_double (parser, &coords[i * 3 + 1]))
618 _gtk_css_parser_error (parser,
619 "Expected a valid Y coordinate");
623 if (type == CAIRO_PATTERN_TYPE_RADIAL)
626 if (! _gtk_css_parser_try (parser, ",", TRUE))
628 _gtk_css_parser_error (parser,
633 if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
635 _gtk_css_parser_error (parser,
636 "Expected a numer for the radius");
642 if (type == CAIRO_PATTERN_TYPE_LINEAR)
643 gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
645 gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
646 coords[3], coords[4], coords[5]);
648 while (_gtk_css_parser_try (parser, ",", TRUE))
650 GtkSymbolicColor *color;
653 if (_gtk_css_parser_try (parser, "from", TRUE))
657 if (!_gtk_css_parser_try (parser, "(", TRUE))
659 gtk_gradient_unref (gradient);
660 _gtk_css_parser_error (parser,
666 else if (_gtk_css_parser_try (parser, "to", TRUE))
670 if (!_gtk_css_parser_try (parser, "(", TRUE))
672 gtk_gradient_unref (gradient);
673 _gtk_css_parser_error (parser,
679 else if (_gtk_css_parser_try (parser, "color-stop", TRUE))
681 if (!_gtk_css_parser_try (parser, "(", TRUE))
683 gtk_gradient_unref (gradient);
684 _gtk_css_parser_error (parser,
689 if (!_gtk_css_parser_try_double (parser, &position))
691 gtk_gradient_unref (gradient);
692 _gtk_css_parser_error (parser,
693 "Expected a valid number");
697 if (!_gtk_css_parser_try (parser, ",", TRUE))
699 gtk_gradient_unref (gradient);
700 _gtk_css_parser_error (parser,
707 gtk_gradient_unref (gradient);
708 _gtk_css_parser_error (parser,
709 "Not a valid color-stop definition");
713 color = _gtk_css_parser_read_symbolic_color (parser);
716 gtk_gradient_unref (gradient);
720 gtk_gradient_add_color_stop (gradient, position, color);
721 gtk_symbolic_color_unref (color);
723 if (!_gtk_css_parser_try (parser, ")", TRUE))
725 gtk_gradient_unref (gradient);
726 _gtk_css_parser_error (parser,
732 if (!_gtk_css_parser_try (parser, ")", TRUE))
734 gtk_gradient_unref (gradient);
735 _gtk_css_parser_error (parser,
740 g_value_take_boxed (value, gradient);
745 gradient_value_print (const GValue *value,
748 GtkGradient *gradient = g_value_get_boxed (value);
750 if (gradient == NULL)
751 g_string_append (string, "none");
754 char *s = gtk_gradient_to_string (gradient);
755 g_string_append (string, s);
761 gtk_css_parse_url (GtkCssParser *parser,
767 if (_gtk_css_parser_try (parser, "url", FALSE))
769 if (!_gtk_css_parser_try (parser, "(", TRUE))
771 _gtk_css_parser_skip_whitespace (parser);
772 if (_gtk_css_parser_try (parser, "(", TRUE))
776 error = g_error_new_literal (GTK_CSS_PROVIDER_ERROR,
777 GTK_CSS_PROVIDER_ERROR_DEPRECATED,
778 "Whitespace between 'url' and '(' is not allowed");
780 _gtk_css_parser_take_error (parser, error);
784 _gtk_css_parser_error (parser, "Expected '(' after 'url'");
789 path = _gtk_css_parser_read_string (parser);
793 if (!_gtk_css_parser_try (parser, ")", TRUE))
795 _gtk_css_parser_error (parser, "No closing ')' found for 'url'");
802 path = _gtk_css_parser_try_name (parser, TRUE);
805 _gtk_css_parser_error (parser, "Not a valid url");
810 file = g_file_resolve_relative_path (base, path);
817 pattern_value_parse (GtkCssParser *parser,
821 if (_gtk_css_parser_begins_with (parser, '-'))
823 g_value_unset (value);
824 g_value_init (value, GTK_TYPE_GRADIENT);
825 return gradient_value_parse (parser, base, value);
829 GError *error = NULL;
833 cairo_surface_t *surface;
834 cairo_pattern_t *pattern;
836 cairo_matrix_t matrix;
838 file = gtk_css_parse_url (parser, base);
842 path = g_file_get_path (file);
843 g_object_unref (file);
845 pixbuf = gdk_pixbuf_new_from_file (path, &error);
849 _gtk_css_parser_take_error (parser, error);
853 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
854 gdk_pixbuf_get_width (pixbuf),
855 gdk_pixbuf_get_height (pixbuf));
856 cr = cairo_create (surface);
857 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
859 pattern = cairo_pattern_create_for_surface (surface);
861 cairo_matrix_init_scale (&matrix,
862 gdk_pixbuf_get_width (pixbuf),
863 gdk_pixbuf_get_height (pixbuf));
864 cairo_pattern_set_matrix (pattern, &matrix);
866 cairo_surface_destroy (surface);
868 g_object_unref (pixbuf);
870 g_value_take_boxed (value, pattern);
877 shadow_value_parse (GtkCssParser *parser,
882 gdouble hoffset, voffset, blur, spread;
883 GtkSymbolicColor *color;
886 shadow = _gtk_shadow_new ();
890 inset = _gtk_css_parser_try (parser, "inset", TRUE);
892 if (!_gtk_css_parser_try_double (parser, &hoffset) ||
893 !_gtk_css_parser_try_double (parser, &voffset))
895 _gtk_css_parser_error (parser, "Horizontal and vertical offsets are required");
896 _gtk_shadow_unref (shadow);
900 if (!_gtk_css_parser_try_double (parser, &blur))
903 if (!_gtk_css_parser_try_double (parser, &spread))
906 /* XXX: the color is optional and UA-defined if it's missing,
907 * but it doesn't really make sense for us...
909 color = _gtk_css_parser_read_symbolic_color (parser);
913 _gtk_shadow_unref (shadow);
917 _gtk_shadow_append (shadow,
922 gtk_symbolic_color_unref (color);
925 while (_gtk_css_parser_try (parser, ",", TRUE));
927 g_value_take_boxed (value, shadow);
932 shadow_value_print (const GValue *value,
937 shadow = g_value_get_boxed (value);
940 g_string_append (string, "none");
942 _gtk_shadow_print (shadow, string);
946 slice_value_parse (GtkCssParser *parser,
950 gdouble distance_top, distance_bottom;
951 gdouble distance_left, distance_right;
952 GtkSliceSideModifier mods[2];
956 GError *error = NULL;
960 /* Parse image url */
961 file = gtk_css_parse_url (parser, base);
965 if (!_gtk_css_parser_try_double (parser, &distance_top) ||
966 !_gtk_css_parser_try_double (parser, &distance_right) ||
967 !_gtk_css_parser_try_double (parser, &distance_bottom) ||
968 !_gtk_css_parser_try_double (parser, &distance_left))
970 _gtk_css_parser_error (parser, "Expected a number");
971 g_object_unref (file);
975 for (i = 0; i < 2; i++)
977 if (_gtk_css_parser_try (parser, "stretch", TRUE))
978 mods[i] = GTK_SLICE_STRETCH;
979 else if (_gtk_css_parser_try (parser, "repeat", TRUE))
980 mods[i] = GTK_SLICE_REPEAT;
983 mods[1] = mods[0] = GTK_SLICE_STRETCH;
990 path = g_file_get_path (file);
991 g_object_unref (file);
992 pixbuf = gdk_pixbuf_new_from_file (path, &error);
996 _gtk_css_parser_take_error (parser, error);
1000 slice = _gtk_9slice_new (pixbuf,
1001 distance_top, distance_bottom,
1002 distance_left, distance_right,
1004 g_object_unref (pixbuf);
1006 g_value_take_boxed (value, slice);
1011 enum_value_parse (GtkCssParser *parser,
1015 GEnumClass *enum_class;
1016 GEnumValue *enum_value;
1019 str = _gtk_css_parser_try_ident (parser, TRUE);
1022 _gtk_css_parser_error (parser, "Expected an identifier");
1026 enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1027 enum_value = g_enum_get_value_by_nick (enum_class, str);
1030 g_value_set_enum (value, enum_value->value);
1032 _gtk_css_parser_error (parser,
1033 "Unknown value '%s' for enum type '%s'",
1034 str, g_type_name (G_VALUE_TYPE (value)));
1036 g_type_class_unref (enum_class);
1039 return enum_value != NULL;
1043 enum_value_print (const GValue *value,
1046 GEnumClass *enum_class;
1047 GEnumValue *enum_value;
1049 enum_class = g_type_class_ref (G_VALUE_TYPE (value));
1050 enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
1052 g_string_append (string, enum_value->value_nick);
1054 g_type_class_unref (enum_class);
1058 flags_value_parse (GtkCssParser *parser,
1062 GFlagsClass *flags_class;
1063 GFlagsValue *flag_value;
1067 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1070 str = _gtk_css_parser_try_ident (parser, TRUE);
1073 _gtk_css_parser_error (parser, "Expected an identifier");
1074 g_type_class_unref (flags_class);
1078 flag_value = g_flags_get_value_by_nick (flags_class, str);
1081 _gtk_css_parser_error (parser,
1082 "Unknown flag value '%s' for type '%s'",
1083 str, g_type_name (G_VALUE_TYPE (value)));
1084 /* XXX Do we want to return FALSE here? We can get
1085 * forward-compatibility for new values this way
1088 g_type_class_unref (flags_class);
1094 while (_gtk_css_parser_try (parser, ",", FALSE));
1096 g_type_class_unref (flags_class);
1098 g_value_set_enum (value, flags);
1104 flags_value_print (const GValue *value,
1107 GFlagsClass *flags_class;
1110 flags_class = g_type_class_ref (G_VALUE_TYPE (value));
1111 flags = g_value_get_flags (value);
1113 for (i = 0; i < flags_class->n_values; i++)
1115 GFlagsValue *flags_value = &flags_class->values[i];
1117 if (flags & flags_value->value)
1119 if (string->len != 0)
1120 g_string_append (string, ", ");
1122 g_string_append (string, flags_value->value_nick);
1126 g_type_class_unref (flags_class);
1130 bindings_value_parse (GtkCssParser *parser,
1135 GtkBindingSet *binding_set;
1138 array = g_ptr_array_new ();
1141 name = _gtk_css_parser_try_ident (parser, TRUE);
1144 _gtk_css_parser_error (parser, "Not a valid binding name");
1145 g_ptr_array_free (array, TRUE);
1149 binding_set = gtk_binding_set_find (name);
1153 _gtk_css_parser_error (parser, "No binding set named '%s'", name);
1158 g_ptr_array_add (array, binding_set);
1161 while (_gtk_css_parser_try (parser, ",", TRUE));
1163 g_value_take_boxed (value, array);
1169 bindings_value_print (const GValue *value,
1175 array = g_value_get_boxed (value);
1177 for (i = 0; i < array->len; i++)
1179 GtkBindingSet *binding_set = g_ptr_array_index (array, i);
1182 g_string_append (string, ", ");
1183 g_string_append (string, binding_set->set_name);
1190 unpack_border (const GValue *value,
1197 GParameter *parameter = g_new0 (GParameter, 4);
1198 GtkBorder *border = g_value_get_boxed (value);
1200 parameter[0].name = top;
1201 g_value_init (¶meter[0].value, G_TYPE_INT);
1202 g_value_set_int (¶meter[0].value, border->top);
1203 parameter[1].name = left;
1204 g_value_init (¶meter[1].value, G_TYPE_INT);
1205 g_value_set_int (¶meter[1].value, border->left);
1206 parameter[2].name = bottom;
1207 g_value_init (¶meter[2].value, G_TYPE_INT);
1208 g_value_set_int (¶meter[2].value, border->bottom);
1209 parameter[3].name = right;
1210 g_value_init (¶meter[3].value, G_TYPE_INT);
1211 g_value_set_int (¶meter[3].value, border->right);
1218 pack_border (GValue *value,
1219 GtkStyleProperties *props,
1220 GtkStateFlags state,
1229 gtk_style_properties_get (props,
1242 g_value_set_boxed (value, &border);
1246 unpack_border_width (const GValue *value,
1249 return unpack_border (value, n_params,
1250 "border-top-width", "border-left-width",
1251 "border-bottom-width", "border-right-width");
1255 pack_border_width (GValue *value,
1256 GtkStyleProperties *props,
1257 GtkStateFlags state)
1259 pack_border (value, props, state,
1260 "border-top-width", "border-left-width",
1261 "border-bottom-width", "border-right-width");
1265 unpack_padding (const GValue *value,
1268 return unpack_border (value, n_params,
1269 "padding-top", "padding-left",
1270 "padding-bottom", "padding-right");
1274 pack_padding (GValue *value,
1275 GtkStyleProperties *props,
1276 GtkStateFlags state)
1278 pack_border (value, props, state,
1279 "padding-top", "padding-left",
1280 "padding-bottom", "padding-right");
1284 unpack_margin (const GValue *value,
1287 return unpack_border (value, n_params,
1288 "margin-top", "margin-left",
1289 "margin-bottom", "margin-right");
1293 pack_margin (GValue *value,
1294 GtkStyleProperties *props,
1295 GtkStateFlags state)
1297 pack_border (value, props, state,
1298 "margin-top", "margin-left",
1299 "margin-bottom", "margin-right");
1305 css_string_funcs_init (void)
1307 if (G_LIKELY (parse_funcs != NULL))
1310 parse_funcs = g_hash_table_new (NULL, NULL);
1311 print_funcs = g_hash_table_new (NULL, NULL);
1313 register_conversion_function (GDK_TYPE_RGBA,
1316 register_conversion_function (GDK_TYPE_COLOR,
1319 register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
1320 symbolic_color_value_parse,
1321 symbolic_color_value_print);
1322 register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
1323 font_description_value_parse,
1324 font_description_value_print);
1325 register_conversion_function (G_TYPE_BOOLEAN,
1326 boolean_value_parse,
1327 boolean_value_print);
1328 register_conversion_function (G_TYPE_INT,
1331 register_conversion_function (G_TYPE_UINT,
1334 register_conversion_function (G_TYPE_DOUBLE,
1336 double_value_print);
1337 register_conversion_function (G_TYPE_FLOAT,
1340 register_conversion_function (G_TYPE_STRING,
1342 string_value_print);
1343 register_conversion_function (GTK_TYPE_THEMING_ENGINE,
1344 theming_engine_value_parse,
1345 theming_engine_value_print);
1346 register_conversion_function (GTK_TYPE_ANIMATION_DESCRIPTION,
1347 animation_description_value_parse,
1348 animation_description_value_print);
1349 register_conversion_function (GTK_TYPE_BORDER,
1351 border_value_print);
1352 register_conversion_function (GTK_TYPE_GRADIENT,
1353 gradient_value_parse,
1354 gradient_value_print);
1355 register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
1356 pattern_value_parse,
1358 register_conversion_function (GTK_TYPE_9SLICE,
1361 register_conversion_function (GTK_TYPE_SHADOW,
1363 shadow_value_print);
1364 register_conversion_function (G_TYPE_ENUM,
1367 register_conversion_function (G_TYPE_FLAGS,
1373 _gtk_style_property_parse_value (const GtkStyleProperty *property,
1375 GtkCssParser *parser,
1378 GtkStyleParseFunc func;
1380 g_return_val_if_fail (value != NULL, FALSE);
1381 g_return_val_if_fail (parser != NULL, FALSE);
1383 css_string_funcs_init ();
1387 if (_gtk_css_parser_try (parser, "none", TRUE))
1389 /* Insert the default value, so it has an opportunity
1390 * to override other style providers when merged
1392 g_param_value_set_default (property->pspec, value);
1395 else if (property->property_parse_func)
1397 GError *error = NULL;
1401 value_str = _gtk_css_parser_read_value (parser);
1402 if (value_str == NULL)
1405 success = (*property->property_parse_func) (value_str, value, &error);
1412 func = property->parse_func;
1418 func = g_hash_table_lookup (parse_funcs,
1419 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
1421 func = g_hash_table_lookup (parse_funcs,
1422 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
1426 _gtk_css_parser_error (parser,
1427 "Cannot convert to type '%s'",
1428 g_type_name (G_VALUE_TYPE (value)));
1432 return (*func) (parser, base, value);
1436 _gtk_style_property_print_value (const GtkStyleProperty *property,
1437 const GValue *value,
1440 GtkStylePrintFunc func;
1442 css_string_funcs_init ();
1445 func = property->print_func;
1450 func = g_hash_table_lookup (print_funcs,
1451 GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
1453 func = g_hash_table_lookup (print_funcs,
1454 GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));
1458 char *s = g_strdup_value_contents (value);
1459 g_string_append (string, s);
1464 func (value, string);
1468 _gtk_style_property_is_shorthand (const GtkStyleProperty *property)
1470 g_return_val_if_fail (property != NULL, FALSE);
1472 return property->pack_func != NULL;
1476 _gtk_style_property_unpack (const GtkStyleProperty *property,
1477 const GValue *value,
1480 g_return_val_if_fail (property != NULL, NULL);
1481 g_return_val_if_fail (property->unpack_func != NULL, NULL);
1482 g_return_val_if_fail (value != NULL, NULL);
1483 g_return_val_if_fail (n_params != NULL, NULL);
1485 return property->unpack_func (value, n_params);
1489 _gtk_style_property_pack (const GtkStyleProperty *property,
1490 GtkStyleProperties *props,
1491 GtkStateFlags state,
1494 g_return_if_fail (property != NULL);
1495 g_return_if_fail (property->pack_func != NULL);
1496 g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
1497 g_return_if_fail (G_IS_VALUE (value));
1499 property->pack_func (value, props, state);
1503 gtk_style_property_init (void)
1507 if (G_LIKELY (properties))
1510 /* stuff is never freed, so no need for free functions */
1511 properties = g_hash_table_new (g_str_hash, g_str_equal);
1513 /* note that gtk_style_properties_register_property() calls this function,
1514 * so make sure we're sanely inited to avoid infloops */
1516 pspec = g_param_spec_boxed ("color",
1520 gtk_style_param_set_inherit (pspec, TRUE);
1521 gtk_style_properties_register_property (NULL, pspec);
1523 gtk_style_properties_register_property (NULL,
1524 g_param_spec_boxed ("background-color",
1529 pspec = g_param_spec_boxed ("font",
1532 PANGO_TYPE_FONT_DESCRIPTION, 0);
1533 gtk_style_param_set_inherit (pspec, TRUE);
1534 gtk_style_properties_register_property (NULL, pspec);
1536 pspec = g_param_spec_boxed ("text-shadow",
1539 GTK_TYPE_SHADOW, 0);
1540 gtk_style_param_set_inherit (pspec, TRUE);
1541 gtk_style_properties_register_property (NULL, pspec);
1543 gtk_style_properties_register_property (NULL,
1544 g_param_spec_int ("margin-top",
1547 0, G_MAXINT, 0, 0));
1548 gtk_style_properties_register_property (NULL,
1549 g_param_spec_int ("margin-left",
1552 0, G_MAXINT, 0, 0));
1553 gtk_style_properties_register_property (NULL,
1554 g_param_spec_int ("margin-bottom",
1557 0, G_MAXINT, 0, 0));
1558 gtk_style_properties_register_property (NULL,
1559 g_param_spec_int ("margin-right",
1562 0, G_MAXINT, 0, 0));
1563 _gtk_style_property_register (g_param_spec_boxed ("margin",
1566 GTK_TYPE_BORDER, 0),
1572 gtk_style_properties_register_property (NULL,
1573 g_param_spec_int ("padding-top",
1576 0, G_MAXINT, 0, 0));
1577 gtk_style_properties_register_property (NULL,
1578 g_param_spec_int ("padding-left",
1581 0, G_MAXINT, 0, 0));
1582 gtk_style_properties_register_property (NULL,
1583 g_param_spec_int ("padding-bottom",
1585 "Padding at bottom",
1586 0, G_MAXINT, 0, 0));
1587 gtk_style_properties_register_property (NULL,
1588 g_param_spec_int ("padding-right",
1591 0, G_MAXINT, 0, 0));
1592 _gtk_style_property_register (g_param_spec_boxed ("padding",
1595 GTK_TYPE_BORDER, 0),
1601 gtk_style_properties_register_property (NULL,
1602 g_param_spec_int ("border-top-width",
1604 "Border width at top",
1605 0, G_MAXINT, 0, 0));
1606 gtk_style_properties_register_property (NULL,
1607 g_param_spec_int ("border-left-width",
1608 "border left width",
1609 "Border width at left",
1610 0, G_MAXINT, 0, 0));
1611 gtk_style_properties_register_property (NULL,
1612 g_param_spec_int ("border-bottom-width",
1613 "border bottom width",
1614 "Border width at bottom",
1615 0, G_MAXINT, 0, 0));
1616 gtk_style_properties_register_property (NULL,
1617 g_param_spec_int ("border-right-width",
1618 "border right width",
1619 "Border width at right",
1620 0, G_MAXINT, 0, 0));
1621 _gtk_style_property_register (g_param_spec_boxed ("border-width",
1623 "Border width, in pixels",
1624 GTK_TYPE_BORDER, 0),
1626 unpack_border_width,
1630 gtk_style_properties_register_property (NULL,
1631 g_param_spec_int ("border-radius",
1633 "Border radius, in pixels",
1634 0, G_MAXINT, 0, 0));
1635 gtk_style_properties_register_property (NULL,
1636 g_param_spec_enum ("border-style",
1639 GTK_TYPE_BORDER_STYLE,
1640 GTK_BORDER_STYLE_NONE, 0));
1641 gtk_style_properties_register_property (NULL,
1642 g_param_spec_boxed ("border-color",
1646 gtk_style_properties_register_property (NULL,
1647 g_param_spec_boxed ("background-image",
1650 CAIRO_GOBJECT_TYPE_PATTERN, 0));
1651 gtk_style_properties_register_property (NULL,
1652 g_param_spec_boxed ("border-image",
1655 GTK_TYPE_9SLICE, 0));
1656 gtk_style_properties_register_property (NULL,
1657 g_param_spec_object ("engine",
1660 GTK_TYPE_THEMING_ENGINE, 0));
1661 gtk_style_properties_register_property (NULL,
1662 g_param_spec_boxed ("transition",
1663 "Transition animation description",
1664 "Transition animation description",
1665 GTK_TYPE_ANIMATION_DESCRIPTION, 0));
1667 /* Private property holding the binding sets */
1668 _gtk_style_property_register (g_param_spec_boxed ("gtk-key-bindings",
1671 G_TYPE_PTR_ARRAY, 0),
1675 bindings_value_parse,
1676 bindings_value_print);
1679 const GtkStyleProperty *
1680 _gtk_style_property_lookup (const char *name)
1682 gtk_style_property_init ();
1684 return g_hash_table_lookup (properties, name);
1688 _gtk_style_property_register (GParamSpec *pspec,
1689 GtkStylePropertyParser property_parse_func,
1690 GtkStyleUnpackFunc unpack_func,
1691 GtkStylePackFunc pack_func,
1692 GtkStyleParseFunc parse_func,
1693 GtkStylePrintFunc print_func)
1695 const GtkStyleProperty *existing;
1696 GtkStyleProperty *node;
1698 g_return_if_fail ((pack_func == NULL) == (unpack_func == NULL));
1700 gtk_style_property_init ();
1702 existing = _gtk_style_property_lookup (pspec->name);
1703 if (existing != NULL)
1705 g_warning ("Property \"%s\" was already registered with type %s",
1706 pspec->name, g_type_name (existing->pspec->value_type));
1710 node = g_slice_new0 (GtkStyleProperty);
1711 node->pspec = pspec;
1712 node->property_parse_func = property_parse_func;
1713 node->pack_func = pack_func;
1714 node->unpack_func = unpack_func;
1715 node->parse_func = parse_func;
1716 node->print_func = print_func;
1718 g_hash_table_insert (properties, pspec->name, node);