Skip to main content

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::{SignalHandlerId, connect_raw},
7    translate::*,
8};
9use libc::{c_char, c_int, c_uchar};
10
11use crate::{Editable, prelude::*};
12
13// rustdoc-stripper-ignore-next
14/// Trait containing manually implemented methods of
15/// [`Editable`](crate::Editable).
16pub trait EditableExtManual: IsA<Editable> + 'static {
17    /// Emitted when text is inserted into the widget by the user.
18    ///
19    /// The default handler for this signal will normally be responsible
20    /// for inserting the text, so by connecting to this signal and then
21    /// stopping the signal with g_signal_stop_emission(), it is possible
22    /// to modify the inserted text, or prevent it from being inserted entirely.
23    /// ## `text`
24    /// the new text to insert
25    /// ## `length`
26    /// the length of the new text, in bytes,
27    ///     or -1 if new_text is nul-terminated
28    /// ## `position`
29    /// the position, in characters,
30    ///     at which to insert the new text. this is an in-out
31    ///     parameter.  After the signal emission is finished, it
32    ///     should point after the newly inserted text.
33    fn connect_insert_text<F>(&self, f: F) -> SignalHandlerId
34    where
35        F: Fn(&Self, &str, &mut i32) + 'static,
36    {
37        unsafe {
38            let f: Box<F> = Box::new(f);
39            connect_raw(
40                self.to_glib_none().0 as *mut _,
41                c"insert-text".as_ptr() as *mut _,
42                Some(transmute::<*const (), unsafe extern "C" fn()>(
43                    insert_text_trampoline::<Self, F> as *const (),
44                )),
45                Box::into_raw(f),
46            )
47        }
48    }
49}
50
51impl<O: IsA<Editable>> EditableExtManual for O {}
52
53unsafe extern "C" fn insert_text_trampoline<T, F: Fn(&T, &str, &mut i32) + 'static>(
54    this: *mut crate::ffi::GtkEditable,
55    new_text: *mut c_char,
56    new_text_length: c_int,
57    position: *mut c_int,
58    f: &F,
59) where
60    T: IsA<Editable>,
61{
62    unsafe {
63        let buf = if new_text_length == 0 {
64            &[]
65        } else if new_text_length != -1 {
66            slice::from_raw_parts(new_text as *mut c_uchar, new_text_length as usize)
67        } else {
68            CStr::from_ptr(new_text).to_bytes()
69        };
70        let string = str::from_utf8(buf).unwrap();
71        f(
72            Editable::from_glib_borrow(this).unsafe_cast_ref(),
73            string,
74            &mut *position,
75        );
76    }
77}