Skip to main content

gdk4/subclass/
paintable.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 [`Paintable`] interface.
5
6use glib::translate::*;
7
8use crate::{Paintable, PaintableFlags, Snapshot, ffi, prelude::*, subclass::prelude::*};
9
10pub trait PaintableImpl: ObjectImpl + ObjectSubclass<Type: IsA<Paintable>> {
11    #[doc(alias = "get_current_image")]
12    fn current_image(&self) -> Paintable {
13        self.parent_current_image()
14    }
15
16    #[doc(alias = "get_flags")]
17    fn flags(&self) -> PaintableFlags {
18        self.parent_flags()
19    }
20
21    #[doc(alias = "get_intrinsic_width")]
22    fn intrinsic_width(&self) -> i32 {
23        self.parent_intrinsic_width()
24    }
25
26    #[doc(alias = "get_intrinsic_height")]
27    fn intrinsic_height(&self) -> i32 {
28        self.parent_intrinsic_height()
29    }
30
31    #[doc(alias = "get_intrinsic_aspect_ratio")]
32    fn intrinsic_aspect_ratio(&self) -> f64 {
33        self.parent_intrinsic_aspect_ratio()
34    }
35
36    fn snapshot(&self, snapshot: &Snapshot, width: f64, height: f64);
37}
38
39pub trait PaintableImplExt: PaintableImpl {
40    fn parent_current_image(&self) -> Paintable {
41        unsafe {
42            let type_data = Self::type_data();
43            let parent_iface = type_data.as_ref().parent_interface::<Paintable>()
44                as *const ffi::GdkPaintableInterface;
45            let func = (*parent_iface)
46                .get_current_image
47                .expect("no parent \"get_current_image\" implementation");
48
49            let ret = func(self.obj().unsafe_cast_ref::<Paintable>().to_glib_none().0);
50
51            from_glib_full(ret)
52        }
53    }
54
55    fn parent_flags(&self) -> PaintableFlags {
56        unsafe {
57            let type_data = Self::type_data();
58            let parent_iface = type_data.as_ref().parent_interface::<Paintable>()
59                as *const ffi::GdkPaintableInterface;
60            let func = (*parent_iface)
61                .get_flags
62                .expect("no parent \"get_flags\" implementation");
63
64            from_glib(func(
65                self.obj().unsafe_cast_ref::<Paintable>().to_glib_none().0,
66            ))
67        }
68    }
69
70    fn parent_intrinsic_width(&self) -> i32 {
71        unsafe {
72            let type_data = Self::type_data();
73            let parent_iface = type_data.as_ref().parent_interface::<Paintable>()
74                as *const ffi::GdkPaintableInterface;
75            let func = (*parent_iface)
76                .get_intrinsic_width
77                .expect("no parent \"get_intrinsic_width\" implementation");
78
79            func(self.obj().unsafe_cast_ref::<Paintable>().to_glib_none().0)
80        }
81    }
82
83    fn parent_intrinsic_height(&self) -> i32 {
84        unsafe {
85            let type_data = Self::type_data();
86            let parent_iface = type_data.as_ref().parent_interface::<Paintable>()
87                as *const ffi::GdkPaintableInterface;
88            let func = (*parent_iface)
89                .get_intrinsic_height
90                .expect("no parent \"get_intrinsic_height\" implementation");
91
92            func(self.obj().unsafe_cast_ref::<Paintable>().to_glib_none().0)
93        }
94    }
95
96    fn parent_intrinsic_aspect_ratio(&self) -> f64 {
97        unsafe {
98            let type_data = Self::type_data();
99            let parent_iface = type_data.as_ref().parent_interface::<Paintable>()
100                as *const ffi::GdkPaintableInterface;
101            let func = (*parent_iface)
102                .get_intrinsic_aspect_ratio
103                .expect("no parent \"get_intrinsic_aspect_ratio\" implementation");
104
105            func(self.obj().unsafe_cast_ref::<Paintable>().to_glib_none().0)
106        }
107    }
108
109    fn parent_snapshot(&self, snapshot: &Snapshot, width: f64, height: f64) {
110        unsafe {
111            let type_data = Self::type_data();
112            let parent_iface = type_data.as_ref().parent_interface::<Paintable>()
113                as *const ffi::GdkPaintableInterface;
114            let func = (*parent_iface)
115                .snapshot
116                .expect("no parent \"snapshot\" implementation");
117
118            func(
119                self.obj().unsafe_cast_ref::<Paintable>().to_glib_none().0,
120                snapshot.to_glib_none().0,
121                width,
122                height,
123            )
124        }
125    }
126}
127
128impl<T: PaintableImpl> PaintableImplExt for T {}
129
130unsafe impl<T: PaintableImpl> IsImplementable<T> for Paintable {
131    fn interface_init(iface: &mut glib::Interface<Self>) {
132        let iface = iface.as_mut();
133
134        iface.get_current_image = Some(paintable_get_current_image::<T>);
135        iface.get_flags = Some(paintable_get_flags::<T>);
136        iface.get_intrinsic_width = Some(paintable_get_intrinsic_width::<T>);
137        iface.get_intrinsic_height = Some(paintable_get_intrinsic_height::<T>);
138        iface.get_intrinsic_aspect_ratio = Some(paintable_get_intrinsic_aspect_ratio::<T>);
139        iface.snapshot = Some(paintable_snapshot::<T>);
140    }
141}
142
143unsafe extern "C" fn paintable_get_current_image<T: PaintableImpl>(
144    paintable: *mut ffi::GdkPaintable,
145) -> *mut ffi::GdkPaintable {
146    unsafe {
147        let instance = &*(paintable as *mut T::Instance);
148        let imp = instance.imp();
149
150        imp.current_image().into_glib_ptr()
151    }
152}
153
154unsafe extern "C" fn paintable_get_flags<T: PaintableImpl>(
155    paintable: *mut ffi::GdkPaintable,
156) -> ffi::GdkPaintableFlags {
157    unsafe {
158        let instance = &*(paintable as *mut T::Instance);
159        let imp = instance.imp();
160
161        imp.flags().into_glib()
162    }
163}
164
165unsafe extern "C" fn paintable_get_intrinsic_width<T: PaintableImpl>(
166    paintable: *mut ffi::GdkPaintable,
167) -> i32 {
168    unsafe {
169        let instance = &*(paintable as *mut T::Instance);
170        let imp = instance.imp();
171
172        imp.intrinsic_width()
173    }
174}
175
176unsafe extern "C" fn paintable_get_intrinsic_height<T: PaintableImpl>(
177    paintable: *mut ffi::GdkPaintable,
178) -> i32 {
179    unsafe {
180        let instance = &*(paintable as *mut T::Instance);
181        let imp = instance.imp();
182
183        imp.intrinsic_height()
184    }
185}
186
187unsafe extern "C" fn paintable_get_intrinsic_aspect_ratio<T: PaintableImpl>(
188    paintable: *mut ffi::GdkPaintable,
189) -> f64 {
190    unsafe {
191        let instance = &*(paintable as *mut T::Instance);
192        let imp = instance.imp();
193
194        imp.intrinsic_aspect_ratio()
195    }
196}
197
198unsafe extern "C" fn paintable_snapshot<T: PaintableImpl>(
199    paintable: *mut ffi::GdkPaintable,
200    snapshotptr: *mut ffi::GdkSnapshot,
201    width: f64,
202    height: f64,
203) {
204    unsafe {
205        let instance = &*(paintable as *mut T::Instance);
206        let imp = instance.imp();
207
208        imp.snapshot(&Snapshot::from_glib_borrow(snapshotptr), width, height)
209    }
210}