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
//! Button

use super::{
    WgtArc,
    WgtProxy,
};
use crate::cql;
use std::sync::{
    Arc,
    Mutex,
};

type CbClicked = Arc<Mutex<FnMut(&mut Btn, bool)>>;

#[cql::derive::Wgt]
pub struct Btn {
    cb_clicked: Option<CbClicked>,
}

impl Btn {
    pub fn make(s: &str) -> Self {
        Self {
            proxy:      WgtProxy::from_h(unsafe {
                cql::btn_make(cql::to_cstring(s).as_ptr())
            }),
            cb_clicked: None,
        }
    }

    pub fn text(&self) -> String {
        cql::to_string(unsafe { cql::btn_text(self.proxy.ch()) })
    }

    pub fn set_text(&mut self, s: &str) {
        unsafe {
            cql::btn_setText(self.proxy.ch(), cql::to_cstring(s).as_ptr());
        }
    }

    pub fn disconn(&mut self) {
        unsafe {
            cql::btn_disConn(self.proxy.ch());
        }
    }

    /// Set the callback on clicked.
    pub fn on_clicked(btn: &mut WgtArc, cb: CbClicked) {
        // TD macro
        let mut btn = btn.lock().unwrap();
        let btn = btn.as_btn_mut();

        btn.cb_clicked = Some(cb);
        unsafe {
            cql::btn_onClicked(btn.proxy.ch(), cql::x_cbBool {
                obj: btn as *const _ as cql::x_obj,
                cb:  Some(apply),
            })
        };

        unsafe extern "C" fn apply(par: cql::x_obj, b: cql::bool_) {
            use std::ops::DerefMut;

            let btn = par as *mut Btn;
            let mut cb = (*btn).cb_clicked.as_ref().unwrap().lock().unwrap(); // TD macro
            cb.deref_mut()(&mut *btn, 0 != b);
        }
    }
}

// eof