forked from pwiecz/go-fltk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.go
223 lines (177 loc) · 5.34 KB
/
browser.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package fltk
/*
#include <stdlib.h>
#include "browser.h"
*/
import "C"
import (
"errors"
"unsafe"
)
type Browser struct {
Group
icons map[int]Image
columnWidths []int
dataMap map[uintptr]interface{}
lastDataID uintptr
}
var (
ErrInvalidLine = errors.New("line doesn't exist")
)
func NewBrowser(x, y, w, h int, text ...string) *Browser {
b := &Browser{}
b.dataMap = make(map[uintptr]interface{})
b.icons = make(map[int]Image)
initWidget(b, unsafe.Pointer(C.go_fltk_new_Browser(C.int(x), C.int(y), C.int(w), C.int(h), cStringOpt(text))))
return b
}
func (b *Browser) Add(str string) {
cStr := C.CString(str)
defer C.free(unsafe.Pointer(cStr))
C.go_fltk_Browser_add((*C.Fl_Browser)(b.ptr()), cStr, C.uintptr_t(0))
}
func (b *Browser) AddWithData(str string, data interface{}) {
cStr := C.CString(str)
defer C.free(unsafe.Pointer(cStr))
b.lastDataID++
id := b.lastDataID
b.dataMap[id] = data
C.go_fltk_Browser_add((*C.Fl_Browser)(b.ptr()), cStr, C.uintptr_t(id))
}
func (b *Browser) TopLine() int {
return int(C.go_fltk_Browser_topline((*C.Fl_Browser)(b.ptr())))
}
func (b *Browser) SetBottomLine(line int) error {
if line < 1 || line > b.Size() {
return ErrInvalidLine
}
C.go_fltk_Browser_set_bottomline((*C.Fl_Browser)(b.ptr()), C.int(line))
return nil
}
func (b *Browser) SetMiddleLine(line int) error {
if line < 1 || line > b.Size() {
return ErrInvalidLine
}
C.go_fltk_Browser_set_middleline((*C.Fl_Browser)(b.ptr()), C.int(line))
return nil
}
func (b *Browser) SetTopLine(line int) error {
if line < 1 || line > b.Size() {
return ErrInvalidLine
}
C.go_fltk_Browser_set_topline((*C.Fl_Browser)(b.ptr()), C.int(line))
return nil
}
func (b *Browser) Clear() {
for k := range b.icons {
delete(b.icons, k)
}
b.dataMap = make(map[uintptr]interface{})
C.go_fltk_Browser_clear((*C.Fl_Browser)(b.ptr()))
}
func (b *Browser) Remove(line int) error {
if line < 1 || line > b.Size() {
return ErrInvalidLine
}
delete(b.icons, line)
// TODO: got the id from C++ is expensive, need a better way to delete go reference
id := uintptr(C.go_fltk_Browser_data((*C.Fl_Browser)(b.ptr()), C.int(line)))
delete(b.dataMap, id)
C.go_fltk_Browser_remove((*C.Fl_Browser)(b.ptr()), C.int(line))
return nil
}
func (b *Browser) ColumnChar() rune {
return rune(C.go_fltk_Browser_column_char((*C.Fl_Browser)(b.ptr())))
}
func (b *Browser) SetColumnChar(r rune) {
cStr := C.CString(string(r))
defer C.free(unsafe.Pointer(cStr))
C.go_fltk_Browser_set_column_char((*C.Fl_Browser)(b.ptr()), *cStr)
}
func (b *Browser) HideLine(line int) error {
if line < 1 || line > b.Size() {
return ErrInvalidLine
}
C.go_fltk_Browser_hide_line((*C.Fl_Browser)(b.ptr()), C.int(line))
return nil
}
func (b *Browser) Size() int {
return int(C.go_fltk_Browser_size((*C.Fl_Browser)(b.ptr())))
}
func (b *Browser) Icon(line int) Image {
return b.icons[line]
}
func (b *Browser) SetIcon(line int, i Image) {
if i == nil {
delete(b.icons, line)
C.go_fltk_Browser_set_icon((*C.Fl_Browser)(b.ptr()), C.int(line), nil)
return
}
b.icons[line] = i
C.go_fltk_Browser_set_icon((*C.Fl_Browser)(b.ptr()), C.int(line), b.icons[line].getImage().ptr())
}
func (b *Browser) FormatChar() rune {
return rune(C.go_fltk_Browser_format_char((*C.Fl_Browser)(b.ptr())))
}
func (b *Browser) SetFormatChar(r rune) {
cStr := C.CString(string(r))
defer C.free(unsafe.Pointer(cStr))
C.go_fltk_Browser_set_format_char((*C.Fl_Browser)(b.ptr()), *cStr)
}
func (b *Browser) Displayed(line int) bool {
return C.go_fltk_Browser_displayed((*C.Fl_Browser)(b.ptr()), C.int(line)) == 1
}
func (b *Browser) Text(line int) string {
cStr := C.go_fltk_Browser_text((*C.Fl_Browser)(b.ptr()), C.int(line))
return C.GoString(cStr)
}
func (b *Browser) SetColumnWidths(widths ...int) {
cArr := make([]C.int, len(widths))
for i, v := range widths {
cArr[i] = C.int(v)
}
b.columnWidths = widths
C.go_fltk_Browser_set_column_widths((*C.Fl_Browser)(b.ptr()), (*C.int)(&cArr[0]))
}
// Store column widths in Go instead of calling from C++ as it's complex and expensive
// to convert between them
func (b *Browser) ColumnWidths() []int {
return b.columnWidths
}
func (b *Browser) Data(line int) interface{} {
id := uintptr(C.go_fltk_Browser_data((*C.Fl_Browser)(b.ptr()), C.int(line)))
return b.dataMap[id]
}
func (b *Browser) Value() int {
return int(C.go_fltk_Browser_value((*C.Fl_Browser)(b.ptr())))
}
func (b *Browser) SetValue(line int) {
C.go_fltk_Browser_set_value((*C.Fl_Browser)(b.ptr()), C.int(line))
}
type SelectBrowser struct {
Browser
}
func NewSelectBrowser(x, y, w, h int, text ...string) *SelectBrowser {
b := &SelectBrowser{}
b.dataMap = make(map[uintptr]interface{})
initWidget(b, unsafe.Pointer(C.go_fltk_new_Select_Browser(C.int(x), C.int(y), C.int(w), C.int(h), cStringOpt(text))))
return b
}
type HoldBrowser struct {
Browser
}
func NewHoldBrowser(x, y, w, h int, text ...string) *HoldBrowser {
b := &HoldBrowser{}
b.dataMap = make(map[uintptr]interface{})
initWidget(b, unsafe.Pointer(C.go_fltk_new_Hold_Browser(C.int(x), C.int(y), C.int(w), C.int(h), cStringOpt(text))))
return b
}
type MultiBrowser struct {
Browser
}
func NewMultiBrowser(x, y, w, h int, text ...string) *MultiBrowser {
b := &MultiBrowser{}
b.dataMap = make(map[uintptr]interface{})
initWidget(b, unsafe.Pointer(C.go_fltk_new_Multi_Browser(C.int(x), C.int(y), C.int(w), C.int(h), cStringOpt(text))))
return b
}