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