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