forked from czkoko/ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
menu.go
129 lines (102 loc) · 2.93 KB
/
menu.go
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
package ui
import (
"unsafe"
)
// #include "ui.h"
// extern void doMenuItemOnClicked(uiMenuItem *, uiWindow *, void *);
// static inline void realuiMenuItemOnClicked(uiMenuItem *i)
// {
// uiMenuItemOnClicked(i, doMenuItemOnClicked, NULL);
// }
import "C"
var menuItems = make(map[*C.uiMenuItem]*MenuItem)
// Menu represents a menu on the menu bar located at the top
// of a Window. Currently, every window has the same menu bar
// and thus the same menus and menu items.
type Menu struct {
m *C.uiMenu
}
// MenuItem represents an item in a Menu.
type MenuItem struct {
i *C.uiMenuItem
onClicked func(*MenuItem, *Window)
onClickedAllowed bool
}
// NewMenu creates a new Menu.
func NewMenu(name string) *Menu {
m := new(Menu)
cname := C.CString(name)
m.m = C.uiNewMenu(cname)
freestr(cname)
return m
}
// AppendSeparator appends a separator to the Menu.
func (m *Menu) AppendSeparator() {
C.uiMenuAppendSeparator(m.m)
}
// AppendItem appends a new item to the Menu.
func (m *Menu) AppendItem(name string) *MenuItem {
cname := C.CString(name)
i := C.uiMenuAppendItem(m.m, cname)
freestr(cname)
return newMenuItem(i, true)
}
// AppendCheckItem appends a check item to the Menu.
func (m *Menu) AppendCheckItem(name string) *MenuItem {
cname := C.CString(name)
i := C.uiMenuAppendCheckItem(m.m, cname)
freestr(cname)
return newMenuItem(i, true)
}
// AppendQuitItem appends a quit item to the Menu.
func (m *Menu) AppendQuitItem() *MenuItem {
i := C.uiMenuAppendQuitItem(m.m)
return newMenuItem(i, false)
}
// AppendPreferencesItem appends a preferences item to the Menu.
func (m *Menu) AppendPreferencesItem() *MenuItem {
i := C.uiMenuAppendPreferencesItem(m.m)
return newMenuItem(i, true)
}
// AppendAboutItem appends an about item to the Menu.
func (m *Menu) AppendAboutItem() *MenuItem {
i := C.uiMenuAppendAboutItem(m.m)
return newMenuItem(i, true)
}
// Enable enables the MenuItem.
func (i *MenuItem) Enable() {
C.uiMenuItemEnable(i.i)
}
// Disable disables the MenuItem.
func (i *MenuItem) Disable() {
C.uiMenuItemDisable(i.i)
}
// Checked returns the check status of a menu item.
func (i *MenuItem) Checked() bool {
return tobool(C.uiMenuItemChecked(i.i))
}
// SetChecked sets the check status of a menu item.
func (i *MenuItem) SetChecked(checked bool) {
C.uiMenuItemSetChecked(i.i, frombool(checked))
}
// OnClicked registers f to be run when the user clicks the MenuItem.
// Only one function can be registered at a time.
func (i *MenuItem) OnClicked(f func(*MenuItem, *Window)) {
i.onClicked = f
}
//export doMenuItemOnClicked
func doMenuItemOnClicked(i *C.uiMenuItem, w *C.uiWindow, data unsafe.Pointer) {
ii := menuItems[i]
//ww := windows[w]
if ii.onClicked != nil {
ii.onClicked(ii, nil)
}
}
func newMenuItem(i *C.uiMenuItem, onClickedAllowed bool) *MenuItem {
if onClickedAllowed {
C.realuiMenuItemOnClicked(i)
}
result := &MenuItem{i: i, onClickedAllowed: onClickedAllowed}
menuItems[i] = result
return result
}