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 from_glib_none(f(self.obj().unsafe_cast_ref::<GLArea>().to_glib_none().0));
37            };
38            None
39        }
40    }
41
42    fn parent_render(&self, context: &GLContext) -> glib::Propagation {
43        unsafe {
44            let data = Self::type_data();
45            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkGLAreaClass;
46            let f = (*parent_class)
47                .render
48                .expect("No parent class impl for \"render\"");
49            from_glib(f(
50                self.obj().unsafe_cast_ref::<GLArea>().to_glib_none().0,
51                context.to_glib_none().0,
52            ))
53        }
54    }
55
56    fn parent_resize(&self, width: i32, height: i32) {
57        unsafe {
58            let data = Self::type_data();
59            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkGLAreaClass;
60            if let Some(f) = (*parent_class).resize {
61                f(
62                    self.obj().unsafe_cast_ref::<GLArea>().to_glib_none().0,
63                    width,
64                    height,
65                )
66            }
67        }
68    }
69}
70
71impl<T: GLAreaImpl> GLAreaImplExt for T {}
72
73unsafe impl<T: GLAreaImpl> IsSubclassable<T> for GLArea {
74    fn class_init(class: &mut glib::Class<Self>) {
75        Self::parent_class_init::<T>(class);
76
77        let klass = class.as_mut();
78        klass.create_context = Some(gl_area_create_context::<T>);
79        klass.render = Some(gl_area_render::<T>);
80        klass.resize = Some(gl_area_resize::<T>);
81    }
82}
83
84unsafe extern "C" fn gl_area_create_context<T: GLAreaImpl>(
85    ptr: *mut ffi::GtkGLArea,
86) -> *mut gdk::ffi::GdkGLContext {
87    let instance = &*(ptr as *mut T::Instance);
88    let imp = instance.imp();
89
90    imp.create_context().into_glib_ptr()
91}
92
93unsafe extern "C" fn gl_area_render<T: GLAreaImpl>(
94    ptr: *mut ffi::GtkGLArea,
95    context: *mut gdk::ffi::GdkGLContext,
96) -> glib::ffi::gboolean {
97    let instance = &*(ptr as *mut T::Instance);
98    let imp = instance.imp();
99
100    imp.render(&from_glib_borrow(context)).into_glib()
101}
102
103unsafe extern "C" fn gl_area_resize<T: GLAreaImpl>(
104    ptr: *mut ffi::GtkGLArea,
105    width: i32,
106    height: i32,
107) {
108    let instance = &*(ptr as *mut T::Instance);
109    let imp = instance.imp();
110
111    imp.resize(width, height)
112}