gtk4/auto/
single_selection.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
5#[cfg(feature = "v4_12")]
6#[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
7use crate::SectionModel;
8use crate::{ffi, SelectionModel};
9use glib::{
10    prelude::*,
11    signal::{connect_raw, SignalHandlerId},
12    translate::*,
13};
14use std::boxed::Box as Box_;
15
16#[cfg(feature = "v4_12")]
17#[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
18glib::wrapper! {
19    /// [`SingleSelection`][crate::SingleSelection] is a [`SelectionModel`][crate::SelectionModel] that allows selecting a single
20    /// item.
21    ///
22    /// Note that the selection is *persistent* -- if the selected item is removed
23    /// and re-added in the same [`items-changed`][struct@crate::gio::ListModel#items-changed] emission, it
24    /// stays selected. In particular, this means that changing the sort order of an
25    /// underlying sort model will preserve the selection.
26    ///
27    /// ## Properties
28    ///
29    ///
30    /// #### `autoselect`
31    ///  If the selection will always select an item.
32    ///
33    /// Readable | Writeable
34    ///
35    ///
36    /// #### `can-unselect`
37    ///  If unselecting the selected item is allowed.
38    ///
39    /// Readable | Writeable
40    ///
41    ///
42    /// #### `item-type`
43    ///  The type of items. See [`ListModelExtManual::item_type()`][crate::gio::prelude::ListModelExtManual::item_type()].
44    ///
45    /// Readable
46    ///
47    ///
48    /// #### `model`
49    ///  The model being managed.
50    ///
51    /// Readable | Writeable
52    ///
53    ///
54    /// #### `n-items`
55    ///  The number of items. See [`ListModelExtManual::n_items()`][crate::gio::prelude::ListModelExtManual::n_items()].
56    ///
57    /// Readable
58    ///
59    ///
60    /// #### `selected`
61    ///  Position of the selected item.
62    ///
63    /// Readable | Writeable
64    ///
65    ///
66    /// #### `selected-item`
67    ///  The selected item.
68    ///
69    /// Readable
70    ///
71    /// # Implements
72    ///
73    /// [`trait@glib::ObjectExt`], [`trait@gio::prelude::ListModelExt`], [`SectionModelExt`][trait@crate::prelude::SectionModelExt], [`SelectionModelExt`][trait@crate::prelude::SelectionModelExt]
74    #[doc(alias = "GtkSingleSelection")]
75    pub struct SingleSelection(Object<ffi::GtkSingleSelection, ffi::GtkSingleSelectionClass>) @implements gio::ListModel, SectionModel, SelectionModel;
76
77    match fn {
78        type_ => || ffi::gtk_single_selection_get_type(),
79    }
80}
81
82#[cfg(not(any(feature = "v4_12")))]
83glib::wrapper! {
84    #[doc(alias = "GtkSingleSelection")]
85    pub struct SingleSelection(Object<ffi::GtkSingleSelection, ffi::GtkSingleSelectionClass>) @implements gio::ListModel, SelectionModel;
86
87    match fn {
88        type_ => || ffi::gtk_single_selection_get_type(),
89    }
90}
91
92impl SingleSelection {
93    /// Creates a new selection to handle @model.
94    /// ## `model`
95    /// the `GListModel` to manage
96    ///
97    /// # Returns
98    ///
99    /// a new [`SingleSelection`][crate::SingleSelection]
100    #[doc(alias = "gtk_single_selection_new")]
101    pub fn new(model: Option<impl IsA<gio::ListModel>>) -> SingleSelection {
102        assert_initialized_main_thread!();
103        unsafe {
104            from_glib_full(ffi::gtk_single_selection_new(
105                model.map(|p| p.upcast()).into_glib_ptr(),
106            ))
107        }
108    }
109
110    // rustdoc-stripper-ignore-next
111    /// Creates a new builder-pattern struct instance to construct [`SingleSelection`] objects.
112    ///
113    /// This method returns an instance of [`SingleSelectionBuilder`](crate::builders::SingleSelectionBuilder) which can be used to create [`SingleSelection`] objects.
114    pub fn builder() -> SingleSelectionBuilder {
115        SingleSelectionBuilder::new()
116    }
117
118    /// Checks if autoselect has been enabled or disabled via
119    /// gtk_single_selection_set_autoselect().
120    ///
121    /// # Returns
122    ///
123    /// [`true`] if autoselect is enabled
124    #[doc(alias = "gtk_single_selection_get_autoselect")]
125    #[doc(alias = "get_autoselect")]
126    #[doc(alias = "autoselect")]
127    pub fn is_autoselect(&self) -> bool {
128        unsafe {
129            from_glib(ffi::gtk_single_selection_get_autoselect(
130                self.to_glib_none().0,
131            ))
132        }
133    }
134
135    /// If [`true`], gtk_selection_model_unselect_item() is supported and allows
136    /// unselecting the selected item.
137    ///
138    /// # Returns
139    ///
140    /// [`true`] to support unselecting
141    #[doc(alias = "gtk_single_selection_get_can_unselect")]
142    #[doc(alias = "get_can_unselect")]
143    #[doc(alias = "can-unselect")]
144    pub fn can_unselect(&self) -> bool {
145        unsafe {
146            from_glib(ffi::gtk_single_selection_get_can_unselect(
147                self.to_glib_none().0,
148            ))
149        }
150    }
151
152    /// Gets the model that @self is wrapping.
153    ///
154    /// # Returns
155    ///
156    /// The model being wrapped
157    #[doc(alias = "gtk_single_selection_get_model")]
158    #[doc(alias = "get_model")]
159    pub fn model(&self) -> Option<gio::ListModel> {
160        unsafe { from_glib_none(ffi::gtk_single_selection_get_model(self.to_glib_none().0)) }
161    }
162
163    /// Gets the position of the selected item.
164    ///
165    /// If no item is selected, `GTK_INVALID_LIST_POSITION` is returned.
166    ///
167    /// # Returns
168    ///
169    /// The position of the selected item
170    #[doc(alias = "gtk_single_selection_get_selected")]
171    #[doc(alias = "get_selected")]
172    pub fn selected(&self) -> u32 {
173        unsafe { ffi::gtk_single_selection_get_selected(self.to_glib_none().0) }
174    }
175
176    /// Gets the selected item.
177    ///
178    /// If no item is selected, [`None`] is returned.
179    ///
180    /// # Returns
181    ///
182    /// The selected item
183    #[doc(alias = "gtk_single_selection_get_selected_item")]
184    #[doc(alias = "get_selected_item")]
185    #[doc(alias = "selected-item")]
186    pub fn selected_item(&self) -> Option<glib::Object> {
187        unsafe {
188            from_glib_none(ffi::gtk_single_selection_get_selected_item(
189                self.to_glib_none().0,
190            ))
191        }
192    }
193
194    /// Enables or disables autoselect.
195    ///
196    /// If @autoselect is [`true`], @self will enforce that an item is always
197    /// selected. It will select a new item when the currently selected
198    /// item is deleted and it will disallow unselecting the current item.
199    /// ## `autoselect`
200    /// [`true`] to always select an item
201    #[doc(alias = "gtk_single_selection_set_autoselect")]
202    #[doc(alias = "autoselect")]
203    pub fn set_autoselect(&self, autoselect: bool) {
204        unsafe {
205            ffi::gtk_single_selection_set_autoselect(self.to_glib_none().0, autoselect.into_glib());
206        }
207    }
208
209    /// If [`true`], unselecting the current item via
210    /// gtk_selection_model_unselect_item() is supported.
211    ///
212    /// Note that setting [`autoselect`][struct@crate::SingleSelection#autoselect] will
213    /// cause unselecting to not work, so it practically makes no sense
214    /// to set both at the same time.
215    /// ## `can_unselect`
216    /// [`true`] to allow unselecting
217    #[doc(alias = "gtk_single_selection_set_can_unselect")]
218    #[doc(alias = "can-unselect")]
219    pub fn set_can_unselect(&self, can_unselect: bool) {
220        unsafe {
221            ffi::gtk_single_selection_set_can_unselect(
222                self.to_glib_none().0,
223                can_unselect.into_glib(),
224            );
225        }
226    }
227
228    /// Sets the model that @self should wrap.
229    ///
230    /// If @model is [`None`], @self will be empty.
231    /// ## `model`
232    /// A `GListModel` to wrap
233    #[doc(alias = "gtk_single_selection_set_model")]
234    #[doc(alias = "model")]
235    pub fn set_model(&self, model: Option<&impl IsA<gio::ListModel>>) {
236        unsafe {
237            ffi::gtk_single_selection_set_model(
238                self.to_glib_none().0,
239                model.map(|p| p.as_ref()).to_glib_none().0,
240            );
241        }
242    }
243
244    /// Selects the item at the given position.
245    ///
246    /// If the list does not have an item at @position or
247    /// `GTK_INVALID_LIST_POSITION` is given, the behavior depends on the
248    /// value of the [`autoselect`][struct@crate::SingleSelection#autoselect] property:
249    /// If it is set, no change will occur and the old item will stay
250    /// selected. If it is unset, the selection will be unset and no item
251    /// will be selected. This also applies if [`can-unselect`][struct@crate::SingleSelection#can-unselect]
252    /// is set to [`false`].
253    /// ## `position`
254    /// the item to select or `GTK_INVALID_LIST_POSITION`
255    #[doc(alias = "gtk_single_selection_set_selected")]
256    #[doc(alias = "selected")]
257    pub fn set_selected(&self, position: u32) {
258        unsafe {
259            ffi::gtk_single_selection_set_selected(self.to_glib_none().0, position);
260        }
261    }
262
263    #[doc(alias = "autoselect")]
264    pub fn connect_autoselect_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
265        unsafe extern "C" fn notify_autoselect_trampoline<F: Fn(&SingleSelection) + 'static>(
266            this: *mut ffi::GtkSingleSelection,
267            _param_spec: glib::ffi::gpointer,
268            f: glib::ffi::gpointer,
269        ) {
270            let f: &F = &*(f as *const F);
271            f(&from_glib_borrow(this))
272        }
273        unsafe {
274            let f: Box_<F> = Box_::new(f);
275            connect_raw(
276                self.as_ptr() as *mut _,
277                b"notify::autoselect\0".as_ptr() as *const _,
278                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
279                    notify_autoselect_trampoline::<F> as *const (),
280                )),
281                Box_::into_raw(f),
282            )
283        }
284    }
285
286    #[doc(alias = "can-unselect")]
287    pub fn connect_can_unselect_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
288        unsafe extern "C" fn notify_can_unselect_trampoline<F: Fn(&SingleSelection) + 'static>(
289            this: *mut ffi::GtkSingleSelection,
290            _param_spec: glib::ffi::gpointer,
291            f: glib::ffi::gpointer,
292        ) {
293            let f: &F = &*(f as *const F);
294            f(&from_glib_borrow(this))
295        }
296        unsafe {
297            let f: Box_<F> = Box_::new(f);
298            connect_raw(
299                self.as_ptr() as *mut _,
300                b"notify::can-unselect\0".as_ptr() as *const _,
301                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
302                    notify_can_unselect_trampoline::<F> as *const (),
303                )),
304                Box_::into_raw(f),
305            )
306        }
307    }
308
309    #[doc(alias = "model")]
310    pub fn connect_model_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
311        unsafe extern "C" fn notify_model_trampoline<F: Fn(&SingleSelection) + 'static>(
312            this: *mut ffi::GtkSingleSelection,
313            _param_spec: glib::ffi::gpointer,
314            f: glib::ffi::gpointer,
315        ) {
316            let f: &F = &*(f as *const F);
317            f(&from_glib_borrow(this))
318        }
319        unsafe {
320            let f: Box_<F> = Box_::new(f);
321            connect_raw(
322                self.as_ptr() as *mut _,
323                b"notify::model\0".as_ptr() as *const _,
324                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
325                    notify_model_trampoline::<F> as *const (),
326                )),
327                Box_::into_raw(f),
328            )
329        }
330    }
331
332    #[doc(alias = "selected")]
333    pub fn connect_selected_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
334        unsafe extern "C" fn notify_selected_trampoline<F: Fn(&SingleSelection) + 'static>(
335            this: *mut ffi::GtkSingleSelection,
336            _param_spec: glib::ffi::gpointer,
337            f: glib::ffi::gpointer,
338        ) {
339            let f: &F = &*(f as *const F);
340            f(&from_glib_borrow(this))
341        }
342        unsafe {
343            let f: Box_<F> = Box_::new(f);
344            connect_raw(
345                self.as_ptr() as *mut _,
346                b"notify::selected\0".as_ptr() as *const _,
347                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
348                    notify_selected_trampoline::<F> as *const (),
349                )),
350                Box_::into_raw(f),
351            )
352        }
353    }
354
355    #[doc(alias = "selected-item")]
356    pub fn connect_selected_item_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
357        unsafe extern "C" fn notify_selected_item_trampoline<F: Fn(&SingleSelection) + 'static>(
358            this: *mut ffi::GtkSingleSelection,
359            _param_spec: glib::ffi::gpointer,
360            f: glib::ffi::gpointer,
361        ) {
362            let f: &F = &*(f as *const F);
363            f(&from_glib_borrow(this))
364        }
365        unsafe {
366            let f: Box_<F> = Box_::new(f);
367            connect_raw(
368                self.as_ptr() as *mut _,
369                b"notify::selected-item\0".as_ptr() as *const _,
370                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
371                    notify_selected_item_trampoline::<F> as *const (),
372                )),
373                Box_::into_raw(f),
374            )
375        }
376    }
377}
378
379impl Default for SingleSelection {
380    fn default() -> Self {
381        glib::object::Object::new::<Self>()
382    }
383}
384
385// rustdoc-stripper-ignore-next
386/// A [builder-pattern] type to construct [`SingleSelection`] objects.
387///
388/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
389#[must_use = "The builder must be built to be used"]
390pub struct SingleSelectionBuilder {
391    builder: glib::object::ObjectBuilder<'static, SingleSelection>,
392}
393
394impl SingleSelectionBuilder {
395    fn new() -> Self {
396        Self {
397            builder: glib::object::Object::builder(),
398        }
399    }
400
401    /// If the selection will always select an item.
402    pub fn autoselect(self, autoselect: bool) -> Self {
403        Self {
404            builder: self.builder.property("autoselect", autoselect),
405        }
406    }
407
408    /// If unselecting the selected item is allowed.
409    pub fn can_unselect(self, can_unselect: bool) -> Self {
410        Self {
411            builder: self.builder.property("can-unselect", can_unselect),
412        }
413    }
414
415    /// The model being managed.
416    pub fn model(self, model: &impl IsA<gio::ListModel>) -> Self {
417        Self {
418            builder: self.builder.property("model", model.clone().upcast()),
419        }
420    }
421
422    /// Position of the selected item.
423    pub fn selected(self, selected: u32) -> Self {
424        Self {
425            builder: self.builder.property("selected", selected),
426        }
427    }
428
429    // rustdoc-stripper-ignore-next
430    /// Build the [`SingleSelection`].
431    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
432    pub fn build(self) -> SingleSelection {
433        assert_initialized_main_thread!();
434        self.builder.build()
435    }
436}