1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2011 Benjamin Otte <otte@gnome.org>
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtkcssselectorprivate.h"
24 #include "gtkstylecontextprivate.h"
26 struct _GtkCssSelector
28 GtkCssSelector * previous; /* link to next element in selector or NULL if last */
29 GtkCssCombinator combine; /* how to combine with the previous element */
30 const char * name; /* quarked name of element we match or NULL if any */
31 GType type; /* cache for type belonging to name - G_TYPE_INVALID if uncached */
32 GQuark * ids; /* 0-terminated list of required ids or NULL if none */
33 GQuark * classes; /* 0-terminated list of required classes or NULL if none */
34 GtkRegionFlags pseudo_classes; /* required pseudo classes */
35 GtkStateFlags state; /* required state flags (currently not checked when matching) */
39 _gtk_css_selector_new (GtkCssSelector *previous,
40 GtkCssCombinator combine,
44 GtkRegionFlags pseudo_classes,
47 GtkCssSelector *selector;
49 selector = g_slice_new0 (GtkCssSelector);
50 selector->previous = previous;
51 selector->combine = combine;
52 selector->name = name ? g_quark_to_string (g_quark_from_string (name)) : NULL;
53 selector->type = !name || _gtk_style_context_check_region_name (name) ? G_TYPE_NONE : G_TYPE_INVALID;
55 selector->classes = classes;
56 selector->pseudo_classes = pseudo_classes;
57 selector->state = state;
63 _gtk_css_selector_free (GtkCssSelector *selector)
65 g_return_if_fail (selector != NULL);
67 if (selector->previous)
68 _gtk_css_selector_free (selector->previous);
70 g_free (selector->ids);
71 g_free (selector->classes);
73 g_slice_free (GtkCssSelector, selector);
77 _gtk_css_selector_print (const GtkCssSelector *selector,
80 if (selector->previous)
82 _gtk_css_selector_print (selector->previous, str);
83 switch (selector->combine)
85 case GTK_CSS_COMBINE_DESCANDANT:
86 g_string_append (str, " ");
88 case GTK_CSS_COMBINE_CHILD:
89 g_string_append (str, " > ");
92 g_assert_not_reached ();
97 g_string_append (str, selector->name);
98 else if (selector->ids == NULL &&
99 selector->classes == NULL &&
100 selector->pseudo_classes == 0 &&
101 selector->state == 0)
102 g_string_append (str, "*");
108 for (id = selector->ids; *id != 0; id++)
110 g_string_append_c (str, '#');
111 g_string_append (str, g_quark_to_string (*id));
115 if (selector->classes)
119 for (class = selector->classes; *class != 0; class++)
121 g_string_append_c (str, '.');
122 g_string_append (str, g_quark_to_string (*class));
126 if (selector->pseudo_classes)
128 static const char * flag_names[] = {
137 for (i = 0; i < G_N_ELEMENTS (flag_names); i++)
139 if (selector->pseudo_classes & (1 << i))
141 g_string_append_c (str, ':');
142 g_string_append (str, flag_names[i]);
149 static const char * state_names[] = {
159 for (i = 0; i < G_N_ELEMENTS (state_names); i++)
161 if (selector->state & (1 << i))
163 g_string_append_c (str, ':');
164 g_string_append (str, state_names[i]);
171 _gtk_css_selector_to_string (const GtkCssSelector *selector)
175 g_return_val_if_fail (selector != NULL, NULL);
177 string = g_string_new (NULL);
179 _gtk_css_selector_print (selector, string);
181 return g_string_free (string, FALSE);
185 gtk_css_selector_matches_type (const GtkCssSelector *selector,
186 /* const */ GtkWidgetPath *path,
189 if (selector->name == NULL)
192 if (selector->pseudo_classes)
195 if (selector->type == G_TYPE_NONE)
198 /* ugh, assigning to a const variable */
199 if (selector->type == G_TYPE_INVALID)
200 ((GtkCssSelector *) selector)->type = g_type_from_name (selector->name);
202 if (selector->type == G_TYPE_INVALID)
205 return g_type_is_a (gtk_widget_path_iter_get_object_type (path, id), selector->type);
209 gtk_css_selector_matches_region (const GtkCssSelector *selector,
210 /* const */ GtkWidgetPath *path,
214 GtkRegionFlags flags;
216 if (selector->name == NULL)
219 if (selector->name != region)
222 if (!gtk_widget_path_iter_has_region (path, id, region, &flags))
224 /* This function must be called with existing regions */
225 g_assert_not_reached ();
228 return (selector->pseudo_classes & flags) == selector->pseudo_classes;
232 gtk_css_selector_matches_rest (const GtkCssSelector *selector,
233 /* const */ GtkWidgetPath *path,
240 for (name = selector->ids; *name; name++)
242 if (!gtk_widget_path_iter_has_qname (path, id, *name))
247 if (selector->classes)
251 for (class = selector->classes; *class; class++)
253 if (!gtk_widget_path_iter_has_qclass (path, id, *class))
262 gtk_css_selector_matches_previous (const GtkCssSelector *selector,
263 /* const */ GtkWidgetPath *path,
268 gtk_css_selector_matches_from (const GtkCssSelector *selector,
269 /* const */ GtkWidgetPath *path,
275 if (!gtk_css_selector_matches_rest (selector, path, id))
278 for (l = regions; l; l = l->next)
280 const char *region = l->data;
282 if (gtk_css_selector_matches_region (selector, path, id, region))
287 remaining = g_slist_copy (regions);
288 remaining = g_slist_remove (remaining, region);
289 match = gtk_css_selector_matches_previous (selector,
293 g_slist_free (remaining);
299 if (gtk_css_selector_matches_type (selector, path, id))
305 return selector->previous == NULL;
307 regions = gtk_widget_path_iter_list_regions (path, id - 1);
308 match = gtk_css_selector_matches_previous (selector,
312 g_slist_free (regions);
320 gtk_css_selector_matches_previous (const GtkCssSelector *selector,
321 /* const */ GtkWidgetPath *path,
325 if (!selector->previous)
328 if (gtk_css_selector_matches_from (selector->previous,
334 if (selector->combine == GTK_CSS_COMBINE_DESCANDANT)
336 /* with this magic we run the loop while id >= 0 */
342 list = gtk_widget_path_iter_list_regions (path, id);
343 match = gtk_css_selector_matches_from (selector->previous,
357 _gtk_css_selector_matches (const GtkCssSelector *selector,
358 /* const */ GtkWidgetPath *path)
364 g_return_val_if_fail (selector != NULL, FALSE);
365 g_return_val_if_fail (path != NULL, FALSE);
367 length = gtk_widget_path_length (path);
371 list = gtk_widget_path_iter_list_regions (path, length - 1);
372 match = gtk_css_selector_matches_from (selector,
383 /* http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel */
384 v = v - ((v >> 1) & 0x55555555);
385 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
386 return (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
389 /* Computes specificity according to CSS 2.1.
390 * The arguments must be initialized to 0 */
392 _gtk_css_selector_get_specificity (const GtkCssSelector *selector,
399 if (selector->previous)
400 _gtk_css_selector_get_specificity (selector->previous, ids, classes, elements);
403 for (count = selector->ids; *count; count++)
406 if (selector->classes)
407 for (count = selector->classes; *count; count++)
410 *classes += count_bits (selector->state) + count_bits (selector->pseudo_classes);
417 _gtk_css_selector_compare (const GtkCssSelector *a,
418 const GtkCssSelector *b)
420 guint a_ids = 0, a_classes = 0, a_elements = 0;
421 guint b_ids = 0, b_classes = 0, b_elements = 0;
424 _gtk_css_selector_get_specificity (a, &a_ids, &a_classes, &a_elements);
425 _gtk_css_selector_get_specificity (b, &b_ids, &b_classes, &b_elements);
427 compare = a_ids - b_ids;
431 compare = a_classes - b_classes;
435 return a_elements - b_elements;
439 _gtk_css_selector_get_state_flags (GtkCssSelector *selector)
441 g_return_val_if_fail (selector != NULL, 0);
443 return selector->state;