-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.go
220 lines (172 loc) · 4.12 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
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
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import (
"fmt"
"syscall"
"unsafe"
)
import (
"github.com/lxn/win"
)
type Menu struct {
hMenu win.HMENU
hWnd win.HWND
actions *ActionList
}
func newMenuBar(hWnd win.HWND) (*Menu, error) {
hMenu := win.CreateMenu()
if hMenu == 0 {
return nil, lastError("CreateMenu")
}
m := &Menu{hMenu: hMenu, hWnd: hWnd}
m.actions = newActionList(m)
return m, nil
}
func NewMenu() (*Menu, error) {
hMenu := win.CreatePopupMenu()
if hMenu == 0 {
return nil, lastError("CreatePopupMenu")
}
var mi win.MENUINFO
mi.CbSize = uint32(unsafe.Sizeof(mi))
if !win.GetMenuInfo(hMenu, &mi) {
return nil, lastError("GetMenuInfo")
}
mi.FMask |= win.MIM_STYLE
mi.DwStyle = win.MNS_CHECKORBMP
if !win.SetMenuInfo(hMenu, &mi) {
return nil, lastError("SetMenuInfo")
}
m := &Menu{hMenu: hMenu}
m.actions = newActionList(m)
return m, nil
}
func (m *Menu) Dispose() {
if m.hMenu != 0 {
win.DestroyMenu(m.hMenu)
m.hMenu = 0
}
}
func (m *Menu) IsDisposed() bool {
return m.hMenu == 0
}
func (m *Menu) Actions() *ActionList {
return m.actions
}
func (m *Menu) initMenuItemInfoFromAction(mii *win.MENUITEMINFO, action *Action) {
mii.CbSize = uint32(unsafe.Sizeof(*mii))
mii.FMask = win.MIIM_FTYPE | win.MIIM_ID | win.MIIM_STATE | win.MIIM_STRING
if action.image != nil {
mii.FMask |= win.MIIM_BITMAP
mii.HbmpItem = action.image.handle()
}
if action.IsSeparator() {
mii.FType = win.MFT_SEPARATOR
} else {
mii.FType = win.MFT_STRING
var text string
if s := action.shortcut; s.Key != 0 {
text = fmt.Sprintf("%s\t%s", action.text, s.String())
} else {
text = action.text
}
mii.DwTypeData = syscall.StringToUTF16Ptr(text)
mii.Cch = uint32(len([]rune(action.text)))
}
mii.WID = uint32(action.id)
if action.Enabled() {
mii.FState &^= win.MFS_DISABLED
} else {
mii.FState |= win.MFS_DISABLED
}
if action.Checkable() {
mii.FMask |= win.MIIM_CHECKMARKS
}
if action.Checked() {
mii.FState |= win.MFS_CHECKED
}
menu := action.menu
if menu != nil {
mii.FMask |= win.MIIM_SUBMENU
mii.HSubMenu = menu.hMenu
}
}
func (m *Menu) onActionChanged(action *Action) error {
if !action.Visible() {
return nil
}
var mii win.MENUITEMINFO
m.initMenuItemInfoFromAction(&mii, action)
if !win.SetMenuItemInfo(m.hMenu, uint32(m.actions.indexInObserver(action)), true, &mii) {
return newError("SetMenuItemInfo failed")
}
return nil
}
func (m *Menu) onActionVisibleChanged(action *Action) error {
if !action.IsSeparator() {
defer m.actions.updateSeparatorVisibility()
}
if action.Visible() {
return m.insertAction(action, true)
}
return m.removeAction(action, true)
}
func (m *Menu) insertAction(action *Action, visibleChanged bool) (err error) {
if !visibleChanged {
action.addChangedHandler(m)
defer func() {
if err != nil {
action.removeChangedHandler(m)
}
}()
}
if !action.Visible() {
return
}
index := m.actions.indexInObserver(action)
var mii win.MENUITEMINFO
m.initMenuItemInfoFromAction(&mii, action)
if !win.InsertMenuItem(m.hMenu, uint32(index), true, &mii) {
return newError("InsertMenuItem failed")
}
menu := action.menu
if menu != nil {
menu.hWnd = m.hWnd
}
if m.hWnd != 0 {
win.DrawMenuBar(m.hWnd)
}
return
}
func (m *Menu) removeAction(action *Action, visibleChanged bool) error {
index := m.actions.indexInObserver(action)
if !win.RemoveMenu(m.hMenu, uint32(index), win.MF_BYPOSITION) {
return lastError("RemoveMenu")
}
if !visibleChanged {
action.removeChangedHandler(m)
}
if m.hWnd != 0 {
win.DrawMenuBar(m.hWnd)
}
return nil
}
func (m *Menu) onInsertedAction(action *Action) error {
return m.insertAction(action, false)
}
func (m *Menu) onRemovingAction(action *Action) error {
return m.removeAction(action, false)
}
func (m *Menu) onClearingActions() error {
for i := m.actions.Len() - 1; i >= 0; i-- {
if action := m.actions.At(i); action.Visible() {
if err := m.onRemovingAction(action); err != nil {
return err
}
}
}
return nil
}