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
5use crate::ffi;
6#[cfg(feature = "v4_12")]
7#[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
8use crate::SectionModel;
9use glib::{
10 prelude::*,
11 signal::{connect_raw, SignalHandlerId},
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 [`MapListModel`][crate::MapListModel] maps the items in a list 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 | Construct Only
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 let item = from_glib_full(item);
120 let callback = &*(user_data as *mut P);
121 (*callback)(&item).to_glib_full()
122 }
123 let map_func = Some(map_func_func::<P> as _);
124 unsafe extern "C" fn user_destroy_func<P: Fn(&glib::Object) -> glib::Object + 'static>(
125 data: glib::ffi::gpointer,
126 ) {
127 let _callback = Box_::from_raw(data as *mut P);
128 }
129 let destroy_call3 = Some(user_destroy_func::<P> as _);
130 let super_callback0: Box_<P> = map_func_data;
131 unsafe {
132 from_glib_full(ffi::gtk_map_list_model_new(
133 model.map(|p| p.upcast()).into_glib_ptr(),
134 map_func,
135 Box_::into_raw(super_callback0) as *mut _,
136 destroy_call3,
137 ))
138 }
139 }
140
141 /// Gets the model that is currently being mapped or [`None`] if none.
142 ///
143 /// # Returns
144 ///
145 /// The model that gets mapped
146 #[doc(alias = "gtk_map_list_model_get_model")]
147 #[doc(alias = "get_model")]
148 pub fn model(&self) -> Option<gio::ListModel> {
149 unsafe { from_glib_none(ffi::gtk_map_list_model_get_model(self.to_glib_none().0)) }
150 }
151
152 /// Checks if a map function is currently set on @self.
153 ///
154 /// # Returns
155 ///
156 /// [`true`] if a map function is set
157 #[doc(alias = "gtk_map_list_model_has_map")]
158 #[doc(alias = "has-map")]
159 pub fn has_map(&self) -> bool {
160 unsafe { from_glib(ffi::gtk_map_list_model_has_map(self.to_glib_none().0)) }
161 }
162
163 /// Sets the function used to map items.
164 ///
165 /// The function will be called whenever an item needs to be mapped
166 /// and must return the item to use for the given input item.
167 ///
168 /// Note that [`MapListModel`][crate::MapListModel] may call this function multiple times
169 /// on the same item, because it may delete items it doesn't need anymore.
170 ///
171 /// GTK makes no effort to ensure that @map_func conforms to the item type
172 /// of @self. It assumes that the caller knows what they are doing and the map
173 /// function returns items of the appropriate type.
174 /// ## `map_func`
175 /// map function
176 #[doc(alias = "gtk_map_list_model_set_map_func")]
177 pub fn set_map_func<P: Fn(&glib::Object) -> glib::Object + 'static>(&self, map_func: P) {
178 let map_func_data: Box_<P> = Box_::new(map_func);
179 unsafe extern "C" fn map_func_func<P: Fn(&glib::Object) -> glib::Object + 'static>(
180 item: *mut glib::gobject_ffi::GObject,
181 user_data: glib::ffi::gpointer,
182 ) -> *mut glib::gobject_ffi::GObject {
183 let item = from_glib_full(item);
184 let callback = &*(user_data as *mut P);
185 (*callback)(&item).to_glib_full()
186 }
187 let map_func = Some(map_func_func::<P> as _);
188 unsafe extern "C" fn user_destroy_func<P: Fn(&glib::Object) -> glib::Object + 'static>(
189 data: glib::ffi::gpointer,
190 ) {
191 let _callback = Box_::from_raw(data as *mut P);
192 }
193 let destroy_call3 = Some(user_destroy_func::<P> as _);
194 let super_callback0: Box_<P> = map_func_data;
195 unsafe {
196 ffi::gtk_map_list_model_set_map_func(
197 self.to_glib_none().0,
198 map_func,
199 Box_::into_raw(super_callback0) as *mut _,
200 destroy_call3,
201 );
202 }
203 }
204
205 /// Sets the model to be mapped.
206 ///
207 /// GTK makes no effort to ensure that @model conforms to the item type
208 /// expected by the map function. It assumes that the caller knows what
209 /// they are doing and have set up an appropriate map function.
210 /// ## `model`
211 /// The model to be mapped
212 #[doc(alias = "gtk_map_list_model_set_model")]
213 pub fn set_model(&self, model: Option<&impl IsA<gio::ListModel>>) {
214 unsafe {
215 ffi::gtk_map_list_model_set_model(
216 self.to_glib_none().0,
217 model.map(|p| p.as_ref()).to_glib_none().0,
218 );
219 }
220 }
221
222 #[doc(alias = "has-map")]
223 pub fn connect_has_map_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
224 unsafe extern "C" fn notify_has_map_trampoline<F: Fn(&MapListModel) + 'static>(
225 this: *mut ffi::GtkMapListModel,
226 _param_spec: glib::ffi::gpointer,
227 f: glib::ffi::gpointer,
228 ) {
229 let f: &F = &*(f as *const F);
230 f(&from_glib_borrow(this))
231 }
232 unsafe {
233 let f: Box_<F> = Box_::new(f);
234 connect_raw(
235 self.as_ptr() as *mut _,
236 b"notify::has-map\0".as_ptr() as *const _,
237 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
238 notify_has_map_trampoline::<F> as *const (),
239 )),
240 Box_::into_raw(f),
241 )
242 }
243 }
244}