-
Notifications
You must be signed in to change notification settings - Fork 887
/
radiobutton.go
135 lines (106 loc) · 2.64 KB
/
radiobutton.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
// 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 RadioButtonGroup struct {
buttons []*RadioButton
checkedButton *RadioButton
}
func (rbg *RadioButtonGroup) Buttons() []*RadioButton {
buttons := make([]*RadioButton, len(rbg.buttons))
copy(buttons, rbg.buttons)
return buttons
}
func (rbg *RadioButtonGroup) CheckedButton() *RadioButton {
return rbg.checkedButton
}
type radioButtonish interface {
radioButton() *RadioButton
}
type RadioButton struct {
Button
group *RadioButtonGroup
value interface{}
}
func NewRadioButton(parent Container) (*RadioButton, error) {
rb := new(RadioButton)
if count := parent.Children().Len(); count > 0 {
if prevRB, ok := parent.Children().At(count - 1).(radioButtonish); ok {
rb.group = prevRB.radioButton().group
}
}
var groupBit uint32
if rb.group == nil {
groupBit = win.WS_GROUP
rb.group = new(RadioButtonGroup)
}
if err := InitWidget(
rb,
parent,
"BUTTON",
groupBit|win.WS_TABSTOP|win.WS_VISIBLE|win.BS_AUTORADIOBUTTON,
0); err != nil {
return nil, err
}
rb.Button.init()
rb.SetBackground(nullBrushSingleton)
rb.GraphicsEffects().Add(InteractionEffect)
rb.GraphicsEffects().Add(FocusEffect)
rb.MustRegisterProperty("CheckedValue", NewProperty(
func() interface{} {
if rb.Checked() {
return rb.value
}
return nil
},
func(v interface{}) error {
checked := v == rb.value
if checked {
rb.group.checkedButton = rb
}
rb.SetChecked(checked)
return nil
},
rb.CheckedChanged()))
rb.group.buttons = append(rb.group.buttons, rb)
return rb, nil
}
func (rb *RadioButton) radioButton() *RadioButton {
return rb
}
func (rb *RadioButton) TextOnLeftSide() bool {
return rb.hasStyleBits(win.BS_LEFTTEXT)
}
func (rb *RadioButton) SetTextOnLeftSide(textLeft bool) error {
return rb.ensureStyleBits(win.BS_LEFTTEXT, textLeft)
}
func (rb *RadioButton) Group() *RadioButtonGroup {
return rb.group
}
func (rb *RadioButton) Value() interface{} {
return rb.value
}
func (rb *RadioButton) SetValue(value interface{}) {
rb.value = value
}
func (rb *RadioButton) 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:
prevChecked := rb.group.checkedButton
rb.group.checkedButton = rb
if prevChecked != rb {
if prevChecked != nil {
prevChecked.setChecked(false)
}
rb.setChecked(true)
}
}
}
return rb.Button.WndProc(hwnd, msg, wParam, lParam)
}