]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssstringvalue.c
cssvalue: Pass property ID to transition function
[~andy/gtk] / gtk / gtkcssstringvalue.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 "gtkcssstringvalueprivate.h"
21
22 #include <string.h>
23
24 struct _GtkCssValue {
25   GTK_CSS_VALUE_BASE
26   char *string;
27 };
28
29 static void
30 gtk_css_value_string_free (GtkCssValue *value)
31 {
32   g_slice_free (GtkCssValue, value);
33 }
34
35 static GtkCssValue *
36 gtk_css_value_string_compute (GtkCssValue        *value,
37                               guint               property_id,
38                               GtkStyleContext    *context,
39                               GtkCssDependencies *dependencies)
40 {
41   return _gtk_css_value_ref (value);
42 }
43
44 static gboolean
45 gtk_css_value_string_equal (const GtkCssValue *value1,
46                             const GtkCssValue *value2)
47 {
48   return g_strcmp0 (value1->string, value2->string) == 0;
49 }
50
51 static GtkCssValue *
52 gtk_css_value_string_transition (GtkCssValue *start,
53                                  GtkCssValue *end,
54                                  guint        property_id,
55                                  double       progress)
56 {
57   return NULL;
58 }
59
60 static void
61 gtk_css_value_string_print (const GtkCssValue *value,
62                             GString           *str)
63 {
64   char *string = value->string;
65   gsize len;
66
67   g_string_append_c (str, '"');
68
69   do {
70     len = strcspn (string, "\"\n\r\f");
71     g_string_append_len (str, string, len);
72     string += len;
73     switch (*string)
74       {
75       case '\0':
76         break;
77       case '\n':
78         g_string_append (str, "\\A ");
79         break;
80       case '\r':
81         g_string_append (str, "\\D ");
82         break;
83       case '\f':
84         g_string_append (str, "\\C ");
85         break;
86       case '\"':
87         g_string_append (str, "\\\"");
88         break;
89       case '\\':
90         g_string_append (str, "\\\\");
91         break;
92       default:
93         g_assert_not_reached ();
94         break;
95       }
96   } while (*string);
97
98   g_string_append_c (str, '"');
99 }
100
101 static void
102 gtk_css_value_ident_print (const GtkCssValue *value,
103                             GString           *str)
104 {
105   char *string = value->string;
106   gsize len;
107
108   do {
109     len = strcspn (string, "\"\n\r\f");
110     g_string_append_len (str, string, len);
111     string += len;
112     switch (*string)
113       {
114       case '\0':
115         break;
116       case '\n':
117         g_string_append (str, "\\A ");
118         break;
119       case '\r':
120         g_string_append (str, "\\D ");
121         break;
122       case '\f':
123         g_string_append (str, "\\C ");
124         break;
125       case '\"':
126         g_string_append (str, "\\\"");
127         break;
128       case '\'':
129         g_string_append (str, "\\'");
130         break;
131       case '\\':
132         g_string_append (str, "\\\\");
133         break;
134       default:
135         g_assert_not_reached ();
136         break;
137       }
138   } while (*string);
139 }
140
141 static const GtkCssValueClass GTK_CSS_VALUE_STRING = {
142   gtk_css_value_string_free,
143   gtk_css_value_string_compute,
144   gtk_css_value_string_equal,
145   gtk_css_value_string_transition,
146   gtk_css_value_string_print
147 };
148
149 static const GtkCssValueClass GTK_CSS_VALUE_IDENT = {
150   gtk_css_value_string_free,
151   gtk_css_value_string_compute,
152   gtk_css_value_string_equal,
153   gtk_css_value_string_transition,
154   gtk_css_value_ident_print
155 };
156
157 GtkCssValue *
158 _gtk_css_string_value_new (const char *string)
159 {
160   return _gtk_css_string_value_new_take (g_strdup (string));
161 }
162
163 GtkCssValue *
164 _gtk_css_string_value_new_take (char *string)
165 {
166   GtkCssValue *result;
167
168   result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_STRING);
169   result->string = string;
170
171   return result;
172 }
173
174 GtkCssValue *
175 _gtk_css_string_value_parse (GtkCssParser *parser)
176 {
177   char *s;
178
179   g_return_val_if_fail (parser != NULL, NULL);
180
181   s = _gtk_css_parser_read_string (parser);
182   if (s == NULL)
183     return NULL;
184   
185   return _gtk_css_string_value_new_take (s);
186 }
187
188 const char *
189 _gtk_css_string_value_get (const GtkCssValue *value)
190 {
191   g_return_val_if_fail (value != NULL, NULL);
192   g_return_val_if_fail (value->class == &GTK_CSS_VALUE_STRING, NULL);
193
194   return value->string;
195 }
196
197 GtkCssValue *
198 _gtk_css_ident_value_new (const char *ident)
199 {
200   return _gtk_css_ident_value_new_take (g_strdup (ident));
201 }
202
203 GtkCssValue *
204 _gtk_css_ident_value_new_take (char *ident)
205 {
206   GtkCssValue *result;
207
208   result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_IDENT);
209   result->string = ident;
210
211   return result;
212 }
213
214 GtkCssValue *
215 _gtk_css_ident_value_try_parse (GtkCssParser *parser)
216 {
217   char *ident;
218
219   g_return_val_if_fail (parser != NULL, NULL);
220
221   ident = _gtk_css_parser_try_ident (parser, TRUE);
222   if (ident == NULL)
223     return NULL;
224   
225   return _gtk_css_ident_value_new_take (ident);
226 }
227
228 const char *
229 _gtk_css_ident_value_get (const GtkCssValue *value)
230 {
231   g_return_val_if_fail (value != NULL, NULL);
232   g_return_val_if_fail (value->class == &GTK_CSS_VALUE_IDENT, NULL);
233
234   return value->string;
235 }
236