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