gio/auto/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;
6use glib::{
7 object::ObjectType as _,
8 prelude::*,
9 signal::{SignalHandlerId, connect_raw},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 /// , in order to facilitate the
16 /// maximum level of laziness in the implementation of the list and to
17 /// reduce the required number of signal connections at a given time.
18 ///
19 /// This interface is intended only to be used from a single thread. The
20 /// thread in which it is appropriate to use it depends on the particular
21 /// implementation, but typically it will be from the thread that owns
22 /// the thread-default main context (see
23 /// [`glib::MainContext::push_thread_default()`][crate::glib::MainContext::push_thread_default()]) in effect at the time that the
24 /// model was created.
25 ///
26 /// Over time, it has established itself as good practice for list model
27 /// implementations to provide properties `item-type` and `n-items` to
28 /// ease working with them. While it is not required, it is recommended
29 /// that implementations provide these two properties. They should return
30 /// the values of [`ListModelExt::item_type()`][crate::prelude::ListModelExt::item_type()] and
31 /// [`ListModelExt::n_items()`][crate::prelude::ListModelExt::n_items()] respectively and be defined as such:
32 ///
33 /// **⚠️ The following code is in c ⚠️**
34 ///
35 /// ```c
36 /// properties[PROP_ITEM_TYPE] =
37 /// g_param_spec_gtype ("item-type", NULL, NULL, G_TYPE_OBJECT,
38 /// G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
39 /// properties[PROP_N_ITEMS] =
40 /// g_param_spec_uint ("n-items", NULL, NULL, 0, G_MAXUINT, 0,
41 /// G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
42 /// ```
43 ///
44 /// ## Signals
45 ///
46 ///
47 /// #### `items-changed`
48 /// This signal is emitted whenever items were added to or removed
49 /// from @list. At @position, @removed items were removed and @added
50 /// items were added in their place.
51 ///
52 /// Note: If `removed != added`, the positions of all later items
53 /// in the model change.
54 ///
55 ///
56 ///
57 /// # Implements
58 ///
59 /// [`ListModelExt`][trait@crate::prelude::ListModelExt], [`ListModelExtManual`][trait@crate::prelude::ListModelExtManual]
60 #[doc(alias = "GListModel")]
61 pub struct ListModel(Interface<ffi::GListModel, ffi::GListModelInterface>);
62
63 match fn {
64 type_ => || ffi::g_list_model_get_type(),
65 }
66}
67
68impl ListModel {
69 pub const NONE: Option<&'static ListModel> = None;
70}
71
72/// Trait containing all [`struct@ListModel`] methods.
73///
74/// # Implementors
75///
76/// [`ListModel`][struct@crate::ListModel], [`ListStore`][struct@crate::ListStore]
77pub trait ListModelExt: IsA<ListModel> + 'static {
78 /// Gets the type of the items in @self.
79 ///
80 /// All items returned from g_list_model_get_item() are of the type
81 /// returned by this function, or a subtype, or if the type is an
82 /// interface, they are an implementation of that interface.
83 ///
84 /// The item type of a #GListModel can not change during the life of the
85 /// model.
86 ///
87 /// # Returns
88 ///
89 /// the #GType of the items contained in @self.
90 #[doc(alias = "g_list_model_get_item_type")]
91 #[doc(alias = "get_item_type")]
92 fn item_type(&self) -> glib::types::Type {
93 unsafe {
94 from_glib(ffi::g_list_model_get_item_type(
95 self.as_ref().to_glib_none().0,
96 ))
97 }
98 }
99
100 /// Gets the number of items in @self.
101 ///
102 /// Depending on the model implementation, calling this function may be
103 /// less efficient than iterating the list with increasing values for
104 /// @position until g_list_model_get_item() returns [`None`].
105 ///
106 /// # Returns
107 ///
108 /// the number of items in @self.
109 #[doc(alias = "g_list_model_get_n_items")]
110 #[doc(alias = "get_n_items")]
111 fn n_items(&self) -> u32 {
112 unsafe { ffi::g_list_model_get_n_items(self.as_ref().to_glib_none().0) }
113 }
114
115 /// Get the item at @position.
116 ///
117 /// If @position is greater than the number of items in @self, [`None`] is
118 /// returned.
119 ///
120 /// [`None`] is never returned for an index that is smaller than the length
121 /// of the list.
122 ///
123 /// This function is meant to be used by language bindings in place
124 /// of g_list_model_get_item().
125 ///
126 /// See also: g_list_model_get_n_items()
127 /// ## `position`
128 /// the position of the item to fetch
129 ///
130 /// # Returns
131 ///
132 /// the object at @position.
133 #[doc(alias = "g_list_model_get_object")]
134 #[doc(alias = "get_object")]
135 fn item(&self, position: u32) -> Option<glib::Object> {
136 unsafe {
137 from_glib_full(ffi::g_list_model_get_object(
138 self.as_ref().to_glib_none().0,
139 position,
140 ))
141 }
142 }
143
144 /// Emits the #GListModel::items-changed signal on @self.
145 ///
146 /// This function should only be called by classes implementing
147 /// #GListModel. It has to be called after the internal representation
148 /// of @self has been updated, because handlers connected to this signal
149 /// might query the new state of the list.
150 ///
151 /// Implementations must only make changes to the model (as visible to
152 /// its consumer) in places that will not cause problems for that
153 /// consumer. For models that are driven directly by a write API (such
154 /// as #GListStore), changes can be reported in response to uses of that
155 /// API. For models that represent remote data, changes should only be
156 /// made from a fresh mainloop dispatch. It is particularly not
157 /// permitted to make changes in response to a call to the #GListModel
158 /// consumer API.
159 ///
160 /// Stated another way: in general, it is assumed that code making a
161 /// series of accesses to the model via the API, without returning to the
162 /// mainloop, and without calling other code, will continue to view the
163 /// same contents of the model.
164 /// ## `position`
165 /// the position at which @self changed
166 /// ## `removed`
167 /// the number of items removed
168 /// ## `added`
169 /// the number of items added
170 #[doc(alias = "g_list_model_items_changed")]
171 fn items_changed(&self, position: u32, removed: u32, added: u32) {
172 unsafe {
173 ffi::g_list_model_items_changed(
174 self.as_ref().to_glib_none().0,
175 position,
176 removed,
177 added,
178 );
179 }
180 }
181
182 /// This signal is emitted whenever items were added to or removed
183 /// from @list. At @position, @removed items were removed and @added
184 /// items were added in their place.
185 ///
186 /// Note: If `removed != added`, the positions of all later items
187 /// in the model change.
188 /// ## `position`
189 /// the position at which @list changed
190 /// ## `removed`
191 /// the number of items removed
192 /// ## `added`
193 /// the number of items added
194 #[doc(alias = "items-changed")]
195 fn connect_items_changed<F: Fn(&Self, u32, u32, u32) + 'static>(
196 &self,
197 f: F,
198 ) -> SignalHandlerId {
199 unsafe extern "C" fn items_changed_trampoline<
200 P: IsA<ListModel>,
201 F: Fn(&P, u32, u32, u32) + 'static,
202 >(
203 this: *mut ffi::GListModel,
204 position: std::ffi::c_uint,
205 removed: std::ffi::c_uint,
206 added: std::ffi::c_uint,
207 f: glib::ffi::gpointer,
208 ) {
209 unsafe {
210 let f: &F = &*(f as *const F);
211 f(
212 ListModel::from_glib_borrow(this).unsafe_cast_ref(),
213 position,
214 removed,
215 added,
216 )
217 }
218 }
219 unsafe {
220 let f: Box_<F> = Box_::new(f);
221 connect_raw(
222 self.as_ptr() as *mut _,
223 c"items-changed".as_ptr(),
224 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
225 items_changed_trampoline::<Self, F> as *const (),
226 )),
227 Box_::into_raw(f),
228 )
229 }
230 }
231}
232
233impl<O: IsA<ListModel>> ListModelExt for O {}