1use std::{ffi::CStr, mem::transmute, slice, str};
4
5use glib::{
6 signal::{connect_raw, SignalHandlerId},
7 translate::*,
8};
9use libc::{c_char, c_int, c_uchar};
10
11use crate::{prelude::*, Editable};
12mod sealed {
13 pub trait Sealed {}
14 impl<T: super::IsA<super::Editable>> Sealed for T {}
15}
16
17pub trait EditableExtManual: sealed::Sealed + IsA<Editable> + 'static {
21 fn connect_insert_text<F>(&self, f: F) -> SignalHandlerId
38 where
39 F: Fn(&Self, &str, &mut i32) + 'static,
40 {
41 unsafe {
42 let f: Box<F> = Box::new(f);
43 connect_raw(
44 self.to_glib_none().0 as *mut _,
45 b"insert-text\0".as_ptr() as *mut _,
46 Some(transmute::<usize, unsafe extern "C" fn()>(
47 insert_text_trampoline::<Self, F> as usize,
48 )),
49 Box::into_raw(f),
50 )
51 }
52 }
53}
54
55impl<O: IsA<Editable>> EditableExtManual for O {}
56
57unsafe extern "C" fn insert_text_trampoline<T, F: Fn(&T, &str, &mut i32) + 'static>(
58 this: *mut crate::ffi::GtkEditable,
59 new_text: *mut c_char,
60 new_text_length: c_int,
61 position: *mut c_int,
62 f: &F,
63) where
64 T: IsA<Editable>,
65{
66 let buf = if new_text_length == 0 {
67 &[]
68 } else if new_text_length != -1 {
69 slice::from_raw_parts(new_text as *mut c_uchar, new_text_length as usize)
70 } else {
71 CStr::from_ptr(new_text).to_bytes()
72 };
73 let string = str::from_utf8(buf).unwrap();
74 f(
75 Editable::from_glib_borrow(this).unsafe_cast_ref(),
76 string,
77 &mut *position,
78 );
79}