forked from gonutz/wui
/
checkbox.go
49 lines (40 loc) · 885 Bytes
/
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
//+build windows
package wui
import "github.com/gonutz/w32"
func NewCheckbox() *Checkbox {
return &Checkbox{}
}
type Checkbox struct {
textControl
checked bool
onChange func(bool)
}
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
}