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