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
use crate::{prelude::*, Frame, Widget};
use glib::translate::*;
use gtk::subclass::prelude::*;

pub trait PanelFrameImpl: WidgetImpl {
    fn page_closed(&self, widget: &Widget) {
        PanelFrameImplExt::parent_page_closed(self, widget)
    }
    fn adopt_widget(&self, widget: &Widget) -> bool {
        PanelFrameImplExt::parent_adopt_widget(self, widget)
    }
}

pub trait PanelFrameImplExt: ObjectSubclass {
    fn parent_page_closed(&self, widget: &Widget);
    fn parent_adopt_widget(&self, widget: &Widget) -> bool;
}

impl<T: PanelFrameImpl> PanelFrameImplExt for T {
    fn parent_page_closed(&self, widget: &Widget) {
        unsafe {
            let data = T::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::PanelFrameClass;
            if let Some(f) = (*parent_class).page_closed {
                f(
                    self.obj().unsafe_cast_ref::<Frame>().to_glib_none().0,
                    widget.to_glib_none().0,
                );
            }
        }
    }
    fn parent_adopt_widget(&self, widget: &Widget) -> bool {
        unsafe {
            let data = T::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::PanelFrameClass;
            if let Some(f) = (*parent_class).adopt_widget {
                return f(
                    self.obj().unsafe_cast_ref::<Frame>().to_glib_none().0,
                    widget.to_glib_none().0,
                ) != 0;
            }
        }
        false
    }
}

unsafe impl<T: PanelFrameImpl> IsSubclassable<T> for Frame {
    fn class_init(class: &mut glib::Class<Self>) {
        Self::parent_class_init::<T>(class);
        let klass = class.as_mut();
        klass.page_closed = Some(panel_frame_page_closed::<T>);
        klass.adopt_widget = Some(panel_frame_adopt_widget::<T>);
    }
}

unsafe extern "C" fn panel_frame_page_closed<T: PanelFrameImpl>(
    ptr: *mut ffi::PanelFrame,
    widget: *mut ffi::PanelWidget,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();
    let widget: Borrowed<Widget> = from_glib_borrow(widget);

    PanelFrameImpl::page_closed(imp, &widget);
}

unsafe extern "C" fn panel_frame_adopt_widget<T: PanelFrameImpl>(
    ptr: *mut ffi::PanelFrame,
    widget: *mut ffi::PanelWidget,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();
    let widget: Borrowed<Widget> = from_glib_borrow(widget);

    PanelFrameImpl::adopt_widget(imp, &widget) as glib::ffi::gboolean
}