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
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::translate::*;

use glib::subclass::prelude::*;

use glib::Cast;

use super::bin::BinImpl;
use crate::DirectionType;
use crate::ScrollType;
use crate::ScrolledWindow;

pub trait ScrolledWindowImpl: ScrolledWindowImplExt + BinImpl {
    fn move_focus_out(&self, direction_type: DirectionType) {
        self.parent_move_focus_out(direction_type)
    }

    fn scroll_child(&self, scroll: ScrollType, horizontal: bool) -> bool {
        self.parent_scroll_child(scroll, horizontal)
    }
}

pub trait ScrolledWindowImplExt: ObjectSubclass {
    fn parent_move_focus_out(&self, direction_type: DirectionType);
    fn parent_scroll_child(&self, scroll: ScrollType, borizontal: bool) -> bool;
}

impl<T: ScrolledWindowImpl> ScrolledWindowImplExt for T {
    fn parent_move_focus_out(&self, direction_type: DirectionType) {
        unsafe {
            let data = T::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkScrolledWindowClass;
            if let Some(f) = (*parent_class).move_focus_out {
                f(
                    self.instance()
                        .unsafe_cast_ref::<ScrolledWindow>()
                        .to_glib_none()
                        .0,
                    direction_type.into_glib(),
                )
            }
        }
    }

    fn parent_scroll_child(&self, scroll: ScrollType, horizontal: bool) -> bool {
        unsafe {
            let data = T::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkScrolledWindowClass;
            if let Some(f) = (*parent_class).scroll_child {
                from_glib(f(
                    self.instance()
                        .unsafe_cast_ref::<ScrolledWindow>()
                        .to_glib_none()
                        .0,
                    scroll.into_glib(),
                    horizontal.into_glib(),
                ))
            } else {
                false
            }
        }
    }
}

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

        let klass = class.as_mut();
        klass.move_focus_out = Some(window_move_focus_out::<T>);
        klass.scroll_child = Some(window_scroll_child::<T>);
    }
}

unsafe extern "C" fn window_move_focus_out<T: ScrolledWindowImpl>(
    ptr: *mut ffi::GtkScrolledWindow,
    directiontypeptr: ffi::GtkDirectionType,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();
    let direction_type: DirectionType = from_glib(directiontypeptr);

    imp.move_focus_out(direction_type)
}

unsafe extern "C" fn window_scroll_child<T: ScrolledWindowImpl>(
    ptr: *mut ffi::GtkScrolledWindow,
    scrollptr: ffi::GtkScrollType,
    horizontalptr: glib::ffi::gboolean,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();
    let scroll: ScrollType = from_glib(scrollptr);
    let horizontal: bool = from_glib(horizontalptr);

    imp.scroll_child(scroll, horizontal).into_glib()
}