gtk4/
editable.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use 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
17// rustdoc-stripper-ignore-next
18/// Trait containing manually implemented methods of
19/// [`Editable`](crate::Editable).
20pub trait EditableExtManual: sealed::Sealed + IsA<Editable> + 'static {
21    /// Emitted when text is inserted into the widget by the user.
22    ///
23    /// The default handler for this signal will normally be responsible
24    /// for inserting the text, so by connecting to this signal and then
25    /// stopping the signal with g_signal_stop_emission(), it is possible
26    /// to modify the inserted text, or prevent it from being inserted entirely.
27    /// ## `text`
28    /// the new text to insert
29    /// ## `length`
30    /// the length of the new text, in bytes,
31    ///     or -1 if new_text is nul-terminated
32    /// ## `position`
33    /// the position, in characters,
34    ///     at which to insert the new text. this is an in-out
35    ///     parameter.  After the signal emission is finished, it
36    ///     should point after the newly inserted text.
37    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}