forked from lxn/walk
/
button.go
147 lines (114 loc) · 2.65 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
// 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 (
"github.com/lxn/win"
)
type clickable interface {
raiseClicked()
}
type setCheckeder interface {
setChecked(checked bool)
}
type Button struct {
WidgetBase
checkedChangedPublisher EventPublisher
clickedPublisher EventPublisher
textChangedPublisher EventPublisher
image Image
}
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("Text", NewProperty(
func() interface{} {
return b.Text()
},
func(v interface{}) error {
return b.SetText(v.(string))
},
b.textChangedPublisher.Event()))
}
func (b *Button) Image() Image {
return b.image
}
func (b *Button) SetImage(image Image) error {
var typ uintptr
var handle uintptr
switch img := image.(type) {
case nil:
// zeroes are good
case *Bitmap:
typ = win.IMAGE_BITMAP
handle = uintptr(img.hBmp)
case *Icon:
typ = win.IMAGE_ICON
handle = uintptr(img.hIcon)
default:
return newError("image must be either *walk.Bitmap or *walk.Icon")
}
b.SendMessage(win.BM_SETIMAGE, typ, handle)
b.image = image
return b.updateParentLayout()
}
func (b *Button) Text() string {
return windowText(b.hWnd)
}
func (b *Button) SetText(value string) error {
if value == b.Text() {
return nil
}
if err := setWindowText(b.hWnd, value); err != nil {
return err
}
return b.updateParentLayout()
}
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) 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:
switch win.HIWORD(uint32(wParam)) {
case win.BN_CLICKED:
b.raiseClicked()
}
case win.WM_SETTEXT:
b.textChangedPublisher.Publish()
}
return b.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}