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
use controls::Window;
use libc::{c_int, c_void};
use std::ffi::CString;
use std::mem;
use ui_sys::{self, uiMenu, uiMenuItem, uiWindow};
use UI;
#[derive(Clone)]
pub struct MenuItem {
ui_menu_item: *mut uiMenuItem,
}
#[derive(Clone)]
pub struct Menu {
ui_menu: *mut uiMenu,
}
impl MenuItem {
pub fn enable(&self, _ctx: &UI) {
unsafe { ui_sys::uiMenuItemEnable(self.ui_menu_item) }
}
pub fn disable(&self, _ctx: &UI) {
unsafe { ui_sys::uiMenuItemDisable(self.ui_menu_item) }
}
pub fn checked(&self, _ctx: &UI) -> bool {
unsafe { ui_sys::uiMenuItemChecked(self.ui_menu_item) != 0 }
}
pub fn set_checked(&self, _ctx: &UI, checked: bool) {
unsafe { ui_sys::uiMenuItemSetChecked(self.ui_menu_item, checked as c_int) }
}
pub fn on_clicked<F: FnMut(&MenuItem, &Window)>(&self, _ctx: &UI, callback: F) {
unsafe {
let mut data: Box<Box<FnMut(&MenuItem, &Window)>> = Box::new(Box::new(callback));
ui_sys::uiMenuItemOnClicked(
self.ui_menu_item,
c_callback,
&mut *data as *mut Box<FnMut(&MenuItem, &Window)> as *mut c_void,
);
mem::forget(data);
}
extern "C" fn c_callback(
menu_item: *mut uiMenuItem,
window: *mut uiWindow,
data: *mut c_void,
) {
unsafe {
let menu_item = MenuItem {
ui_menu_item: menu_item,
};
let window = Window::from_raw(window);
mem::transmute::<*mut c_void, &mut Box<FnMut(&MenuItem, &Window)>>(data)(
&menu_item, &window,
);
mem::forget(window);
}
}
}
pub unsafe fn from_raw(raw: *mut uiMenuItem) -> Self {
MenuItem { ui_menu_item: raw }
}
}
impl Menu {
pub fn new(_ctx: &UI, name: &str) -> Menu {
unsafe {
let c_string = CString::new(name.as_bytes().to_vec()).unwrap();
Menu {
ui_menu: ui_sys::uiNewMenu(c_string.as_ptr()),
}
}
}
pub fn append_item(&self, name: &str) -> MenuItem {
unsafe {
let c_string = CString::new(name.as_bytes().to_vec()).unwrap();
MenuItem {
ui_menu_item: ui_sys::uiMenuAppendItem(self.ui_menu, c_string.as_ptr()),
}
}
}
pub fn append_check_item(&self, name: &str) -> MenuItem {
unsafe {
let c_string = CString::new(name.as_bytes().to_vec()).unwrap();
MenuItem {
ui_menu_item: ui_sys::uiMenuAppendCheckItem(self.ui_menu, c_string.as_ptr()),
}
}
}
pub fn append_separator(&self) {
unsafe { ui_sys::uiMenuAppendSeparator(self.ui_menu) }
}
}