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    /// Creates a list of path of all selected rows.
152    ///
153    /// Additionally, if you are planning on modifying the model after calling
154    /// this function, you may want to convert the returned list into a list
155    /// of `GtkTreeRowReferences`.
156    ///
157    /// To do this, you can use [`TreeRowReference::new()`][crate::TreeRowReference::new()].
158    ///
159    /// To free the return value, use:
160    ///
161    ///
162    ///
163    /// **⚠️ The following code is in C ⚠️**
164    ///
165    /// ```C
166    /// g_list_free_full (list, (GDestroyNotify) gtk_tree_path_free);
167    /// ```
168    ///
169    /// # Returns
170    ///
171    /// the selected paths
172    ///
173    /// ## `model`
174    /// A pointer to set to the [`TreeModel`][crate::TreeModel], or [`None`].
175    #[doc(alias = "gtk_tree_selection_get_selected_rows")]
176    #[doc(alias = "get_selected_rows")]
177    fn selected_rows(&self) -> (Vec<TreePath>, TreeModel) {
178        unsafe {
179            let mut model = std::ptr::null_mut();
180            let ret =
181                FromGlibPtrContainer::from_glib_full(ffi::gtk_tree_selection_get_selected_rows(
182                    self.as_ref().to_glib_none().0,
183                    &mut model,
184                ));
185            (ret, from_glib_none(model))
186        }
187    }
188
189    /// Returns the tree view associated with `self`.
190    ///
191    /// # Returns
192    ///
193    /// A [`TreeView`][crate::TreeView]
194    #[doc(alias = "gtk_tree_selection_get_tree_view")]
195    #[doc(alias = "get_tree_view")]
196    fn tree_view(&self) -> Option<TreeView> {
197        unsafe {
198            from_glib_none(ffi::gtk_tree_selection_get_tree_view(
199                self.as_ref().to_glib_none().0,
200            ))
201        }
202    }
203
204    //#[doc(alias = "gtk_tree_selection_get_user_data")]
205    //#[doc(alias = "get_user_data")]
206    //fn user_data(&self) -> /*Unimplemented*/Option<Basic: Pointer> {
207    //    unsafe { TODO: call ffi:gtk_tree_selection_get_user_data() }
208    //}
209
210    /// Returns [`true`] if the row at `iter` is currently selected.
211    /// ## `iter`
212    /// A valid [`TreeIter`][crate::TreeIter]
213    ///
214    /// # Returns
215    ///
216    /// [`true`], if `iter` is selected
217    #[doc(alias = "gtk_tree_selection_iter_is_selected")]
218    fn iter_is_selected(&self, iter: &TreeIter) -> bool {
219        unsafe {
220            from_glib(ffi::gtk_tree_selection_iter_is_selected(
221                self.as_ref().to_glib_none().0,
222                mut_override(iter.to_glib_none().0),
223            ))
224        }
225    }
226
227    /// Returns [`true`] if the row pointed to by `path` is currently selected. If `path`
228    /// does not point to a valid location, [`false`] is returned
229    /// ## `path`
230    /// A [`TreePath`][crate::TreePath] to check selection on.
231    ///
232    /// # Returns
233    ///
234    /// [`true`] if `path` is selected.
235    #[doc(alias = "gtk_tree_selection_path_is_selected")]
236    fn path_is_selected(&self, path: &TreePath) -> bool {
237        unsafe {
238            from_glib(ffi::gtk_tree_selection_path_is_selected(
239                self.as_ref().to_glib_none().0,
240                mut_override(path.to_glib_none().0),
241            ))
242        }
243    }
244
245    /// Selects all the nodes. `self` must be set to [`SelectionMode::Multiple`][crate::SelectionMode::Multiple]
246    /// mode.
247    #[doc(alias = "gtk_tree_selection_select_all")]
248    fn select_all(&self) {
249        unsafe {
250            ffi::gtk_tree_selection_select_all(self.as_ref().to_glib_none().0);
251        }
252    }
253
254    /// Selects the specified iterator.
255    /// ## `iter`
256    /// The [`TreeIter`][crate::TreeIter] to be selected.
257    #[doc(alias = "gtk_tree_selection_select_iter")]
258    fn select_iter(&self, iter: &TreeIter) {
259        unsafe {
260            ffi::gtk_tree_selection_select_iter(
261                self.as_ref().to_glib_none().0,
262                mut_override(iter.to_glib_none().0),
263            );
264        }
265    }
266
267    /// Select the row at `path`.
268    /// ## `path`
269    /// The [`TreePath`][crate::TreePath] to be selected.
270    #[doc(alias = "gtk_tree_selection_select_path")]
271    fn select_path(&self, path: &TreePath) {
272        unsafe {
273            ffi::gtk_tree_selection_select_path(
274                self.as_ref().to_glib_none().0,
275                mut_override(path.to_glib_none().0),
276            );
277        }
278    }
279
280    /// Selects a range of nodes, determined by `start_path` and `end_path` inclusive.
281    /// `self` must be set to [`SelectionMode::Multiple`][crate::SelectionMode::Multiple] mode.
282    /// ## `start_path`
283    /// The initial node of the range.
284    /// ## `end_path`
285    /// The final node of the range.
286    #[doc(alias = "gtk_tree_selection_select_range")]
287    fn select_range(&self, start_path: &TreePath, end_path: &TreePath) {
288        unsafe {
289            ffi::gtk_tree_selection_select_range(
290                self.as_ref().to_glib_none().0,
291                mut_override(start_path.to_glib_none().0),
292                mut_override(end_path.to_glib_none().0),
293            );
294        }
295    }
296
297    /// Calls a function for each selected node. Note that you cannot modify
298    /// the tree or selection from within this function. As a result,
299    /// [`selected_rows()`][Self::selected_rows()] might be more useful.
300    /// ## `func`
301    /// The function to call for each selected node.
302    #[doc(alias = "gtk_tree_selection_selected_foreach")]
303    fn selected_foreach<P: FnMut(&TreeModel, &TreePath, &TreeIter)>(&self, func: P) {
304        let mut func_data: P = func;
305        unsafe extern "C" fn func_func<P: FnMut(&TreeModel, &TreePath, &TreeIter)>(
306            model: *mut ffi::GtkTreeModel,
307            path: *mut ffi::GtkTreePath,
308            iter: *mut ffi::GtkTreeIter,
309            data: glib::ffi::gpointer,
310        ) {
311            unsafe {
312                let model = from_glib_borrow(model);
313                let path = from_glib_borrow(path);
314                let iter = from_glib_borrow(iter);
315                let callback = data as *mut P;
316                (*callback)(&model, &path, &iter)
317            }
318        }
319        let func = Some(func_func::<P> as _);
320        let super_callback0: &mut P = &mut func_data;
321        unsafe {
322            ffi::gtk_tree_selection_selected_foreach(
323                self.as_ref().to_glib_none().0,
324                func,
325                super_callback0 as *mut _ as *mut _,
326            );
327        }
328    }
329
330    /// Sets the selection mode of the `self`. If the previous type was
331    /// [`SelectionMode::Multiple`][crate::SelectionMode::Multiple], then the anchor is kept selected, if it was
332    /// previously selected.
333    /// ## `type_`
334    /// The selection mode
335    #[doc(alias = "gtk_tree_selection_set_mode")]
336    #[doc(alias = "mode")]
337    fn set_mode(&self, type_: SelectionMode) {
338        unsafe {
339            ffi::gtk_tree_selection_set_mode(self.as_ref().to_glib_none().0, type_.into_glib());
340        }
341    }
342
343    /// Sets the selection function.
344    ///
345    /// If set, this function is called before any node is selected or unselected,
346    /// giving some control over which nodes are selected. The select function
347    /// should return [`true`] if the state of the node may be toggled, and [`false`]
348    /// if the state of the node should be left unchanged.
349    /// ## `func`
350    /// The selection function. May be [`None`]
351    #[doc(alias = "gtk_tree_selection_set_select_function")]
352    fn set_select_function(
353        &self,
354        func: Option<Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>>,
355    ) {
356        let func_data: Box_<
357            Option<Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>>,
358        > = Box_::new(func);
359        unsafe extern "C" fn func_func(
360            selection: *mut ffi::GtkTreeSelection,
361            model: *mut ffi::GtkTreeModel,
362            path: *mut ffi::GtkTreePath,
363            path_currently_selected: glib::ffi::gboolean,
364            data: glib::ffi::gpointer,
365        ) -> glib::ffi::gboolean {
366            unsafe {
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 = &*(data as *mut Option<
372                    Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>,
373                >);
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        }
382        let func = if func_data.is_some() {
383            Some(func_func as _)
384        } else {
385            None
386        };
387        unsafe extern "C" fn destroy_func(data: glib::ffi::gpointer) {
388            unsafe {
389                let _callback = Box_::from_raw(
390                    data as *mut Option<
391                        Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>,
392                    >,
393                );
394            }
395        }
396        let destroy_call3 = Some(destroy_func as _);
397        let super_callback0: Box_<
398            Option<Box_<dyn Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static>>,
399        > = func_data;
400        unsafe {
401            ffi::gtk_tree_selection_set_select_function(
402                self.as_ref().to_glib_none().0,
403                func,
404                Box_::into_raw(super_callback0) as *mut _,
405                destroy_call3,
406            );
407        }
408    }
409
410    /// Unselects all the nodes.
411    #[doc(alias = "gtk_tree_selection_unselect_all")]
412    fn unselect_all(&self) {
413        unsafe {
414            ffi::gtk_tree_selection_unselect_all(self.as_ref().to_glib_none().0);
415        }
416    }
417
418    /// Unselects the specified iterator.
419    /// ## `iter`
420    /// The [`TreeIter`][crate::TreeIter] to be unselected.
421    #[doc(alias = "gtk_tree_selection_unselect_iter")]
422    fn unselect_iter(&self, iter: &TreeIter) {
423        unsafe {
424            ffi::gtk_tree_selection_unselect_iter(
425                self.as_ref().to_glib_none().0,
426                mut_override(iter.to_glib_none().0),
427            );
428        }
429    }
430
431    /// Unselects the row at `path`.
432    /// ## `path`
433    /// The [`TreePath`][crate::TreePath] to be unselected.
434    #[doc(alias = "gtk_tree_selection_unselect_path")]
435    fn unselect_path(&self, path: &TreePath) {
436        unsafe {
437            ffi::gtk_tree_selection_unselect_path(
438                self.as_ref().to_glib_none().0,
439                mut_override(path.to_glib_none().0),
440            );
441        }
442    }
443
444    /// Unselects a range of nodes, determined by `start_path` and `end_path`
445    /// inclusive.
446    /// ## `start_path`
447    /// The initial node of the range.
448    /// ## `end_path`
449    /// The initial node of the range.
450    #[doc(alias = "gtk_tree_selection_unselect_range")]
451    fn unselect_range(&self, start_path: &TreePath, end_path: &TreePath) {
452        unsafe {
453            ffi::gtk_tree_selection_unselect_range(
454                self.as_ref().to_glib_none().0,
455                mut_override(start_path.to_glib_none().0),
456                mut_override(end_path.to_glib_none().0),
457            );
458        }
459    }
460
461    /// Emitted whenever the selection has (possibly) changed. Please note that
462    /// this signal is mostly a hint. It may only be emitted once when a range
463    /// of rows are selected, and it may occasionally be emitted when nothing
464    /// has happened.
465    #[doc(alias = "changed")]
466    fn connect_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
467        unsafe extern "C" fn changed_trampoline<P: IsA<TreeSelection>, F: Fn(&P) + 'static>(
468            this: *mut ffi::GtkTreeSelection,
469            f: glib::ffi::gpointer,
470        ) {
471            unsafe {
472                let f: &F = &*(f as *const F);
473                f(TreeSelection::from_glib_borrow(this).unsafe_cast_ref())
474            }
475        }
476        unsafe {
477            let f: Box_<F> = Box_::new(f);
478            connect_raw(
479                self.as_ptr() as *mut _,
480                c"changed".as_ptr(),
481                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
482                    changed_trampoline::<Self, F> as *const (),
483                )),
484                Box_::into_raw(f),
485            )
486        }
487    }
488
489    #[doc(alias = "mode")]
490    fn connect_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
491        unsafe extern "C" fn notify_mode_trampoline<P: IsA<TreeSelection>, F: Fn(&P) + 'static>(
492            this: *mut ffi::GtkTreeSelection,
493            _param_spec: glib::ffi::gpointer,
494            f: glib::ffi::gpointer,
495        ) {
496            unsafe {
497                let f: &F = &*(f as *const F);
498                f(TreeSelection::from_glib_borrow(this).unsafe_cast_ref())
499            }
500        }
501        unsafe {
502            let f: Box_<F> = Box_::new(f);
503            connect_raw(
504                self.as_ptr() as *mut _,
505                c"notify::mode".as_ptr(),
506                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
507                    notify_mode_trampoline::<Self, F> as *const (),
508                )),
509                Box_::into_raw(f),
510            )
511        }
512    }
513}
514
515impl<O: IsA<TreeSelection>> TreeSelectionExt for O {}