1#[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 #[doc(alias = "GtkSingleSelection")]
20 pub struct SingleSelection(Object<ffi::GtkSingleSelection, ffi::GtkSingleSelectionClass>) @implements gio::ListModel, SectionModel, SelectionModel;
21
22 match fn {
23 type_ => || ffi::gtk_single_selection_get_type(),
24 }
25}
26
27#[cfg(not(any(feature = "v4_12")))]
28glib::wrapper! {
29 #[doc(alias = "GtkSingleSelection")]
30 pub struct SingleSelection(Object<ffi::GtkSingleSelection, ffi::GtkSingleSelectionClass>) @implements gio::ListModel, SelectionModel;
31
32 match fn {
33 type_ => || ffi::gtk_single_selection_get_type(),
34 }
35}
36
37impl SingleSelection {
38 #[doc(alias = "gtk_single_selection_new")]
39 pub fn new(model: Option<impl IsA<gio::ListModel>>) -> SingleSelection {
40 assert_initialized_main_thread!();
41 unsafe {
42 from_glib_full(ffi::gtk_single_selection_new(
43 model.map(|p| p.upcast()).into_glib_ptr(),
44 ))
45 }
46 }
47
48 pub fn builder() -> SingleSelectionBuilder {
53 SingleSelectionBuilder::new()
54 }
55
56 #[doc(alias = "gtk_single_selection_get_autoselect")]
57 #[doc(alias = "get_autoselect")]
58 #[doc(alias = "autoselect")]
59 pub fn is_autoselect(&self) -> bool {
60 unsafe {
61 from_glib(ffi::gtk_single_selection_get_autoselect(
62 self.to_glib_none().0,
63 ))
64 }
65 }
66
67 #[doc(alias = "gtk_single_selection_get_can_unselect")]
68 #[doc(alias = "get_can_unselect")]
69 #[doc(alias = "can-unselect")]
70 pub fn can_unselect(&self) -> bool {
71 unsafe {
72 from_glib(ffi::gtk_single_selection_get_can_unselect(
73 self.to_glib_none().0,
74 ))
75 }
76 }
77
78 #[doc(alias = "gtk_single_selection_get_model")]
79 #[doc(alias = "get_model")]
80 pub fn model(&self) -> Option<gio::ListModel> {
81 unsafe { from_glib_none(ffi::gtk_single_selection_get_model(self.to_glib_none().0)) }
82 }
83
84 #[doc(alias = "gtk_single_selection_get_selected")]
85 #[doc(alias = "get_selected")]
86 pub fn selected(&self) -> u32 {
87 unsafe { ffi::gtk_single_selection_get_selected(self.to_glib_none().0) }
88 }
89
90 #[doc(alias = "gtk_single_selection_get_selected_item")]
91 #[doc(alias = "get_selected_item")]
92 #[doc(alias = "selected-item")]
93 pub fn selected_item(&self) -> Option<glib::Object> {
94 unsafe {
95 from_glib_none(ffi::gtk_single_selection_get_selected_item(
96 self.to_glib_none().0,
97 ))
98 }
99 }
100
101 #[doc(alias = "gtk_single_selection_set_autoselect")]
102 #[doc(alias = "autoselect")]
103 pub fn set_autoselect(&self, autoselect: bool) {
104 unsafe {
105 ffi::gtk_single_selection_set_autoselect(self.to_glib_none().0, autoselect.into_glib());
106 }
107 }
108
109 #[doc(alias = "gtk_single_selection_set_can_unselect")]
110 #[doc(alias = "can-unselect")]
111 pub fn set_can_unselect(&self, can_unselect: bool) {
112 unsafe {
113 ffi::gtk_single_selection_set_can_unselect(
114 self.to_glib_none().0,
115 can_unselect.into_glib(),
116 );
117 }
118 }
119
120 #[doc(alias = "gtk_single_selection_set_model")]
121 #[doc(alias = "model")]
122 pub fn set_model(&self, model: Option<&impl IsA<gio::ListModel>>) {
123 unsafe {
124 ffi::gtk_single_selection_set_model(
125 self.to_glib_none().0,
126 model.map(|p| p.as_ref()).to_glib_none().0,
127 );
128 }
129 }
130
131 #[doc(alias = "gtk_single_selection_set_selected")]
132 #[doc(alias = "selected")]
133 pub fn set_selected(&self, position: u32) {
134 unsafe {
135 ffi::gtk_single_selection_set_selected(self.to_glib_none().0, position);
136 }
137 }
138
139 #[doc(alias = "autoselect")]
140 pub fn connect_autoselect_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
141 unsafe extern "C" fn notify_autoselect_trampoline<F: Fn(&SingleSelection) + 'static>(
142 this: *mut ffi::GtkSingleSelection,
143 _param_spec: glib::ffi::gpointer,
144 f: glib::ffi::gpointer,
145 ) {
146 let f: &F = &*(f as *const F);
147 f(&from_glib_borrow(this))
148 }
149 unsafe {
150 let f: Box_<F> = Box_::new(f);
151 connect_raw(
152 self.as_ptr() as *mut _,
153 c"notify::autoselect".as_ptr() as *const _,
154 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
155 notify_autoselect_trampoline::<F> as *const (),
156 )),
157 Box_::into_raw(f),
158 )
159 }
160 }
161
162 #[doc(alias = "can-unselect")]
163 pub fn connect_can_unselect_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
164 unsafe extern "C" fn notify_can_unselect_trampoline<F: Fn(&SingleSelection) + 'static>(
165 this: *mut ffi::GtkSingleSelection,
166 _param_spec: glib::ffi::gpointer,
167 f: glib::ffi::gpointer,
168 ) {
169 let f: &F = &*(f as *const F);
170 f(&from_glib_borrow(this))
171 }
172 unsafe {
173 let f: Box_<F> = Box_::new(f);
174 connect_raw(
175 self.as_ptr() as *mut _,
176 c"notify::can-unselect".as_ptr() as *const _,
177 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
178 notify_can_unselect_trampoline::<F> as *const (),
179 )),
180 Box_::into_raw(f),
181 )
182 }
183 }
184
185 #[doc(alias = "model")]
186 pub fn connect_model_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
187 unsafe extern "C" fn notify_model_trampoline<F: Fn(&SingleSelection) + 'static>(
188 this: *mut ffi::GtkSingleSelection,
189 _param_spec: glib::ffi::gpointer,
190 f: glib::ffi::gpointer,
191 ) {
192 let f: &F = &*(f as *const F);
193 f(&from_glib_borrow(this))
194 }
195 unsafe {
196 let f: Box_<F> = Box_::new(f);
197 connect_raw(
198 self.as_ptr() as *mut _,
199 c"notify::model".as_ptr() as *const _,
200 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
201 notify_model_trampoline::<F> as *const (),
202 )),
203 Box_::into_raw(f),
204 )
205 }
206 }
207
208 #[doc(alias = "selected")]
209 pub fn connect_selected_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
210 unsafe extern "C" fn notify_selected_trampoline<F: Fn(&SingleSelection) + 'static>(
211 this: *mut ffi::GtkSingleSelection,
212 _param_spec: glib::ffi::gpointer,
213 f: glib::ffi::gpointer,
214 ) {
215 let f: &F = &*(f as *const F);
216 f(&from_glib_borrow(this))
217 }
218 unsafe {
219 let f: Box_<F> = Box_::new(f);
220 connect_raw(
221 self.as_ptr() as *mut _,
222 c"notify::selected".as_ptr() as *const _,
223 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
224 notify_selected_trampoline::<F> as *const (),
225 )),
226 Box_::into_raw(f),
227 )
228 }
229 }
230
231 #[doc(alias = "selected-item")]
232 pub fn connect_selected_item_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
233 unsafe extern "C" fn notify_selected_item_trampoline<F: Fn(&SingleSelection) + 'static>(
234 this: *mut ffi::GtkSingleSelection,
235 _param_spec: glib::ffi::gpointer,
236 f: glib::ffi::gpointer,
237 ) {
238 let f: &F = &*(f as *const F);
239 f(&from_glib_borrow(this))
240 }
241 unsafe {
242 let f: Box_<F> = Box_::new(f);
243 connect_raw(
244 self.as_ptr() as *mut _,
245 c"notify::selected-item".as_ptr() as *const _,
246 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
247 notify_selected_item_trampoline::<F> as *const (),
248 )),
249 Box_::into_raw(f),
250 )
251 }
252 }
253}
254
255impl Default for SingleSelection {
256 fn default() -> Self {
257 glib::object::Object::new::<Self>()
258 }
259}
260
261#[must_use = "The builder must be built to be used"]
266pub struct SingleSelectionBuilder {
267 builder: glib::object::ObjectBuilder<'static, SingleSelection>,
268}
269
270impl SingleSelectionBuilder {
271 fn new() -> Self {
272 Self {
273 builder: glib::object::Object::builder(),
274 }
275 }
276
277 pub fn autoselect(self, autoselect: bool) -> Self {
278 Self {
279 builder: self.builder.property("autoselect", autoselect),
280 }
281 }
282
283 pub fn can_unselect(self, can_unselect: bool) -> Self {
284 Self {
285 builder: self.builder.property("can-unselect", can_unselect),
286 }
287 }
288
289 pub fn model(self, model: &impl IsA<gio::ListModel>) -> Self {
290 Self {
291 builder: self.builder.property("model", model.clone().upcast()),
292 }
293 }
294
295 pub fn selected(self, selected: u32) -> Self {
296 Self {
297 builder: self.builder.property("selected", selected),
298 }
299 }
300
301 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
304 pub fn build(self) -> SingleSelection {
305 assert_initialized_main_thread!();
306 self.builder.build()
307 }
308}