[go: up one dir, main page]

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};
12
13// rustdoc-stripper-ignore-next
14/// Trait containing manually implemented methods of
15/// [`Editable`](crate::Editable).
16pub trait EditableExtManual: IsA<Editable> + 'static {
17    fn connect_insert_text<F>(&self, f: F) -> SignalHandlerId
18    where
19        F: Fn(&Self, &str, &mut i32) + 'static,
20    {
21        unsafe {
22            let f: Box<F> = Box::new(f);
23            connect_raw(
24                self.to_glib_none().0 as *mut _,
25                c"insert-text".as_ptr() as *mut _,
26                Some(transmute::<usize, unsafe extern "C" fn()>(
27                    insert_text_trampoline::<Self, F> as usize,
28                )),
29                Box::into_raw(f),
30            )
31        }
32    }
33}
34
35impl<O: IsA<Editable>> EditableExtManual for O {}
36
37unsafe extern "C" fn insert_text_trampoline<T, F: Fn(&T, &str, &mut i32) + 'static>(
38    this: *mut crate::ffi::GtkEditable,
39    new_text: *mut c_char,
40    new_text_length: c_int,
41    position: *mut c_int,
42    f: &F,
43) where
44    T: IsA<Editable>,
45{
46    let buf = if new_text_length == 0 {
47        &[]
48    } else if new_text_length != -1 {
49        slice::from_raw_parts(new_text as *mut c_uchar, new_text_length as usize)
50    } else {
51        CStr::from_ptr(new_text).to_bytes()
52    };
53    let string = str::from_utf8(buf).unwrap();
54    f(
55        Editable::from_glib_borrow(this).unsafe_cast_ref(),
56        string,
57        &mut *position,
58    );
59}