Skip to main content

gtk/auto/
widget_path.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{StateFlags, Widget};
6use glib::{prelude::*, translate::*};
7use std::fmt;
8
9glib::wrapper! {
10    /// GtkWidgetPath is a boxed type that represents a widget hierarchy from
11    /// the topmost widget, typically a toplevel, to any child. This widget
12    /// path abstraction is used in [`StyleContext`][crate::StyleContext] on behalf of the real
13    /// widget in order to query style information.
14    ///
15    /// If you are using GTK+ widgets, you probably will not need to use
16    /// this API directly, as there is [`WidgetExt::path()`][crate::prelude::WidgetExt::path()], and the style
17    /// context returned by [`WidgetExt::style_context()`][crate::prelude::WidgetExt::style_context()] will be automatically
18    /// updated on widget hierarchy changes.
19    ///
20    /// The widget path generation is generally simple:
21    ///
22    /// ## Defining a button within a window
23    ///
24    ///
25    ///
26    /// **⚠️ The following code is in C ⚠️**
27    ///
28    /// ```C
29    /// {
30    ///   GtkWidgetPath *path;
31    ///
32    ///   path = gtk_widget_path_new ();
33    ///   gtk_widget_path_append_type (path, GTK_TYPE_WINDOW);
34    ///   gtk_widget_path_append_type (path, GTK_TYPE_BUTTON);
35    /// }
36    /// ```
37    ///
38    /// Although more complex information, such as widget names, or
39    /// different classes (property that may be used by other widget
40    /// types) and intermediate regions may be included:
41    ///
42    /// ## Defining the first tab widget in a notebook
43    ///
44    ///
45    ///
46    /// **⚠️ The following code is in C ⚠️**
47    ///
48    /// ```C
49    /// {
50    ///   GtkWidgetPath *path;
51    ///   guint pos;
52    ///
53    ///   path = gtk_widget_path_new ();
54    ///
55    ///   pos = gtk_widget_path_append_type (path, GTK_TYPE_NOTEBOOK);
56    ///   gtk_widget_path_iter_add_region (path, pos, "tab", GTK_REGION_EVEN | GTK_REGION_FIRST);
57    ///
58    ///   pos = gtk_widget_path_append_type (path, GTK_TYPE_LABEL);
59    ///   gtk_widget_path_iter_set_name (path, pos, "first tab label");
60    /// }
61    /// ```
62    ///
63    /// All this information will be used to match the style information
64    /// that applies to the described widget.
65    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
66    pub struct WidgetPath(Shared<ffi::GtkWidgetPath>);
67
68    match fn {
69        ref => |ptr| ffi::gtk_widget_path_ref(ptr),
70        unref => |ptr| ffi::gtk_widget_path_unref(ptr),
71        type_ => || ffi::gtk_widget_path_get_type(),
72    }
73}
74
75impl WidgetPath {
76    /// Returns an empty widget path.
77    ///
78    /// # Returns
79    ///
80    /// A newly created, empty, [`WidgetPath`][crate::WidgetPath]
81    #[doc(alias = "gtk_widget_path_new")]
82    pub fn new() -> WidgetPath {
83        assert_initialized_main_thread!();
84        unsafe { from_glib_full(ffi::gtk_widget_path_new()) }
85    }
86
87    /// Appends the data from `widget` to the widget hierarchy represented
88    /// by `self`. This function is a shortcut for adding information from
89    /// `widget` to the given `self`. This includes setting the name or
90    /// adding the style classes from `widget`.
91    /// ## `widget`
92    /// the widget to append to the widget path
93    ///
94    /// # Returns
95    ///
96    /// the position where the data was inserted
97    #[doc(alias = "gtk_widget_path_append_for_widget")]
98    pub fn append_for_widget(&self, widget: &impl IsA<Widget>) -> i32 {
99        unsafe {
100            ffi::gtk_widget_path_append_for_widget(
101                self.to_glib_none().0,
102                widget.as_ref().to_glib_none().0,
103            )
104        }
105    }
106
107    /// Appends a widget type to the widget hierarchy represented by `self`.
108    /// ## `type_`
109    /// widget type to append
110    ///
111    /// # Returns
112    ///
113    /// the position where the element was inserted
114    #[doc(alias = "gtk_widget_path_append_type")]
115    pub fn append_type(&self, type_: glib::types::Type) -> i32 {
116        unsafe { ffi::gtk_widget_path_append_type(self.to_glib_none().0, type_.into_glib()) }
117    }
118
119    /// Appends a widget type with all its siblings to the widget hierarchy
120    /// represented by `self`. Using this function instead of
121    /// [`append_type()`][Self::append_type()] will allow the CSS theming to use
122    /// sibling matches in selectors and apply :nth-`child()` pseudo classes.
123    /// In turn, it requires a lot more care in widget implementations as
124    /// widgets need to make sure to call [`WidgetExt::reset_style()`][crate::prelude::WidgetExt::reset_style()] on all
125    /// involved widgets when the `siblings` path changes.
126    /// ## `siblings`
127    /// a widget path describing a list of siblings. This path
128    ///  may not contain any siblings itself and it must not be modified
129    ///  afterwards.
130    /// ## `sibling_index`
131    /// index into `siblings` for where the added element is
132    ///  positioned.
133    ///
134    /// # Returns
135    ///
136    /// the position where the element was inserted.
137    #[doc(alias = "gtk_widget_path_append_with_siblings")]
138    pub fn append_with_siblings(&self, siblings: &WidgetPath, sibling_index: u32) -> i32 {
139        unsafe {
140            ffi::gtk_widget_path_append_with_siblings(
141                self.to_glib_none().0,
142                siblings.to_glib_none().0,
143                sibling_index,
144            )
145        }
146    }
147
148    #[doc(alias = "gtk_widget_path_copy")]
149    #[must_use]
150    pub fn copy(&self) -> Option<WidgetPath> {
151        unsafe { from_glib_full(ffi::gtk_widget_path_copy(self.to_glib_none().0)) }
152    }
153
154    /// Returns the topmost object type, that is, the object type this path
155    /// is representing.
156    ///
157    /// # Returns
158    ///
159    /// The object type
160    #[doc(alias = "gtk_widget_path_get_object_type")]
161    #[doc(alias = "get_object_type")]
162    pub fn object_type(&self) -> glib::types::Type {
163        unsafe { from_glib(ffi::gtk_widget_path_get_object_type(self.to_glib_none().0)) }
164    }
165
166    /// Returns [`true`] if any of the parents of the widget represented
167    /// in `self` is of type `type_`, or any subtype of it.
168    /// ## `type_`
169    /// widget type to check in parents
170    ///
171    /// # Returns
172    ///
173    /// [`true`] if any parent is of type `type_`
174    #[doc(alias = "gtk_widget_path_has_parent")]
175    pub fn has_parent(&self, type_: glib::types::Type) -> bool {
176        unsafe {
177            from_glib(ffi::gtk_widget_path_has_parent(
178                self.to_glib_none().0,
179                type_.into_glib(),
180            ))
181        }
182    }
183
184    /// Returns [`true`] if the widget type represented by this path
185    /// is `type_`, or a subtype of it.
186    /// ## `type_`
187    /// widget type to match
188    ///
189    /// # Returns
190    ///
191    /// [`true`] if the widget represented by `self` is of type `type_`
192    #[doc(alias = "gtk_widget_path_is_type")]
193    pub fn is_type(&self, type_: glib::types::Type) -> bool {
194        unsafe {
195            from_glib(ffi::gtk_widget_path_is_type(
196                self.to_glib_none().0,
197                type_.into_glib(),
198            ))
199        }
200    }
201
202    /// Adds the class `name` to the widget at position `pos` in
203    /// the hierarchy defined in `self`. See
204    /// [`StyleContextExt::add_class()`][crate::prelude::StyleContextExt::add_class()].
205    /// ## `pos`
206    /// position to modify, -1 for the path head
207    /// ## `name`
208    /// a class name
209    #[doc(alias = "gtk_widget_path_iter_add_class")]
210    pub fn iter_add_class(&self, pos: i32, name: &str) {
211        unsafe {
212            ffi::gtk_widget_path_iter_add_class(self.to_glib_none().0, pos, name.to_glib_none().0);
213        }
214    }
215
216    /// Removes all classes from the widget at position `pos` in the
217    /// hierarchy defined in `self`.
218    /// ## `pos`
219    /// position to modify, -1 for the path head
220    #[doc(alias = "gtk_widget_path_iter_clear_classes")]
221    pub fn iter_clear_classes(&self, pos: i32) {
222        unsafe {
223            ffi::gtk_widget_path_iter_clear_classes(self.to_glib_none().0, pos);
224        }
225    }
226
227    /// Returns the name corresponding to the widget found at
228    /// the position `pos` in the widget hierarchy defined by
229    /// `self`
230    /// ## `pos`
231    /// position to get the widget name for, -1 for the path head
232    ///
233    /// # Returns
234    ///
235    /// The widget name, or [`None`] if none was set.
236    #[doc(alias = "gtk_widget_path_iter_get_name")]
237    pub fn iter_get_name(&self, pos: i32) -> Option<glib::GString> {
238        unsafe {
239            from_glib_none(ffi::gtk_widget_path_iter_get_name(
240                self.to_glib_none().0,
241                pos,
242            ))
243        }
244    }
245
246    /// Returns the object name that is at position `pos` in the widget
247    /// hierarchy defined in `self`.
248    /// ## `pos`
249    /// position to get the object name for, -1 for the path head
250    ///
251    /// # Returns
252    ///
253    /// the name or [`None`]
254    #[doc(alias = "gtk_widget_path_iter_get_object_name")]
255    pub fn iter_get_object_name(&self, pos: i32) -> Option<glib::GString> {
256        unsafe {
257            from_glib_none(ffi::gtk_widget_path_iter_get_object_name(
258                self.to_glib_none().0,
259                pos,
260            ))
261        }
262    }
263
264    /// Returns the object `GType` that is at position `pos` in the widget
265    /// hierarchy defined in `self`.
266    /// ## `pos`
267    /// position to get the object type for, -1 for the path head
268    ///
269    /// # Returns
270    ///
271    /// a widget type
272    #[doc(alias = "gtk_widget_path_iter_get_object_type")]
273    pub fn iter_get_object_type(&self, pos: i32) -> glib::types::Type {
274        unsafe {
275            from_glib(ffi::gtk_widget_path_iter_get_object_type(
276                self.to_glib_none().0,
277                pos,
278            ))
279        }
280    }
281
282    /// Returns the index into the list of siblings for the element at `pos` as
283    /// returned by [`iter_get_siblings()`][Self::iter_get_siblings()]. If that function would
284    /// return [`None`] because the element at `pos` has no siblings, this function
285    /// will return 0.
286    /// ## `pos`
287    /// position to get the sibling index for, -1 for the path head
288    ///
289    /// # Returns
290    ///
291    /// 0 or the index into the list of siblings for the element at `pos`.
292    #[doc(alias = "gtk_widget_path_iter_get_sibling_index")]
293    pub fn iter_get_sibling_index(&self, pos: i32) -> u32 {
294        unsafe { ffi::gtk_widget_path_iter_get_sibling_index(self.to_glib_none().0, pos) }
295    }
296
297    /// Returns the list of siblings for the element at `pos`. If the element
298    /// was not added with siblings, [`None`] is returned.
299    /// ## `pos`
300    /// position to get the siblings for, -1 for the path head
301    ///
302    /// # Returns
303    ///
304    /// [`None`] or the list of siblings for the element at `pos`.
305    #[doc(alias = "gtk_widget_path_iter_get_siblings")]
306    #[must_use]
307    pub fn iter_get_siblings(&self, pos: i32) -> Option<WidgetPath> {
308        unsafe {
309            from_glib_none(ffi::gtk_widget_path_iter_get_siblings(
310                self.to_glib_none().0,
311                pos,
312            ))
313        }
314    }
315
316    /// Returns the state flags corresponding to the widget found at
317    /// the position `pos` in the widget hierarchy defined by
318    /// `self`
319    /// ## `pos`
320    /// position to get the state for, -1 for the path head
321    ///
322    /// # Returns
323    ///
324    /// The state flags
325    #[doc(alias = "gtk_widget_path_iter_get_state")]
326    pub fn iter_get_state(&self, pos: i32) -> StateFlags {
327        unsafe {
328            from_glib(ffi::gtk_widget_path_iter_get_state(
329                self.to_glib_none().0,
330                pos,
331            ))
332        }
333    }
334
335    /// Returns [`true`] if the widget at position `pos` has the class `name`
336    /// defined, [`false`] otherwise.
337    /// ## `pos`
338    /// position to query, -1 for the path head
339    /// ## `name`
340    /// class name
341    ///
342    /// # Returns
343    ///
344    /// [`true`] if the class `name` is defined for the widget at `pos`
345    #[doc(alias = "gtk_widget_path_iter_has_class")]
346    pub fn iter_has_class(&self, pos: i32, name: &str) -> bool {
347        unsafe {
348            from_glib(ffi::gtk_widget_path_iter_has_class(
349                self.to_glib_none().0,
350                pos,
351                name.to_glib_none().0,
352            ))
353        }
354    }
355
356    /// Returns [`true`] if the widget at position `pos` has the name `name`,
357    /// [`false`] otherwise.
358    /// ## `pos`
359    /// position to query, -1 for the path head
360    /// ## `name`
361    /// a widget name
362    ///
363    /// # Returns
364    ///
365    /// [`true`] if the widget at `pos` has this name
366    #[doc(alias = "gtk_widget_path_iter_has_name")]
367    pub fn iter_has_name(&self, pos: i32, name: &str) -> bool {
368        unsafe {
369            from_glib(ffi::gtk_widget_path_iter_has_name(
370                self.to_glib_none().0,
371                pos,
372                name.to_glib_none().0,
373            ))
374        }
375    }
376
377    /// See [`iter_has_class()`][Self::iter_has_class()]. This is a version that operates
378    /// with GQuarks.
379    /// ## `pos`
380    /// position to query, -1 for the path head
381    /// ## `qname`
382    /// class name as a `GQuark`
383    ///
384    /// # Returns
385    ///
386    /// [`true`] if the widget at `pos` has the class defined.
387    #[doc(alias = "gtk_widget_path_iter_has_qclass")]
388    pub fn iter_has_qclass(&self, pos: i32, qname: glib::Quark) -> bool {
389        unsafe {
390            from_glib(ffi::gtk_widget_path_iter_has_qclass(
391                self.to_glib_none().0,
392                pos,
393                qname.into_glib(),
394            ))
395        }
396    }
397
398    /// See [`iter_has_name()`][Self::iter_has_name()]. This is a version
399    /// that operates on `GQuarks`.
400    /// ## `pos`
401    /// position to query, -1 for the path head
402    /// ## `qname`
403    /// widget name as a `GQuark`
404    ///
405    /// # Returns
406    ///
407    /// [`true`] if the widget at `pos` has this name
408    #[doc(alias = "gtk_widget_path_iter_has_qname")]
409    pub fn iter_has_qname(&self, pos: i32, qname: glib::Quark) -> bool {
410        unsafe {
411            from_glib(ffi::gtk_widget_path_iter_has_qname(
412                self.to_glib_none().0,
413                pos,
414                qname.into_glib(),
415            ))
416        }
417    }
418
419    /// Returns a list with all the class names defined for the widget
420    /// at position `pos` in the hierarchy defined in `self`.
421    /// ## `pos`
422    /// position to query, -1 for the path head
423    ///
424    /// # Returns
425    ///
426    /// The list of
427    ///  classes, This is a list of strings, the `GSList` contents
428    ///  are owned by GTK+, but you should use `g_slist_free()` to
429    ///  free the list itself.
430    #[doc(alias = "gtk_widget_path_iter_list_classes")]
431    pub fn iter_list_classes(&self, pos: i32) -> Vec<glib::GString> {
432        unsafe {
433            FromGlibPtrContainer::from_glib_container(ffi::gtk_widget_path_iter_list_classes(
434                self.to_glib_none().0,
435                pos,
436            ))
437        }
438    }
439
440    /// Removes the class `name` from the widget at position `pos` in
441    /// the hierarchy defined in `self`.
442    /// ## `pos`
443    /// position to modify, -1 for the path head
444    /// ## `name`
445    /// class name
446    #[doc(alias = "gtk_widget_path_iter_remove_class")]
447    pub fn iter_remove_class(&self, pos: i32, name: &str) {
448        unsafe {
449            ffi::gtk_widget_path_iter_remove_class(
450                self.to_glib_none().0,
451                pos,
452                name.to_glib_none().0,
453            );
454        }
455    }
456
457    /// Sets the widget name for the widget found at position `pos`
458    /// in the widget hierarchy defined by `self`.
459    /// ## `pos`
460    /// position to modify, -1 for the path head
461    /// ## `name`
462    /// widget name
463    #[doc(alias = "gtk_widget_path_iter_set_name")]
464    pub fn iter_set_name(&self, pos: i32, name: &str) {
465        unsafe {
466            ffi::gtk_widget_path_iter_set_name(self.to_glib_none().0, pos, name.to_glib_none().0);
467        }
468    }
469
470    /// Sets the object name for a given position in the widget hierarchy
471    /// defined by `self`.
472    ///
473    /// When set, the object name overrides the object type when matching
474    /// CSS.
475    /// ## `pos`
476    /// position to modify, -1 for the path head
477    /// ## `name`
478    /// object name to set or [`None`] to unset
479    #[doc(alias = "gtk_widget_path_iter_set_object_name")]
480    pub fn iter_set_object_name(&self, pos: i32, name: Option<&str>) {
481        unsafe {
482            ffi::gtk_widget_path_iter_set_object_name(
483                self.to_glib_none().0,
484                pos,
485                name.to_glib_none().0,
486            );
487        }
488    }
489
490    /// Sets the object type for a given position in the widget hierarchy
491    /// defined by `self`.
492    /// ## `pos`
493    /// position to modify, -1 for the path head
494    /// ## `type_`
495    /// object type to set
496    #[doc(alias = "gtk_widget_path_iter_set_object_type")]
497    pub fn iter_set_object_type(&self, pos: i32, type_: glib::types::Type) {
498        unsafe {
499            ffi::gtk_widget_path_iter_set_object_type(
500                self.to_glib_none().0,
501                pos,
502                type_.into_glib(),
503            );
504        }
505    }
506
507    /// Sets the widget name for the widget found at position `pos`
508    /// in the widget hierarchy defined by `self`.
509    ///
510    /// If you want to update just a single state flag, you need to do
511    /// this manually, as this function updates all state flags.
512    ///
513    /// ## Setting a flag
514    ///
515    ///
516    ///
517    /// **⚠️ The following code is in C ⚠️**
518    ///
519    /// ```C
520    /// gtk_widget_path_iter_set_state (path, pos, gtk_widget_path_iter_get_state (path, pos) | flag);
521    /// ```
522    ///
523    /// ## Unsetting a flag
524    ///
525    ///
526    ///
527    /// **⚠️ The following code is in C ⚠️**
528    ///
529    /// ```C
530    /// gtk_widget_path_iter_set_state (path, pos, gtk_widget_path_iter_get_state (path, pos) & ~flag);
531    /// ```
532    /// ## `pos`
533    /// position to modify, -1 for the path head
534    /// ## `state`
535    /// state flags
536    #[doc(alias = "gtk_widget_path_iter_set_state")]
537    pub fn iter_set_state(&self, pos: i32, state: StateFlags) {
538        unsafe {
539            ffi::gtk_widget_path_iter_set_state(self.to_glib_none().0, pos, state.into_glib());
540        }
541    }
542
543    /// Returns the number of [`Widget`][crate::Widget] `GTypes` between the represented
544    /// widget and its topmost container.
545    ///
546    /// # Returns
547    ///
548    /// the number of elements in the path
549    #[doc(alias = "gtk_widget_path_length")]
550    pub fn length(&self) -> i32 {
551        unsafe { ffi::gtk_widget_path_length(self.to_glib_none().0) }
552    }
553
554    /// Prepends a widget type to the widget hierachy represented by `self`.
555    /// ## `type_`
556    /// widget type to prepend
557    #[doc(alias = "gtk_widget_path_prepend_type")]
558    pub fn prepend_type(&self, type_: glib::types::Type) {
559        unsafe {
560            ffi::gtk_widget_path_prepend_type(self.to_glib_none().0, type_.into_glib());
561        }
562    }
563
564    /// Dumps the widget path into a string representation. It tries to match
565    /// the CSS style as closely as possible (Note that there might be paths
566    /// that cannot be represented in CSS).
567    ///
568    /// The main use of this code is for debugging purposes, so that you can
569    /// `g_print()` the path or dump it in a gdb session.
570    ///
571    /// # Returns
572    ///
573    /// A new string describing `self`.
574    #[doc(alias = "gtk_widget_path_to_string")]
575    #[doc(alias = "to_string")]
576    pub fn to_str(&self) -> glib::GString {
577        unsafe { from_glib_full(ffi::gtk_widget_path_to_string(self.to_glib_none().0)) }
578    }
579}
580
581impl Default for WidgetPath {
582    fn default() -> Self {
583        Self::new()
584    }
585}
586
587impl fmt::Display for WidgetPath {
588    #[inline]
589    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
590        f.write_str(&self.to_str())
591    }
592}