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

// rustdoc-stripper-ignore-next
//! Traits intended for subclassing [`Buffer`](crate::Buffer).

use crate::{prelude::*, BracketMatchType, Buffer};
use glib::translate::*;
use gtk::subclass::prelude::*;

pub trait BufferImpl: TextBufferImpl {
    fn bracket_matched(&self, iter: &mut gtk::TextIter, state: BracketMatchType) {
        self.parent_bracket_matched(iter, state)
    }
}

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

pub trait BufferImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_bracket_matched(&self, iter: &mut gtk::TextIter, state: BracketMatchType) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkSourceBufferClass;
            if let Some(f) = (*parent_class).bracket_matched {
                f(
                    self.obj().unsafe_cast_ref::<Buffer>().to_glib_none().0,
                    iter.to_glib_none_mut().0,
                    state.into_glib(),
                )
            }
        }
    }
}

impl<T: BufferImpl> BufferImplExt for T {}

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

        let klass = class.as_mut();
        klass.bracket_matched = Some(buffer_bracket_matched::<T>);
    }
}

unsafe extern "C" fn buffer_bracket_matched<T: BufferImpl>(
    ptr: *mut ffi::GtkSourceBuffer,
    iterptr: *mut gtk::ffi::GtkTextIter,
    state: i32,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();
    let mut iter = from_glib_full(iterptr);
    imp.bracket_matched(&mut iter, from_glib(state));
    *iterptr = *iter.to_glib_full();
}