Skip to main content

gtk4/
tree_model_filter.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::boxed::Box as Box_;
4
5use glib::translate::*;
6
7use crate::{TreeIter, TreeModel, TreeModelFilter, TreePath, ffi, prelude::*};
8
9impl TreeModelFilter {
10    #[doc(alias = "gtk_tree_model_filter_new")]
11    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
12    #[allow(deprecated)]
13    pub fn new(child_model: &impl IsA<TreeModel>, root: Option<&TreePath>) -> Self {
14        skip_assert_initialized!();
15        unsafe {
16            TreeModel::from_glib_none(ffi::gtk_tree_model_filter_new(
17                child_model.as_ref().to_glib_none().0,
18                mut_override(root.to_glib_none().0),
19            ))
20            .unsafe_cast()
21        }
22    }
23}
24
25// rustdoc-stripper-ignore-next
26/// Trait containing manually implemented methods of
27/// [`TreeModelFilter`](crate::TreeModelFilter).
28#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
29#[allow(deprecated)]
30pub trait TreeModelFilterExtManual: IsA<TreeModelFilter> + 'static {
31    /// With the @n_columns and @types parameters, you give an array of column
32    /// types for this model (which will be exposed to the parent model/view).
33    /// The @func, @data and @destroy parameters are for specifying the modify
34    /// function. The modify function will get called for each
35    /// data access, the goal of the modify function is to return the data which
36    /// should be displayed at the location specified using the parameters of the
37    /// modify function.
38    ///
39    /// Note that gtk_tree_model_filter_set_modify_func()
40    /// can only be called once for a given filter model.
41    ///
42    /// # Deprecated since 4.10
43    ///
44    /// ## `types`
45    /// The `GType`s of the columns.
46    /// ## `func`
47    /// A `GtkTreeModelFilterModifyFunc`
48    #[doc(alias = "gtk_tree_model_filter_set_modify_func")]
49    fn set_modify_func<F: Fn(&TreeModel, &TreeIter, i32) -> glib::Value + 'static>(
50        &self,
51        types: &[glib::Type],
52        func: F,
53    ) {
54        unsafe {
55            let types_ptr: Vec<glib::ffi::GType> = types.iter().map(|t| t.into_glib()).collect();
56
57            unsafe extern "C" fn func_trampoline<
58                F: Fn(&TreeModel, &TreeIter, i32) -> glib::Value + 'static,
59            >(
60                model: *mut ffi::GtkTreeModel,
61                iter: *mut ffi::GtkTreeIter,
62                value: *mut glib::gobject_ffi::GValue,
63                column: i32,
64                user_data: glib::ffi::gpointer,
65            ) {
66                unsafe {
67                    let f: &F = &*(user_data as *const F);
68                    let ret = f(&from_glib_borrow(model), &from_glib_borrow(iter), column);
69                    *value = ret.into_raw();
70                }
71            }
72
73            unsafe extern "C" fn destroy_func<
74                F: Fn(&TreeModel, &TreeIter, i32) -> glib::Value + 'static,
75            >(
76                user_data: glib::ffi::gpointer,
77            ) {
78                unsafe {
79                    let _callback: Box_<Option<Box_<F>>> = Box_::from_raw(user_data as *mut _);
80                }
81            }
82            let callback_data: Box_<F> = Box_::new(func);
83
84            ffi::gtk_tree_model_filter_set_modify_func(
85                self.as_ref().to_glib_none().0,
86                types.len() as i32,
87                mut_override(types_ptr.as_ptr()),
88                Some(func_trampoline::<F> as _),
89                Box_::into_raw(callback_data) as *mut _,
90                Some(destroy_func::<F> as _),
91            )
92        }
93    }
94}
95
96impl<O: IsA<TreeModelFilter>> TreeModelFilterExtManual for O {}