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