-
Notifications
You must be signed in to change notification settings - Fork 50
/
listbox.go
472 lines (392 loc) · 9.97 KB
/
listbox.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package clui
import (
term "github.com/nsf/termbox-go"
"strings"
)
/*
ListBox is control to display a list of items and allow to user to select any of them.
Content is scrollable with arrow keys or by clicking up and bottom buttons
on the scroll(now content is scrollable with mouse dragging only on Windows).
ListBox calls onSelectItem item function after a user changes currently
selected item with mouse or using keyboard. Event structure has 2 fields filled:
Y - selected item number in list(-1 if nothing is selected),
Msg - text of the selected item.
*/
type ListBox struct {
BaseControl
// own listbox members
items []string
currSelection int
topLine int
buttonPos int
onSelectItem func(Event)
onKeyPress func(term.Key) bool
}
/*
CreateListBox creates a new frame.
view - is a View that manages the control
parent - is container that keeps the control. The same View can be a view and a parent at the same time.
width and height - are minimal size of the control.
scale - the way of scaling the control when the parent is resized. Use DoNotScale constant if the
control should keep its original size.
*/
func CreateListBox(parent Control, width, height int, scale int) *ListBox {
l := new(ListBox)
l.BaseControl = NewBaseControl()
if height == AutoSize {
height = 3
}
if width == AutoSize {
width = 5
}
l.SetSize(width, height)
l.SetConstraints(width, height)
l.currSelection = -1
l.items = make([]string, 0)
l.topLine = 0
l.parent = parent
l.buttonPos = -1
l.SetTabStop(true)
l.SetScale(scale)
l.onSelectItem = nil
if parent != nil {
parent.AddChild(l)
}
return l
}
func (l *ListBox) drawScroll() {
PushAttributes()
defer PopAttributes()
pos := ThumbPosition(l.currSelection, len(l.items), l.height)
l.buttonPos = pos
DrawScrollBar(l.x+l.width-1, l.y, 1, l.height, pos)
}
func (l *ListBox) drawItems() {
PushAttributes()
defer PopAttributes()
maxCurr := len(l.items) - 1
curr := l.topLine
dy := 0
maxDy := l.height - 1
maxWidth := l.width - 1
fg, bg := RealColor(l.fg, l.Style(), ColorEditText), RealColor(l.bg, l.Style(), ColorEditBack)
if l.Active() {
fg, bg = RealColor(l.fg, l.Style(), ColorEditActiveText), RealColor(l.bg, l.Style(), ColorEditActiveBack)
}
fgSel, bgSel := RealColor(l.fgActive, l.Style(), ColorSelectionText), RealColor(l.bgActive, l.Style(), ColorSelectionBack)
for curr <= maxCurr && dy <= maxDy {
f, b := fg, bg
if curr == l.currSelection {
f, b = fgSel, bgSel
}
SetTextColor(f)
SetBackColor(b)
FillRect(l.x, l.y+dy, l.width-1, 1, ' ')
str := SliceColorized(l.items[curr], 0, maxWidth)
DrawText(l.x, l.y+dy, str)
curr++
dy++
}
}
// Draw repaints the control on its View surface
func (l *ListBox) Draw() {
if l.hidden {
return
}
PushAttributes()
defer PopAttributes()
x, y := l.Pos()
w, h := l.Size()
fg, bg := RealColor(l.fg, l.Style(), ColorEditText), RealColor(l.bg, l.Style(), ColorEditBack)
if l.Active() {
fg, bg = RealColor(l.fg, l.Style(), ColorEditActiveText), RealColor(l.bg, l.Style(), ColorEditActiveBack)
}
SetTextColor(fg)
SetBackColor(bg)
FillRect(x, y, w, h, ' ')
l.drawItems()
l.drawScroll()
}
func (l *ListBox) home() {
if l.currSelection == 0 {
return
}
if len(l.items) > 0 {
l.currSelection = 0
}
l.topLine = 0
if l.onSelectItem != nil {
ev := Event{Y: l.currSelection, Msg: l.SelectedItemText()}
l.onSelectItem(ev)
}
}
func (l *ListBox) end() {
length := len(l.items)
if length == 0 || l.currSelection == length-1 {
return
}
l.currSelection = length - 1
if length > l.height {
l.topLine = length - l.height
}
if l.onSelectItem != nil {
ev := Event{Y: l.currSelection, Msg: l.SelectedItemText()}
l.onSelectItem(ev)
}
}
func (l *ListBox) moveUp(dy int) {
if l.topLine == 0 && l.currSelection == 0 {
return
}
if l.currSelection == -1 {
if len(l.items) != 0 {
l.currSelection = 0
}
return
}
if l.currSelection < dy {
l.currSelection = 0
} else {
l.currSelection -= dy
}
l.EnsureVisible()
if l.onSelectItem != nil {
ev := Event{Y: l.currSelection, Msg: l.SelectedItemText()}
l.onSelectItem(ev)
}
}
func (l *ListBox) moveDown(dy int) {
length := len(l.items)
if length == 0 || l.currSelection == length-1 {
return
}
if l.currSelection+dy >= length {
l.currSelection = length - 1
} else {
l.currSelection += dy
}
l.EnsureVisible()
if l.onSelectItem != nil {
ev := Event{Y: l.currSelection, Msg: l.SelectedItemText()}
l.onSelectItem(ev)
}
}
// EnsureVisible makes the currently selected item visible and scrolls the item list if it is required
func (l *ListBox) EnsureVisible() {
length := len(l.items)
if length <= l.height || l.currSelection == -1 {
return
}
diff := l.currSelection - l.topLine
if diff >= 0 && diff < l.height {
return
}
if diff < 0 {
l.topLine = l.currSelection
} else {
top := l.currSelection - l.height + 1
if length-top > l.height {
l.topLine = top
} else {
l.topLine = length - l.height
}
}
}
// Clear deletes all ListBox items
func (l *ListBox) Clear() {
l.items = make([]string, 0)
l.currSelection = -1
l.topLine = 0
}
func (l *ListBox) processMouseClick(ev Event) bool {
if ev.Key != term.MouseLeft {
return false
}
dx := ev.X - l.x
dy := ev.Y - l.y
if dx == l.width-1 {
if dy < 0 || dy >= l.height || len(l.items) < 2 {
return true
}
if dy == 0 {
l.moveUp(1)
return true
}
if dy == l.height-1 {
l.moveDown(1)
return true
}
l.buttonPos = dy
l.recalcPositionByScroll()
return true
}
if dx < 0 || dx >= l.width || dy < 0 || dy >= l.height {
return true
}
if dy >= len(l.items) {
return true
}
l.SelectItem(l.topLine + dy)
WindowManager().BeginUpdate()
onSelFunc := l.onSelectItem
WindowManager().EndUpdate()
if onSelFunc != nil {
ev := Event{Y: l.topLine + dy, Msg: l.SelectedItemText()}
onSelFunc(ev)
}
return true
}
func (l *ListBox) recalcPositionByScroll() {
newPos := ItemByThumbPosition(l.buttonPos, len(l.items), l.height)
if newPos < 1 {
return
}
l.currSelection = newPos
l.EnsureVisible()
}
/*
ProcessEvent processes all events come from the control parent. If a control
processes an event it should return true. If the method returns false it means
that the control do not want or cannot process the event and the caller sends
the event to the control parent
*/
func (l *ListBox) ProcessEvent(event Event) bool {
if !l.Active() || !l.Enabled() {
return false
}
switch event.Type {
case EventKey:
if l.onKeyPress != nil {
res := l.onKeyPress(event.Key)
if res {
return true
}
}
switch event.Key {
case term.KeyHome:
l.home()
return true
case term.KeyEnd:
l.end()
return true
case term.KeyArrowUp:
l.moveUp(1)
return true
case term.KeyArrowDown:
l.moveDown(1)
return true
case term.KeyPgdn:
l.moveDown(l.height)
return true
case term.KeyPgup:
l.moveUp(l.height)
return true
case term.KeyCtrlM:
if l.currSelection != -1 && l.onSelectItem != nil {
ev := Event{Y: l.currSelection, Msg: l.SelectedItemText()}
l.onSelectItem(ev)
}
default:
return false
}
case EventMouse:
return l.processMouseClick(event)
}
return false
}
// own methods
// AddItem adds a new item to item list.
// Returns true if the operation is successful
func (l *ListBox) AddItem(item string) bool {
l.items = append(l.items, item)
return true
}
// SelectItem selects item which number in the list equals
// id. If the item exists the ListBox scrolls the list to
// make the item visible.
// Returns true if the item is selected successfully
func (l *ListBox) SelectItem(id int) bool {
if len(l.items) <= id || id < 0 {
return false
}
l.currSelection = id
l.EnsureVisible()
return true
}
// Item returns item text by its index.
// If index is out of range an empty string and false are returned
func (l *ListBox) Item(id int) (string, bool) {
if len(l.items) <= id || id < 0 {
return "", false
}
return l.items[id], true
}
// FindItem looks for an item in list which text equals
// to text, by default the search is casesensitive.
// Returns item number in item list or -1 if nothing is found.
func (l *ListBox) FindItem(text string, caseSensitive bool) int {
for idx, itm := range l.items {
if itm == text || (caseSensitive && strings.EqualFold(itm, text)) {
return idx
}
}
return -1
}
// PartialFindItem looks for an item in list which text starts from
// the given substring, by default the search is casesensitive.
// Returns item number in item list or -1 if nothing is found.
func (l *ListBox) PartialFindItem(text string, caseSensitive bool) int {
if !caseSensitive {
text = strings.ToLower(text)
}
for idx, itm := range l.items {
if caseSensitive {
if strings.HasPrefix(itm, text) {
return idx
}
} else {
low := strings.ToLower(itm)
if strings.HasPrefix(low, text) {
return idx
}
}
}
return -1
}
// SelectedItem returns currently selected item id
func (l *ListBox) SelectedItem() int {
return l.currSelection
}
// SelectedItemText returns text of currently selected item or empty sting if nothing is
// selected or ListBox is empty.
func (l *ListBox) SelectedItemText() string {
if l.currSelection == -1 {
return ""
}
return l.items[l.currSelection]
}
// RemoveItem deletes an item which number is id in item list
// Returns true if item is deleted
func (l *ListBox) RemoveItem(id int) bool {
if id < 0 || id >= len(l.items) {
return false
}
l.items = append(l.items[:id], l.items[id+1:]...)
return true
}
// OnSelectItem sets a callback that is called every time
// the selected item is changed
func (l *ListBox) OnSelectItem(fn func(Event)) {
l.onSelectItem = fn
}
// OnKeyPress sets the callback that is called when a user presses a Key while
// the controls is active. If a handler processes the key it should return
// true. If handler returns false it means that the default handler will
// process the key
func (l *ListBox) OnKeyPress(fn func(term.Key) bool) {
l.onKeyPress = fn
}
// ItemCount returns the number of items in the ListBox
func (l *ListBox) ItemCount() int {
return len(l.items)
}