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