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")]
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 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#[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 pub fn autoselect(self, autoselect: bool) -> Self {
402 Self {
403 builder: self.builder.property("autoselect", autoselect),
404 }
405 }
406
407 pub fn can_unselect(self, can_unselect: bool) -> Self {
409 Self {
410 builder: self.builder.property("can-unselect", can_unselect),
411 }
412 }
413
414 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 pub fn selected(self, selected: u32) -> Self {
423 Self {
424 builder: self.builder.property("selected", selected),
425 }
426 }
427
428 #[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}