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

use crate::{TextBuffer, TextIter};
use glib::object::{Cast, IsA};
use glib::signal::{connect_raw, SignalHandlerId};
use glib::translate::*;
use libc::{c_char, c_int};
use std::boxed::Box as Box_;
use std::mem::transmute;
use std::{slice, str};

pub trait TextBufferExtManual: 'static {
    /// Emitted to insert text in a [`TextBuffer`][crate::TextBuffer].
    ///
    /// Insertion actually occurs in the default handler.
    ///
    /// Note that if your handler runs before the default handler
    /// it must not invalidate the `location` iter (or has to
    /// revalidate it). The default signal handler revalidates
    /// it to point to the end of the inserted text.
    ///
    /// See also: [``TextBufferExt::insert()``][crate::prelude::`TextBufferExt::insert()`],
    /// [``TextBufferExt::insert_range()``][crate::prelude::`TextBufferExt::insert_range()`].
    /// ## `location`
    /// position to insert `text` in `textbuffer`
    /// ## `text`
    /// the UTF-8 text to be inserted
    /// ## `len`
    /// length of the inserted text in bytes
    fn connect_insert_text<F: Fn(&Self, &mut TextIter, &str) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;
}

impl<O: IsA<TextBuffer>> TextBufferExtManual for O {
    fn connect_insert_text<F: Fn(&Self, &mut TextIter, &str) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.to_glib_none().0 as *mut _,
                b"insert-text\0".as_ptr() as *mut _,
                Some(transmute(insert_text_trampoline::<Self, F> as usize)),
                Box_::into_raw(f),
            )
        }
    }
}

unsafe extern "C" fn insert_text_trampoline<T, F: Fn(&T, &mut TextIter, &str) + 'static>(
    this: *mut ffi::GtkTextBuffer,
    location: *mut ffi::GtkTextIter,
    text: *mut c_char,
    len: c_int,
    f: glib::ffi::gpointer,
) where
    T: IsA<TextBuffer>,
{
    let mut location_copy = from_glib_none(location);
    let f: &F = &*(f as *const F);
    f(
        TextBuffer::from_glib_borrow(this).unsafe_cast_ref(),
        &mut location_copy,
        str::from_utf8(slice::from_raw_parts(text as *const u8, len as usize)).unwrap(),
    )
}