Skip to main content

gtk/auto/
tree_selection.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::{SelectionMode, TreeIter, TreeModel, TreePath, TreeView};
6use glib::{
7    prelude::*,
8    signal::{connect_raw, SignalHandlerId},
9    translate::*,
10};
11use std::{boxed::Box as Box_, fmt, mem::transmute, ptr};
12
13glib::wrapper! {
14    /// The [`TreeSelection`][crate::TreeSelection] object is a helper object to manage the selection
15    /// for a [`TreeView`][crate::TreeView] widget. The [`TreeSelection`][crate::TreeSelection] object is
16    /// automatically created when a new [`TreeView`][crate::TreeView] widget is created, and
17    /// cannot exist independently of this widget. The primary reason the
18    /// [`TreeSelection`][crate::TreeSelection] objects exists is for cleanliness of code and API.
19    /// That is, there is no conceptual reason all these functions could not be
20    /// methods on the [`TreeView`][crate::TreeView] widget instead of a separate function.
21    ///
22    /// The [`TreeSelection`][crate::TreeSelection] object is gotten from a [`TreeView`][crate::TreeView] by calling
23    /// [`TreeViewExt::selection()`][crate::prelude::TreeViewExt::selection()]. It can be manipulated to check the
24    /// selection status of the tree, as well as select and deselect individual
25    /// rows. Selection is done completely view side. As a result, multiple
26    /// views of the same model can have completely different selections.
27    /// Additionally, you cannot change the selection of a row on the model that
28    /// is not currently displayed by the view without expanding its parents
29    /// first.
30    ///
31    /// One of the important things to remember when monitoring the selection of
32    /// a view is that the [`changed`][struct@crate::TreeSelection#changed] signal is mostly a hint.
33    /// That is, it may only emit one signal when a range of rows is selected.
34    /// Additionally, it may on occasion emit a [`changed`][struct@crate::TreeSelection#changed] signal
35    /// when nothing has happened (mostly as a result of programmers calling
36    /// select_row on an already selected row).
37    ///
38    /// ## Properties
39    ///
40    ///
41    /// #### `mode`
42    ///  Selection mode.
43    /// See [`TreeSelectionExt::set_mode()`][crate::prelude::TreeSelectionExt::set_mode()] for more information on this property.
44    ///
45    /// Readable | Writeable
46    ///
47    /// ## Signals
48    ///
49    ///
50    /// #### `changed`
51    ///  Emitted whenever the selection has (possibly) changed. Please note that
52    /// this signal is mostly a hint. It may only be emitted once when a range
53    /// of rows are selected, and it may occasionally be emitted when nothing
54    /// has happened.
55    ///
56    ///
57    ///
58    /// # Implements
59    ///
60    /// [`TreeSelectionExt`][trait@crate::prelude::TreeSelectionExt], [`trait@glib::ObjectExt`]
61    #[doc(alias = "GtkTreeSelection")]
62    pub struct TreeSelection(Object<ffi::GtkTreeSelection, ffi::GtkTreeSelectionClass>);
63
64    match fn {
65        type_ => || ffi::gtk_tree_selection_get_type(),
66    }
67}
68
69impl TreeSelection {
70    pub const NONE: Option<&'static TreeSelection> = None;
71}
72
73mod sealed {
74    pub trait Sealed {}
75    impl<T: super::IsA<super::TreeSelection>> Sealed for T {}
76}
77
78/// Trait containing all [`struct@TreeSelection`] methods.
79///
80/// # Implementors
81///
82/// [`TreeSelection`][struct@crate::TreeSelection]
83pub trait TreeSelectionExt: IsA<TreeSelection> + sealed::Sealed + 'static {
84    /// Returns the number of rows that have been selected in `tree`.
85    ///
86    /// # Returns
87    ///
88    /// The number of rows selected.
89    #[doc(alias = "gtk_tree_selection_count_selected_rows")]
90    fn count_selected_rows(&self) -> i32 {
91        unsafe { ffi::gtk_tree_selection_count_selected_rows(self.as_ref().to_glib_none().0) }
92    }
93
94    /// Gets the selection mode for `self`. See
95    /// [`set_mode()`][Self::set_mode()].
96    ///
97    /// # Returns
98    ///
99    /// the current selection mode
100    #[doc(alias = "gtk_tree_selection_get_mode")]
101    #[doc(alias = "get_mode")]
102    fn mode(&self) -> SelectionMode {
103        unsafe {
104            from_glib(ffi::gtk_tree_selection_get_mode(
105                self.as_ref().to_glib_none().0,
106            ))
107        }
108    }
109
110    //#[doc(alias = "gtk_tree_selection_get_select_function")]
111    //#[doc(alias = "get_select_function")]
112    //fn select_function(&self) -> Option<Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>> {
113    //    unsafe { TODO: call ffi:gtk_tree_selection_get_select_function() }
114    //}
115
116    /// Sets `iter` to the currently selected node, if `self` is set to
117    /// [`SelectionMode::Single`][crate::SelectionMode::Single] or [`SelectionMode::Browse`][crate::SelectionMode::Browse].
118    ///
119    /// The `iter` argument may be [`None`] if you just want to test if `self`
120    /// has any selected nodes.
121    ///
122    /// The `model` argument is filled with the current model as a convenience.
123    ///
124    /// This function will not work with [`SelectionMode::Multiple`][crate::SelectionMode::Multiple]. See
125    /// [`selected_rows()`][Self::selected_rows()] instead.
126    ///
127    /// # Returns
128    ///
129    /// [`true`], if there is a selected node.
130    ///
131    /// ## `model`
132    /// the model
133    ///
134    /// ## `iter`
135    /// the iterator for the selected row
136    #[doc(alias = "gtk_tree_selection_get_selected")]
137    #[doc(alias = "get_selected")]
138    fn selected(&self) -> Option<(TreeModel, TreeIter)> {
139        unsafe {
140            let mut model = ptr::null_mut();
141            let mut iter = TreeIter::uninitialized();
142            let ret = from_glib(ffi::gtk_tree_selection_get_selected(
143                self.as_ref().to_glib_none().0,
144                &mut model,
145                iter.to_glib_none_mut().0,
146            ));
147            if ret {
148                Some((from_glib_none(model), iter))
149            } else {
150                None
151            }
152        }
153    }
154
155    /// Creates a list of path of all selected rows.
156    ///
157    /// Additionally, if you are planning on modifying the model after calling
158    /// this function, you may want to convert the returned list into a list
159    /// of `GtkTreeRowReferences`.
160    ///
161    /// To do this, you can use [`TreeRowReference::new()`][crate::TreeRowReference::new()].
162    ///
163    /// To free the return value, use:
164    ///
165    ///
166    ///
167    /// **⚠️ The following code is in C ⚠️**
168    ///
169    /// ```C
170    /// g_list_free_full (list, (GDestroyNotify) gtk_tree_path_free);
171    /// ```
172    ///
173    /// # Returns
174    ///
175    /// the selected paths
176    ///
177    /// ## `model`
178    /// A pointer to set to the [`TreeModel`][crate::TreeModel], or [`None`].
179    #[doc(alias = "gtk_tree_selection_get_selected_rows")]
180    #[doc(alias = "get_selected_rows")]
181    fn selected_rows(&self) -> (Vec<TreePath>, TreeModel) {
182        unsafe {
183            let mut model = ptr::null_mut();
184            let ret =
185                FromGlibPtrContainer::from_glib_full(ffi::gtk_tree_selection_get_selected_rows(
186                    self.as_ref().to_glib_none().0,
187                    &mut model,
188                ));
189            (ret, from_glib_none(model))
190        }
191    }
192
193    /// Returns the tree view associated with `self`.
194    ///
195    /// # Returns
196    ///
197    /// A [`TreeView`][crate::TreeView]
198    #[doc(alias = "gtk_tree_selection_get_tree_view")]
199    #[doc(alias = "get_tree_view")]
200    fn tree_view(&self) -> Option<TreeView> {
201        unsafe {
202            from_glib_none(ffi::gtk_tree_selection_get_tree_view(
203                self.as_ref().to_glib_none().0,
204            ))
205        }
206    }
207
208    //#[doc(alias = "gtk_tree_selection_get_user_data")]
209    //#[doc(alias = "get_user_data")]
210    //fn user_data(&self) -> /*Unimplemented*/Option<Basic: Pointer> {
211    //    unsafe { TODO: call ffi:gtk_tree_selection_get_user_data() }
212    //}
213
214    /// Returns [`true`] if the row at `iter` is currently selected.
215    /// ## `iter`
216    /// A valid [`TreeIter`][crate::TreeIter]
217    ///
218    /// # Returns
219    ///
220    /// [`true`], if `iter` is selected
221    #[doc(alias = "gtk_tree_selection_iter_is_selected")]
222    fn iter_is_selected(&self, iter: &TreeIter) -> bool {
223        unsafe {
224            from_glib(ffi::gtk_tree_selection_iter_is_selected(
225                self.as_ref().to_glib_none().0,
226                mut_override(iter.to_glib_none().0),
227            ))
228        }
229    }
230
231    /// Returns [`true`] if the row pointed to by `path` is currently selected. If `path`
232    /// does not point to a valid location, [`false`] is returned
233    /// ## `path`
234    /// A [`TreePath`][crate::TreePath] to check selection on.
235    ///
236    /// # Returns
237    ///
238    /// [`true`] if `path` is selected.
239    #[doc(alias = "gtk_tree_selection_path_is_selected")]
240    fn path_is_selected(&self, path: &TreePath) -> bool {
241        unsafe {
242            from_glib(ffi::gtk_tree_selection_path_is_selected(
243                self.as_ref().to_glib_none().0,
244                mut_override(path.to_glib_none().0),
245            ))
246        }
247    }
248
249    /// Selects all the nodes. `self` must be set to [`SelectionMode::Multiple`][crate::SelectionMode::Multiple]
250    /// mode.
251    #[doc(alias = "gtk_tree_selection_select_all")]
252    fn select_all(&self) {
253        unsafe {
254            ffi::gtk_tree_selection_select_all(self.as_ref().to_glib_none().0);
255        }
256    }
257
258    /// Selects the specified iterator.
259    /// ## `iter`
260    /// The [`TreeIter`][crate::TreeIter] to be selected.
261    #[doc(alias = "gtk_tree_selection_select_iter")]
262    fn select_iter(&self, iter: &TreeIter) {
263        unsafe {
264            ffi::gtk_tree_selection_select_iter(
265                self.as_ref().to_glib_none().0,
266                mut_override(iter.to_glib_none().0),
267            );
268        }
269    }
270
271    /// Select the row at `path`.
272    /// ## `path`
273    /// The [`TreePath`][crate::TreePath] to be selected.
274    #[doc(alias = "gtk_tree_selection_select_path")]
275    fn select_path(&self, path: &TreePath) {
276        unsafe {
277            ffi::gtk_tree_selection_select_path(
278                self.as_ref().to_glib_none().0,
279                mut_override(path.to_glib_none().0),
280            );
281        }
282    }
283
284    /// Selects a range of nodes, determined by `start_path` and `end_path` inclusive.
285    /// `self` must be set to [`SelectionMode::Multiple`][crate::SelectionMode::Multiple] mode.
286    /// ## `start_path`
287    /// The initial node of the range.
288    /// ## `end_path`
289    /// The final node of the range.
290    #[doc(alias = "gtk_tree_selection_select_range")]
291    fn select_range(&self, start_path: &TreePath, end_path: &TreePath) {
292        unsafe {
293            ffi::gtk_tree_selection_select_range(
294                self.as_ref().to_glib_none().0,
295                mut_override(start_path.to_glib_none().0),
296                mut_override(end_path.to_glib_none().0),
297            );
298        }
299    }
300
301    /// Calls a function for each selected node. Note that you cannot modify
302    /// the tree or selection from within this function. As a result,
303    /// [`selected_rows()`][Self::selected_rows()] might be more useful.
304    /// ## `func`
305    /// The function to call for each selected node.
306    #[doc(alias = "gtk_tree_selection_selected_foreach")]
307    fn selected_foreach<P: FnMut(&TreeModel, &TreePath, &TreeIter)>(&self, func: P) {
308        let func_data: P = func;
309        unsafe extern "C" fn func_func<P: FnMut(&TreeModel, &TreePath, &TreeIter)>(
310            model: *mut ffi::GtkTreeModel,
311            path: *mut ffi::GtkTreePath,
312            iter: *mut ffi::GtkTreeIter,
313            data: glib::ffi::gpointer,
314        ) {
315            let model = from_glib_borrow(model);
316            let path = from_glib_borrow(path);
317            let iter = from_glib_borrow(iter);
318            let callback: *mut P = data as *const _ as usize as *mut P;
319            (*callback)(&model, &path, &iter)
320        }
321        let func = Some(func_func::<P> as _);
322        let super_callback0: &P = &func_data;
323        unsafe {
324            ffi::gtk_tree_selection_selected_foreach(
325                self.as_ref().to_glib_none().0,
326                func,
327                super_callback0 as *const _ as usize as *mut _,
328            );
329        }
330    }
331
332    /// Sets the selection mode of the `self`. If the previous type was
333    /// [`SelectionMode::Multiple`][crate::SelectionMode::Multiple], then the anchor is kept selected, if it was
334    /// previously selected.
335    /// ## `type_`
336    /// The selection mode
337    #[doc(alias = "gtk_tree_selection_set_mode")]
338    fn set_mode(&self, type_: SelectionMode) {
339        unsafe {
340            ffi::gtk_tree_selection_set_mode(self.as_ref().to_glib_none().0, type_.into_glib());
341        }
342    }
343
344    /// Sets the selection function.
345    ///
346    /// If set, this function is called before any node is selected or unselected,
347    /// giving some control over which nodes are selected. The select function
348    /// should return [`true`] if the state of the node may be toggled, and [`false`]
349    /// if the state of the node should be left unchanged.
350    /// ## `func`
351    /// The selection function. May be [`None`]
352    #[doc(alias = "gtk_tree_selection_set_select_function")]
353    fn set_select_function(
354        &self,
355        func: Option<Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>>,
356    ) {
357        let func_data: Box_<
358            Option<Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>>,
359        > = Box_::new(func);
360        unsafe extern "C" fn func_func(
361            selection: *mut ffi::GtkTreeSelection,
362            model: *mut ffi::GtkTreeModel,
363            path: *mut ffi::GtkTreePath,
364            path_currently_selected: glib::ffi::gboolean,
365            data: glib::ffi::gpointer,
366        ) -> glib::ffi::gboolean {
367            let selection = from_glib_borrow(selection);
368            let model = from_glib_borrow(model);
369            let path = from_glib_borrow(path);
370            let path_currently_selected = from_glib(path_currently_selected);
371            let callback: &Option<
372                Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>,
373            > = &*(data as *mut _);
374            if let Some(ref callback) = *callback {
375                callback(&selection, &model, &path, path_currently_selected)
376            } else {
377                panic!("cannot get closure...")
378            }
379            .into_glib()
380        }
381        let func = if func_data.is_some() {
382            Some(func_func as _)
383        } else {
384            None
385        };
386        unsafe extern "C" fn destroy_func(data: glib::ffi::gpointer) {
387            let _callback: Box_<
388                Option<Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>>,
389            > = Box_::from_raw(data as *mut _);
390        }
391        let destroy_call3 = Some(destroy_func as _);
392        let super_callback0: Box_<
393            Option<Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>>,
394        > = func_data;
395        unsafe {
396            ffi::gtk_tree_selection_set_select_function(
397                self.as_ref().to_glib_none().0,
398                func,
399                Box_::into_raw(super_callback0) as *mut _,
400                destroy_call3,
401            );
402        }
403    }
404
405    /// Unselects all the nodes.
406    #[doc(alias = "gtk_tree_selection_unselect_all")]
407    fn unselect_all(&self) {
408        unsafe {
409            ffi::gtk_tree_selection_unselect_all(self.as_ref().to_glib_none().0);
410        }
411    }
412
413    /// Unselects the specified iterator.
414    /// ## `iter`
415    /// The [`TreeIter`][crate::TreeIter] to be unselected.
416    #[doc(alias = "gtk_tree_selection_unselect_iter")]
417    fn unselect_iter(&self, iter: &TreeIter) {
418        unsafe {
419            ffi::gtk_tree_selection_unselect_iter(
420                self.as_ref().to_glib_none().0,
421                mut_override(iter.to_glib_none().0),
422            );
423        }
424    }
425
426    /// Unselects the row at `path`.
427    /// ## `path`
428    /// The [`TreePath`][crate::TreePath] to be unselected.
429    #[doc(alias = "gtk_tree_selection_unselect_path")]
430    fn unselect_path(&self, path: &TreePath) {
431        unsafe {
432            ffi::gtk_tree_selection_unselect_path(
433                self.as_ref().to_glib_none().0,
434                mut_override(path.to_glib_none().0),
435            );
436        }
437    }
438
439    /// Unselects a range of nodes, determined by `start_path` and `end_path`
440    /// inclusive.
441    /// ## `start_path`
442    /// The initial node of the range.
443    /// ## `end_path`
444    /// The initial node of the range.
445    #[doc(alias = "gtk_tree_selection_unselect_range")]
446    fn unselect_range(&self, start_path: &TreePath, end_path: &TreePath) {
447        unsafe {
448            ffi::gtk_tree_selection_unselect_range(
449                self.as_ref().to_glib_none().0,
450                mut_override(start_path.to_glib_none().0),
451                mut_override(end_path.to_glib_none().0),
452            );
453        }
454    }
455
456    /// Emitted whenever the selection has (possibly) changed. Please note that
457    /// this signal is mostly a hint. It may only be emitted once when a range
458    /// of rows are selected, and it may occasionally be emitted when nothing
459    /// has happened.
460    #[doc(alias = "changed")]
461    fn connect_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
462        unsafe extern "C" fn changed_trampoline<P: IsA<TreeSelection>, F: Fn(&P) + 'static>(
463            this: *mut ffi::GtkTreeSelection,
464            f: glib::ffi::gpointer,
465        ) {
466            let f: &F = &*(f as *const F);
467            f(TreeSelection::from_glib_borrow(this).unsafe_cast_ref())
468        }
469        unsafe {
470            let f: Box_<F> = Box_::new(f);
471            connect_raw(
472                self.as_ptr() as *mut _,
473                b"changed\0".as_ptr() as *const _,
474                Some(transmute::<_, unsafe extern "C" fn()>(
475                    changed_trampoline::<Self, F> as *const (),
476                )),
477                Box_::into_raw(f),
478            )
479        }
480    }
481
482    #[doc(alias = "mode")]
483    fn connect_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
484        unsafe extern "C" fn notify_mode_trampoline<P: IsA<TreeSelection>, F: Fn(&P) + 'static>(
485            this: *mut ffi::GtkTreeSelection,
486            _param_spec: glib::ffi::gpointer,
487            f: glib::ffi::gpointer,
488        ) {
489            let f: &F = &*(f as *const F);
490            f(TreeSelection::from_glib_borrow(this).unsafe_cast_ref())
491        }
492        unsafe {
493            let f: Box_<F> = Box_::new(f);
494            connect_raw(
495                self.as_ptr() as *mut _,
496                b"notify::mode\0".as_ptr() as *const _,
497                Some(transmute::<_, unsafe extern "C" fn()>(
498                    notify_mode_trampoline::<Self, F> as *const (),
499                )),
500                Box_::into_raw(f),
501            )
502        }
503    }
504}
505
506impl<O: IsA<TreeSelection>> TreeSelectionExt for O {}
507
508impl fmt::Display for TreeSelection {
509    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
510        f.write_str("TreeSelection")
511    }
512}