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

// rustdoc-stripper-ignore-next
//! Traits intended for implementing the [`Scrollable`](crate::Scrollable) interface.

use crate::subclass::prelude::*;
use crate::{Border, Scrollable};
use glib::translate::*;
use glib::Cast;

pub trait ScrollableImpl: ObjectImpl {
    #[doc(alias = "get_border")]
    fn border(&self) -> Option<Border> {
        self.parent_border()
    }
}

pub trait ScrollableImplExt: ObjectSubclass {
    fn parent_border(&self) -> Option<Border>;
}

impl<T: ScrollableImpl> ScrollableImplExt for T {
    fn parent_border(&self) -> Option<Border> {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Scrollable>()
                as *const ffi::GtkScrollableInterface;

            if let Some(func) = (*parent_iface).get_border {
                let border = std::ptr::null_mut();
                if from_glib(func(
                    self.obj().unsafe_cast_ref::<Scrollable>().to_glib_none().0,
                    border,
                )) {
                    return Some(from_glib_none(border));
                }
            }
            None
        }
    }
}

unsafe impl<T: ScrollableImpl> IsImplementable<T> for Scrollable {
    fn interface_init(iface: &mut glib::Interface<Self>) {
        let iface = iface.as_mut();

        assert!(
            crate::rt::is_initialized(),
            "GTK has to be initialized first"
        );

        iface.get_border = Some(scrollable_get_border::<T>);
    }
}

unsafe extern "C" fn scrollable_get_border<T: ScrollableImpl>(
    scrollable: *mut ffi::GtkScrollable,
    borderptr: *mut ffi::GtkBorder,
) -> glib::ffi::gboolean {
    let instance = &*(scrollable as *mut T::Instance);
    let imp = instance.imp();

    if let Some(border) = imp.border() {
        *borderptr = *border.to_glib_full();
        true.into_glib()
    } else {
        *borderptr = ffi::GtkBorder {
            top: 0,
            right: 0,
            left: 0,
            bottom: 0,
        };
        false.into_glib()
    }
}