Skip to main content

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::{SelectionModel, ffi};
9use glib::{
10    prelude::*,
11    signal::{SignalHandlerId, connect_raw},
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            unsafe {
270                let f: &F = &*(f as *const F);
271                f(&from_glib_borrow(this))
272            }
273        }
274        unsafe {
275            let f: Box_<F> = Box_::new(f);
276            connect_raw(
277                self.as_ptr() as *mut _,
278                c"notify::autoselect".as_ptr() as *const _,
279                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
280                    notify_autoselect_trampoline::<F> as *const (),
281                )),
282                Box_::into_raw(f),
283            )
284        }
285    }
286
287    #[doc(alias = "can-unselect")]
288    pub fn connect_can_unselect_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
289        unsafe extern "C" fn notify_can_unselect_trampoline<F: Fn(&SingleSelection) + 'static>(
290            this: *mut ffi::GtkSingleSelection,
291            _param_spec: glib::ffi::gpointer,
292            f: glib::ffi::gpointer,
293        ) {
294            unsafe {
295                let f: &F = &*(f as *const F);
296                f(&from_glib_borrow(this))
297            }
298        }
299        unsafe {
300            let f: Box_<F> = Box_::new(f);
301            connect_raw(
302                self.as_ptr() as *mut _,
303                c"notify::can-unselect".as_ptr() as *const _,
304                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
305                    notify_can_unselect_trampoline::<F> as *const (),
306                )),
307                Box_::into_raw(f),
308            )
309        }
310    }
311
312    #[doc(alias = "model")]
313    pub fn connect_model_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
314        unsafe extern "C" fn notify_model_trampoline<F: Fn(&SingleSelection) + 'static>(
315            this: *mut ffi::GtkSingleSelection,
316            _param_spec: glib::ffi::gpointer,
317            f: glib::ffi::gpointer,
318        ) {
319            unsafe {
320                let f: &F = &*(f as *const F);
321                f(&from_glib_borrow(this))
322            }
323        }
324        unsafe {
325            let f: Box_<F> = Box_::new(f);
326            connect_raw(
327                self.as_ptr() as *mut _,
328                c"notify::model".as_ptr() as *const _,
329                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
330                    notify_model_trampoline::<F> as *const (),
331                )),
332                Box_::into_raw(f),
333            )
334        }
335    }
336
337    #[doc(alias = "selected")]
338    pub fn connect_selected_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
339        unsafe extern "C" fn notify_selected_trampoline<F: Fn(&SingleSelection) + 'static>(
340            this: *mut ffi::GtkSingleSelection,
341            _param_spec: glib::ffi::gpointer,
342            f: glib::ffi::gpointer,
343        ) {
344            unsafe {
345                let f: &F = &*(f as *const F);
346                f(&from_glib_borrow(this))
347            }
348        }
349        unsafe {
350            let f: Box_<F> = Box_::new(f);
351            connect_raw(
352                self.as_ptr() as *mut _,
353                c"notify::selected".as_ptr() as *const _,
354                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
355                    notify_selected_trampoline::<F> as *const (),
356                )),
357                Box_::into_raw(f),
358            )
359        }
360    }
361
362    #[doc(alias = "selected-item")]
363    pub fn connect_selected_item_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
364        unsafe extern "C" fn notify_selected_item_trampoline<F: Fn(&SingleSelection) + 'static>(
365            this: *mut ffi::GtkSingleSelection,
366            _param_spec: glib::ffi::gpointer,
367            f: glib::ffi::gpointer,
368        ) {
369            unsafe {
370                let f: &F = &*(f as *const F);
371                f(&from_glib_borrow(this))
372            }
373        }
374        unsafe {
375            let f: Box_<F> = Box_::new(f);
376            connect_raw(
377                self.as_ptr() as *mut _,
378                c"notify::selected-item".as_ptr() as *const _,
379                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
380                    notify_selected_item_trampoline::<F> as *const (),
381                )),
382                Box_::into_raw(f),
383            )
384        }
385    }
386}
387
388impl Default for SingleSelection {
389    fn default() -> Self {
390        glib::object::Object::new::<Self>()
391    }
392}
393
394// rustdoc-stripper-ignore-next
395/// A [builder-pattern] type to construct [`SingleSelection`] objects.
396///
397/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
398#[must_use = "The builder must be built to be used"]
399pub struct SingleSelectionBuilder {
400    builder: glib::object::ObjectBuilder<'static, SingleSelection>,
401}
402
403impl SingleSelectionBuilder {
404    fn new() -> Self {
405        Self {
406            builder: glib::object::Object::builder(),
407        }
408    }
409
410    /// If the selection will always select an item.
411    pub fn autoselect(self, autoselect: bool) -> Self {
412        Self {
413            builder: self.builder.property("autoselect", autoselect),
414        }
415    }
416
417    /// If unselecting the selected item is allowed.
418    pub fn can_unselect(self, can_unselect: bool) -> Self {
419        Self {
420            builder: self.builder.property("can-unselect", can_unselect),
421        }
422    }
423
424    /// The model being managed.
425    pub fn model(self, model: &impl IsA<gio::ListModel>) -> Self {
426        Self {
427            builder: self.builder.property("model", model.clone().upcast()),
428        }
429    }
430
431    /// Position of the selected item.
432    pub fn selected(self, selected: u32) -> Self {
433        Self {
434            builder: self.builder.property("selected", selected),
435        }
436    }
437
438    // rustdoc-stripper-ignore-next
439    /// Build the [`SingleSelection`].
440    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
441    pub fn build(self) -> SingleSelection {
442        assert_initialized_main_thread!();
443        self.builder.build()
444    }
445}