-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolbar.go
337 lines (255 loc) · 6.97 KB
/
toolbar.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
// 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.
package walk
import (
"syscall"
"unsafe"
)
import . "github.com/lxn/go-winapi"
var toolBarOrigWndProcPtr uintptr
var _ subclassedWidget = &ToolBar{}
type ToolBar struct {
WidgetBase
imageList *ImageList
actions *ActionList
defaultButtonWidth int
maxTextRows int
}
func newToolBar(parent Container, style uint32) (*ToolBar, error) {
tb := &ToolBar{}
tb.actions = newActionList(tb)
if err := initChildWidget(
tb,
parent,
"ToolbarWindow32",
CCS_NODIVIDER|style,
0); err != nil {
return nil, err
}
return tb, nil
}
func NewToolBar(parent Container) (*ToolBar, error) {
return newToolBar(parent, TBSTYLE_WRAPABLE)
}
func NewVerticalToolBar(parent Container) (*ToolBar, error) {
tb, err := newToolBar(parent, CCS_VERT|CCS_NORESIZE)
if err != nil {
return nil, err
}
tb.defaultButtonWidth = 100
return tb, nil
}
func (*ToolBar) origWndProcPtr() uintptr {
return toolBarOrigWndProcPtr
}
func (*ToolBar) setOrigWndProcPtr(ptr uintptr) {
toolBarOrigWndProcPtr = ptr
}
func (tb *ToolBar) LayoutFlags() LayoutFlags {
style := GetWindowLong(tb.hWnd, GWL_STYLE)
if style&CCS_VERT > 0 {
return ShrinkableVert | GrowableVert | GreedyVert
}
// FIXME: Since reimplementation of BoxLayout we must return 0 here,
// otherwise the ToolBar contained in MainWindow will eat half the space.
return 0 //ShrinkableHorz | GrowableHorz
}
func (tb *ToolBar) MinSizeHint() Size {
return tb.SizeHint()
}
func (tb *ToolBar) SizeHint() Size {
if tb.actions.Len() == 0 {
return Size{}
}
size := uint32(SendMessage(tb.hWnd, TB_GETBUTTONSIZE, 0, 0))
width := tb.defaultButtonWidth
if width == 0 {
width = int(LOWORD(size))
}
height := int(HIWORD(size))
return Size{width, height}
}
func (tb *ToolBar) applyDefaultButtonWidth() error {
if tb.defaultButtonWidth == 0 {
return nil
}
lParam := uintptr(
MAKELONG(uint16(tb.defaultButtonWidth), uint16(tb.defaultButtonWidth)))
if 0 == SendMessage(tb.hWnd, TB_SETBUTTONWIDTH, 0, lParam) {
return newError("SendMessage(TB_SETBUTTONWIDTH)")
}
size := uint32(SendMessage(tb.hWnd, TB_GETBUTTONSIZE, 0, 0))
height := HIWORD(size)
lParam = uintptr(MAKELONG(uint16(tb.defaultButtonWidth), height))
if FALSE == SendMessage(tb.hWnd, TB_SETBUTTONSIZE, 0, lParam) {
return newError("SendMessage(TB_SETBUTTONSIZE)")
}
return nil
}
// DefaultButtonWidth returns the default button width of the ToolBar.
//
// The default value for a horizontal ToolBar is 0, resulting in automatic
// sizing behavior. For a vertical ToolBar, the default is 100 pixels.
func (tb *ToolBar) DefaultButtonWidth() int {
return tb.defaultButtonWidth
}
// SetDefaultButtonWidth sets the default button width of the ToolBar.
//
// Calling this method affects all buttons in the ToolBar, no matter if they are
// added before or after the call. A width of 0 results in automatic sizing
// behavior. Negative values are not allowed.
func (tb *ToolBar) SetDefaultButtonWidth(width int) error {
if width == tb.defaultButtonWidth {
return nil
}
if width < 0 {
return newError("width must be >= 0")
}
old := tb.defaultButtonWidth
tb.defaultButtonWidth = width
for _, action := range tb.actions.actions {
if err := tb.onActionChanged(action); err != nil {
tb.defaultButtonWidth = old
return err
}
}
return tb.applyDefaultButtonWidth()
}
func (tb *ToolBar) MaxTextRows() int {
return tb.maxTextRows
}
func (tb *ToolBar) SetMaxTextRows(maxTextRows int) error {
if 0 == SendMessage(tb.hWnd, TB_SETMAXTEXTROWS, uintptr(maxTextRows), 0) {
return newError("SendMessage(TB_SETMAXTEXTROWS)")
}
tb.maxTextRows = maxTextRows
return nil
}
func (tb *ToolBar) Actions() *ActionList {
return tb.actions
}
func (tb *ToolBar) ImageList() *ImageList {
return tb.imageList
}
func (tb *ToolBar) SetImageList(value *ImageList) {
var hIml HIMAGELIST
if value != nil {
hIml = value.hIml
}
SendMessage(tb.hWnd, TB_SETIMAGELIST, 0, uintptr(hIml))
tb.imageList = value
}
func (tb *ToolBar) imageIndex(image *Bitmap) (imageIndex int32, err error) {
imageIndex = -1
if image != nil {
// FIXME: Protect against duplicate insertion
if imageIndex, err = tb.imageList.AddMasked(image); err != nil {
return
}
}
return
}
func (tb *ToolBar) wndProc(hwnd HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case WM_NOTIFY:
nmm := (*NMMOUSE)(unsafe.Pointer(lParam))
switch int32(nmm.Hdr.Code) {
case NM_CLICK:
actionId := uint16(nmm.DwItemSpec)
if action := actionsById[actionId]; action != nil {
action.raiseTriggered()
}
}
}
return tb.WidgetBase.wndProc(hwnd, msg, wParam, lParam)
}
func (tb *ToolBar) initButtonForAction(action *Action, state, style *byte, image *int32, text *uintptr) (err error) {
if tb.hasStyleBits(CCS_VERT) {
*state |= TBSTATE_WRAP
} else if tb.defaultButtonWidth == 0 {
*style |= BTNS_AUTOSIZE
}
if action.checked {
*state |= TBSTATE_CHECKED
}
if action.enabled {
*state |= TBSTATE_ENABLED
}
if action.checkable {
*style |= BTNS_CHECK
}
if action.exclusive {
*style |= BTNS_GROUP
}
if *image, err = tb.imageIndex(action.image); err != nil {
return
}
*text = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(action.Text())))
return
}
func (tb *ToolBar) onActionChanged(action *Action) error {
tbbi := TBBUTTONINFO{
DwMask: TBIF_IMAGE | TBIF_STATE | TBIF_STYLE | TBIF_TEXT,
}
tbbi.CbSize = uint32(unsafe.Sizeof(tbbi))
if err := tb.initButtonForAction(
action,
&tbbi.FsState,
&tbbi.FsStyle,
&tbbi.IImage,
&tbbi.PszText); err != nil {
return err
}
if 0 == SendMessage(
tb.hWnd,
TB_SETBUTTONINFO,
uintptr(action.id),
uintptr(unsafe.Pointer(&tbbi))) {
return newError("SendMessage(TB_SETBUTTONINFO) failed")
}
return nil
}
func (tb *ToolBar) onInsertingAction(index int, action *Action) error {
tbb := TBBUTTON{
IdCommand: int32(action.id),
}
if err := tb.initButtonForAction(
action,
&tbb.FsState,
&tbb.FsStyle,
&tbb.IBitmap,
&tbb.IString); err != nil {
return err
}
tb.SetVisible(true)
SendMessage(tb.hWnd, TB_BUTTONSTRUCTSIZE, uintptr(unsafe.Sizeof(tbb)), 0)
if FALSE == SendMessage(tb.hWnd, TB_ADDBUTTONS, 1, uintptr(unsafe.Pointer(&tbb))) {
return newError("SendMessage(TB_ADDBUTTONS)")
}
if err := tb.applyDefaultButtonWidth(); err != nil {
return err
}
SendMessage(tb.hWnd, TB_AUTOSIZE, 0, 0)
action.addChangedHandler(tb)
return nil
}
func (tb *ToolBar) removeAt(index int) error {
action := tb.actions.At(index)
action.removeChangedHandler(tb)
if 0 == SendMessage(tb.hWnd, TB_DELETEBUTTON, uintptr(index), 0) {
return newError("SendMessage(TB_DELETEBUTTON) failed")
}
return nil
}
func (tb *ToolBar) onRemovingAction(index int, action *Action) error {
return tb.removeAt(index)
}
func (tb *ToolBar) onClearingActions() error {
for i := tb.actions.Len() - 1; i >= 0; i-- {
if err := tb.removeAt(i); err != nil {
return err
}
}
return nil
}