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