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
use super::Control;
use libc::c_void;
use std::ffi::{CStr, CString};
use std::mem;
use ui::UI;
use ui_sys::{self, uiButton, uiControl, uiLabel};
define_control!{
rust_type: Label,
sys_type: uiLabel
}
define_control!{
rust_type: Button,
sys_type: uiButton
}
impl Button {
pub fn new(_ctx: &UI, text: &str) -> Button {
unsafe {
let c_string = CString::new(text.as_bytes().to_vec()).unwrap();
Button::from_raw(ui_sys::uiNewButton(c_string.as_ptr()))
}
}
pub fn text(&self, _ctx: &UI) -> String {
unsafe {
CStr::from_ptr(ui_sys::uiButtonText(self.uiButton))
.to_string_lossy()
.into_owned()
}
}
pub fn text_ref(&self, _ctx: &UI) -> &CStr {
unsafe { CStr::from_ptr(ui_sys::uiButtonText(self.uiButton)) }
}
pub fn set_text(&mut self, _ctx: &UI, text: &str) {
unsafe {
let c_string = CString::new(text.as_bytes().to_vec()).unwrap();
ui_sys::uiButtonSetText(self.uiButton, c_string.as_ptr())
}
}
pub fn on_clicked<F: FnMut(&mut Button)>(&mut self, _ctx: &UI, callback: F) {
unsafe {
let mut data: Box<Box<FnMut(&mut Button)>> = Box::new(Box::new(callback));
ui_sys::uiButtonOnClicked(
self.uiButton,
c_callback,
&mut *data as *mut Box<FnMut(&mut Button)> as *mut c_void,
);
mem::forget(data);
}
extern "C" fn c_callback(button: *mut uiButton, data: *mut c_void) {
unsafe {
let mut button = Button { uiButton: button };
mem::transmute::<*mut c_void, &mut Box<FnMut(&mut Button)>>(data)(&mut button)
}
}
}
}
impl Label {
pub fn new(_ctx: &UI, text: &str) -> Label {
unsafe {
let c_string = CString::new(text.as_bytes().to_vec()).unwrap();
Label::from_raw(ui_sys::uiNewLabel(c_string.as_ptr()))
}
}
pub fn text(&self, _ctx: &UI) -> String {
unsafe {
CStr::from_ptr(ui_sys::uiLabelText(self.uiLabel))
.to_string_lossy()
.into_owned()
}
}
pub fn text_ref(&self, _ctx: &UI) -> &CStr {
unsafe { CStr::from_ptr(ui_sys::uiLabelText(self.uiLabel)) }
}
pub fn set_text(&mut self, _ctx: &UI, text: &str) {
unsafe {
let c_string = CString::new(text.as_bytes().to_vec()).unwrap();
ui_sys::uiLabelSetText(self.uiLabel, c_string.as_ptr())
}
}
}