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