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")]
75 pub struct SingleSelection(Object<ffi::GtkSingleSelection, ffi::GtkSingleSelectionClass>) @implements gio::ListModel, SectionModel, SelectionModel;
76
77 match fn {
78 type_ => || ffi::gtk_single_selection_get_type(),
79 }
80}
81
82#[cfg(not(any(feature = "v4_12")))]
83glib::wrapper! {
84 #[doc(alias = "GtkSingleSelection")]
85 pub struct SingleSelection(Object<ffi::GtkSingleSelection, ffi::GtkSingleSelectionClass>) @implements gio::ListModel, SelectionModel;
86
87 match fn {
88 type_ => || ffi::gtk_single_selection_get_type(),
89 }
90}
91
92impl SingleSelection {
93 #[doc(alias = "gtk_single_selection_new")]
101 pub fn new(model: Option<impl IsA<gio::ListModel>>) -> SingleSelection {
102 assert_initialized_main_thread!();
103 unsafe {
104 from_glib_full(ffi::gtk_single_selection_new(
105 model.map(|p| p.upcast()).into_glib_ptr(),
106 ))
107 }
108 }
109
110 pub fn builder() -> SingleSelectionBuilder {
115 SingleSelectionBuilder::new()
116 }
117
118 #[doc(alias = "gtk_single_selection_get_autoselect")]
125 #[doc(alias = "get_autoselect")]
126 #[doc(alias = "autoselect")]
127 pub fn is_autoselect(&self) -> bool {
128 unsafe {
129 from_glib(ffi::gtk_single_selection_get_autoselect(
130 self.to_glib_none().0,
131 ))
132 }
133 }
134
135 #[doc(alias = "gtk_single_selection_get_can_unselect")]
142 #[doc(alias = "get_can_unselect")]
143 #[doc(alias = "can-unselect")]
144 pub fn can_unselect(&self) -> bool {
145 unsafe {
146 from_glib(ffi::gtk_single_selection_get_can_unselect(
147 self.to_glib_none().0,
148 ))
149 }
150 }
151
152 #[doc(alias = "gtk_single_selection_get_model")]
158 #[doc(alias = "get_model")]
159 pub fn model(&self) -> Option<gio::ListModel> {
160 unsafe { from_glib_none(ffi::gtk_single_selection_get_model(self.to_glib_none().0)) }
161 }
162
163 #[doc(alias = "gtk_single_selection_get_selected")]
171 #[doc(alias = "get_selected")]
172 pub fn selected(&self) -> u32 {
173 unsafe { ffi::gtk_single_selection_get_selected(self.to_glib_none().0) }
174 }
175
176 #[doc(alias = "gtk_single_selection_get_selected_item")]
184 #[doc(alias = "get_selected_item")]
185 #[doc(alias = "selected-item")]
186 pub fn selected_item(&self) -> Option<glib::Object> {
187 unsafe {
188 from_glib_none(ffi::gtk_single_selection_get_selected_item(
189 self.to_glib_none().0,
190 ))
191 }
192 }
193
194 #[doc(alias = "gtk_single_selection_set_autoselect")]
202 #[doc(alias = "autoselect")]
203 pub fn set_autoselect(&self, autoselect: bool) {
204 unsafe {
205 ffi::gtk_single_selection_set_autoselect(self.to_glib_none().0, autoselect.into_glib());
206 }
207 }
208
209 #[doc(alias = "gtk_single_selection_set_can_unselect")]
218 #[doc(alias = "can-unselect")]
219 pub fn set_can_unselect(&self, can_unselect: bool) {
220 unsafe {
221 ffi::gtk_single_selection_set_can_unselect(
222 self.to_glib_none().0,
223 can_unselect.into_glib(),
224 );
225 }
226 }
227
228 #[doc(alias = "gtk_single_selection_set_model")]
234 #[doc(alias = "model")]
235 pub fn set_model(&self, model: Option<&impl IsA<gio::ListModel>>) {
236 unsafe {
237 ffi::gtk_single_selection_set_model(
238 self.to_glib_none().0,
239 model.map(|p| p.as_ref()).to_glib_none().0,
240 );
241 }
242 }
243
244 #[doc(alias = "gtk_single_selection_set_selected")]
256 #[doc(alias = "selected")]
257 pub fn set_selected(&self, position: u32) {
258 unsafe {
259 ffi::gtk_single_selection_set_selected(self.to_glib_none().0, position);
260 }
261 }
262
263 #[doc(alias = "autoselect")]
264 pub fn connect_autoselect_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
265 unsafe extern "C" fn notify_autoselect_trampoline<F: Fn(&SingleSelection) + 'static>(
266 this: *mut ffi::GtkSingleSelection,
267 _param_spec: glib::ffi::gpointer,
268 f: glib::ffi::gpointer,
269 ) {
270 let f: &F = &*(f as *const F);
271 f(&from_glib_borrow(this))
272 }
273 unsafe {
274 let f: Box_<F> = Box_::new(f);
275 connect_raw(
276 self.as_ptr() as *mut _,
277 b"notify::autoselect\0".as_ptr() as *const _,
278 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
279 notify_autoselect_trampoline::<F> as *const (),
280 )),
281 Box_::into_raw(f),
282 )
283 }
284 }
285
286 #[doc(alias = "can-unselect")]
287 pub fn connect_can_unselect_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
288 unsafe extern "C" fn notify_can_unselect_trampoline<F: Fn(&SingleSelection) + 'static>(
289 this: *mut ffi::GtkSingleSelection,
290 _param_spec: glib::ffi::gpointer,
291 f: glib::ffi::gpointer,
292 ) {
293 let f: &F = &*(f as *const F);
294 f(&from_glib_borrow(this))
295 }
296 unsafe {
297 let f: Box_<F> = Box_::new(f);
298 connect_raw(
299 self.as_ptr() as *mut _,
300 b"notify::can-unselect\0".as_ptr() as *const _,
301 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
302 notify_can_unselect_trampoline::<F> as *const (),
303 )),
304 Box_::into_raw(f),
305 )
306 }
307 }
308
309 #[doc(alias = "model")]
310 pub fn connect_model_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
311 unsafe extern "C" fn notify_model_trampoline<F: Fn(&SingleSelection) + 'static>(
312 this: *mut ffi::GtkSingleSelection,
313 _param_spec: glib::ffi::gpointer,
314 f: glib::ffi::gpointer,
315 ) {
316 let f: &F = &*(f as *const F);
317 f(&from_glib_borrow(this))
318 }
319 unsafe {
320 let f: Box_<F> = Box_::new(f);
321 connect_raw(
322 self.as_ptr() as *mut _,
323 b"notify::model\0".as_ptr() as *const _,
324 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
325 notify_model_trampoline::<F> as *const (),
326 )),
327 Box_::into_raw(f),
328 )
329 }
330 }
331
332 #[doc(alias = "selected")]
333 pub fn connect_selected_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
334 unsafe extern "C" fn notify_selected_trampoline<F: Fn(&SingleSelection) + 'static>(
335 this: *mut ffi::GtkSingleSelection,
336 _param_spec: glib::ffi::gpointer,
337 f: glib::ffi::gpointer,
338 ) {
339 let f: &F = &*(f as *const F);
340 f(&from_glib_borrow(this))
341 }
342 unsafe {
343 let f: Box_<F> = Box_::new(f);
344 connect_raw(
345 self.as_ptr() as *mut _,
346 b"notify::selected\0".as_ptr() as *const _,
347 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
348 notify_selected_trampoline::<F> as *const (),
349 )),
350 Box_::into_raw(f),
351 )
352 }
353 }
354
355 #[doc(alias = "selected-item")]
356 pub fn connect_selected_item_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
357 unsafe extern "C" fn notify_selected_item_trampoline<F: Fn(&SingleSelection) + 'static>(
358 this: *mut ffi::GtkSingleSelection,
359 _param_spec: glib::ffi::gpointer,
360 f: glib::ffi::gpointer,
361 ) {
362 let f: &F = &*(f as *const F);
363 f(&from_glib_borrow(this))
364 }
365 unsafe {
366 let f: Box_<F> = Box_::new(f);
367 connect_raw(
368 self.as_ptr() as *mut _,
369 b"notify::selected-item\0".as_ptr() as *const _,
370 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
371 notify_selected_item_trampoline::<F> as *const (),
372 )),
373 Box_::into_raw(f),
374 )
375 }
376 }
377}
378
379impl Default for SingleSelection {
380 fn default() -> Self {
381 glib::object::Object::new::<Self>()
382 }
383}
384
385#[must_use = "The builder must be built to be used"]
390pub struct SingleSelectionBuilder {
391 builder: glib::object::ObjectBuilder<'static, SingleSelection>,
392}
393
394impl SingleSelectionBuilder {
395 fn new() -> Self {
396 Self {
397 builder: glib::object::Object::builder(),
398 }
399 }
400
401 pub fn autoselect(self, autoselect: bool) -> Self {
403 Self {
404 builder: self.builder.property("autoselect", autoselect),
405 }
406 }
407
408 pub fn can_unselect(self, can_unselect: bool) -> Self {
410 Self {
411 builder: self.builder.property("can-unselect", can_unselect),
412 }
413 }
414
415 pub fn model(self, model: &impl IsA<gio::ListModel>) -> Self {
417 Self {
418 builder: self.builder.property("model", model.clone().upcast()),
419 }
420 }
421
422 pub fn selected(self, selected: u32) -> Self {
424 Self {
425 builder: self.builder.property("selected", selected),
426 }
427 }
428
429 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
432 pub fn build(self) -> SingleSelection {
433 assert_initialized_main_thread!();
434 self.builder.build()
435 }
436}