1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Take a look at the license at the top of the repository in the LICENSE file.

// rustdoc-stripper-ignore-next
//! Traits intended for subclassing [`GLArea`](crate::GLArea).

use crate::{prelude::*, subclass::prelude::*, GLArea};
use gdk::GLContext;
use glib::translate::*;

#[allow(clippy::upper_case_acronyms)]
pub trait GLAreaImpl: GLAreaImplExt + WidgetImpl {
    fn create_context(&self) -> Option<GLContext> {
        self.parent_create_context()
    }

    fn render(&self, context: &GLContext) -> bool {
        self.parent_render(context)
    }

    fn resize(&self, width: i32, height: i32) {
        self.parent_resize(width, height)
    }
}

#[allow(clippy::upper_case_acronyms)]
pub trait GLAreaImplExt: ObjectSubclass {
    fn parent_create_context(&self) -> Option<GLContext>;
    fn parent_render(&self, context: &GLContext) -> bool;
    fn parent_resize(&self, width: i32, height: i32);
}

impl<T: GLAreaImpl> GLAreaImplExt for T {
    fn parent_create_context(&self) -> Option<GLContext> {
        unsafe {
            let data = T::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkGLAreaClass;
            if let Some(f) = (*parent_class).create_context {
                return Some(from_glib_none(f(self
                    .obj()
                    .unsafe_cast_ref::<GLArea>()
                    .to_glib_none()
                    .0)));
            };
            None
        }
    }

    fn parent_render(&self, context: &GLContext) -> bool {
        unsafe {
            let data = T::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkGLAreaClass;
            let f = (*parent_class)
                .render
                .expect("No parent class impl for \"render\"");
            from_glib(f(
                self.obj().unsafe_cast_ref::<GLArea>().to_glib_none().0,
                context.to_glib_none().0,
            ))
        }
    }

    fn parent_resize(&self, width: i32, height: i32) {
        unsafe {
            let data = T::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkGLAreaClass;
            if let Some(f) = (*parent_class).resize {
                f(
                    self.obj().unsafe_cast_ref::<GLArea>().to_glib_none().0,
                    width,
                    height,
                )
            }
        }
    }
}

unsafe impl<T: GLAreaImpl> IsSubclassable<T> for GLArea {
    fn class_init(class: &mut glib::Class<Self>) {
        Self::parent_class_init::<T>(class);

        let klass = class.as_mut();
        klass.create_context = Some(gl_area_create_context::<T>);
        klass.render = Some(gl_area_render::<T>);
        klass.resize = Some(gl_area_resize::<T>);
    }
}

unsafe extern "C" fn gl_area_create_context<T: GLAreaImpl>(
    ptr: *mut ffi::GtkGLArea,
) -> *mut gdk::ffi::GdkGLContext {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.create_context().into_glib_ptr()
}

unsafe extern "C" fn gl_area_render<T: GLAreaImpl>(
    ptr: *mut ffi::GtkGLArea,
    context: *mut gdk::ffi::GdkGLContext,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.render(&from_glib_borrow(context)).into_glib()
}

unsafe extern "C" fn gl_area_resize<T: GLAreaImpl>(
    ptr: *mut ffi::GtkGLArea,
    width: i32,
    height: i32,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.resize(width, height)
}