-
Notifications
You must be signed in to change notification settings - Fork 1
/
selectinput_linux.go
144 lines (120 loc) · 3.18 KB
/
selectinput_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
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
136
137
138
139
140
141
142
143
144
package goey
import (
"unsafe"
"bitbucket.org/rj/goey/base"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
type selectinputElement struct {
Control
onChange func(int)
shChange glib.SignalHandle
onFocus focusSlot
onBlur blurSlot
}
func (w *SelectInput) mount(parent base.Control) (base.Element, error) {
control, err := gtk.ComboBoxTextNew()
if err != nil {
return nil, err
}
parent.Handle.Add(control)
for _, v := range w.Items {
control.AppendText(v)
}
if !w.Unset {
control.SetActive(w.Value)
}
control.SetCanFocus(true)
control.SetSensitive(!w.Disabled)
retval := &selectinputElement{
Control: Control{&control.Widget},
onChange: w.OnChange,
}
control.Connect("destroy", selectinputOnDestroy, retval)
retval.shChange = setSignalHandler(&control.Widget, 0, w.OnChange != nil, "changed", selectinputOnChanged, retval)
if child, err := control.GetChild(); err == nil {
child.SetCanFocus(true)
retval.onFocus.Set(child, w.OnFocus)
retval.onBlur.Set(child, w.OnBlur)
}
control.Show()
return retval, nil
}
func selectinputOnChanged(widget *gtk.ComboBoxText, mounted *selectinputElement) {
if mounted.onChange == nil {
return
}
mounted.onChange(widget.GetActive())
}
func selectinputOnDestroy(widget *gtk.ComboBoxText, mounted *selectinputElement) {
mounted.handle = nil
}
func (w *selectinputElement) comboboxtext() *gtk.ComboBoxText {
return (*gtk.ComboBoxText)(unsafe.Pointer(w.handle))
}
func (w *selectinputElement) Props() base.Widget {
value := w.comboboxtext().GetActive()
unset := value < 0
if unset {
value = 0
}
return &SelectInput{
Items: w.propsItems(),
Value: int(value),
Unset: unset,
Disabled: !w.comboboxtext().GetSensitive(),
OnChange: w.onChange,
OnFocus: w.onFocus.callback,
OnBlur: w.onBlur.callback,
}
}
func (w *selectinputElement) propsItems() []string {
// Get the model for the combobox, which contains the list of items.
model, err := w.comboboxtext().GetModel()
if err != nil {
return nil
}
// Iterate through the list. The model can in principle hold a tree, but
// that won't occur within the combobox.
items := []string{}
for iter, ok := model.GetIterFirst(); ok; ok = model.IterNext(iter) {
v, err := model.GetValue(iter, 0)
if err != nil {
return nil
}
vs, err := v.GetString()
if err != nil {
return nil
}
items = append(items, vs)
}
return items
}
func (w *selectinputElement) TakeFocus() bool {
widget, err := w.comboboxtext().GetChild()
if err != nil {
return false
}
control := Control{widget}
return control.TakeFocus()
}
func (w *selectinputElement) updateProps(data *SelectInput) error {
cbt := w.comboboxtext()
w.onChange = nil // temporarily break OnChange to prevent event
// Todo, can we avoid rebuilding the list?
cbt.RemoveAll()
for _, v := range data.Items {
cbt.AppendText(v)
}
if !data.Unset {
cbt.SetActive(data.Value)
}
cbt.SetSensitive(!data.Disabled)
w.onChange = data.OnChange
w.shChange = setSignalHandler(&cbt.Widget, w.shChange, data.OnChange != nil, "changed", selectinputOnChanged, w)
if child, err := cbt.GetChild(); err == nil {
w.onFocus.Set(child, data.OnFocus)
w.onBlur.Set(child, data.OnBlur)
}
return nil
}