gtk4/subclass/
selection_model.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3// rustdoc-stripper-ignore-next
4//! Traits intended for implementing the
5//! [`SelectionModel`](crate::SelectionModel) interface.
6
7use glib::translate::*;
8
9use crate::{ffi, prelude::*, subclass::prelude::*, Bitset, SelectionModel};
10
11pub trait SelectionModelImpl: ListModelImpl {
12    #[doc(alias = "get_selection_in_range")]
13    fn selection_in_range(&self, position: u32, n_items: u32) -> Bitset {
14        self.parent_selection_in_range(position, n_items)
15    }
16
17    fn is_selected(&self, position: u32) -> bool {
18        self.parent_is_selected(position)
19    }
20
21    fn select_all(&self) -> bool {
22        self.parent_select_all()
23    }
24
25    fn select_item(&self, position: u32, unselect_rest: bool) -> bool {
26        self.parent_select_item(position, unselect_rest)
27    }
28
29    fn select_range(&self, position: u32, n_items: u32, unselect_rest: bool) -> bool {
30        self.parent_select_range(position, n_items, unselect_rest)
31    }
32
33    fn set_selection(&self, selected: &Bitset, mask: &Bitset) -> bool {
34        self.parent_set_selection(selected, mask)
35    }
36
37    fn unselect_all(&self) -> bool {
38        self.parent_unselect_all()
39    }
40
41    fn unselect_item(&self, position: u32) -> bool {
42        self.parent_unselect_item(position)
43    }
44
45    fn unselect_range(&self, position: u32, n_items: u32) -> bool {
46        self.parent_unselect_range(position, n_items)
47    }
48}
49
50mod sealed {
51    pub trait Sealed {}
52    impl<T: super::SelectionModelImplExt> Sealed for T {}
53}
54
55pub trait SelectionModelImplExt: sealed::Sealed + ObjectSubclass {
56    fn parent_selection_in_range(&self, position: u32, n_items: u32) -> Bitset {
57        unsafe {
58            let type_data = Self::type_data();
59            let parent_iface = type_data.as_ref().parent_interface::<SelectionModel>()
60                as *const ffi::GtkSelectionModelInterface;
61
62            let func = (*parent_iface)
63                .get_selection_in_range
64                .expect("no parent \"get_selection_in_range\" implementation");
65
66            from_glib_full(func(
67                self.obj()
68                    .unsafe_cast_ref::<SelectionModel>()
69                    .to_glib_none()
70                    .0,
71                position,
72                n_items,
73            ))
74        }
75    }
76
77    fn parent_is_selected(&self, position: u32) -> bool {
78        unsafe {
79            let type_data = Self::type_data();
80            let parent_iface = type_data.as_ref().parent_interface::<SelectionModel>()
81                as *const ffi::GtkSelectionModelInterface;
82
83            let func = (*parent_iface)
84                .is_selected
85                .expect("no parent \"is_selected\" implementation");
86
87            from_glib(func(
88                self.obj()
89                    .unsafe_cast_ref::<SelectionModel>()
90                    .to_glib_none()
91                    .0,
92                position,
93            ))
94        }
95    }
96
97    fn parent_select_all(&self) -> bool {
98        unsafe {
99            let type_data = Self::type_data();
100            let parent_iface = type_data.as_ref().parent_interface::<SelectionModel>()
101                as *const ffi::GtkSelectionModelInterface;
102
103            let func = (*parent_iface)
104                .select_all
105                .expect("no parent \"select_all\" implementation");
106
107            from_glib(func(
108                self.obj()
109                    .unsafe_cast_ref::<SelectionModel>()
110                    .to_glib_none()
111                    .0,
112            ))
113        }
114    }
115
116    fn parent_select_item(&self, position: u32, unselect_rest: bool) -> bool {
117        unsafe {
118            let type_data = Self::type_data();
119            let parent_iface = type_data.as_ref().parent_interface::<SelectionModel>()
120                as *const ffi::GtkSelectionModelInterface;
121
122            let func = (*parent_iface)
123                .select_item
124                .expect("no parent \"select_item\" implementation");
125
126            from_glib(func(
127                self.obj()
128                    .unsafe_cast_ref::<SelectionModel>()
129                    .to_glib_none()
130                    .0,
131                position,
132                unselect_rest.into_glib(),
133            ))
134        }
135    }
136
137    fn parent_select_range(&self, position: u32, n_items: u32, unselect_rest: bool) -> bool {
138        unsafe {
139            let type_data = Self::type_data();
140            let parent_iface = type_data.as_ref().parent_interface::<SelectionModel>()
141                as *const ffi::GtkSelectionModelInterface;
142
143            let func = (*parent_iface)
144                .select_range
145                .expect("no parent \"select_range\" implementation");
146
147            from_glib(func(
148                self.obj()
149                    .unsafe_cast_ref::<SelectionModel>()
150                    .to_glib_none()
151                    .0,
152                position,
153                n_items,
154                unselect_rest.into_glib(),
155            ))
156        }
157    }
158
159    fn parent_set_selection(&self, selected: &Bitset, mask: &Bitset) -> bool {
160        unsafe {
161            let type_data = Self::type_data();
162            let parent_iface = type_data.as_ref().parent_interface::<SelectionModel>()
163                as *const ffi::GtkSelectionModelInterface;
164
165            let func = (*parent_iface)
166                .set_selection
167                .expect("no parent \"set_selection\" implementation");
168
169            from_glib(func(
170                self.obj()
171                    .unsafe_cast_ref::<SelectionModel>()
172                    .to_glib_none()
173                    .0,
174                selected.to_glib_none().0,
175                mask.to_glib_none().0,
176            ))
177        }
178    }
179
180    fn parent_unselect_all(&self) -> bool {
181        unsafe {
182            let type_data = Self::type_data();
183            let parent_iface = type_data.as_ref().parent_interface::<SelectionModel>()
184                as *const ffi::GtkSelectionModelInterface;
185
186            let func = (*parent_iface)
187                .unselect_all
188                .expect("no parent \"unselect_all\" implementation");
189
190            from_glib(func(
191                self.obj()
192                    .unsafe_cast_ref::<SelectionModel>()
193                    .to_glib_none()
194                    .0,
195            ))
196        }
197    }
198
199    fn parent_unselect_item(&self, position: u32) -> bool {
200        unsafe {
201            let type_data = Self::type_data();
202            let parent_iface = type_data.as_ref().parent_interface::<SelectionModel>()
203                as *const ffi::GtkSelectionModelInterface;
204
205            let func = (*parent_iface)
206                .unselect_item
207                .expect("no parent \"unselect_item\" implementation");
208
209            from_glib(func(
210                self.obj()
211                    .unsafe_cast_ref::<SelectionModel>()
212                    .to_glib_none()
213                    .0,
214                position,
215            ))
216        }
217    }
218
219    fn parent_unselect_range(&self, position: u32, n_items: u32) -> bool {
220        unsafe {
221            let type_data = Self::type_data();
222            let parent_iface = type_data.as_ref().parent_interface::<SelectionModel>()
223                as *const ffi::GtkSelectionModelInterface;
224
225            let func = (*parent_iface)
226                .unselect_range
227                .expect("no parent \"unselect_range\" implementation");
228
229            from_glib(func(
230                self.obj()
231                    .unsafe_cast_ref::<SelectionModel>()
232                    .to_glib_none()
233                    .0,
234                position,
235                n_items,
236            ))
237        }
238    }
239}
240
241impl<T: SelectionModelImpl> SelectionModelImplExt for T {}
242
243unsafe impl<T: SelectionModelImpl> IsImplementable<T> for SelectionModel {
244    fn interface_init(iface: &mut glib::Interface<Self>) {
245        let iface = iface.as_mut();
246
247        assert_initialized_main_thread!();
248
249        iface.get_selection_in_range = Some(model_get_selection_in_range::<T>);
250        iface.is_selected = Some(model_is_selected::<T>);
251        iface.select_all = Some(model_select_all::<T>);
252        iface.select_item = Some(model_select_item::<T>);
253        iface.select_range = Some(model_select_range::<T>);
254        iface.set_selection = Some(model_set_selection::<T>);
255        iface.unselect_all = Some(model_unselect_all::<T>);
256        iface.unselect_item = Some(model_unselect_item::<T>);
257        iface.unselect_range = Some(model_unselect_range::<T>);
258    }
259}
260
261unsafe extern "C" fn model_get_selection_in_range<T: SelectionModelImpl>(
262    model: *mut ffi::GtkSelectionModel,
263    position: u32,
264    n_items: u32,
265) -> *mut ffi::GtkBitset {
266    let instance = &*(model as *mut T::Instance);
267    let imp = instance.imp();
268
269    imp.selection_in_range(position, n_items).into_glib_ptr()
270}
271
272unsafe extern "C" fn model_is_selected<T: SelectionModelImpl>(
273    model: *mut ffi::GtkSelectionModel,
274    position: u32,
275) -> glib::ffi::gboolean {
276    let instance = &*(model as *mut T::Instance);
277    let imp = instance.imp();
278
279    imp.is_selected(position).into_glib()
280}
281
282unsafe extern "C" fn model_select_all<T: SelectionModelImpl>(
283    model: *mut ffi::GtkSelectionModel,
284) -> glib::ffi::gboolean {
285    let instance = &*(model as *mut T::Instance);
286    let imp = instance.imp();
287
288    imp.select_all().into_glib()
289}
290
291unsafe extern "C" fn model_select_item<T: SelectionModelImpl>(
292    model: *mut ffi::GtkSelectionModel,
293    position: u32,
294    unselect_rest: glib::ffi::gboolean,
295) -> glib::ffi::gboolean {
296    let instance = &*(model as *mut T::Instance);
297    let imp = instance.imp();
298
299    imp.select_item(position, from_glib(unselect_rest))
300        .into_glib()
301}
302
303unsafe extern "C" fn model_select_range<T: SelectionModelImpl>(
304    model: *mut ffi::GtkSelectionModel,
305    position: u32,
306    n_items: u32,
307    unselect_rest: glib::ffi::gboolean,
308) -> glib::ffi::gboolean {
309    let instance = &*(model as *mut T::Instance);
310    let imp = instance.imp();
311
312    imp.select_range(position, n_items, from_glib(unselect_rest))
313        .into_glib()
314}
315
316unsafe extern "C" fn model_set_selection<T: SelectionModelImpl>(
317    model: *mut ffi::GtkSelectionModel,
318    selected_ptr: *mut ffi::GtkBitset,
319    mask_ptr: *mut ffi::GtkBitset,
320) -> glib::ffi::gboolean {
321    let instance = &*(model as *mut T::Instance);
322    let imp = instance.imp();
323
324    let selected = from_glib_borrow(selected_ptr);
325    let mask = from_glib_borrow(mask_ptr);
326
327    imp.set_selection(&selected, &mask).into_glib()
328}
329
330unsafe extern "C" fn model_unselect_all<T: SelectionModelImpl>(
331    model: *mut ffi::GtkSelectionModel,
332) -> glib::ffi::gboolean {
333    let instance = &*(model as *mut T::Instance);
334    let imp = instance.imp();
335
336    imp.unselect_all().into_glib()
337}
338
339unsafe extern "C" fn model_unselect_item<T: SelectionModelImpl>(
340    model: *mut ffi::GtkSelectionModel,
341    position: u32,
342) -> glib::ffi::gboolean {
343    let instance = &*(model as *mut T::Instance);
344    let imp = instance.imp();
345
346    imp.unselect_item(position).into_glib()
347}
348
349unsafe extern "C" fn model_unselect_range<T: SelectionModelImpl>(
350    model: *mut ffi::GtkSelectionModel,
351    position: u32,
352    n_items: u32,
353) -> glib::ffi::gboolean {
354    let instance = &*(model as *mut T::Instance);
355    let imp = instance.imp();
356
357    imp.unselect_range(position, n_items).into_glib()
358}