]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssarrayvalue.c
gtk: Fix warnings for some uses of GtkLinkButton
[~andy/gtk] / gtk / gtkcssarrayvalue.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 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 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 "gtkcssarrayvalueprivate.h"
21
22 #include <string.h>
23
24 struct _GtkCssValue {
25   GTK_CSS_VALUE_BASE
26   guint         n_values;
27   GtkCssValue  *values[1];
28 };
29
30 static void
31 gtk_css_value_array_free (GtkCssValue *value)
32 {
33   guint i;
34
35   for (i = 0; i < value->n_values; i++)
36     {
37       _gtk_css_value_unref (value->values[i]);
38     }
39
40   g_slice_free1 (sizeof (GtkCssValue) + sizeof (GtkCssValue *) * (value->n_values - 1), value);
41 }
42
43 static gboolean
44 gtk_css_value_array_equal (const GtkCssValue *value1,
45                            const GtkCssValue *value2)
46 {
47   guint i;
48
49   if (value1->n_values != value2->n_values)
50     return FALSE;
51
52   for (i = 0; i < value1->n_values; i++)
53     {
54       if (!_gtk_css_value_equal (value1->values[i],
55                                  value2->values[i]))
56         return FALSE;
57     }
58
59   return TRUE;
60 }
61
62 static GtkCssValue *
63 gtk_css_value_array_transition (GtkCssValue *start,
64                                 GtkCssValue *end,
65                                 double       progress)
66 {
67   return NULL;
68 }
69
70 static void
71 gtk_css_value_array_print (const GtkCssValue *value,
72                            GString           *string)
73 {
74   guint i;
75
76   if (value->n_values == 0)
77     {
78       g_string_append (string, "none");
79       return;
80     }
81
82   for (i = 0; i < value->n_values; i++)
83     {
84       if (i > 0)
85         g_string_append (string, ", ");
86       _gtk_css_value_print (value->values[i], string);
87     }
88 }
89
90 static const GtkCssValueClass GTK_CSS_VALUE_ARRAY = {
91   gtk_css_value_array_free,
92   gtk_css_value_array_equal,
93   gtk_css_value_array_transition,
94   gtk_css_value_array_print
95 };
96
97 static GtkCssValue none_singleton = { &GTK_CSS_VALUE_ARRAY, 1, 0, { NULL } };
98
99 GtkCssValue *
100 _gtk_css_array_value_new (GtkCssValue *content)
101 {
102   if (content == NULL)
103     return _gtk_css_value_ref (&none_singleton);
104
105   return _gtk_css_array_value_new_from_array (&content, 1);
106 }
107
108 GtkCssValue *
109 _gtk_css_array_value_new_from_array (GtkCssValue **values,
110                                      guint         n_values)
111 {
112   GtkCssValue *result;
113            
114   g_return_val_if_fail (values != NULL, NULL);
115   g_return_val_if_fail (n_values > 0, NULL);
116          
117   result = _gtk_css_value_alloc (&GTK_CSS_VALUE_ARRAY, sizeof (GtkCssValue) + sizeof (GtkCssValue *) * (n_values - 1));
118   result->n_values = n_values;
119   memcpy (&result->values[0], values, sizeof (GtkCssValue *) * n_values);
120             
121   return result;
122 }
123
124 GtkCssValue *
125 _gtk_css_array_value_parse (GtkCssParser *parser,
126                             GtkCssValue  *(* parse_func) (GtkCssParser *parser),
127                             gboolean      allow_none)
128 {
129   GtkCssValue *value, *result;
130   GPtrArray *values;
131
132   if (allow_none &&
133       _gtk_css_parser_try (parser, "none", TRUE))
134     return _gtk_css_value_ref (&none_singleton);
135
136   values = g_ptr_array_new ();
137
138   do {
139     value = parse_func (parser);
140
141     if (value == NULL)
142       {
143         g_ptr_array_set_free_func (values, (GDestroyNotify) _gtk_css_value_unref);
144         g_ptr_array_free (values, TRUE);
145         return NULL;
146       }
147
148     g_ptr_array_add (values, value);
149   } while (_gtk_css_parser_try (parser, ",", TRUE));
150
151   result = _gtk_css_array_value_new_from_array ((GtkCssValue **) values->pdata, values->len);
152   g_ptr_array_free (values, TRUE);
153   return result;
154 }
155
156 GtkCssValue *
157 _gtk_css_array_value_compute (GtkCssValue     *value,
158                               GtkCssValue *    (* compute_func) (GtkCssValue *, GtkStyleContext *),
159                               GtkStyleContext *context)
160 {
161   GtkCssValue *result;
162   gboolean changed = FALSE;
163   guint i;
164
165   g_return_val_if_fail (value->class == &GTK_CSS_VALUE_ARRAY, NULL);
166   g_return_val_if_fail (compute_func != NULL, NULL);
167
168   if (value->n_values == 0)
169     return _gtk_css_value_ref (value);
170
171   result = _gtk_css_array_value_new_from_array (value->values, value->n_values);
172   for (i = 0; i < value->n_values; i++)
173     {
174       result->values[i] = (* compute_func) (value->values[i], context);
175       changed |= (result->values[i] != value->values[i]);
176     }
177
178   if (!changed)
179     {
180       _gtk_css_value_unref (result);
181       return _gtk_css_value_ref (value);
182     }
183
184   return result;
185 }
186
187 GtkCssValue *
188 _gtk_css_array_value_get_nth (const GtkCssValue *value,
189                               guint              i)
190 {
191   g_return_val_if_fail (value != NULL, NULL);
192   g_return_val_if_fail (value->class == &GTK_CSS_VALUE_ARRAY, NULL);
193   g_return_val_if_fail (value->n_values > 0, NULL);
194
195   return value->values[i % value->n_values];
196 }
197
198 guint
199 _gtk_css_array_value_get_n_values (const GtkCssValue *value)
200 {
201   g_return_val_if_fail (value != NULL, 0);
202   g_return_val_if_fail (value->class == &GTK_CSS_VALUE_ARRAY, 0);
203
204   return value->n_values;
205 }
206