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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use super::Control;
use libc::c_void;
use std::ffi::{CStr, CString};
use std::i64;
use std::mem;
use ui::UI;
use ui_sys::{
self, uiCheckbox, uiCombobox, uiControl, uiEntry, uiMultilineEntry, uiSlider, uiSpinbox,
};
pub trait NumericEntry {
fn value(&self, ctx: &UI) -> i64;
fn set_value(&mut self, ctx: &UI, value: i64);
fn on_changed<F: FnMut(i64)>(&mut self, ctx: &UI, callback: F);
}
pub trait TextEntry {
fn value(&self, ctx: &UI) -> String;
fn set_value(&mut self, ctx: &UI, value: &str);
fn on_changed<F: FnMut(String)>(&mut self, ctx: &UI, callback: F);
}
define_control!{
rust_type: Spinbox,
sys_type: uiSpinbox
}
define_control!{
rust_type: Slider,
sys_type: uiSlider
}
impl Spinbox {
pub fn new(_ctx: &UI, min: i64, max: i64) -> Self {
unsafe { Spinbox::from_raw(ui_sys::uiNewSpinbox(min, max)) }
}
pub fn new_unlimited(_ctx: &UI) -> Self {
Self::new(_ctx, i64::MIN, i64::MAX)
}
}
impl Slider {
pub fn new(_ctx: &UI, min: i64, max: i64) -> Self {
unsafe { Slider::from_raw(ui_sys::uiNewSlider(min, max)) }
}
}
impl NumericEntry for Spinbox {
fn value(&self, _ctx: &UI) -> i64 {
unsafe { ui_sys::uiSpinboxValue(self.uiSpinbox) }
}
fn set_value(&mut self, _ctx: &UI, value: i64) {
unsafe { ui_sys::uiSpinboxSetValue(self.uiSpinbox, value) }
}
fn on_changed<F: FnMut(i64)>(&mut self, _ctx: &UI, callback: F) {
unsafe {
let mut data: Box<Box<FnMut(i64)>> = Box::new(Box::new(callback));
ui_sys::uiSpinboxOnChanged(
self.uiSpinbox,
c_callback,
&mut *data as *mut Box<FnMut(i64)> as *mut c_void,
);
mem::forget(data);
}
extern "C" fn c_callback(spinbox: *mut uiSpinbox, data: *mut c_void) {
unsafe {
let val = ui_sys::uiSpinboxValue(spinbox);
mem::transmute::<*mut c_void, &mut Box<FnMut(i64)>>(data)(val);
}
}
}
}
impl NumericEntry for Slider {
fn value(&self, _ctx: &UI) -> i64 {
unsafe { ui_sys::uiSliderValue(self.uiSlider) }
}
fn set_value(&mut self, _ctx: &UI, value: i64) {
unsafe { ui_sys::uiSliderSetValue(self.uiSlider, value) }
}
fn on_changed<F: FnMut(i64)>(&mut self, _ctx: &UI, callback: F) {
unsafe {
let mut data: Box<Box<FnMut(i64)>> = Box::new(Box::new(callback));
ui_sys::uiSliderOnChanged(
self.uiSlider,
c_callback,
&mut *data as *mut Box<FnMut(i64)> as *mut c_void,
);
mem::forget(data);
}
extern "C" fn c_callback(slider: *mut uiSlider, data: *mut c_void) {
unsafe {
let val = ui_sys::uiSliderValue(slider);
mem::transmute::<*mut c_void, &mut Box<FnMut(i64)>>(data)(val);
}
}
}
}
define_control! {
rust_type: Entry,
sys_type: uiEntry
}
define_control! {
rust_type: MultilineEntry,
sys_type: uiMultilineEntry
}
impl Entry {
pub fn new(_ctx: &UI) -> Entry {
unsafe { Entry::from_raw(ui_sys::uiNewEntry()) }
}
}
impl MultilineEntry {
pub fn new(_ctx: &UI) -> MultilineEntry {
unsafe { MultilineEntry::from_raw(ui_sys::uiNewMultilineEntry()) }
}
}
impl TextEntry for Entry {
fn value(&self, _ctx: &UI) -> String {
unsafe {
CStr::from_ptr(ui_sys::uiEntryText(self.uiEntry))
.to_string_lossy()
.into_owned()
}
}
fn set_value(&mut self, _ctx: &UI, value: &str) {
let cstring = CString::new(value.as_bytes().to_vec()).unwrap();
unsafe { ui_sys::uiEntrySetText(self.uiEntry, cstring.as_ptr()) }
}
fn on_changed<F: FnMut(String)>(&mut self, _ctx: &UI, callback: F) {
unsafe {
let mut data: Box<Box<FnMut(String)>> = Box::new(Box::new(callback));
ui_sys::uiEntryOnChanged(
self.uiEntry,
c_callback,
&mut *data as *mut Box<FnMut(String)> as *mut c_void,
);
mem::forget(data);
}
extern "C" fn c_callback(entry: *mut uiEntry, data: *mut c_void) {
unsafe {
let string = CStr::from_ptr(ui_sys::uiEntryText(entry))
.to_string_lossy()
.into_owned();
mem::transmute::<*mut c_void, &mut Box<FnMut(String)>>(data)(string);
mem::forget(entry);
}
}
}
}
impl TextEntry for MultilineEntry {
fn value(&self, _ctx: &UI) -> String {
unsafe {
CStr::from_ptr(ui_sys::uiMultilineEntryText(self.uiMultilineEntry))
.to_string_lossy()
.into_owned()
}
}
fn set_value(&mut self, _ctx: &UI, value: &str) {
let cstring = CString::new(value.as_bytes().to_vec()).unwrap();
unsafe { ui_sys::uiMultilineEntrySetText(self.uiMultilineEntry, cstring.as_ptr()) }
}
fn on_changed<F: FnMut(String)>(&mut self, _ctx: &UI, callback: F) {
unsafe {
let mut data: Box<Box<FnMut(String)>> = Box::new(Box::new(callback));
ui_sys::uiMultilineEntryOnChanged(
self.uiMultilineEntry,
c_callback,
&mut *data as *mut Box<FnMut(String)> as *mut c_void,
);
mem::forget(data);
}
extern "C" fn c_callback(entry: *mut uiMultilineEntry, data: *mut c_void) {
unsafe {
let string = CStr::from_ptr(ui_sys::uiMultilineEntryText(entry))
.to_string_lossy()
.into_owned();
mem::transmute::<*mut c_void, &mut Box<FnMut(String)>>(data)(string);
mem::forget(entry);
}
}
}
}
define_control! {
rust_type: Combobox,
sys_type: uiCombobox
}
impl Combobox {
pub fn new(_ctx: &UI) -> Self {
unsafe { Combobox::from_raw(ui_sys::uiNewCombobox()) }
}
pub fn append(&self, _ctx: &UI, name: &str) {
unsafe {
let c_string = CString::new(name.as_bytes().to_vec()).unwrap();
ui_sys::uiComboboxAppend(self.uiCombobox, c_string.as_ptr())
}
}
pub fn set_selected(&mut self, _ctx: &UI, value: i64) {
unsafe { ui_sys::uiComboboxSetSelected(self.uiCombobox, value) }
}
pub fn on_selected<F: FnMut(i64)>(&mut self, _ctx: &UI, callback: F) {
unsafe {
let mut data: Box<Box<FnMut(i64)>> = Box::new(Box::new(callback));
ui_sys::uiComboboxOnSelected(
self.uiCombobox,
c_callback,
&mut *data as *mut Box<FnMut(i64)> as *mut c_void,
);
mem::forget(data);
}
extern "C" fn c_callback(combobox: *mut uiCombobox, data: *mut c_void) {
unsafe {
let val = ui_sys::uiComboboxSelected(combobox);
mem::transmute::<*mut c_void, &mut Box<FnMut(i64)>>(data)(val);
}
}
}
}
define_control! {
rust_type: Checkbox,
sys_type: uiCheckbox
}
impl Checkbox {
pub fn new(_ctx: &UI, text: &str) -> Self {
let c_string = CString::new(text.as_bytes().to_vec()).unwrap();
unsafe { Checkbox::from_raw(ui_sys::uiNewCheckbox(c_string.as_ptr())) }
}
pub fn checked(&self, _ctx: &UI) -> bool {
unsafe { ui_sys::uiCheckboxChecked(self.uiCheckbox) != 0 }
}
pub fn set_checked(&mut self, _ctx: &UI, checked: bool) {
unsafe { ui_sys::uiCheckboxSetChecked(self.uiCheckbox, checked as i32) }
}
pub fn on_toggled<F: FnMut(bool)>(&mut self, _ctx: &UI, callback: F) {
unsafe {
let mut data: Box<Box<FnMut(bool)>> = Box::new(Box::new(callback));
ui_sys::uiCheckboxOnToggled(
self.uiCheckbox,
c_callback,
&mut *data as *mut Box<FnMut(bool)> as *mut c_void,
);
mem::forget(data);
}
extern "C" fn c_callback(checkbox: *mut uiCheckbox, data: *mut c_void) {
unsafe {
let val = ui_sys::uiCheckboxChecked(checkbox) != 0;
mem::transmute::<*mut c_void, &mut Box<FnMut(bool)>>(data)(val);
}
}
}
}