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_type (const GtkCssMatcher *matcher,
68 const GtkWidgetPath *siblings;
70 siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
71 if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index))
72 return g_type_is_a (gtk_widget_path_iter_get_object_type (siblings, matcher->path.sibling_index), type);
74 return g_type_is_a (gtk_widget_path_iter_get_object_type (matcher->path.path, matcher->path.index), type);
78 gtk_css_matcher_widget_path_has_class (const GtkCssMatcher *matcher,
81 const GtkWidgetPath *siblings;
83 siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
84 if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index))
85 return gtk_widget_path_iter_has_qclass (siblings, matcher->path.sibling_index, class_name);
87 return gtk_widget_path_iter_has_qclass (matcher->path.path, matcher->path.index, class_name);
91 gtk_css_matcher_widget_path_has_id (const GtkCssMatcher *matcher,
94 const GtkWidgetPath *siblings;
96 siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
97 if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index))
98 return gtk_widget_path_iter_has_name (siblings, matcher->path.sibling_index, id);
100 return gtk_widget_path_iter_has_name (matcher->path.path, matcher->path.index, id);
104 gtk_css_matcher_widget_path_has_regions (const GtkCssMatcher *matcher)
106 const GtkWidgetPath *siblings;
110 siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
111 if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index))
112 regions = gtk_widget_path_iter_list_regions (siblings, matcher->path.sibling_index);
114 regions = gtk_widget_path_iter_list_regions (matcher->path.path, matcher->path.index);
115 result = regions != NULL;
116 g_slist_free (regions);
122 gtk_css_matcher_widget_path_has_region (const GtkCssMatcher *matcher,
124 GtkRegionFlags flags)
126 const GtkWidgetPath *siblings;
127 GtkRegionFlags region_flags;
129 siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
130 if (siblings && matcher->path.sibling_index != gtk_widget_path_iter_get_sibling_index (matcher->path.path, matcher->path.index))
132 if (!gtk_widget_path_iter_has_region (siblings, matcher->path.sibling_index, region, ®ion_flags))
137 if (!gtk_widget_path_iter_has_region (matcher->path.path, matcher->path.index, region, ®ion_flags))
141 if ((flags & region_flags) != flags)
148 gtk_css_matcher_widget_path_has_position (const GtkCssMatcher *matcher,
153 const GtkWidgetPath *siblings;
156 siblings = gtk_widget_path_iter_get_siblings (matcher->path.path, matcher->path.index);
161 x = matcher->path.sibling_index + 1;
163 x = gtk_widget_path_length (siblings) - matcher->path.sibling_index;
176 static const GtkCssMatcherClass GTK_CSS_MATCHER_WIDGET_PATH = {
177 gtk_css_matcher_widget_path_get_parent,
178 gtk_css_matcher_widget_path_get_previous,
179 gtk_css_matcher_widget_path_get_state,
180 gtk_css_matcher_widget_path_has_type,
181 gtk_css_matcher_widget_path_has_class,
182 gtk_css_matcher_widget_path_has_id,
183 gtk_css_matcher_widget_path_has_regions,
184 gtk_css_matcher_widget_path_has_region,
185 gtk_css_matcher_widget_path_has_position,
190 _gtk_css_matcher_init (GtkCssMatcher *matcher,
191 const GtkWidgetPath *path,
194 if (gtk_widget_path_length (path) == 0)
197 matcher->path.klass = >K_CSS_MATCHER_WIDGET_PATH;
198 matcher->path.path = path;
199 matcher->path.state_flags = state;
200 matcher->path.index = gtk_widget_path_length (path) - 1;
201 matcher->path.sibling_index = gtk_widget_path_iter_get_sibling_index (path, matcher->path.index);
206 /* GTK_CSS_MATCHER_WIDGET_ANY */
209 gtk_css_matcher_any_get_parent (GtkCssMatcher *matcher,
210 const GtkCssMatcher *child)
212 _gtk_css_matcher_any_init (matcher);
218 gtk_css_matcher_any_get_previous (GtkCssMatcher *matcher,
219 const GtkCssMatcher *next)
221 _gtk_css_matcher_any_init (matcher);
227 gtk_css_matcher_any_get_state (const GtkCssMatcher *matcher)
229 /* XXX: This gets tricky when we implement :not() */
231 return GTK_STATE_FLAG_ACTIVE | GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_SELECTED
232 | GTK_STATE_FLAG_INSENSITIVE | GTK_STATE_FLAG_INCONSISTENT
233 | GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_BACKDROP;
237 gtk_css_matcher_any_has_type (const GtkCssMatcher *matcher,
244 gtk_css_matcher_any_has_class (const GtkCssMatcher *matcher,
251 gtk_css_matcher_any_has_id (const GtkCssMatcher *matcher,
258 gtk_css_matcher_any_has_regions (const GtkCssMatcher *matcher)
264 gtk_css_matcher_any_has_region (const GtkCssMatcher *matcher,
266 GtkRegionFlags flags)
272 gtk_css_matcher_any_has_position (const GtkCssMatcher *matcher,
280 static const GtkCssMatcherClass GTK_CSS_MATCHER_ANY = {
281 gtk_css_matcher_any_get_parent,
282 gtk_css_matcher_any_get_previous,
283 gtk_css_matcher_any_get_state,
284 gtk_css_matcher_any_has_type,
285 gtk_css_matcher_any_has_class,
286 gtk_css_matcher_any_has_id,
287 gtk_css_matcher_any_has_regions,
288 gtk_css_matcher_any_has_region,
289 gtk_css_matcher_any_has_position,
294 _gtk_css_matcher_any_init (GtkCssMatcher *matcher)
296 matcher->klass = >K_CSS_MATCHER_ANY;
299 /* GTK_CSS_MATCHER_WIDGET_SUPERSET */
302 gtk_css_matcher_superset_get_parent (GtkCssMatcher *matcher,
303 const GtkCssMatcher *child)
305 _gtk_css_matcher_any_init (matcher);
311 gtk_css_matcher_superset_get_previous (GtkCssMatcher *matcher,
312 const GtkCssMatcher *next)
314 _gtk_css_matcher_any_init (matcher);
320 gtk_css_matcher_superset_get_state (const GtkCssMatcher *matcher)
322 /* XXX: This gets tricky when we implement :not() */
324 if (matcher->superset.relevant & GTK_CSS_CHANGE_STATE)
325 return _gtk_css_matcher_get_state (matcher->superset.subset);
327 return GTK_STATE_FLAG_ACTIVE | GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_SELECTED
328 | GTK_STATE_FLAG_INSENSITIVE | GTK_STATE_FLAG_INCONSISTENT
329 | GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_BACKDROP;
333 gtk_css_matcher_superset_has_type (const GtkCssMatcher *matcher,
336 if (matcher->superset.relevant & GTK_CSS_CHANGE_NAME)
337 return _gtk_css_matcher_has_type (matcher->superset.subset, type);
343 gtk_css_matcher_superset_has_class (const GtkCssMatcher *matcher,
346 if (matcher->superset.relevant & GTK_CSS_CHANGE_CLASS)
347 return _gtk_css_matcher_has_class (matcher->superset.subset, class_name);
353 gtk_css_matcher_superset_has_id (const GtkCssMatcher *matcher,
356 if (matcher->superset.relevant & GTK_CSS_CHANGE_NAME)
357 return _gtk_css_matcher_has_id (matcher->superset.subset, id);
363 gtk_css_matcher_superset_has_regions (const GtkCssMatcher *matcher)
365 if (matcher->superset.relevant & GTK_CSS_CHANGE_NAME)
366 return _gtk_css_matcher_has_regions (matcher->superset.subset);
372 gtk_css_matcher_superset_has_region (const GtkCssMatcher *matcher,
374 GtkRegionFlags flags)
376 if (matcher->superset.relevant & GTK_CSS_CHANGE_NAME)
378 if (matcher->superset.relevant & GTK_CSS_CHANGE_POSITION)
379 return _gtk_css_matcher_has_region (matcher->superset.subset, region, flags);
381 return _gtk_css_matcher_has_region (matcher->superset.subset, region, 0);
388 gtk_css_matcher_superset_has_position (const GtkCssMatcher *matcher,
393 if (matcher->superset.relevant & GTK_CSS_CHANGE_POSITION)
394 return _gtk_css_matcher_has_position (matcher->superset.subset, forward, a, b);
399 static const GtkCssMatcherClass GTK_CSS_MATCHER_SUPERSET = {
400 gtk_css_matcher_superset_get_parent,
401 gtk_css_matcher_superset_get_previous,
402 gtk_css_matcher_superset_get_state,
403 gtk_css_matcher_superset_has_type,
404 gtk_css_matcher_superset_has_class,
405 gtk_css_matcher_superset_has_id,
406 gtk_css_matcher_superset_has_regions,
407 gtk_css_matcher_superset_has_region,
408 gtk_css_matcher_superset_has_position,
413 _gtk_css_matcher_superset_init (GtkCssMatcher *matcher,
414 const GtkCssMatcher *subset,
415 GtkCssChange relevant)
417 g_return_if_fail (subset != NULL);
418 g_return_if_fail ((relevant & ~(GTK_CSS_CHANGE_CLASS | GTK_CSS_CHANGE_NAME | GTK_CSS_CHANGE_POSITION | GTK_CSS_CHANGE_STATE)) == 0);
420 matcher->superset.klass = >K_CSS_MATCHER_SUPERSET;
421 matcher->superset.subset = subset;
422 matcher->superset.relevant = relevant;