-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkbox_linux.go
99 lines (79 loc) · 2.33 KB
/
checkbox_linux.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
package goey
import (
"unsafe"
"bitbucket.org/rj/goey/base"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
type checkboxElement struct {
Control
onChange func(bool)
shClick glib.SignalHandle
onFocus focusSlot
onBlur blurSlot
}
func (w *Checkbox) mount(parent base.Control) (base.Element, error) {
// Create the control
control, err := gtk.CheckButtonNewWithLabel(w.Text)
if err != nil {
return nil, err
}
parent.Handle.Add(control)
// Update properties on the control
control.SetActive(w.Value)
control.SetSensitive(!w.Disabled)
control.Show()
// Create the element
retval := &checkboxElement{
Control: Control{&control.Widget},
onChange: w.OnChange,
}
// Connect all callbacks for the events
control.Connect("destroy", checkboxOnDestroy, retval)
retval.shClick = setSignalHandler(&control.Widget, 0, w.OnChange != nil, "clicked", checkboxOnClick, retval)
retval.onFocus.Set(&control.Widget, w.OnFocus)
retval.onBlur.Set(&control.Widget, w.OnBlur)
return retval, nil
}
func checkboxOnClick(widget *gtk.CheckButton, mounted *checkboxElement) {
if mounted.onChange == nil {
return
}
mounted.onChange(widget.GetActive())
}
func checkboxOnDestroy(widget *gtk.CheckButton, mounted *checkboxElement) {
mounted.handle = nil
}
func (w *checkboxElement) checkbutton() *gtk.CheckButton {
return (*gtk.CheckButton)(unsafe.Pointer(w.handle))
}
func (w *checkboxElement) Click() {
w.checkbutton().Clicked()
}
func (w *checkboxElement) Props() base.Widget {
checkbutton := w.checkbutton()
text, err := checkbutton.GetLabel()
if err != nil {
panic("Could not get label: " + err.Error())
}
return &Checkbox{
Value: checkbutton.GetActive(),
Text: text,
Disabled: !checkbutton.GetSensitive(),
OnChange: w.onChange,
OnFocus: w.onFocus.callback,
OnBlur: w.onBlur.callback,
}
}
func (w *checkboxElement) updateProps(data *Checkbox) error {
checkbutton := w.checkbutton()
w.onChange = nil // temporarily break OnChange to prevent event
checkbutton.SetLabel(data.Text)
checkbutton.SetActive(data.Value)
checkbutton.SetSensitive(!data.Disabled)
w.onChange = data.OnChange
w.shClick = setSignalHandler(&checkbutton.Widget, w.shClick, data.OnChange != nil, "clicked", checkboxOnClick, w)
w.onFocus.Set(&checkbutton.Widget, data.OnFocus)
w.onBlur.Set(&checkbutton.Widget, data.OnBlur)
return nil
}