1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2012 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, see <http://www.gnu.org/licenses/>.
20 #include "gtkcssmatcherprivate.h"
22 #include "gtkwidgetpath.h"
24 /* GTK_CSS_MATCHER_WIDGET_PATH */
27 gtk_css_matcher_widget_path_get_parent (GtkCssMatcher *matcher,
28 const GtkCssMatcher *child)
30 if (child->path.index == 0)
33 matcher->path.klass = child->path.klass;
34 matcher->path.path = child->path.path;
35 matcher->path.state_flags = 0;
36 matcher->path.index = child->path.index - 1;
37 matcher->path.sibling_index = gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index);
43 gtk_css_matcher_widget_path_get_previous (GtkCssMatcher *matcher,
44 const GtkCssMatcher *next)
46 if (next->path.sibling_index == 0)
49 matcher->path.klass = next->path.klass;
50 matcher->path.path = next->path.path;
51 matcher->path.state_flags = 0;
52 matcher->path.index = next->path.index;
53 matcher->path.sibling_index = next->path.sibling_index - 1;
59 gtk_css_matcher_widget_path_get_state (const GtkCssMatcher *matcher)
61 return matcher->path.state_flags;
65 gtk_css_matcher_widget_path_has_name (const GtkCssMatcher *matcher,
68 const GtkWidgetPath *siblings;
71 type = g_type_from_name (name);
72 siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
73 if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index))
74 return g_type_is_a (gtk_widget_path_iter_get_object_type (siblings, matcher->path.sibling_index), type);
76 return g_type_is_a (gtk_widget_path_iter_get_object_type (matcher->path.path, matcher->path.index), type);
80 gtk_css_matcher_widget_path_has_class (const GtkCssMatcher *matcher,
83 const GtkWidgetPath *siblings;
85 siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
86 if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index))
87 return gtk_widget_path_iter_has_qclass (siblings, matcher->path.sibling_index, class_name);
89 return gtk_widget_path_iter_has_qclass (matcher->path.path, matcher->path.index, class_name);
93 gtk_css_matcher_widget_path_has_id (const GtkCssMatcher *matcher,
96 const GtkWidgetPath *siblings;
98 siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
99 if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index))
100 return gtk_widget_path_iter_has_name (siblings, matcher->path.sibling_index, id);
102 return gtk_widget_path_iter_has_name (matcher->path.path, matcher->path.index, id);
106 gtk_css_matcher_widget_path_has_regions (const GtkCssMatcher *matcher)
108 const GtkWidgetPath *siblings;
112 siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
113 if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index))
114 regions = gtk_widget_path_iter_list_regions (siblings, matcher->path.sibling_index);
116 regions = gtk_widget_path_iter_list_regions (matcher->path.path, matcher->path.index);
117 result = regions != NULL;
118 g_slist_free (regions);
124 gtk_css_matcher_widget_path_has_region (const GtkCssMatcher *matcher,
126 GtkRegionFlags flags)
128 const GtkWidgetPath *siblings;
129 GtkRegionFlags region_flags;
131 siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
132 if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index))
134 if (!gtk_widget_path_iter_has_region (siblings, matcher->path.sibling_index, region, ®ion_flags))
139 if (!gtk_widget_path_iter_has_region (matcher->path.path, matcher->path.index, region, ®ion_flags))
143 if ((flags & region_flags) != flags)
150 gtk_css_matcher_widget_path_has_position (const GtkCssMatcher *matcher,
155 const GtkWidgetPath *siblings;
158 siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
163 x = matcher->path.sibling_index + 1;
165 x = gtk_widget_path_length (siblings) - matcher->path.sibling_index;
178 static const GtkCssMatcherClass GTK_CSS_MATCHER_WIDGET_PATH = {
179 gtk_css_matcher_widget_path_get_parent,
180 gtk_css_matcher_widget_path_get_previous,
181 gtk_css_matcher_widget_path_get_state,
182 gtk_css_matcher_widget_path_has_name,
183 gtk_css_matcher_widget_path_has_class,
184 gtk_css_matcher_widget_path_has_id,
185 gtk_css_matcher_widget_path_has_regions,
186 gtk_css_matcher_widget_path_has_region,
187 gtk_css_matcher_widget_path_has_position,
192 _gtk_css_matcher_init (GtkCssMatcher *matcher,
193 const GtkWidgetPath *path,
196 if (gtk_widget_path_length (path) == 0)
199 matcher->path.klass = >K_CSS_MATCHER_WIDGET_PATH;
200 matcher->path.path = path;
201 matcher->path.state_flags = state;
202 matcher->path.index = gtk_widget_path_length (path) - 1;
203 matcher->path.sibling_index = gtk_widget_path_iter_get_sibling_index (path, matcher->path.index);
208 /* GTK_CSS_MATCHER_WIDGET_ANY */
211 gtk_css_matcher_any_get_parent (GtkCssMatcher *matcher,
212 const GtkCssMatcher *child)
214 _gtk_css_matcher_any_init (matcher);
220 gtk_css_matcher_any_get_previous (GtkCssMatcher *matcher,
221 const GtkCssMatcher *next)
223 _gtk_css_matcher_any_init (matcher);
229 gtk_css_matcher_any_get_state (const GtkCssMatcher *matcher)
231 /* XXX: This gets tricky when we implement :not() */
233 return GTK_STATE_FLAG_ACTIVE | GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_SELECTED
234 | GTK_STATE_FLAG_INSENSITIVE | GTK_STATE_FLAG_INCONSISTENT
235 | GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_BACKDROP;
239 gtk_css_matcher_any_has_name (const GtkCssMatcher *matcher,
246 gtk_css_matcher_any_has_class (const GtkCssMatcher *matcher,
253 gtk_css_matcher_any_has_id (const GtkCssMatcher *matcher,
260 gtk_css_matcher_any_has_regions (const GtkCssMatcher *matcher)
266 gtk_css_matcher_any_has_region (const GtkCssMatcher *matcher,
268 GtkRegionFlags flags)
274 gtk_css_matcher_any_has_position (const GtkCssMatcher *matcher,
282 static const GtkCssMatcherClass GTK_CSS_MATCHER_ANY = {
283 gtk_css_matcher_any_get_parent,
284 gtk_css_matcher_any_get_previous,
285 gtk_css_matcher_any_get_state,
286 gtk_css_matcher_any_has_name,
287 gtk_css_matcher_any_has_class,
288 gtk_css_matcher_any_has_id,
289 gtk_css_matcher_any_has_regions,
290 gtk_css_matcher_any_has_region,
291 gtk_css_matcher_any_has_position,
296 _gtk_css_matcher_any_init (GtkCssMatcher *matcher)
298 matcher->klass = >K_CSS_MATCHER_ANY;
301 /* GTK_CSS_MATCHER_WIDGET_SUPERSET */
304 gtk_css_matcher_superset_get_parent (GtkCssMatcher *matcher,
305 const GtkCssMatcher *child)
307 _gtk_css_matcher_any_init (matcher);
313 gtk_css_matcher_superset_get_previous (GtkCssMatcher *matcher,
314 const GtkCssMatcher *next)
316 _gtk_css_matcher_any_init (matcher);
322 gtk_css_matcher_superset_get_state (const GtkCssMatcher *matcher)
324 /* XXX: This gets tricky when we implement :not() */
326 if (matcher->superset.relevant & GTK_CSS_CHANGE_STATE)
327 return _gtk_css_matcher_get_state (matcher->superset.subset);
329 return GTK_STATE_FLAG_ACTIVE | GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_SELECTED
330 | GTK_STATE_FLAG_INSENSITIVE | GTK_STATE_FLAG_INCONSISTENT
331 | GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_BACKDROP;
335 gtk_css_matcher_superset_has_name (const GtkCssMatcher *matcher,
338 if (matcher->superset.relevant & GTK_CSS_CHANGE_NAME)
339 return _gtk_css_matcher_has_name (matcher->superset.subset, name);
345 gtk_css_matcher_superset_has_class (const GtkCssMatcher *matcher,
348 if (matcher->superset.relevant & GTK_CSS_CHANGE_CLASS)
349 return _gtk_css_matcher_has_class (matcher->superset.subset, class_name);
355 gtk_css_matcher_superset_has_id (const GtkCssMatcher *matcher,
358 if (matcher->superset.relevant & GTK_CSS_CHANGE_NAME)
359 return _gtk_css_matcher_has_id (matcher->superset.subset, id);
365 gtk_css_matcher_superset_has_regions (const GtkCssMatcher *matcher)
367 if (matcher->superset.relevant & GTK_CSS_CHANGE_NAME)
368 return _gtk_css_matcher_has_regions (matcher->superset.subset);
374 gtk_css_matcher_superset_has_region (const GtkCssMatcher *matcher,
376 GtkRegionFlags flags)
378 if (matcher->superset.relevant & GTK_CSS_CHANGE_NAME)
380 if (matcher->superset.relevant & GTK_CSS_CHANGE_POSITION)
381 return _gtk_css_matcher_has_region (matcher->superset.subset, region, flags);
383 return _gtk_css_matcher_has_region (matcher->superset.subset, region, 0);
390 gtk_css_matcher_superset_has_position (const GtkCssMatcher *matcher,
395 if (matcher->superset.relevant & GTK_CSS_CHANGE_POSITION)
396 return _gtk_css_matcher_has_position (matcher->superset.subset, forward, a, b);
401 static const GtkCssMatcherClass GTK_CSS_MATCHER_SUPERSET = {
402 gtk_css_matcher_superset_get_parent,
403 gtk_css_matcher_superset_get_previous,
404 gtk_css_matcher_superset_get_state,
405 gtk_css_matcher_superset_has_name,
406 gtk_css_matcher_superset_has_class,
407 gtk_css_matcher_superset_has_id,
408 gtk_css_matcher_superset_has_regions,
409 gtk_css_matcher_superset_has_region,
410 gtk_css_matcher_superset_has_position,
415 _gtk_css_matcher_superset_init (GtkCssMatcher *matcher,
416 const GtkCssMatcher *subset,
417 GtkCssChange relevant)
419 g_return_if_fail (subset != NULL);
420 g_return_if_fail ((relevant & ~(GTK_CSS_CHANGE_CLASS | GTK_CSS_CHANGE_NAME | GTK_CSS_CHANGE_POSITION | GTK_CSS_CHANGE_STATE)) == 0);
422 matcher->superset.klass = >K_CSS_MATCHER_SUPERSET;
423 matcher->superset.subset = subset;
424 matcher->superset.relevant = relevant;