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
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 {
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 == 0 {
&[]
} else 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,
);
}