-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmenu.go
446 lines (365 loc) · 10.2 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
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
// 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.
//go:build windows
// +build windows
package walk
import (
"fmt"
"syscall"
"unsafe"
"github.com/tailscale/win"
)
type Menu struct {
hMenu win.HMENU
window Window
actions *ActionList
getDPI func() int
initPopupPublisher EventPublisher
sharedMetrics *menuSharedMetrics // shared theme metrics across all menus associated with the current window
perMenuMetrics menuSpecificMetrics // per-menu metrics
allowOwnerDrawInvalidation bool
}
func newMenuBar(window Window) (menu *Menu, _ error) {
hMenu := win.CreateMenu()
if hMenu == 0 {
return nil, lastError("CreateMenu")
}
defer func() {
if menu == nil {
win.DestroyMenu(hMenu)
}
}()
m := &Menu{
hMenu: hMenu,
window: window,
}
// For resolveMenu to work, we must set DwMenuData to m.
mi := win.MENUINFO{
FMask: win.MIM_MENUDATA,
DwMenuData: uintptr(unsafe.Pointer(m)),
}
mi.CbSize = uint32(unsafe.Sizeof(mi))
if !win.SetMenuInfo(hMenu, &mi) {
return nil, lastError("SetMenuInfo")
}
m.actions = newActionList(m)
menu = m
return menu, nil
}
func NewMenu() (menu *Menu, _ error) {
hMenu := win.CreatePopupMenu()
if hMenu == 0 {
return nil, lastError("CreatePopupMenu")
}
defer func() {
if menu == nil {
win.DestroyMenu(hMenu)
}
}()
mi := win.MENUINFO{FMask: win.MIM_STYLE}
mi.CbSize = uint32(unsafe.Sizeof(mi))
if !win.GetMenuInfo(hMenu, &mi) {
return nil, lastError("GetMenuInfo")
}
mi.DwStyle |= win.MNS_CHECKORBMP
mi.DwStyle &= ^uint32(win.MNS_NOCHECK)
m := &Menu{
hMenu: hMenu,
}
// For resolveMenu to work, we must set DwMenuData to m.
mi.FMask |= win.MIM_MENUDATA
mi.DwMenuData = uintptr(unsafe.Pointer(m))
if !win.SetMenuInfo(hMenu, &mi) {
return nil, lastError("SetMenuInfo")
}
m.actions = newActionList(m)
menu = m
return menu, nil
}
// resolveMenu resolves a Walk Menu from an hmenu.
func resolveMenu(hmenu win.HMENU) *Menu {
mi := win.MENUINFO{FMask: win.MIM_MENUDATA}
mi.CbSize = uint32(unsafe.Sizeof(mi))
if !win.GetMenuInfo(hmenu, &mi) {
return nil
}
return (*Menu)(unsafe.Pointer(mi.DwMenuData))
}
// InitPopup returns the event that is published when m is about to be displayed
// as a popup menu.
func (m *Menu) InitPopup() *Event {
return m.initPopupPublisher.Event()
}
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
}
// onInitPopup is invoked whenever m is about to be displayed as a popup menu.
// window specifies the parent Window for which the menu is to be shown.
func (m *Menu) onInitPopup(window Window) {
m.allowOwnerDrawInvalidation = true
defer func() {
m.allowOwnerDrawInvalidation = false
}()
m.initPopupPublisher.Publish()
m.perMenuMetrics.reset()
m.updateItemsForWindow(window)
}
func (m *Menu) Actions() *ActionList {
return m.actions
}
func (m *Menu) updateItemsForWindow(window Window) {
if m.window == nil {
m.window = window
defer func() {
m.window = nil
}()
}
var needAccelSpace bool
var numOwnerDraw int
var sm *menuSharedMetrics
for _, action := range m.actions.actions {
needAccelSpace = needAccelSpace || action.shortcut.Key != 0
switch {
case action.ownerDrawInfo != nil:
if numOwnerDraw == 0 {
// We've encountered the first owner-drawn item in the menu. Obtain
// shared metrics from the window theme.
sm = window.AsWindowBase().menuSharedMetrics()
}
action.ownerDrawInfo.sharedMetrics = sm
action.ownerDrawInfo.perMenuMetrics = &m.perMenuMetrics
numOwnerDraw++
fallthrough
case action.image != nil:
m.onActionChanged(action)
}
if action.menu != nil {
action.menu.updateItemsForWindow(window)
}
}
if numOwnerDraw > 0 && (needAccelSpace || numOwnerDraw < len(m.actions.actions)) {
// If we need accelerator space, then we need to measure each item's
// shortcut text.
// If we have any owner-drawn items, any remaining actions that are not
// owner-drawn must be set to owner-drawn via DefaultActionOwnerDrawHandler.
// Failure to do so would result in non-owner-drawn items being rendered
// without any theming whatsoever.
m.actions.forEach(func(a *Action) bool {
if needAccelSpace {
defer m.perMenuMetrics.measureAccelTextExtent(m.window, a)
}
if a.OwnerDraw() {
return true
}
a.ownerDrawInfo = newOwnerDrawnMenuItemInfo(a, DefaultActionOwnerDrawHandler)
a.ownerDrawInfo.sharedMetrics = sm
a.ownerDrawInfo.perMenuMetrics = &m.perMenuMetrics
m.onActionChanged(a)
return true
})
}
}
func (m *Menu) resolveDPI() int {
switch {
case m.getDPI != nil:
return m.getDPI()
case m.window != nil:
return m.window.DPI()
default:
return screenDPI()
}
}
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_DATA
mii.FType = 0
mii.DwItemData = 0
setString := true
switch {
case action.ownerDrawInfo != nil:
mii.FType |= win.MFT_OWNERDRAW
if m.allowOwnerDrawInvalidation {
// Terrible hack: owner-drawn items won't be asked to recompute their sizes
// without specifying win.MIIM_BITMAP with a zero HbmpItem!
mii.FMask |= win.MIIM_BITMAP
mii.HbmpItem = 0
}
// Setting DwItemData to the pointer to our ownerDrawInfo enables
// (*WindowBase).WndProc to quickly resolve the menu item being drawn.
mii.DwItemData = uintptr(unsafe.Pointer(action.ownerDrawInfo))
setString = false
case action.image != nil:
mii.FMask |= win.MIIM_BITMAP
dpi := m.resolveDPI()
if bmp, err := iconCache.Bitmap(action.image, dpi); err == nil {
mii.HbmpItem = bmp.hBmp
}
case action.IsSeparator():
mii.FType |= win.MFT_SEPARATOR
setString = false
default:
}
if setString {
mii.FMask |= win.MIIM_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.WID = uint32(action.id)
if action.Enabled() {
mii.FState &^= win.MFS_DISABLED
} else {
mii.FState |= win.MFS_DISABLED
}
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 {
defer m.ensureMenuBarRedrawn()
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.Checked() && action.Exclusive() {
first, last, index, err := m.actions.positionsForExclusiveCheck(action)
if err != nil {
return err
}
if !win.CheckMenuRadioItem(m.hMenu, uint32(first), uint32(last), uint32(index), win.MF_BYPOSITION) {
return newError("CheckMenuRadioItem failed")
}
if err := m.actions.uncheckActionsForExclusiveCheck(first, last, index); err != nil {
return err
}
}
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.menu == m {
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
}
// onMnemonic is called when m contains owner-drawn items and its parent Window
// receives a keypress. It enumerates all visible menu items, and if a match is
// found, it returns an action code telling Windows to execute the item specifed
// by positional index.
func (m *Menu) onMnemonic(key Key) (index, action uint16) {
m.actions.forEachVisible(func(a *Action) bool {
if odi := a.ownerDrawInfo; odi != nil {
if aKey := odi.mnemonic; aKey != 0 && aKey == key {
action = win.MNC_EXECUTE
return false
}
}
index++
return true
})
return index, action
}