-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_listbox.v
626 lines (574 loc) · 14.7 KB
/
widget_listbox.v
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
module ui
import gx
type ListBoxSelectionChangedFn = fn (voidptr, &ListBox) // The second arg is ListBox
const (
_item_height = 20
_col_list_bkgrnd = gx.white
_col_item_select = gx.light_blue
_col_border = gx.gray
_text_offset_y = 3
_text_offset_x = 5
)
[heap]
pub struct ListBox {
pub mut:
height int
width int
x int
y int
offset_x int
offset_y int
z_index int
parent Layout = empty_stack
ui &UI = 0
items []ListItem = []ListItem{}
selection int = -1
draw_count int
on_change ListBoxSelectionChangedFn = ListBoxSelectionChangedFn(0)
is_focused bool
draw_lines bool
col_bkgrnd gx.Color = ui._col_list_bkgrnd
col_selected gx.Color = ui._col_item_select
col_border gx.Color = ui._col_border
item_height int = ui._item_height
text_offset_y int = ui._text_offset_y
id string
// text styles
text_styles TextStyles
text_size f64
text_cfg gx.TextCfg
hidden bool
// files droped
files_droped bool
// guess adjusted width
adj_width int
adj_height int
// component state for composable widget
component voidptr
// scrollview
has_scrollview bool
scrollview &ScrollView = 0
on_scroll_change ScrollViewChangedFn = ScrollViewChangedFn(0)
}
[heap]
struct ListItem {
id string
list &ListBox
mut:
x int
y int
text string
draw_text string
}
[params]
pub struct ListBoxParams {
mut:
x int
y int
width int
height int
z_index int
on_change ListBoxSelectionChangedFn = ListBoxSelectionChangedFn(0)
draw_lines bool // Draw a rectangle around every item?
col_border gx.Color = ui._col_border // Item and list border color
col_bkgrnd gx.Color = ui._col_list_bkgrnd // ListBox background color
col_selected gx.Color = ui._col_item_select // Selected item background color
item_height int = ui._item_height
text_offset_y int = ui._text_offset_y
id string // To use one callback for multiple ListBoxes
// related to text drawing
text_cfg gx.TextCfg
text_size f64
selection int = -1
scrollview bool = true
items map[string]string
// files droped
files_droped bool
}
// Keys of the items map are IDs of the elements, values are text
pub fn listbox(c ListBoxParams) &ListBox {
mut list := &ListBox{
x: c.x // if c.draw_lines { c.x } else { c.x - 1 }
y: c.y // if c.draw_lines { c.y } else { c.y - 1 }
width: c.width
height: c.height
z_index: c.z_index
selection: c.selection
on_change: c.on_change
draw_lines: c.draw_lines
col_bkgrnd: c.col_bkgrnd
col_selected: c.col_selected
col_border: c.col_border
item_height: c.item_height
text_offset_y: c.text_offset_y
text_cfg: c.text_cfg
text_size: c.text_size
files_droped: c.files_droped
id: c.id
ui: 0
}
for id, text in c.items {
// println(" append $id -> $text ")
list.append_item(id, text, 0)
}
if c.scrollview {
scrollview_add(mut list)
}
return list
}
fn (mut lb ListBox) init(parent Layout) {
lb.parent = parent
ui := parent.get_ui()
lb.ui = ui
lb.init_style()
lb.draw_count = lb.height / lb.item_height
lb.text_offset_y = (lb.item_height - text_height(lb, 'W')) / 2
if lb.text_offset_y < 0 {
lb.text_offset_y = 0
}
// update lb.width and lb.height to adjusted sizes when initialized to 0
lb.init_size()
lb.init_items()
if has_scrollview(lb) {
lb.scrollview.init(parent)
scrollview_update(lb)
}
mut subscriber := parent.get_subscriber()
subscriber.subscribe_method(events.on_click, on_change, lb)
subscriber.subscribe_method(events.on_key_up, on_key_up, lb)
lb.ui.window.evt_mngr.add_receiver(lb, [events.on_mouse_down, events.on_scroll])
// println("lb $lb.files_droped")
if lb.files_droped {
subscriber.subscribe_method(events.on_files_droped, on_files_droped, lb)
// lb.ui.window.evt_mngr.add_receiver(lb, [events.on_files_droped])
}
}
[manualfree]
fn (mut lb ListBox) cleanup() {
mut subscriber := lb.parent.get_subscriber()
subscriber.unsubscribe_method(events.on_click, lb)
subscriber.unsubscribe_method(events.on_key_up, lb)
lb.ui.window.evt_mngr.rm_receiver(lb, [events.on_mouse_down, events.on_scroll])
if lb.files_droped {
subscriber.unsubscribe_method(events.on_files_droped, lb)
// lb.ui.window.evt_mngr.rm_receiver(lb, [events.on_files_droped])
}
unsafe { lb.free() }
}
[unsafe]
pub fn (lb &ListBox) free() {
$if free ? {
print('listbox $lb.id')
}
unsafe {
lb.id.free()
for item in lb.items {
item.free()
}
lb.items.free()
free(lb)
}
$if free ? {
println(' -> freed')
}
}
[unsafe]
fn (item &ListItem) free() {
$if free ? {
print('\tlistbox item $item.id')
}
unsafe {
item.id.free()
item.text.free()
item.draw_text.free()
// Failing: free(item)
}
$if free ? {
println(' -> freed')
}
}
fn (mut lb ListBox) init_style() {
$if nodtw ? {
if is_empty_text_cfg(lb.text_cfg) {
lb.text_cfg = lb.ui.window.text_cfg
}
if lb.text_size > 0 {
_, win_height := lb.ui.window.size()
lb.text_cfg = gx.TextCfg{
...lb.text_cfg
size: text_size_as_int(lb.text_size, win_height)
}
}
} $else {
mut dtw := DrawTextWidget(lb)
dtw.init_style()
dtw.update_text_size(lb.text_size)
}
}
fn (mut lb ListBox) init_items() {
for i, mut item in lb.items {
// println("$i $item.text get ot draw-> ${lb.get_draw_to(item.text)}")
item.draw_text = item.text[0..lb.get_draw_to(item.text)]
item.x = 0
item.y = lb.item_height * i
// println("item init $i, $item.text, $item.x $item.y")
}
}
pub fn (mut lb ListBox) add_item(id string, text string) {
lb.append_item(id, text, lb.get_draw_to(text))
lb.update_adj_size()
}
fn (mut lb ListBox) get_draw_to(text string) int {
if lb.has_scrollview {
return 0
}
width := text_width(lb, text)
real_w := lb.width + ui._text_offset_x * 2
mut draw_to := text.len
if width >= real_w {
draw_to = int(f32(text.len) * (f32(real_w) / f32(width)))
for draw_to > 1 && text_width(lb, text[0..draw_to]) > real_w {
draw_to--
}
}
// println('width $width >= real_w $real_w draw_to: $draw_to, $text, ${text[0..draw_to]}')
return draw_to
}
pub fn (mut lb ListBox) append_item(id string, text string, draw_to int) {
lb.items << ListItem{
x: 0
y: lb.item_height * lb.items.len
id: id
text: text
list: unsafe { lb }
draw_text: text[0..draw_to]
}
}
pub fn (lb &ListBox) is_selected() bool {
if lb.selection < 0 || lb.selection >= lb.items.len {
return false
}
return true
}
pub fn (lb &ListBox) ids() []string {
mut res := []string{}
for _, item in lb.items {
res << item.id
}
return res
}
pub fn (lb &ListBox) values() []string {
mut res := []string{}
for _, item in lb.items {
res << item.text
}
return res
}
pub fn (lb &ListBox) indices() []int {
mut res := []int{}
for inx, _ in lb.items {
res << inx
}
return res
}
// Returns the ID and the text of the selected item
pub fn (lb &ListBox) selected() ?(string, string) {
if !lb.is_selected() {
return error('Nothing is selected')
}
return lb.items[lb.selection].id, lb.items[lb.selection].text
}
// Returns the index of the selected item
pub fn (lb &ListBox) selected_inx() ?int {
if !lb.is_selected() {
return error('Nothing is selected')
}
return lb.selection
}
pub fn (mut lb ListBox) set_text(id string, text string) {
for i in 0 .. lb.items.len {
if lb.items[i].id == id {
lb.items[i].text = text
lb.items[i].draw_text = text[0..lb.get_draw_to(text)]
break
}
}
}
pub fn (mut lb ListBox) remove_item(id string) {
for i in 0 .. lb.items.len {
if lb.items[i].id == id {
lb.remove_inx(i)
lb.update_adj_size()
break
}
}
}
pub fn (mut lb ListBox) remove_inx(i int) {
if i < 0 || i >= lb.items.len {
return
}
for j in (i + 1) .. lb.items.len {
lb.items[j].y -= lb.item_height
}
lb.items.delete(i)
}
[manualfree]
pub fn (mut lb ListBox) clear() {
for item in lb.items {
unsafe { item.free() }
}
lb.items.clear()
lb.selection = -1
}
fn (mut lb ListBox) draw_item(li ListItem, selected bool) {
col := if selected { lb.col_selected } else { lb.col_bkgrnd }
width := if lb.has_scrollview && lb.adj_width > lb.width { lb.adj_width } else { lb.width }
// println("linrssss draw ${li.draw_text} $li.x + $lb.x + $ui._text_offset_x, $li.y + $lb.y + $lb.text_offset_y, $lb.width, $lb.item_height")
lb.ui.gg.draw_rect_filled(li.x + lb.x + ui._text_offset_x, li.y + lb.y + lb.text_offset_y,
width - 2 * ui._text_offset_x, lb.item_height, col)
$if nodtw ? {
lb.ui.gg.draw_text_def(li.x + lb.x + ui._text_offset_x, li.y + lb.y + lb.text_offset_y,
if lb.has_scrollview { li.text } else { li.draw_text })
} $else {
DrawTextWidget(lb).draw_text(li.x + lb.x + ui._text_offset_x, li.y + lb.y + lb.text_offset_y,
if lb.has_scrollview { li.text } else { li.draw_text })
}
if lb.draw_lines {
// println("line item $li.x + $lb.x, $li.y + $lb.x, $lb.width, $lb.item_height")
lb.ui.gg.draw_rect_empty(li.x + lb.x + ui._text_offset_x, li.y + lb.y + lb.text_offset_y,
width, lb.item_height, lb.col_border)
}
}
fn (lb &ListBox) visible_items() (int, int) {
mut j1, mut j2 := 0, 0
if lb.has_scrollview {
j1 = lb.scrollview.offset_y / lb.item_height
if j1 < 0 {
j1 = 0
}
}
if lb.has_scrollview {
j2 = (lb.scrollview.offset_y + lb.height) / lb.item_height
} else {
j2 = lb.height / lb.item_height
}
jmax := lb.items.len - 1
if j2 > jmax {
j2 = jmax
}
return j1, j2
}
fn (mut lb ListBox) draw() {
offset_start(mut lb)
DrawTextWidget(lb).load_style()
// scrollview_clip(mut lb)
scrollview_draw_begin(mut lb)
height := if lb.has_scrollview && lb.adj_height > lb.height {
lb.adj_height + lb.text_offset_y
} else {
lb.height
}
// println("draw $lb.x, $lb.y, $lb.width $lb.height $height")
lb.ui.gg.draw_rect_filled(lb.x, lb.y, lb.width, height, lb.col_bkgrnd)
// println("draw rect")
from, to := lb.visible_items()
if lb.items.len == 0 {
$if nodtw ? {
draw_text_with_color(lb, lb.x + ui._text_offset_x, lb.y + lb.text_offset_y,
if lb.files_droped { 'Empty listbox. Drop files here ...' } else { '' },
gx.gray)
} $else {
DrawTextWidget(lb).draw_styled_text(lb.x + ui._text_offset_x, lb.y + lb.text_offset_y,
if lb.files_droped { 'Empty listbox. Drop files here ...' } else { '' },
color: gx.gray)
}
} else {
for inx, item in lb.items {
// println("$inx >= $lb.draw_count")
if inx >= lb.draw_count && !has_scrollview(lb) {
break
}
if !has_scrollview(lb) || (inx >= from && inx <= to) {
lb.draw_item(item, inx == lb.selection)
}
}
}
if !lb.draw_lines {
lb.ui.gg.draw_rect_empty(lb.x - 1, lb.y - 1, lb.width + 2, height + 2, lb.col_border)
}
// scrollview_draw(lb)
scrollview_draw_end(lb)
offset_end(mut lb)
}
fn (lb &ListBox) point_inside(x f64, y f64) bool {
// println("inside lb $x $y (${lb.x + lb.offset_x}, ${lb.y + lb.offset_y}, $lb.width, $lb.height)")
if lb.has_scrollview {
return lb.scrollview.point_inside(x, y, .view)
} else {
return point_inside(lb, x, y)
}
}
fn (li &ListItem) point_inside(x f64, y f64) bool {
lix, liy := li.x + li.list.x + li.list.offset_x, li.y + li.list.y + li.list.offset_y
return x >= lix && x <= lix + li.list.width && y >= liy && y <= liy + li.list.item_height
}
fn on_change(mut lb ListBox, e &MouseEvent, window &Window) {
// println("onclick $e.action ${int(e.action)}")
if lb.hidden {
return
}
if !lb.ui.window.is_top_widget(lb, events.on_mouse_down) {
return
}
if e.action != .up {
return
}
if !lb.point_inside(e.x, e.y) {
lb.unfocus()
return
}
lb.focus()
for inx, item in lb.items {
if !lb.has_scrollview && inx >= lb.draw_count {
break
}
// println(' $item.id -> ($e.x,$e.y)')
if item.point_inside(e.x, e.y) {
if lb.selection != inx {
lb.selection = inx
if lb.on_change != ListBoxSelectionChangedFn(0) {
lb.on_change(window.state, lb)
}
}
break
}
}
}
fn on_files_droped(mut lb ListBox, e &MouseEvent, window &Window) {
// println("on_files_droped")
if lb.hidden {
return
}
// println("on files: inside ${lb.point_inside(e.x, e.y)}")
if !lb.point_inside(e.x, e.y) {
return
}
// println("${lb.ui.window.point_inside_receivers(events.on_files_droped)}")
// if !lb.ui.window.is_top_widget(lb, events.on_files_droped) {
// return
// }
num_dropped := get_num_dropped_files()
for i in 0 .. num_dropped {
path := get_dropped_file_path(i)
lb.add_item(path, path.clone())
}
lb.ui.window.update_layout()
}
// Up and Down keys work on the list when it's is_focused
fn on_key_up(mut lb ListBox, e &KeyEvent, window &Window) {
if lb.hidden {
return
}
if !lb.is_focused {
return
}
match e.key {
.down {
if lb.selection >= lb.draw_count - 1 {
return
}
if lb.selection >= lb.items.len - 1 {
return
}
lb.selection++
}
.up {
if lb.selection <= 0 {
return
}
lb.selection--
}
else {
return
}
}
if lb.on_change != ListBoxSelectionChangedFn(0) {
lb.on_change(window.state, lb)
}
}
pub fn (mut lb ListBox) set_pos(x int, y int) {
if lb.x != x || lb.y != y {
// println("set pos lb: $x, $y")
lb.x = x
lb.y = y
}
}
fn (mut lb ListBox) set_visible(state bool) {
lb.hidden = !state
}
fn (mut lb ListBox) focus() {
mut f := Focusable(lb)
f.set_focus()
}
fn (mut lb ListBox) unfocus() {
lb.is_focused = false
}
// Needed for ScrollableWidget
fn (mut lb ListBox) adj_size() (int, int) {
if lb.adj_width == 0 {
mut width := 0
for item in lb.items {
width = text_width(lb, item.text) + ui._text_offset_x * 2
// println('$item.text -> $width')
if width > lb.adj_width {
lb.adj_width = width
}
}
// println("adj_width: $lb.adj_width")
}
if lb.adj_height == 0 {
lb.adj_height = lb.items.len * lb.item_height + 2 * lb.text_offset_y
}
// println("lb adj: ($lb.adj_width, $lb.adj_height) size: ($lb.width, $lb.height)")
return lb.adj_width, lb.adj_height
}
fn (mut lb ListBox) update_adj_size() {
mut width := 0
for item in lb.items {
width = text_width(lb, item.text) + ui._text_offset_x * 2
// println('$item.text -> $width')
if width > lb.adj_width {
lb.adj_width = width
}
}
lb.adj_height = lb.items.len * lb.item_height
scrollview_update(lb)
}
fn (mut lb ListBox) init_size() {
if lb.width == 0 {
lb.width, _ = lb.adj_size()
}
if lb.height == 0 {
_, lb.height = lb.adj_size()
}
}
pub fn (lb &ListBox) size() (int, int) {
// println("lb size: $lb.width, $lb.height")
return lb.width, lb.height
}
pub fn (mut lb ListBox) propose_size(w int, h int) (int, int) {
// println("lb propose: ($w, $h)")
lb.resize(w, h)
scrollview_update(lb)
return lb.width, lb.height
}
fn (mut lb ListBox) resize(width int, height int) {
if width != lb.width {
lb.init_items()
}
lb.width = width
lb.height = height
lb.draw_count = lb.height / lb.item_height
}
// Normally useless but required for scrollview_draw_begin()
fn (lb &ListBox) set_children_pos() {}