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