Skip to main content

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::{SelectionMode, TreeIter, TreeModel, TreePath, TreeView, ffi};
7use glib::{
8    object::ObjectType as _,
9    prelude::*,
10    signal::{SignalHandlerId, connect_raw},
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            unsafe {
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        }
354        let func = Some(func_func::<P> as _);
355        let super_callback0: &mut P = &mut func_data;
356        unsafe {
357            ffi::gtk_tree_selection_selected_foreach(
358                self.to_glib_none().0,
359                func,
360                super_callback0 as *mut _ as *mut _,
361            );
362        }
363    }
364
365    /// Sets the selection mode of the @self.  If the previous type was
366    /// [`SelectionMode::Multiple`][crate::SelectionMode::Multiple], then the anchor is kept selected, if it was
367    /// previously selected.
368    ///
369    /// # Deprecated since 4.10
370    ///
371    /// Use GtkListView or GtkColumnView
372    /// ## `type_`
373    /// The selection mode
374    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
375    #[allow(deprecated)]
376    #[doc(alias = "gtk_tree_selection_set_mode")]
377    #[doc(alias = "mode")]
378    pub fn set_mode(&self, type_: SelectionMode) {
379        unsafe {
380            ffi::gtk_tree_selection_set_mode(self.to_glib_none().0, type_.into_glib());
381        }
382    }
383
384    /// Sets the selection function.
385    ///
386    /// If set, this function is called before any node is selected or unselected,
387    /// giving some control over which nodes are selected. The select function
388    /// should return [`true`] if the state of the node may be toggled, and [`false`]
389    /// if the state of the node should be left unchanged.
390    ///
391    /// # Deprecated since 4.10
392    ///
393    /// Use GtkListView or GtkColumnView
394    /// ## `func`
395    /// The selection function. May be [`None`]
396    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
397    #[allow(deprecated)]
398    #[doc(alias = "gtk_tree_selection_set_select_function")]
399    pub fn set_select_function<
400        P: Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static,
401    >(
402        &self,
403        func: P,
404    ) {
405        let func_data: Box_<P> = Box_::new(func);
406        unsafe extern "C" fn func_func<
407            P: Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static,
408        >(
409            selection: *mut ffi::GtkTreeSelection,
410            model: *mut ffi::GtkTreeModel,
411            path: *mut ffi::GtkTreePath,
412            path_currently_selected: glib::ffi::gboolean,
413            data: glib::ffi::gpointer,
414        ) -> glib::ffi::gboolean {
415            unsafe {
416                let selection = from_glib_borrow(selection);
417                let model = from_glib_borrow(model);
418                let path = from_glib_borrow(path);
419                let path_currently_selected = from_glib(path_currently_selected);
420                let callback = &*(data as *mut P);
421                (*callback)(&selection, &model, &path, path_currently_selected).into_glib()
422            }
423        }
424        let func = Some(func_func::<P> as _);
425        unsafe extern "C" fn destroy_func<
426            P: Fn(&TreeSelection, &TreeModel, &TreePath, bool) -> bool + 'static,
427        >(
428            data: glib::ffi::gpointer,
429        ) {
430            unsafe {
431                let _callback = Box_::from_raw(data as *mut P);
432            }
433        }
434        let destroy_call3 = Some(destroy_func::<P> as _);
435        let super_callback0: Box_<P> = func_data;
436        unsafe {
437            ffi::gtk_tree_selection_set_select_function(
438                self.to_glib_none().0,
439                func,
440                Box_::into_raw(super_callback0) as *mut _,
441                destroy_call3,
442            );
443        }
444    }
445
446    /// Unselects all the nodes.
447    ///
448    /// # Deprecated since 4.10
449    ///
450    /// Use GtkListView or GtkColumnView
451    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
452    #[allow(deprecated)]
453    #[doc(alias = "gtk_tree_selection_unselect_all")]
454    pub fn unselect_all(&self) {
455        unsafe {
456            ffi::gtk_tree_selection_unselect_all(self.to_glib_none().0);
457        }
458    }
459
460    /// Unselects the specified iterator.
461    ///
462    /// # Deprecated since 4.10
463    ///
464    /// Use GtkListView or GtkColumnView
465    /// ## `iter`
466    /// The [`TreeIter`][crate::TreeIter] to be unselected.
467    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
468    #[allow(deprecated)]
469    #[doc(alias = "gtk_tree_selection_unselect_iter")]
470    pub fn unselect_iter(&self, iter: &TreeIter) {
471        unsafe {
472            ffi::gtk_tree_selection_unselect_iter(
473                self.to_glib_none().0,
474                mut_override(iter.to_glib_none().0),
475            );
476        }
477    }
478
479    /// Unselects the row at @path.
480    ///
481    /// # Deprecated since 4.10
482    ///
483    /// Use GtkListView or GtkColumnView
484    /// ## `path`
485    /// The [`TreePath`][crate::TreePath] to be unselected.
486    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
487    #[allow(deprecated)]
488    #[doc(alias = "gtk_tree_selection_unselect_path")]
489    pub fn unselect_path(&self, path: &TreePath) {
490        unsafe {
491            ffi::gtk_tree_selection_unselect_path(
492                self.to_glib_none().0,
493                mut_override(path.to_glib_none().0),
494            );
495        }
496    }
497
498    /// Unselects a range of nodes, determined by @start_path and @end_path
499    /// inclusive.
500    ///
501    /// # Deprecated since 4.10
502    ///
503    /// Use GtkListView or GtkColumnView
504    /// ## `start_path`
505    /// The initial node of the range.
506    /// ## `end_path`
507    /// The initial node of the range.
508    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
509    #[allow(deprecated)]
510    #[doc(alias = "gtk_tree_selection_unselect_range")]
511    pub fn unselect_range(&self, start_path: &TreePath, end_path: &TreePath) {
512        unsafe {
513            ffi::gtk_tree_selection_unselect_range(
514                self.to_glib_none().0,
515                mut_override(start_path.to_glib_none().0),
516                mut_override(end_path.to_glib_none().0),
517            );
518        }
519    }
520
521    /// Emitted whenever the selection has (possibly) changed. Please note that
522    /// this signal is mostly a hint.  It may only be emitted once when a range
523    /// of rows are selected, and it may occasionally be emitted when nothing
524    /// has happened.
525    #[doc(alias = "changed")]
526    pub fn connect_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
527        unsafe extern "C" fn changed_trampoline<F: Fn(&TreeSelection) + 'static>(
528            this: *mut ffi::GtkTreeSelection,
529            f: glib::ffi::gpointer,
530        ) {
531            unsafe {
532                let f: &F = &*(f as *const F);
533                f(&from_glib_borrow(this))
534            }
535        }
536        unsafe {
537            let f: Box_<F> = Box_::new(f);
538            connect_raw(
539                self.as_ptr() as *mut _,
540                c"changed".as_ptr() as *const _,
541                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
542                    changed_trampoline::<F> as *const (),
543                )),
544                Box_::into_raw(f),
545            )
546        }
547    }
548
549    #[doc(alias = "mode")]
550    pub fn connect_mode_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
551        unsafe extern "C" fn notify_mode_trampoline<F: Fn(&TreeSelection) + 'static>(
552            this: *mut ffi::GtkTreeSelection,
553            _param_spec: glib::ffi::gpointer,
554            f: glib::ffi::gpointer,
555        ) {
556            unsafe {
557                let f: &F = &*(f as *const F);
558                f(&from_glib_borrow(this))
559            }
560        }
561        unsafe {
562            let f: Box_<F> = Box_::new(f);
563            connect_raw(
564                self.as_ptr() as *mut _,
565                c"notify::mode".as_ptr() as *const _,
566                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
567                    notify_mode_trampoline::<F> as *const (),
568                )),
569                Box_::into_raw(f),
570            )
571        }
572    }
573}