]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssshorthandpropertyimpl.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkcssshorthandpropertyimpl.c
1 /*
2  * Copyright © 2011 Red Hat Inc.
3  *
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Authors: Benjamin Otte <otte@gnome.org>
18  */
19
20 #include "config.h"
21
22 #include "gtkcssshorthandpropertyprivate.h"
23
24 #include <cairo-gobject.h>
25 #include <math.h>
26
27 #include "gtkcssarrayvalueprivate.h"
28 #include "gtkcssbgsizevalueprivate.h"
29 #include "gtkcssbordervalueprivate.h"
30 #include "gtkcsscolorvalueprivate.h"
31 #include "gtkcsscornervalueprivate.h"
32 #include "gtkcsseasevalueprivate.h"
33 #include "gtkcssenumvalueprivate.h"
34 #include "gtkcssimageprivate.h"
35 #include "gtkcssimagevalueprivate.h"
36 #include "gtkcssnumbervalueprivate.h"
37 #include "gtkcsspositionvalueprivate.h"
38 #include "gtkcssrepeatvalueprivate.h"
39 #include "gtkcssstringvalueprivate.h"
40 #include "gtkcssstylefuncsprivate.h"
41 #include "gtkcssvalueprivate.h"
42 #include "gtkstylepropertiesprivate.h"
43 #include "gtktypebuiltins.h"
44
45 /* this is in case round() is not provided by the compiler, 
46  * such as in the case of C89 compilers, like MSVC
47  */
48 #include "fallback-c89.c"
49
50 /*** PARSING ***/
51
52 static gboolean
53 value_is_done_parsing (GtkCssParser *parser)
54 {
55   return _gtk_css_parser_is_eof (parser) ||
56          _gtk_css_parser_begins_with (parser, ',') ||
57          _gtk_css_parser_begins_with (parser, ';') ||
58          _gtk_css_parser_begins_with (parser, '}');
59 }
60
61 static gboolean
62 parse_four_numbers (GtkCssShorthandProperty  *shorthand,
63                     GtkCssValue             **values,
64                     GtkCssParser             *parser,
65                     GtkCssNumberParseFlags    flags)
66 {
67   guint i;
68
69   for (i = 0; i < 4; i++)
70     {
71       if (!_gtk_css_parser_has_number (parser))
72         break;
73
74       values[i] = _gtk_css_number_value_parse (parser, flags);
75       if (values[i] == NULL)
76         return FALSE;
77     }
78
79   if (i == 0)
80     {
81       _gtk_css_parser_error (parser, "Expected a length");
82       return FALSE;
83     }
84
85   for (; i < 4; i++)
86     {
87       values[i] = _gtk_css_value_ref (values[(i - 1) >> 1]);
88     }
89
90   return TRUE;
91 }
92
93 static gboolean
94 parse_margin (GtkCssShorthandProperty  *shorthand,
95               GtkCssValue             **values,
96               GtkCssParser             *parser)
97 {
98   return parse_four_numbers (shorthand,
99                              values,
100                              parser,
101                              GTK_CSS_NUMBER_AS_PIXELS
102                              | GTK_CSS_PARSE_LENGTH);
103 }
104
105 static gboolean
106 parse_padding (GtkCssShorthandProperty  *shorthand,
107                GtkCssValue             **values,
108                GtkCssParser             *parser)
109 {
110   return parse_four_numbers (shorthand,
111                              values,
112                              parser,
113                              GTK_CSS_POSITIVE_ONLY
114                              | GTK_CSS_NUMBER_AS_PIXELS
115                              | GTK_CSS_PARSE_LENGTH);
116 }
117
118 static gboolean
119 parse_border_width (GtkCssShorthandProperty  *shorthand,
120                     GtkCssValue             **values,
121                     GtkCssParser             *parser)
122 {
123   return parse_four_numbers (shorthand,
124                              values,
125                              parser,
126                              GTK_CSS_POSITIVE_ONLY
127                              | GTK_CSS_NUMBER_AS_PIXELS
128                              | GTK_CSS_PARSE_LENGTH);
129 }
130
131 static gboolean 
132 parse_border_radius (GtkCssShorthandProperty  *shorthand,
133                      GtkCssValue             **values,
134                      GtkCssParser             *parser)
135 {
136   GtkCssValue *x[4] = { NULL, }, *y[4] = { NULL, };
137   guint i;
138
139   for (i = 0; i < 4; i++)
140     {
141       if (!_gtk_css_parser_has_number (parser))
142         break;
143       x[i] = _gtk_css_number_value_parse (parser,
144                                           GTK_CSS_POSITIVE_ONLY
145                                           | GTK_CSS_PARSE_PERCENT
146                                           | GTK_CSS_NUMBER_AS_PIXELS
147                                           | GTK_CSS_PARSE_LENGTH);
148       if (x[i] == NULL)
149         goto fail;
150     }
151
152   if (i == 0)
153     {
154       _gtk_css_parser_error (parser, "Expected a number");
155       goto fail;
156     }
157
158   /* The magic (i - 1) >> 1 below makes it take the correct value
159    * according to spec. Feel free to check the 4 cases */
160   for (; i < 4; i++)
161     x[i] = _gtk_css_value_ref (x[(i - 1) >> 1]);
162
163   if (_gtk_css_parser_try (parser, "/", TRUE))
164     {
165       for (i = 0; i < 4; i++)
166         {
167           if (!_gtk_css_parser_has_number (parser))
168             break;
169           y[i] = _gtk_css_number_value_parse (parser,
170                                               GTK_CSS_POSITIVE_ONLY
171                                               | GTK_CSS_PARSE_PERCENT
172                                               | GTK_CSS_NUMBER_AS_PIXELS
173                                               | GTK_CSS_PARSE_LENGTH);
174           if (y[i] == NULL)
175             goto fail;
176         }
177
178       if (i == 0)
179         {
180           _gtk_css_parser_error (parser, "Expected a number");
181           goto fail;
182         }
183
184       for (; i < 4; i++)
185         y[i] = _gtk_css_value_ref (y[(i - 1) >> 1]);
186     }
187   else
188     {
189       for (i = 0; i < 4; i++)
190         y[i] = _gtk_css_value_ref (x[i]);
191     }
192
193   for (i = 0; i < 4; i++)
194     {
195       values[i] = _gtk_css_corner_value_new (x[i], y[i]);
196     }
197
198   return TRUE;
199
200 fail:
201   for (i = 0; i < 4; i++)
202     {
203       if (x[i])
204         _gtk_css_value_unref (x[i]);
205       if (y[i])
206         _gtk_css_value_unref (y[i]);
207     }
208   return FALSE;
209 }
210
211 static gboolean 
212 parse_border_color (GtkCssShorthandProperty  *shorthand,
213                     GtkCssValue             **values,
214                     GtkCssParser             *parser)
215 {
216   guint i;
217
218   for (i = 0; i < 4; i++)
219     {
220       values[i] = _gtk_css_color_value_parse (parser);
221       if (values[i] == NULL)
222         return FALSE;
223
224       if (value_is_done_parsing (parser))
225         break;
226     }
227
228   for (i++; i < 4; i++)
229     {
230       values[i] = _gtk_css_value_ref (values[(i - 1) >> 1]);
231     }
232
233   return TRUE;
234 }
235
236 static gboolean
237 parse_border_style (GtkCssShorthandProperty  *shorthand,
238                     GtkCssValue             **values,
239                     GtkCssParser             *parser)
240 {
241   guint i;
242
243   for (i = 0; i < 4; i++)
244     {
245       values[i] = _gtk_css_border_style_value_try_parse (parser);
246       if (values[i] == NULL)
247         break;
248     }
249
250   if (i == 0)
251     {
252       _gtk_css_parser_error (parser, "Expected a border style");
253       return FALSE;
254     }
255
256   for (; i < 4; i++)
257     values[i] = _gtk_css_value_ref (values[(i - 1) >> 1]);
258
259   return TRUE;
260 }
261
262 static gboolean
263 parse_border_image (GtkCssShorthandProperty  *shorthand,
264                     GtkCssValue             **values,
265                     GtkCssParser             *parser)
266 {
267   do
268     {
269       if (values[0] == NULL &&
270           (_gtk_css_parser_has_prefix (parser, "none") ||
271            _gtk_css_image_can_parse (parser)))
272         {
273           GtkCssImage *image;
274
275           if (_gtk_css_parser_try (parser, "none", TRUE))
276             image = NULL;
277           else
278             {
279               image = _gtk_css_image_new_parse (parser);
280               if (image == NULL)
281                 return FALSE;
282             }
283
284           values[0] = _gtk_css_image_value_new (image);
285         }
286       else if (values[3] == NULL &&
287                (values[3] = _gtk_css_border_repeat_value_try_parse (parser)))
288         {
289           /* please move along */
290         }
291       else if (values[1] == NULL)
292         {
293           values[1] = _gtk_css_border_value_parse (parser,
294                                                    GTK_CSS_PARSE_PERCENT
295                                                    | GTK_CSS_PARSE_NUMBER
296                                                    | GTK_CSS_POSITIVE_ONLY,
297                                                    FALSE,
298                                                    TRUE);
299           if (values[1] == NULL)
300             return FALSE;
301
302           if (_gtk_css_parser_try (parser, "/", TRUE))
303             {
304               values[2] = _gtk_css_border_value_parse (parser,
305                                                        GTK_CSS_PARSE_PERCENT
306                                                        | GTK_CSS_PARSE_LENGTH
307                                                        | GTK_CSS_PARSE_NUMBER
308                                                        | GTK_CSS_POSITIVE_ONLY,
309                                                        TRUE,
310                                                        FALSE);
311               if (values[2] == NULL)
312                 return FALSE;
313             }
314         }
315       else
316         {
317           /* We parsed everything and there's still stuff left?
318            * Pretend we didn't notice and let the normal code produce
319            * a 'junk at end of value' error */
320           break;
321         }
322     }
323   while (!value_is_done_parsing (parser));
324
325   return TRUE;
326 }
327
328 static gboolean
329 parse_border_side (GtkCssShorthandProperty  *shorthand,
330                    GtkCssValue             **values,
331                    GtkCssParser             *parser)
332 {
333   do
334   {
335     if (values[0] == NULL &&
336          _gtk_css_parser_has_number (parser))
337       {
338         values[0] = _gtk_css_number_value_parse (parser,
339                                                  GTK_CSS_POSITIVE_ONLY
340                                                  | GTK_CSS_NUMBER_AS_PIXELS
341                                                  | GTK_CSS_PARSE_LENGTH);
342         if (values[0] == NULL)
343           return FALSE;
344       }
345     else if (values[1] == NULL &&
346              (values[1] = _gtk_css_border_style_value_try_parse (parser)))
347       {
348         /* Nothing to do */
349       }
350     else if (values[2] == NULL)
351       {
352         values[2] = _gtk_css_color_value_parse (parser);
353         if (values[2] == NULL)
354           return FALSE;
355       }
356   }
357   while (!value_is_done_parsing (parser));
358
359   return TRUE;
360 }
361
362 static gboolean
363 parse_border (GtkCssShorthandProperty  *shorthand,
364               GtkCssValue             **values,
365               GtkCssParser             *parser)
366 {
367   do
368   {
369     if (values[0] == NULL &&
370          _gtk_css_parser_has_number (parser))
371       {
372         values[0] = _gtk_css_number_value_parse (parser,
373                                                  GTK_CSS_POSITIVE_ONLY
374                                                  | GTK_CSS_NUMBER_AS_PIXELS
375                                                  | GTK_CSS_PARSE_LENGTH);
376         if (values[0] == NULL)
377           return FALSE;
378         values[1] = _gtk_css_value_ref (values[0]);
379         values[2] = _gtk_css_value_ref (values[0]);
380         values[3] = _gtk_css_value_ref (values[0]);
381       }
382     else if (values[4] == NULL &&
383              (values[4] = _gtk_css_border_style_value_try_parse (parser)))
384       {
385         values[5] = _gtk_css_value_ref (values[4]);
386         values[6] = _gtk_css_value_ref (values[4]);
387         values[7] = _gtk_css_value_ref (values[4]);
388       }
389     else if (!G_IS_VALUE (&values[8]))
390       {
391         values[8] = _gtk_css_color_value_parse (parser);
392         if (values[8] == NULL)
393           return FALSE;
394
395         values[9] = _gtk_css_value_ref (values[8]);
396         values[10] = _gtk_css_value_ref (values[8]);
397         values[11] = _gtk_css_value_ref (values[8]);
398       }
399     else
400       {
401         /* We parsed everything and there's still stuff left?
402          * Pretend we didn't notice and let the normal code produce
403          * a 'junk at end of value' error */
404         break;
405       }
406   }
407   while (!value_is_done_parsing (parser));
408
409   /* Note that border-image values are not set: according to the spec
410      they just need to be reset when using the border shorthand */
411
412   return TRUE;
413 }
414
415 static gboolean
416 parse_font (GtkCssShorthandProperty  *shorthand,
417             GtkCssValue             **values,
418             GtkCssParser             *parser)
419 {
420   PangoFontDescription *desc;
421   guint mask;
422   char *str;
423
424   str = _gtk_css_parser_read_value (parser);
425   if (str == NULL)
426     return FALSE;
427
428   desc = pango_font_description_from_string (str);
429   g_free (str);
430
431   mask = pango_font_description_get_set_fields (desc);
432
433   if (mask & PANGO_FONT_MASK_FAMILY)
434     {
435       values[0] = _gtk_css_array_value_new (_gtk_css_string_value_new (pango_font_description_get_family (desc)));
436     }
437   if (mask & PANGO_FONT_MASK_STYLE)
438     {
439       values[1] = _gtk_css_font_style_value_new (pango_font_description_get_style (desc));
440     }
441   if (mask & PANGO_FONT_MASK_VARIANT)
442     {
443       values[2] = _gtk_css_font_variant_value_new (pango_font_description_get_variant (desc));
444     }
445   if (mask & PANGO_FONT_MASK_WEIGHT)
446     {
447       values[3] = _gtk_css_font_weight_value_new (pango_font_description_get_weight (desc));
448     }
449   if (mask & PANGO_FONT_MASK_SIZE)
450     {
451       values[4] = _gtk_css_number_value_new ((double) pango_font_description_get_size (desc) / PANGO_SCALE, GTK_CSS_PX);
452     }
453
454   pango_font_description_free (desc);
455
456   return TRUE;
457 }
458
459 static gboolean
460 parse_one_background (GtkCssShorthandProperty  *shorthand,
461                       GtkCssValue             **values,
462                       GtkCssParser             *parser)
463 {
464   GtkCssValue *value = NULL;
465
466   do
467     {
468       /* the image part */
469       if (values[0] == NULL &&
470           (_gtk_css_parser_has_prefix (parser, "none") ||
471            _gtk_css_image_can_parse (parser)))
472         {
473           GtkCssImage *image;
474
475           if (_gtk_css_parser_try (parser, "none", TRUE))
476             image = NULL;
477           else
478             {
479               image = _gtk_css_image_new_parse (parser);
480               if (image == NULL)
481                 return FALSE;
482             }
483
484           values[0] = _gtk_css_image_value_new (image);
485         }
486       else if (values[1] == NULL &&
487                (value = _gtk_css_position_value_try_parse (parser)))
488         {
489           values[1] = value;
490           value = NULL;
491
492           if (_gtk_css_parser_try (parser, "/", TRUE) &&
493               (value = _gtk_css_bg_size_value_parse (parser)))
494             {
495               values[2] = value;
496               value = NULL;
497             }
498         }
499       else if (values[3] == NULL &&
500                (value = _gtk_css_background_repeat_value_try_parse (parser)))
501         {
502           values[3] = value;
503           value = NULL;
504         }
505       else if ((values[4] == NULL || values[5] == NULL) &&
506                (value = _gtk_css_area_value_try_parse (parser)))
507         {
508           values[4] = value;
509
510           if (values[5] == NULL)
511             {
512               values[5] = values[4];
513               values[4] = NULL;
514             }
515           value = NULL;
516         }
517       else if (values[6] == NULL)
518         {
519           value = _gtk_css_color_value_parse (parser);
520           if (value == NULL)
521             values[6] = _gtk_css_value_ref (_gtk_css_style_property_get_initial_value 
522                                             (_gtk_css_shorthand_property_get_subproperty (shorthand, 6)));
523           else
524             values[6] = value;
525
526           value = NULL;
527         }
528       else
529         {
530           /* We parsed everything and there's still stuff left?
531            * Pretend we didn't notice and let the normal code produce
532            * a 'junk at end of value' error */
533           break;
534         }
535     }
536   while (!value_is_done_parsing (parser));
537
538   if (values[5] != NULL && values[4] == NULL)
539     values[4] = _gtk_css_value_ref (values[5]);
540
541   return TRUE;
542 }
543
544 static gboolean
545 parse_background (GtkCssShorthandProperty  *shorthand,
546                   GtkCssValue             **values,
547                   GtkCssParser             *parser)
548 {
549   GtkCssValue *step_values[7];
550   GPtrArray *arrays[6];
551   guint i;
552
553   for (i = 0; i < 6; i++)
554     {
555       arrays[i] = g_ptr_array_new ();
556       step_values[i] = NULL;
557     }
558   
559   step_values[6] = NULL;
560
561   do {
562     if (!parse_one_background (shorthand, step_values, parser))
563       {
564         for (i = 0; i < 6; i++)
565           {
566             g_ptr_array_set_free_func (arrays[i], (GDestroyNotify) _gtk_css_value_unref);
567             g_ptr_array_unref (arrays[i]);
568           }
569         return FALSE;
570       }
571
572       for (i = 0; i < 6; i++)
573         {
574           if (step_values[i] == NULL)
575             {
576               GtkCssValue *initial = _gtk_css_style_property_get_initial_value (
577                                          _gtk_css_shorthand_property_get_subproperty (shorthand, i));
578               step_values[i] = _gtk_css_value_ref (_gtk_css_array_value_get_nth (initial, 0));
579             }
580
581           g_ptr_array_add (arrays[i], step_values[i]);
582           step_values[i] = NULL;
583         }
584   } while (_gtk_css_parser_try (parser, ",", TRUE));
585
586   for (i = 0; i < 6; i++)
587     {
588       values[i] = _gtk_css_array_value_new_from_array ((GtkCssValue **) arrays[i]->pdata, arrays[i]->len);
589       g_ptr_array_unref (arrays[i]);
590     }
591
592   values[6] = step_values[6];
593
594   return TRUE;
595 }
596
597 static gboolean
598 parse_one_transition (GtkCssShorthandProperty  *shorthand,
599                       GtkCssValue             **values,
600                       GtkCssParser             *parser)
601 {
602   do
603     {
604       /* the image part */
605       if (values[2] == NULL &&
606           _gtk_css_parser_has_number (parser) && !_gtk_css_parser_begins_with (parser, '-'))
607         {
608           GtkCssValue *number = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_TIME);
609
610           if (number == NULL)
611             return FALSE;
612
613           if (values[1] == NULL)
614             values[1] = number;
615           else
616             values[2] = number;
617         }
618       else if (values[3] == NULL &&
619                _gtk_css_ease_value_can_parse (parser))
620         {
621           values[3] = _gtk_css_ease_value_parse (parser);
622
623           if (values[3] == NULL)
624             return FALSE;
625         }
626       else if (values[0] == NULL)
627         {
628           values[0] = _gtk_css_ident_value_try_parse (parser);
629           if (values[0] == NULL)
630             {
631               _gtk_css_parser_error (parser, "Unknown value for property");
632               return FALSE;
633             }
634
635         }
636       else
637         {
638           /* We parsed everything and there's still stuff left?
639            * Pretend we didn't notice and let the normal code produce
640            * a 'junk at end of value' error */
641           break;
642         }
643     }
644   while (!value_is_done_parsing (parser));
645
646   return TRUE;
647 }
648
649 static gboolean
650 parse_transition (GtkCssShorthandProperty  *shorthand,
651                   GtkCssValue             **values,
652                   GtkCssParser             *parser)
653 {
654   GtkCssValue *step_values[4];
655   GPtrArray *arrays[4];
656   guint i;
657
658   for (i = 0; i < 4; i++)
659     {
660       arrays[i] = g_ptr_array_new ();
661       step_values[i] = NULL;
662     }
663
664   do {
665     if (!parse_one_transition (shorthand, step_values, parser))
666       {
667         for (i = 0; i < 4; i++)
668           {
669             g_ptr_array_set_free_func (arrays[i], (GDestroyNotify) _gtk_css_value_unref);
670             g_ptr_array_unref (arrays[i]);
671           }
672         return FALSE;
673       }
674
675       for (i = 0; i < 4; i++)
676         {
677           if (step_values[i] == NULL)
678             {
679               GtkCssValue *initial = _gtk_css_style_property_get_initial_value (
680                                          _gtk_css_shorthand_property_get_subproperty (shorthand, i));
681               step_values[i] = _gtk_css_value_ref (_gtk_css_array_value_get_nth (initial, 0));
682             }
683
684           g_ptr_array_add (arrays[i], step_values[i]);
685           step_values[i] = NULL;
686         }
687   } while (_gtk_css_parser_try (parser, ",", TRUE));
688
689   for (i = 0; i < 4; i++)
690     {
691       values[i] = _gtk_css_array_value_new_from_array ((GtkCssValue **) arrays[i]->pdata, arrays[i]->len);
692       g_ptr_array_unref (arrays[i]);
693     }
694
695   return TRUE;
696 }
697
698 static gboolean
699 parse_one_animation (GtkCssShorthandProperty  *shorthand,
700                      GtkCssValue             **values,
701                      GtkCssParser             *parser)
702 {
703   do
704     {
705       if (values[1] == NULL && _gtk_css_parser_try (parser, "infinite", TRUE))
706         {
707           values[1] = _gtk_css_number_value_new (HUGE_VAL, GTK_CSS_NUMBER);
708         }
709       else if ((values[1] == NULL || values[3] == NULL) &&
710           _gtk_css_parser_has_number (parser))
711         {
712           GtkCssValue *value;
713           
714           value = _gtk_css_number_value_parse (parser,
715                                                GTK_CSS_POSITIVE_ONLY
716                                                | (values[1] == NULL ? GTK_CSS_PARSE_NUMBER : 0)
717                                                | (values[3] == NULL ? GTK_CSS_PARSE_TIME : 0));
718           if (_gtk_css_number_value_get_unit (value) == GTK_CSS_NUMBER)
719             values[1] = value;
720           else if (values[2] == NULL)
721             values[2] = value;
722           else
723             values[3] = value;
724         }
725       else if (values[4] == NULL &&
726                _gtk_css_ease_value_can_parse (parser))
727         {
728           values[4] = _gtk_css_ease_value_parse (parser);
729
730           if (values[4] == NULL)
731             return FALSE;
732         }
733       else if (values[5] == NULL &&
734                (values[5] = _gtk_css_direction_value_try_parse (parser)))
735         {
736           /* nothing to do */
737         }
738       else if (values[6] == NULL &&
739                (values[6] = _gtk_css_fill_mode_value_try_parse (parser)))
740         {
741           /* nothing to do */
742         }
743       else if (values[0] == NULL &&
744                (values[0] = _gtk_css_ident_value_try_parse (parser)))
745         {
746           /* nothing to do */
747           /* keep in mind though that this needs to come last as fill modes, directions
748            * etc are valid idents */
749         }
750       else
751         {
752           /* We parsed everything and there's still stuff left?
753            * Pretend we didn't notice and let the normal code produce
754            * a 'junk at end of value' error */
755           break;
756         }
757     }
758   while (!value_is_done_parsing (parser));
759
760   return TRUE;
761 }
762
763 static gboolean
764 parse_animation (GtkCssShorthandProperty  *shorthand,
765                  GtkCssValue             **values,
766                  GtkCssParser             *parser)
767 {
768   GtkCssValue *step_values[7];
769   GPtrArray *arrays[6];
770   guint i;
771
772   for (i = 0; i < 6; i++)
773     {
774       arrays[i] = g_ptr_array_new ();
775       step_values[i] = NULL;
776     }
777   
778   step_values[6] = NULL;
779
780   do {
781     if (!parse_one_animation (shorthand, step_values, parser))
782       {
783         for (i = 0; i < 6; i++)
784           {
785             g_ptr_array_set_free_func (arrays[i], (GDestroyNotify) _gtk_css_value_unref);
786             g_ptr_array_unref (arrays[i]);
787           }
788         return FALSE;
789       }
790
791       for (i = 0; i < 6; i++)
792         {
793           if (step_values[i] == NULL)
794             {
795               GtkCssValue *initial = _gtk_css_style_property_get_initial_value (
796                                          _gtk_css_shorthand_property_get_subproperty (shorthand, i));
797               step_values[i] = _gtk_css_value_ref (_gtk_css_array_value_get_nth (initial, 0));
798             }
799
800           g_ptr_array_add (arrays[i], step_values[i]);
801           step_values[i] = NULL;
802         }
803   } while (_gtk_css_parser_try (parser, ",", TRUE));
804
805   for (i = 0; i < 6; i++)
806     {
807       values[i] = _gtk_css_array_value_new_from_array ((GtkCssValue **) arrays[i]->pdata, arrays[i]->len);
808       g_ptr_array_unref (arrays[i]);
809     }
810
811   values[6] = step_values[6];
812
813   return TRUE;
814 }
815
816 /*** PACKING ***/
817
818 static void
819 unpack_border (GtkCssShorthandProperty *shorthand,
820                GtkStyleProperties      *props,
821                GtkStateFlags            state,
822                const GValue            *value)
823 {
824   GValue v = G_VALUE_INIT;
825   GtkBorder *border = g_value_get_boxed (value);
826
827   g_value_init (&v, G_TYPE_INT);
828
829   g_value_set_int (&v, border->top);
830   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 0)), props, state, &v);
831   g_value_set_int (&v, border->right);
832   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 1)), props, state, &v);
833   g_value_set_int (&v, border->bottom);
834   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 2)), props, state, &v);
835   g_value_set_int (&v, border->left);
836   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 3)), props, state, &v);
837
838   g_value_unset (&v);
839 }
840
841 static void
842 pack_border (GtkCssShorthandProperty *shorthand,
843              GValue                  *value,
844              GtkStyleQueryFunc        query_func,
845              gpointer                 query_data)
846 {
847   GtkCssStyleProperty *prop;
848   GtkBorder border;
849   GValue v;
850
851   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 0);
852   _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data);
853   border.top = g_value_get_int (&v);
854   g_value_unset (&v);
855
856   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 1);
857   _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data);
858   border.right = g_value_get_int (&v);
859   g_value_unset (&v);
860
861   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 2);
862   _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data);
863   border.bottom = g_value_get_int (&v);
864   g_value_unset (&v);
865
866   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 3);
867   _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data);
868   border.left = g_value_get_int (&v);
869   g_value_unset (&v);
870
871   g_value_init (value, GTK_TYPE_BORDER);
872   g_value_set_boxed (value, &border);
873 }
874
875 static void
876 unpack_border_radius (GtkCssShorthandProperty *shorthand,
877                       GtkStyleProperties      *props,
878                       GtkStateFlags            state,
879                       const GValue            *value)
880 {
881   GtkCssValue *css_value;
882   guint i;
883   
884   css_value = _gtk_css_corner_value_new (_gtk_css_number_value_new (g_value_get_int (value), GTK_CSS_PX),
885                                          _gtk_css_number_value_new (g_value_get_int (value), GTK_CSS_PX));
886
887   for (i = 0; i < 4; i++)
888     _gtk_style_properties_set_property_by_property (props,
889                                                     _gtk_css_shorthand_property_get_subproperty (shorthand, i),
890                                                     state,
891                                                     css_value);
892
893   _gtk_css_value_unref (css_value);
894 }
895
896 static void
897 pack_border_radius (GtkCssShorthandProperty *shorthand,
898                     GValue                  *value,
899                     GtkStyleQueryFunc        query_func,
900                     gpointer                 query_data)
901 {
902   GtkCssStyleProperty *prop;
903   GtkCssValue *v;
904   int i = 0;
905
906   prop = GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("border-top-left-radius"));
907   v = (* query_func) (_gtk_css_style_property_get_id (prop), query_data);
908   if (v)
909     i = _gtk_css_corner_value_get_x (v, 100);
910
911   g_value_init (value, G_TYPE_INT);
912   g_value_set_int (value, i);
913 }
914
915 static void
916 unpack_font_description (GtkCssShorthandProperty *shorthand,
917                          GtkStyleProperties      *props,
918                          GtkStateFlags            state,
919                          const GValue            *value)
920 {
921   GtkStyleProperty *prop;
922   PangoFontDescription *description;
923   PangoFontMask mask;
924   GValue v = G_VALUE_INIT;
925   
926   /* For backwards compat, we only unpack values that are indeed set.
927    * For strict CSS conformance we need to unpack all of them.
928    * Note that we do set all of them in the parse function, so it
929    * will not have effects when parsing CSS files. It will though
930    * for custom style providers.
931    */
932
933   description = g_value_get_boxed (value);
934
935   if (description)
936     mask = pango_font_description_get_set_fields (description);
937   else
938     mask = 0;
939
940   if (mask & PANGO_FONT_MASK_FAMILY)
941     {
942       GPtrArray *strv = g_ptr_array_new ();
943
944       g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
945       g_ptr_array_add (strv, NULL);
946       g_value_init (&v, G_TYPE_STRV);
947       g_value_take_boxed (&v, g_ptr_array_free (strv, FALSE));
948
949       prop = _gtk_style_property_lookup ("font-family");
950       _gtk_style_property_assign (prop, props, state, &v);
951       g_value_unset (&v);
952     }
953
954   if (mask & PANGO_FONT_MASK_STYLE)
955     {
956       g_value_init (&v, PANGO_TYPE_STYLE);
957       g_value_set_enum (&v, pango_font_description_get_style (description));
958
959       prop = _gtk_style_property_lookup ("font-style");
960       _gtk_style_property_assign (prop, props, state, &v);
961       g_value_unset (&v);
962     }
963
964   if (mask & PANGO_FONT_MASK_VARIANT)
965     {
966       g_value_init (&v, PANGO_TYPE_VARIANT);
967       g_value_set_enum (&v, pango_font_description_get_variant (description));
968
969       prop = _gtk_style_property_lookup ("font-variant");
970       _gtk_style_property_assign (prop, props, state, &v);
971       g_value_unset (&v);
972     }
973
974   if (mask & PANGO_FONT_MASK_WEIGHT)
975     {
976       g_value_init (&v, PANGO_TYPE_WEIGHT);
977       g_value_set_enum (&v, pango_font_description_get_weight (description));
978
979       prop = _gtk_style_property_lookup ("font-weight");
980       _gtk_style_property_assign (prop, props, state, &v);
981       g_value_unset (&v);
982     }
983
984   if (mask & PANGO_FONT_MASK_SIZE)
985     {
986       g_value_init (&v, G_TYPE_DOUBLE);
987       g_value_set_double (&v, (double) pango_font_description_get_size (description) / PANGO_SCALE);
988
989       prop = _gtk_style_property_lookup ("font-size");
990       _gtk_style_property_assign (prop, props, state, &v);
991       g_value_unset (&v);
992     }
993 }
994
995 static void
996 pack_font_description (GtkCssShorthandProperty *shorthand,
997                        GValue                  *value,
998                        GtkStyleQueryFunc        query_func,
999                        gpointer                 query_data)
1000 {
1001   PangoFontDescription *description;
1002   GtkCssValue *v;
1003
1004   description = pango_font_description_new ();
1005
1006   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-family"))), query_data);
1007   if (v)
1008     {
1009       /* xxx: Can we set all the families here somehow? */
1010       pango_font_description_set_family (description, _gtk_css_string_value_get (_gtk_css_array_value_get_nth (v, 0)));
1011     }
1012
1013   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-size"))), query_data);
1014   if (v)
1015     pango_font_description_set_size (description, round (_gtk_css_number_value_get (v, 100) * PANGO_SCALE));
1016
1017   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-style"))), query_data);
1018   if (v)
1019     pango_font_description_set_style (description, _gtk_css_font_style_value_get (v));
1020
1021   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-variant"))), query_data);
1022   if (v)
1023     pango_font_description_set_variant (description, _gtk_css_font_variant_value_get (v));
1024
1025   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-weight"))), query_data);
1026   if (v)
1027     pango_font_description_set_weight (description, _gtk_css_font_weight_value_get (v));
1028
1029   g_value_init (value, PANGO_TYPE_FONT_DESCRIPTION);
1030   g_value_take_boxed (value, description);
1031 }
1032
1033 static void
1034 unpack_to_everything (GtkCssShorthandProperty *shorthand,
1035                       GtkStyleProperties      *props,
1036                       GtkStateFlags            state,
1037                       const GValue            *value)
1038 {
1039   GtkCssStyleProperty *prop;
1040   guint i, n;
1041   
1042   n = _gtk_css_shorthand_property_get_n_subproperties (shorthand);
1043
1044   for (i = 0; i < n; i++)
1045     {
1046       prop = _gtk_css_shorthand_property_get_subproperty (shorthand, i);
1047       _gtk_style_property_assign (GTK_STYLE_PROPERTY (prop), props, state, value);
1048     }
1049 }
1050
1051 static void
1052 pack_first_element (GtkCssShorthandProperty *shorthand,
1053                     GValue                  *value,
1054                     GtkStyleQueryFunc        query_func,
1055                     gpointer                 query_data)
1056 {
1057   GtkCssStyleProperty *prop;
1058
1059   /* NB: This is a fallback for properties that originally were
1060    * not used as shorthand. We just pick the first subproperty
1061    * as a representative.
1062    * Lesson learned: Don't query the shorthand, query the 
1063    * real properties instead. */
1064   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 0);
1065   _gtk_style_property_query (GTK_STYLE_PROPERTY (prop),
1066                              value,
1067                              query_func,
1068                              query_data);
1069 }
1070
1071 static void
1072 _gtk_css_shorthand_property_register (const char                        *name,
1073                                       GType                              value_type,
1074                                       const char                       **subproperties,
1075                                       GtkCssShorthandPropertyParseFunc   parse_func,
1076                                       GtkCssShorthandPropertyAssignFunc  assign_func,
1077                                       GtkCssShorthandPropertyQueryFunc   query_func)
1078 {
1079   GtkCssShorthandProperty *node;
1080
1081   node = g_object_new (GTK_TYPE_CSS_SHORTHAND_PROPERTY,
1082                        "name", name,
1083                        "value-type", value_type,
1084                        "subproperties", subproperties,
1085                        NULL);
1086
1087   node->parse = parse_func;
1088   node->assign = assign_func;
1089   node->query = query_func;
1090 }
1091
1092 void
1093 _gtk_css_shorthand_property_init_properties (void)
1094 {
1095   /* The order is important here, be careful when changing it */
1096   const char *font_subproperties[] = { "font-family", "font-style", "font-variant", "font-weight", "font-size", NULL };
1097   const char *margin_subproperties[] = { "margin-top", "margin-right", "margin-bottom", "margin-left", NULL };
1098   const char *padding_subproperties[] = { "padding-top", "padding-right", "padding-bottom", "padding-left", NULL };
1099   const char *border_width_subproperties[] = { "border-top-width", "border-right-width", "border-bottom-width", "border-left-width", NULL };
1100   const char *border_radius_subproperties[] = { "border-top-left-radius", "border-top-right-radius",
1101                                                 "border-bottom-right-radius", "border-bottom-left-radius", NULL };
1102   const char *border_color_subproperties[] = { "border-top-color", "border-right-color", "border-bottom-color", "border-left-color", NULL };
1103   const char *border_style_subproperties[] = { "border-top-style", "border-right-style", "border-bottom-style", "border-left-style", NULL };
1104   const char *border_image_subproperties[] = { "border-image-source", "border-image-slice", "border-image-width", "border-image-repeat", NULL };
1105   const char *border_top_subproperties[] = { "border-top-width", "border-top-style", "border-top-color", NULL };
1106   const char *border_right_subproperties[] = { "border-right-width", "border-right-style", "border-right-color", NULL };
1107   const char *border_bottom_subproperties[] = { "border-bottom-width", "border-bottom-style", "border-bottom-color", NULL };
1108   const char *border_left_subproperties[] = { "border-left-width", "border-left-style", "border-left-color", NULL };
1109   const char *border_subproperties[] = { "border-top-width", "border-right-width", "border-bottom-width", "border-left-width",
1110                                          "border-top-style", "border-right-style", "border-bottom-style", "border-left-style",
1111                                          "border-top-color", "border-right-color", "border-bottom-color", "border-left-color",
1112                                          "border-image-source", "border-image-slice", "border-image-width", "border-image-repeat", NULL };
1113   const char *outline_subproperties[] = { "outline-width", "outline-style", "outline-color", NULL };
1114   const char *background_subproperties[] = { "background-image", "background-position", "background-size", "background-repeat", "background-clip", "background-origin",
1115                                              "background-color", NULL };
1116   const char *transition_subproperties[] = { "transition-property", "transition-duration", "transition-delay", "transition-timing-function", NULL };
1117   const char *animation_subproperties[] = { "animation-name", "animation-iteration-count", "animation-duration", "animation-delay", 
1118                                             "animation-timing-function", "animation-direction", "animation-fill-mode", NULL };
1119
1120   _gtk_css_shorthand_property_register   ("font",
1121                                           PANGO_TYPE_FONT_DESCRIPTION,
1122                                           font_subproperties,
1123                                           parse_font,
1124                                           unpack_font_description,
1125                                           pack_font_description);
1126   _gtk_css_shorthand_property_register   ("margin",
1127                                           GTK_TYPE_BORDER,
1128                                           margin_subproperties,
1129                                           parse_margin,
1130                                           unpack_border,
1131                                           pack_border);
1132   _gtk_css_shorthand_property_register   ("padding",
1133                                           GTK_TYPE_BORDER,
1134                                           padding_subproperties,
1135                                           parse_padding,
1136                                           unpack_border,
1137                                           pack_border);
1138   _gtk_css_shorthand_property_register   ("border-width",
1139                                           GTK_TYPE_BORDER,
1140                                           border_width_subproperties,
1141                                           parse_border_width,
1142                                           unpack_border,
1143                                           pack_border);
1144   _gtk_css_shorthand_property_register   ("border-radius",
1145                                           G_TYPE_INT,
1146                                           border_radius_subproperties,
1147                                           parse_border_radius,
1148                                           unpack_border_radius,
1149                                           pack_border_radius);
1150   _gtk_css_shorthand_property_register   ("border-color",
1151                                           GDK_TYPE_RGBA,
1152                                           border_color_subproperties,
1153                                           parse_border_color,
1154                                           unpack_to_everything,
1155                                           pack_first_element);
1156   _gtk_css_shorthand_property_register   ("border-style",
1157                                           GTK_TYPE_BORDER_STYLE,
1158                                           border_style_subproperties,
1159                                           parse_border_style,
1160                                           unpack_to_everything,
1161                                           pack_first_element);
1162   _gtk_css_shorthand_property_register   ("border-image",
1163                                           G_TYPE_NONE,
1164                                           border_image_subproperties,
1165                                           parse_border_image,
1166                                           NULL,
1167                                           NULL);
1168   _gtk_css_shorthand_property_register   ("border-top",
1169                                           G_TYPE_NONE,
1170                                           border_top_subproperties,
1171                                           parse_border_side,
1172                                           NULL,
1173                                           NULL);
1174   _gtk_css_shorthand_property_register   ("border-right",
1175                                           G_TYPE_NONE,
1176                                           border_right_subproperties,
1177                                           parse_border_side,
1178                                           NULL,
1179                                           NULL);
1180   _gtk_css_shorthand_property_register   ("border-bottom",
1181                                           G_TYPE_NONE,
1182                                           border_bottom_subproperties,
1183                                           parse_border_side,
1184                                           NULL,
1185                                           NULL);
1186   _gtk_css_shorthand_property_register   ("border-left",
1187                                           G_TYPE_NONE,
1188                                           border_left_subproperties,
1189                                           parse_border_side,
1190                                           NULL,
1191                                           NULL);
1192   _gtk_css_shorthand_property_register   ("border",
1193                                           G_TYPE_NONE,
1194                                           border_subproperties,
1195                                           parse_border,
1196                                           NULL,
1197                                           NULL);
1198   _gtk_css_shorthand_property_register   ("outline",
1199                                           G_TYPE_NONE,
1200                                           outline_subproperties,
1201                                           parse_border_side,
1202                                           NULL,
1203                                           NULL);
1204   _gtk_css_shorthand_property_register   ("background",
1205                                           G_TYPE_NONE,
1206                                           background_subproperties,
1207                                           parse_background,
1208                                           NULL,
1209                                           NULL);
1210   _gtk_css_shorthand_property_register   ("transition",
1211                                           G_TYPE_NONE,
1212                                           transition_subproperties,
1213                                           parse_transition,
1214                                           NULL,
1215                                           NULL);
1216   _gtk_css_shorthand_property_register   ("animation",
1217                                           G_TYPE_NONE,
1218                                           animation_subproperties,
1219                                           parse_animation,
1220                                           NULL,
1221                                           NULL);
1222 }