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