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

use crate::Editable;
use glib::object::Cast;
use glib::signal::{connect_raw, SignalHandlerId};
use glib::translate::*;
use glib::IsA;
use libc::{c_char, c_int, c_uchar};
use std::ffi::CStr;
use std::mem::transmute;
use std::slice;
use std::str;

pub trait EditableExtManual: 'static {
    /// Emitted when text is inserted into the widget by the user.
    ///
    /// The default handler for this signal will normally be responsible
    /// for inserting the text, so by connecting to this signal and then
    /// stopping the signal with `g_signal_stop_emission()`, it is possible
    /// to modify the inserted text, or prevent it from being inserted entirely.
    /// ## `text`
    /// the new text to insert
    /// ## `length`
    /// the length of the new text, in bytes,
    ///  or -1 if new_text is nul-terminated
    /// ## `position`
    /// the position, in characters,
    ///  at which to insert the new text. this is an in-out
    ///  parameter. After the signal emission is finished, it
    ///  should point after the newly inserted text.
    fn connect_insert_text<F>(&self, f: F) -> SignalHandlerId
    where
        F: Fn(&Self, &str, &mut i32) + 'static;
}

impl<T: IsA<Editable>> EditableExtManual for T {
    fn connect_insert_text<F>(&self, f: F) -> SignalHandlerId
    where
        F: Fn(&Self, &str, &mut i32) + 'static,
    {
        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, &str, &mut i32) + 'static>(
    this: *mut ffi::GtkEditable,
    new_text: *mut c_char,
    new_text_length: c_int,
    position: *mut c_int,
    f: &F,
) where
    T: IsA<Editable>,
{
    let buf = if new_text_length != -1 {
        slice::from_raw_parts(new_text as *mut c_uchar, new_text_length as usize)
    } else {
        CStr::from_ptr(new_text).to_bytes()
    };
    let string = str::from_utf8(buf).unwrap();
    f(
        Editable::from_glib_borrow(this).unsafe_cast_ref(),
        string,
        &mut *position,
    );
}