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
|
/* GTK - The GIMP Toolkit
* Copyright (C) 2011 Benjamin Otte <otte@gnome.org>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gtkcssselectorprivate.h"
#include "gtkstylecontextprivate.h"
struct _GtkCssSelector
{
GtkCssSelector * previous; /* link to next element in selector or NULL if last */
GtkCssCombinator combine; /* how to combine with the previous element */
const char * name; /* quarked name of element we match or NULL if any */
GType type; /* cache for type belonging to name - G_TYPE_INVALID if uncached */
GQuark * ids; /* 0-terminated list of required ids or NULL if none */
GQuark * classes; /* 0-terminated list of required classes or NULL if none */
GtkRegionFlags pseudo_classes; /* required pseudo classes */
GtkStateFlags state; /* required state flags (currently not checked when matching) */
};
GtkCssSelector *
_gtk_css_selector_new (GtkCssSelector *previous,
GtkCssCombinator combine,
const char * name,
GQuark * ids,
GQuark * classes,
GtkRegionFlags pseudo_classes,
GtkStateFlags state)
{
GtkCssSelector *selector;
selector = g_slice_new0 (GtkCssSelector);
selector->previous = previous;
selector->combine = combine;
selector->name = name ? g_quark_to_string (g_quark_from_string (name)) : NULL;
selector->type = !name || _gtk_style_context_check_region_name (name) ? G_TYPE_NONE : G_TYPE_INVALID;
selector->ids = ids;
selector->classes = classes;
selector->pseudo_classes = pseudo_classes;
selector->state = state;
return selector;
}
void
_gtk_css_selector_free (GtkCssSelector *selector)
{
g_return_if_fail (selector != NULL);
if (selector->previous)
_gtk_css_selector_free (selector->previous);
g_free (selector->ids);
g_free (selector->classes);
g_slice_free (GtkCssSelector, selector);
}
void
_gtk_css_selector_print (const GtkCssSelector *selector,
GString * str)
{
if (selector->previous)
{
_gtk_css_selector_print (selector->previous, str);
switch (selector->combine)
{
case GTK_CSS_COMBINE_DESCANDANT:
g_string_append (str, " ");
break;
case GTK_CSS_COMBINE_CHILD:
g_string_append (str, " > ");
break;
default:
g_assert_not_reached ();
}
}
if (selector->name)
g_string_append (str, selector->name);
else if (selector->ids == NULL &&
selector->classes == NULL &&
selector->pseudo_classes == 0 &&
selector->state == 0)
g_string_append (str, "*");
if (selector->ids)
{
GQuark *id;
for (id = selector->ids; *id != 0; id++)
{
g_string_append_c (str, '#');
g_string_append (str, g_quark_to_string (*id));
}
}
if (selector->classes)
{
GQuark *class;
for (class = selector->classes; *class != 0; class++)
{
g_string_append_c (str, '.');
g_string_append (str, g_quark_to_string (*class));
}
}
if (selector->pseudo_classes)
{
static const char * flag_names[] = {
"nth-child(even)",
"nth-child(odd)",
"first-child",
"last-child",
"only-child",
"sorted"
};
guint i;
for (i = 0; i < G_N_ELEMENTS (flag_names); i++)
{
if (selector->pseudo_classes & (1 << i))
{
g_string_append_c (str, ':');
g_string_append (str, flag_names[i]);
}
}
}
if (selector->state)
{
static const char * state_names[] = {
"active",
"hover",
"selected",
"insensitive",
"inconsistent",
"focus",
"backdrop"
};
guint i;
for (i = 0; i < G_N_ELEMENTS (state_names); i++)
{
if (selector->state & (1 << i))
{
g_string_append_c (str, ':');
g_string_append (str, state_names[i]);
}
}
}
}
char *
_gtk_css_selector_to_string (const GtkCssSelector *selector)
{
GString *string;
g_return_val_if_fail (selector != NULL, NULL);
string = g_string_new (NULL);
_gtk_css_selector_print (selector, string);
return g_string_free (string, FALSE);
}
static GtkRegionFlags
compute_region_flags_for_index (const GtkWidgetPath *path,
guint id)
{
const GtkWidgetPath *siblings;
guint sibling_id, n_siblings;
GtkRegionFlags flags;
siblings = gtk_widget_path_iter_get_siblings (path, id);
if (siblings == NULL)
return 0;
sibling_id = gtk_widget_path_iter_get_sibling_index (path, id);
n_siblings = gtk_widget_path_length (siblings);
flags = (sibling_id % 2) ? GTK_REGION_EVEN : GTK_REGION_ODD;
if (sibling_id == 0)
flags |= GTK_REGION_FIRST;
if (sibling_id + 1 == n_siblings)
flags |= GTK_REGION_LAST;
if (n_siblings == 1)
flags |= GTK_REGION_ONLY;
return flags;
}
static gboolean
gtk_css_selector_matches_type (const GtkCssSelector *selector,
const GtkWidgetPath *path,
guint id)
{
if (selector->pseudo_classes)
{
GtkRegionFlags flags = compute_region_flags_for_index (path, id);
if ((selector->pseudo_classes & flags) != selector->pseudo_classes)
return FALSE;
}
if (selector->name == NULL)
return TRUE;
if (selector->type == G_TYPE_NONE)
return FALSE;
/* ugh, assigning to a const variable */
if (selector->type == G_TYPE_INVALID)
((GtkCssSelector *) selector)->type = g_type_from_name (selector->name);
if (selector->type == G_TYPE_INVALID)
return FALSE;
return g_type_is_a (gtk_widget_path_iter_get_object_type (path, id), selector->type);
}
static gboolean
gtk_css_selector_matches_region (const GtkCssSelector *selector,
const GtkWidgetPath *path,
guint id,
const char * region)
{
GtkRegionFlags flags;
if (selector->name == NULL)
return TRUE;
if (selector->name != region)
return FALSE;
if (!gtk_widget_path_iter_has_region (path, id, region, &flags))
{
/* This function must be called with existing regions */
g_assert_not_reached ();
}
return (selector->pseudo_classes & flags) == selector->pseudo_classes;
}
static gboolean
gtk_css_selector_matches_rest (const GtkCssSelector *selector,
const GtkWidgetPath *path,
guint id)
{
if (selector->ids)
{
GQuark *name;
for (name = selector->ids; *name; name++)
{
if (!gtk_widget_path_iter_has_qname (path, id, *name))
return FALSE;
}
}
if (selector->classes)
{
GQuark *class;
for (class = selector->classes; *class; class++)
{
if (!gtk_widget_path_iter_has_qclass (path, id, *class))
return FALSE;
}
}
return TRUE;
}
static gboolean
gtk_css_selector_matches_previous (const GtkCssSelector *selector,
const GtkWidgetPath *path,
guint id,
GSList *regions);
static gboolean
gtk_css_selector_matches_from (const GtkCssSelector *selector,
const GtkWidgetPath *path,
guint id,
GSList *regions)
{
GSList *l;
if (!gtk_css_selector_matches_rest (selector, path, id))
return FALSE;
for (l = regions; l; l = l->next)
{
const char *region = l->data;
if (gtk_css_selector_matches_region (selector, path, id, region))
{
GSList *remaining;
gboolean match;
remaining = g_slist_copy (regions);
remaining = g_slist_remove (remaining, region);
match = gtk_css_selector_matches_previous (selector,
path,
id,
remaining);
g_slist_free (remaining);
if (match)
return TRUE;
}
}
if (gtk_css_selector_matches_type (selector, path, id))
{
GSList *regions;
gboolean match;
if (id <= 0)
return selector->previous == NULL;
regions = gtk_widget_path_iter_list_regions (path, id - 1);
match = gtk_css_selector_matches_previous (selector,
path,
id - 1,
regions);
g_slist_free (regions);
return match;
}
return FALSE;
}
static gboolean
gtk_css_selector_matches_previous (const GtkCssSelector *selector,
const GtkWidgetPath *path,
guint id,
GSList *regions)
{
if (!selector->previous)
return TRUE;
if (gtk_css_selector_matches_from (selector->previous,
path,
id,
regions))
return TRUE;
if (selector->combine == GTK_CSS_COMBINE_DESCANDANT)
{
/* with this magic we run the loop while id >= 0 */
while (id-- != 0)
{
GSList *list;
gboolean match;
list = gtk_widget_path_iter_list_regions (path, id);
match = gtk_css_selector_matches_from (selector->previous,
path,
id,
list);
g_slist_free (list);
if (match)
return TRUE;
}
}
return FALSE;
}
/**
* _gtk_css_selector_matches:
* @selector: the selector
* @path: the path to check
* @state: The state to match
*
* Checks if the @selector matches the given @path. If @length is
* smaller than the number of elements in @path, it is assumed that
* only the first @length element of @path are valid and the rest
* does not exist. This is useful for doing parent matches for the
* 'inherit' keyword.
*
* Returns: %TRUE if the selector matches @path
**/
gboolean
_gtk_css_selector_matches (const GtkCssSelector *selector,
const GtkWidgetPath *path,
GtkStateFlags state)
{
GSList *list;
gboolean match;
guint length;
g_return_val_if_fail (selector != NULL, FALSE);
g_return_val_if_fail (path != NULL, FALSE);
if ((selector->state & state) != selector->state)
return FALSE;
length = gtk_widget_path_length (path);
if (length == 0)
return FALSE;
list = gtk_widget_path_iter_list_regions (path, length - 1);
match = gtk_css_selector_matches_from (selector,
path,
length - 1,
list);
g_slist_free (list);
return match;
}
static guint
count_bits (guint v)
{
/* http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel */
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
return (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
}
/* Computes specificity according to CSS 2.1.
* The arguments must be initialized to 0 */
static void
_gtk_css_selector_get_specificity (const GtkCssSelector *selector,
guint *ids,
guint *classes,
guint *elements)
{
GQuark *count;
if (selector->previous)
_gtk_css_selector_get_specificity (selector->previous, ids, classes, elements);
if (selector->ids)
for (count = selector->ids; *count; count++)
(*ids)++;
if (selector->classes)
for (count = selector->classes; *count; count++)
(*classes)++;
*classes += count_bits (selector->state) + count_bits (selector->pseudo_classes);
if (selector->name)
(*elements)++;
}
int
_gtk_css_selector_compare (const GtkCssSelector *a,
const GtkCssSelector *b)
{
guint a_ids = 0, a_classes = 0, a_elements = 0;
guint b_ids = 0, b_classes = 0, b_elements = 0;
int compare;
_gtk_css_selector_get_specificity (a, &a_ids, &a_classes, &a_elements);
_gtk_css_selector_get_specificity (b, &b_ids, &b_classes, &b_elements);
compare = a_ids - b_ids;
if (compare)
return compare;
compare = a_classes - b_classes;
if (compare)
return compare;
return a_elements - b_elements;
}
GtkStateFlags
_gtk_css_selector_get_state_flags (GtkCssSelector *selector)
{
g_return_val_if_fail (selector != NULL, 0);
return selector->state;
}
|