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

use crate::SpinRow;
use glib::{
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use gtk::{glib, prelude::*};
use libc::{c_double, c_int};
use std::{boxed::Box as Box_, mem::transmute};

impl SpinRow {
    /// Emitted to convert the user's input into a double value.
    ///
    /// The signal handler is expected to use [`EditableExtManual::text()`][crate::gtk::prelude::EditableExtManual::text()] to
    /// retrieve the text of the spinbutton and set new_value to the new value.
    ///
    /// The default conversion uses `strtod()`.
    ///
    /// See [`input`][struct@crate::Gtk::SpinButton#input].
    ///
    /// # 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(input_trampoline::<F> as usize)),
                Box_::into_raw(f),
            )
        }
    }
}

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