1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 
18 #include "config.h"
19 
20 #include "gtkwidgetpath.h"
21 
22 #include <string.h>
23 
24 #include "gtkcssnodedeclarationprivate.h"
25 #include "gtkprivate.h"
26 #include "gtkstylecontextprivate.h"
27 #include "gtktypebuiltins.h"
28 #include "gtkwidget.h"
29 #include "gtkwidgetpathprivate.h"
30 
31 /**
32  * SECTION:gtkwidgetpath
33  * @Short_description: Widget path abstraction
34  * @Title: GtkWidgetPath
35  * @See_also: #GtkStyleContext
36  *
37  * GtkWidgetPath is a boxed type that represents a widget hierarchy from
38  * the topmost widget, typically a toplevel, to any child. This widget
39  * path abstraction is used in #GtkStyleContext on behalf of the real
40  * widget in order to query style information.
41  *
42  * If you are using GTK+ widgets, you probably will not need to use
43  * this API directly, as there is gtk_widget_get_path(), and the style
44  * context returned by gtk_widget_get_style_context() will be automatically
45  * updated on widget hierarchy changes.
46  *
47  * The widget path generation is generally simple:
48  *
49  * ## Defining a button within a window
50  *
51  * |[<!-- language="C" -->
52  * {
53  *   GtkWidgetPath *path;
54  *
55  *   path = gtk_widget_path_new ();
56  *   gtk_widget_path_append_type (path, GTK_TYPE_WINDOW);
57  *   gtk_widget_path_append_type (path, GTK_TYPE_BUTTON);
58  * }
59  * ]|
60  *
61  * Although more complex information, such as widget names, or
62  * different classes (property that may be used by other widget
63  * types) and intermediate regions may be included:
64  *
65  * ## Defining the first tab widget in a notebook
66  *
67  * |[<!-- language="C" -->
68  * {
69  *   GtkWidgetPath *path;
70  *   guint pos;
71  *
72  *   path = gtk_widget_path_new ();
73  *
74  *   pos = gtk_widget_path_append_type (path, GTK_TYPE_NOTEBOOK);
75  *   gtk_widget_path_iter_add_region (path, pos, "tab", GTK_REGION_EVEN | GTK_REGION_FIRST);
76  *
77  *   pos = gtk_widget_path_append_type (path, GTK_TYPE_LABEL);
78  *   gtk_widget_path_iter_set_name (path, pos, "first tab label");
79  * }
80  * ]|
81  *
82  * All this information will be used to match the style information
83  * that applies to the described widget.
84  **/
85 
86 G_DEFINE_BOXED_TYPE (GtkWidgetPath, gtk_widget_path,
87 		     gtk_widget_path_ref, gtk_widget_path_unref)
88 
89 
90 typedef struct GtkPathElement GtkPathElement;
91 
92 struct GtkPathElement
93 {
94   GtkCssNodeDeclaration *decl;
95   guint sibling_index;
96   GtkWidgetPath *siblings;
97 };
98 
99 struct _GtkWidgetPath
100 {
101   guint ref_count;
102 
103   GArray *elems; /* First element contains the described widget */
104 };
105 
106 /**
107  * gtk_widget_path_new:
108  *
109  * Returns an empty widget path.
110  *
111  * Returns: (transfer full): A newly created, empty, #GtkWidgetPath
112  *
113  * Since: 3.0
114  **/
115 GtkWidgetPath *
gtk_widget_path_new(void)116 gtk_widget_path_new (void)
117 {
118   GtkWidgetPath *path;
119 
120   path = g_slice_new0 (GtkWidgetPath);
121   path->elems = g_array_new (FALSE, TRUE, sizeof (GtkPathElement));
122   path->ref_count = 1;
123 
124   return path;
125 }
126 
127 static void
gtk_path_element_copy(GtkPathElement * dest,const GtkPathElement * src)128 gtk_path_element_copy (GtkPathElement       *dest,
129                        const GtkPathElement *src)
130 {
131   memset (dest, 0, sizeof (GtkPathElement));
132 
133   dest->decl = gtk_css_node_declaration_ref (src->decl);
134   if (src->siblings)
135     dest->siblings = gtk_widget_path_ref (src->siblings);
136   dest->sibling_index = src->sibling_index;
137 }
138 
139 /**
140  * gtk_widget_path_copy:
141  * @path: a #GtkWidgetPath
142  *
143  * Returns a copy of @path
144  *
145  * Returns: (transfer full): a copy of @path
146  *
147  * Since: 3.0
148  **/
149 GtkWidgetPath *
gtk_widget_path_copy(const GtkWidgetPath * path)150 gtk_widget_path_copy (const GtkWidgetPath *path)
151 {
152   GtkWidgetPath *new_path;
153   guint i;
154 
155   gtk_internal_return_val_if_fail (path != NULL, NULL);
156 
157   new_path = gtk_widget_path_new ();
158 
159   g_array_set_size (new_path->elems, path->elems->len);
160 
161   for (i = 0; i < path->elems->len; i++)
162     {
163       GtkPathElement *elem, *dest;
164 
165       elem = &g_array_index (path->elems, GtkPathElement, i);
166       dest = &g_array_index (new_path->elems, GtkPathElement, i);
167 
168       gtk_path_element_copy (dest, elem);
169     }
170 
171   return new_path;
172 }
173 
174 /**
175  * gtk_widget_path_ref:
176  * @path: a #GtkWidgetPath
177  *
178  * Increments the reference count on @path.
179  *
180  * Returns: @path itself.
181  *
182  * Since: 3.2
183  **/
184 GtkWidgetPath *
gtk_widget_path_ref(GtkWidgetPath * path)185 gtk_widget_path_ref (GtkWidgetPath *path)
186 {
187   gtk_internal_return_val_if_fail (path != NULL, path);
188 
189   path->ref_count += 1;
190 
191   return path;
192 }
193 
194 /**
195  * gtk_widget_path_unref:
196  * @path: a #GtkWidgetPath
197  *
198  * Decrements the reference count on @path, freeing the structure
199  * if the reference count reaches 0.
200  *
201  * Since: 3.2
202  **/
203 void
gtk_widget_path_unref(GtkWidgetPath * path)204 gtk_widget_path_unref (GtkWidgetPath *path)
205 {
206   guint i;
207 
208   gtk_internal_return_if_fail (path != NULL);
209 
210   path->ref_count -= 1;
211   if (path->ref_count > 0)
212     return;
213 
214   for (i = 0; i < path->elems->len; i++)
215     {
216       GtkPathElement *elem;
217 
218       elem = &g_array_index (path->elems, GtkPathElement, i);
219 
220       gtk_css_node_declaration_unref (elem->decl);
221       if (elem->siblings)
222         gtk_widget_path_unref (elem->siblings);
223     }
224 
225   g_array_free (path->elems, TRUE);
226   g_slice_free (GtkWidgetPath, path);
227 }
228 
229 /**
230  * gtk_widget_path_free:
231  * @path: a #GtkWidgetPath
232  *
233  * Decrements the reference count on @path, freeing the structure
234  * if the reference count reaches 0.
235  *
236  * Since: 3.0
237  **/
238 void
gtk_widget_path_free(GtkWidgetPath * path)239 gtk_widget_path_free (GtkWidgetPath *path)
240 {
241   gtk_internal_return_if_fail (path != NULL);
242 
243   gtk_widget_path_unref (path);
244 }
245 
246 /**
247  * gtk_widget_path_length:
248  * @path: a #GtkWidgetPath
249  *
250  * Returns the number of #GtkWidget #GTypes between the represented
251  * widget and its topmost container.
252  *
253  * Returns: the number of elements in the path
254  *
255  * Since: 3.0
256  **/
257 gint
gtk_widget_path_length(const GtkWidgetPath * path)258 gtk_widget_path_length (const GtkWidgetPath *path)
259 {
260   gtk_internal_return_val_if_fail (path != NULL, 0);
261 
262   return path->elems->len;
263 }
264 
265 /**
266  * gtk_widget_path_to_string:
267  * @path: the path
268  *
269  * Dumps the widget path into a string representation. It tries to match
270  * the CSS style as closely as possible (Note that there might be paths
271  * that cannot be represented in CSS).
272  *
273  * The main use of this code is for debugging purposes, so that you can
274  * g_print() the path or dump it in a gdb session.
275  *
276  * Returns: A new string describing @path.
277  *
278  * Since: 3.2
279  **/
280 char *
gtk_widget_path_to_string(const GtkWidgetPath * path)281 gtk_widget_path_to_string (const GtkWidgetPath *path)
282 {
283   GString *string;
284   guint i, j, n;
285 
286   gtk_internal_return_val_if_fail (path != NULL, NULL);
287 
288   string = g_string_new ("");
289 
290   for (i = 0; i < path->elems->len; i++)
291     {
292       GtkPathElement *elem;
293       GtkStateFlags state;
294       const GQuark *classes;
295       GList *list, *regions;
296 
297       elem = &g_array_index (path->elems, GtkPathElement, i);
298 
299       if (i > 0)
300         g_string_append_c (string, ' ');
301 
302       if (gtk_css_node_declaration_get_name (elem->decl))
303         g_string_append (string, gtk_css_node_declaration_get_name (elem->decl));
304       else
305         g_string_append (string, g_type_name (gtk_css_node_declaration_get_type (elem->decl)));
306 
307       if (gtk_css_node_declaration_get_id (elem->decl))
308         {
309           g_string_append_c (string, '(');
310           g_string_append (string, gtk_css_node_declaration_get_id (elem->decl));
311           g_string_append_c (string, ')');
312         }
313 
314       state = gtk_css_node_declaration_get_state (elem->decl);
315       if (state)
316         {
317           GFlagsClass *fclass;
318 
319           fclass = g_type_class_ref (GTK_TYPE_STATE_FLAGS);
320           for (j = 0; j < fclass->n_values; j++)
321             {
322               if (state & fclass->values[j].value)
323                 {
324                   g_string_append_c (string, ':');
325                   g_string_append (string, fclass->values[j].value_nick);
326                 }
327             }
328           g_type_class_unref (fclass);
329         }
330 
331       if (elem->siblings)
332         g_string_append_printf (string, "[%d/%d]",
333                                 elem->sibling_index + 1,
334                                 gtk_widget_path_length (elem->siblings));
335 
336       classes = gtk_css_node_declaration_get_classes (elem->decl, &n);
337       for (j = 0; j < n; j++)
338         {
339           g_string_append_c (string, '.');
340           g_string_append (string, g_quark_to_string (classes[j]));
341         }
342 
343       regions = gtk_css_node_declaration_list_regions (elem->decl);
344       for (list = regions; list; list = list->next)
345         {
346           static const char *flag_names[] = {
347             "even",
348             "odd",
349             "first",
350             "last",
351             "only",
352             "sorted"
353           };
354           GtkRegionFlags flags;
355           GQuark region = GPOINTER_TO_UINT (regions->data);
356 
357           gtk_css_node_declaration_has_region (elem->decl, region, &flags);
358           g_string_append_c (string, ' ');
359           g_string_append (string, g_quark_to_string (region));
360           for (j = 0; j < G_N_ELEMENTS(flag_names); j++)
361             {
362               if (flags & (1 << j))
363                 {
364                   g_string_append_c (string, ':');
365                   g_string_append (string, flag_names[j]);
366                 }
367             }
368         }
369       g_list_free (regions);
370     }
371 
372   return g_string_free (string, FALSE);
373 }
374 
375 /**
376  * gtk_widget_path_prepend_type:
377  * @path: a #GtkWidgetPath
378  * @type: widget type to prepend
379  *
380  * Prepends a widget type to the widget hierachy represented by @path.
381  *
382  * Since: 3.0
383  **/
384 void
gtk_widget_path_prepend_type(GtkWidgetPath * path,GType type)385 gtk_widget_path_prepend_type (GtkWidgetPath *path,
386                               GType          type)
387 {
388   GtkPathElement new = { NULL };
389 
390   gtk_internal_return_if_fail (path != NULL);
391 
392   new.decl = gtk_css_node_declaration_new ();
393   gtk_css_node_declaration_set_type (&new.decl, type);
394 
395   g_array_prepend_val (path->elems, new);
396 }
397 
398 /**
399  * gtk_widget_path_append_type:
400  * @path: a #GtkWidgetPath
401  * @type: widget type to append
402  *
403  * Appends a widget type to the widget hierarchy represented by @path.
404  *
405  * Returns: the position where the element was inserted
406  *
407  * Since: 3.0
408  **/
409 gint
gtk_widget_path_append_type(GtkWidgetPath * path,GType type)410 gtk_widget_path_append_type (GtkWidgetPath *path,
411                              GType          type)
412 {
413   GtkPathElement new = { NULL };
414 
415   gtk_internal_return_val_if_fail (path != NULL, 0);
416 
417   new.decl = gtk_css_node_declaration_new ();
418   gtk_css_node_declaration_set_type (&new.decl, type);
419   g_array_append_val (path->elems, new);
420 
421   return path->elems->len - 1;
422 }
423 
424 /**
425  * gtk_widget_path_append_with_siblings:
426  * @path: the widget path to append to
427  * @siblings: a widget path describing a list of siblings. This path
428  *   may not contain any siblings itself and it must not be modified
429  *   afterwards.
430  * @sibling_index: index into @siblings for where the added element is
431  *   positioned.
432  *
433  * Appends a widget type with all its siblings to the widget hierarchy
434  * represented by @path. Using this function instead of
435  * gtk_widget_path_append_type() will allow the CSS theming to use
436  * sibling matches in selectors and apply :nth-child() pseudo classes.
437  * In turn, it requires a lot more care in widget implementations as
438  * widgets need to make sure to call gtk_widget_reset_style() on all
439  * involved widgets when the @siblings path changes.
440  *
441  * Returns: the position where the element was inserted.
442  *
443  * Since: 3.2
444  **/
445 gint
gtk_widget_path_append_with_siblings(GtkWidgetPath * path,GtkWidgetPath * siblings,guint sibling_index)446 gtk_widget_path_append_with_siblings (GtkWidgetPath *path,
447                                       GtkWidgetPath *siblings,
448                                       guint          sibling_index)
449 {
450   GtkPathElement new;
451 
452   gtk_internal_return_val_if_fail (path != NULL, 0);
453   gtk_internal_return_val_if_fail (siblings != NULL, 0);
454   gtk_internal_return_val_if_fail (sibling_index < gtk_widget_path_length (siblings), 0);
455 
456   gtk_path_element_copy (&new, &g_array_index (siblings->elems, GtkPathElement, sibling_index));
457   new.siblings = gtk_widget_path_ref (siblings);
458   new.sibling_index = sibling_index;
459   g_array_append_val (path->elems, new);
460 
461   return path->elems->len - 1;
462 }
463 
464 /**
465  * gtk_widget_path_iter_get_siblings:
466  * @path: a #GtkWidgetPath
467  * @pos: position to get the siblings for, -1 for the path head
468  *
469  * Returns the list of siblings for the element at @pos. If the element
470  * was not added with siblings, %NULL is returned.
471  *
472  * Returns: %NULL or the list of siblings for the element at @pos.
473  **/
474 const GtkWidgetPath *
gtk_widget_path_iter_get_siblings(const GtkWidgetPath * path,gint pos)475 gtk_widget_path_iter_get_siblings (const GtkWidgetPath *path,
476                                    gint                 pos)
477 {
478   GtkPathElement *elem;
479 
480   gtk_internal_return_val_if_fail (path != NULL, NULL);
481   gtk_internal_return_val_if_fail (path->elems->len != 0, NULL);
482 
483   if (pos < 0 || pos >= path->elems->len)
484     pos = path->elems->len - 1;
485 
486   elem = &g_array_index (path->elems, GtkPathElement, pos);
487   return elem->siblings;
488 }
489 
490 /**
491  * gtk_widget_path_iter_get_sibling_index:
492  * @path: a #GtkWidgetPath
493  * @pos: position to get the sibling index for, -1 for the path head
494  *
495  * Returns the index into the list of siblings for the element at @pos as
496  * returned by gtk_widget_path_iter_get_siblings(). If that function would
497  * return %NULL because the element at @pos has no siblings, this function
498  * will return 0.
499  *
500  * Returns: 0 or the index into the list of siblings for the element at @pos.
501  **/
502 guint
gtk_widget_path_iter_get_sibling_index(const GtkWidgetPath * path,gint pos)503 gtk_widget_path_iter_get_sibling_index (const GtkWidgetPath *path,
504                                         gint                 pos)
505 {
506   GtkPathElement *elem;
507 
508   gtk_internal_return_val_if_fail (path != NULL, G_TYPE_INVALID);
509   gtk_internal_return_val_if_fail (path->elems->len != 0, G_TYPE_INVALID);
510 
511   if (pos < 0 || pos >= path->elems->len)
512     pos = path->elems->len - 1;
513 
514   elem = &g_array_index (path->elems, GtkPathElement, pos);
515   return elem->sibling_index;
516 }
517 
518 /**
519  * gtk_widget_path_iter_get_object_name:
520  * @path: a #GtkWidgetPath
521  * @pos: position to get the object name for, -1 for the path head
522  *
523  * Returns the object name that is at position @pos in the widget
524  * hierarchy defined in @path.
525  *
526  * Returns: (nullable): the name or %NULL
527  *
528  * Since: 3.20
529  **/
530 const char *
gtk_widget_path_iter_get_object_name(const GtkWidgetPath * path,gint pos)531 gtk_widget_path_iter_get_object_name (const GtkWidgetPath *path,
532                                       gint                 pos)
533 {
534   GtkPathElement *elem;
535 
536   gtk_internal_return_val_if_fail (path != NULL, NULL);
537   gtk_internal_return_val_if_fail (path->elems->len != 0, NULL);
538 
539   if (pos < 0 || pos >= path->elems->len)
540     pos = path->elems->len - 1;
541 
542   elem = &g_array_index (path->elems, GtkPathElement, pos);
543   return gtk_css_node_declaration_get_name (elem->decl);
544 }
545 
546 /**
547  * gtk_widget_path_iter_set_object_name:
548  * @path: a #GtkWidgetPath
549  * @pos: position to modify, -1 for the path head
550  * @name: (allow-none): object name to set or %NULL to unset
551  *
552  * Sets the object name for a given position in the widget hierarchy
553  * defined by @path.
554  *
555  * When set, the object name overrides the object type when matching
556  * CSS.
557  *
558  * Since: 3.20
559  **/
560 void
gtk_widget_path_iter_set_object_name(GtkWidgetPath * path,gint pos,const char * name)561 gtk_widget_path_iter_set_object_name (GtkWidgetPath *path,
562                                       gint           pos,
563                                       const char    *name)
564 {
565   GtkPathElement *elem;
566 
567   gtk_internal_return_if_fail (path != NULL);
568   gtk_internal_return_if_fail (path->elems->len != 0);
569 
570   if (pos < 0 || pos >= path->elems->len)
571     pos = path->elems->len - 1;
572 
573   elem = &g_array_index (path->elems, GtkPathElement, pos);
574   gtk_css_node_declaration_set_name (&elem->decl, g_intern_string (name));
575 }
576 
577 /**
578  * gtk_widget_path_iter_get_object_type:
579  * @path: a #GtkWidgetPath
580  * @pos: position to get the object type for, -1 for the path head
581  *
582  * Returns the object #GType that is at position @pos in the widget
583  * hierarchy defined in @path.
584  *
585  * Returns: a widget type
586  *
587  * Since: 3.0
588  **/
589 GType
gtk_widget_path_iter_get_object_type(const GtkWidgetPath * path,gint pos)590 gtk_widget_path_iter_get_object_type (const GtkWidgetPath *path,
591                                       gint                 pos)
592 {
593   GtkPathElement *elem;
594 
595   gtk_internal_return_val_if_fail (path != NULL, G_TYPE_INVALID);
596   gtk_internal_return_val_if_fail (path->elems->len != 0, G_TYPE_INVALID);
597 
598   if (pos < 0 || pos >= path->elems->len)
599     pos = path->elems->len - 1;
600 
601   elem = &g_array_index (path->elems, GtkPathElement, pos);
602   return gtk_css_node_declaration_get_type (elem->decl);
603 }
604 
605 /**
606  * gtk_widget_path_iter_set_object_type:
607  * @path: a #GtkWidgetPath
608  * @pos: position to modify, -1 for the path head
609  * @type: object type to set
610  *
611  * Sets the object type for a given position in the widget hierarchy
612  * defined by @path.
613  *
614  * Since: 3.0
615  **/
616 void
gtk_widget_path_iter_set_object_type(GtkWidgetPath * path,gint pos,GType type)617 gtk_widget_path_iter_set_object_type (GtkWidgetPath *path,
618                                       gint           pos,
619                                       GType          type)
620 {
621   GtkPathElement *elem;
622 
623   gtk_internal_return_if_fail (path != NULL);
624   gtk_internal_return_if_fail (path->elems->len != 0);
625 
626   if (pos < 0 || pos >= path->elems->len)
627     pos = path->elems->len - 1;
628 
629   elem = &g_array_index (path->elems, GtkPathElement, pos);
630   gtk_css_node_declaration_set_type (&elem->decl, type);
631 }
632 
633 /**
634  * gtk_widget_path_iter_get_state:
635  * @path: a #GtkWidgetPath
636  * @pos: position to get the state for, -1 for the path head
637  *
638  * Returns the state flags corresponding to the widget found at
639  * the position @pos in the widget hierarchy defined by
640  * @path
641  *
642  * Returns: The state flags
643  *
644  * Since: 3.14
645  **/
646 GtkStateFlags
gtk_widget_path_iter_get_state(const GtkWidgetPath * path,gint pos)647 gtk_widget_path_iter_get_state (const GtkWidgetPath *path,
648                                 gint                 pos)
649 {
650   GtkPathElement *elem;
651 
652   gtk_internal_return_val_if_fail (path != NULL, 0);
653   gtk_internal_return_val_if_fail (path->elems->len != 0, 0);
654 
655   if (pos < 0 || pos >= path->elems->len)
656     pos = path->elems->len - 1;
657 
658   elem = &g_array_index (path->elems, GtkPathElement, pos);
659   return gtk_css_node_declaration_get_state (elem->decl);
660 }
661 
662 /**
663  * gtk_widget_path_iter_set_state:
664  * @path: a #GtkWidgetPath
665  * @pos: position to modify, -1 for the path head
666  * @state: state flags
667  *
668  * Sets the widget name for the widget found at position @pos
669  * in the widget hierarchy defined by @path.
670  *
671  * If you want to update just a single state flag, you need to do
672  * this manually, as this function updates all state flags.
673  *
674  * ## Setting a flag
675  *
676  * |[<!-- language="C" -->
677  * gtk_widget_path_iter_set_state (path, pos, gtk_widget_path_iter_get_state (path, pos) | flag);
678  * ]|
679  *
680  * ## Unsetting a flag
681  *
682  * |[<!-- language="C" -->
683  * gtk_widget_path_iter_set_state (path, pos, gtk_widget_path_iter_get_state (path, pos) & ~flag);
684  * ]|
685  *
686  *
687  * Since: 3.14
688  **/
689 void
gtk_widget_path_iter_set_state(GtkWidgetPath * path,gint pos,GtkStateFlags state)690 gtk_widget_path_iter_set_state (GtkWidgetPath *path,
691                                 gint           pos,
692                                 GtkStateFlags  state)
693 {
694   GtkPathElement *elem;
695 
696   gtk_internal_return_if_fail (path != NULL);
697   gtk_internal_return_if_fail (path->elems->len != 0);
698 
699   if (pos < 0 || pos >= path->elems->len)
700     pos = path->elems->len - 1;
701 
702   elem = &g_array_index (path->elems, GtkPathElement, pos);
703 
704   gtk_css_node_declaration_set_state (&elem->decl, state);
705 }
706 
707 /**
708  * gtk_widget_path_iter_get_name:
709  * @path: a #GtkWidgetPath
710  * @pos: position to get the widget name for, -1 for the path head
711  *
712  * Returns the name corresponding to the widget found at
713  * the position @pos in the widget hierarchy defined by
714  * @path
715  *
716  * Returns: (nullable): The widget name, or %NULL if none was set.
717  **/
718 const gchar *
gtk_widget_path_iter_get_name(const GtkWidgetPath * path,gint pos)719 gtk_widget_path_iter_get_name (const GtkWidgetPath *path,
720                                gint                 pos)
721 {
722   GtkPathElement *elem;
723 
724   gtk_internal_return_val_if_fail (path != NULL, NULL);
725   gtk_internal_return_val_if_fail (path->elems->len != 0, NULL);
726 
727   if (pos < 0 || pos >= path->elems->len)
728     pos = path->elems->len - 1;
729 
730   elem = &g_array_index (path->elems, GtkPathElement, pos);
731   return gtk_css_node_declaration_get_id (elem->decl);
732 }
733 
734 /**
735  * gtk_widget_path_iter_set_name:
736  * @path: a #GtkWidgetPath
737  * @pos: position to modify, -1 for the path head
738  * @name: widget name
739  *
740  * Sets the widget name for the widget found at position @pos
741  * in the widget hierarchy defined by @path.
742  *
743  * Since: 3.0
744  **/
745 void
gtk_widget_path_iter_set_name(GtkWidgetPath * path,gint pos,const gchar * name)746 gtk_widget_path_iter_set_name (GtkWidgetPath *path,
747                                gint           pos,
748                                const gchar   *name)
749 {
750   GtkPathElement *elem;
751 
752   gtk_internal_return_if_fail (path != NULL);
753   gtk_internal_return_if_fail (path->elems->len != 0);
754   gtk_internal_return_if_fail (name != NULL);
755 
756   if (pos < 0 || pos >= path->elems->len)
757     pos = path->elems->len - 1;
758 
759   elem = &g_array_index (path->elems, GtkPathElement, pos);
760 
761   gtk_css_node_declaration_set_id (&elem->decl, g_intern_string (name));
762 }
763 
764 /**
765  * gtk_widget_path_iter_has_qname:
766  * @path: a #GtkWidgetPath
767  * @pos: position to query, -1 for the path head
768  * @qname: widget name as a #GQuark
769  *
770  * See gtk_widget_path_iter_has_name(). This is a version
771  * that operates on #GQuarks.
772  *
773  * Returns: %TRUE if the widget at @pos has this name
774  *
775  * Since: 3.0
776  **/
777 gboolean
gtk_widget_path_iter_has_qname(const GtkWidgetPath * path,gint pos,GQuark qname)778 gtk_widget_path_iter_has_qname (const GtkWidgetPath *path,
779                                 gint                 pos,
780                                 GQuark               qname)
781 {
782   gtk_internal_return_val_if_fail (path != NULL, FALSE);
783   gtk_internal_return_val_if_fail (path->elems->len != 0, FALSE);
784   gtk_internal_return_val_if_fail (qname != 0, FALSE);
785 
786   return gtk_widget_path_iter_has_name (path, pos, g_quark_to_string (qname));
787 }
788 
789 /**
790  * gtk_widget_path_iter_has_name:
791  * @path: a #GtkWidgetPath
792  * @pos: position to query, -1 for the path head
793  * @name: a widget name
794  *
795  * Returns %TRUE if the widget at position @pos has the name @name,
796  * %FALSE otherwise.
797  *
798  * Returns: %TRUE if the widget at @pos has this name
799  *
800  * Since: 3.0
801  **/
802 gboolean
gtk_widget_path_iter_has_name(const GtkWidgetPath * path,gint pos,const gchar * name)803 gtk_widget_path_iter_has_name (const GtkWidgetPath *path,
804                                gint                 pos,
805                                const gchar         *name)
806 {
807   GtkPathElement *elem;
808 
809   gtk_internal_return_val_if_fail (path != NULL, FALSE);
810   gtk_internal_return_val_if_fail (path->elems->len != 0, FALSE);
811 
812   if (pos < 0 || pos >= path->elems->len)
813     pos = path->elems->len - 1;
814 
815   name = g_intern_string (name);
816   elem = &g_array_index (path->elems, GtkPathElement, pos);
817 
818   return gtk_css_node_declaration_get_id (elem->decl) == name;
819 }
820 
821 /**
822  * gtk_widget_path_iter_add_class:
823  * @path: a #GtkWidget
824  * @pos: position to modify, -1 for the path head
825  * @name: a class name
826  *
827  * Adds the class @name to the widget at position @pos in
828  * the hierarchy defined in @path. See
829  * gtk_style_context_add_class().
830  *
831  * Since: 3.0
832  **/
833 void
gtk_widget_path_iter_add_class(GtkWidgetPath * path,gint pos,const gchar * name)834 gtk_widget_path_iter_add_class (GtkWidgetPath *path,
835                                 gint           pos,
836                                 const gchar   *name)
837 {
838   gtk_internal_return_if_fail (path != NULL);
839   gtk_internal_return_if_fail (path->elems->len != 0);
840   gtk_internal_return_if_fail (name != NULL);
841 
842   if (pos < 0 || pos >= path->elems->len)
843     pos = path->elems->len - 1;
844 
845   gtk_widget_path_iter_add_qclass (path, pos, g_quark_from_string (name));
846 }
847 
848 void
gtk_widget_path_iter_add_qclass(GtkWidgetPath * path,gint pos,GQuark qname)849 gtk_widget_path_iter_add_qclass (GtkWidgetPath *path,
850                                  gint           pos,
851                                  GQuark         qname)
852 {
853   GtkPathElement *elem;
854 
855   elem = &g_array_index (path->elems, GtkPathElement, pos);
856 
857   gtk_css_node_declaration_add_class (&elem->decl, qname);
858 }
859 
860 /**
861  * gtk_widget_path_iter_remove_class:
862  * @path: a #GtkWidgetPath
863  * @pos: position to modify, -1 for the path head
864  * @name: class name
865  *
866  * Removes the class @name from the widget at position @pos in
867  * the hierarchy defined in @path.
868  *
869  * Since: 3.0
870  **/
871 void
gtk_widget_path_iter_remove_class(GtkWidgetPath * path,gint pos,const gchar * name)872 gtk_widget_path_iter_remove_class (GtkWidgetPath *path,
873                                    gint           pos,
874                                    const gchar   *name)
875 {
876   GtkPathElement *elem;
877   GQuark qname;
878 
879   gtk_internal_return_if_fail (path != NULL);
880   gtk_internal_return_if_fail (path->elems->len != 0);
881   gtk_internal_return_if_fail (name != NULL);
882 
883   if (pos < 0 || pos >= path->elems->len)
884     pos = path->elems->len - 1;
885 
886   elem = &g_array_index (path->elems, GtkPathElement, pos);
887   qname = g_quark_try_string (name);
888   if (qname == 0)
889     return;
890 
891   gtk_css_node_declaration_remove_class (&elem->decl, qname);
892 }
893 
894 /**
895  * gtk_widget_path_iter_clear_classes:
896  * @path: a #GtkWidget
897  * @pos: position to modify, -1 for the path head
898  *
899  * Removes all classes from the widget at position @pos in the
900  * hierarchy defined in @path.
901  *
902  * Since: 3.0
903  **/
904 void
gtk_widget_path_iter_clear_classes(GtkWidgetPath * path,gint pos)905 gtk_widget_path_iter_clear_classes (GtkWidgetPath *path,
906                                     gint           pos)
907 {
908   GtkPathElement *elem;
909 
910   gtk_internal_return_if_fail (path != NULL);
911   gtk_internal_return_if_fail (path->elems->len != 0);
912 
913   if (pos < 0 || pos >= path->elems->len)
914     pos = path->elems->len - 1;
915 
916   elem = &g_array_index (path->elems, GtkPathElement, pos);
917 
918   gtk_css_node_declaration_clear_classes (&elem->decl);
919 }
920 
921 /**
922  * gtk_widget_path_iter_list_classes:
923  * @path: a #GtkWidgetPath
924  * @pos: position to query, -1 for the path head
925  *
926  * Returns a list with all the class names defined for the widget
927  * at position @pos in the hierarchy defined in @path.
928  *
929  * Returns: (transfer container) (element-type utf8): The list of
930  *          classes, This is a list of strings, the #GSList contents
931  *          are owned by GTK+, but you should use g_slist_free() to
932  *          free the list itself.
933  *
934  * Since: 3.0
935  **/
936 GSList *
gtk_widget_path_iter_list_classes(const GtkWidgetPath * path,gint pos)937 gtk_widget_path_iter_list_classes (const GtkWidgetPath *path,
938                                    gint                 pos)
939 {
940   GtkPathElement *elem;
941   GSList *list = NULL;
942   const GQuark *classes;
943   guint i, n;
944 
945   gtk_internal_return_val_if_fail (path != NULL, NULL);
946   gtk_internal_return_val_if_fail (path->elems->len != 0, NULL);
947 
948   if (pos < 0 || pos >= path->elems->len)
949     pos = path->elems->len - 1;
950 
951   elem = &g_array_index (path->elems, GtkPathElement, pos);
952   classes = gtk_css_node_declaration_get_classes (elem->decl, &n);
953 
954   for (i = 0; i < n; i++)
955     {
956       list = g_slist_prepend (list, (gchar *) g_quark_to_string (classes[i]));
957     }
958 
959   return g_slist_reverse (list);
960 }
961 
962 /**
963  * gtk_widget_path_iter_has_qclass:
964  * @path: a #GtkWidgetPath
965  * @pos: position to query, -1 for the path head
966  * @qname: class name as a #GQuark
967  *
968  * See gtk_widget_path_iter_has_class(). This is a version that operates
969  * with GQuarks.
970  *
971  * Returns: %TRUE if the widget at @pos has the class defined.
972  *
973  * Since: 3.0
974  **/
975 gboolean
gtk_widget_path_iter_has_qclass(const GtkWidgetPath * path,gint pos,GQuark qname)976 gtk_widget_path_iter_has_qclass (const GtkWidgetPath *path,
977                                  gint                 pos,
978                                  GQuark               qname)
979 {
980   GtkPathElement *elem;
981 
982   gtk_internal_return_val_if_fail (path != NULL, FALSE);
983   gtk_internal_return_val_if_fail (path->elems->len != 0, FALSE);
984   gtk_internal_return_val_if_fail (qname != 0, FALSE);
985 
986   if (pos < 0 || pos >= path->elems->len)
987     pos = path->elems->len - 1;
988 
989   elem = &g_array_index (path->elems, GtkPathElement, pos);
990 
991   return gtk_css_node_declaration_has_class (elem->decl, qname);
992 }
993 
994 /**
995  * gtk_widget_path_iter_has_class:
996  * @path: a #GtkWidgetPath
997  * @pos: position to query, -1 for the path head
998  * @name: class name
999  *
1000  * Returns %TRUE if the widget at position @pos has the class @name
1001  * defined, %FALSE otherwise.
1002  *
1003  * Returns: %TRUE if the class @name is defined for the widget at @pos
1004  *
1005  * Since: 3.0
1006  **/
1007 gboolean
gtk_widget_path_iter_has_class(const GtkWidgetPath * path,gint pos,const gchar * name)1008 gtk_widget_path_iter_has_class (const GtkWidgetPath *path,
1009                                 gint                 pos,
1010                                 const gchar         *name)
1011 {
1012   GQuark qname;
1013 
1014   gtk_internal_return_val_if_fail (path != NULL, FALSE);
1015   gtk_internal_return_val_if_fail (path->elems->len != 0, FALSE);
1016   gtk_internal_return_val_if_fail (name != NULL, FALSE);
1017 
1018   if (pos < 0 || pos >= path->elems->len)
1019     pos = path->elems->len - 1;
1020 
1021   qname = g_quark_try_string (name);
1022 
1023   if (qname == 0)
1024     return FALSE;
1025 
1026   return gtk_widget_path_iter_has_qclass (path, pos, qname);
1027 }
1028 
1029 /**
1030  * gtk_widget_path_iter_add_region:
1031  * @path: a #GtkWidgetPath
1032  * @pos: position to modify, -1 for the path head
1033  * @name: region name
1034  * @flags: flags affecting the region
1035  *
1036  * Adds the region @name to the widget at position @pos in
1037  * the hierarchy defined in @path. See
1038  * gtk_style_context_add_region().
1039  *
1040  * Region names must only contain lowercase letters
1041  * and “-”, starting always with a lowercase letter.
1042  *
1043  * Since: 3.0
1044  *
1045  * Deprecated: 3.14: The use of regions is deprecated.
1046  **/
1047 void
gtk_widget_path_iter_add_region(GtkWidgetPath * path,gint pos,const gchar * name,GtkRegionFlags flags)1048 gtk_widget_path_iter_add_region (GtkWidgetPath  *path,
1049                                  gint            pos,
1050                                  const gchar    *name,
1051                                  GtkRegionFlags  flags)
1052 {
1053   GtkPathElement *elem;
1054   GQuark qname;
1055 
1056   gtk_internal_return_if_fail (path != NULL);
1057   gtk_internal_return_if_fail (path->elems->len != 0);
1058   gtk_internal_return_if_fail (name != NULL);
1059   gtk_internal_return_if_fail (_gtk_style_context_check_region_name (name));
1060 
1061   if (pos < 0 || pos >= path->elems->len)
1062     pos = path->elems->len - 1;
1063 
1064   elem = &g_array_index (path->elems, GtkPathElement, pos);
1065   qname = g_quark_from_string (name);
1066 
1067   gtk_css_node_declaration_add_region (&elem->decl, qname, flags);
1068 }
1069 
1070 /**
1071  * gtk_widget_path_iter_remove_region:
1072  * @path: a #GtkWidgetPath
1073  * @pos: position to modify, -1 for the path head
1074  * @name: region name
1075  *
1076  * Removes the region @name from the widget at position @pos in
1077  * the hierarchy defined in @path.
1078  *
1079  * Since: 3.0
1080  *
1081  * Deprecated: 3.14: The use of regions is deprecated.
1082  **/
1083 void
gtk_widget_path_iter_remove_region(GtkWidgetPath * path,gint pos,const gchar * name)1084 gtk_widget_path_iter_remove_region (GtkWidgetPath *path,
1085                                     gint           pos,
1086                                     const gchar   *name)
1087 {
1088   GtkPathElement *elem;
1089   GQuark qname;
1090 
1091   gtk_internal_return_if_fail (path != NULL);
1092   gtk_internal_return_if_fail (path->elems->len != 0);
1093   gtk_internal_return_if_fail (name != NULL);
1094 
1095   if (pos < 0 || pos >= path->elems->len)
1096     pos = path->elems->len - 1;
1097 
1098   elem = &g_array_index (path->elems, GtkPathElement, pos);
1099   qname = g_quark_try_string (name);
1100   if (qname == 0)
1101     return;
1102 
1103   gtk_css_node_declaration_remove_region (&elem->decl, qname);
1104 }
1105 
1106 /**
1107  * gtk_widget_path_iter_clear_regions:
1108  * @path: a #GtkWidgetPath
1109  * @pos: position to modify, -1 for the path head
1110  *
1111  * Removes all regions from the widget at position @pos in the
1112  * hierarchy defined in @path.
1113  *
1114  * Since: 3.0
1115  *
1116  * Deprecated: 3.14: The use of regions is deprecated.
1117  **/
1118 void
gtk_widget_path_iter_clear_regions(GtkWidgetPath * path,gint pos)1119 gtk_widget_path_iter_clear_regions (GtkWidgetPath *path,
1120                                     gint           pos)
1121 {
1122   GtkPathElement *elem;
1123 
1124   gtk_internal_return_if_fail (path != NULL);
1125   gtk_internal_return_if_fail (path->elems->len != 0);
1126 
1127   if (pos < 0 || pos >= path->elems->len)
1128     pos = path->elems->len - 1;
1129 
1130   elem = &g_array_index (path->elems, GtkPathElement, pos);
1131 
1132   gtk_css_node_declaration_clear_regions (&elem->decl);
1133 }
1134 
1135 /**
1136  * gtk_widget_path_iter_list_regions:
1137  * @path: a #GtkWidgetPath
1138  * @pos: position to query, -1 for the path head
1139  *
1140  * Returns a list with all the region names defined for the widget
1141  * at position @pos in the hierarchy defined in @path.
1142  *
1143  * Returns: (transfer container) (element-type utf8): The list of
1144  *          regions, This is a list of strings, the #GSList contents
1145  *          are owned by GTK+, but you should use g_slist_free() to
1146  *          free the list itself.
1147  *
1148  * Since: 3.0
1149  *
1150  * Deprecated: 3.14: The use of regions is deprecated.
1151  **/
1152 GSList *
gtk_widget_path_iter_list_regions(const GtkWidgetPath * path,gint pos)1153 gtk_widget_path_iter_list_regions (const GtkWidgetPath *path,
1154                                    gint                 pos)
1155 {
1156   GtkPathElement *elem;
1157   GSList *list = NULL;
1158   GList *l, *wrong_list;
1159 
1160   gtk_internal_return_val_if_fail (path != NULL, NULL);
1161   gtk_internal_return_val_if_fail (path->elems->len != 0, NULL);
1162 
1163   if (pos < 0 || pos >= path->elems->len)
1164     pos = path->elems->len - 1;
1165 
1166   elem = &g_array_index (path->elems, GtkPathElement, pos);
1167 
1168   wrong_list = gtk_css_node_declaration_list_regions (elem->decl);
1169   for (l = wrong_list; l; l = l->next)
1170     {
1171       list = g_slist_prepend (list, (char *) g_quark_to_string (GPOINTER_TO_UINT (l->data)));
1172     }
1173   g_list_free (wrong_list);
1174 
1175   return list;
1176 }
1177 
1178 /**
1179  * gtk_widget_path_iter_has_qregion:
1180  * @path: a #GtkWidgetPath
1181  * @pos: position to query, -1 for the path head
1182  * @qname: region name as a #GQuark
1183  * @flags: (out): return location for the region flags
1184  *
1185  * See gtk_widget_path_iter_has_region(). This is a version that operates
1186  * with GQuarks.
1187  *
1188  * Returns: %TRUE if the widget at @pos has the region defined.
1189  *
1190  * Since: 3.0
1191  *
1192  * Deprecated: 3.14: The use of regions is deprecated.
1193  **/
1194 gboolean
gtk_widget_path_iter_has_qregion(const GtkWidgetPath * path,gint pos,GQuark qname,GtkRegionFlags * flags)1195 gtk_widget_path_iter_has_qregion (const GtkWidgetPath *path,
1196                                   gint                 pos,
1197                                   GQuark               qname,
1198                                   GtkRegionFlags      *flags)
1199 {
1200   GtkPathElement *elem;
1201 
1202   gtk_internal_return_val_if_fail (path != NULL, FALSE);
1203   gtk_internal_return_val_if_fail (path->elems->len != 0, FALSE);
1204   gtk_internal_return_val_if_fail (qname != 0, FALSE);
1205 
1206   if (pos < 0 || pos >= path->elems->len)
1207     pos = path->elems->len - 1;
1208 
1209   elem = &g_array_index (path->elems, GtkPathElement, pos);
1210 
1211   return gtk_css_node_declaration_has_region (elem->decl, qname, flags);
1212 }
1213 
1214 /**
1215  * gtk_widget_path_iter_has_region:
1216  * @path: a #GtkWidgetPath
1217  * @pos: position to query, -1 for the path head
1218  * @name: region name
1219  * @flags: (out): return location for the region flags
1220  *
1221  * Returns %TRUE if the widget at position @pos has the class @name
1222  * defined, %FALSE otherwise.
1223  *
1224  * Returns: %TRUE if the class @name is defined for the widget at @pos
1225  *
1226  * Since: 3.0
1227  *
1228  * Deprecated: 3.14: The use of regions is deprecated.
1229  **/
1230 gboolean
gtk_widget_path_iter_has_region(const GtkWidgetPath * path,gint pos,const gchar * name,GtkRegionFlags * flags)1231 gtk_widget_path_iter_has_region (const GtkWidgetPath *path,
1232                                  gint                 pos,
1233                                  const gchar         *name,
1234                                  GtkRegionFlags      *flags)
1235 {
1236   GQuark qname;
1237 
1238   gtk_internal_return_val_if_fail (path != NULL, FALSE);
1239   gtk_internal_return_val_if_fail (path->elems->len != 0, FALSE);
1240   gtk_internal_return_val_if_fail (name != NULL, FALSE);
1241 
1242   if (pos < 0 || pos >= path->elems->len)
1243     pos = path->elems->len - 1;
1244 
1245   qname = g_quark_try_string (name);
1246 
1247   if (qname == 0)
1248     return FALSE;
1249 
1250 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
1251   return gtk_widget_path_iter_has_qregion (path, pos, qname, flags);
1252 G_GNUC_END_IGNORE_DEPRECATIONS
1253 }
1254 
1255 /**
1256  * gtk_widget_path_get_object_type:
1257  * @path: a #GtkWidget
1258  *
1259  * Returns the topmost object type, that is, the object type this path
1260  * is representing.
1261  *
1262  * Returns: The object type
1263  *
1264  * Since: 3.0
1265  **/
1266 GType
gtk_widget_path_get_object_type(const GtkWidgetPath * path)1267 gtk_widget_path_get_object_type (const GtkWidgetPath *path)
1268 {
1269   GtkPathElement *elem;
1270 
1271   gtk_internal_return_val_if_fail (path != NULL, G_TYPE_INVALID);
1272 
1273   elem = &g_array_index (path->elems, GtkPathElement,
1274                          path->elems->len - 1);
1275   return gtk_css_node_declaration_get_type (elem->decl);
1276 }
1277 
1278 /**
1279  * gtk_widget_path_is_type:
1280  * @path: a #GtkWidgetPath
1281  * @type: widget type to match
1282  *
1283  * Returns %TRUE if the widget type represented by this path
1284  * is @type, or a subtype of it.
1285  *
1286  * Returns: %TRUE if the widget represented by @path is of type @type
1287  *
1288  * Since: 3.0
1289  **/
1290 gboolean
gtk_widget_path_is_type(const GtkWidgetPath * path,GType type)1291 gtk_widget_path_is_type (const GtkWidgetPath *path,
1292                          GType                type)
1293 {
1294   GtkPathElement *elem;
1295 
1296   gtk_internal_return_val_if_fail (path != NULL, FALSE);
1297 
1298   elem = &g_array_index (path->elems, GtkPathElement,
1299                          path->elems->len - 1);
1300 
1301   return g_type_is_a (gtk_css_node_declaration_get_type (elem->decl), type);
1302 }
1303 
1304 /**
1305  * gtk_widget_path_has_parent:
1306  * @path: a #GtkWidgetPath
1307  * @type: widget type to check in parents
1308  *
1309  * Returns %TRUE if any of the parents of the widget represented
1310  * in @path is of type @type, or any subtype of it.
1311  *
1312  * Returns: %TRUE if any parent is of type @type
1313  *
1314  * Since: 3.0
1315  **/
1316 gboolean
gtk_widget_path_has_parent(const GtkWidgetPath * path,GType type)1317 gtk_widget_path_has_parent (const GtkWidgetPath *path,
1318                             GType                type)
1319 {
1320   guint i;
1321 
1322   gtk_internal_return_val_if_fail (path != NULL, FALSE);
1323 
1324   for (i = 0; i < path->elems->len - 1; i++)
1325     {
1326       GtkPathElement *elem;
1327 
1328       elem = &g_array_index (path->elems, GtkPathElement, i);
1329 
1330       if (g_type_is_a (gtk_css_node_declaration_get_type (elem->decl), type))
1331         return TRUE;
1332     }
1333 
1334   return FALSE;
1335 }
1336