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