]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssstyleproperty.c
styleproperty: Move value printing to real properties
[~andy/gtk] / gtk / gtkcssstyleproperty.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 "gtkcssstylepropertyprivate.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_ID,
33   PROP_INHERIT,
34   PROP_INITIAL
35 };
36
37 G_DEFINE_TYPE (GtkCssStyleProperty, _gtk_css_style_property, GTK_TYPE_STYLE_PROPERTY)
38
39 static void
40 gtk_css_style_property_constructed (GObject *object)
41 {
42   GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
43   GtkCssStylePropertyClass *klass = GTK_CSS_STYLE_PROPERTY_GET_CLASS (property);
44
45   property->id = klass->style_properties->len;
46   g_ptr_array_add (klass->style_properties, property);
47
48   G_OBJECT_CLASS (_gtk_css_style_property_parent_class)->constructed (object);
49 }
50
51 static void
52 gtk_css_style_property_set_property (GObject      *object,
53                                      guint         prop_id,
54                                      const GValue *value,
55                                      GParamSpec   *pspec)
56 {
57   GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
58   const GValue *initial;
59
60   switch (prop_id)
61     {
62     case PROP_INHERIT:
63       property->inherit = g_value_get_boolean (value);
64       break;
65     case PROP_INITIAL:
66       initial = g_value_get_boxed (value);
67       g_assert (initial);
68       g_value_init (&property->initial_value, G_VALUE_TYPE (initial));
69       g_value_copy (initial, &property->initial_value);
70       break;
71     default:
72       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
73       break;
74     }
75 }
76
77 static void
78 gtk_css_style_property_get_property (GObject    *object,
79                                      guint       prop_id,
80                                      GValue     *value,
81                                      GParamSpec *pspec)
82 {
83   GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
84
85   switch (prop_id)
86     {
87     case PROP_ID:
88       g_value_set_boolean (value, property->id);
89       break;
90     case PROP_INHERIT:
91       g_value_set_boolean (value, property->inherit);
92       break;
93     case PROP_INITIAL:
94       g_value_set_boxed (value, &property->initial_value);
95       break;
96     default:
97       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
98       break;
99     }
100 }
101
102 static void
103 _gtk_css_style_property_class_init (GtkCssStylePropertyClass *klass)
104 {
105   GObjectClass *object_class = G_OBJECT_CLASS (klass);
106
107   object_class->constructed = gtk_css_style_property_constructed;
108   object_class->set_property = gtk_css_style_property_set_property;
109   object_class->get_property = gtk_css_style_property_get_property;
110
111   g_object_class_install_property (object_class,
112                                    PROP_ID,
113                                    g_param_spec_uint ("id",
114                                                       P_("ID"),
115                                                       P_("The numeric id for quick access"),
116                                                       0, G_MAXUINT, 0,
117                                                       G_PARAM_READABLE));
118   g_object_class_install_property (object_class,
119                                    PROP_INHERIT,
120                                    g_param_spec_boolean ("inherit",
121                                                          P_("Inherit"),
122                                                          P_("Set if the value is inherited by default"),
123                                                          FALSE,
124                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
125   g_object_class_install_property (object_class,
126                                    PROP_INITIAL,
127                                    g_param_spec_boxed ("initial-value",
128                                                        P_("Initial value"),
129                                                        P_("The initial specified value used for this property"),
130                                                        G_TYPE_VALUE,
131                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
132
133   klass->style_properties = g_ptr_array_new ();
134 }
135
136
137 static void
138 _gtk_css_style_property_init (GtkCssStyleProperty *style_property)
139 {
140 }
141
142 /**
143  * _gtk_css_style_property_get_n_properties:
144  *
145  * Gets the number of style properties. This number can increase when new
146  * theme engines are loaded. Shorthand properties are not included here.
147  *
148  * Returns: The number of style properties.
149  **/
150 guint
151 _gtk_css_style_property_get_n_properties (void)
152 {
153   GtkCssStylePropertyClass *klass;
154
155   klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
156
157   return klass->style_properties->len;
158 }
159
160 /**
161  * _gtk_css_style_property_lookup_by_id:
162  * @id: the id of the property
163  *
164  * Gets the style property with the given id. All style properties (but not
165  * shorthand properties) are indexable by id so that it's easy to use arrays
166  * when doing style lookups.
167  *
168  * Returns: (transfer none): The style property with the given id
169  **/
170 GtkCssStyleProperty *
171 _gtk_css_style_property_lookup_by_id (guint id)
172 {
173   GtkCssStylePropertyClass *klass;
174
175   klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
176
177   return g_ptr_array_index (klass->style_properties, id);
178 }
179
180 /**
181  * _gtk_css_style_property_is_inherit:
182  * @property: the property
183  *
184  * Queries if the given @property is inherited. See
185  * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance>
186  * the CSS documentation</ulink> for an explanation of this concept.
187  *
188  * Returns: %TRUE if the property is inherited by default.
189  **/
190 gboolean
191 _gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
192 {
193   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
194
195   return property->inherit;
196 }
197
198 /**
199  * _gtk_css_style_property_get_id:
200  * @property: the property
201  *
202  * Gets the id for the given property. IDs are used to allow using arrays
203  * for style lookups.
204  *
205  * Returns: The id of the property
206  **/
207 guint
208 _gtk_css_style_property_get_id (GtkCssStyleProperty *property)
209 {
210   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
211
212   return property->id;
213 }
214
215 /**
216  * _gtk_css_style_property_get_initial_value:
217  * @property: the property
218  *
219  * Queries the initial value of the given @property. See
220  * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
221  * the CSS documentation</ulink> for an explanation of this concept.
222  *
223  * Returns: a reference to the initial value. The value will never change.
224  **/
225 const GValue *
226 _gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
227 {
228   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
229
230   return &property->initial_value;
231 }
232
233 /**
234  * _gtk_css_style_property_print_value:
235  * @property: the property
236  * @value: the value to print
237  * @string: the string to print to
238  *
239  * Prints @value to the given @string in CSS format. The @value must be a
240  * valid specified value as parsed using the parse functions or as assigned
241  * via _gtk_style_property_assign().
242  **/
243 void
244 _gtk_css_style_property_print_value (GtkCssStyleProperty    *property,
245                                      const GValue           *value,
246                                      GString                *string)
247 {
248   g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
249   g_return_if_fail (value != NULL);
250   g_return_if_fail (string != NULL);
251
252   if (G_VALUE_HOLDS (value, GTK_TYPE_CSS_SPECIAL_VALUE))
253     {
254       GEnumClass *enum_class;
255       GEnumValue *enum_value;
256
257       enum_class = g_type_class_ref (GTK_TYPE_CSS_SPECIAL_VALUE);
258       enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
259
260       g_string_append (string, enum_value->value_nick);
261
262       g_type_class_unref (enum_class);
263     }
264   else if (GTK_STYLE_PROPERTY (property)->print_func)
265     (* GTK_STYLE_PROPERTY (property)->print_func) (value, string);
266   else
267     _gtk_css_style_print_value (value, string);
268 }
269