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
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! Traits for notifying client code when the state of a KISS-UI widget is updated.

use widget_prelude::*;

use iup_sys::Ihandle;

use std::cell::RefCell;
use std::collections::HashMap;

/// Set this within a callback to tell the framework if it should close or not.
///
/// If `Callback::close.set()` is called within a callback, then when the callback returns,
/// the dialog containing the widget on which the callback was invoked will be closed.
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum CallbackStatus {
    //Ignore,
    /// The default `CallbackStatus`, does nothing when set.
    Default,
    /// If this is set within a callback, then when the callback returns the dialog containing the
    /// widget on which the callback was invoked will be closed.
    Close,
    //Continue,
}

impl CallbackStatus {
    pub fn close(&mut self) {
        *self = CallbackStatus::Close;
    }

    #[doc(hidden)]
    pub fn to_cb_return(self) -> ::libc::c_int {
        use self::CallbackStatus::*;

        match self {
            Close => ::iup_sys::IUP_CLOSE,
            Default => ::iup_sys::IUP_DEFAULT,
            // _ => unimplemented!(),
        }
    }
}

impl From<()> for CallbackStatus {
    fn from(_: ()) -> CallbackStatus {
        CallbackStatus::Default
    }
}

pub trait Callback<Args>: 'static {
    fn on_callback(&mut self, args: Args) -> CallbackStatus; 
}

impl<Args, Out: Into<CallbackStatus>, F: 'static> Callback<Args> for F where F: FnMut(Args) -> Out {
    /// Because of the `impl From<()> for CallbackStatus`, closures that return `()` can be
    /// accepted by this impl.
    fn on_callback(&mut self, args: Args) -> CallbackStatus {
        self(args).into()
    }
}

#[doc(hidden)]
pub type CallbackMap<T> = RefCell<HashMap<*mut Ihandle, Box<Callback<T>>>>;

macro_rules! callback_impl {
    ($cb_attr:expr, $base:expr, $callback:expr, $self_ty:ident) => (
        { 
            thread_local!(
                static CALLBACKS: ::callback::CallbackMap<$self_ty> = 
                    ::std::cell::RefCell::new(::std::collections::HashMap::new())
            );

            extern fn extern_callback(element: *mut ::iup_sys::Ihandle) 
            -> ::libc::c_int {
                use ::callback::CallbackStatus;

                let widget = unsafe { $self_ty::from_ptr(element) };

                CALLBACKS.with(|callbacks| 
                    callbacks.borrow_mut()
                        .get_mut(&widget.ptr())
                        .map(|cb| cb.on_callback(widget))
                ).unwrap_or(CallbackStatus::Default).to_cb_return()
            }

            CALLBACKS.with(|callbacks| 
                callbacks.borrow_mut().insert($base.ptr(), Box::new($callback))
            );
            $base.set_callback($cb_attr, extern_callback);                
        }
    )
}

/// A trait describing a widget that can be clicked, and can notify client code when this occurs.
pub trait OnClick: Widget {
    fn set_onclick<Cb>(self, on_click: Cb) -> Self where Cb: Callback<Self>;
}

macro_rules! impl_onclick {
    ($self_ty:ident) => (
        impl $crate::callback::OnClick for $self_ty {
            fn set_onclick<Cb>(self, on_click: Cb) -> Self where Cb: ::callback::Callback<Self> {
                callback_impl! { $crate::attrs::ACTION, self, on_click, $self_ty }
                self
            }
        }
    )
}

/// A trait describing a widget which has a value that can be changed by the user, and can notify
/// client code when this occurs.
pub trait OnValueChange: Widget {
    fn set_on_value_changed<Cb>(self, on_value_chaged: Cb) -> Self where Cb: Callback<Self>;
}

macro_rules! impl_on_value_change {
    ($self_ty:ident) => (
        impl $crate::callback::OnValueChange for $self_ty {
            fn set_on_value_changed<Cb>(self, on_value_changed: Cb) -> Self where Cb: ::callback::Callback<Self> {
                callback_impl! { $crate::attrs::VALUE_CHANGED_CB, self, on_value_changed, $self_ty }
                self
            }
        }
    )
}

/// A trait describing a widget that can be shown, and can notify client code when this occurs.
pub trait OnShow: Widget {
    fn set_on_show<Cb>(self, on_show: Cb) -> Self where Cb: Callback<Self>;
}

macro_rules! impl_on_show {
    ($self_ty:ident) => (
        impl ::callback::OnShow for $self_ty {
            fn set_on_show<Cb>(self, on_show: Cb) -> Self where Cb: ::callback::Callback<Self> {
                callback_impl! { ::attrs::MAP_CB, self, on_show, $self_ty }
                self
            }
        }
    )
}