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