gtk4/auto/selection_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, Bitset};
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 /// An interface that adds support for selection to list models.
16 ///
17 /// This support is then used by widgets using list models to add the ability
18 /// to select and unselect various items.
19 ///
20 /// GTK provides default implementations of the most common selection modes such
21 /// as [`SingleSelection`][crate::SingleSelection], so you will only need to implement this
22 /// interface if you want detailed control about how selections should be handled.
23 ///
24 /// A [`SelectionModel`][crate::SelectionModel] supports a single boolean per item indicating if an item is
25 /// selected or not. This can be queried via [`SelectionModelExt::is_selected()`][crate::prelude::SelectionModelExt::is_selected()].
26 /// When the selected state of one or more items changes, the model will emit the
27 /// [`selection-changed`][struct@crate::SelectionModel#selection-changed] signal by calling the
28 /// [`SelectionModelExt::selection_changed()`][crate::prelude::SelectionModelExt::selection_changed()] function. The positions given
29 /// in that signal may have their selection state changed, though that is not a
30 /// requirement. If new items added to the model via the
31 /// [`items-changed`][struct@crate::gio::ListModel#items-changed] signal are selected or not is up to the
32 /// implementation.
33 ///
34 /// Note that items added via [`items-changed`][struct@crate::gio::ListModel#items-changed] may already
35 /// be selected and no [`selection-changed`][struct@crate::SelectionModel#selection-changed] will be
36 /// emitted for them. So to track which items are selected, it is necessary to
37 /// listen to both signals.
38 ///
39 /// Additionally, the interface can expose functionality to select and unselect
40 /// items. If these functions are implemented, GTK's list widgets will allow users
41 /// to select and unselect items. However, [`SelectionModel`][crate::SelectionModel]s are free to only
42 /// implement them partially or not at all. In that case the widgets will not
43 /// support the unimplemented operations.
44 ///
45 /// When selecting or unselecting is supported by a model, the return values of
46 /// the selection functions do *not* indicate if selection or unselection happened.
47 /// They are only meant to indicate complete failure, like when this mode of
48 /// selecting is not supported by the model.
49 ///
50 /// Selections may happen asynchronously, so the only reliable way to find out
51 /// when an item was selected is to listen to the signals that indicate selection.
52 ///
53 /// ## Signals
54 ///
55 ///
56 /// #### `selection-changed`
57 /// Emitted when the selection state of some of the items in @model changes.
58 ///
59 /// Note that this signal does not specify the new selection state of the
60 /// items, they need to be queried manually. It is also not necessary for
61 /// a model to change the selection state of any of the items in the selection
62 /// model, though it would be rather useless to emit such a signal.
63 ///
64 ///
65 /// <details><summary><h4>ListModel</h4></summary>
66 ///
67 ///
68 /// #### `items-changed`
69 /// This signal is emitted whenever items were added to or removed
70 /// from @list. At @position, @removed items were removed and @added
71 /// items were added in their place.
72 ///
73 /// Note: If `removed != added`, the positions of all later items
74 /// in the model change.
75 ///
76 ///
77 /// </details>
78 ///
79 /// # Implements
80 ///
81 /// [`SelectionModelExt`][trait@crate::prelude::SelectionModelExt], [`trait@gio::prelude::ListModelExt`]
82 #[doc(alias = "GtkSelectionModel")]
83 pub struct SelectionModel(Interface<ffi::GtkSelectionModel, ffi::GtkSelectionModelInterface>) @requires gio::ListModel;
84
85 match fn {
86 type_ => || ffi::gtk_selection_model_get_type(),
87 }
88}
89
90impl SelectionModel {
91 pub const NONE: Option<&'static SelectionModel> = None;
92}
93
94/// Trait containing all [`struct@SelectionModel`] methods.
95///
96/// # Implementors
97///
98/// [`MultiSelection`][struct@crate::MultiSelection], [`NoSelection`][struct@crate::NoSelection], [`SelectionModel`][struct@crate::SelectionModel], [`SingleSelection`][struct@crate::SingleSelection]
99pub trait SelectionModelExt: IsA<SelectionModel> + 'static {
100 /// Gets the set containing all currently selected items in the model.
101 ///
102 /// This function may be slow, so if you are only interested in single item,
103 /// consider using [`is_selected()`][Self::is_selected()] or if you are only
104 /// interested in a few, consider [`selection_in_range()`][Self::selection_in_range()].
105 ///
106 /// # Returns
107 ///
108 /// a [`Bitset`][crate::Bitset] containing all the values currently
109 /// selected in @self. If no items are selected, the bitset is empty.
110 /// The bitset must not be modified.
111 #[doc(alias = "gtk_selection_model_get_selection")]
112 #[doc(alias = "get_selection")]
113 fn selection(&self) -> Bitset {
114 unsafe {
115 from_glib_full(ffi::gtk_selection_model_get_selection(
116 self.as_ref().to_glib_none().0,
117 ))
118 }
119 }
120
121 /// Gets the set of selected items in a range.
122 ///
123 /// This function is an optimization for
124 /// [`selection()`][Self::selection()] when you are only
125 /// interested in part of the model's selected state. A common use
126 /// case is in response to the [`selection-changed`][struct@crate::SelectionModel#selection-changed]
127 /// signal.
128 /// ## `position`
129 /// start of the queried range
130 /// ## `n_items`
131 /// number of items in the queried range
132 ///
133 /// # Returns
134 ///
135 /// A [`Bitset`][crate::Bitset] that matches the selection state
136 /// for the given range with all other values being undefined.
137 /// The bitset must not be modified.
138 #[doc(alias = "gtk_selection_model_get_selection_in_range")]
139 #[doc(alias = "get_selection_in_range")]
140 fn selection_in_range(&self, position: u32, n_items: u32) -> Bitset {
141 unsafe {
142 from_glib_full(ffi::gtk_selection_model_get_selection_in_range(
143 self.as_ref().to_glib_none().0,
144 position,
145 n_items,
146 ))
147 }
148 }
149
150 /// Checks if the given item is selected.
151 /// ## `position`
152 /// the position of the item to query
153 ///
154 /// # Returns
155 ///
156 /// [`true`] if the item is selected
157 #[doc(alias = "gtk_selection_model_is_selected")]
158 fn is_selected(&self, position: u32) -> bool {
159 unsafe {
160 from_glib(ffi::gtk_selection_model_is_selected(
161 self.as_ref().to_glib_none().0,
162 position,
163 ))
164 }
165 }
166
167 /// Requests to select all items in the model.
168 ///
169 /// # Returns
170 ///
171 /// [`true`] if this action was supported and no fallback should be
172 /// tried. This does not mean that all items are now selected.
173 #[doc(alias = "gtk_selection_model_select_all")]
174 fn select_all(&self) -> bool {
175 unsafe {
176 from_glib(ffi::gtk_selection_model_select_all(
177 self.as_ref().to_glib_none().0,
178 ))
179 }
180 }
181
182 /// Requests to select an item in the model.
183 /// ## `position`
184 /// the position of the item to select
185 /// ## `unselect_rest`
186 /// whether previously selected items should be unselected
187 ///
188 /// # Returns
189 ///
190 /// [`true`] if this action was supported and no fallback should be
191 /// tried. This does not mean the item was selected.
192 #[doc(alias = "gtk_selection_model_select_item")]
193 fn select_item(&self, position: u32, unselect_rest: bool) -> bool {
194 unsafe {
195 from_glib(ffi::gtk_selection_model_select_item(
196 self.as_ref().to_glib_none().0,
197 position,
198 unselect_rest.into_glib(),
199 ))
200 }
201 }
202
203 /// Requests to select a range of items in the model.
204 /// ## `position`
205 /// the first item to select
206 /// ## `n_items`
207 /// the number of items to select
208 /// ## `unselect_rest`
209 /// whether previously selected items should be unselected
210 ///
211 /// # Returns
212 ///
213 /// [`true`] if this action was supported and no fallback should be
214 /// tried. This does not mean the range was selected.
215 #[doc(alias = "gtk_selection_model_select_range")]
216 fn select_range(&self, position: u32, n_items: u32, unselect_rest: bool) -> bool {
217 unsafe {
218 from_glib(ffi::gtk_selection_model_select_range(
219 self.as_ref().to_glib_none().0,
220 position,
221 n_items,
222 unselect_rest.into_glib(),
223 ))
224 }
225 }
226
227 /// Helper function for implementations of [`SelectionModel`][crate::SelectionModel].
228 ///
229 /// Call this when the selection changes to emit the
230 /// [`selection-changed`][struct@crate::SelectionModel#selection-changed] signal.
231 /// ## `position`
232 /// the first changed item
233 /// ## `n_items`
234 /// the number of changed items
235 #[doc(alias = "gtk_selection_model_selection_changed")]
236 fn selection_changed(&self, position: u32, n_items: u32) {
237 unsafe {
238 ffi::gtk_selection_model_selection_changed(
239 self.as_ref().to_glib_none().0,
240 position,
241 n_items,
242 );
243 }
244 }
245
246 /// Make selection changes.
247 ///
248 /// This is the most advanced selection updating method that allows
249 /// the most fine-grained control over selection changes. If you can,
250 /// you should try the simpler versions, as implementations are more
251 /// likely to implement support for those.
252 ///
253 /// Requests that the selection state of all positions set in @mask
254 /// be updated to the respective value in the @selected bitmask.
255 ///
256 /// In pseudocode, it would look something like this:
257 ///
258 /// **⚠️ The following code is in c ⚠️**
259 ///
260 /// ```c
261 /// for (i = 0; i < n_items; i++)
262 /// {
263 /// // don't change values not in the mask
264 /// if (!gtk_bitset_contains (mask, i))
265 /// continue;
266 ///
267 /// if (gtk_bitset_contains (selected, i))
268 /// select_item (i);
269 /// else
270 /// unselect_item (i);
271 /// }
272 ///
273 /// gtk_selection_model_selection_changed (model,
274 /// first_changed_item,
275 /// n_changed_items);
276 /// ```
277 ///
278 /// @mask and @selected must not be modified. They may refer to the
279 /// same bitset, which would mean that every item in the set should
280 /// be selected.
281 /// ## `selected`
282 /// bitmask specifying if items should be selected or unselected
283 /// ## `mask`
284 /// bitmask specifying which items should be updated
285 ///
286 /// # Returns
287 ///
288 /// [`true`] if this action was supported and no fallback should be
289 /// tried. This does not mean that all items were updated according
290 /// to the inputs.
291 #[doc(alias = "gtk_selection_model_set_selection")]
292 fn set_selection(&self, selected: &Bitset, mask: &Bitset) -> bool {
293 unsafe {
294 from_glib(ffi::gtk_selection_model_set_selection(
295 self.as_ref().to_glib_none().0,
296 selected.to_glib_none().0,
297 mask.to_glib_none().0,
298 ))
299 }
300 }
301
302 /// Requests to unselect all items in the model.
303 ///
304 /// # Returns
305 ///
306 /// [`true`] if this action was supported and no fallback should be
307 /// tried. This does not mean that all items are now unselected.
308 #[doc(alias = "gtk_selection_model_unselect_all")]
309 fn unselect_all(&self) -> bool {
310 unsafe {
311 from_glib(ffi::gtk_selection_model_unselect_all(
312 self.as_ref().to_glib_none().0,
313 ))
314 }
315 }
316
317 /// Requests to unselect an item in the model.
318 /// ## `position`
319 /// the position of the item to unselect
320 ///
321 /// # Returns
322 ///
323 /// [`true`] if this action was supported and no fallback should be
324 /// tried. This does not mean the item was unselected.
325 #[doc(alias = "gtk_selection_model_unselect_item")]
326 fn unselect_item(&self, position: u32) -> bool {
327 unsafe {
328 from_glib(ffi::gtk_selection_model_unselect_item(
329 self.as_ref().to_glib_none().0,
330 position,
331 ))
332 }
333 }
334
335 /// Requests to unselect a range of items in the model.
336 /// ## `position`
337 /// the first item to unselect
338 /// ## `n_items`
339 /// the number of items to unselect
340 ///
341 /// # Returns
342 ///
343 /// [`true`] if this action was supported and no fallback should be
344 /// tried. This does not mean the range was unselected.
345 #[doc(alias = "gtk_selection_model_unselect_range")]
346 fn unselect_range(&self, position: u32, n_items: u32) -> bool {
347 unsafe {
348 from_glib(ffi::gtk_selection_model_unselect_range(
349 self.as_ref().to_glib_none().0,
350 position,
351 n_items,
352 ))
353 }
354 }
355
356 /// Emitted when the selection state of some of the items in @model changes.
357 ///
358 /// Note that this signal does not specify the new selection state of the
359 /// items, they need to be queried manually. It is also not necessary for
360 /// a model to change the selection state of any of the items in the selection
361 /// model, though it would be rather useless to emit such a signal.
362 /// ## `position`
363 /// The first item that may have changed
364 /// ## `n_items`
365 /// number of items with changes
366 #[doc(alias = "selection-changed")]
367 fn connect_selection_changed<F: Fn(&Self, u32, u32) + 'static>(&self, f: F) -> SignalHandlerId {
368 unsafe extern "C" fn selection_changed_trampoline<
369 P: IsA<SelectionModel>,
370 F: Fn(&P, u32, u32) + 'static,
371 >(
372 this: *mut ffi::GtkSelectionModel,
373 position: std::ffi::c_uint,
374 n_items: std::ffi::c_uint,
375 f: glib::ffi::gpointer,
376 ) {
377 let f: &F = &*(f as *const F);
378 f(
379 SelectionModel::from_glib_borrow(this).unsafe_cast_ref(),
380 position,
381 n_items,
382 )
383 }
384 unsafe {
385 let f: Box_<F> = Box_::new(f);
386 connect_raw(
387 self.as_ptr() as *mut _,
388 c"selection-changed".as_ptr() as *const _,
389 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
390 selection_changed_trampoline::<Self, F> as *const (),
391 )),
392 Box_::into_raw(f),
393 )
394 }
395 }
396}
397
398impl<O: IsA<SelectionModel>> SelectionModelExt for O {}