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