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