1#[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 #[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 #[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 pub fn builder() -> SingleSelectionBuilder {
114 SingleSelectionBuilder::new()
115 }
116
117 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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#[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 pub fn autoselect(self, autoselect: bool) -> Self {
412 Self {
413 builder: self.builder.property("autoselect", autoselect),
414 }
415 }
416
417 pub fn can_unselect(self, can_unselect: bool) -> Self {
419 Self {
420 builder: self.builder.property("can-unselect", can_unselect),
421 }
422 }
423
424 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 pub fn selected(self, selected: u32) -> Self {
433 Self {
434 builder: self.builder.property("selected", selected),
435 }
436 }
437
438 #[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}