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