gtk4/subclass/
drawing_area.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 subclassing [`DrawingArea`].
5
6use glib::translate::*;
7
8use crate::{ffi, prelude::*, subclass::prelude::*, DrawingArea};
9
10pub trait DrawingAreaImpl: WidgetImpl + ObjectSubclass<Type: IsA<DrawingArea>> {
11    fn resize(&self, width: i32, height: i32) {
12        self.parent_resize(width, height)
13    }
14}
15
16pub trait DrawingAreaImplExt: DrawingAreaImpl {
17    fn parent_resize(&self, width: i32, height: i32) {
18        unsafe {
19            let data = Self::type_data();
20            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkDrawingAreaClass;
21            if let Some(f) = (*parent_class).resize {
22                f(
23                    self.obj().unsafe_cast_ref::<DrawingArea>().to_glib_none().0,
24                    width,
25                    height,
26                )
27            }
28        }
29    }
30}
31
32impl<T: DrawingAreaImpl> DrawingAreaImplExt for T {}
33
34unsafe impl<T: DrawingAreaImpl> IsSubclassable<T> for DrawingArea {
35    fn class_init(class: &mut glib::Class<Self>) {
36        Self::parent_class_init::<T>(class);
37
38        let klass = class.as_mut();
39        klass.resize = Some(drawing_area_resize::<T>);
40    }
41}
42
43unsafe extern "C" fn drawing_area_resize<T: DrawingAreaImpl>(
44    ptr: *mut ffi::GtkDrawingArea,
45    width: i32,
46    height: i32,
47) {
48    let instance = &*(ptr as *mut T::Instance);
49    let imp = instance.imp();
50
51    imp.resize(width, height)
52}