-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbrowser.go
313 lines (254 loc) · 7.71 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package fltk
/*
#include <stdlib.h>
#include "browser.h"
*/
import "C"
import (
"errors"
"unsafe"
)
type Browser struct {
Group
icons map[int]Image
columnWidths []int
dataMap *browserDataMap
}
type browserDataMap struct {
dataMap map[uintptr]interface{}
id uintptr
}
func newBrowserDataMap() *browserDataMap {
return &browserDataMap{
dataMap: make(map[uintptr]interface{}),
}
}
func (m *browserDataMap) register(data interface{}) uintptr {
m.id++
m.dataMap[m.id] = data
return m.id
}
func (m *browserDataMap) unregister(id uintptr) {
delete(m.dataMap, id)
}
func (m *browserDataMap) data(id uintptr) interface{} {
if id == 0 {
return nil
}
if data, ok := m.dataMap[id]; ok {
return data
}
return nil
}
var (
ErrInvalidLine = errors.New("line doesn't exist")
)
func NewBrowser(x, y, w, h int, text ...string) *Browser {
b := &Browser{}
b.dataMap = newBrowserDataMap()
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))
id := b.dataMap.register(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 = newBrowserDataMap()
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)))
b.dataMap.unregister(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.data(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))
}
func (b *Browser) SetSelected(line int, val bool) bool {
if val {
return C.go_fltk_Browser_select((*C.Fl_Browser)(b.ptr()), C.int(line), 1) != 0
} else {
return C.go_fltk_Browser_select((*C.Fl_Browser)(b.ptr()), C.int(line), 0) != 0
}
}
func (b *Browser) IsSelected(line int) bool {
return C.go_fltk_Browser_selected((*C.Fl_Browser)(b.ptr()), C.int(line)) != 0
}
type SelectBrowser struct {
Browser
}
func NewSelectBrowser(x, y, w, h int, text ...string) *SelectBrowser {
b := &SelectBrowser{}
b.dataMap = newBrowserDataMap()
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 = newBrowserDataMap()
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 = newBrowserDataMap()
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
}
type CheckBrowser struct {
Group
dataMap *browserDataMap
}
func NewCheckBrowser(x, y, w, h int, text ...string) *CheckBrowser {
b := &CheckBrowser{}
b.dataMap = newBrowserDataMap()
initWidget(b, unsafe.Pointer(C.go_fltk_new_Check_Browser(C.int(x), C.int(y), C.int(w), C.int(h), cStringOpt(text))))
return b
}
func (b *CheckBrowser) Add(s string, checked bool) {
str := C.CString(s)
defer C.free(unsafe.Pointer(str))
if checked {
C.go_fltk_Check_Browser_add((*C.Fl_Check_Browser)(b.ptr()), str, 1)
} else {
C.go_fltk_Check_Browser_add((*C.Fl_Check_Browser)(b.ptr()), str, 0)
}
}
func (b *CheckBrowser) SetChecked(item int, checked bool) {
if checked {
C.go_fltk_Check_Browser_set_checked((*C.Fl_Check_Browser)(b.ptr()), C.int(item), 1)
} else {
C.go_fltk_Check_Browser_set_checked((*C.Fl_Check_Browser)(b.ptr()), C.int(item), 0)
}
}
func (b *CheckBrowser) IsChecked(item int) bool {
return C.go_fltk_Check_Browser_is_checked((*C.Fl_Check_Browser)(b.ptr()), C.int(item)) != 0
}
func (b *CheckBrowser) CheckedCount() int {
return int(C.go_fltk_Check_Browser_nchecked((*C.Fl_Check_Browser)(b.ptr())))
}
func (b *CheckBrowser) Remove(item int) {
C.go_fltk_Check_Browser_remove((*C.Fl_Check_Browser)(b.ptr()), C.int(item))
}
func (b *CheckBrowser) Clear() {
C.go_fltk_Check_Browser_clear((*C.Fl_Check_Browser)(b.ptr()))
}
func (b *CheckBrowser) ItemCount() int {
return int(C.go_fltk_Check_Browser_nitems((*C.Fl_Check_Browser)(b.ptr())))
}
func (b *CheckBrowser) Text(item int) string {
return C.GoString(C.go_fltk_Check_Browser_text((*C.Fl_Check_Browser)(b.ptr()), C.int(item)))
}