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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use crate::enums::{
Align, CallbackTrigger, Color, Damage, Event, Font, FrameType, LabelType, Shortcut,
};
use crate::image::Image;
use crate::prelude::*;
use crate::utils::FlString;
use fltk_sys::menu::*;
use std::{
ffi::{CStr, CString},
mem,
os::raw,
};
#[derive(WidgetBase, WidgetExt, MenuExt, Debug)]
pub struct MenuBar {
inner: *mut Fl_Menu_Bar,
tracker: *mut fltk_sys::fl::Fl_Widget_Tracker,
is_derived: bool,
}
#[derive(WidgetBase, WidgetExt, MenuExt, Debug)]
pub struct MenuButton {
inner: *mut Fl_Menu_Button,
tracker: *mut fltk_sys::fl::Fl_Widget_Tracker,
is_derived: bool,
}
#[repr(i32)]
#[derive(WidgetType, Debug, Copy, Clone, PartialEq)]
pub enum MenuButtonType {
Popup1 = 1,
Popup2,
Popup12,
Popup3,
Popup13,
Popup23,
Popup123,
}
impl MenuButton {
pub fn popup(&self) -> Option<MenuItem> {
assert!(!self.was_deleted());
unsafe {
let ptr = Fl_Menu_Button_popup(self.inner);
if ptr.is_null() {
None
} else {
let item = MenuItem {
inner: ptr as *mut Fl_Menu_Item,
size: Fl_Menu_Item_children(ptr),
};
Some(item)
}
}
}
}
#[derive(WidgetBase, WidgetExt, MenuExt, Debug)]
pub struct Choice {
inner: *mut Fl_Choice,
tracker: *mut fltk_sys::fl::Fl_Widget_Tracker,
is_derived: bool,
}
#[derive(WidgetBase, WidgetExt, MenuExt, Debug)]
pub struct SysMenuBar {
inner: *mut Fl_Sys_Menu_Bar,
tracker: *mut fltk_sys::fl::Fl_Widget_Tracker,
is_derived: bool,
}
#[derive(Debug, Clone)]
pub struct MenuItem {
inner: *mut Fl_Menu_Item,
size: i32,
}
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum MenuFlag {
Normal = 0,
Inactive = 1,
Toggle = 2,
Value = 4,
Radio = 8,
Invisible = 0x10,
SubmenuPointer = 0x20,
Submenu = 0x40,
MenuDivider = 0x80,
MenuHorizontal = 0x100,
}
impl MenuItem {
pub fn new(choices: &[&'static str]) -> MenuItem {
unsafe {
let sz = choices.len();
let mut temp: Vec<*mut raw::c_char> = vec![];
for &choice in choices {
let c = CString::safe_new(choice);
temp.push(c.into_raw());
}
let item_ptr = Fl_Menu_Item_new(temp.as_ptr() as *mut *mut raw::c_char, sz as i32);
assert!(!item_ptr.is_null());
MenuItem { inner: item_ptr, size: choices.len() as i32 }
}
}
pub fn popup(&self, x: i32, y: i32) -> Option<MenuItem> {
assert!(!self.was_deleted());
unsafe {
let item = Fl_Menu_Item_popup(self.inner, x, y);
if item.is_null() {
None
} else {
let item = MenuItem {
inner: item as *mut Fl_Menu_Item,
size: Fl_Menu_Item_children(item),
};
Some(item)
}
}
}
pub fn label(&self) -> Option<String> {
assert!(!self.was_deleted());
unsafe {
let label_ptr = Fl_Menu_Item_label(self.inner);
if label_ptr.is_null() {
return None;
}
Some(
CStr::from_ptr(label_ptr as *mut raw::c_char)
.to_string_lossy()
.to_string(),
)
}
}
pub fn set_label(&mut self, txt: &str) {
assert!(!self.was_deleted());
unsafe {
let txt = CString::safe_new(txt);
Fl_Menu_Item_set_label(self.inner, txt.into_raw());
}
}
pub fn label_type(&self) -> LabelType {
assert!(!self.was_deleted());
unsafe { mem::transmute(Fl_Menu_Item_label_type(self.inner)) }
}
pub fn set_label_type(&mut self, typ: LabelType) {
assert!(!self.was_deleted());
unsafe {
Fl_Menu_Item_set_label_type(self.inner, typ as i32);
}
}
pub fn label_color(&self) -> Color {
assert!(!self.was_deleted());
unsafe { mem::transmute(Fl_Menu_Item_label_color(self.inner)) }
}
pub fn set_label_color(&mut self, color: Color) {
assert!(!self.was_deleted());
unsafe { Fl_Menu_Item_set_label_color(self.inner, color.bits() as u32) }
}
pub fn label_font(&self) -> Font {
assert!(!self.was_deleted());
unsafe { mem::transmute(Fl_Menu_Item_label_font(self.inner)) }
}
pub fn set_label_font(&mut self, font: Font) {
assert!(!self.was_deleted());
unsafe { Fl_Menu_Item_set_label_font(self.inner, font.bits() as i32) }
}
pub fn label_size(&self) -> i32 {
assert!(!self.was_deleted());
unsafe { Fl_Menu_Item_label_size(self.inner) as i32 }
}
pub fn set_label_size(&mut self, sz: i32) {
assert!(!self.was_deleted());
let sz = if sz < 1 { 1 } else { sz };
unsafe { Fl_Menu_Item_set_label_size(self.inner, sz) }
}
pub fn value(&self) -> bool {
assert!(!self.was_deleted());
unsafe { Fl_Menu_Item_value(self.inner) != 0 }
}
pub fn set(&mut self) {
assert!(!self.was_deleted());
unsafe { Fl_Menu_Item_set(self.inner) }
}
pub fn clear(&mut self) {
assert!(!self.was_deleted());
unsafe { Fl_Menu_Item_clear(self.inner) }
}
pub fn visible(&self) -> bool {
assert!(!self.was_deleted());
unsafe { Fl_Menu_Item_visible(self.inner) != 0 }
}
pub fn active(&mut self) -> bool {
assert!(!self.was_deleted());
unsafe { Fl_Menu_Item_active(self.inner) != 0 }
}
pub fn activate(&mut self) {
assert!(!self.was_deleted());
unsafe { Fl_Menu_Item_activate(self.inner) }
}
pub fn deactivate(&mut self) {
assert!(!self.was_deleted());
unsafe { Fl_Menu_Item_deactivate(self.inner) }
}
pub fn is_submenu(&self) -> bool {
assert!(!self.was_deleted());
unsafe { Fl_Menu_Item_submenu(self.inner) != 0 }
}
pub fn is_checkbox(&self) -> bool {
assert!(!self.was_deleted());
unsafe { Fl_Menu_Item_checkbox(self.inner) != 0 }
}
pub fn is_radio(&self) -> bool {
assert!(!self.was_deleted());
unsafe { Fl_Menu_Item_radio(self.inner) != 0 }
}
pub fn show(&mut self) {
assert!(!self.was_deleted());
unsafe { Fl_Menu_Item_show(self.inner) }
}
pub fn hide(&mut self) {
assert!(!self.was_deleted());
unsafe { Fl_Menu_Item_hide(self.inner) }
}
pub fn next(&self, idx: i32) -> Option<MenuItem> {
assert!(!self.was_deleted());
unsafe {
let ptr = Fl_Menu_Item_next(self.inner, idx as i32);
if ptr.is_null() {
return None;
}
let label_ptr = Fl_Menu_Item_label(ptr);
if label_ptr.is_null() {
return None;
}
Some(MenuItem { inner: ptr, size: Fl_Menu_Item_children(ptr) })
}
}
pub fn children(&self) -> i32 {
unsafe {
Fl_Menu_Item_children(self.inner)
}
}
pub fn submenus(&self) -> i32 {
let mut i = 0;
while let Some(_item) = self.next(i) {
i += 1;
}
i
}
pub fn size(&self) -> i32 {
self.size
}
pub fn at(&self, idx: i32) -> Option<MenuItem> {
assert!(idx < self.size);
unsafe {
let ptr = Fl_Menu_Item_at(self.inner, idx);
if ptr.is_null() {
None
} else {
Some(MenuItem { inner: ptr as _, size: Fl_Menu_Item_children(ptr) })
}
}
}
pub unsafe fn user_data(&self) -> Option<Box<dyn FnMut()>> {
let ptr = Fl_Menu_Item_user_data(self.inner);
if ptr.is_null() {
None
} else {
let x = ptr as *mut Box<dyn FnMut()>;
let x = Box::from_raw(x);
Fl_Menu_Item_set_callback(self.inner, None, std::ptr::null_mut());
Some(*x)
}
}
pub fn set_callback<F: FnMut(&mut Self) + 'static>(&mut self, cb: F) {
assert!(!self.was_deleted());
unsafe {
unsafe extern "C" fn shim(wid: *mut fltk_sys::menu::Fl_Widget, data: *mut raw::c_void) {
let mut wid = crate::widget::Widget::from_widget_ptr(wid as *mut _);
let a: *mut Box<dyn FnMut(&mut crate::widget::Widget)> =
data as *mut Box<dyn FnMut(&mut crate::widget::Widget)>;
let f: &mut (dyn FnMut(&mut crate::widget::Widget)) = &mut **a;
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(&mut wid)));
}
let _old_data = self.user_data();
let a: *mut Box<dyn FnMut(&mut Self)> = Box::into_raw(Box::new(Box::new(cb)));
let data: *mut raw::c_void = a as *mut std::ffi::c_void;
let callback: fltk_sys::menu::Fl_Callback = Some(shim);
Fl_Menu_Item_set_callback(self.inner, callback, data);
}
}
pub fn emit<T: 'static + Clone + Send + Sync>(
&mut self,
sender: crate::app::Sender<T>,
msg: T,
) {
self.set_callback(move |_| sender.send(msg.clone()));
}
pub fn was_deleted(&self) -> bool {
self.inner.is_null()
}
pub fn draw<M: MenuExt>(&self, x: i32, y: i32, w: i32, h: i32, menu: &M, selected: bool) {
assert!(!self.was_deleted());
unsafe {
Fl_Menu_Item_draw(
self.inner,
x,
y,
w,
h,
menu.as_widget_ptr() as _,
selected as i32,
)
}
}
pub fn measure(&self) -> (i32, i32) {
assert!(!self.was_deleted());
let h: &mut i32 = &mut 0;
let ret = unsafe { Fl_Menu_Item_measure(self.inner, h as _, std::ptr::null()) };
(ret, *h)
}
#[doc(hidden)]
pub unsafe fn set_image<I: ImageExt>(&mut self, mut image: I) {
assert!(!self.was_deleted());
assert!(!image.was_deleted());
image.increment_arc();
Fl_Menu_Item_image(self.inner, image.as_image_ptr() as _)
}
pub fn add_image<I: ImageExt>(&mut self, image: Option<I>, on_left: bool) {
assert!(!self.was_deleted());
unsafe {
if let Some(image) = image {
assert!(!image.was_deleted());
Fl_Menu_Item_add_image(self.inner, image.as_image_ptr() as _, on_left as i32)
} else {
Fl_Menu_Item_add_image(self.inner, std::ptr::null_mut(), on_left as i32)
}
}
}
}
pub unsafe fn delete_menu_item(item: MenuItem) {
Fl_Menu_Item_delete(item.inner)
}
unsafe impl Send for MenuItem {}
unsafe impl Sync for MenuItem {}
impl PartialEq for MenuItem {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}
impl Eq for MenuItem {}
impl IntoIterator for MenuItem {
type Item = MenuItem;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
let mut v: Vec<MenuItem> = vec![];
let mut i = 0;
while let Some(item) = self.at(i) {
v.push(item);
i += 1;
}
v.into_iter()
}
}