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 | #include "gtk/css/gtkcssserializerprivate.h" |
22 | |
23 | #include <string.h> |
24 | |
25 | struct _GtkCssValue { |
26 | GTK_CSS_VALUE_BASE |
27 | char *string; |
28 | }; |
29 | |
30 | static void |
31 | gtk_css_value_string_free (GtkCssValue *value) |
32 | { |
33 | g_free (mem: value->string); |
34 | g_slice_free (GtkCssValue, value); |
35 | } |
36 | |
37 | static GtkCssValue * |
38 | gtk_css_value_string_compute (GtkCssValue *value, |
39 | guint property_id, |
40 | GtkStyleProvider *provider, |
41 | GtkCssStyle *style, |
42 | GtkCssStyle *parent_style) |
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 (str1: value1->string, str2: 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 | if (value->string == NULL) |
68 | { |
69 | g_string_append (string: str, val: "none" ); |
70 | return; |
71 | } |
72 | |
73 | gtk_css_print_string (str, string: value->string, FALSE); |
74 | } |
75 | |
76 | static void |
77 | gtk_css_value_ident_print (const GtkCssValue *value, |
78 | GString *str) |
79 | { |
80 | char *string = value->string; |
81 | gsize len; |
82 | |
83 | do { |
84 | len = strcspn (s: string, reject: "\"\n\r\f" ); |
85 | g_string_append_len (string: str, val: string, len); |
86 | string += len; |
87 | switch (*string) |
88 | { |
89 | case '\0': |
90 | goto out; |
91 | case '\n': |
92 | g_string_append (string: str, val: "\\A " ); |
93 | break; |
94 | case '\r': |
95 | g_string_append (string: str, val: "\\D " ); |
96 | break; |
97 | case '\f': |
98 | g_string_append (string: str, val: "\\C " ); |
99 | break; |
100 | case '\"': |
101 | g_string_append (string: str, val: "\\\"" ); |
102 | break; |
103 | case '\'': |
104 | g_string_append (string: str, val: "\\'" ); |
105 | break; |
106 | case '\\': |
107 | g_string_append (string: str, val: "\\\\" ); |
108 | break; |
109 | default: |
110 | g_assert_not_reached (); |
111 | break; |
112 | } |
113 | string++; |
114 | } while (*string); |
115 | |
116 | out: |
117 | ; |
118 | } |
119 | |
120 | static const GtkCssValueClass GTK_CSS_VALUE_STRING = { |
121 | "GtkCssStringValue" , |
122 | gtk_css_value_string_free, |
123 | gtk_css_value_string_compute, |
124 | gtk_css_value_string_equal, |
125 | gtk_css_value_string_transition, |
126 | NULL, |
127 | NULL, |
128 | gtk_css_value_string_print |
129 | }; |
130 | |
131 | static const GtkCssValueClass GTK_CSS_VALUE_IDENT = { |
132 | "GtkCssIdentValue" , |
133 | gtk_css_value_string_free, |
134 | gtk_css_value_string_compute, |
135 | gtk_css_value_string_equal, |
136 | gtk_css_value_string_transition, |
137 | NULL, |
138 | NULL, |
139 | gtk_css_value_ident_print |
140 | }; |
141 | |
142 | GtkCssValue * |
143 | _gtk_css_string_value_new (const char *string) |
144 | { |
145 | return _gtk_css_string_value_new_take (string: g_strdup (str: string)); |
146 | } |
147 | |
148 | GtkCssValue * |
149 | _gtk_css_string_value_new_take (char *string) |
150 | { |
151 | GtkCssValue *result; |
152 | |
153 | result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_STRING); |
154 | result->string = string; |
155 | result->is_computed = TRUE; |
156 | |
157 | return result; |
158 | } |
159 | |
160 | GtkCssValue * |
161 | _gtk_css_string_value_parse (GtkCssParser *parser) |
162 | { |
163 | char *s; |
164 | |
165 | g_return_val_if_fail (parser != NULL, NULL); |
166 | |
167 | s = gtk_css_parser_consume_string (self: parser); |
168 | if (s == NULL) |
169 | return NULL; |
170 | |
171 | return _gtk_css_string_value_new_take (string: s); |
172 | } |
173 | |
174 | const char * |
175 | _gtk_css_string_value_get (const GtkCssValue *value) |
176 | { |
177 | g_return_val_if_fail (value != NULL, NULL); |
178 | g_return_val_if_fail (value->class == >K_CSS_VALUE_STRING, NULL); |
179 | |
180 | return value->string; |
181 | } |
182 | |
183 | GtkCssValue * |
184 | _gtk_css_ident_value_new (const char *ident) |
185 | { |
186 | return _gtk_css_ident_value_new_take (ident: g_strdup (str: ident)); |
187 | } |
188 | |
189 | GtkCssValue * |
190 | _gtk_css_ident_value_new_take (char *ident) |
191 | { |
192 | GtkCssValue *result; |
193 | |
194 | result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_IDENT); |
195 | result->string = ident; |
196 | result->is_computed = TRUE; |
197 | |
198 | return result; |
199 | } |
200 | |
201 | GtkCssValue * |
202 | _gtk_css_ident_value_try_parse (GtkCssParser *parser) |
203 | { |
204 | char *ident; |
205 | |
206 | g_return_val_if_fail (parser != NULL, NULL); |
207 | |
208 | if (!gtk_css_parser_has_token (self: parser, token_type: GTK_CSS_TOKEN_IDENT)) |
209 | return NULL; |
210 | |
211 | ident = gtk_css_parser_consume_ident (self: parser); |
212 | if (ident == NULL) |
213 | { |
214 | g_assert_not_reached (); |
215 | } |
216 | |
217 | return _gtk_css_ident_value_new_take (ident); |
218 | } |
219 | |
220 | const char * |
221 | _gtk_css_ident_value_get (const GtkCssValue *value) |
222 | { |
223 | g_return_val_if_fail (value != NULL, NULL); |
224 | g_return_val_if_fail (value->class == >K_CSS_VALUE_IDENT, NULL); |
225 | |
226 | return value->string; |
227 | } |
228 | |
229 | |