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
use std::ptr;
use std::mem;
use std::hash::Hash;
use winapi::{HMENU, DWORD, HBRUSH, c_int, UINT, BOOL};
use ui::UiInner;
use controls::AnyHandle;
pub unsafe fn list_menu_children<ID: Hash+Clone>(ui: &UiInner<ID>, menu: HMENU) -> Vec<u64> {
use low::defs::{GetMenuItemCount, GetSubMenu, GetMenuItemID};
let mut children: Vec<u64> = Vec::new();
let children_count = GetMenuItemCount(menu);
for i in 0..children_count {
let sub_menu = GetSubMenu(menu, i as c_int);
if sub_menu.is_null() {
let handle = AnyHandle::HMENU_ITEM(menu, GetMenuItemID(menu, i));
let id = ui.inner_id_from_handle(&handle) .expect("Could not match menu handle to menu control");
children.push( id );
} else {
let handle = AnyHandle::HMENU(sub_menu);
let id = ui.inner_id_from_handle(&handle).expect("Could not match menu handle to menu control");
children.push( id );
children.append( &mut list_menu_children(ui, sub_menu) );
}
}
children
}
#[inline(always)]
unsafe fn resolve_menu_parent(parent: &AnyHandle) -> HMENU {
use user32::GetMenu;
match parent {
&AnyHandle::HWND(parent_h) => {
let menubar = GetMenu(parent_h);
if menubar.is_null() { panic!("Tried to resolve a menu parent, but the parent window do not have a menubar.") }
menubar
},
&AnyHandle::HMENU(parent_h) => parent_h,
_ => { unreachable!(); }
}
}
#[inline(always)]
pub unsafe fn menu_index_in_parent(h: HMENU, parent: &AnyHandle) -> UINT {
use low::defs::{GetMenuItemCount, GetSubMenu};
let parent_h = resolve_menu_parent(parent);
let children_count = GetMenuItemCount(parent_h);
let mut sub_menu: HMENU;
for i in 0..children_count {
sub_menu = GetSubMenu(parent_h, i as c_int);
if sub_menu.is_null() { continue; }
else if sub_menu == h { return i as UINT; }
}
panic!("Menu/MenuItem not found in parent!")
}
pub unsafe fn remove_menu_from_parent(h: HMENU, parent: &AnyHandle) {
use user32::{GetMenu, DrawMenuBar};
use low::defs::{RemoveMenu, MF_BYPOSITION};
let index = menu_index_in_parent(h, parent);
match parent {
&AnyHandle::HWND(parent_h) => {
let menubar = GetMenu(parent_h);
RemoveMenu(menubar, index, MF_BYPOSITION);
DrawMenuBar(parent_h);
},
&AnyHandle::HMENU(parent_h) => {
RemoveMenu(parent_h, index, MF_BYPOSITION);
}
_ => { unreachable!(); }
}
}
pub unsafe fn remove_menu_item_from_parent(parent_h: HMENU, uid: UINT) {
use low::defs::RemoveMenu;
RemoveMenu(parent_h, uid, 0);
}
#[inline(always)]
pub unsafe fn use_menu_command(h: HMENU) {
use low::defs::{MENUINFO, MNS_NOTIFYBYPOS, MIM_STYLE, SetMenuInfo};
let mut info = MENUINFO {
cbSize: mem::size_of::<MENUINFO>() as DWORD,
fMask: MIM_STYLE,
dwStyle: MNS_NOTIFYBYPOS,
cyMax: 0,
hbrBack: mem::transmute(ptr::null_mut::<HBRUSH>()),
dwContextHelpID: 0,
dwMenuData: 0
};
SetMenuInfo(h, &mut info);
}
#[inline(always)]
pub unsafe fn enable_menuitem(h: HMENU, pos: Option<UINT>, id: Option<UINT>, enabled: bool) {
use winapi::MENUITEMINFOW;
use low::defs::{SetMenuItemInfoW, GetMenuItemCount, MIIM_STATE, MFS_DISABLED, MFS_ENABLED};
let use_position = id.is_none();
let choice = if use_position { pos } else { id };
let value = match choice {
Some(p) => p,
None => (GetMenuItemCount(h) - 1) as u32
};
let state = match enabled {
true => MFS_ENABLED,
false => MFS_DISABLED
};
let mut info = MENUITEMINFOW {
cbSize: mem::size_of::<MENUITEMINFOW>() as UINT,
fMask: MIIM_STATE, fType: 0, fState: state,
wID: 0, hSubMenu: ptr::null_mut(), hbmpChecked: ptr::null_mut(),
hbmpUnchecked: ptr::null_mut(), dwItemData: 0, dwTypeData: ptr::null_mut(),
cch: 0, hbmpItem: ptr::null_mut()
};
SetMenuItemInfoW(h, value, use_position as BOOL, &mut info);
}
#[inline(always)]
pub unsafe fn enable_menu(menu: HMENU, parent: &AnyHandle, enabled: bool) {
let parent_h = resolve_menu_parent(parent);
let index = menu_index_in_parent(menu, parent);
enable_menuitem(parent_h, Some(index), None, enabled);
}
#[inline(always)]
pub unsafe fn is_menuitem_enabled(h: HMENU, pos: Option<UINT>, id: Option<UINT>) -> bool {
use winapi::MENUITEMINFOW;
use low::defs::{GetMenuItemInfoW, MIIM_STATE, MFS_DISABLED};
if id.is_none() && pos.is_none() { panic!("Both pos and id are None"); }
let use_position = id.is_none();
let choice = if use_position { pos } else { id };
let value = match choice {
Some(p) => p,
None => unreachable!()
};
let mut info = MENUITEMINFOW {
cbSize: mem::size_of::<MENUITEMINFOW>() as UINT,
fMask: MIIM_STATE, fType: 0, fState: 0,
wID: 0, hSubMenu: ptr::null_mut(), hbmpChecked: ptr::null_mut(),
hbmpUnchecked: ptr::null_mut(), dwItemData: 0, dwTypeData: ptr::null_mut(),
cch: 0, hbmpItem: ptr::null_mut()
};
GetMenuItemInfoW(h, value, use_position as BOOL, &mut info);
(info.fState & MFS_DISABLED) != MFS_DISABLED
}
#[inline(always)]
pub unsafe fn is_menu_enabled(menu: HMENU, parent: &AnyHandle) -> bool {
let parent_h = resolve_menu_parent(parent);
let index = menu_index_in_parent(menu, parent);
is_menuitem_enabled(parent_h, Some(index), None)
}
#[inline(always)]
pub unsafe fn get_menu_id(parent_h: HMENU, index: c_int) -> UINT {
::low::defs::GetMenuItemID(parent_h, index)
}