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