]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssshorthandproperty.c
0dcfff0cad64ccc7ed13731987054e5c35aeca22
[~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, 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 "gtkcssstylefuncsprivate.h"
26 #include "gtkcsstypesprivate.h"
27 #include "gtkintl.h"
28 #include "gtkprivatetypebuiltins.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   shorthand->query (shorthand, value, query_func, query_data);
85 }
86
87 static void
88 gtk_css_shorthand_property_unset_value (gpointer value)
89 {
90   if (G_IS_VALUE (value))
91     g_value_unset (value);
92 }
93
94 static gboolean
95 gtk_css_shorthand_property_parse_value (GtkStyleProperty *property,
96                                         GValue           *value,
97                                         GtkCssParser     *parser,
98                                         GFile            *base)
99 {
100   GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
101   GArray *array;
102   guint i;
103
104   array = g_array_new (FALSE, TRUE, sizeof (GValue));
105   g_array_set_clear_func (array, gtk_css_shorthand_property_unset_value);
106   g_array_set_size (array, shorthand->subproperties->len);
107
108   if (_gtk_css_parser_try (parser, "initial", TRUE))
109     {
110       /* the initial value can be explicitly specified with the
111        * ‘initial’ keyword which all properties accept.
112        */
113       for (i = 0; i < shorthand->subproperties->len; i++)
114         {
115           GValue *val = &g_array_index (array, GValue, i);
116           g_value_init (val, GTK_TYPE_CSS_SPECIAL_VALUE);
117           g_value_set_enum (val, GTK_CSS_INITIAL);
118         }
119     }
120   else if (_gtk_css_parser_try (parser, "inherit", TRUE))
121     {
122       /* All properties accept the ‘inherit’ value which
123        * explicitly specifies that the value will be determined
124        * by inheritance. The ‘inherit’ value can be used to
125        * strengthen inherited values in the cascade, and it can
126        * also be used on properties that are not normally inherited.
127        */
128       for (i = 0; i < shorthand->subproperties->len; i++)
129         {
130           GValue *val = &g_array_index (array, GValue, i);
131           g_value_init (val, GTK_TYPE_CSS_SPECIAL_VALUE);
132           g_value_set_enum (val, GTK_CSS_INHERIT);
133         }
134     }
135   else if (!shorthand->parse (shorthand, (GValue *) array->data, parser, base))
136     {
137       g_array_free (array, TRUE);
138       return FALSE;
139     }
140
141   /* All values that aren't set by the parse func are set to their
142    * default values here.
143    * XXX: Is the default always initial or can it be inherit? */
144   for (i = 0; i < shorthand->subproperties->len; i++)
145     {
146       GValue *val = &g_array_index (array, GValue, i);
147       if (G_IS_VALUE (val))
148         continue;
149       g_value_init (val, GTK_TYPE_CSS_SPECIAL_VALUE);
150       g_value_set_enum (val, GTK_CSS_INITIAL);
151     }
152
153   g_value_init (value, G_TYPE_ARRAY);
154   g_value_take_boxed (value, array);
155   return TRUE;
156 }
157
158 static void
159 _gtk_css_shorthand_property_class_init (GtkCssShorthandPropertyClass *klass)
160 {
161   GObjectClass *object_class = G_OBJECT_CLASS (klass);
162   GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
163
164   object_class->set_property = gtk_css_shorthand_property_set_property;
165
166   g_object_class_install_property (object_class,
167                                    PROP_SUBPROPERTIES,
168                                    g_param_spec_boxed ("subproperties",
169                                                        P_("Subproperties"),
170                                                        P_("The list of subproperties"),
171                                                        G_TYPE_STRV,
172                                                        G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
173
174   property_class->assign = _gtk_css_shorthand_property_assign;
175   property_class->query = _gtk_css_shorthand_property_query;
176   property_class->parse_value = gtk_css_shorthand_property_parse_value;
177 }
178
179 static void
180 _gtk_css_shorthand_property_init (GtkCssShorthandProperty *shorthand)
181 {
182   shorthand->subproperties = g_ptr_array_new_with_free_func (g_object_unref);
183 }
184
185 GtkCssStyleProperty *
186 _gtk_css_shorthand_property_get_subproperty (GtkCssShorthandProperty *shorthand,
187                                              guint                    property)
188 {
189   g_return_val_if_fail (GTK_IS_CSS_SHORTHAND_PROPERTY (shorthand), NULL);
190   g_return_val_if_fail (property < shorthand->subproperties->len, NULL);
191
192   return g_ptr_array_index (shorthand->subproperties, property);
193 }
194
195 guint
196 _gtk_css_shorthand_property_get_n_subproperties (GtkCssShorthandProperty *shorthand)
197 {
198   g_return_val_if_fail (GTK_IS_CSS_SHORTHAND_PROPERTY (shorthand), 0);
199   
200   return shorthand->subproperties->len;
201 }
202