-
Notifications
You must be signed in to change notification settings - Fork 17
/
combobox.go
74 lines (59 loc) · 1.91 KB
/
combobox.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
package gocoa
// #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework Cocoa
// #import "combobox.h"
import "C"
type ComboBox struct {
comboBoxPtr C.ComboBoxPtr
comboBoxID int
callback func()
}
var comboBoxes []*ComboBox
func NewComboBox(x int, y int, width int, height int) *ComboBox {
comboBoxID := len(comboBoxes)
comboBoxPtr := C.ComboBox_New(C.int(comboBoxID), C.int(x), C.int(y), C.int(width), C.int(height))
comboBox := &ComboBox{
comboBoxPtr: comboBoxPtr,
comboBoxID: comboBoxID,
}
comboBoxes = append(comboBoxes, comboBox)
return comboBox
}
func (comboBox *ComboBox) AddItem(item string) {
C.ComboBox_AddItem(comboBox.comboBoxPtr, C.CString(item))
}
func (comboBox *ComboBox) SetEditable(editable bool) {
if editable {
C.ComboBox_SetEditable(comboBox.comboBoxPtr, C.int(1))
} else {
C.ComboBox_SetEditable(comboBox.comboBoxPtr, C.int(0))
}
}
func (comboBox *ComboBox) SelectedIndex() int {
return int(C.ComboBox_SelectedIndex(comboBox.comboBoxPtr))
}
func (comboBox *ComboBox) SelectedText() string {
return C.GoString(C.ComboBox_SelectedText(comboBox.comboBoxPtr))
}
func (comboBox *ComboBox) StringValue() string {
return C.GoString(C.ComboBox_StringValue(comboBox.comboBoxPtr))
}
func (comboBox *ComboBox) SetSelectedIndex(selectedIndex int) {
C.ComboBox_SetSelectedIndex(comboBox.comboBoxPtr, C.int(selectedIndex))
}
func (comboBox *ComboBox) SetSelectedText(selectedText string) {
C.ComboBox_SetSelectedText(comboBox.comboBoxPtr, C.CString(selectedText))
}
func (comboBox *ComboBox) SetStringValue(stringValue string) {
C.ComboBox_SetStringValue(comboBox.comboBoxPtr, C.CString(stringValue))
}
//export onSelectionDidChange
func onSelectionDidChange(id C.int) {
comboBoxID := int(id)
if comboBoxID < len(comboBoxes) && comboBoxes[comboBoxID].callback != nil {
comboBoxes[comboBoxID].callback()
}
}
func (comboBox *ComboBox) OnSelectionDidChange(fn func()) {
comboBox.callback = fn
}