gtk4/auto/
map_list_model.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
5#[cfg(feature = "v4_12")]
6#[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
7use crate::SectionModel;
8use crate::ffi;
9use glib::{
10    prelude::*,
11    signal::{SignalHandlerId, connect_raw},
12    translate::*,
13};
14use std::boxed::Box as Box_;
15
16#[cfg(feature = "v4_12")]
17#[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
18glib::wrapper! {
19    /// A list model that maps the items in another model to different items.
20    ///
21    /// [`MapListModel`][crate::MapListModel] uses a `callback::Gtk::MapListModelMapFunc.
22    ///
23    /// Example: Create a list of `GtkEventControllers`
24    /// **⚠️ The following code is in c ⚠️**
25    ///
26    /// ```c
27    /// static gpointer
28    /// map_to_controllers (gpointer widget,
29    ///                     gpointer data)
30    /// {
31    ///   gpointer result = gtk_widget_observe_controllers (widget);
32    ///   g_object_unref (widget);
33    ///   return result;
34    /// }
35    ///
36    /// widgets = gtk_widget_observe_children (widget);
37    ///
38    /// controllers = gtk_map_list_model_new (widgets,
39    ///                                       map_to_controllers,
40    ///                                       NULL, NULL);
41    ///
42    /// model = gtk_flatten_list_model_new (GTK_TYPE_EVENT_CONTROLLER,
43    ///                                     controllers);
44    /// ```
45    ///
46    /// [`MapListModel`][crate::MapListModel] will attempt to discard the mapped objects as soon as
47    /// they are no longer needed and recreate them if necessary.
48    ///
49    /// [`MapListModel`][crate::MapListModel] passes through sections from the underlying model.
50    ///
51    /// ## Properties
52    ///
53    ///
54    /// #### `has-map`
55    ///  If a map is set for this model
56    ///
57    /// Readable
58    ///
59    ///
60    /// #### `item-type`
61    ///  The type of items. See [`ListModelExtManual::item_type()`][crate::gio::prelude::ListModelExtManual::item_type()].
62    ///
63    /// Readable
64    ///
65    ///
66    /// #### `model`
67    ///  The model being mapped.
68    ///
69    /// Readable | Writeable
70    ///
71    ///
72    /// #### `n-items`
73    ///  The number of items. See [`ListModelExtManual::n_items()`][crate::gio::prelude::ListModelExtManual::n_items()].
74    ///
75    /// Readable
76    ///
77    /// # Implements
78    ///
79    /// [`trait@glib::ObjectExt`], [`trait@gio::prelude::ListModelExt`], [`SectionModelExt`][trait@crate::prelude::SectionModelExt]
80    #[doc(alias = "GtkMapListModel")]
81    pub struct MapListModel(Object<ffi::GtkMapListModel, ffi::GtkMapListModelClass>) @implements gio::ListModel, SectionModel;
82
83    match fn {
84        type_ => || ffi::gtk_map_list_model_get_type(),
85    }
86}
87
88#[cfg(not(any(feature = "v4_12")))]
89glib::wrapper! {
90    #[doc(alias = "GtkMapListModel")]
91    pub struct MapListModel(Object<ffi::GtkMapListModel, ffi::GtkMapListModelClass>) @implements gio::ListModel;
92
93    match fn {
94        type_ => || ffi::gtk_map_list_model_get_type(),
95    }
96}
97
98impl MapListModel {
99    /// Creates a new [`MapListModel`][crate::MapListModel] for the given arguments.
100    /// ## `model`
101    /// The model to map
102    /// ## `map_func`
103    /// map function
104    ///
105    /// # Returns
106    ///
107    /// a new [`MapListModel`][crate::MapListModel]
108    #[doc(alias = "gtk_map_list_model_new")]
109    pub fn new<P: Fn(&glib::Object) -> glib::Object + 'static>(
110        model: Option<impl IsA<gio::ListModel>>,
111        map_func: P,
112    ) -> MapListModel {
113        assert_initialized_main_thread!();
114        let map_func_data: Box_<P> = Box_::new(map_func);
115        unsafe extern "C" fn map_func_func<P: Fn(&glib::Object) -> glib::Object + 'static>(
116            item: *mut glib::gobject_ffi::GObject,
117            user_data: glib::ffi::gpointer,
118        ) -> *mut glib::gobject_ffi::GObject {
119            unsafe {
120                let item = from_glib_full(item);
121                let callback = &*(user_data as *mut P);
122                (*callback)(&item).to_glib_full()
123            }
124        }
125        let map_func = Some(map_func_func::<P> as _);
126        unsafe extern "C" fn user_destroy_func<P: Fn(&glib::Object) -> glib::Object + 'static>(
127            data: glib::ffi::gpointer,
128        ) {
129            unsafe {
130                let _callback = Box_::from_raw(data as *mut P);
131            }
132        }
133        let destroy_call3 = Some(user_destroy_func::<P> as _);
134        let super_callback0: Box_<P> = map_func_data;
135        unsafe {
136            from_glib_full(ffi::gtk_map_list_model_new(
137                model.map(|p| p.upcast()).into_glib_ptr(),
138                map_func,
139                Box_::into_raw(super_callback0) as *mut _,
140                destroy_call3,
141            ))
142        }
143    }
144
145    /// Gets the model that is currently being mapped or [`None`] if none.
146    ///
147    /// # Returns
148    ///
149    /// The model that gets mapped
150    #[doc(alias = "gtk_map_list_model_get_model")]
151    #[doc(alias = "get_model")]
152    pub fn model(&self) -> Option<gio::ListModel> {
153        unsafe { from_glib_none(ffi::gtk_map_list_model_get_model(self.to_glib_none().0)) }
154    }
155
156    /// Checks if a map function is currently set on @self.
157    ///
158    /// # Returns
159    ///
160    /// [`true`] if a map function is set
161    #[doc(alias = "gtk_map_list_model_has_map")]
162    #[doc(alias = "has-map")]
163    pub fn has_map(&self) -> bool {
164        unsafe { from_glib(ffi::gtk_map_list_model_has_map(self.to_glib_none().0)) }
165    }
166
167    /// Sets the function used to map items.
168    ///
169    /// The function will be called whenever an item needs to be mapped
170    /// and must return the item to use for the given input item.
171    ///
172    /// Note that [`MapListModel`][crate::MapListModel] may call this function multiple times
173    /// on the same item, because it may delete items it doesn't need anymore.
174    ///
175    /// GTK makes no effort to ensure that @map_func conforms to the item type
176    /// of @self. It assumes that the caller knows what they are doing and the map
177    /// function returns items of the appropriate type.
178    /// ## `map_func`
179    /// map function
180    #[doc(alias = "gtk_map_list_model_set_map_func")]
181    pub fn set_map_func<P: Fn(&glib::Object) -> glib::Object + 'static>(&self, map_func: P) {
182        let map_func_data: Box_<P> = Box_::new(map_func);
183        unsafe extern "C" fn map_func_func<P: Fn(&glib::Object) -> glib::Object + 'static>(
184            item: *mut glib::gobject_ffi::GObject,
185            user_data: glib::ffi::gpointer,
186        ) -> *mut glib::gobject_ffi::GObject {
187            unsafe {
188                let item = from_glib_full(item);
189                let callback = &*(user_data as *mut P);
190                (*callback)(&item).to_glib_full()
191            }
192        }
193        let map_func = Some(map_func_func::<P> as _);
194        unsafe extern "C" fn user_destroy_func<P: Fn(&glib::Object) -> glib::Object + 'static>(
195            data: glib::ffi::gpointer,
196        ) {
197            unsafe {
198                let _callback = Box_::from_raw(data as *mut P);
199            }
200        }
201        let destroy_call3 = Some(user_destroy_func::<P> as _);
202        let super_callback0: Box_<P> = map_func_data;
203        unsafe {
204            ffi::gtk_map_list_model_set_map_func(
205                self.to_glib_none().0,
206                map_func,
207                Box_::into_raw(super_callback0) as *mut _,
208                destroy_call3,
209            );
210        }
211    }
212
213    /// Sets the model to be mapped.
214    ///
215    /// GTK makes no effort to ensure that @model conforms to the item type
216    /// expected by the map function. It assumes that the caller knows what
217    /// they are doing and have set up an appropriate map function.
218    /// ## `model`
219    /// The model to be mapped
220    #[doc(alias = "gtk_map_list_model_set_model")]
221    #[doc(alias = "model")]
222    pub fn set_model(&self, model: Option<&impl IsA<gio::ListModel>>) {
223        unsafe {
224            ffi::gtk_map_list_model_set_model(
225                self.to_glib_none().0,
226                model.map(|p| p.as_ref()).to_glib_none().0,
227            );
228        }
229    }
230
231    #[doc(alias = "has-map")]
232    pub fn connect_has_map_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
233        unsafe extern "C" fn notify_has_map_trampoline<F: Fn(&MapListModel) + 'static>(
234            this: *mut ffi::GtkMapListModel,
235            _param_spec: glib::ffi::gpointer,
236            f: glib::ffi::gpointer,
237        ) {
238            unsafe {
239                let f: &F = &*(f as *const F);
240                f(&from_glib_borrow(this))
241            }
242        }
243        unsafe {
244            let f: Box_<F> = Box_::new(f);
245            connect_raw(
246                self.as_ptr() as *mut _,
247                c"notify::has-map".as_ptr() as *const _,
248                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
249                    notify_has_map_trampoline::<F> as *const (),
250                )),
251                Box_::into_raw(f),
252            )
253        }
254    }
255
256    #[doc(alias = "model")]
257    pub fn connect_model_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
258        unsafe extern "C" fn notify_model_trampoline<F: Fn(&MapListModel) + 'static>(
259            this: *mut ffi::GtkMapListModel,
260            _param_spec: glib::ffi::gpointer,
261            f: glib::ffi::gpointer,
262        ) {
263            unsafe {
264                let f: &F = &*(f as *const F);
265                f(&from_glib_borrow(this))
266            }
267        }
268        unsafe {
269            let f: Box_<F> = Box_::new(f);
270            connect_raw(
271                self.as_ptr() as *mut _,
272                c"notify::model".as_ptr() as *const _,
273                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
274                    notify_model_trampoline::<F> as *const (),
275                )),
276                Box_::into_raw(f),
277            )
278        }
279    }
280}