-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.go
110 lines (87 loc) · 2.01 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
// 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 (
"github.com/lxn/win"
)
type clickable interface {
raiseClicked()
}
type Button struct {
WidgetBase
checkedChangedPublisher EventPublisher
clickedPublisher EventPublisher
textChangedPublisher EventPublisher
}
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) 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.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)
}