forked from lxn/walk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.go
307 lines (245 loc) · 5.92 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
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
// 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"
"github.com/lxn/win"
)
type Menu struct {
hMenu win.HMENU
window Window
actions *ActionList
getDPI func() int
}
func newMenuBar(window Window) (*Menu, error) {
hMenu := win.CreateMenu()
if hMenu == 0 {
return nil, lastError("CreateMenu")
}
m := &Menu{
hMenu: hMenu,
window: window,
}
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() {
m.actions.Clear()
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) updateItemsWithImageForWindow(window Window) {
if m.window == nil {
m.window = window
defer func() {
m.window = nil
}()
}
for _, action := range m.actions.actions {
if action.image != nil {
m.onActionChanged(action)
}
if action.menu != nil {
action.menu.updateItemsWithImageForWindow(window)
}
}
}
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
dpi := 96
if m.getDPI != nil {
dpi = m.getDPI()
} else if m.window != nil {
dpi = m.window.DPI()
}
if bmp, err := iconCache.Bitmap(action.image, dpi); err == nil {
mii.HbmpItem = bmp.hBmp
}
}
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
}
if action.Exclusive() {
mii.FType |= win.MFT_RADIOCHECK
}
menu := action.menu
if menu != nil {
mii.FMask |= win.MIIM_SUBMENU
mii.HSubMenu = menu.hMenu
}
}
func (m *Menu) handleDefaultState(action *Action) {
if action.Default() {
// Unset other default actions before we set this one. Otherwise insertion fails.
win.SetMenuDefaultItem(m.hMenu, ^uint32(0), false)
for _, otherAction := range m.actions.actions {
if otherAction != action {
otherAction.SetDefault(false)
}
}
}
}
func (m *Menu) onActionChanged(action *Action) error {
m.handleDefaultState(action)
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")
}
if action.Default() {
win.SetMenuDefaultItem(m.hMenu, uint32(m.actions.indexInObserver(action)), true)
}
if action.Exclusive() && action.Checked() {
var first, last int
index := m.actions.Index(action)
for i := index; i >= 0; i-- {
first = i
if !m.actions.At(i).Exclusive() {
break
}
}
for i := index; i < m.actions.Len(); i++ {
last = i
if !m.actions.At(i).Exclusive() {
break
}
}
if !win.CheckMenuRadioItem(m.hMenu, uint32(first), uint32(last), uint32(index), win.MF_BYPOSITION) {
return newError("CheckMenuRadioItem 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) {
m.handleDefaultState(action)
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")
}
if action.Default() {
win.SetMenuDefaultItem(m.hMenu, uint32(m.actions.indexInObserver(action)), true)
}
menu := action.menu
if menu != nil {
menu.window = m.window
}
m.ensureMenuBarRedrawn()
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)
}
m.ensureMenuBarRedrawn()
return nil
}
func (m *Menu) ensureMenuBarRedrawn() {
if m.window != nil {
if mw, ok := m.window.(*MainWindow); ok && mw != nil {
win.DrawMenuBar(mw.Handle())
}
}
}
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
}