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::{ffi, TreeListRow};
6use glib::{
7 prelude::*,
8 signal::{connect_raw, SignalHandlerId},
9 translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14 /// [`TreeListModel`][crate::TreeListModel] is 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 let item = from_glib_borrow(item);
95 let callback = &*(user_data as *mut P);
96 (*callback)(&item).to_glib_full()
97 }
98 let create_func = Some(create_func_func::<P> as _);
99 unsafe extern "C" fn user_destroy_func<
100 P: Fn(&glib::Object) -> Option<gio::ListModel> + 'static,
101 >(
102 data: glib::ffi::gpointer,
103 ) {
104 let _callback = Box_::from_raw(data as *mut P);
105 }
106 let destroy_call5 = Some(user_destroy_func::<P> as _);
107 let super_callback0: Box_<P> = create_func_data;
108 unsafe {
109 from_glib_full(ffi::gtk_tree_list_model_new(
110 root.upcast().into_glib_ptr(),
111 passthrough.into_glib(),
112 autoexpand.into_glib(),
113 create_func,
114 Box_::into_raw(super_callback0) as *mut _,
115 destroy_call5,
116 ))
117 }
118 }
119
120 /// Gets whether the model is set to automatically expand new rows
121 /// that get added.
122 ///
123 /// This can be either rows added by changes to the underlying
124 /// models or via [`TreeListRow::set_expanded()`][crate::TreeListRow::set_expanded()].
125 ///
126 /// # Returns
127 ///
128 /// [`true`] if the model is set to autoexpand
129 #[doc(alias = "gtk_tree_list_model_get_autoexpand")]
130 #[doc(alias = "get_autoexpand")]
131 #[doc(alias = "autoexpand")]
132 pub fn is_autoexpand(&self) -> bool {
133 unsafe {
134 from_glib(ffi::gtk_tree_list_model_get_autoexpand(
135 self.to_glib_none().0,
136 ))
137 }
138 }
139
140 /// Gets the row item corresponding to the child at index @position for
141 /// @self's root model.
142 ///
143 /// If @position is greater than the number of children in the root model,
144 /// [`None`] is returned.
145 ///
146 /// Do not confuse this function with [`row()`][Self::row()].
147 /// ## `position`
148 /// position of the child to get
149 ///
150 /// # Returns
151 ///
152 /// the child in @position
153 #[doc(alias = "gtk_tree_list_model_get_child_row")]
154 #[doc(alias = "get_child_row")]
155 pub fn child_row(&self, position: u32) -> Option<TreeListRow> {
156 unsafe {
157 from_glib_full(ffi::gtk_tree_list_model_get_child_row(
158 self.to_glib_none().0,
159 position,
160 ))
161 }
162 }
163
164 /// Gets the root model that @self was created with.
165 ///
166 /// # Returns
167 ///
168 /// the root model
169 #[doc(alias = "gtk_tree_list_model_get_model")]
170 #[doc(alias = "get_model")]
171 pub fn model(&self) -> gio::ListModel {
172 unsafe { from_glib_none(ffi::gtk_tree_list_model_get_model(self.to_glib_none().0)) }
173 }
174
175 /// Gets whether the model is passing through original row items.
176 ///
177 /// If this function returns [`false`], the `GListModel` functions for @self
178 /// return custom [`TreeListRow`][crate::TreeListRow] objects. You need to call
179 /// [`TreeListRow::item()`][crate::TreeListRow::item()] on these objects to get the original
180 /// item.
181 ///
182 /// If [`true`], the values of the child models are passed through in their
183 /// original state. You then need to call [`row()`][Self::row()]
184 /// to get the custom [`TreeListRow`][crate::TreeListRow]s.
185 ///
186 /// # Returns
187 ///
188 /// [`true`] if the model is passing through original row items
189 #[doc(alias = "gtk_tree_list_model_get_passthrough")]
190 #[doc(alias = "get_passthrough")]
191 #[doc(alias = "passthrough")]
192 pub fn is_passthrough(&self) -> bool {
193 unsafe {
194 from_glib(ffi::gtk_tree_list_model_get_passthrough(
195 self.to_glib_none().0,
196 ))
197 }
198 }
199
200 /// Gets the row object for the given row.
201 ///
202 /// If @position is greater than the number of items in @self,
203 /// [`None`] is returned.
204 ///
205 /// The row object can be used to expand and collapse rows as
206 /// well as to inspect its position in the tree. See its
207 /// documentation for details.
208 ///
209 /// This row object is persistent and will refer to the current
210 /// item as long as the row is present in @self, independent of
211 /// other rows being added or removed.
212 ///
213 /// If @self is set to not be passthrough, this function is
214 /// equivalent to calling g_list_model_get_item().
215 ///
216 /// Do not confuse this function with [`child_row()`][Self::child_row()].
217 /// ## `position`
218 /// the position of the row to fetch
219 ///
220 /// # Returns
221 ///
222 /// The row item
223 #[doc(alias = "gtk_tree_list_model_get_row")]
224 #[doc(alias = "get_row")]
225 pub fn row(&self, position: u32) -> Option<TreeListRow> {
226 unsafe {
227 from_glib_full(ffi::gtk_tree_list_model_get_row(
228 self.to_glib_none().0,
229 position,
230 ))
231 }
232 }
233
234 /// Sets whether the model should autoexpand.
235 ///
236 /// If set to [`true`], the model will recursively expand all rows that
237 /// get added to the model. This can be either rows added by changes
238 /// to the underlying models or via [`TreeListRow::set_expanded()`][crate::TreeListRow::set_expanded()].
239 /// ## `autoexpand`
240 /// [`true`] to make the model autoexpand its rows
241 #[doc(alias = "gtk_tree_list_model_set_autoexpand")]
242 #[doc(alias = "autoexpand")]
243 pub fn set_autoexpand(&self, autoexpand: bool) {
244 unsafe {
245 ffi::gtk_tree_list_model_set_autoexpand(self.to_glib_none().0, autoexpand.into_glib());
246 }
247 }
248
249 #[doc(alias = "autoexpand")]
250 pub fn connect_autoexpand_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
251 unsafe extern "C" fn notify_autoexpand_trampoline<F: Fn(&TreeListModel) + 'static>(
252 this: *mut ffi::GtkTreeListModel,
253 _param_spec: glib::ffi::gpointer,
254 f: glib::ffi::gpointer,
255 ) {
256 let f: &F = &*(f as *const F);
257 f(&from_glib_borrow(this))
258 }
259 unsafe {
260 let f: Box_<F> = Box_::new(f);
261 connect_raw(
262 self.as_ptr() as *mut _,
263 b"notify::autoexpand\0".as_ptr() as *const _,
264 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
265 notify_autoexpand_trampoline::<F> as *const (),
266 )),
267 Box_::into_raw(f),
268 )
269 }
270 }
271
272 #[doc(alias = "model")]
273 pub fn connect_model_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
274 unsafe extern "C" fn notify_model_trampoline<F: Fn(&TreeListModel) + 'static>(
275 this: *mut ffi::GtkTreeListModel,
276 _param_spec: glib::ffi::gpointer,
277 f: glib::ffi::gpointer,
278 ) {
279 let f: &F = &*(f as *const F);
280 f(&from_glib_borrow(this))
281 }
282 unsafe {
283 let f: Box_<F> = Box_::new(f);
284 connect_raw(
285 self.as_ptr() as *mut _,
286 b"notify::model\0".as_ptr() as *const _,
287 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
288 notify_model_trampoline::<F> as *const (),
289 )),
290 Box_::into_raw(f),
291 )
292 }
293 }
294}