X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=gtk%2Fgtkcssshorthandpropertyimpl.c;h=b01494b7da888fd165f144482b339b94423f49d6;hb=d97861bd8b338c3d25d7ffb5496edee9eee9bfbb;hp=c13f0c21be7adcaa031e1f75cc56eb36bd6e5d7b;hpb=53b2f05a648141b5057f5161623e739b80c02710;p=~andy%2Fgtk diff --git a/gtk/gtkcssshorthandpropertyimpl.c b/gtk/gtkcssshorthandpropertyimpl.c index c13f0c21b..b01494b7d 100644 --- a/gtk/gtkcssshorthandpropertyimpl.c +++ b/gtk/gtkcssshorthandpropertyimpl.c @@ -12,8 +12,7 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * License along with this library. If not, see . * * Authors: Benjamin Otte */ @@ -25,10 +24,21 @@ #include #include +#include "gtkcssarrayvalueprivate.h" +#include "gtkcssbgsizevalueprivate.h" +#include "gtkcssbordervalueprivate.h" +#include "gtkcsscolorvalueprivate.h" +#include "gtkcsscornervalueprivate.h" +#include "gtkcsseasevalueprivate.h" +#include "gtkcssenumvalueprivate.h" #include "gtkcssimageprivate.h" +#include "gtkcssimagevalueprivate.h" +#include "gtkcssnumbervalueprivate.h" +#include "gtkcsspositionvalueprivate.h" +#include "gtkcssrepeatvalueprivate.h" +#include "gtkcssstringvalueprivate.h" #include "gtkcssstylefuncsprivate.h" -#include "gtkcsstypesprivate.h" -#include "gtkprivatetypebuiltins.h" +#include "gtkcssvalueprivate.h" #include "gtkstylepropertiesprivate.h" #include "gtktypebuiltins.h" @@ -43,154 +53,197 @@ static gboolean value_is_done_parsing (GtkCssParser *parser) { return _gtk_css_parser_is_eof (parser) || + _gtk_css_parser_begins_with (parser, ',') || _gtk_css_parser_begins_with (parser, ';') || _gtk_css_parser_begins_with (parser, '}'); } static gboolean -parse_border_width (GtkCssShorthandProperty *shorthand, - GValue *values, - GtkCssParser *parser, - GFile *base) +parse_four_numbers (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser, + GtkCssNumberParseFlags flags) { - GValue temp = G_VALUE_INIT; - GtkBorder *border; + guint i; + + for (i = 0; i < 4; i++) + { + if (!_gtk_css_parser_has_number (parser)) + break; - g_value_init (&temp, GTK_TYPE_BORDER); - if (!_gtk_css_style_parse_value (&temp, parser, base)) + values[i] = _gtk_css_number_value_parse (parser, flags); + if (values[i] == NULL) + return FALSE; + } + + if (i == 0) { - g_value_unset (&temp); + _gtk_css_parser_error (parser, "Expected a length"); return FALSE; } - border = g_value_get_boxed (&temp); + for (; i < 4; i++) + { + values[i] = _gtk_css_value_ref (values[(i - 1) >> 1]); + } - g_value_init (&values[0], G_TYPE_INT); - g_value_init (&values[1], G_TYPE_INT); - g_value_init (&values[2], G_TYPE_INT); - g_value_init (&values[3], G_TYPE_INT); - g_value_set_int (&values[0], border->top); - g_value_set_int (&values[1], border->right); - g_value_set_int (&values[2], border->bottom); - g_value_set_int (&values[3], border->left); + return TRUE; +} - g_value_unset (&temp); +static gboolean +parse_margin (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) +{ + return parse_four_numbers (shorthand, + values, + parser, + GTK_CSS_NUMBER_AS_PIXELS + | GTK_CSS_PARSE_LENGTH); +} - return TRUE; +static gboolean +parse_padding (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) +{ + return parse_four_numbers (shorthand, + values, + parser, + GTK_CSS_POSITIVE_ONLY + | GTK_CSS_NUMBER_AS_PIXELS + | GTK_CSS_PARSE_LENGTH); +} + +static gboolean +parse_border_width (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) +{ + return parse_four_numbers (shorthand, + values, + parser, + GTK_CSS_POSITIVE_ONLY + | GTK_CSS_NUMBER_AS_PIXELS + | GTK_CSS_PARSE_LENGTH); } static gboolean -parse_border_radius (GtkCssShorthandProperty *shorthand, - GValue *values, - GtkCssParser *parser, - GFile *base) +parse_border_radius (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) { - GtkCssBorderCornerRadius borders[4]; + GtkCssValue *x[4] = { NULL, }, *y[4] = { NULL, }; guint i; - for (i = 0; i < G_N_ELEMENTS (borders); i++) + for (i = 0; i < 4; i++) { - if (!_gtk_css_parser_try_double (parser, &borders[i].horizontal)) + if (!_gtk_css_parser_has_number (parser)) break; - if (borders[i].horizontal < 0) - { - _gtk_css_parser_error (parser, "Border radius values cannot be negative"); - return FALSE; - } + x[i] = _gtk_css_number_value_parse (parser, + GTK_CSS_POSITIVE_ONLY + | GTK_CSS_PARSE_PERCENT + | GTK_CSS_NUMBER_AS_PIXELS + | GTK_CSS_PARSE_LENGTH); + if (x[i] == NULL) + goto fail; } if (i == 0) { _gtk_css_parser_error (parser, "Expected a number"); - return FALSE; + goto fail; } /* The magic (i - 1) >> 1 below makes it take the correct value * according to spec. Feel free to check the 4 cases */ - for (; i < G_N_ELEMENTS (borders); i++) - borders[i].horizontal = borders[(i - 1) >> 1].horizontal; + for (; i < 4; i++) + x[i] = _gtk_css_value_ref (x[(i - 1) >> 1]); if (_gtk_css_parser_try (parser, "/", TRUE)) { - for (i = 0; i < G_N_ELEMENTS (borders); i++) + for (i = 0; i < 4; i++) { - if (!_gtk_css_parser_try_double (parser, &borders[i].vertical)) + if (!_gtk_css_parser_has_number (parser)) break; - if (borders[i].vertical < 0) - { - _gtk_css_parser_error (parser, "Border radius values cannot be negative"); - return FALSE; - } + y[i] = _gtk_css_number_value_parse (parser, + GTK_CSS_POSITIVE_ONLY + | GTK_CSS_PARSE_PERCENT + | GTK_CSS_NUMBER_AS_PIXELS + | GTK_CSS_PARSE_LENGTH); + if (y[i] == NULL) + goto fail; } if (i == 0) { _gtk_css_parser_error (parser, "Expected a number"); - return FALSE; + goto fail; } - for (; i < G_N_ELEMENTS (borders); i++) - borders[i].vertical = borders[(i - 1) >> 1].vertical; - + for (; i < 4; i++) + y[i] = _gtk_css_value_ref (y[(i - 1) >> 1]); } else { - for (i = 0; i < G_N_ELEMENTS (borders); i++) - borders[i].vertical = borders[i].horizontal; + for (i = 0; i < 4; i++) + y[i] = _gtk_css_value_ref (x[i]); } - for (i = 0; i < G_N_ELEMENTS (borders); i++) + for (i = 0; i < 4; i++) { - g_value_init (&values[i], GTK_TYPE_CSS_BORDER_CORNER_RADIUS); - g_value_set_boxed (&values[i], &borders[i]); + values[i] = _gtk_css_corner_value_new (x[i], y[i]); } return TRUE; + +fail: + for (i = 0; i < 4; i++) + { + if (x[i]) + _gtk_css_value_unref (x[i]); + if (y[i]) + _gtk_css_value_unref (y[i]); + } + return FALSE; } static gboolean -parse_border_color (GtkCssShorthandProperty *shorthand, - GValue *values, - GtkCssParser *parser, - GFile *base) +parse_border_color (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) { - GtkSymbolicColor *symbolic; guint i; for (i = 0; i < 4; i++) { - symbolic = _gtk_css_parser_read_symbolic_color (parser); - if (symbolic == NULL) + values[i] = _gtk_css_color_value_parse (parser); + if (values[i] == NULL) return FALSE; - g_value_init (&values[i], GTK_TYPE_SYMBOLIC_COLOR); - g_value_set_boxed (&values[i], symbolic); - if (value_is_done_parsing (parser)) break; } for (i++; i < 4; i++) { - g_value_init (&values[i], GTK_TYPE_SYMBOLIC_COLOR); - g_value_copy (&values[(i - 1) >> 1], &values[i]); + values[i] = _gtk_css_value_ref (values[(i - 1) >> 1]); } return TRUE; } static gboolean -parse_border_style (GtkCssShorthandProperty *shorthand, - GValue *values, - GtkCssParser *parser, - GFile *base) +parse_border_style (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) { - GtkBorderStyle styles[4]; guint i; for (i = 0; i < 4; i++) { - if (!_gtk_css_parser_try_enum (parser, GTK_TYPE_BORDER_STYLE, (int *)&styles[i])) + values[i] = _gtk_css_border_style_value_try_parse (parser); + if (values[i] == NULL) break; } @@ -200,101 +253,105 @@ parse_border_style (GtkCssShorthandProperty *shorthand, return FALSE; } - for (; i < G_N_ELEMENTS (styles); i++) - styles[i] = styles[(i - 1) >> 1]; - - for (i = 0; i < G_N_ELEMENTS (styles); i++) - { - g_value_init (&values[i], GTK_TYPE_BORDER_STYLE); - g_value_set_enum (&values[i], styles[i]); - } + for (; i < 4; i++) + values[i] = _gtk_css_value_ref (values[(i - 1) >> 1]); return TRUE; } static gboolean -parse_border_image (GtkCssShorthandProperty *shorthand, - GValue *values, - GtkCssParser *parser, - GFile *base) +parse_border_image (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) { - GtkCssImage *image; - - if (_gtk_css_parser_try (parser, "none", TRUE)) - image = NULL; - else + do { - image = _gtk_css_image_new_parse (parser, base); - if (!image) - return FALSE; - } - g_value_init (&values[0], GTK_TYPE_CSS_IMAGE); - g_value_set_object (&values[0], image); + if (values[0] == NULL && + (_gtk_css_parser_has_prefix (parser, "none") || + _gtk_css_image_can_parse (parser))) + { + GtkCssImage *image; - if (value_is_done_parsing (parser)) - return TRUE; + if (_gtk_css_parser_try (parser, "none", TRUE)) + image = NULL; + else + { + image = _gtk_css_image_new_parse (parser); + if (image == NULL) + return FALSE; + } - g_value_init (&values[1], GTK_TYPE_BORDER); - if (!_gtk_css_style_parse_value (&values[1], parser, base)) - return FALSE; + values[0] = _gtk_css_image_value_new (image); + } + else if (values[3] == NULL && + (values[3] = _gtk_css_border_repeat_value_try_parse (parser))) + { + /* please move along */ + } + else if (values[1] == NULL) + { + values[1] = _gtk_css_border_value_parse (parser, + GTK_CSS_PARSE_PERCENT + | GTK_CSS_PARSE_NUMBER + | GTK_CSS_POSITIVE_ONLY, + FALSE, + TRUE); + if (values[1] == NULL) + return FALSE; - if (_gtk_css_parser_try (parser, "/", TRUE)) - { - g_value_init (&values[2], GTK_TYPE_BORDER); - if (!_gtk_css_style_parse_value (&values[2], parser, base)) - return FALSE; + if (_gtk_css_parser_try (parser, "/", TRUE)) + { + values[2] = _gtk_css_border_value_parse (parser, + GTK_CSS_PARSE_PERCENT + | GTK_CSS_PARSE_LENGTH + | GTK_CSS_PARSE_NUMBER + | GTK_CSS_POSITIVE_ONLY, + TRUE, + FALSE); + if (values[2] == NULL) + return FALSE; + } + } + else + { + /* We parsed everything and there's still stuff left? + * Pretend we didn't notice and let the normal code produce + * a 'junk at end of value' error */ + break; + } } - - if (value_is_done_parsing (parser)) - return TRUE; - - g_value_init (&values[3], GTK_TYPE_CSS_BORDER_IMAGE_REPEAT); - if (!_gtk_css_style_parse_value (&values[3], parser, base)) - return FALSE; + while (!value_is_done_parsing (parser)); return TRUE; } static gboolean -parse_border_side (GtkCssShorthandProperty *shorthand, - GValue *values, - GtkCssParser *parser, - GFile *base) +parse_border_side (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) { - int width; - int style; - do { - if (!G_IS_VALUE (&values[0]) && - _gtk_css_parser_try_length (parser, &width)) + if (values[0] == NULL && + _gtk_css_parser_has_number (parser)) { - g_value_init (&values[0], G_TYPE_INT); - g_value_set_int (&values[0], width); + values[0] = _gtk_css_number_value_parse (parser, + GTK_CSS_POSITIVE_ONLY + | GTK_CSS_NUMBER_AS_PIXELS + | GTK_CSS_PARSE_LENGTH); + if (values[0] == NULL) + return FALSE; } - else if (!G_IS_VALUE (&values[1]) && - _gtk_css_parser_try_enum (parser, GTK_TYPE_BORDER_STYLE, &style)) + else if (values[1] == NULL && + (values[1] = _gtk_css_border_style_value_try_parse (parser))) { - g_value_init (&values[1], GTK_TYPE_BORDER_STYLE); - g_value_set_enum (&values[1], style); + /* Nothing to do */ } - else if (!G_IS_VALUE (&values[2])) + else if (values[2] == NULL) { - GtkSymbolicColor *symbolic; - - symbolic = _gtk_css_parser_read_symbolic_color (parser); - if (symbolic == NULL) + values[2] = _gtk_css_color_value_parse (parser); + if (values[2] == NULL) return FALSE; - - g_value_init (&values[2], GTK_TYPE_SYMBOLIC_COLOR); - g_value_take_boxed (&values[2], symbolic); - } - else - { - /* We parsed everything and there's still stuff left? - * Pretend we didn't notice and let the normal code produce - * a 'junk at end of value' error */ - break; } } while (!value_is_done_parsing (parser)); @@ -303,56 +360,41 @@ parse_border_side (GtkCssShorthandProperty *shorthand, } static gboolean -parse_border (GtkCssShorthandProperty *shorthand, - GValue *values, - GtkCssParser *parser, - GFile *base) +parse_border (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) { - int width; - int style; - do { - if (!G_IS_VALUE (&values[0]) && - _gtk_css_parser_try_length (parser, &width)) + if (values[0] == NULL && + _gtk_css_parser_has_number (parser)) { - g_value_init (&values[0], G_TYPE_INT); - g_value_init (&values[1], G_TYPE_INT); - g_value_init (&values[2], G_TYPE_INT); - g_value_init (&values[3], G_TYPE_INT); - g_value_set_int (&values[0], width); - g_value_set_int (&values[1], width); - g_value_set_int (&values[2], width); - g_value_set_int (&values[3], width); + values[0] = _gtk_css_number_value_parse (parser, + GTK_CSS_POSITIVE_ONLY + | GTK_CSS_NUMBER_AS_PIXELS + | GTK_CSS_PARSE_LENGTH); + if (values[0] == NULL) + return FALSE; + values[1] = _gtk_css_value_ref (values[0]); + values[2] = _gtk_css_value_ref (values[0]); + values[3] = _gtk_css_value_ref (values[0]); } - else if (!G_IS_VALUE (&values[4]) && - _gtk_css_parser_try_enum (parser, GTK_TYPE_BORDER_STYLE, &style)) + else if (values[4] == NULL && + (values[4] = _gtk_css_border_style_value_try_parse (parser))) { - g_value_init (&values[4], GTK_TYPE_BORDER_STYLE); - g_value_init (&values[5], GTK_TYPE_BORDER_STYLE); - g_value_init (&values[6], GTK_TYPE_BORDER_STYLE); - g_value_init (&values[7], GTK_TYPE_BORDER_STYLE); - g_value_set_enum (&values[4], style); - g_value_set_enum (&values[5], style); - g_value_set_enum (&values[6], style); - g_value_set_enum (&values[7], style); + values[5] = _gtk_css_value_ref (values[4]); + values[6] = _gtk_css_value_ref (values[4]); + values[7] = _gtk_css_value_ref (values[4]); } else if (!G_IS_VALUE (&values[8])) { - GtkSymbolicColor *symbolic; - - symbolic = _gtk_css_parser_read_symbolic_color (parser); - if (symbolic == NULL) + values[8] = _gtk_css_color_value_parse (parser); + if (values[8] == NULL) return FALSE; - g_value_init (&values[8], GTK_TYPE_SYMBOLIC_COLOR); - g_value_init (&values[9], GTK_TYPE_SYMBOLIC_COLOR); - g_value_init (&values[10], GTK_TYPE_SYMBOLIC_COLOR); - g_value_init (&values[11], GTK_TYPE_SYMBOLIC_COLOR); - g_value_set_boxed (&values[8], symbolic); - g_value_set_boxed (&values[9], symbolic); - g_value_set_boxed (&values[10], symbolic); - g_value_take_boxed (&values[11], symbolic); + values[9] = _gtk_css_value_ref (values[8]); + values[10] = _gtk_css_value_ref (values[8]); + values[11] = _gtk_css_value_ref (values[8]); } else { @@ -371,10 +413,9 @@ parse_border (GtkCssShorthandProperty *shorthand, } static gboolean -parse_font (GtkCssShorthandProperty *shorthand, - GValue *values, - GtkCssParser *parser, - GFile *base) +parse_font (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) { PangoFontDescription *desc; guint mask; @@ -391,33 +432,23 @@ parse_font (GtkCssShorthandProperty *shorthand, if (mask & PANGO_FONT_MASK_FAMILY) { - GPtrArray *strv = g_ptr_array_new (); - - g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (desc))); - g_ptr_array_add (strv, NULL); - g_value_init (&values[0], G_TYPE_STRV); - g_value_take_boxed (&values[0], g_ptr_array_free (strv, FALSE)); + values[0] = _gtk_css_array_value_new (_gtk_css_string_value_new (pango_font_description_get_family (desc))); } if (mask & PANGO_FONT_MASK_STYLE) { - g_value_init (&values[1], PANGO_TYPE_STYLE); - g_value_set_enum (&values[1], pango_font_description_get_style (desc)); + values[1] = _gtk_css_font_style_value_new (pango_font_description_get_style (desc)); } if (mask & PANGO_FONT_MASK_VARIANT) { - g_value_init (&values[2], PANGO_TYPE_VARIANT); - g_value_set_enum (&values[2], pango_font_description_get_variant (desc)); + values[2] = _gtk_css_font_variant_value_new (pango_font_description_get_variant (desc)); } if (mask & PANGO_FONT_MASK_WEIGHT) { - g_value_init (&values[3], PANGO_TYPE_WEIGHT); - g_value_set_enum (&values[3], pango_font_description_get_weight (desc)); + values[3] = _gtk_css_font_weight_value_new (pango_font_description_get_weight (desc)); } if (mask & PANGO_FONT_MASK_SIZE) { - g_value_init (&values[4], G_TYPE_DOUBLE); - g_value_set_double (&values[4], - (double) pango_font_description_get_size (desc) / PANGO_SCALE); + values[4] = _gtk_css_number_value_new ((double) pango_font_description_get_size (desc) / PANGO_SCALE, GTK_CSS_PX); } pango_font_description_free (desc); @@ -426,17 +457,16 @@ parse_font (GtkCssShorthandProperty *shorthand, } static gboolean -parse_background (GtkCssShorthandProperty *shorthand, - GValue *values, - GtkCssParser *parser, - GFile *base) +parse_one_background (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) { - int enum_value; + GtkCssValue *value = NULL; do { /* the image part */ - if (!G_IS_VALUE (&values[0]) && + if (values[0] == NULL && (_gtk_css_parser_has_prefix (parser, "none") || _gtk_css_image_can_parse (parser))) { @@ -446,55 +476,276 @@ parse_background (GtkCssShorthandProperty *shorthand, image = NULL; else { - image = _gtk_css_image_new_parse (parser, base); + image = _gtk_css_image_new_parse (parser); if (image == NULL) return FALSE; } - g_value_init (&values[0], GTK_TYPE_CSS_IMAGE); - g_value_take_object (&values[0], image); + values[0] = _gtk_css_image_value_new (image); } - else if (!G_IS_VALUE (&values[1]) && - _gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &enum_value)) + else if (values[1] == NULL && + (value = _gtk_css_position_value_try_parse (parser))) { - if (enum_value <= GTK_CSS_BACKGROUND_REPEAT_MASK) + values[1] = value; + value = NULL; + + if (_gtk_css_parser_try (parser, "/", TRUE) && + (value = _gtk_css_bg_size_value_parse (parser))) { - int vertical; - - if (_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &vertical)) - { - if (vertical >= GTK_CSS_BACKGROUND_REPEAT_MASK) - { - _gtk_css_parser_error (parser, "Not a valid 2nd value for border-repeat"); - return FALSE; - } - else - enum_value |= vertical << GTK_CSS_BACKGROUND_REPEAT_SHIFT; - } - else - enum_value |= enum_value << GTK_CSS_BACKGROUND_REPEAT_SHIFT; + values[2] = value; + value = NULL; } + } + else if (values[3] == NULL && + (value = _gtk_css_background_repeat_value_try_parse (parser))) + { + values[3] = value; + value = NULL; + } + else if ((values[4] == NULL || values[5] == NULL) && + (value = _gtk_css_area_value_try_parse (parser))) + { + values[4] = value; - g_value_init (&values[1], GTK_TYPE_CSS_BACKGROUND_REPEAT); - g_value_set_enum (&values[1], enum_value); + if (values[5] == NULL) + { + values[5] = values[4]; + values[4] = NULL; + } + value = NULL; } - else if ((!G_IS_VALUE (&values[2]) || !G_IS_VALUE (&values[3])) && - _gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_AREA, &enum_value)) + else if (values[6] == NULL) { - guint idx = !G_IS_VALUE (&values[2]) ? 2 : 3; - g_value_init (&values[idx], GTK_TYPE_CSS_AREA); - g_value_set_enum (&values[idx], enum_value); + value = _gtk_css_color_value_parse (parser); + if (value == NULL) + values[6] = _gtk_css_value_ref (_gtk_css_style_property_get_initial_value + (_gtk_css_shorthand_property_get_subproperty (shorthand, 6))); + else + values[6] = value; + + value = NULL; } - else if (!G_IS_VALUE (&values[4])) + else { - GtkSymbolicColor *symbolic; - - symbolic = _gtk_css_parser_read_symbolic_color (parser); - if (symbolic == NULL) + /* We parsed everything and there's still stuff left? + * Pretend we didn't notice and let the normal code produce + * a 'junk at end of value' error */ + break; + } + } + while (!value_is_done_parsing (parser)); + + if (values[5] != NULL && values[4] == NULL) + values[4] = _gtk_css_value_ref (values[5]); + + return TRUE; +} + +static gboolean +parse_background (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) +{ + GtkCssValue *step_values[7]; + GPtrArray *arrays[6]; + guint i; + + for (i = 0; i < 6; i++) + { + arrays[i] = g_ptr_array_new (); + step_values[i] = NULL; + } + + step_values[6] = NULL; + + do { + if (!parse_one_background (shorthand, step_values, parser)) + { + for (i = 0; i < 6; i++) + { + g_ptr_array_set_free_func (arrays[i], (GDestroyNotify) _gtk_css_value_unref); + g_ptr_array_unref (arrays[i]); + } + return FALSE; + } + + for (i = 0; i < 6; i++) + { + if (step_values[i] == NULL) + { + GtkCssValue *initial = _gtk_css_style_property_get_initial_value ( + _gtk_css_shorthand_property_get_subproperty (shorthand, i)); + step_values[i] = _gtk_css_value_ref (_gtk_css_array_value_get_nth (initial, 0)); + } + + g_ptr_array_add (arrays[i], step_values[i]); + step_values[i] = NULL; + } + } while (_gtk_css_parser_try (parser, ",", TRUE)); + + for (i = 0; i < 6; i++) + { + values[i] = _gtk_css_array_value_new_from_array ((GtkCssValue **) arrays[i]->pdata, arrays[i]->len); + g_ptr_array_unref (arrays[i]); + } + + values[6] = step_values[6]; + + return TRUE; +} + +static gboolean +parse_one_transition (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) +{ + do + { + /* the image part */ + if (values[2] == NULL && + _gtk_css_parser_has_number (parser) && !_gtk_css_parser_begins_with (parser, '-')) + { + GtkCssValue *number = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_TIME); + + if (number == NULL) + return FALSE; + + if (values[1] == NULL) + values[1] = number; + else + values[2] = number; + } + else if (values[3] == NULL && + _gtk_css_ease_value_can_parse (parser)) + { + values[3] = _gtk_css_ease_value_parse (parser); + + if (values[3] == NULL) return FALSE; + } + else if (values[0] == NULL) + { + values[0] = _gtk_css_ident_value_try_parse (parser); + if (values[0] == NULL) + { + _gtk_css_parser_error (parser, "Unknown value for property"); + return FALSE; + } + + } + else + { + /* We parsed everything and there's still stuff left? + * Pretend we didn't notice and let the normal code produce + * a 'junk at end of value' error */ + break; + } + } + while (!value_is_done_parsing (parser)); + + return TRUE; +} + +static gboolean +parse_transition (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) +{ + GtkCssValue *step_values[4]; + GPtrArray *arrays[4]; + guint i; + + for (i = 0; i < 4; i++) + { + arrays[i] = g_ptr_array_new (); + step_values[i] = NULL; + } + + do { + if (!parse_one_transition (shorthand, step_values, parser)) + { + for (i = 0; i < 4; i++) + { + g_ptr_array_set_free_func (arrays[i], (GDestroyNotify) _gtk_css_value_unref); + g_ptr_array_unref (arrays[i]); + } + return FALSE; + } + + for (i = 0; i < 4; i++) + { + if (step_values[i] == NULL) + { + GtkCssValue *initial = _gtk_css_style_property_get_initial_value ( + _gtk_css_shorthand_property_get_subproperty (shorthand, i)); + step_values[i] = _gtk_css_value_ref (_gtk_css_array_value_get_nth (initial, 0)); + } + + g_ptr_array_add (arrays[i], step_values[i]); + step_values[i] = NULL; + } + } while (_gtk_css_parser_try (parser, ",", TRUE)); + + for (i = 0; i < 4; i++) + { + values[i] = _gtk_css_array_value_new_from_array ((GtkCssValue **) arrays[i]->pdata, arrays[i]->len); + g_ptr_array_unref (arrays[i]); + } + + return TRUE; +} + +static gboolean +parse_one_animation (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) +{ + do + { + if (values[1] == NULL && _gtk_css_parser_try (parser, "infinite", TRUE)) + { + values[1] = _gtk_css_number_value_new (HUGE_VAL, GTK_CSS_NUMBER); + } + else if ((values[1] == NULL || values[3] == NULL) && + _gtk_css_parser_has_number (parser)) + { + GtkCssValue *value; + + value = _gtk_css_number_value_parse (parser, + GTK_CSS_POSITIVE_ONLY + | (values[1] == NULL ? GTK_CSS_PARSE_NUMBER : 0) + | (values[3] == NULL ? GTK_CSS_PARSE_TIME : 0)); + if (_gtk_css_number_value_get_unit (value) == GTK_CSS_NUMBER) + values[1] = value; + else if (values[2] == NULL) + values[2] = value; + else + values[3] = value; + } + else if (values[4] == NULL && + _gtk_css_ease_value_can_parse (parser)) + { + values[4] = _gtk_css_ease_value_parse (parser); - g_value_init (&values[4], GTK_TYPE_SYMBOLIC_COLOR); - g_value_take_boxed (&values[4], symbolic); + if (values[4] == NULL) + return FALSE; + } + else if (values[5] == NULL && + (values[5] = _gtk_css_direction_value_try_parse (parser))) + { + /* nothing to do */ + } + else if (values[6] == NULL && + (values[6] = _gtk_css_fill_mode_value_try_parse (parser))) + { + /* nothing to do */ + } + else if (values[0] == NULL && + (values[0] = _gtk_css_ident_value_try_parse (parser))) + { + /* nothing to do */ + /* keep in mind though that this needs to come last as fill modes, directions + * etc are valid idents */ } else { @@ -509,122 +760,168 @@ parse_background (GtkCssShorthandProperty *shorthand, return TRUE; } +static gboolean +parse_animation (GtkCssShorthandProperty *shorthand, + GtkCssValue **values, + GtkCssParser *parser) +{ + GtkCssValue *step_values[7]; + GPtrArray *arrays[6]; + guint i; + + for (i = 0; i < 6; i++) + { + arrays[i] = g_ptr_array_new (); + step_values[i] = NULL; + } + + step_values[6] = NULL; + + do { + if (!parse_one_animation (shorthand, step_values, parser)) + { + for (i = 0; i < 6; i++) + { + g_ptr_array_set_free_func (arrays[i], (GDestroyNotify) _gtk_css_value_unref); + g_ptr_array_unref (arrays[i]); + } + return FALSE; + } + + for (i = 0; i < 6; i++) + { + if (step_values[i] == NULL) + { + GtkCssValue *initial = _gtk_css_style_property_get_initial_value ( + _gtk_css_shorthand_property_get_subproperty (shorthand, i)); + step_values[i] = _gtk_css_value_ref (_gtk_css_array_value_get_nth (initial, 0)); + } + + g_ptr_array_add (arrays[i], step_values[i]); + step_values[i] = NULL; + } + } while (_gtk_css_parser_try (parser, ",", TRUE)); + + for (i = 0; i < 6; i++) + { + values[i] = _gtk_css_array_value_new_from_array ((GtkCssValue **) arrays[i]->pdata, arrays[i]->len); + g_ptr_array_unref (arrays[i]); + } + + values[6] = step_values[6]; + + return TRUE; +} + /*** PACKING ***/ -static GParameter * +static void unpack_border (GtkCssShorthandProperty *shorthand, - const GValue *value, - guint *n_params) + GtkStyleProperties *props, + GtkStateFlags state, + const GValue *value) { - GParameter *parameter = g_new0 (GParameter, 4); + GValue v = G_VALUE_INIT; GtkBorder *border = g_value_get_boxed (value); - parameter[0].name = _gtk_style_property_get_name (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 0))); - g_value_init (¶meter[0].value, G_TYPE_INT); - g_value_set_int (¶meter[0].value, border->top); - parameter[1].name = _gtk_style_property_get_name (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 1))); - g_value_init (¶meter[1].value, G_TYPE_INT); - g_value_set_int (¶meter[1].value, border->right); - parameter[2].name = _gtk_style_property_get_name (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 2))); - g_value_init (¶meter[2].value, G_TYPE_INT); - g_value_set_int (¶meter[2].value, border->bottom); - parameter[3].name = _gtk_style_property_get_name (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 3))); - g_value_init (¶meter[3].value, G_TYPE_INT); - g_value_set_int (¶meter[3].value, border->left); - - *n_params = 4; - return parameter; + g_value_init (&v, G_TYPE_INT); + + g_value_set_int (&v, border->top); + _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 0)), props, state, &v); + g_value_set_int (&v, border->right); + _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 1)), props, state, &v); + g_value_set_int (&v, border->bottom); + _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 2)), props, state, &v); + g_value_set_int (&v, border->left); + _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 3)), props, state, &v); + + g_value_unset (&v); } static void pack_border (GtkCssShorthandProperty *shorthand, GValue *value, - GtkStyleProperties *props, - GtkStateFlags state) + GtkStyleQueryFunc query_func, + gpointer query_data) { GtkCssStyleProperty *prop; GtkBorder border; - const GValue *v; + GValue v; prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 0); - v = _gtk_style_properties_peek_property (props, prop, state); - if (v) - border.top = g_value_get_int (v); + _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data); + border.top = g_value_get_int (&v); + g_value_unset (&v); + prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 1); - v = _gtk_style_properties_peek_property (props, prop, state); - if (v) - border.right = g_value_get_int (v); + _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data); + border.right = g_value_get_int (&v); + g_value_unset (&v); + prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 2); - v = _gtk_style_properties_peek_property (props, prop, state); - if (v) - border.bottom = g_value_get_int (v); + _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data); + border.bottom = g_value_get_int (&v); + g_value_unset (&v); + prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 3); - v = _gtk_style_properties_peek_property (props, prop, state); - if (v) - border.left = g_value_get_int (v); + _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data); + border.left = g_value_get_int (&v); + g_value_unset (&v); + g_value_init (value, GTK_TYPE_BORDER); g_value_set_boxed (value, &border); } -static GParameter * +static void unpack_border_radius (GtkCssShorthandProperty *shorthand, - const GValue *value, - guint *n_params) + GtkStyleProperties *props, + GtkStateFlags state, + const GValue *value) { - GParameter *parameter = g_new0 (GParameter, 4); - GtkCssBorderCornerRadius border; + GtkCssValue *css_value; + guint i; - border.horizontal = border.vertical = g_value_get_int (value); - - parameter[0].name = "border-top-left-radius"; - g_value_init (¶meter[0].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS); - g_value_set_boxed (¶meter[0].value, &border); - parameter[1].name = "border-top-right-radius"; - g_value_init (¶meter[1].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS); - g_value_set_boxed (¶meter[1].value, &border); - parameter[2].name = "border-bottom-right-radius"; - g_value_init (¶meter[2].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS); - g_value_set_boxed (¶meter[2].value, &border); - parameter[3].name = "border-bottom-left-radius"; - g_value_init (¶meter[3].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS); - g_value_set_boxed (¶meter[3].value, &border); - - *n_params = 4; - return parameter; + css_value = _gtk_css_corner_value_new (_gtk_css_number_value_new (g_value_get_int (value), GTK_CSS_PX), + _gtk_css_number_value_new (g_value_get_int (value), GTK_CSS_PX)); + + for (i = 0; i < 4; i++) + _gtk_style_properties_set_property_by_property (props, + _gtk_css_shorthand_property_get_subproperty (shorthand, i), + state, + css_value); + + _gtk_css_value_unref (css_value); } static void pack_border_radius (GtkCssShorthandProperty *shorthand, GValue *value, - GtkStyleProperties *props, - GtkStateFlags state) + GtkStyleQueryFunc query_func, + gpointer query_data) { - GtkCssBorderCornerRadius *top_left; - - /* NB: We are an int property, so we have to resolve to an int here. - * So we just resolve to an int. We pick one and stick to it. - * Lesson learned: Don't query border-radius shorthand, query the - * real properties instead. */ - gtk_style_properties_get (props, - state, - "border-top-left-radius", &top_left, - NULL); + GtkCssStyleProperty *prop; + GtkCssValue *v; + int i = 0; - if (top_left) - g_value_set_int (value, top_left->horizontal); + prop = GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("border-top-left-radius")); + v = (* query_func) (_gtk_css_style_property_get_id (prop), query_data); + if (v) + i = _gtk_css_corner_value_get_x (v, 100); - g_free (top_left); + g_value_init (value, G_TYPE_INT); + g_value_set_int (value, i); } -static GParameter * +static void unpack_font_description (GtkCssShorthandProperty *shorthand, - const GValue *value, - guint *n_params) + GtkStyleProperties *props, + GtkStateFlags state, + const GValue *value) { - GParameter *parameter = g_new0 (GParameter, 5); + GtkStyleProperty *prop; PangoFontDescription *description; PangoFontMask mask; - guint n; + GValue v = G_VALUE_INIT; /* For backwards compat, we only unpack values that are indeed set. * For strict CSS conformance we need to unpack all of them. @@ -634,7 +931,6 @@ unpack_font_description (GtkCssShorthandProperty *shorthand, */ description = g_value_get_boxed (value); - n = 0; if (description) mask = pango_font_description_get_set_fields (description); @@ -647,141 +943,129 @@ unpack_font_description (GtkCssShorthandProperty *shorthand, g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description))); g_ptr_array_add (strv, NULL); - parameter[n].name = "font-family"; - g_value_init (¶meter[n].value, G_TYPE_STRV); - g_value_take_boxed (¶meter[n].value, - g_ptr_array_free (strv, FALSE)); - n++; + g_value_init (&v, G_TYPE_STRV); + g_value_take_boxed (&v, g_ptr_array_free (strv, FALSE)); + + prop = _gtk_style_property_lookup ("font-family"); + _gtk_style_property_assign (prop, props, state, &v); + g_value_unset (&v); } if (mask & PANGO_FONT_MASK_STYLE) { - parameter[n].name = "font-style"; - g_value_init (¶meter[n].value, PANGO_TYPE_STYLE); - g_value_set_enum (¶meter[n].value, - pango_font_description_get_style (description)); - n++; + g_value_init (&v, PANGO_TYPE_STYLE); + g_value_set_enum (&v, pango_font_description_get_style (description)); + + prop = _gtk_style_property_lookup ("font-style"); + _gtk_style_property_assign (prop, props, state, &v); + g_value_unset (&v); } if (mask & PANGO_FONT_MASK_VARIANT) { - parameter[n].name = "font-variant"; - g_value_init (¶meter[n].value, PANGO_TYPE_VARIANT); - g_value_set_enum (¶meter[n].value, - pango_font_description_get_variant (description)); - n++; + g_value_init (&v, PANGO_TYPE_VARIANT); + g_value_set_enum (&v, pango_font_description_get_variant (description)); + + prop = _gtk_style_property_lookup ("font-variant"); + _gtk_style_property_assign (prop, props, state, &v); + g_value_unset (&v); } if (mask & PANGO_FONT_MASK_WEIGHT) { - parameter[n].name = "font-weight"; - g_value_init (¶meter[n].value, PANGO_TYPE_WEIGHT); - g_value_set_enum (¶meter[n].value, - pango_font_description_get_weight (description)); - n++; + g_value_init (&v, PANGO_TYPE_WEIGHT); + g_value_set_enum (&v, pango_font_description_get_weight (description)); + + prop = _gtk_style_property_lookup ("font-weight"); + _gtk_style_property_assign (prop, props, state, &v); + g_value_unset (&v); } if (mask & PANGO_FONT_MASK_SIZE) { - parameter[n].name = "font-size"; - g_value_init (¶meter[n].value, G_TYPE_DOUBLE); - g_value_set_double (¶meter[n].value, - (double) pango_font_description_get_size (description) / PANGO_SCALE); - n++; - } + g_value_init (&v, G_TYPE_DOUBLE); + g_value_set_double (&v, (double) pango_font_description_get_size (description) / PANGO_SCALE); - *n_params = n; - - return parameter; + prop = _gtk_style_property_lookup ("font-size"); + _gtk_style_property_assign (prop, props, state, &v); + g_value_unset (&v); + } } static void pack_font_description (GtkCssShorthandProperty *shorthand, GValue *value, - GtkStyleProperties *props, - GtkStateFlags state) + GtkStyleQueryFunc query_func, + gpointer query_data) { PangoFontDescription *description; - char **families; - PangoStyle style; - PangoVariant variant; - PangoWeight weight; - double size; - - gtk_style_properties_get (props, - state, - "font-family", &families, - "font-style", &style, - "font-variant", &variant, - "font-weight", &weight, - "font-size", &size, - NULL); + GtkCssValue *v; description = pango_font_description_new (); - /* xxx: Can we set all the families here somehow? */ - if (families) - pango_font_description_set_family (description, families[0]); - pango_font_description_set_size (description, round (size * PANGO_SCALE)); - pango_font_description_set_style (description, style); - pango_font_description_set_variant (description, variant); - pango_font_description_set_weight (description, weight); - g_strfreev (families); + v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-family"))), query_data); + if (v) + { + /* xxx: Can we set all the families here somehow? */ + pango_font_description_set_family (description, _gtk_css_string_value_get (_gtk_css_array_value_get_nth (v, 0))); + } + + v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-size"))), query_data); + if (v) + pango_font_description_set_size (description, round (_gtk_css_number_value_get (v, 100) * PANGO_SCALE)); + + v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-style"))), query_data); + if (v) + pango_font_description_set_style (description, _gtk_css_font_style_value_get (v)); + + v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-variant"))), query_data); + if (v) + pango_font_description_set_variant (description, _gtk_css_font_variant_value_get (v)); + v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-weight"))), query_data); + if (v) + pango_font_description_set_weight (description, _gtk_css_font_weight_value_get (v)); + + g_value_init (value, PANGO_TYPE_FONT_DESCRIPTION); g_value_take_boxed (value, description); } -static GParameter * +static void unpack_to_everything (GtkCssShorthandProperty *shorthand, - const GValue *value, - guint *n_params) + GtkStyleProperties *props, + GtkStateFlags state, + const GValue *value) { GtkCssStyleProperty *prop; - GParameter *parameter; guint i, n; - GType type; n = _gtk_css_shorthand_property_get_n_subproperties (shorthand); - parameter = g_new0 (GParameter, n); - type = G_VALUE_TYPE (value); for (i = 0; i < n; i++) { prop = _gtk_css_shorthand_property_get_subproperty (shorthand, i); - parameter[i].name = _gtk_style_property_get_name (GTK_STYLE_PROPERTY (prop)); - g_value_init (¶meter[i].value, type); - g_value_copy (value, ¶meter[i].value); + _gtk_style_property_assign (GTK_STYLE_PROPERTY (prop), props, state, value); } - - *n_params = n; - return parameter; } static void pack_first_element (GtkCssShorthandProperty *shorthand, GValue *value, - GtkStyleProperties *props, - GtkStateFlags state) + GtkStyleQueryFunc query_func, + gpointer query_data) { GtkCssStyleProperty *prop; - const GValue *v; - guint i; /* NB: This is a fallback for properties that originally were * not used as shorthand. We just pick the first subproperty * as a representative. * Lesson learned: Don't query the shorthand, query the * real properties instead. */ - for (i = 0; i < _gtk_css_shorthand_property_get_n_subproperties (shorthand); i++) - { - prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 0); - v = _gtk_style_properties_peek_property (props, prop, state); - if (v) - { - g_value_copy (v, value); - return; - } - } + prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 0); + _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), + value, + query_func, + query_data); } static void @@ -827,8 +1111,11 @@ _gtk_css_shorthand_property_init_properties (void) "border-top-color", "border-right-color", "border-bottom-color", "border-left-color", "border-image-source", "border-image-slice", "border-image-width", "border-image-repeat", NULL }; const char *outline_subproperties[] = { "outline-width", "outline-style", "outline-color", NULL }; - const char *background_subproperties[] = { "background-image", "background-repeat", "background-clip", "background-origin", + const char *background_subproperties[] = { "background-image", "background-position", "background-size", "background-repeat", "background-clip", "background-origin", "background-color", NULL }; + const char *transition_subproperties[] = { "transition-property", "transition-duration", "transition-delay", "transition-timing-function", NULL }; + const char *animation_subproperties[] = { "animation-name", "animation-iteration-count", "animation-duration", "animation-delay", + "animation-timing-function", "animation-direction", "animation-fill-mode", NULL }; _gtk_css_shorthand_property_register ("font", PANGO_TYPE_FONT_DESCRIPTION, @@ -839,13 +1126,13 @@ _gtk_css_shorthand_property_init_properties (void) _gtk_css_shorthand_property_register ("margin", GTK_TYPE_BORDER, margin_subproperties, - parse_border_width, + parse_margin, unpack_border, pack_border); _gtk_css_shorthand_property_register ("padding", GTK_TYPE_BORDER, padding_subproperties, - parse_border_width, + parse_padding, unpack_border, pack_border); _gtk_css_shorthand_property_register ("border-width", @@ -920,4 +1207,16 @@ _gtk_css_shorthand_property_init_properties (void) parse_background, NULL, NULL); + _gtk_css_shorthand_property_register ("transition", + G_TYPE_NONE, + transition_subproperties, + parse_transition, + NULL, + NULL); + _gtk_css_shorthand_property_register ("animation", + G_TYPE_NONE, + animation_subproperties, + parse_animation, + NULL, + NULL); }