-
Notifications
You must be signed in to change notification settings - Fork 23
/
checkbox.go
76 lines (61 loc) · 1.35 KB
/
checkbox.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
//+build windows
package wui
import "github.com/gonutz/w32/v2"
func NewCheckBox() *CheckBox {
return &CheckBox{}
}
type CheckBox struct {
textControl
checked bool
onChange func(bool)
}
var _ Control = (*CheckBox)(nil)
func (*CheckBox) canFocus() bool {
return true
}
func (c *CheckBox) OnTabFocus() func() {
return c.onTabFocus
}
func (c *CheckBox) SetOnTabFocus(f func()) {
c.onTabFocus = f
}
func (*CheckBox) eatsTabs() bool {
return false
}
func (c *CheckBox) create(id int) {
c.textControl.create(id, 0, "BUTTON", w32.WS_TABSTOP|w32.BS_AUTOCHECKBOX)
w32.SendMessage(c.handle, w32.BM_SETCHECK, toCheckState(c.checked), 0)
}
func (c *CheckBox) Checked() bool {
return c.checked
}
func (c *CheckBox) SetChecked(checked bool) {
if checked == c.checked {
return
}
c.checked = checked
if c.handle != 0 {
w32.SendMessage(c.handle, w32.BM_SETCHECK, toCheckState(c.checked), 0)
}
if c.onChange != nil {
c.onChange(c.checked)
}
return
}
func toCheckState(checked bool) uintptr {
if checked {
return w32.BST_CHECKED
}
return w32.BST_UNCHECKED
}
func (c *CheckBox) SetOnChange(f func(checked bool)) {
c.onChange = f
}
func (c *CheckBox) handleNotification(cmd uintptr) {
if cmd == w32.BN_CLICKED {
c.checked = w32.SendMessage(c.handle, w32.BM_GETCHECK, 0, 0) == w32.BST_CHECKED
if c.onChange != nil {
c.onChange(c.checked)
}
}
}