2 * Copyright © 2011 Red Hat Inc.
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.1 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 Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Benjamin Otte <otte@gnome.org>
23 #include "gtkcssshorthandpropertyprivate.h"
25 #include <cairo-gobject.h>
28 #include "gtkborderimageprivate.h"
29 #include "gtkcssstylefuncsprivate.h"
30 #include "gtkcsstypesprivate.h"
32 /* this is in case round() is not provided by the compiler,
33 * such as in the case of C89 compilers, like MSVC
35 #include "fallback-c89.c"
40 value_is_done_parsing (GtkCssParser *parser)
42 return _gtk_css_parser_is_eof (parser) ||
43 _gtk_css_parser_begins_with (parser, ';') ||
44 _gtk_css_parser_begins_with (parser, '}');
48 parse_border (GtkCssShorthandProperty *shorthand,
53 GValue temp = G_VALUE_INIT;
56 g_value_init (&temp, GTK_TYPE_BORDER);
57 if (!_gtk_css_style_parse_value (&temp, parser, base))
59 g_value_unset (&temp);
63 border = g_value_get_boxed (&temp);
65 g_value_init (&values[0], G_TYPE_INT);
66 g_value_init (&values[1], G_TYPE_INT);
67 g_value_init (&values[2], G_TYPE_INT);
68 g_value_init (&values[3], G_TYPE_INT);
69 g_value_set_int (&values[0], border->top);
70 g_value_set_int (&values[1], border->right);
71 g_value_set_int (&values[2], border->bottom);
72 g_value_set_int (&values[3], border->left);
74 g_value_unset (&temp);
80 parse_border_radius (GtkCssShorthandProperty *shorthand,
85 GtkCssBorderCornerRadius borders[4];
88 for (i = 0; i < G_N_ELEMENTS (borders); i++)
90 if (!_gtk_css_parser_try_double (parser, &borders[i].horizontal))
92 if (borders[i].horizontal < 0)
94 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
101 _gtk_css_parser_error (parser, "Expected a number");
105 /* The magic (i - 1) >> 1 below makes it take the correct value
106 * according to spec. Feel free to check the 4 cases */
107 for (; i < G_N_ELEMENTS (borders); i++)
108 borders[i].horizontal = borders[(i - 1) >> 1].horizontal;
110 if (_gtk_css_parser_try (parser, "/", TRUE))
112 for (i = 0; i < G_N_ELEMENTS (borders); i++)
114 if (!_gtk_css_parser_try_double (parser, &borders[i].vertical))
116 if (borders[i].vertical < 0)
118 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
125 _gtk_css_parser_error (parser, "Expected a number");
129 for (; i < G_N_ELEMENTS (borders); i++)
130 borders[i].vertical = borders[(i - 1) >> 1].vertical;
135 for (i = 0; i < G_N_ELEMENTS (borders); i++)
136 borders[i].vertical = borders[i].horizontal;
139 for (i = 0; i < G_N_ELEMENTS (borders); i++)
141 g_value_init (&values[i], GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
142 g_value_set_boxed (&values[i], &borders[i]);
149 parse_border_color (GtkCssShorthandProperty *shorthand,
151 GtkCssParser *parser,
154 GtkSymbolicColor *symbolic;
157 for (i = 0; i < 4; i++)
159 symbolic = _gtk_css_parser_read_symbolic_color (parser);
160 if (symbolic == NULL)
163 g_value_init (&values[i], GTK_TYPE_SYMBOLIC_COLOR);
164 g_value_set_boxed (&values[i], symbolic);
166 if (value_is_done_parsing (parser))
170 for (i++; i < 4; i++)
172 g_value_init (&values[i], GTK_TYPE_SYMBOLIC_COLOR);
173 g_value_copy (&values[(i - 1) >> 1], &values[i]);
180 parse_border_image (GtkCssShorthandProperty *shorthand,
182 GtkCssParser *parser,
187 if (_gtk_css_parser_try (parser, "none", TRUE))
191 image = _gtk_css_image_new_parse (parser, base);
195 g_value_init (&values[0], GTK_TYPE_CSS_IMAGE);
196 g_value_set_object (&values[0], image);
198 if (value_is_done_parsing (parser))
201 g_value_init (&values[1], GTK_TYPE_BORDER);
202 if (!_gtk_css_style_parse_value (&values[1], parser, base))
205 if (_gtk_css_parser_try (parser, "/", TRUE))
207 g_value_init (&values[2], GTK_TYPE_BORDER);
208 if (!_gtk_css_style_parse_value (&values[2], parser, base))
212 if (value_is_done_parsing (parser))
215 g_value_init (&values[3], GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
216 if (!_gtk_css_style_parse_value (&values[3], parser, base))
223 parse_font (GtkCssShorthandProperty *shorthand,
225 GtkCssParser *parser,
228 PangoFontDescription *desc;
232 str = _gtk_css_parser_read_value (parser);
236 desc = pango_font_description_from_string (str);
239 mask = pango_font_description_get_set_fields (desc);
241 if (mask & PANGO_FONT_MASK_FAMILY)
243 GPtrArray *strv = g_ptr_array_new ();
245 g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (desc)));
246 g_ptr_array_add (strv, NULL);
247 g_value_init (&values[0], G_TYPE_STRV);
248 g_value_take_boxed (&values[0], g_ptr_array_free (strv, FALSE));
250 if (mask & PANGO_FONT_MASK_STYLE)
252 g_value_init (&values[1], PANGO_TYPE_STYLE);
253 g_value_set_enum (&values[1], pango_font_description_get_style (desc));
255 if (mask & PANGO_FONT_MASK_VARIANT)
257 g_value_init (&values[2], PANGO_TYPE_VARIANT);
258 g_value_set_enum (&values[2], pango_font_description_get_variant (desc));
260 if (mask & PANGO_FONT_MASK_WEIGHT)
262 g_value_init (&values[3], PANGO_TYPE_WEIGHT);
263 g_value_set_enum (&values[3], pango_font_description_get_weight (desc));
265 if (mask & PANGO_FONT_MASK_SIZE)
267 g_value_init (&values[4], G_TYPE_DOUBLE);
268 g_value_set_double (&values[4],
269 (double) pango_font_description_get_size (desc) / PANGO_SCALE);
272 pango_font_description_free (desc);
280 unpack_border (const GValue *value,
287 GParameter *parameter = g_new0 (GParameter, 4);
288 GtkBorder *border = g_value_get_boxed (value);
290 parameter[0].name = top;
291 g_value_init (¶meter[0].value, G_TYPE_INT);
292 g_value_set_int (¶meter[0].value, border->top);
293 parameter[1].name = left;
294 g_value_init (¶meter[1].value, G_TYPE_INT);
295 g_value_set_int (¶meter[1].value, border->left);
296 parameter[2].name = bottom;
297 g_value_init (¶meter[2].value, G_TYPE_INT);
298 g_value_set_int (¶meter[2].value, border->bottom);
299 parameter[3].name = right;
300 g_value_init (¶meter[3].value, G_TYPE_INT);
301 g_value_set_int (¶meter[3].value, border->right);
308 pack_border (GValue *value,
309 GtkStyleProperties *props,
319 gtk_style_properties_get (props,
332 g_value_set_boxed (value, &border);
336 unpack_border_width (const GValue *value,
339 return unpack_border (value, n_params,
340 "border-top-width", "border-left-width",
341 "border-bottom-width", "border-right-width");
345 pack_border_width (GValue *value,
346 GtkStyleProperties *props,
349 pack_border (value, props, state,
350 "border-top-width", "border-left-width",
351 "border-bottom-width", "border-right-width");
355 unpack_padding (const GValue *value,
358 return unpack_border (value, n_params,
359 "padding-top", "padding-left",
360 "padding-bottom", "padding-right");
364 pack_padding (GValue *value,
365 GtkStyleProperties *props,
368 pack_border (value, props, state,
369 "padding-top", "padding-left",
370 "padding-bottom", "padding-right");
374 unpack_margin (const GValue *value,
377 return unpack_border (value, n_params,
378 "margin-top", "margin-left",
379 "margin-bottom", "margin-right");
383 pack_margin (GValue *value,
384 GtkStyleProperties *props,
387 pack_border (value, props, state,
388 "margin-top", "margin-left",
389 "margin-bottom", "margin-right");
393 unpack_border_radius (const GValue *value,
396 GParameter *parameter = g_new0 (GParameter, 4);
397 GtkCssBorderCornerRadius border;
399 border.horizontal = border.vertical = g_value_get_int (value);
401 parameter[0].name = "border-top-left-radius";
402 g_value_init (¶meter[0].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
403 g_value_set_boxed (¶meter[0].value, &border);
404 parameter[1].name = "border-top-right-radius";
405 g_value_init (¶meter[1].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
406 g_value_set_boxed (¶meter[1].value, &border);
407 parameter[2].name = "border-bottom-right-radius";
408 g_value_init (¶meter[2].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
409 g_value_set_boxed (¶meter[2].value, &border);
410 parameter[3].name = "border-bottom-left-radius";
411 g_value_init (¶meter[3].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
412 g_value_set_boxed (¶meter[3].value, &border);
419 pack_border_radius (GValue *value,
420 GtkStyleProperties *props,
423 GtkCssBorderCornerRadius *top_left;
425 /* NB: We are an int property, so we have to resolve to an int here.
426 * So we just resolve to an int. We pick one and stick to it.
427 * Lesson learned: Don't query border-radius shorthand, query the
428 * real properties instead. */
429 gtk_style_properties_get (props,
431 "border-top-left-radius", &top_left,
435 g_value_set_int (value, top_left->horizontal);
441 unpack_font_description (const GValue *value,
444 GParameter *parameter = g_new0 (GParameter, 5);
445 PangoFontDescription *description;
449 /* For backwards compat, we only unpack values that are indeed set.
450 * For strict CSS conformance we need to unpack all of them.
451 * Note that we do set all of them in the parse function, so it
452 * will not have effects when parsing CSS files. It will though
453 * for custom style providers.
456 description = g_value_get_boxed (value);
460 mask = pango_font_description_get_set_fields (description);
464 if (mask & PANGO_FONT_MASK_FAMILY)
466 GPtrArray *strv = g_ptr_array_new ();
468 g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
469 g_ptr_array_add (strv, NULL);
470 parameter[n].name = "font-family";
471 g_value_init (¶meter[n].value, G_TYPE_STRV);
472 g_value_take_boxed (¶meter[n].value,
473 g_ptr_array_free (strv, FALSE));
477 if (mask & PANGO_FONT_MASK_STYLE)
479 parameter[n].name = "font-style";
480 g_value_init (¶meter[n].value, PANGO_TYPE_STYLE);
481 g_value_set_enum (¶meter[n].value,
482 pango_font_description_get_style (description));
486 if (mask & PANGO_FONT_MASK_VARIANT)
488 parameter[n].name = "font-variant";
489 g_value_init (¶meter[n].value, PANGO_TYPE_VARIANT);
490 g_value_set_enum (¶meter[n].value,
491 pango_font_description_get_variant (description));
495 if (mask & PANGO_FONT_MASK_WEIGHT)
497 parameter[n].name = "font-weight";
498 g_value_init (¶meter[n].value, PANGO_TYPE_WEIGHT);
499 g_value_set_enum (¶meter[n].value,
500 pango_font_description_get_weight (description));
504 if (mask & PANGO_FONT_MASK_SIZE)
506 parameter[n].name = "font-size";
507 g_value_init (¶meter[n].value, G_TYPE_DOUBLE);
508 g_value_set_double (¶meter[n].value,
509 (double) pango_font_description_get_size (description) / PANGO_SCALE);
519 pack_font_description (GValue *value,
520 GtkStyleProperties *props,
523 PangoFontDescription *description;
526 PangoVariant variant;
530 gtk_style_properties_get (props,
532 "font-family", &families,
533 "font-style", &style,
534 "font-variant", &variant,
535 "font-weight", &weight,
539 description = pango_font_description_new ();
540 /* xxx: Can we set all the families here somehow? */
542 pango_font_description_set_family (description, families[0]);
543 pango_font_description_set_size (description, round (size * PANGO_SCALE));
544 pango_font_description_set_style (description, style);
545 pango_font_description_set_variant (description, variant);
546 pango_font_description_set_weight (description, weight);
548 g_strfreev (families);
550 g_value_take_boxed (value, description);
554 unpack_border_color (const GValue *value,
557 GParameter *parameter = g_new0 (GParameter, 4);
560 type = G_VALUE_TYPE (value);
561 if (type == G_TYPE_PTR_ARRAY)
562 type = GTK_TYPE_SYMBOLIC_COLOR;
564 parameter[0].name = "border-top-color";
565 g_value_init (¶meter[0].value, type);
566 parameter[1].name = "border-right-color";
567 g_value_init (¶meter[1].value, type);
568 parameter[2].name = "border-bottom-color";
569 g_value_init (¶meter[2].value, type);
570 parameter[3].name = "border-left-color";
571 g_value_init (¶meter[3].value, type);
573 if (G_VALUE_TYPE (value) == G_TYPE_PTR_ARRAY)
575 GPtrArray *array = g_value_get_boxed (value);
578 for (i = 0; i < 4; i++)
579 g_value_set_boxed (¶meter[i].value, g_ptr_array_index (array, i));
583 /* can be RGBA or symbolic color */
584 gpointer p = g_value_get_boxed (value);
586 g_value_set_boxed (¶meter[0].value, p);
587 g_value_set_boxed (¶meter[1].value, p);
588 g_value_set_boxed (¶meter[2].value, p);
589 g_value_set_boxed (¶meter[3].value, p);
597 pack_border_color (GValue *value,
598 GtkStyleProperties *props,
601 /* NB: We are a color property, so we have to resolve to a color here.
602 * So we just resolve to a color. We pick one and stick to it.
603 * Lesson learned: Don't query border-color shorthand, query the
604 * real properties instead. */
605 g_value_unset (value);
606 gtk_style_properties_get_property (props, "border-top-color", state, value);
610 _gtk_css_shorthand_property_register (const char *name,
612 const char **subproperties,
613 GtkCssShorthandPropertyParseFunc parse_func,
614 GtkStyleUnpackFunc unpack_func,
615 GtkStylePackFunc pack_func)
617 GtkStyleProperty *node;
619 g_return_if_fail (pack_func != NULL);
620 g_return_if_fail (unpack_func != NULL);
622 node = g_object_new (GTK_TYPE_CSS_SHORTHAND_PROPERTY,
624 "value-type", value_type,
625 "subproperties", subproperties,
628 GTK_CSS_SHORTHAND_PROPERTY (node)->parse = parse_func;
629 node->pack_func = pack_func;
630 node->unpack_func = unpack_func;
634 _gtk_css_shorthand_property_init_properties (void)
636 /* The order is important here, be careful when changing it */
637 const char *font_subproperties[] = { "font-family", "font-style", "font-variant", "font-weight", "font-size", NULL };
638 const char *margin_subproperties[] = { "margin-top", "margin-right", "margin-bottom", "margin-left", NULL };
639 const char *padding_subproperties[] = { "padding-top", "padding-right", "padding-bottom", "padding-left", NULL };
640 const char *border_width_subproperties[] = { "border-top-width", "border-right-width", "border-bottom-width", "border-left-width", NULL };
641 const char *border_radius_subproperties[] = { "border-top-left-radius", "border-top-right-radius",
642 "border-bottom-right-radius", "border-bottom-left-radius", NULL };
643 const char *border_color_subproperties[] = { "border-top-color", "border-right-color", "border-bottom-color", "border-left-color", NULL };
644 const char *border_image_subproperties[] = { "border-image-source", "border-image-slice", "border-image-width", "border-image-repeat", NULL };
646 _gtk_css_shorthand_property_register ("font",
647 PANGO_TYPE_FONT_DESCRIPTION,
650 unpack_font_description,
651 pack_font_description);
652 _gtk_css_shorthand_property_register ("margin",
654 margin_subproperties,
658 _gtk_css_shorthand_property_register ("padding",
660 padding_subproperties,
664 _gtk_css_shorthand_property_register ("border-width",
666 border_width_subproperties,
670 _gtk_css_shorthand_property_register ("border-radius",
672 border_radius_subproperties,
674 unpack_border_radius,
676 _gtk_css_shorthand_property_register ("border-color",
678 border_color_subproperties,
682 _gtk_css_shorthand_property_register ("border-image",
683 GTK_TYPE_BORDER_IMAGE,
684 border_image_subproperties,
686 _gtk_border_image_unpack,
687 _gtk_border_image_pack);