]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssshorthandpropertyimpl.c
shorthand: add background-position and background-size to bg shorthand
[~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 "gtkcsscornervalueprivate.h"
31 #include "gtkcsseasevalueprivate.h"
32 #include "gtkcssenumvalueprivate.h"
33 #include "gtkcssimageprivate.h"
34 #include "gtkcssimagevalueprivate.h"
35 #include "gtkcssnumbervalueprivate.h"
36 #include "gtkcsspositionvalueprivate.h"
37 #include "gtkcssrepeatvalueprivate.h"
38 #include "gtkcssstringvalueprivate.h"
39 #include "gtkcssstylefuncsprivate.h"
40 #include "gtkcssvalueprivate.h"
41 #include "gtkstylepropertiesprivate.h"
42 #include "gtksymboliccolorprivate.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_symbolic_value_new (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_symbolic_value_new (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_symbolic_value_new (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_background (GtkCssShorthandProperty  *shorthand,
461                   GtkCssValue             **values,
462                   GtkCssParser             *parser)
463 {
464   GtkCssValue *value;
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_array_value_new (_gtk_css_image_value_new (image));
485         }
486       else if (values[3] == NULL &&
487                (value = _gtk_css_background_repeat_value_try_parse (parser)))
488         {
489           values[3] = _gtk_css_array_value_new (value);
490           value = NULL;
491         }
492       else if ((values[4] == NULL || values[5] == NULL) &&
493                (value = _gtk_css_area_value_try_parse (parser)))
494         {
495           values[5] = _gtk_css_array_value_new (value);
496
497           if (values[4] == NULL)
498             {
499               values[4] = values[5];
500               values[5] = NULL;
501             }
502           value = NULL;
503         }
504       else if (values[6] == NULL)
505         {
506           value = _gtk_css_symbolic_value_new (parser);
507           if (value == NULL)
508             return FALSE;
509
510           values[6] = _gtk_css_array_value_new (value);
511           value = NULL;
512         }
513       else if (values[1] == NULL &&
514                (value = _gtk_css_position_value_parse (parser)))
515         {
516           values[1] = _gtk_css_array_value_new (value);
517           value = NULL;
518
519           if (_gtk_css_parser_try (parser, "/", TRUE) &&
520               (value = _gtk_css_bg_size_value_parse (parser)))
521             {
522               values[2] = _gtk_css_array_value_new (value);
523               value = NULL;
524             }
525         }
526       else
527         {
528           /* We parsed everything and there's still stuff left?
529            * Pretend we didn't notice and let the normal code produce
530            * a 'junk at end of value' error */
531           break;
532         }
533     }
534   while (!value_is_done_parsing (parser));
535
536   return TRUE;
537 }
538
539 static gboolean
540 parse_one_transition (GtkCssShorthandProperty  *shorthand,
541                       GtkCssValue             **values,
542                       GtkCssParser             *parser)
543 {
544   do
545     {
546       /* the image part */
547       if (values[2] == NULL &&
548           _gtk_css_parser_has_number (parser) && !_gtk_css_parser_begins_with (parser, '-'))
549         {
550           GtkCssValue *number = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_TIME);
551
552           if (number == NULL)
553             return FALSE;
554
555           if (values[1] == NULL)
556             values[1] = number;
557           else
558             values[2] = number;
559         }
560       else if (values[3] == NULL &&
561                _gtk_css_ease_value_can_parse (parser))
562         {
563           values[3] = _gtk_css_ease_value_parse (parser);
564
565           if (values[3] == NULL)
566             return FALSE;
567         }
568       else if (values[0] == NULL)
569         {
570           values[0] = _gtk_css_ident_value_try_parse (parser);
571           if (values[0] == NULL)
572             {
573               _gtk_css_parser_error (parser, "Unknown value for property");
574               return FALSE;
575             }
576
577         }
578       else
579         {
580           /* We parsed everything and there's still stuff left?
581            * Pretend we didn't notice and let the normal code produce
582            * a 'junk at end of value' error */
583           break;
584         }
585     }
586   while (!value_is_done_parsing (parser));
587
588   return TRUE;
589 }
590
591 static gboolean
592 parse_transition (GtkCssShorthandProperty  *shorthand,
593                   GtkCssValue             **values,
594                   GtkCssParser             *parser)
595 {
596   GtkCssValue *step_values[4];
597   GPtrArray *arrays[4];
598   guint i;
599
600   for (i = 0; i < 4; i++)
601     {
602       arrays[i] = g_ptr_array_new ();
603       step_values[i] = NULL;
604     }
605
606   do {
607     if (!parse_one_transition (shorthand, step_values, parser))
608       {
609         for (i = 0; i < 4; i++)
610           {
611             g_ptr_array_set_free_func (arrays[i], (GDestroyNotify) _gtk_css_value_unref);
612             g_ptr_array_unref (arrays[i]);
613             return FALSE;
614           }
615       }
616
617       for (i = 0; i < 4; i++)
618         {
619           if (step_values[i] == NULL)
620             {
621               GtkCssValue *initial = _gtk_css_style_property_get_initial_value (
622                                          _gtk_css_shorthand_property_get_subproperty (shorthand, i));
623               step_values[i] = _gtk_css_value_ref (_gtk_css_array_value_get_nth (initial, 0));
624             }
625
626           g_ptr_array_add (arrays[i], step_values[i]);
627           step_values[i] = NULL;
628         }
629   } while (_gtk_css_parser_try (parser, ",", TRUE));
630
631   for (i = 0; i < 4; i++)
632     {
633       values[i] = _gtk_css_array_value_new_from_array ((GtkCssValue **) arrays[i]->pdata, arrays[i]->len);
634       g_ptr_array_unref (arrays[i]);
635     }
636
637   return TRUE;
638 }
639
640 /*** PACKING ***/
641
642 static void
643 unpack_border (GtkCssShorthandProperty *shorthand,
644                GtkStyleProperties      *props,
645                GtkStateFlags            state,
646                const GValue            *value)
647 {
648   GValue v = G_VALUE_INIT;
649   GtkBorder *border = g_value_get_boxed (value);
650
651   g_value_init (&v, G_TYPE_INT);
652
653   g_value_set_int (&v, border->top);
654   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 0)), props, state, &v);
655   g_value_set_int (&v, border->right);
656   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 1)), props, state, &v);
657   g_value_set_int (&v, border->bottom);
658   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 2)), props, state, &v);
659   g_value_set_int (&v, border->left);
660   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 3)), props, state, &v);
661
662   g_value_unset (&v);
663 }
664
665 static void
666 pack_border (GtkCssShorthandProperty *shorthand,
667              GValue                  *value,
668              GtkStyleQueryFunc        query_func,
669              gpointer                 query_data)
670 {
671   GtkCssStyleProperty *prop;
672   GtkBorder border;
673   GValue v;
674
675   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 0);
676   _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data);
677   border.top = g_value_get_int (&v);
678   g_value_unset (&v);
679
680   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 1);
681   _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data);
682   border.right = g_value_get_int (&v);
683   g_value_unset (&v);
684
685   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 2);
686   _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data);
687   border.bottom = g_value_get_int (&v);
688   g_value_unset (&v);
689
690   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 3);
691   _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data);
692   border.left = g_value_get_int (&v);
693   g_value_unset (&v);
694
695   g_value_init (value, GTK_TYPE_BORDER);
696   g_value_set_boxed (value, &border);
697 }
698
699 static void
700 unpack_border_radius (GtkCssShorthandProperty *shorthand,
701                       GtkStyleProperties      *props,
702                       GtkStateFlags            state,
703                       const GValue            *value)
704 {
705   GtkCssValue *css_value;
706   guint i;
707   
708   css_value = _gtk_css_corner_value_new (_gtk_css_number_value_new (g_value_get_int (value), GTK_CSS_PX),
709                                          _gtk_css_number_value_new (g_value_get_int (value), GTK_CSS_PX));
710
711   for (i = 0; i < 4; i++)
712     _gtk_style_properties_set_property_by_property (props,
713                                                     _gtk_css_shorthand_property_get_subproperty (shorthand, i),
714                                                     state,
715                                                     css_value);
716
717   _gtk_css_value_unref (css_value);
718 }
719
720 static void
721 pack_border_radius (GtkCssShorthandProperty *shorthand,
722                     GValue                  *value,
723                     GtkStyleQueryFunc        query_func,
724                     gpointer                 query_data)
725 {
726   GtkCssStyleProperty *prop;
727   GtkCssValue *v;
728   int i = 0;
729
730   prop = GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("border-top-left-radius"));
731   v = (* query_func) (_gtk_css_style_property_get_id (prop), query_data);
732   if (v)
733     i = _gtk_css_corner_value_get_x (v, 100);
734
735   g_value_init (value, G_TYPE_INT);
736   g_value_set_int (value, i);
737 }
738
739 static void
740 unpack_font_description (GtkCssShorthandProperty *shorthand,
741                          GtkStyleProperties      *props,
742                          GtkStateFlags            state,
743                          const GValue            *value)
744 {
745   GtkStyleProperty *prop;
746   PangoFontDescription *description;
747   PangoFontMask mask;
748   GValue v = G_VALUE_INIT;
749   
750   /* For backwards compat, we only unpack values that are indeed set.
751    * For strict CSS conformance we need to unpack all of them.
752    * Note that we do set all of them in the parse function, so it
753    * will not have effects when parsing CSS files. It will though
754    * for custom style providers.
755    */
756
757   description = g_value_get_boxed (value);
758
759   if (description)
760     mask = pango_font_description_get_set_fields (description);
761   else
762     mask = 0;
763
764   if (mask & PANGO_FONT_MASK_FAMILY)
765     {
766       GPtrArray *strv = g_ptr_array_new ();
767
768       g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
769       g_ptr_array_add (strv, NULL);
770       g_value_init (&v, G_TYPE_STRV);
771       g_value_take_boxed (&v, g_ptr_array_free (strv, FALSE));
772
773       prop = _gtk_style_property_lookup ("font-family");
774       _gtk_style_property_assign (prop, props, state, &v);
775       g_value_unset (&v);
776     }
777
778   if (mask & PANGO_FONT_MASK_STYLE)
779     {
780       g_value_init (&v, PANGO_TYPE_STYLE);
781       g_value_set_enum (&v, pango_font_description_get_style (description));
782
783       prop = _gtk_style_property_lookup ("font-style");
784       _gtk_style_property_assign (prop, props, state, &v);
785       g_value_unset (&v);
786     }
787
788   if (mask & PANGO_FONT_MASK_VARIANT)
789     {
790       g_value_init (&v, PANGO_TYPE_VARIANT);
791       g_value_set_enum (&v, pango_font_description_get_variant (description));
792
793       prop = _gtk_style_property_lookup ("font-variant");
794       _gtk_style_property_assign (prop, props, state, &v);
795       g_value_unset (&v);
796     }
797
798   if (mask & PANGO_FONT_MASK_WEIGHT)
799     {
800       g_value_init (&v, PANGO_TYPE_WEIGHT);
801       g_value_set_enum (&v, pango_font_description_get_weight (description));
802
803       prop = _gtk_style_property_lookup ("font-weight");
804       _gtk_style_property_assign (prop, props, state, &v);
805       g_value_unset (&v);
806     }
807
808   if (mask & PANGO_FONT_MASK_SIZE)
809     {
810       g_value_init (&v, G_TYPE_DOUBLE);
811       g_value_set_double (&v, (double) pango_font_description_get_size (description) / PANGO_SCALE);
812
813       prop = _gtk_style_property_lookup ("font-size");
814       _gtk_style_property_assign (prop, props, state, &v);
815       g_value_unset (&v);
816     }
817 }
818
819 static void
820 pack_font_description (GtkCssShorthandProperty *shorthand,
821                        GValue                  *value,
822                        GtkStyleQueryFunc        query_func,
823                        gpointer                 query_data)
824 {
825   PangoFontDescription *description;
826   GtkCssValue *v;
827
828   description = pango_font_description_new ();
829
830   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-family"))), query_data);
831   if (v)
832     {
833       /* xxx: Can we set all the families here somehow? */
834       pango_font_description_set_family (description, _gtk_css_string_value_get (_gtk_css_array_value_get_nth (v, 0)));
835     }
836
837   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-size"))), query_data);
838   if (v)
839     pango_font_description_set_size (description, round (_gtk_css_number_value_get (v, 100) * PANGO_SCALE));
840
841   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-style"))), query_data);
842   if (v)
843     pango_font_description_set_style (description, _gtk_css_font_style_value_get (v));
844
845   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-variant"))), query_data);
846   if (v)
847     pango_font_description_set_variant (description, _gtk_css_font_variant_value_get (v));
848
849   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-weight"))), query_data);
850   if (v)
851     pango_font_description_set_weight (description, _gtk_css_font_weight_value_get (v));
852
853   g_value_init (value, PANGO_TYPE_FONT_DESCRIPTION);
854   g_value_take_boxed (value, description);
855 }
856
857 static void
858 unpack_to_everything (GtkCssShorthandProperty *shorthand,
859                       GtkStyleProperties      *props,
860                       GtkStateFlags            state,
861                       const GValue            *value)
862 {
863   GtkCssStyleProperty *prop;
864   guint i, n;
865   
866   n = _gtk_css_shorthand_property_get_n_subproperties (shorthand);
867
868   for (i = 0; i < n; i++)
869     {
870       prop = _gtk_css_shorthand_property_get_subproperty (shorthand, i);
871       _gtk_style_property_assign (GTK_STYLE_PROPERTY (prop), props, state, value);
872     }
873 }
874
875 static void
876 pack_first_element (GtkCssShorthandProperty *shorthand,
877                     GValue                  *value,
878                     GtkStyleQueryFunc        query_func,
879                     gpointer                 query_data)
880 {
881   GtkCssStyleProperty *prop;
882
883   /* NB: This is a fallback for properties that originally were
884    * not used as shorthand. We just pick the first subproperty
885    * as a representative.
886    * Lesson learned: Don't query the shorthand, query the 
887    * real properties instead. */
888   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 0);
889   _gtk_style_property_query (GTK_STYLE_PROPERTY (prop),
890                              value,
891                              query_func,
892                              query_data);
893 }
894
895 static void
896 _gtk_css_shorthand_property_register (const char                        *name,
897                                       GType                              value_type,
898                                       const char                       **subproperties,
899                                       GtkCssShorthandPropertyParseFunc   parse_func,
900                                       GtkCssShorthandPropertyAssignFunc  assign_func,
901                                       GtkCssShorthandPropertyQueryFunc   query_func)
902 {
903   GtkCssShorthandProperty *node;
904
905   node = g_object_new (GTK_TYPE_CSS_SHORTHAND_PROPERTY,
906                        "name", name,
907                        "value-type", value_type,
908                        "subproperties", subproperties,
909                        NULL);
910
911   node->parse = parse_func;
912   node->assign = assign_func;
913   node->query = query_func;
914 }
915
916 void
917 _gtk_css_shorthand_property_init_properties (void)
918 {
919   /* The order is important here, be careful when changing it */
920   const char *font_subproperties[] = { "font-family", "font-style", "font-variant", "font-weight", "font-size", NULL };
921   const char *margin_subproperties[] = { "margin-top", "margin-right", "margin-bottom", "margin-left", NULL };
922   const char *padding_subproperties[] = { "padding-top", "padding-right", "padding-bottom", "padding-left", NULL };
923   const char *border_width_subproperties[] = { "border-top-width", "border-right-width", "border-bottom-width", "border-left-width", NULL };
924   const char *border_radius_subproperties[] = { "border-top-left-radius", "border-top-right-radius",
925                                                 "border-bottom-right-radius", "border-bottom-left-radius", NULL };
926   const char *border_color_subproperties[] = { "border-top-color", "border-right-color", "border-bottom-color", "border-left-color", NULL };
927   const char *border_style_subproperties[] = { "border-top-style", "border-right-style", "border-bottom-style", "border-left-style", NULL };
928   const char *border_image_subproperties[] = { "border-image-source", "border-image-slice", "border-image-width", "border-image-repeat", NULL };
929   const char *border_top_subproperties[] = { "border-top-width", "border-top-style", "border-top-color", NULL };
930   const char *border_right_subproperties[] = { "border-right-width", "border-right-style", "border-right-color", NULL };
931   const char *border_bottom_subproperties[] = { "border-bottom-width", "border-bottom-style", "border-bottom-color", NULL };
932   const char *border_left_subproperties[] = { "border-left-width", "border-left-style", "border-left-color", NULL };
933   const char *border_subproperties[] = { "border-top-width", "border-right-width", "border-bottom-width", "border-left-width",
934                                          "border-top-style", "border-right-style", "border-bottom-style", "border-left-style",
935                                          "border-top-color", "border-right-color", "border-bottom-color", "border-left-color",
936                                          "border-image-source", "border-image-slice", "border-image-width", "border-image-repeat", NULL };
937   const char *outline_subproperties[] = { "outline-width", "outline-style", "outline-color", NULL };
938   const char *background_subproperties[] = { "background-image", "background-position", "background-size", "background-repeat", "background-clip", "background-origin",
939                                              "background-color", NULL };
940   const char *transition_subproperties[] = { "transition-property", "transition-duration", "transition-delay", "transition-timing-function", NULL };
941
942   _gtk_css_shorthand_property_register   ("font",
943                                           PANGO_TYPE_FONT_DESCRIPTION,
944                                           font_subproperties,
945                                           parse_font,
946                                           unpack_font_description,
947                                           pack_font_description);
948   _gtk_css_shorthand_property_register   ("margin",
949                                           GTK_TYPE_BORDER,
950                                           margin_subproperties,
951                                           parse_margin,
952                                           unpack_border,
953                                           pack_border);
954   _gtk_css_shorthand_property_register   ("padding",
955                                           GTK_TYPE_BORDER,
956                                           padding_subproperties,
957                                           parse_padding,
958                                           unpack_border,
959                                           pack_border);
960   _gtk_css_shorthand_property_register   ("border-width",
961                                           GTK_TYPE_BORDER,
962                                           border_width_subproperties,
963                                           parse_border_width,
964                                           unpack_border,
965                                           pack_border);
966   _gtk_css_shorthand_property_register   ("border-radius",
967                                           G_TYPE_INT,
968                                           border_radius_subproperties,
969                                           parse_border_radius,
970                                           unpack_border_radius,
971                                           pack_border_radius);
972   _gtk_css_shorthand_property_register   ("border-color",
973                                           GDK_TYPE_RGBA,
974                                           border_color_subproperties,
975                                           parse_border_color,
976                                           unpack_to_everything,
977                                           pack_first_element);
978   _gtk_css_shorthand_property_register   ("border-style",
979                                           GTK_TYPE_BORDER_STYLE,
980                                           border_style_subproperties,
981                                           parse_border_style,
982                                           unpack_to_everything,
983                                           pack_first_element);
984   _gtk_css_shorthand_property_register   ("border-image",
985                                           G_TYPE_NONE,
986                                           border_image_subproperties,
987                                           parse_border_image,
988                                           NULL,
989                                           NULL);
990   _gtk_css_shorthand_property_register   ("border-top",
991                                           G_TYPE_NONE,
992                                           border_top_subproperties,
993                                           parse_border_side,
994                                           NULL,
995                                           NULL);
996   _gtk_css_shorthand_property_register   ("border-right",
997                                           G_TYPE_NONE,
998                                           border_right_subproperties,
999                                           parse_border_side,
1000                                           NULL,
1001                                           NULL);
1002   _gtk_css_shorthand_property_register   ("border-bottom",
1003                                           G_TYPE_NONE,
1004                                           border_bottom_subproperties,
1005                                           parse_border_side,
1006                                           NULL,
1007                                           NULL);
1008   _gtk_css_shorthand_property_register   ("border-left",
1009                                           G_TYPE_NONE,
1010                                           border_left_subproperties,
1011                                           parse_border_side,
1012                                           NULL,
1013                                           NULL);
1014   _gtk_css_shorthand_property_register   ("border",
1015                                           G_TYPE_NONE,
1016                                           border_subproperties,
1017                                           parse_border,
1018                                           NULL,
1019                                           NULL);
1020   _gtk_css_shorthand_property_register   ("outline",
1021                                           G_TYPE_NONE,
1022                                           outline_subproperties,
1023                                           parse_border_side,
1024                                           NULL,
1025                                           NULL);
1026   _gtk_css_shorthand_property_register   ("background",
1027                                           G_TYPE_NONE,
1028                                           background_subproperties,
1029                                           parse_background,
1030                                           NULL,
1031                                           NULL);
1032   _gtk_css_shorthand_property_register   ("transition",
1033                                           G_TYPE_NONE,
1034                                           transition_subproperties,
1035                                           parse_transition,
1036                                           NULL,
1037                                           NULL);
1038 }