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 implementing the [`Indenter`](crate::Indenter) interface.

use crate::{prelude::*, Indenter, View};
use glib::subclass::prelude::*;
use glib::translate::*;

pub trait IndenterImpl: ObjectImpl {
    fn is_trigger(
        &self,
        view: &View,
        location: &gtk::TextIter,
        state: gdk::ModifierType,
        keyval: gdk::Key,
    ) -> bool {
        self.parent_is_trigger(view, location, state, keyval)
    }

    fn indent(&self, view: &View, iter: &mut gtk::TextIter) {
        self.parent_indent(view, iter)
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::IndenterImplExt> Sealed for T {}
}

pub trait IndenterImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_is_trigger(
        &self,
        view: &View,
        location: &gtk::TextIter,
        state: gdk::ModifierType,
        keyval: gdk::Key,
    ) -> bool {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Indenter>()
                as *const ffi::GtkSourceIndenterInterface;

            let func = (*parent_iface)
                .is_trigger
                .expect("no parent \"is_trigger\" implementation");

            from_glib(func(
                self.obj().unsafe_cast_ref::<Indenter>().to_glib_none().0,
                view.to_glib_none().0,
                location.to_glib_none().0,
                state.into_glib(),
                keyval.into_glib(),
            ))
        }
    }

    fn parent_indent(&self, view: &View, iter: &mut gtk::TextIter) {
        unsafe {
            let type_data = Self::type_data();
            let parent_iface = type_data.as_ref().parent_interface::<Indenter>()
                as *const ffi::GtkSourceIndenterInterface;

            let func = (*parent_iface)
                .indent
                .expect("no parent \"indent\" implementation");

            func(
                self.obj().unsafe_cast_ref::<Indenter>().to_glib_none().0,
                view.to_glib_none().0,
                iter.to_glib_none_mut().0,
            )
        }
    }
}
impl<T: IndenterImpl> IndenterImplExt for T {}

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

        iface.is_trigger = Some(indenter_is_trigger::<T>);
        iface.indent = Some(indenter_indent::<T>);
    }
}

unsafe extern "C" fn indenter_is_trigger<T: IndenterImpl>(
    indenter: *mut ffi::GtkSourceIndenter,
    view: *mut ffi::GtkSourceView,
    iter: *const gtk::ffi::GtkTextIter,
    state: gdk::ffi::GdkModifierType,
    keyval: u32,
) -> glib::ffi::gboolean {
    let instance = &*(indenter as *mut T::Instance);
    let imp = instance.imp();

    imp.is_trigger(
        &from_glib_borrow(view),
        &from_glib_borrow(iter),
        from_glib(state),
        from_glib(keyval),
    )
    .into_glib()
}

unsafe extern "C" fn indenter_indent<T: IndenterImpl>(
    indenter: *mut ffi::GtkSourceIndenter,
    view: *mut ffi::GtkSourceView,
    iterptr: *mut gtk::ffi::GtkTextIter,
) {
    let instance = &*(indenter as *mut T::Instance);
    let imp = instance.imp();
    let mut iter = from_glib_full(iterptr);
    imp.indent(&from_glib_borrow(view), &mut iter);
    *iterptr = *iter.to_glib_full();
}