gtk4/auto/tree_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::{TreeListRow, ffi};
6use glib::{
7 prelude::*,
8 signal::{SignalHandlerId, connect_raw},
9 translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14 /// A list model that can create child models on demand.
15 ///
16 /// ## Properties
17 ///
18 ///
19 /// #### `autoexpand`
20 /// If all rows should be expanded by default.
21 ///
22 /// Readable | Writeable
23 ///
24 ///
25 /// #### `item-type`
26 /// The type of items. See [`ListModelExtManual::item_type()`][crate::gio::prelude::ListModelExtManual::item_type()].
27 ///
28 /// Readable
29 ///
30 ///
31 /// #### `model`
32 /// The root model displayed.
33 ///
34 /// Readable
35 ///
36 ///
37 /// #### `n-items`
38 /// The number of items. See [`ListModelExtManual::n_items()`][crate::gio::prelude::ListModelExtManual::n_items()].
39 ///
40 /// Readable
41 ///
42 ///
43 /// #### `passthrough`
44 /// Gets whether the model is in passthrough mode.
45 ///
46 /// If [`false`], the `GListModel` functions for this object return custom
47 /// [`TreeListRow`][crate::TreeListRow] objects. If [`true`], the values of the child
48 /// models are pass through unmodified.
49 ///
50 /// Readable | Writeable | Construct Only
51 ///
52 /// # Implements
53 ///
54 /// [`trait@glib::ObjectExt`], [`trait@gio::prelude::ListModelExt`]
55 #[doc(alias = "GtkTreeListModel")]
56 pub struct TreeListModel(Object<ffi::GtkTreeListModel, ffi::GtkTreeListModelClass>) @implements gio::ListModel;
57
58 match fn {
59 type_ => || ffi::gtk_tree_list_model_get_type(),
60 }
61}
62
63impl TreeListModel {
64 /// Creates a new empty [`TreeListModel`][crate::TreeListModel] displaying @root
65 /// with all rows collapsed.
66 /// ## `root`
67 /// The `GListModel` to use as root
68 /// ## `passthrough`
69 /// [`true`] to pass through items from the models
70 /// ## `autoexpand`
71 /// [`true`] to set the autoexpand property and expand the @root model
72 /// ## `create_func`
73 /// function to
74 /// call to create the `GListModel` for the children of an item
75 ///
76 /// # Returns
77 ///
78 /// a newly created [`TreeListModel`][crate::TreeListModel].
79 #[doc(alias = "gtk_tree_list_model_new")]
80 pub fn new<P: Fn(&glib::Object) -> Option<gio::ListModel> + 'static>(
81 root: impl IsA<gio::ListModel>,
82 passthrough: bool,
83 autoexpand: bool,
84 create_func: P,
85 ) -> TreeListModel {
86 assert_initialized_main_thread!();
87 let create_func_data: Box_<P> = Box_::new(create_func);
88 unsafe extern "C" fn create_func_func<
89 P: Fn(&glib::Object) -> Option<gio::ListModel> + 'static,
90 >(
91 item: *mut glib::gobject_ffi::GObject,
92 user_data: glib::ffi::gpointer,
93 ) -> *mut gio::ffi::GListModel {
94 unsafe {
95 let item = from_glib_borrow(item);
96 let callback = &*(user_data as *mut P);
97 (*callback)(&item).to_glib_full()
98 }
99 }
100 let create_func = Some(create_func_func::<P> as _);
101 unsafe extern "C" fn user_destroy_func<
102 P: Fn(&glib::Object) -> Option<gio::ListModel> + 'static,
103 >(
104 data: glib::ffi::gpointer,
105 ) {
106 unsafe {
107 let _callback = Box_::from_raw(data as *mut P);
108 }
109 }
110 let destroy_call5 = Some(user_destroy_func::<P> as _);
111 let super_callback0: Box_<P> = create_func_data;
112 unsafe {
113 from_glib_full(ffi::gtk_tree_list_model_new(
114 root.upcast().into_glib_ptr(),
115 passthrough.into_glib(),
116 autoexpand.into_glib(),
117 create_func,
118 Box_::into_raw(super_callback0) as *mut _,
119 destroy_call5,
120 ))
121 }
122 }
123
124 /// Gets whether the model is set to automatically expand new rows
125 /// that get added.
126 ///
127 /// This can be either rows added by changes to the underlying
128 /// models or via [`TreeListRow::set_expanded()`][crate::TreeListRow::set_expanded()].
129 ///
130 /// # Returns
131 ///
132 /// [`true`] if the model is set to autoexpand
133 #[doc(alias = "gtk_tree_list_model_get_autoexpand")]
134 #[doc(alias = "get_autoexpand")]
135 #[doc(alias = "autoexpand")]
136 pub fn is_autoexpand(&self) -> bool {
137 unsafe {
138 from_glib(ffi::gtk_tree_list_model_get_autoexpand(
139 self.to_glib_none().0,
140 ))
141 }
142 }
143
144 /// Gets the row item corresponding to the child at index @position for
145 /// @self's root model.
146 ///
147 /// If @position is greater than the number of children in the root model,
148 /// [`None`] is returned.
149 ///
150 /// Do not confuse this function with [`row()`][Self::row()].
151 /// ## `position`
152 /// position of the child to get
153 ///
154 /// # Returns
155 ///
156 /// the child in @position
157 #[doc(alias = "gtk_tree_list_model_get_child_row")]
158 #[doc(alias = "get_child_row")]
159 pub fn child_row(&self, position: u32) -> Option<TreeListRow> {
160 unsafe {
161 from_glib_full(ffi::gtk_tree_list_model_get_child_row(
162 self.to_glib_none().0,
163 position,
164 ))
165 }
166 }
167
168 /// Gets the root model that @self was created with.
169 ///
170 /// # Returns
171 ///
172 /// the root model
173 #[doc(alias = "gtk_tree_list_model_get_model")]
174 #[doc(alias = "get_model")]
175 pub fn model(&self) -> gio::ListModel {
176 unsafe { from_glib_none(ffi::gtk_tree_list_model_get_model(self.to_glib_none().0)) }
177 }
178
179 /// Gets whether the model is passing through original row items.
180 ///
181 /// If this function returns [`false`], the `GListModel` functions for @self
182 /// return custom [`TreeListRow`][crate::TreeListRow] objects. You need to call
183 /// [`TreeListRow::item()`][crate::TreeListRow::item()] on these objects to get the original
184 /// item.
185 ///
186 /// If [`true`], the values of the child models are passed through in their
187 /// original state. You then need to call [`row()`][Self::row()]
188 /// to get the custom [`TreeListRow`][crate::TreeListRow]s.
189 ///
190 /// # Returns
191 ///
192 /// [`true`] if the model is passing through original row items
193 #[doc(alias = "gtk_tree_list_model_get_passthrough")]
194 #[doc(alias = "get_passthrough")]
195 #[doc(alias = "passthrough")]
196 pub fn is_passthrough(&self) -> bool {
197 unsafe {
198 from_glib(ffi::gtk_tree_list_model_get_passthrough(
199 self.to_glib_none().0,
200 ))
201 }
202 }
203
204 /// Gets the row object for the given row.
205 ///
206 /// If @position is greater than the number of items in @self,
207 /// [`None`] is returned.
208 ///
209 /// The row object can be used to expand and collapse rows as
210 /// well as to inspect its position in the tree. See its
211 /// documentation for details.
212 ///
213 /// This row object is persistent and will refer to the current
214 /// item as long as the row is present in @self, independent of
215 /// other rows being added or removed.
216 ///
217 /// If @self is set to not be passthrough, this function is
218 /// equivalent to calling g_list_model_get_item().
219 ///
220 /// Do not confuse this function with [`child_row()`][Self::child_row()].
221 /// ## `position`
222 /// the position of the row to fetch
223 ///
224 /// # Returns
225 ///
226 /// The row item
227 #[doc(alias = "gtk_tree_list_model_get_row")]
228 #[doc(alias = "get_row")]
229 pub fn row(&self, position: u32) -> Option<TreeListRow> {
230 unsafe {
231 from_glib_full(ffi::gtk_tree_list_model_get_row(
232 self.to_glib_none().0,
233 position,
234 ))
235 }
236 }
237
238 /// Sets whether the model should autoexpand.
239 ///
240 /// If set to [`true`], the model will recursively expand all rows that
241 /// get added to the model. This can be either rows added by changes
242 /// to the underlying models or via [`TreeListRow::set_expanded()`][crate::TreeListRow::set_expanded()].
243 /// ## `autoexpand`
244 /// [`true`] to make the model autoexpand its rows
245 #[doc(alias = "gtk_tree_list_model_set_autoexpand")]
246 #[doc(alias = "autoexpand")]
247 pub fn set_autoexpand(&self, autoexpand: bool) {
248 unsafe {
249 ffi::gtk_tree_list_model_set_autoexpand(self.to_glib_none().0, autoexpand.into_glib());
250 }
251 }
252
253 #[doc(alias = "autoexpand")]
254 pub fn connect_autoexpand_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
255 unsafe extern "C" fn notify_autoexpand_trampoline<F: Fn(&TreeListModel) + 'static>(
256 this: *mut ffi::GtkTreeListModel,
257 _param_spec: glib::ffi::gpointer,
258 f: glib::ffi::gpointer,
259 ) {
260 unsafe {
261 let f: &F = &*(f as *const F);
262 f(&from_glib_borrow(this))
263 }
264 }
265 unsafe {
266 let f: Box_<F> = Box_::new(f);
267 connect_raw(
268 self.as_ptr() as *mut _,
269 c"notify::autoexpand".as_ptr() as *const _,
270 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
271 notify_autoexpand_trampoline::<F> as *const (),
272 )),
273 Box_::into_raw(f),
274 )
275 }
276 }
277
278 #[doc(alias = "model")]
279 pub fn connect_model_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
280 unsafe extern "C" fn notify_model_trampoline<F: Fn(&TreeListModel) + 'static>(
281 this: *mut ffi::GtkTreeListModel,
282 _param_spec: glib::ffi::gpointer,
283 f: glib::ffi::gpointer,
284 ) {
285 unsafe {
286 let f: &F = &*(f as *const F);
287 f(&from_glib_borrow(this))
288 }
289 }
290 unsafe {
291 let f: Box_<F> = Box_::new(f);
292 connect_raw(
293 self.as_ptr() as *mut _,
294 c"notify::model".as_ptr() as *const _,
295 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
296 notify_model_trampoline::<F> as *const (),
297 )),
298 Box_::into_raw(f),
299 )
300 }
301 }
302}