]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyleproperty.c
styleproperty: Make _gtk_style_property_query() take a GValue
[~andy/gtk] / gtk / gtkstyleproperty.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
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 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
18 #include "config.h"
19
20 #include "gtkstylepropertyprivate.h"
21
22 #include "gtkcssprovider.h"
23 #include "gtkcssparserprivate.h"
24 #include "gtkcssshorthandpropertyprivate.h"
25 #include "gtkcssstylefuncsprivate.h"
26 #include "gtkcssstylepropertyprivate.h"
27 #include "gtkcsstypesprivate.h"
28 #include "gtkintl.h"
29 #include "gtkprivatetypebuiltins.h"
30 #include "gtkstylepropertiesprivate.h"
31
32 enum {
33   PROP_0,
34   PROP_NAME,
35   PROP_VALUE_TYPE
36 };
37
38 G_DEFINE_ABSTRACT_TYPE (GtkStyleProperty, _gtk_style_property, G_TYPE_OBJECT)
39
40 static void
41 gtk_style_property_finalize (GObject *object)
42 {
43   GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
44
45   g_warning ("finalizing %s `%s', how could this happen?", G_OBJECT_TYPE_NAME (object), property->name);
46
47   G_OBJECT_CLASS (_gtk_style_property_parent_class)->finalize (object);
48 }
49
50 static void
51 gtk_style_property_set_property (GObject      *object,
52                                  guint         prop_id,
53                                  const GValue *value,
54                                  GParamSpec   *pspec)
55 {
56   GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
57   GtkStylePropertyClass *klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
58
59   switch (prop_id)
60     {
61     case PROP_NAME:
62       property->name = g_value_dup_string (value);
63       g_assert (property->name);
64       g_assert (g_hash_table_lookup (klass->properties, property->name) == NULL);
65       g_hash_table_insert (klass->properties, property->name, property);
66       break;
67     case PROP_VALUE_TYPE:
68       property->value_type = g_value_get_gtype (value);
69       break;
70     default:
71       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
72       break;
73     }
74 }
75
76 static void
77 gtk_style_property_get_property (GObject    *object,
78                                  guint       prop_id,
79                                  GValue     *value,
80                                  GParamSpec *pspec)
81 {
82   GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
83
84   switch (prop_id)
85     {
86     case PROP_NAME:
87       g_value_set_string (value, property->name);
88       break;
89     case PROP_VALUE_TYPE:
90       g_value_set_gtype (value, property->value_type);
91       break;
92     default:
93       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
94       break;
95     }
96 }
97
98 static void
99 _gtk_style_property_class_init (GtkStylePropertyClass *klass)
100 {
101   GObjectClass *object_class = G_OBJECT_CLASS (klass);
102
103   object_class->finalize = gtk_style_property_finalize;
104   object_class->set_property = gtk_style_property_set_property;
105   object_class->get_property = gtk_style_property_get_property;
106
107   g_object_class_install_property (object_class,
108                                    PROP_NAME,
109                                    g_param_spec_string ("name",
110                                                         P_("Property name"),
111                                                         P_("The name of the property"),
112                                                         NULL,
113                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
114   g_object_class_install_property (object_class,
115                                    PROP_VALUE_TYPE,
116                                    g_param_spec_gtype ("value-type",
117                                                        P_("Value type"),
118                                                        P_("The value type returned by GtkStyleContext"),
119                                                        G_TYPE_NONE,
120                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
121
122   klass->properties = g_hash_table_new (g_str_hash, g_str_equal);
123 }
124
125 static void
126 _gtk_style_property_init (GtkStyleProperty *property)
127 {
128   property->value_type = G_TYPE_NONE;
129 }
130
131 /**
132  * _gtk_style_property_parse_value:
133  * @property: the property
134  * @value: an uninitialized value
135  * @parser: the parser to parse from
136  * @base: the base file for @aprser
137  *
138  * Tries to parse the given @property from the given @parser into
139  * @value. The type that @value will be assigned is dependant on
140  * the parser and no assumptions must be made about it. If the
141  * parsing fails, %FALSE will be returned and @value will be
142  * left uninitialized.
143  *
144  * Only if @property is a #GtkCssShorthandProperty, the @value will
145  * always contain a #GValueArray with the values to be used for
146  * the subproperties.
147  *
148  * Returns: %TRUE on success
149  **/
150 gboolean
151 _gtk_style_property_parse_value (GtkStyleProperty *property,
152                                  GValue           *value,
153                                  GtkCssParser     *parser,
154                                  GFile            *base)
155 {
156   GtkStylePropertyClass *klass;
157
158   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), FALSE);
159   g_return_val_if_fail (value != NULL, FALSE);
160   g_return_val_if_fail (parser != NULL, FALSE);
161
162   klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
163
164   return klass->parse_value (property, value, parser, base);
165 }
166
167 /**
168  * _gtk_style_property_assign:
169  * @property: the property
170  * @props: The properties to assign to
171  * @state: The state to assign
172  * @value: (out): the #GValue with the value to be
173  *     assigned
174  *
175  * This function is called by gtk_style_properties_set() and in
176  * turn gtk_style_context_set() and similar functions to set the
177  * value from code using old APIs.
178  **/
179 void
180 _gtk_style_property_assign (GtkStyleProperty   *property,
181                             GtkStyleProperties *props,
182                             GtkStateFlags       state,
183                             const GValue       *value)
184 {
185   GtkStylePropertyClass *klass;
186
187   g_return_if_fail (GTK_IS_STYLE_PROPERTY (property));
188   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
189   g_return_if_fail (value != NULL);
190
191   klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
192
193   klass->assign (property, props, state, value);
194 }
195
196 /**
197  * _gtk_style_property_query:
198  * @property: the property
199  * @value: (out): an uninitialized #GValue to be filled with the
200  *   contents of the lookup
201  * @query_func: The function to use to query properties
202  * @query_data: The data to pass to @query_func
203  *
204  * This function is called by gtk_style_properties_get() and in
205  * turn gtk_style_context_get() and similar functions to get the
206  * value to return to code using old APIs.
207  **/
208 void
209 _gtk_style_property_query (GtkStyleProperty  *property,
210                            GValue            *value,
211                            GtkStyleQueryFunc  query_func,
212                            gpointer           query_data)
213 {
214   GtkStylePropertyClass *klass;
215
216   g_return_if_fail (value != NULL);
217   g_return_if_fail (GTK_IS_STYLE_PROPERTY (property));
218   g_return_if_fail (query_func != NULL);
219
220   klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
221
222   return klass->query (property, value, query_func, query_data);
223 }
224
225 void
226 _gtk_style_property_init_properties (void)
227 {
228   static gboolean initialized = FALSE;
229
230   if (G_LIKELY (initialized))
231     return;
232
233   initialized = TRUE;
234
235   _gtk_css_style_property_init_properties ();
236   /* initialize shorthands last, they depend on the real properties existing */
237   _gtk_css_shorthand_property_init_properties ();
238 }
239
240 /**
241  * _gtk_style_property_lookup:
242  * @name: name of the property to lookup
243  *
244  * Looks up the CSS property with the given @name. If no such
245  * property exists, %NULL is returned.
246  *
247  * Returns: (transfer none): The property or %NULL if no
248  *     property with the given name exists.
249  **/
250 GtkStyleProperty *
251 _gtk_style_property_lookup (const char *name)
252 {
253   GtkStylePropertyClass *klass;
254
255   g_return_val_if_fail (name != NULL, NULL);
256
257   _gtk_style_property_init_properties ();
258
259   klass = g_type_class_peek (GTK_TYPE_STYLE_PROPERTY);
260
261   return g_hash_table_lookup (klass->properties, name);
262 }
263
264 /**
265  * _gtk_style_property_get_name:
266  * @property: the property to query
267  *
268  * Gets the name of the given property.
269  *
270  * Returns: the name of the property
271  **/
272 const char *
273 _gtk_style_property_get_name (GtkStyleProperty *property)
274 {
275   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), NULL);
276
277   return property->name;
278 }
279
280 /**
281  * _gtk_style_property_get_value_type:
282  * @property: the property to query
283  *
284  * Gets the value type of the @property, if the property is usable
285  * in public API via _gtk_style_property_assign() and
286  * _gtk_style_property_query(). If the @property is not usable in that
287  * way, %G_TYPE_NONE is returned.
288  *
289  * Returns: the value type in use or %G_TYPE_NONE if none.
290  **/
291 GType
292 _gtk_style_property_get_value_type (GtkStyleProperty *property)
293 {
294   g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), G_TYPE_NONE);
295
296   return property->value_type;
297 }