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,
191 _gtk_css_matcher_init (GtkCssMatcher *matcher,
192 const GtkWidgetPath *path,
195 if (gtk_widget_path_length (path) == 0)
198 matcher->path.klass = >K_CSS_MATCHER_WIDGET_PATH;
199 matcher->path.path = path;
200 matcher->path.state_flags = state;
201 matcher->path.index = gtk_widget_path_length (path) - 1;
202 matcher->path.sibling_index = gtk_widget_path_iter_get_sibling_index (path, matcher->path.index);
207 /* GTK_CSS_MATCHER_WIDGET_ANY */
210 gtk_css_matcher_any_get_parent (GtkCssMatcher *matcher,
211 const GtkCssMatcher *child)
213 _gtk_css_matcher_any_init (matcher);
219 gtk_css_matcher_any_get_previous (GtkCssMatcher *matcher,
220 const GtkCssMatcher *next)
222 _gtk_css_matcher_any_init (matcher);
228 gtk_css_matcher_any_get_state (const GtkCssMatcher *matcher)
230 /* XXX: This gets tricky when we implement :not() */
232 return GTK_STATE_FLAG_ACTIVE | GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_SELECTED
233 | GTK_STATE_FLAG_INSENSITIVE | GTK_STATE_FLAG_INCONSISTENT
234 | GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_BACKDROP;
238 gtk_css_matcher_any_has_name (const GtkCssMatcher *matcher,
245 gtk_css_matcher_any_has_class (const GtkCssMatcher *matcher,
252 gtk_css_matcher_any_has_id (const GtkCssMatcher *matcher,
259 gtk_css_matcher_any_has_regions (const GtkCssMatcher *matcher)
265 gtk_css_matcher_any_has_region (const GtkCssMatcher *matcher,
267 GtkRegionFlags flags)
273 gtk_css_matcher_any_has_position (const GtkCssMatcher *matcher,
281 static const GtkCssMatcherClass GTK_CSS_MATCHER_ANY = {
282 gtk_css_matcher_any_get_parent,
283 gtk_css_matcher_any_get_previous,
284 gtk_css_matcher_any_get_state,
285 gtk_css_matcher_any_has_name,
286 gtk_css_matcher_any_has_class,
287 gtk_css_matcher_any_has_id,
288 gtk_css_matcher_any_has_regions,
289 gtk_css_matcher_any_has_region,
290 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_name (const GtkCssMatcher *matcher,
336 if (matcher->superset.relevant & GTK_CSS_CHANGE_NAME)
337 return _gtk_css_matcher_has_name (matcher->superset.subset, name);
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_name,
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,
412 _gtk_css_matcher_superset_init (GtkCssMatcher *matcher,
413 const GtkCssMatcher *subset,
414 GtkCssChange relevant)
416 g_return_if_fail (subset != NULL);
417 g_return_if_fail ((relevant & ~(GTK_CSS_CHANGE_CLASS | GTK_CSS_CHANGE_NAME | GTK_CSS_CHANGE_POSITION | GTK_CSS_CHANGE_STATE)) == 0);
419 matcher->superset.klass = >K_CSS_MATCHER_SUPERSET;
420 matcher->superset.subset = subset;
421 matcher->superset.relevant = relevant;