]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssshorthandpropertyimpl.c
css: Use GtkCssValues instead of GValue in the css machinery
[~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 #include "gtkcssvalueprivate.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_four_numbers (GtkCssShorthandProperty *shorthand,
53                     GValue                  *values,
54                     GtkCssParser            *parser,
55                     GtkCssNumberParseFlags   flags)
56 {
57   GtkCssNumber numbers[4];
58   guint i;
59
60   for (i = 0; i < 4; i++)
61     {
62       if (!_gtk_css_parser_has_number (parser))
63         break;
64
65       if (!_gtk_css_parser_read_number (parser,
66                                         &numbers[i], 
67                                         flags))
68         return FALSE;
69     }
70
71   if (i == 0)
72     {
73       _gtk_css_parser_error (parser, "Expected a length");
74       return FALSE;
75     }
76
77   for (; i < 4; i++)
78     {
79       numbers[i] = numbers[(i - 1) >> 1];
80     }
81
82   for (i = 0; i < 4; i++)
83     {
84       g_value_init (&values[i], GTK_TYPE_CSS_NUMBER);
85       g_value_set_boxed (&values[i], &numbers[i]);
86     }
87
88   return TRUE;
89 }
90
91 static gboolean
92 parse_margin (GtkCssShorthandProperty *shorthand,
93               GValue                  *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                GValue                  *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                     GValue                  *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                      GValue                  *values,
135                      GtkCssParser            *parser,
136                      GFile                   *base)
137 {
138   GtkCssBorderCornerRadius borders[4];
139   guint i;
140
141   for (i = 0; i < G_N_ELEMENTS (borders); i++)
142     {
143       if (!_gtk_css_parser_has_number (parser))
144         break;
145       if (!_gtk_css_parser_read_number (parser,
146                                         &borders[i].horizontal,
147                                         GTK_CSS_POSITIVE_ONLY
148                                         | GTK_CSS_PARSE_PERCENT
149                                         | GTK_CSS_NUMBER_AS_PIXELS
150                                         | GTK_CSS_PARSE_LENGTH))
151         return FALSE;
152     }
153
154   if (i == 0)
155     {
156       _gtk_css_parser_error (parser, "Expected a number");
157       return FALSE;
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 < G_N_ELEMENTS (borders); i++)
163     borders[i].horizontal = borders[(i - 1) >> 1].horizontal;
164
165   if (_gtk_css_parser_try (parser, "/", TRUE))
166     {
167       for (i = 0; i < G_N_ELEMENTS (borders); i++)
168         {
169           if (!_gtk_css_parser_has_number (parser))
170             break;
171           if (!_gtk_css_parser_read_number (parser,
172                                             &borders[i].vertical,
173                                             GTK_CSS_POSITIVE_ONLY
174                                             | GTK_CSS_PARSE_PERCENT
175                                             | GTK_CSS_NUMBER_AS_PIXELS
176                                             | GTK_CSS_PARSE_LENGTH))
177             return FALSE;
178         }
179
180       if (i == 0)
181         {
182           _gtk_css_parser_error (parser, "Expected a number");
183           return FALSE;
184         }
185
186       for (; i < G_N_ELEMENTS (borders); i++)
187         borders[i].vertical = borders[(i - 1) >> 1].vertical;
188
189     }
190   else
191     {
192       for (i = 0; i < G_N_ELEMENTS (borders); i++)
193         borders[i].vertical = borders[i].horizontal;
194     }
195
196   for (i = 0; i < G_N_ELEMENTS (borders); i++)
197     {
198       g_value_init (&values[i], GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
199       g_value_set_boxed (&values[i], &borders[i]);
200     }
201
202   return TRUE;
203 }
204
205 static gboolean 
206 parse_border_color (GtkCssShorthandProperty *shorthand,
207                     GValue                  *values,
208                     GtkCssParser            *parser,
209                     GFile                   *base)
210 {
211   GtkSymbolicColor *symbolic;
212   guint i;
213
214   for (i = 0; i < 4; i++)
215     {
216       if (_gtk_css_parser_try (parser, "currentcolor", TRUE))
217         {
218           symbolic = gtk_symbolic_color_ref (_gtk_symbolic_color_get_current_color ());
219         }
220       else
221         {
222           symbolic = _gtk_css_parser_read_symbolic_color (parser);
223           if (symbolic == NULL)
224             return FALSE;
225         }
226
227       g_value_init (&values[i], GTK_TYPE_SYMBOLIC_COLOR);
228       g_value_set_boxed (&values[i], symbolic);
229
230       if (value_is_done_parsing (parser))
231         break;
232     }
233
234   for (i++; i < 4; i++)
235     {
236       g_value_init (&values[i], G_VALUE_TYPE (&values[(i - 1) >> 1]));
237       g_value_copy (&values[(i - 1) >> 1], &values[i]);
238     }
239
240   return TRUE;
241 }
242
243 static gboolean
244 parse_border_style (GtkCssShorthandProperty *shorthand,
245                     GValue                  *values,
246                     GtkCssParser            *parser,
247                     GFile                   *base)
248 {
249   GtkBorderStyle styles[4];
250   guint i;
251
252   for (i = 0; i < 4; i++)
253     {
254       if (!_gtk_css_parser_try_enum (parser, GTK_TYPE_BORDER_STYLE, (int *)&styles[i]))
255         break;
256     }
257
258   if (i == 0)
259     {
260       _gtk_css_parser_error (parser, "Expected a border style");
261       return FALSE;
262     }
263
264   for (; i < G_N_ELEMENTS (styles); i++)
265     styles[i] = styles[(i - 1) >> 1];
266
267   for (i = 0; i < G_N_ELEMENTS (styles); i++)
268     {
269       g_value_init (&values[i], GTK_TYPE_BORDER_STYLE);
270       g_value_set_enum (&values[i], styles[i]);
271     }
272
273   return TRUE;
274 }
275
276 static gboolean
277 parse_border_image (GtkCssShorthandProperty *shorthand,
278                     GValue                  *values,
279                     GtkCssParser            *parser,
280                     GFile                   *base)
281 {
282   GtkCssImage *image;
283   
284   if (_gtk_css_parser_try (parser, "none", TRUE))
285     image = NULL;
286   else
287     {
288       image = _gtk_css_image_new_parse (parser, base);
289       if (!image)
290         return FALSE;
291     }
292   g_value_init (&values[0], GTK_TYPE_CSS_IMAGE);
293   g_value_set_object (&values[0], image);
294
295   if (value_is_done_parsing (parser))
296     return TRUE;
297
298   g_value_init (&values[1], GTK_TYPE_BORDER);
299   if (!_gtk_css_style_parse_value (&values[1], parser, base))
300     return FALSE;
301
302   if (_gtk_css_parser_try (parser, "/", TRUE))
303     {
304       g_value_init (&values[2], GTK_TYPE_BORDER);
305       if (!_gtk_css_style_parse_value (&values[2], parser, base))
306         return FALSE;
307     }
308
309   if (value_is_done_parsing (parser))
310     return TRUE;
311
312   g_value_init (&values[3], GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
313   if (!_gtk_css_style_parse_value (&values[3], parser, base))
314     return FALSE;
315
316   return TRUE;
317 }
318
319 static gboolean
320 parse_border_side (GtkCssShorthandProperty *shorthand,
321                    GValue                  *values,
322                    GtkCssParser            *parser,
323                    GFile                   *base)
324 {
325   int style;
326
327   do
328   {
329     if (!G_IS_VALUE (&values[0]) &&
330          _gtk_css_parser_has_number (parser))
331       {
332         GtkCssNumber number;
333         if (!_gtk_css_parser_read_number (parser,
334                                           &number,
335                                           GTK_CSS_POSITIVE_ONLY
336                                           | GTK_CSS_NUMBER_AS_PIXELS
337                                           | GTK_CSS_PARSE_LENGTH))
338           return FALSE;
339
340         g_value_init (&values[0], GTK_TYPE_CSS_NUMBER);
341         g_value_set_boxed (&values[0], &number);
342       }
343     else if (!G_IS_VALUE (&values[1]) &&
344              _gtk_css_parser_try_enum (parser, GTK_TYPE_BORDER_STYLE, &style))
345       {
346         g_value_init (&values[1], GTK_TYPE_BORDER_STYLE);
347         g_value_set_enum (&values[1], style);
348       }
349     else if (!G_IS_VALUE (&values[2]))
350       {
351         GtkSymbolicColor *symbolic;
352
353         symbolic = _gtk_css_parser_read_symbolic_color (parser);
354         if (symbolic == NULL)
355           return FALSE;
356
357         g_value_init (&values[2], GTK_TYPE_SYMBOLIC_COLOR);
358         g_value_take_boxed (&values[2], symbolic);
359       }
360     else
361       {
362         /* We parsed everything and there's still stuff left?
363          * Pretend we didn't notice and let the normal code produce
364          * a 'junk at end of value' error */
365         break;
366       }
367   }
368   while (!value_is_done_parsing (parser));
369
370   return TRUE;
371 }
372
373 static gboolean
374 parse_border (GtkCssShorthandProperty *shorthand,
375               GValue                  *values,
376               GtkCssParser            *parser,
377               GFile                   *base)
378 {
379   int style;
380
381   do
382   {
383     if (!G_IS_VALUE (&values[0]) &&
384          _gtk_css_parser_has_number (parser))
385       {
386         GtkCssNumber number;
387         if (!_gtk_css_parser_read_number (parser,
388                                           &number,
389                                           GTK_CSS_POSITIVE_ONLY
390                                           | GTK_CSS_NUMBER_AS_PIXELS
391                                           | GTK_CSS_PARSE_LENGTH))
392           return FALSE;
393
394         g_value_init (&values[0], GTK_TYPE_CSS_NUMBER);
395         g_value_init (&values[1], GTK_TYPE_CSS_NUMBER);
396         g_value_init (&values[2], GTK_TYPE_CSS_NUMBER);
397         g_value_init (&values[3], GTK_TYPE_CSS_NUMBER);
398         g_value_set_boxed (&values[0], &number);
399         g_value_set_boxed (&values[1], &number);
400         g_value_set_boxed (&values[2], &number);
401         g_value_set_boxed (&values[3], &number);
402       }
403     else if (!G_IS_VALUE (&values[4]) &&
404              _gtk_css_parser_try_enum (parser, GTK_TYPE_BORDER_STYLE, &style))
405       {
406         g_value_init (&values[4], GTK_TYPE_BORDER_STYLE);
407         g_value_init (&values[5], GTK_TYPE_BORDER_STYLE);
408         g_value_init (&values[6], GTK_TYPE_BORDER_STYLE);
409         g_value_init (&values[7], GTK_TYPE_BORDER_STYLE);
410         g_value_set_enum (&values[4], style);
411         g_value_set_enum (&values[5], style);
412         g_value_set_enum (&values[6], style);
413         g_value_set_enum (&values[7], style);
414       }
415     else if (!G_IS_VALUE (&values[8]))
416       {
417         GtkSymbolicColor *symbolic;
418
419         symbolic = _gtk_css_parser_read_symbolic_color (parser);
420         if (symbolic == NULL)
421           return FALSE;
422
423         g_value_init (&values[8], GTK_TYPE_SYMBOLIC_COLOR);
424         g_value_init (&values[9], GTK_TYPE_SYMBOLIC_COLOR);
425         g_value_init (&values[10], GTK_TYPE_SYMBOLIC_COLOR);
426         g_value_init (&values[11], GTK_TYPE_SYMBOLIC_COLOR);
427         g_value_set_boxed (&values[8], symbolic);
428         g_value_set_boxed (&values[9], symbolic);
429         g_value_set_boxed (&values[10], symbolic);
430         g_value_take_boxed (&values[11], symbolic);
431       }
432     else
433       {
434         /* We parsed everything and there's still stuff left?
435          * Pretend we didn't notice and let the normal code produce
436          * a 'junk at end of value' error */
437         break;
438       }
439   }
440   while (!value_is_done_parsing (parser));
441
442   /* Note that border-image values are not set: according to the spec
443      they just need to be reset when using the border shorthand */
444
445   return TRUE;
446 }
447
448 static gboolean
449 parse_font (GtkCssShorthandProperty *shorthand,
450             GValue                  *values,
451             GtkCssParser            *parser,
452             GFile                   *base)
453 {
454   PangoFontDescription *desc;
455   guint mask;
456   char *str;
457
458   str = _gtk_css_parser_read_value (parser);
459   if (str == NULL)
460     return FALSE;
461
462   desc = pango_font_description_from_string (str);
463   g_free (str);
464
465   mask = pango_font_description_get_set_fields (desc);
466
467   if (mask & PANGO_FONT_MASK_FAMILY)
468     {
469       GPtrArray *strv = g_ptr_array_new ();
470
471       g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (desc)));
472       g_ptr_array_add (strv, NULL);
473       g_value_init (&values[0], G_TYPE_STRV);
474       g_value_take_boxed (&values[0], g_ptr_array_free (strv, FALSE));
475     }
476   if (mask & PANGO_FONT_MASK_STYLE)
477     {
478       g_value_init (&values[1], PANGO_TYPE_STYLE);
479       g_value_set_enum (&values[1], pango_font_description_get_style (desc));
480     }
481   if (mask & PANGO_FONT_MASK_VARIANT)
482     {
483       g_value_init (&values[2], PANGO_TYPE_VARIANT);
484       g_value_set_enum (&values[2], pango_font_description_get_variant (desc));
485     }
486   if (mask & PANGO_FONT_MASK_WEIGHT)
487     {
488       g_value_init (&values[3], PANGO_TYPE_WEIGHT);
489       g_value_set_enum (&values[3], pango_font_description_get_weight (desc));
490     }
491   if (mask & PANGO_FONT_MASK_SIZE)
492     {
493       g_value_init (&values[4], G_TYPE_DOUBLE);
494       g_value_set_double (&values[4],
495                           (double) pango_font_description_get_size (desc) / PANGO_SCALE);
496     }
497
498   pango_font_description_free (desc);
499
500   return TRUE;
501 }
502
503 static gboolean
504 parse_background (GtkCssShorthandProperty *shorthand,
505                   GValue                  *values,
506                   GtkCssParser            *parser,
507                   GFile                   *base)
508 {
509   int enum_value;
510
511   do
512     {
513       /* the image part */
514       if (!G_IS_VALUE (&values[0]) &&
515           (_gtk_css_parser_has_prefix (parser, "none") ||
516            _gtk_css_image_can_parse (parser)))
517         {
518           GtkCssImage *image;
519
520           if (_gtk_css_parser_try (parser, "none", TRUE))
521             image = NULL;
522           else
523             {
524               image = _gtk_css_image_new_parse (parser, base);
525               if (image == NULL)
526                 return FALSE;
527             }
528
529           g_value_init (&values[0], GTK_TYPE_CSS_IMAGE);
530           g_value_take_object (&values[0], image);
531         }
532       else if (!G_IS_VALUE (&values[1]) &&
533                _gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &enum_value))
534         {
535           if (enum_value <= GTK_CSS_BACKGROUND_REPEAT_MASK)
536             {
537               int vertical;
538
539               if (_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &vertical))
540                 {
541                   if (vertical >= GTK_CSS_BACKGROUND_REPEAT_MASK)
542                     {
543                       _gtk_css_parser_error (parser, "Not a valid 2nd value for border-repeat");
544                       return FALSE;
545                     }
546                   else
547                     enum_value |= vertical << GTK_CSS_BACKGROUND_REPEAT_SHIFT;
548                 }
549               else
550                 enum_value |= enum_value << GTK_CSS_BACKGROUND_REPEAT_SHIFT;
551             }
552
553           g_value_init (&values[1], GTK_TYPE_CSS_BACKGROUND_REPEAT);
554           g_value_set_enum (&values[1], enum_value);
555         }
556       else if ((!G_IS_VALUE (&values[2]) || !G_IS_VALUE (&values[3])) &&
557                _gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_AREA, &enum_value))
558         {
559           guint idx = !G_IS_VALUE (&values[2]) ? 2 : 3;
560           g_value_init (&values[idx], GTK_TYPE_CSS_AREA);
561           g_value_set_enum (&values[idx], enum_value);
562         }
563       else if (!G_IS_VALUE (&values[4]))
564         {
565           GtkSymbolicColor *symbolic;
566           
567           symbolic = _gtk_css_parser_read_symbolic_color (parser);
568           if (symbolic == NULL)
569             return FALSE;
570
571           g_value_init (&values[4], GTK_TYPE_SYMBOLIC_COLOR);
572           g_value_take_boxed (&values[4], symbolic);
573         }
574       else
575         {
576           /* We parsed everything and there's still stuff left?
577            * Pretend we didn't notice and let the normal code produce
578            * a 'junk at end of value' error */
579           break;
580         }
581     }
582   while (!value_is_done_parsing (parser));
583
584   return TRUE;
585 }
586
587 /*** PACKING ***/
588
589 static void
590 unpack_border (GtkCssShorthandProperty *shorthand,
591                GtkStyleProperties      *props,
592                GtkStateFlags            state,
593                const GValue            *value)
594 {
595   GValue v = G_VALUE_INIT;
596   GtkBorder *border = g_value_get_boxed (value);
597
598   g_value_init (&v, G_TYPE_INT);
599
600   g_value_set_int (&v, border->top);
601   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 0)), props, state, &v);
602   g_value_set_int (&v, border->right);
603   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 1)), props, state, &v);
604   g_value_set_int (&v, border->bottom);
605   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 2)), props, state, &v);
606   g_value_set_int (&v, border->left);
607   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 3)), props, state, &v);
608
609   g_value_unset (&v);
610 }
611
612 static GtkCssValue *
613 pack_border (GtkCssShorthandProperty *shorthand,
614              GtkStyleQueryFunc        query_func,
615              gpointer                 query_data)
616 {
617   GtkCssStyleProperty *prop;
618   GtkBorder border;
619   GtkCssValue *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 = _gtk_css_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 = _gtk_css_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 = _gtk_css_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 = _gtk_css_value_get_int (v);
637
638   return _gtk_css_value_new_from_border (&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 GtkCssValue *
663 pack_border_radius (GtkCssShorthandProperty *shorthand,
664                     GtkStyleQueryFunc        query_func,
665                     gpointer                 query_data)
666 {
667   GtkCssBorderCornerRadius *top_left;
668   GtkCssStyleProperty *prop;
669   GtkCssValue *v;
670   int value = 0;
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 = _gtk_css_value_get_border_corner_radius (v);
677       if (top_left)
678         value = top_left->horizontal.value;
679     }
680
681   return _gtk_css_value_new_from_int (value);
682 }
683
684 static void
685 unpack_font_description (GtkCssShorthandProperty *shorthand,
686                          GtkStyleProperties      *props,
687                          GtkStateFlags            state,
688                          const GValue            *value)
689 {
690   GtkStyleProperty *prop;
691   PangoFontDescription *description;
692   PangoFontMask mask;
693   GValue v = G_VALUE_INIT;
694   
695   /* For backwards compat, we only unpack values that are indeed set.
696    * For strict CSS conformance we need to unpack all of them.
697    * Note that we do set all of them in the parse function, so it
698    * will not have effects when parsing CSS files. It will though
699    * for custom style providers.
700    */
701
702   description = g_value_get_boxed (value);
703
704   if (description)
705     mask = pango_font_description_get_set_fields (description);
706   else
707     mask = 0;
708
709   if (mask & PANGO_FONT_MASK_FAMILY)
710     {
711       GPtrArray *strv = g_ptr_array_new ();
712
713       g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
714       g_ptr_array_add (strv, NULL);
715       g_value_init (&v, G_TYPE_STRV);
716       g_value_take_boxed (&v, g_ptr_array_free (strv, FALSE));
717
718       prop = _gtk_style_property_lookup ("font-family");
719       _gtk_style_property_assign (prop, props, state, &v);
720       g_value_unset (&v);
721     }
722
723   if (mask & PANGO_FONT_MASK_STYLE)
724     {
725       g_value_init (&v, PANGO_TYPE_STYLE);
726       g_value_set_enum (&v, pango_font_description_get_style (description));
727
728       prop = _gtk_style_property_lookup ("font-style");
729       _gtk_style_property_assign (prop, props, state, &v);
730       g_value_unset (&v);
731     }
732
733   if (mask & PANGO_FONT_MASK_VARIANT)
734     {
735       g_value_init (&v, PANGO_TYPE_VARIANT);
736       g_value_set_enum (&v, pango_font_description_get_variant (description));
737
738       prop = _gtk_style_property_lookup ("font-variant");
739       _gtk_style_property_assign (prop, props, state, &v);
740       g_value_unset (&v);
741     }
742
743   if (mask & PANGO_FONT_MASK_WEIGHT)
744     {
745       g_value_init (&v, PANGO_TYPE_WEIGHT);
746       g_value_set_enum (&v, pango_font_description_get_weight (description));
747
748       prop = _gtk_style_property_lookup ("font-weight");
749       _gtk_style_property_assign (prop, props, state, &v);
750       g_value_unset (&v);
751     }
752
753   if (mask & PANGO_FONT_MASK_SIZE)
754     {
755       g_value_init (&v, G_TYPE_DOUBLE);
756       g_value_set_double (&v, (double) pango_font_description_get_size (description) / PANGO_SCALE);
757
758       prop = _gtk_style_property_lookup ("font-size");
759       _gtk_style_property_assign (prop, props, state, &v);
760       g_value_unset (&v);
761     }
762 }
763
764 static GtkCssValue *
765 pack_font_description (GtkCssShorthandProperty *shorthand,
766                        GtkStyleQueryFunc        query_func,
767                        gpointer                 query_data)
768 {
769   PangoFontDescription *description;
770   GtkCssValue *v;
771
772   description = pango_font_description_new ();
773
774   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-family"))), query_data);
775   if (v)
776     {
777       const char **families = _gtk_css_value_get_strv (v);
778       /* xxx: Can we set all the families here somehow? */
779       if (families)
780         pango_font_description_set_family (description, families[0]);
781     }
782
783   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-size"))), query_data);
784   if (v)
785     pango_font_description_set_size (description, round (_gtk_css_value_get_double (v) * PANGO_SCALE));
786
787   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-style"))), query_data);
788   if (v)
789     pango_font_description_set_style (description, _gtk_css_value_get_pango_style (v));
790
791   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-variant"))), query_data);
792   if (v)
793     pango_font_description_set_variant (description, _gtk_css_value_get_pango_variant (v));
794
795   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-weight"))), query_data);
796   if (v)
797     pango_font_description_set_weight (description, _gtk_css_value_get_pango_weight (v));
798
799   return _gtk_css_value_new_take_font_description (description);
800 }
801
802 static void
803 unpack_to_everything (GtkCssShorthandProperty *shorthand,
804                       GtkStyleProperties      *props,
805                       GtkStateFlags            state,
806                       const GValue            *value)
807 {
808   GtkCssStyleProperty *prop;
809   guint i, n;
810   
811   n = _gtk_css_shorthand_property_get_n_subproperties (shorthand);
812
813   for (i = 0; i < n; i++)
814     {
815       prop = _gtk_css_shorthand_property_get_subproperty (shorthand, i);
816       _gtk_style_property_assign (GTK_STYLE_PROPERTY (prop), props, state, value);
817     }
818 }
819
820 static GtkCssValue *
821 pack_first_element (GtkCssShorthandProperty *shorthand,
822                     GtkStyleQueryFunc        query_func,
823                     gpointer                 query_data)
824 {
825   GtkCssStyleProperty *prop;
826   GtkCssValue *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           return _gtk_css_value_ref (v);
841         }
842     }
843   return NULL;
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 }