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