1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
/* vim: set ts=8 sw=8 noexpandtab: */
/* The Gtk+ CSS Drawing Library.
* Copyright (C) 2008 Robert Staudinger
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <math.h>
#include <string.h>
#include <ccss/ccss-color-impl.h>
#include <ccss/ccss-property-impl.h>
#include "ccss-gtk-property.h"
static ccss_property_class_t const *
peek_property_class (void);
static ccss_property_class_t const *_fallback_property_class = NULL;
static bool
parse_gboolean (CRTerm const *values,
ccss_gtk_property_t *property)
{
char const *str;
bool ret;
g_return_val_if_fail (values && property, false);
ret = false;
switch (values->type) {
case TERM_IDENT:
case TERM_STRING:
ret = true;
str = cr_string_peek_raw_str (values->content.str);
if (0 == g_ascii_strcasecmp ("true", str))
property->content.gboolean_val = true;
else if (0 == g_ascii_strcasecmp ("false", str))
property->content.gboolean_val = false;
else
ret = false;
break;
default:
/* Shut up compiler. */
ret = false;
}
return ret;
}
static bool
parse_gchararray (CRTerm const *values,
ccss_gtk_property_t *property)
{
char const *str;
bool ret;
g_return_val_if_fail (values && property, false);
ret = false;
switch (values->type) {
case TERM_IDENT:
case TERM_STRING:
str = cr_string_peek_raw_str (values->content.str);
property->content.gchararray_val = g_strdup (str);
ret = true;
break;
default:
/* Shut up compiler. */
ret = false;
}
return ret;
}
static bool
parse_gfloat (CRTerm const *values,
ccss_gtk_property_t *property)
{
bool ret;
g_return_val_if_fail (values && property, false);
ret = false;
switch (values->type) {
case TERM_NUMBER:
property->content.gfloat_val = values->content.num->val;
ret = true;
break;
default:
/* Shut up compiler. */
ret = false;
}
return ret;
}
static bool
parse_gint (CRTerm const *values,
ccss_gtk_property_t *property)
{
bool ret;
g_return_val_if_fail (values && property, false);
ret = false;
switch (values->type) {
case TERM_NUMBER:
/* Be strict about fractionals. */
if (lround (values->content.num->val) == values->content.num->val) {
property->content.gint_val = (int) values->content.num->val;
ret = true;
}
break;
default:
/* Shut up compiler. */
ret = false;
}
return ret;
}
static bool
parse_guchar (CRTerm const *values,
ccss_gtk_property_t *property)
{
bool ret;
g_return_val_if_fail (values && property, false);
ret = false;
switch (values->type) {
case TERM_NUMBER:
/* Be strict about fractionals. */
if (lround (values->content.num->val) == values->content.num->val) {
if (values->content.num->val > G_MAXUINT8)
property->content.guchar_val = G_MAXUINT8;
else if (values->content.num->val < 0)
property->content.guchar_val = 0;
else
property->content.guchar_val = (guchar) values->content.num->val;
ret = true;
}
break;
default:
/* Shut up compiler. */
ret = false;
}
return ret;
}
static bool
parse_gdk_color (ccss_grammar_t const *grammar,
CRTerm const *values,
void *user_data,
ccss_gtk_property_t *property)
{
ccss_color_t color;
bool ret;
g_return_val_if_fail (values && property, false);
memset (&color, 0, sizeof (color));
ret = ccss_color_parse (&color, grammar, user_data, &values);
if (ret) {
property->content.gdkcolor_val.red = color.red * 65535;
property->content.gdkcolor_val.green = color.green * 65535;
property->content.gdkcolor_val.blue = color.blue * 65535;
}
return ret;
}
static bool
parse_gtk_border (CRTerm const *values,
ccss_gtk_property_t *property)
{
gint *border_ptr;
int i;
g_return_val_if_fail (values && property, false);
border_ptr = (gint *) &property->content.gtkborder_val;
for (i = 0; i < 4; i++) {
if (!values) {
break;
}
/* Be strict about fractionals. */
if (TERM_NUMBER == values->type &&
lround (values->content.num->val) == values->content.num->val) {
border_ptr[i] = (gint) values->content.num->val;
values = values->next;
}
}
return i == 4;
}
static bool
parse_gtk_relief_style (CRTerm const *values,
ccss_gtk_property_t *property)
{
char const *str;
bool ret;
g_return_val_if_fail (values && property, false);
ret = false;
switch (values->type) {
case TERM_IDENT:
case TERM_STRING:
ret = true;
str = cr_string_peek_raw_str (values->content.str);
if (0 == g_ascii_strcasecmp ("normal", str))
property->content.gtkreliefstyle_val = GTK_RELIEF_NORMAL;
else if (0 == g_ascii_strcasecmp ("half", str))
property->content.gtkreliefstyle_val = GTK_RELIEF_HALF;
else if (0 == g_ascii_strcasecmp ("none", str))
property->content.gtkreliefstyle_val = GTK_RELIEF_NONE;
else
ret = false;
break;
default:
/* Shut up compiler. */
ret = false;
}
return ret;
}
static bool
parse_gtk_requisition (CRTerm const *values,
ccss_gtk_property_t *property)
{
gint *border_ptr;
int i;
g_return_val_if_fail (values && property, false);
border_ptr = (gint *) &property->content.gtkrequisition_val;
for (i = 0; i < 2; i++) {
if (!values) {
break;
}
/* Be strict about fractionals. */
if (TERM_NUMBER == values->type &&
lround (values->content.num->val) == values->content.num->val) {
border_ptr[i] = (gint) values->content.num->val;
values = values->next;
}
}
return i == 2;
}
static bool
parse_gtk_shadow_type (CRTerm const *values,
ccss_gtk_property_t *property)
{
char const *str;
bool ret;
g_return_val_if_fail (values && property, false);
ret = false;
switch (values->type) {
case TERM_IDENT:
case TERM_STRING:
ret = true;
str = cr_string_peek_raw_str (values->content.str);
if (0 == g_ascii_strcasecmp ("none", str))
property->content.gtkshadowtype_val = GTK_SHADOW_NONE;
else if (0 == g_ascii_strcasecmp ("in", str))
property->content.gtkreliefstyle_val = GTK_SHADOW_IN;
else if (0 == g_ascii_strcasecmp ("out", str))
property->content.gtkreliefstyle_val = GTK_SHADOW_OUT;
else if (0 == g_ascii_strcasecmp ("etched-in", str))
property->content.gtkreliefstyle_val = GTK_SHADOW_ETCHED_IN;
else if (0 == g_ascii_strcasecmp ("etched-out", str))
property->content.gtkreliefstyle_val = GTK_SHADOW_ETCHED_OUT;
else
ret = false;
break;
default:
/* Shut up compiler. */
ret = false;
}
return ret;
}
static bool
parse_gtk_toolbar_space_style (CRTerm const *values,
ccss_gtk_property_t *property)
{
char const *str;
bool ret;
g_return_val_if_fail (values && property, false);
ret = false;
switch (values->type) {
case TERM_IDENT:
case TERM_STRING:
ret = true;
str = cr_string_peek_raw_str (values->content.str);
if (0 == g_ascii_strcasecmp ("empty", str))
property->content.gtktoolbarspacestyle_val = GTK_TOOLBAR_SPACE_EMPTY;
else if (0 == g_ascii_strcasecmp ("line", str))
property->content.gtktoolbarspacestyle_val = GTK_TOOLBAR_SPACE_LINE;
else
ret = false;
break;
default:
/* Shut up compiler. */
ret = false;
}
return ret;
}
static bool
parse_gtk_style_property (ccss_grammar_t const *grammar,
char const *name,
CRTerm const *values,
void *user_data,
ccss_gtk_property_t *property)
{
char **tokens;
char *class_name = NULL;
char *property_name = NULL;
GtkBuilder *builder = NULL;
GType type = 0;
GtkWidget *widget = NULL;
GParamSpec *pspec = NULL;
/* Parse property name. */
tokens = g_strsplit (name, "-", 2);
if (tokens && tokens[0]) {
class_name = tokens[0];
} else {
goto bail;
}
if (tokens && tokens[1]) {
property_name = tokens[1];
} else {
goto bail;
}
/* Introspect property type. */
builder = gtk_builder_new ();
type = gtk_builder_get_type_from_name (builder, class_name);
if (0 == type)
goto bail;
widget = GTK_WIDGET (g_object_new (type, NULL));
if (NULL == widget)
goto bail;
pspec = gtk_widget_class_find_style_property (GTK_WIDGET_GET_CLASS (widget),
property_name);
if (NULL == pspec)
goto bail;
/* Parse value. */
property->gtype = 0;
if (G_TYPE_BOOLEAN == pspec->value_type) {
if (parse_gboolean (values, property)) {
property->gtype = pspec->value_type;
}
} else if (G_TYPE_STRING == pspec->value_type) {
if (parse_gchararray (values, property)) {
property->gtype = pspec->value_type;
}
} else if (G_TYPE_FLOAT == pspec->value_type) {
if (parse_gfloat (values, property)) {
property->gtype = pspec->value_type;
}
} else if (G_TYPE_INT == pspec->value_type) {
if (parse_gint (values, property)) {
property->gtype = pspec->value_type;
}
} else if (G_TYPE_UCHAR == pspec->value_type) {
if (parse_guchar (values, property)) {
property->gtype = pspec->value_type;
}
} else if (GDK_TYPE_COLOR == pspec->value_type) {
if (parse_gdk_color (grammar, values, user_data, property)) {
property->gtype = pspec->value_type;
}
} else if (GTK_TYPE_BORDER == pspec->value_type) {
if (parse_gtk_border (values, property)) {
property->gtype = pspec->value_type;
}
} else if (GTK_TYPE_RELIEF_STYLE == pspec->value_type) {
if (parse_gtk_relief_style (values, property)) {
property->gtype = pspec->value_type;
}
} else if (GTK_TYPE_REQUISITION == pspec->value_type) {
if (parse_gtk_requisition (values, property)) {
property->gtype = pspec->value_type;
}
} else if (GTK_TYPE_SHADOW_TYPE == pspec->value_type) {
if (parse_gtk_shadow_type (values, property)) {
property->gtype = pspec->value_type;
}
} else if (GTK_TYPE_TOOLBAR_SPACE_STYLE == pspec->value_type) {
if (parse_gtk_toolbar_space_style (values, property)) {
property->gtype = pspec->value_type;
}
} else {
g_assert_not_reached ();
}
if (property->gtype) {
property->class_name = class_name, class_name = NULL;
property->property_name = property_name, property_name = NULL;
}
bail:
if (tokens)
g_free (tokens);
if (class_name)
g_free (class_name);
if (property_name)
g_free (property_name);
if (builder)
g_object_unref (G_OBJECT (builder));
if (widget)
gtk_widget_destroy (widget);
return (bool) property->gtype;
}
static void
property_destroy (ccss_gtk_property_t *self)
{
g_return_if_fail (self);
if (G_TYPE_STRING == self->gtype && self->content.gchararray_val) {
g_free (self->content.gchararray_val);
}
g_free (self);
}
static bool
property_convert (ccss_gtk_property_t const *self,
ccss_property_type_t target,
void *value)
{
return false;
}
static bool
property_factory (ccss_grammar_t const *grammar,
ccss_block_t *block,
char const *name,
CRTerm const *values,
void *user_data)
{
ccss_gtk_property_t s, *self;
g_return_val_if_fail (grammar, false);
g_return_val_if_fail (block, false);
g_return_val_if_fail (name, false);
g_return_val_if_fail (values, false);
/* PONDERING: Handle things like GtkSettings `gtk-button-images' in the future? */
memset (&s, 0, sizeof (s));
ccss_property_init (&s.base, peek_property_class ());
self = NULL;
if (0 == g_strcmp0 (name, "xthickness")) {
if (parse_gint (values, &s)) {
s.class_name = NULL;
s.property_name = g_strdup ("xthickness");
self = g_new0 (ccss_gtk_property_t, 1);
*self = s;
ccss_block_add_property (block, "xthickness", &self->base);
}
return (bool) self;
} else if (0 == g_strcmp0 (name, "ythickness")) {
if (parse_gint (values, &s)) {
s.class_name = NULL;
s.property_name = g_strdup ("ythickness");
self = g_new0 (ccss_gtk_property_t, 1);
*self = s;
ccss_block_add_property (block, "ythickness", &self->base);
}
return (bool) self;
} else if (g_str_has_prefix (name, "Gtk")) {
if (parse_gtk_style_property (grammar, name, values, user_data, &s)) {
self = g_new0 (ccss_gtk_property_t, 1);
*self = s;
ccss_block_add_property (block, name, &self->base);
}
return (bool) self;
} else if (g_ascii_isupper (name[0])) {
// FIXME introspect style property and feed back to gtk directly.
// Non-gtk style properties (wnck, nautilus, ...) can't be
// resolved offline because css2gtkrc doesn't link against them.
// May cause some breakage, let's see how it goes.
}
/* Fall back. */
g_return_val_if_fail (_fallback_property_class, false);
g_return_val_if_fail (_fallback_property_class->factory, false);
return _fallback_property_class->factory (grammar, block, name,
values, user_data);
}
static ccss_property_class_t const _properties[] = {
{
.name = "*",
.create = NULL,
.destroy = (ccss_property_destroy_f) property_destroy,
.convert = (ccss_property_convert_f) property_convert,
.factory = property_factory,
.inherit = NULL,
.serialize = NULL
}, {
.name = NULL
}
};
static ccss_property_class_t const *
peek_property_class (void)
{
return &_properties[0];
}
ccss_property_class_t const *
ccss_gtk_property_get_property_classes (void)
{
return _properties;
}
void
ccss_gtk_property_set_fallback_class (ccss_property_class_t const *property_class)
{
g_return_if_fail (property_class);
_fallback_property_class = property_class;
}
bool
ccss_gtk_property_is_style_property (ccss_property_t const *self)
{
return self->vtable->destroy ==
(ccss_property_destroy_f) property_destroy;
}
|