gtk4/subclass/
gl_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 [`GLArea`].
5
6use gdk::GLContext;
7use glib::translate::*;
8
9use crate::{ffi, prelude::*, subclass::prelude::*, GLArea};
10
11#[allow(clippy::upper_case_acronyms)]
12pub trait GLAreaImpl: WidgetImpl + ObjectSubclass<Type: IsA<GLArea>> {
13    /// class closure for the `GtkGLArea::create-context` signal
14    fn create_context(&self) -> Option<GLContext> {
15        self.parent_create_context()
16    }
17
18    /// class closure for the `GtkGLArea::render` signal
19    fn render(&self, context: &GLContext) -> glib::Propagation {
20        self.parent_render(context)
21    }
22
23    /// class closeure for the `GtkGLArea::resize` signal
24    fn resize(&self, width: i32, height: i32) {
25        self.parent_resize(width, height)
26    }
27}
28
29#[allow(clippy::upper_case_acronyms)]
30pub trait GLAreaImplExt: GLAreaImpl {
31    fn parent_create_context(&self) -> Option<GLContext> {
32        unsafe {
33            let data = Self::type_data();
34            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkGLAreaClass;
35            if let Some(f) = (*parent_class).create_context {
36                return Some(from_glib_none(f(self
37                    .obj()
38                    .unsafe_cast_ref::<GLArea>()
39                    .to_glib_none()
40                    .0)));
41            };
42            None
43        }
44    }
45
46    fn parent_render(&self, context: &GLContext) -> glib::Propagation {
47        unsafe {
48            let data = Self::type_data();
49            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkGLAreaClass;
50            let f = (*parent_class)
51                .render
52                .expect("No parent class impl for \"render\"");
53            from_glib(f(
54                self.obj().unsafe_cast_ref::<GLArea>().to_glib_none().0,
55                context.to_glib_none().0,
56            ))
57        }
58    }
59
60    fn parent_resize(&self, width: i32, height: i32) {
61        unsafe {
62            let data = Self::type_data();
63            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkGLAreaClass;
64            if let Some(f) = (*parent_class).resize {
65                f(
66                    self.obj().unsafe_cast_ref::<GLArea>().to_glib_none().0,
67                    width,
68                    height,
69                )
70            }
71        }
72    }
73}
74
75impl<T: GLAreaImpl> GLAreaImplExt for T {}
76
77unsafe impl<T: GLAreaImpl> IsSubclassable<T> for GLArea {
78    fn class_init(class: &mut glib::Class<Self>) {
79        Self::parent_class_init::<T>(class);
80
81        let klass = class.as_mut();
82        klass.create_context = Some(gl_area_create_context::<T>);
83        klass.render = Some(gl_area_render::<T>);
84        klass.resize = Some(gl_area_resize::<T>);
85    }
86}
87
88unsafe extern "C" fn gl_area_create_context<T: GLAreaImpl>(
89    ptr: *mut ffi::GtkGLArea,
90) -> *mut gdk::ffi::GdkGLContext {
91    let instance = &*(ptr as *mut T::Instance);
92    let imp = instance.imp();
93
94    imp.create_context().into_glib_ptr()
95}
96
97unsafe extern "C" fn gl_area_render<T: GLAreaImpl>(
98    ptr: *mut ffi::GtkGLArea,
99    context: *mut gdk::ffi::GdkGLContext,
100) -> glib::ffi::gboolean {
101    let instance = &*(ptr as *mut T::Instance);
102    let imp = instance.imp();
103
104    imp.render(&from_glib_borrow(context)).into_glib()
105}
106
107unsafe extern "C" fn gl_area_resize<T: GLAreaImpl>(
108    ptr: *mut ffi::GtkGLArea,
109    width: i32,
110    height: i32,
111) {
112    let instance = &*(ptr as *mut T::Instance);
113    let imp = instance.imp();
114
115    imp.resize(width, height)
116}