1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Take a look at the license at the top of the repository in the LICENSE file.

// rustdoc-stripper-ignore-next
//! Traits intended for implementing the [`Paintable`](crate::Paintable) interface.

use crate::{prelude::*, subclass::prelude::*, Paintable, PaintableFlags, Snapshot};
use glib::translate::*;

pub trait PaintableImpl: PaintableImplExt + ObjectImpl {
    #[doc(alias = "get_current_image")]
    fn current_image(&self) -> Paintable {
        self.parent_current_image()
    }

    #[doc(alias = "get_flags")]
    fn flags(&self) -> PaintableFlags {
        self.parent_flags()
    }

    #[doc(alias = "get_intrinsic_width")]
    fn intrinsic_width(&self) -> i32 {
        self.parent_intrinsic_width()
    }

    #[doc(alias = "get_intrinsic_height")]
    fn intrinsic_height(&self) -> i32 {
        self.parent_intrinsic_height()
    }

    #[doc(alias = "get_intrinsic_aspect_ratio")]
    fn intrinsic_aspect_ratio(&self) -> f64 {
        self.parent_intrinsic_aspect_ratio()
    }

    fn snapshot(&self, snapshot: &Snapshot, width: f64, height: f64);
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::PaintableImplExt> Sealed for T {}
}

pub trait PaintableImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_current_image(&self) -> Paintable {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Paintable>()
                as *const ffi::GdkPaintableInterface;
            let func = (*parent_iface)
                .get_current_image
                .expect("no parent \"get_current_image\" implementation");

            let ret = func(self.obj().unsafe_cast_ref::<Paintable>().to_glib_none().0);

            from_glib_full(ret)
        }
    }

    fn parent_flags(&self) -> PaintableFlags {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Paintable>()
                as *const ffi::GdkPaintableInterface;
            let func = (*parent_iface)
                .get_flags
                .expect("no parent \"get_flags\" implementation");

            from_glib(func(
                self.obj().unsafe_cast_ref::<Paintable>().to_glib_none().0,
            ))
        }
    }

    fn parent_intrinsic_width(&self) -> i32 {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Paintable>()
                as *const ffi::GdkPaintableInterface;
            let func = (*parent_iface)
                .get_intrinsic_width
                .expect("no parent \"get_intrinsic_width\" implementation");

            func(self.obj().unsafe_cast_ref::<Paintable>().to_glib_none().0)
        }
    }

    fn parent_intrinsic_height(&self) -> i32 {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Paintable>()
                as *const ffi::GdkPaintableInterface;
            let func = (*parent_iface)
                .get_intrinsic_height
                .expect("no parent \"get_intrinsic_height\" implementation");

            func(self.obj().unsafe_cast_ref::<Paintable>().to_glib_none().0)
        }
    }

    fn parent_intrinsic_aspect_ratio(&self) -> f64 {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Paintable>()
                as *const ffi::GdkPaintableInterface;
            let func = (*parent_iface)
                .get_intrinsic_aspect_ratio
                .expect("no parent \"get_intrinsic_aspect_ratio\" implementation");

            func(self.obj().unsafe_cast_ref::<Paintable>().to_glib_none().0)
        }
    }

    fn parent_snapshot(&self, snapshot: &Snapshot, width: f64, height: f64) {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Paintable>()
                as *const ffi::GdkPaintableInterface;
            let func = (*parent_iface)
                .snapshot
                .expect("no parent \"snapshot\" implementation");

            func(
                self.obj().unsafe_cast_ref::<Paintable>().to_glib_none().0,
                snapshot.to_glib_none().0,
                width,
                height,
            )
        }
    }
}

impl<T: PaintableImpl> PaintableImplExt for T {}

unsafe impl<T: PaintableImpl> IsImplementable<T> for Paintable {
    fn interface_init(iface: &mut glib::Interface<Self>) {
        let iface = iface.as_mut();

        iface.get_current_image = Some(paintable_get_current_image::<T>);
        iface.get_flags = Some(paintable_get_flags::<T>);
        iface.get_intrinsic_width = Some(paintable_get_intrinsic_width::<T>);
        iface.get_intrinsic_height = Some(paintable_get_intrinsic_height::<T>);
        iface.get_intrinsic_aspect_ratio = Some(paintable_get_intrinsic_aspect_ratio::<T>);
        iface.snapshot = Some(paintable_snapshot::<T>);
    }
}

unsafe extern "C" fn paintable_get_current_image<T: PaintableImpl>(
    paintable: *mut ffi::GdkPaintable,
) -> *mut ffi::GdkPaintable {
    let instance = &*(paintable as *mut T::Instance);
    let imp = instance.imp();

    imp.current_image().into_glib_ptr()
}

unsafe extern "C" fn paintable_get_flags<T: PaintableImpl>(
    paintable: *mut ffi::GdkPaintable,
) -> ffi::GdkPaintableFlags {
    let instance = &*(paintable as *mut T::Instance);
    let imp = instance.imp();

    imp.flags().into_glib()
}

unsafe extern "C" fn paintable_get_intrinsic_width<T: PaintableImpl>(
    paintable: *mut ffi::GdkPaintable,
) -> i32 {
    let instance = &*(paintable as *mut T::Instance);
    let imp = instance.imp();

    imp.intrinsic_width()
}

unsafe extern "C" fn paintable_get_intrinsic_height<T: PaintableImpl>(
    paintable: *mut ffi::GdkPaintable,
) -> i32 {
    let instance = &*(paintable as *mut T::Instance);
    let imp = instance.imp();

    imp.intrinsic_height()
}

unsafe extern "C" fn paintable_get_intrinsic_aspect_ratio<T: PaintableImpl>(
    paintable: *mut ffi::GdkPaintable,
) -> f64 {
    let instance = &*(paintable as *mut T::Instance);
    let imp = instance.imp();

    imp.intrinsic_aspect_ratio()
}

unsafe extern "C" fn paintable_snapshot<T: PaintableImpl>(
    paintable: *mut ffi::GdkPaintable,
    snapshotptr: *mut ffi::GdkSnapshot,
    width: f64,
    height: f64,
) {
    let instance = &*(paintable as *mut T::Instance);
    let imp = instance.imp();

    imp.snapshot(&Snapshot::from_glib_borrow(snapshotptr), width, height)
}