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