]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsscustomproperty.c
css: No longer expose internal properties
[~andy/gtk] / gtk / gtkcsscustomproperty.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 "gtkcsscustompropertyprivate.h"
24
25 #include <string.h>
26
27 #include "gtkthemingengine.h"
28
29 G_DEFINE_TYPE (GtkCssCustomProperty, _gtk_css_custom_property, GTK_TYPE_CSS_STYLE_PROPERTY)
30
31 static void
32 _gtk_css_custom_property_class_init (GtkCssCustomPropertyClass *klass)
33 {
34 }
35
36
37 static void
38 _gtk_css_custom_property_init (GtkCssCustomProperty *custom_property)
39 {
40 }
41
42 static void
43 gtk_css_custom_property_create_initial_value (GParamSpec *pspec,
44                                               GValue     *value)
45 {
46   g_value_init (value, pspec->value_type);
47
48   if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
49     g_value_set_object (value, gtk_theming_engine_load (NULL));
50   else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
51     g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
52   else if (pspec->value_type == GDK_TYPE_RGBA)
53     {
54       GdkRGBA color;
55       gdk_rgba_parse (&color, "pink");
56       g_value_set_boxed (value, &color);
57     }
58   else if (pspec->value_type == GTK_TYPE_BORDER)
59     {
60       g_value_take_boxed (value, gtk_border_new ());
61     }
62   else
63     g_param_value_set_default (pspec, value);
64 }
65
66 /* Property registration functions */
67
68 /**
69  * gtk_theming_engine_register_property: (skip)
70  * @name_space: namespace for the property name
71  * @parse_func: parsing function to use, or %NULL
72  * @pspec: the #GParamSpec for the new property
73  *
74  * Registers a property so it can be used in the CSS file format,
75  * on the CSS file the property will look like
76  * "-${@name_space}-${property_name}". being
77  * ${property_name} the given to @pspec. @name_space will usually
78  * be the theme engine name.
79  *
80  * For any type a @parse_func may be provided, being this function
81  * used for turning any property value (between ':' and ';') in
82  * CSS to the #GValue needed. For basic types there is already
83  * builtin parsing support, so %NULL may be provided for these
84  * cases.
85  *
86  * <note>
87  * Engines must ensure property registration happens exactly once,
88  * usually GTK+ deals with theming engines as singletons, so this
89  * should be guaranteed to happen once, but bear this in mind
90  * when creating #GtkThemeEngine<!-- -->s yourself.
91  * </note>
92  *
93  * <note>
94  * In order to make use of the custom registered properties in
95  * the CSS file, make sure the engine is loaded first by specifying
96  * the engine property, either in a previous rule or within the same
97  * one.
98  * <programlisting>
99  * &ast; {
100  *     engine: someengine;
101  *     -SomeEngine-custom-property: 2;
102  * }
103  * </programlisting>
104  * </note>
105  *
106  * Since: 3.0
107  **/
108 void
109 gtk_theming_engine_register_property (const gchar            *name_space,
110                                       GtkStylePropertyParser  parse_func,
111                                       GParamSpec             *pspec)
112 {
113   GtkStyleProperty *node;
114   GValue initial = { 0, };
115   gchar *name;
116
117   g_return_if_fail (name_space != NULL);
118   g_return_if_fail (strchr (name_space, ' ') == NULL);
119   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
120
121   name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
122   gtk_css_custom_property_create_initial_value (pspec, &initial);
123
124   node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
125                        "initial-value", &initial,
126                        "name", name,
127                        "value-type", pspec->value_type,
128                        NULL);
129   node->pspec = pspec;
130   node->property_parse_func = parse_func;
131
132   g_value_unset (&initial);
133   g_free (name);
134 }
135
136 /**
137  * gtk_style_properties_register_property: (skip)
138  * @parse_func: parsing function to use, or %NULL
139  * @pspec: the #GParamSpec for the new property
140  *
141  * Registers a property so it can be used in the CSS file format.
142  * This function is the low-level equivalent of
143  * gtk_theming_engine_register_property(), if you are implementing
144  * a theming engine, you want to use that function instead.
145  *
146  * Since: 3.0
147  **/
148 void
149 gtk_style_properties_register_property (GtkStylePropertyParser  parse_func,
150                                         GParamSpec             *pspec)
151 {
152   GtkStyleProperty *node;
153   GValue initial = { 0, };
154
155   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
156
157   gtk_css_custom_property_create_initial_value (pspec, &initial);
158
159   node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
160                        "initial-value", &initial,
161                        "name", pspec->name,
162                        "value-type", pspec->value_type,
163                        NULL);
164   node->pspec = pspec;
165   node->property_parse_func = parse_func;
166
167   g_value_unset (&initial);
168 }
169
170 /**
171  * gtk_style_properties_lookup_property: (skip)
172  * @property_name: property name to look up
173  * @parse_func: (out): return location for the parse function
174  * @pspec: (out) (transfer none): return location for the #GParamSpec
175  *
176  * Returns %TRUE if a property has been registered, if @pspec or
177  * @parse_func are not %NULL, the #GParamSpec and parsing function
178  * will be respectively returned.
179  *
180  * Returns: %TRUE if the property is registered, %FALSE otherwise
181  *
182  * Since: 3.0
183  **/
184 gboolean
185 gtk_style_properties_lookup_property (const gchar             *property_name,
186                                       GtkStylePropertyParser  *parse_func,
187                                       GParamSpec             **pspec)
188 {
189   GtkStyleProperty *node;
190   gboolean found = FALSE;
191
192   g_return_val_if_fail (property_name != NULL, FALSE);
193
194   node = _gtk_style_property_lookup (property_name);
195
196   if (GTK_IS_CSS_CUSTOM_PROPERTY (node))
197     {
198       if (pspec)
199         *pspec = node->pspec;
200
201       if (parse_func)
202         *parse_func = node->property_parse_func;
203
204       found = TRUE;
205     }
206
207   return found;
208 }
209