gtk4/
spin_button.rs

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
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{boxed::Box as Box_, mem::transmute};

use glib::{
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use libc::{c_double, c_int};

use crate::{ffi, prelude::*, SpinButton};

impl SpinButton {
    /// Emitted to convert the users input into a double value.
    ///
    /// The signal handler is expected to use [`EditableExt::text()`][crate::prelude::EditableExt::text()]
    /// to retrieve the text of the spinbutton and set @new_value to the
    /// new value.
    ///
    /// The default conversion uses g_strtod().
    ///
    /// # Returns
    ///
    /// [`true`] for a successful conversion, [`false`] if the input
    ///   was not handled, and `GTK_INPUT_ERROR` if the conversion failed.
    ///
    /// ## `new_value`
    /// return location for the new value
    pub fn connect_input<F>(&self, f: F) -> SignalHandlerId
    where
        F: Fn(&Self) -> Option<Result<f64, ()>> + 'static,
    {
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"input\0".as_ptr() as *mut _,
                Some(transmute::<usize, unsafe extern "C" fn()>(
                    input_trampoline::<F> as usize,
                )),
                Box_::into_raw(f),
            )
        }
    }
}

unsafe extern "C" fn input_trampoline<F: Fn(&SpinButton) -> Option<Result<f64, ()>> + 'static>(
    this: *mut ffi::GtkSpinButton,
    new_value: *mut c_double,
    f: &F,
) -> c_int {
    match f(SpinButton::from_glib_borrow(this).unsafe_cast_ref()) {
        Some(Ok(v)) => {
            *new_value = v;
            glib::ffi::GTRUE
        }
        Some(Err(_)) => ffi::GTK_INPUT_ERROR,
        None => glib::ffi::GFALSE,
    }
}