gtk4/subclass/
section_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 [`SectionModel`](crate::SectionModel)
5//! interface.
6
7use glib::translate::*;
8
9use crate::{ffi, prelude::*, subclass::prelude::*, SectionModel};
10
11pub trait SectionModelImpl: ListModelImpl {
12    #[doc(alias = "get_section")]
13    fn section(&self, position: u32) -> (u32, u32) {
14        self.parent_section(position)
15    }
16}
17
18mod sealed {
19    pub trait Sealed {}
20    impl<T: super::SectionModelImplExt> Sealed for T {}
21}
22
23pub trait SectionModelImplExt: sealed::Sealed + ObjectSubclass {
24    fn parent_section(&self, position: u32) -> (u32, u32) {
25        unsafe {
26            let type_data = Self::type_data();
27            let parent_iface = type_data.as_ref().parent_interface::<SectionModel>()
28                as *const ffi::GtkSectionModelInterface;
29
30            let func = (*parent_iface)
31                .get_section
32                .expect("no parent \"get_section\" implementation");
33
34            let mut start = std::mem::MaybeUninit::uninit();
35            let mut end = std::mem::MaybeUninit::uninit();
36            func(
37                self.obj()
38                    .unsafe_cast_ref::<SectionModel>()
39                    .to_glib_none()
40                    .0,
41                position,
42                start.as_mut_ptr(),
43                end.as_mut_ptr(),
44            );
45            (start.assume_init(), end.assume_init())
46        }
47    }
48}
49
50impl<T: SectionModelImpl> SectionModelImplExt for T {}
51
52unsafe impl<T: SectionModelImpl> IsImplementable<T> for SectionModel {
53    fn interface_init(iface: &mut glib::Interface<Self>) {
54        let iface = iface.as_mut();
55
56        assert_initialized_main_thread!();
57
58        iface.get_section = Some(model_get_section::<T>);
59    }
60}
61
62unsafe extern "C" fn model_get_section<T: SectionModelImpl>(
63    model: *mut ffi::GtkSectionModel,
64    position: u32,
65    startptr: *mut libc::c_uint,
66    endptr: *mut libc::c_uint,
67) {
68    let instance = &*(model as *mut T::Instance);
69    let imp = instance.imp();
70
71    let (start, end) = imp.section(position);
72    *startptr = start;
73    *endptr = end;
74}