]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssshorthandproperty.c
f10201d31cdf257cddc830a42e16610c2f7691ec
[~andy/gtk] / gtk / gtkcssshorthandproperty.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 "gtkcssarrayvalueprivate.h"
25 #include "gtkcssinheritvalueprivate.h"
26 #include "gtkcssinitialvalueprivate.h"
27 #include "gtkcssstylefuncsprivate.h"
28 #include "gtkintl.h"
29
30 enum {
31   PROP_0,
32   PROP_SUBPROPERTIES,
33 };
34
35 G_DEFINE_TYPE (GtkCssShorthandProperty, _gtk_css_shorthand_property, GTK_TYPE_STYLE_PROPERTY)
36
37 static void
38 gtk_css_shorthand_property_set_property (GObject      *object,
39                                          guint         prop_id,
40                                          const GValue *value,
41                                          GParamSpec   *pspec)
42 {
43   GtkCssShorthandProperty *property = GTK_CSS_SHORTHAND_PROPERTY (object);
44   const char **subproperties;
45   guint i;
46
47   switch (prop_id)
48     {
49     case PROP_SUBPROPERTIES:
50       subproperties = g_value_get_boxed (value);
51       g_assert (subproperties);
52       for (i = 0; subproperties[i] != NULL; i++)
53         {
54           GtkStyleProperty *subproperty = _gtk_style_property_lookup (subproperties[i]);
55           g_assert (GTK_IS_CSS_STYLE_PROPERTY (subproperty));
56           g_ptr_array_add (property->subproperties, subproperty);
57         }
58       break;
59     default:
60       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
61       break;
62     }
63 }
64
65 static void
66 _gtk_css_shorthand_property_assign (GtkStyleProperty   *property,
67                                     GtkStyleProperties *props,
68                                     GtkStateFlags       state,
69                                     const GValue       *value)
70 {
71   GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
72
73   shorthand->assign (shorthand, props, state, value);
74 }
75
76 static void
77 _gtk_css_shorthand_property_query (GtkStyleProperty   *property,
78                                    GValue             *value,
79                                    GtkStyleQueryFunc   query_func,
80                                    gpointer            query_data)
81 {
82   GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
83
84   return shorthand->query (shorthand, value, query_func, query_data);
85 }
86
87 static GtkCssValue *
88 gtk_css_shorthand_property_parse_value (GtkStyleProperty *property,
89                                         GtkCssParser     *parser,
90                                         GFile            *base)
91 {
92   GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
93   GtkCssValue **data;
94   GtkCssValue *result;
95   guint i;
96
97   data = g_new0 (GtkCssValue *, shorthand->subproperties->len);
98
99   if (_gtk_css_parser_try (parser, "initial", TRUE))
100     {
101       /* the initial value can be explicitly specified with the
102        * ‘initial’ keyword which all properties accept.
103        */
104       for (i = 0; i < shorthand->subproperties->len; i++)
105         {
106           data[i] = _gtk_css_initial_value_new ();
107         }
108     }
109   else if (_gtk_css_parser_try (parser, "inherit", TRUE))
110     {
111       /* All properties accept the ‘inherit’ value which
112        * explicitly specifies that the value will be determined
113        * by inheritance. The ‘inherit’ value can be used to
114        * strengthen inherited values in the cascade, and it can
115        * also be used on properties that are not normally inherited.
116        */
117       for (i = 0; i < shorthand->subproperties->len; i++)
118         {
119           data[i] = _gtk_css_inherit_value_new ();
120         }
121     }
122   else if (!shorthand->parse (shorthand, data, parser, base))
123     {
124       for (i = 0; i < shorthand->subproperties->len; i++)
125         {
126           if (data[i] != NULL)
127             _gtk_css_value_unref (data[i]);
128         }
129       g_free (data);
130       return NULL;
131     }
132
133   /* All values that aren't set by the parse func are set to their
134    * default values here.
135    * XXX: Is the default always initial or can it be inherit? */
136   for (i = 0; i < shorthand->subproperties->len; i++)
137     {
138       if (data[i] == NULL)
139         data[i] = _gtk_css_initial_value_new ();
140     }
141
142   result = _gtk_css_array_value_new (data, shorthand->subproperties->len);
143   g_free (data);
144   
145   return result;
146 }
147
148 static void
149 _gtk_css_shorthand_property_class_init (GtkCssShorthandPropertyClass *klass)
150 {
151   GObjectClass *object_class = G_OBJECT_CLASS (klass);
152   GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
153
154   object_class->set_property = gtk_css_shorthand_property_set_property;
155
156   g_object_class_install_property (object_class,
157                                    PROP_SUBPROPERTIES,
158                                    g_param_spec_boxed ("subproperties",
159                                                        P_("Subproperties"),
160                                                        P_("The list of subproperties"),
161                                                        G_TYPE_STRV,
162                                                        G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
163
164   property_class->assign = _gtk_css_shorthand_property_assign;
165   property_class->query = _gtk_css_shorthand_property_query;
166   property_class->parse_value = gtk_css_shorthand_property_parse_value;
167 }
168
169 static void
170 _gtk_css_shorthand_property_init (GtkCssShorthandProperty *shorthand)
171 {
172   shorthand->subproperties = g_ptr_array_new_with_free_func (g_object_unref);
173 }
174
175 GtkCssStyleProperty *
176 _gtk_css_shorthand_property_get_subproperty (GtkCssShorthandProperty *shorthand,
177                                              guint                    property)
178 {
179   g_return_val_if_fail (GTK_IS_CSS_SHORTHAND_PROPERTY (shorthand), NULL);
180   g_return_val_if_fail (property < shorthand->subproperties->len, NULL);
181
182   return g_ptr_array_index (shorthand->subproperties, property);
183 }
184
185 guint
186 _gtk_css_shorthand_property_get_n_subproperties (GtkCssShorthandProperty *shorthand)
187 {
188   g_return_val_if_fail (GTK_IS_CSS_SHORTHAND_PROPERTY (shorthand), 0);
189   
190   return shorthand->subproperties->len;
191 }
192