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::{ffi, prelude::*, subclass::prelude::*, Paintable, PaintableFlags, Snapshot};
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    let instance = &*(paintable as *mut T::Instance);
147    let imp = instance.imp();
148
149    imp.current_image().into_glib_ptr()
150}
151
152unsafe extern "C" fn paintable_get_flags<T: PaintableImpl>(
153    paintable: *mut ffi::GdkPaintable,
154) -> ffi::GdkPaintableFlags {
155    let instance = &*(paintable as *mut T::Instance);
156    let imp = instance.imp();
157
158    imp.flags().into_glib()
159}
160
161unsafe extern "C" fn paintable_get_intrinsic_width<T: PaintableImpl>(
162    paintable: *mut ffi::GdkPaintable,
163) -> i32 {
164    let instance = &*(paintable as *mut T::Instance);
165    let imp = instance.imp();
166
167    imp.intrinsic_width()
168}
169
170unsafe extern "C" fn paintable_get_intrinsic_height<T: PaintableImpl>(
171    paintable: *mut ffi::GdkPaintable,
172) -> i32 {
173    let instance = &*(paintable as *mut T::Instance);
174    let imp = instance.imp();
175
176    imp.intrinsic_height()
177}
178
179unsafe extern "C" fn paintable_get_intrinsic_aspect_ratio<T: PaintableImpl>(
180    paintable: *mut ffi::GdkPaintable,
181) -> f64 {
182    let instance = &*(paintable as *mut T::Instance);
183    let imp = instance.imp();
184
185    imp.intrinsic_aspect_ratio()
186}
187
188unsafe extern "C" fn paintable_snapshot<T: PaintableImpl>(
189    paintable: *mut ffi::GdkPaintable,
190    snapshotptr: *mut ffi::GdkSnapshot,
191    width: f64,
192    height: f64,
193) {
194    let instance = &*(paintable as *mut T::Instance);
195    let imp = instance.imp();
196
197    imp.snapshot(&Snapshot::from_glib_borrow(snapshotptr), width, height)
198}