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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use crate::FrameHeader;
use crate::Widget;
use glib::translate::*;
use glib::Cast;
use gtk::subclass::prelude::*;

pub trait FrameHeaderImpl: WidgetImpl {
    fn page_changed(&self, header: &Self::Type, widget: Option<&Widget>) {
        FrameHeaderImplExt::parent_page_changed(self, header, widget);
    }
    fn can_drop(&self, header: &Self::Type, widget: &Widget) -> bool {
        FrameHeaderImplExt::parent_can_drop(self, header, widget)
    }
    fn add_prefix(&self, header: &Self::Type, priority: i32, widget: &Widget) {
        FrameHeaderImplExt::parent_add_prefix(self, header, priority, widget);
    }
    fn add_suffix(&self, header: &Self::Type, priority: i32, widget: &Widget) {
        FrameHeaderImplExt::parent_add_suffix(self, header, priority, widget);
    }
}

pub trait FrameHeaderImplExt: ObjectSubclass {
    fn parent_page_changed(&self, header: &Self::Type, widget: Option<&Widget>);
    fn parent_can_drop(&self, header: &Self::Type, widget: &Widget) -> bool;
    fn parent_add_prefix(&self, header: &Self::Type, priority: i32, widget: &Widget);
    fn parent_add_suffix(&self, header: &Self::Type, priority: i32, widget: &Widget);
}

impl<T: FrameHeaderImpl> FrameHeaderImplExt for T {
    fn parent_page_changed(&self, header: &Self::Type, widget: Option<&Widget>) {
        unsafe {
            let data = T::type_data();
            let parent_iface = data.as_ref().parent_interface::<FrameHeader>()
                as *const ffi::PanelFrameHeaderInterface;
            if let Some(f) = (*parent_iface).page_changed {
                f(
                    header.unsafe_cast_ref::<FrameHeader>().to_glib_none().0,
                    widget
                        .map(|w| w.unsafe_cast_ref::<Widget>())
                        .to_glib_none()
                        .0,
                );
            }
        }
    }
    fn parent_can_drop(&self, header: &Self::Type, widget: &Widget) -> bool {
        unsafe {
            let data = T::type_data();
            let parent_iface = data.as_ref().parent_interface::<FrameHeader>()
                as *const ffi::PanelFrameHeaderInterface;
            if let Some(f) = (*parent_iface).can_drop {
                return f(
                    header.unsafe_cast_ref::<FrameHeader>().to_glib_none().0,
                    widget.unsafe_cast_ref::<Widget>().to_glib_none().0,
                ) != 0;
            }
            false
        }
    }
    fn parent_add_prefix(&self, header: &Self::Type, priority: i32, widget: &Widget) {
        unsafe {
            let data = T::type_data();
            let parent_iface = data.as_ref().parent_interface::<FrameHeader>()
                as *const ffi::PanelFrameHeaderInterface;
            if let Some(f) = (*parent_iface).add_prefix {
                f(
                    header.unsafe_cast_ref::<FrameHeader>().to_glib_none().0,
                    priority,
                    widget.unsafe_cast_ref::<gtk::Widget>().to_glib_none().0,
                );
            }
        }
    }
    fn parent_add_suffix(&self, header: &Self::Type, priority: i32, widget: &Widget) {
        unsafe {
            let data = T::type_data();
            let parent_iface = data.as_ref().parent_interface::<FrameHeader>()
                as *const ffi::PanelFrameHeaderInterface;
            if let Some(f) = (*parent_iface).add_suffix {
                f(
                    header.unsafe_cast_ref::<FrameHeader>().to_glib_none().0,
                    priority,
                    widget.unsafe_cast_ref::<gtk::Widget>().to_glib_none().0,
                );
            }
        }
    }
}

unsafe impl<T: FrameHeaderImpl> IsImplementable<T> for FrameHeader {
    fn interface_init(iface: &mut glib::Interface<Self>) {
        let iface = iface.as_mut();
        iface.page_changed = Some(frame_header_page_changed::<T>);
        iface.can_drop = Some(frame_header_can_drop::<T>);
        iface.add_prefix = Some(frame_header_add_prefix::<T>);
        iface.add_suffix = Some(frame_header_add_suffix::<T>);
    }

    fn instance_init(_instance: &mut glib::subclass::InitializingObject<T>) {}
}

unsafe extern "C" fn frame_header_page_changed<T: FrameHeaderImpl>(
    ptr: *mut ffi::PanelFrameHeader,
    widget: *mut ffi::PanelWidget,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();
    let wrap: Borrowed<FrameHeader> = from_glib_borrow(ptr);
    let widget: Option<Borrowed<Widget>> = if widget.is_null() {
        None
    } else {
        Some(from_glib_borrow(widget))
    };

    FrameHeaderImpl::page_changed(
        imp,
        wrap.unsafe_cast_ref(),
        widget.as_ref().map(|w| w.unsafe_cast_ref()),
    );
}

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

    FrameHeaderImpl::can_drop(imp, wrap.unsafe_cast_ref(), widget.unsafe_cast_ref())
        as glib::ffi::gboolean
}

unsafe extern "C" fn frame_header_add_prefix<T: FrameHeaderImpl>(
    ptr: *mut ffi::PanelFrameHeader,
    priority: std::os::raw::c_int,
    widget: *mut gtk::ffi::GtkWidget,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();
    let wrap: Borrowed<FrameHeader> = from_glib_borrow(ptr);
    let widget: Borrowed<gtk::Widget> = from_glib_borrow(widget);

    FrameHeaderImpl::add_prefix(
        imp,
        wrap.unsafe_cast_ref(),
        priority,
        widget.unsafe_cast_ref(),
    );
}

unsafe extern "C" fn frame_header_add_suffix<T: FrameHeaderImpl>(
    ptr: *mut ffi::PanelFrameHeader,
    priority: std::os::raw::c_int,
    widget: *mut gtk::ffi::GtkWidget,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();
    let wrap: Borrowed<FrameHeader> = from_glib_borrow(ptr);
    let widget: Borrowed<gtk::Widget> = from_glib_borrow(widget);

    FrameHeaderImpl::add_suffix(
        imp,
        wrap.unsafe_cast_ref(),
        priority,
        widget.unsafe_cast_ref(),
    );
}