-
Notifications
You must be signed in to change notification settings - Fork 886
/
button.go
256 lines (196 loc) · 4.46 KB
/
button.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
// 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"
"unsafe"
"github.com/lxn/win"
)
type clickable interface {
raiseClicked()
}
type setCheckeder interface {
setChecked(checked bool)
}
type Button struct {
WidgetBase
checkedChangedPublisher EventPublisher
clickedPublisher EventPublisher
textChangedPublisher EventPublisher
imageChangedPublisher EventPublisher
image Image
persistent bool
}
func (b *Button) init() {
b.MustRegisterProperty("Checked", NewBoolProperty(
func() bool {
return b.Checked()
},
func(v bool) error {
b.SetChecked(v)
return nil
},
b.CheckedChanged()))
b.MustRegisterProperty("Image", NewProperty(
func() interface{} {
return b.Image()
},
func(v interface{}) error {
img, err := ImageFrom(v)
if err != nil {
return err
}
b.SetImage(img)
return nil
},
b.imageChangedPublisher.Event()))
b.MustRegisterProperty("Text", NewProperty(
func() interface{} {
return b.Text()
},
func(v interface{}) error {
return b.SetText(assertStringOr(v, ""))
},
b.textChangedPublisher.Event()))
}
func (b *Button) ApplyDPI(dpi int) {
b.WidgetBase.ApplyDPI(dpi)
b.SetImage(b.image)
}
func (b *Button) Image() Image {
return b.image
}
func (b *Button) SetImage(image Image) error {
var typ, handle uintptr
switch img := image.(type) {
case nil:
case *Bitmap:
typ = win.IMAGE_BITMAP
handle = uintptr(img.hBmp)
case *Icon:
typ = win.IMAGE_ICON
handle = uintptr(img.handleForDPI(b.DPI()))
default:
bmp, err := iconCache.Bitmap(image, b.DPI())
if err != nil {
return err
}
typ = win.IMAGE_BITMAP
handle = uintptr(bmp.hBmp)
}
b.SendMessage(win.BM_SETIMAGE, typ, handle)
b.image = image
b.RequestLayout()
b.imageChangedPublisher.Publish()
return nil
}
func (b *Button) ImageChanged() *Event {
return b.imageChangedPublisher.Event()
}
func (b *Button) Text() string {
return b.text()
}
func (b *Button) SetText(value string) error {
if value == b.Text() {
return nil
}
if err := b.setText(value); err != nil {
return err
}
b.RequestLayout()
return nil
}
func (b *Button) Checked() bool {
return b.SendMessage(win.BM_GETCHECK, 0, 0) == win.BST_CHECKED
}
func (b *Button) SetChecked(checked bool) {
if checked == b.Checked() {
return
}
b.window.(setCheckeder).setChecked(checked)
}
func (b *Button) setChecked(checked bool) {
var chk uintptr
if checked {
chk = win.BST_CHECKED
} else {
chk = win.BST_UNCHECKED
}
b.SendMessage(win.BM_SETCHECK, chk, 0)
b.checkedChangedPublisher.Publish()
}
func (b *Button) CheckedChanged() *Event {
return b.checkedChangedPublisher.Event()
}
func (b *Button) Persistent() bool {
return b.persistent
}
func (b *Button) SetPersistent(value bool) {
b.persistent = value
}
func (b *Button) SaveState() error {
return b.WriteState(fmt.Sprintf("%t", b.Checked()))
}
func (b *Button) RestoreState() error {
s, err := b.ReadState()
if err != nil {
return err
}
b.SetChecked(s == "true")
return nil
}
func (b *Button) Clicked() *Event {
return b.clickedPublisher.Event()
}
func (b *Button) raiseClicked() {
b.clickedPublisher.Publish()
}
func (b *Button) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_COMMAND:
hiWP := win.HIWORD(uint32(wParam))
if hiWP == 0 && lParam == 0 {
if a, ok := actionsById[win.LOWORD(uint32(wParam))]; ok {
a.raiseTriggered()
}
} else {
switch hiWP {
case win.BN_CLICKED:
b.raiseClicked()
}
}
case win.WM_SETTEXT:
b.textChangedPublisher.Publish()
}
return b.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}
// idealSize returns ideal button size in native pixels.
func (b *Button) idealSize() Size {
min := b.dialogBaseUnitsToPixels(Size{50, 14})
if b.Text() == "" {
return min
}
var s win.SIZE
b.SendMessage(win.BCM_GETIDEALSIZE, 0, uintptr(unsafe.Pointer(&s)))
return maxSize(sizeFromSIZE(s), min)
}
func (b *Button) CreateLayoutItem(ctx *LayoutContext) LayoutItem {
return &buttonLayoutItem{
idealSize: b.idealSize(),
}
}
type buttonLayoutItem struct {
LayoutItemBase
idealSize Size // in native pixels
}
func (li *buttonLayoutItem) LayoutFlags() LayoutFlags {
return 0
}
func (li *buttonLayoutItem) IdealSize() Size {
return li.MinSize()
}
func (li *buttonLayoutItem) MinSize() Size {
return li.idealSize
}