]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssshorthandproperty.c
styleproperty: Make query() and assign() vfuncs
[~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 "gtkintl.h"
26
27 enum {
28   PROP_0,
29   PROP_SUBPROPERTIES,
30 };
31
32 G_DEFINE_TYPE (GtkCssShorthandProperty, _gtk_css_shorthand_property, GTK_TYPE_STYLE_PROPERTY)
33
34 static void
35 gtk_css_shorthand_property_set_property (GObject      *object,
36                                          guint         prop_id,
37                                          const GValue *value,
38                                          GParamSpec   *pspec)
39 {
40   GtkCssShorthandProperty *property = GTK_CSS_SHORTHAND_PROPERTY (object);
41   const char **subproperties;
42   guint i;
43
44   switch (prop_id)
45     {
46     case PROP_SUBPROPERTIES:
47       subproperties = g_value_get_boxed (value);
48       g_assert (subproperties);
49       for (i = 0; subproperties[i] != NULL; i++)
50         {
51           GtkStyleProperty *subproperty = _gtk_style_property_lookup (subproperties[i]);
52           g_assert (GTK_IS_CSS_STYLE_PROPERTY (subproperty));
53           g_ptr_array_add (property->subproperties, subproperty);
54         }
55       break;
56     default:
57       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
58       break;
59     }
60 }
61
62 static void
63 _gtk_css_shorthand_property_assign (GtkStyleProperty   *property,
64                                     GtkStyleProperties *props,
65                                     GtkStateFlags       state,
66                                     const GValue       *value)
67 {
68   GParameter *parameters;
69   guint i, n_parameters;
70
71   parameters = _gtk_style_property_unpack (property, value, &n_parameters);
72
73   for (i = 0; i < n_parameters; i++)
74     {
75       _gtk_style_property_assign (_gtk_style_property_lookup (parameters[i].name),
76                                   props,
77                                   state,
78                                   &parameters[i].value);
79       g_value_unset (&parameters[i].value);
80     }
81   g_free (parameters);
82 }
83
84 static void
85 _gtk_css_shorthand_property_query (GtkStyleProperty   *property,
86                                    GtkStyleProperties *props,
87                                    GtkStateFlags       state,
88                                    GtkStylePropertyContext *context,
89                                    GValue             *value)
90 {
91   property->pack_func (value, props, state, context);
92 }
93
94 static void
95 _gtk_css_shorthand_property_class_init (GtkCssShorthandPropertyClass *klass)
96 {
97   GObjectClass *object_class = G_OBJECT_CLASS (klass);
98   GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
99
100   object_class->set_property = gtk_css_shorthand_property_set_property;
101
102   g_object_class_install_property (object_class,
103                                    PROP_SUBPROPERTIES,
104                                    g_param_spec_boxed ("subproperties",
105                                                        P_("Subproperties"),
106                                                        P_("The list of subproperties"),
107                                                        G_TYPE_STRV,
108                                                        G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
109
110   property_class->assign = _gtk_css_shorthand_property_assign;
111   property_class->query = _gtk_css_shorthand_property_query;
112 }
113
114 static void
115 _gtk_css_shorthand_property_init (GtkCssShorthandProperty *shorthand)
116 {
117   shorthand->subproperties = g_ptr_array_new_with_free_func (g_object_unref);
118 }
119
120 GtkCssStyleProperty *
121 _gtk_css_shorthand_property_get_subproperty (GtkCssShorthandProperty *shorthand,
122                                              guint                    property)
123 {
124   g_return_val_if_fail (GTK_IS_CSS_SHORTHAND_PROPERTY (shorthand), NULL);
125   g_return_val_if_fail (property < shorthand->subproperties->len, NULL);
126
127   return g_ptr_array_index (shorthand->subproperties, property);
128 }
129
130 guint
131 _gtk_css_shorthand_property_get_n_subproperties (GtkCssShorthandProperty *shorthand)
132 {
133   g_return_val_if_fail (GTK_IS_CSS_SHORTHAND_PROPERTY (shorthand), 0);
134   
135   return shorthand->subproperties->len;
136 }
137