1use std::{boxed::Box as Box_, mem::transmute};
4
5use glib::{
6 signal::{SignalHandlerId, connect_raw},
7 translate::*,
8};
9use libc::{c_double, c_int};
10
11use crate::{SpinButton, ffi, prelude::*};
12
13impl SpinButton {
14 pub fn connect_input<F>(&self, f: F) -> SignalHandlerId
30 where
31 F: Fn(&Self) -> Option<Result<f64, ()>> + 'static,
32 {
33 unsafe {
34 let f: Box_<F> = Box_::new(f);
35 connect_raw(
36 self.as_ptr() as *mut _,
37 c"input".as_ptr() as *mut _,
38 Some(transmute::<*const (), unsafe extern "C" fn()>(
39 input_trampoline::<F> as *const (),
40 )),
41 Box_::into_raw(f),
42 )
43 }
44 }
45}
46
47unsafe extern "C" fn input_trampoline<F: Fn(&SpinButton) -> Option<Result<f64, ()>> + 'static>(
48 this: *mut ffi::GtkSpinButton,
49 new_value: *mut c_double,
50 f: &F,
51) -> c_int {
52 unsafe {
53 match f(SpinButton::from_glib_borrow(this).unsafe_cast_ref()) {
54 Some(Ok(v)) => {
55 *new_value = v;
56 glib::ffi::GTRUE
57 }
58 Some(Err(_)) => ffi::GTK_INPUT_ERROR,
59 None => glib::ffi::GFALSE,
60 }
61 }
62}