-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathui_grid.lua
2213 lines (1915 loc) · 53.2 KB
/
ui_grid.lua
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--go @ luajit ui_demo.lua
-- -jp=a -e io.stdout:setvbuf'no';io.stderr:setvbuf'no';require'strict';pp=require'pp' "ui_grid.lua"
--Grid widget.
--Written by Cosmin Apreutesei. Public Domain.
local ui = require'ui'
local glue = require'glue'
local box2d = require'box2d'
local push = table.insert
local pop = table.remove
local clamp = glue.clamp
local indexof = glue.indexof
local binsearch = glue.binsearch
local shift = glue.shift
local function popval(t, v)
local i = indexof(v, t)
return i and pop(t, i)
end
local grid = ui.layer:subclass'grid'
ui.grid = grid
grid.iswidget = true
grid.border_color = '#333'
--split panes ----------------------------------------------------------------
local split_pane = ui.layer:subclass'grid_split_pane'
grid.freeze_pane_class = split_pane
grid.scroll_pane_class = split_pane
function grid:create_freeze_pane()
return self:freeze_pane_class({
tags = 'grid_freeze_pane',
iswidget = false,
grid = self,
frozen = true,
}, self.split_pane, self.freeze_pane)
end
function grid:create_scroll_pane()
return self:scroll_pane_class({
tags = 'grid_scroll_pane',
iswidget = false,
grid = self,
frozen = false,
}, self.split_pane, self.scroll_pane)
end
function split_pane:after_init()
self.header_pane = self.grid:create_header_pane(self)
self.rows_pane = self.grid:create_rows_pane(self)
end
function split_pane:col_index_range()
local fc = self.grid._real_freeze_col or 0
if self.frozen then
return 1, fc
else
return fc + 1, 1/0
end
end
--the freeze pane must be small enough so that the splitter is always visible.
--NOTE: only valid after the grid sync'ed.
function split_pane:get_max_w()
if self.frozen then
return self.grid.cw - self.grid.splitter.w
else
return 1/0
end
end
function split_pane:before_sync_layout()
local cols_w = 0
local col_h = self.grid.col_h
for index, col in ipairs(self.grid.cols) do
if col.split_pane == self then
col.h = col_h
if col.visible then
cols_w = cols_w + col.w
end
end
end
self._cols_w = cols_w
self.h = self.grid.ch
if self.frozen then
self.visible = self.grid._real_freeze_col and true or false
self.cw = cols_w
else
local fp = self.freeze_pane
local s = self.grid.splitter
local sw = s.visible and s.w or 0
local x = fp.w + sw
self:transition('x', x) --moving the splitter animates the pane
self.w = self.grid.cw - fp.w - sw
end
--update width of auto_w columns.
if not self.frozen and not self.grid.resizing_col then
local flex_w, fixed_w = 0, 0
for _,col in ipairs(self.grid.cols) do
if col.split_pane == self and col.visible then
if col.auto_w then
flex_w = flex_w + col.w
else
fixed_w = fixed_w + col.w
end
end
end
local avail_flex_w = self.cw - fixed_w
local cols_w = 0
local last_col
for _,col in ipairs(self.grid.cols) do
if col.split_pane == self and col.visible then
if col.auto_w then
col.w = math.floor(avail_flex_w * (col.w / flex_w))
last_col = col
end
cols_w = cols_w + col.w
end
end
if last_col then --dump accumulated error into the last auto_w column.
last_col.w = last_col.w + (self.cw - cols_w)
end
self._cols_w = math.floor(cols_w)
end
--sync column positions.
self:sync_cols_x()
self.header_pane:sync_to_grid()
self.rows_pane:sync_to_grid_header_and_split_pane()
self.header_pane:sync_to_rows_pane()
end
function split_pane:col_at_x(x, clamp_left, clamp_right)
clamp_left = clamp_left == nil or clamp_left
clamp_right = clamp_right == nil or clamp_right
local first_col, last_col
for _,col in ipairs(self.grid.cols) do
if col.visible and col.split_pane == self then
if x >= col.x and x <= col.x2 then
return col
end
first_col = first_col or col
last_col = col
end
end
if clamp_left and x < first_col.x then
return first_col
end
if clamp_right and x > last_col.x2 then
return last_col
end
end
--freeze_col property --------------------------------------------------------
--returns the largest freeze_col which satisfies freeze_pane.max_w.
function grid:max_freeze_col()
local max_w = self.freeze_pane.max_w
local last_i
for i,col in ipairs(self.cols) do
if col.visible then
max_w = max_w - col.w
if max_w < 0 then
return i > 1 and i - 1 or nil
end
end
end
local last_i
for i = #self.cols, 1, -1 do
local col = self.cols[i]
if col.visible then
if last_i then
return i
else
last_i = i
end
end
end
end
function grid:sync_cols()
--limit freeze_col to the maximum allowed by geometry.
local fi = self.freeze_col
local max_fi = fi and self:max_freeze_col()
local fi = max_fi and math.min(fi, max_fi)
self._real_freeze_col = fi or false
--assign columns to the right panes based on freeze_col.
local col1 = self:rel_visible_col(1).index
local col2 = self:rel_visible_col(-1).index
for i,col in ipairs(self.cols) do
local frozen = fi and i <= fi
col.split_pane = frozen and self.freeze_pane or self.scroll_pane
col.parent = col.split_pane.header_pane.content
col:settag('first_col', i == col1)
col:settag('last_col', i == col2)
col:sync_styles()
end
end
--freeze pane <-> scroll pane splitter ---------------------------------------
local splitter = ui.layer:subclass'grid_splitter'
grid.splitter_class = splitter
splitter.w = 6
splitter.background_color = '#666'
splitter.cursor = 'size_h'
ui:style('grid_splitter', {
transition_x = true,
transition_duration = .1,
})
ui:style('grid :resize_col > grid_splitter', {
transition_x = false,
})
function grid:create_splitter()
return self:splitter_class({
iswidget = false,
grid = self,
}, self.splitter)
end
function splitter:sync_to_grid()
self.visible = self.grid._real_freeze_col and true or false
self:transition('x', self.grid.scroll_pane.x - self.w)
self.h = self.grid.ch
end
splitter.mousedown_activate = true
splitter.draggable = true
local drag_splitter = ui.layer:subclass'grid_drag_splitter'
grid.drag_splitter_class = drag_splitter
drag_splitter.background_color = '#fff2'
function grid:create_drag_splitter()
return self:drag_splitter_class({
grid = self,
}, self.drag_splitter)
end
function splitter:start_drag(button, mx, my, area)
if button ~= 'left' then return end
self.grid.scroll_pane.rows_pane.hscrollbar:transition('offset', 0)
local ds = self.drag_splitter or self.grid:create_drag_splitter()
self.drag_splitter = ds
ds.x = self.x
ds.y = self.y
ds.w = self.w
ds.h = self.h
ds.visible = true
self.grid:settag(':move_splitter', true)
return ds
end
function grid:nearest_col(x)
local last_ci
for i,col in ipairs(self.cols) do
if col.visible then
local x = self:to_other(col.parent, x, 0)
if x <= col.cx then
return last_ci
elseif x <= col.x2 then
return i
end
last_ci = i
end
end
return last_ci
end
function drag_splitter:drag(dx, dy)
self.grid.scroll_pane.rows_pane.hscrollbar.offset = 0
local col = self.grid:rel_visible_col(-1)
local last_col_x2 = col and col.parent:to_other(self.grid, col.x2, 0)
local max_w = math.min(last_col_x2 or 0, self.grid.cw) - self.w
self:transition('x', clamp(0, self.x + dx + self.w / 2, max_w), 0)
self.grid.freeze_col = self.grid:nearest_col(self.x)
self:invalidate()
end
function splitter:end_drag(ds)
ds.visible = false
self.grid:settag(':move_splitter', false)
end
--header pane ---------------------------------------------------------------
local header_pane = ui.scrollbox:subclass'grid_header_pane'
grid.header_pane_class = header_pane
header_pane.clip_content = true --for column moving
grid.header_visible = true
function grid:create_header_pane(split_pane)
return self.header_pane_class(self.ui, {
parent = split_pane,
iswidget = false,
split_pane = split_pane,
grid = self,
}, self.header_pane)
end
function header_pane:sync_to_grid()
self.visible = self.grid.header_visible
self.hscrollbar.visible = false
self.h = self.grid.col_h
self.content.h = self.ch
end
function header_pane:sync_to_rows_pane()
local rows_pane = self.split_pane.rows_pane
self.w = rows_pane.view.w
self.content.w = rows_pane.content.w
self.hscrollbar.offset = rows_pane.hscrollbar.offset
end
--rows pane ------------------------------------------------------------------
local rows_pane = ui.scrollbox:subclass'grid_rows_pane'
grid.rows_pane_class = rows_pane
local rows = ui.layer:subclass'grid_rows_layer'
grid.rows_layer_class = rows
function grid:create_rows_pane(split_pane)
local rows = self.rows_layer_class(self.ui, {
grid = self,
iswidget = false,
split_pane = split_pane,
})
local rows_pane = self.rows_pane_class(self.ui, {
parent = split_pane,
iswidget = false,
split_pane = split_pane,
grid = self,
content = rows,
rows_layer = rows,
border_width = 2,
padding = 2,
}, self.rows_pane)
rows.rows_pane = rows_pane
rows_pane.vscrollbar.visible = not split_pane.frozen
rows_pane.hscrollbar.visible = not split_pane.frozen
--synchronize vertical scrolling between split panes.
local barrier
rows_pane.vscrollbar:on('offset_changed', function(sb, offset)
if barrier then return end
barrier = true
rows_pane.split_pane.other_pane.rows_pane.vscrollbar.offset = offset
barrier = false
end)
self:create_cell_mouse_events(rows)
return rows_pane
end
function rows_pane:sync_to_grid_header_and_split_pane()
--sync to grid
self.content.ch = self.grid.rows_h
--sync to header pane
local header_pane = self.split_pane.header_pane
self.y = header_pane.visible and header_pane.h or 0
--sync to split pane
local split_pane = self.split_pane
self.w = split_pane.cw
self.h = split_pane.ch - self.y
if self.grid.resizing_col then
--prevent shrinking the rows pane while resizing a column in order to
--keep scrolling stable and not move the column under the mouse.
self.content.cw = math.max(self.content.cw, split_pane._cols_w)
else
self.content.cw = split_pane._cols_w
end
--resolve view w/h which depends on content w/h when autohide_empty.
self:sync()
--keep the content layer no smaller than the scrollbox view to prevent
--clipping the content while moving a column.
--NOTE: content w/h should not be based on view w/h because view w/h
--depends on content w/h when autohide_empty, but here is ok.
self.content.cw = math.max(self.content.cw, self.view.cw)
end
--column headers -------------------------------------------------------------
local col = ui.layer:subclass'grid_col'
grid.col_class = col
col.text_align_x = 'left'
col.nowrap = true
col._w = 200
col.auto_w = true --distribute pane's width among all columns
col.min_cw = 8
col.max_w = 1000
col.padding_left = 4
col.padding_right = 4
col.clip_content = true --for text
col.background_color = '#111' --for moving
col.border_width = 1
col.border_color = '#333'
col.cursor_resize = 'size_h'
col.resizeable = true
col.moveable = true
grid.col_resize = true
grid.col_move = true
grid.col_h = 24
local function column(col)
if type(col) == 'string' then
return {text = col}
else
return col
end
end
function grid:create_col(col, col_index)
col = self.col_class(self.ui, column(self.col), column(col))
col.grid = self
col.value_index = col.value_index or col_index
--create a cell object for the column.
if col.cell then
col.cell = self:create_cell(col.cell)
else
if not self.cell then
self.cell = self:create_cell(self.cell)
end
col.cell = self.cell
end
return col
end
function grid:remove_col(col)
if col.grid ~= self then return end
popval(self.cols, col)
col.grid = nil
end
function col:get_index()
return indexof(self, self.grid.cols)
end
function col:set_index(i)
local i0 = self.index
local i = clamp(i, 1, #self.grid.cols)
if i == i0 then return end
pop(self.grid.cols, i0)
push(self.grid.cols, i, self)
end
function col:get_w()
return self._w
end
function col:set_w(w)
local padding = self.w - self.cw
self._w = clamp(w, padding + self.min_cw, self.max_w)
end
function col:get_clipped()
if not self.visible then
return true
end
local x, y, w, h = self.x, self.y, self.w, self.h
local w, h = select(3, box2d.clip(x, y, w, h, self.parent:client_rect()))
return w == 0 or h == 0
end
function col:override_hit_test(inherited, x, y, reason)
local widget, area = inherited(self, x, y, reason)
if self.grid.col_resize and self.resizeable and widget == self then
if x >= self.x2 - self.padding_right then
return self, 'resize'
elseif x <= self.x + self.padding_left then
local col = self.grid:rel_visible_col(-1, self)
if col then
return col, 'resize'
end
end
end
return widget, area
end
function grid:rel_visible_col(positions, col, clamp)
if positions == 0 then
return col or self:rel_visible_col(1)
end
local j1, j2, step
local last_col = col
if positions > 0 then
j1, j2, step = col and col.index + 1 or 1, #self.cols, 1
else
j1, j2, step = col and col.index - 1 or #self.cols, 1, -1
positions = -positions
end
for j = j1, j2, step do
local col = self.cols[j]
if col.visible then
positions = positions - 1
last_col = col
if positions == 0 then
return col
elseif positions < 0 then
break
end
end
end
return clamp and last_col or nil
end
--column drag & drop ---------------------------------------------------------
col.draggable = true
col.drag_threshold = 10
function col:mousedown(mx, my, area)
if area == 'resize' or area == 'background' then
self.active = true
end
end
function col:mouseup(mx, my, area)
self.active = false
end
function col:start_drag(button, mx, my, area)
if area == 'resize' then
return self:start_drag_resize(button, mx, my)
else
return self:start_drag_move(button, mx, my)
end
end
function col:drag(dx, dy)
if self.grid.resizing_col then
self:drag_resize(dx, dy)
else
self:drag_move(dx, dy)
end
end
function col:end_drag()
if self.grid.resizing_col then
self:end_drag_resize()
else
self:end_drag_move()
end
end
--column resizing ------------------------------------------------------------
function col:start_drag_resize(button, mx, my)
if button ~= 'left' then return end
self.resizing = true
self:settag(':resizing', true)
self.grid.resizing_col = self
self.grid:settag(':resize_col', true)
self.drag_w = self.w
self.drag_max_w = self.split_pane.max_w - self.split_pane.w + self.w
return self
end
function col:drag_resize(dx, dy)
local w = self.drag_w + dx
self.w = math.min(w, self.drag_max_w)
self:invalidate()
end
function col:end_drag_resize()
self.drag_w = false
self.resizing = false
self:settag(':resizing', false)
self.grid.resizing_col = false
self.grid:settag(':resize_col', false)
end
--column moving --------------------------------------------------------------
function col:start_drag_move(button, mx, my, area)
if button ~= 'left' then return end
if not self.grid.col_move then return end
if not self.moveable then return end
self.window.cursor = 'move'
self.moving = true
self:settag(':moving', true)
self.grid.moving_col = self
self.grid:settag(':move_col', true)
self:to_front()
return self
end
function col:drag_move(dx, dy)
self:transition('x', self.x + dx, 0)
self:invalidate()
end
function col:end_drag_move()
self.index = self.move_index
self.move_index = false
self.moving = false
self:settag(':moving', false)
self.grid.moving_col = false
self.grid:settag(':move_col', false)
self.window.cursor = nil
end
--sync columns x position and move_index for the moving column.
function split_pane:sync_cols_x()
local moving_col = self.grid.moving_col
moving_col = moving_col and moving_col.split_pane == self and moving_col
if moving_col then
moving_col.move_index = false
end
local x = 0
local i = 1
local min_col_index, max_col_index = self:col_index_range()
for _,col in ipairs(self.grid.cols) do
if col.split_pane == self then
if not col.moving and col.visible then
if moving_col
and not moving_col.move_index
and i >= min_col_index
and i <= max_col_index
and moving_col.x < x + col.w / 2
then
moving_col.move_index = i
x = x + moving_col.w --make room for the moving col
end
col:transition('x', x)
x = x + col.w
end
end
if not col.moving then
i = i + 1
end
end
if moving_col and not moving_col.move_index then --too far to the right
x = x + moving_col.w
moving_col.move_index = max_col_index
end
end
ui:style('grid_col', {
transition_x = true,
transition_duration = .2,
})
ui:style('grid :move_col > grid_col', {
transition_x = true,
transition_duration = .5,
})
ui:style([[
grid :resize_col > grid_col,
grid :move_splitter > grid_col,
]], {
transition_x = false,
})
ui:style('grid_col :moving, grid_cell :moving', {
opacity = .7,
})
ui:style('grid_col !last_col !:moving', {
border_width_right = 0,
})
--cells ----------------------------------------------------------------------
local cell = ui.layer:subclass'grid_cell'
grid.cell_class = cell
cell.text_align_x = 'left'
cell.nowrap = true
cell.padding_left = 4
cell.padding_right = 4
cell.clip_content = true --for text
ui:style('grid_cell even', {
background_color = '#020202',
})
ui:style('grid_cell :moving', {
background_color = '#000',
})
ui:style('grid_cell :hot', {
background_color = '#111',
})
ui:style('grid_cell :selected', {
background_color = '#111',
})
ui:style('grid_cell :grid_focused :selected', {
background_color = '#113',
})
ui:style('grid_cell :focused :selected', {
background_color = '#181818',
})
ui:style('grid_cell :grid_focused :focused :selected', {
background_color = '#181844',
})
local border_width = ui:value_of'border_width'
ui:style('grid_cell first_col !last_col !multi_select !cell_select :selected', {
border_width_right = 0,
})
ui:style('grid_cell last_col !first_col !multi_select !cell_select :selected', {
border_width_left = 0,
})
ui:style('grid_cell !first_col !last_col !multi_select !cell_select :selected', {
border_width_left = 0,
border_width_right = 0,
})
function grid.sync_cell_to_grid(grid, self)
self:settag('grid_cell', true)
self:settag('standalone', false)
self:settag('multi_select', grid.multi_select)
self:settag('cell_select', grid.cell_select)
self:settag(':grid_focused', grid.focused)
end
function grid.sync_cell_to_col(grid, self, col)
self.parent = col.split_pane.rows_pane.content
self.x = col.x
self.w = col.w
self:settag(':moving', col.moving)
self:settag(':resizing', col.resizing)
local index = col.index
self:settag('first_col', col.tags.first_col)
self:settag('last_col', col.tags.last_col)
end
function grid.sync_cell_to_row(grid, self, i, y, h)
self.y = y
self.h = h
self:settag('even', i % 2 == 0)
if grid.moving_row_index == i then
self:settag(':moving', true)
end
end
function grid:display_value(i, col, val)
if type(val) == 'nil' or type(val) == 'boolean' then
return string.format('<%s>', tostring(val))
end
return tostring(val)
end
function grid.sync_cell_to_value(grid, self, i, col, val)
self.text = grid:display_value(i, col, val)
self:settag(':selected', grid:cell_selected(i, col))
self:settag(':focused', grid:cell_focused(i, col))
end
function cell:invalidate() end --we call draw() manually
function grid:create_cell(cell)
cell = self.cell_class(self.ui, cell)
cell:inherit() --speed up cell drawing
cell.iswidget = false
return cell
end
local attr = glue.attr
function grid:cell_at(i, col)
return col.cell
end
--rows -----------------------------------------------------------------------
grid.row_h = 24
grid.var_row_h = false
function grid:get_rows_h()
local y, h = self:row_yh(self.row_count)
return y + h
end
function grid:_sync_row_y(i)
local h = self.row_h
local t = self._row_y
local y = i > 1 and t[i-1] + (self:row_var_h(i-1) or h) or 0
local n = self.row_count
for i = i, n do
t[i] = y
y = y + (self:row_var_h(i) or h)
end
t[n+1] = y --for computing row_h of the last row
end
function grid:_init_row_y()
if not self.var_row_h then return end
self._row_y = {}
self:_sync_row_y(1)
end
function grid:row_yh(i)
local n = self.row_count
if n == 0 then
return 0, 0
end
i = clamp(i, 1, n)
if self.var_row_h then
local y = self._row_y[i]
return y, self._row_y[i+1] - y
else
return self.row_h * (i - 1), self.row_h
end
end
function grid:_insert_row_y_rows(i, len)
if not self._row_y then return end
shift(self._row_y, i, len)
self:_sync_row_y(i)
end
function grid:_remove_row_y_rows(i, len)
if not self._row_y then return end
shift(self._row_y, i, -len)
self:_sync_row_y(i)
end
function grid:row_at_y(y, clamp_top, clamp_bottom)
clamp_top = clamp_top == nil or clamp_top
clamp_bottom = clamp_bottom == nil or clamp_bottom
if self.var_row_h then
local t = self._row_y
if #t == 0 then return nil end
if y < t[1] then
return clamp_top and 1 or nil
elseif y > t[#t] then
return clamp_bottom and self.row_count or nil
end
local i = binsearch(y, t)
return y < t[i] and i-1 or i
else
local i = math.floor(y / self.row_h) + 1
if i < 1 then
return clamp_top and 1 or nil
end
if i >= self.row_count then
return clamp_bottom and self.row_count or nil
end
return i
end
end
function grid:abs_row_y(y)
return y + self.scroll_pane.rows_pane.vscrollbar.offset
end
function grid:rel_row_y(y)
return y - self.scroll_pane.rows_pane.vscrollbar.offset
end
function grid:row_screen_yh(i)
local y, h = self:row_yh(i)
return self:rel_row_y(y), h
end
function grid:row_at_screen_y(y, ...)
return self:row_at_y(self:abs_row_y(y), ...)
end
function grid:row_at_screen_bottom_y(y, ...)
local screen_h = self.scroll_pane.rows_pane.vscrollbar.view_length
return self:row_at_screen_y(y + screen_h, ...)
end
function grid:visible_rows_range()
local h = self.scroll_pane.rows_pane.view.ch
local i1 = self:row_at_screen_y(0)
local i2 = self:row_at_screen_y(h - 1)
return i1, i2
end
--cell drawing & hit-testing -------------------------------------------------
function grid:draw_row_col(cr, i, col, y, h, hot)
if self.editmode
and self.focused_row_index == i
and self.focused_col == col
then
return
end
local cell = self:cell_at(i, col)
if cell ~= self._cell then
self._cell = cell
self:sync_cell_to_grid(cell)
self:sync_cell_to_col(cell, col)
end
self:sync_cell_to_row(cell, i, y, h)
self:sync_cell_to_value(cell, i, col, self:cell_value(i, col))
cell:settag(':hot', hot)
cell.visible = true
cell:sync()
cell:sync_layout()
cell:draw(cr)
cell.visible = false
end
function grid:draw_rows_col(cr, i1, i2, col, hot_i, hot_col)
local moving_i = self.moving_row_index
local moving_y, moving_h
if moving_i then
moving_y, moving_h = self:row_screen_yh(moving_i)
moving_y = self:abs_row_y(self.moving_row_y + self.moving_row_dy)
end
for i = i1, i2 do
if i ~= moving_i then
local y, h = self:row_yh(i)
local hot = i == hot_i and (not self.cell_select or col == hot_col)
if moving_y then
if i > moving_i then
--remove the space originally taken by the moving row
y = y - moving_h
end
if moving_y < y + h / 2 then
--make space for the moving row at its target position
y = y + moving_h
end
end
self:draw_row_col(cr, i, col, y, h, hot)
end
end
if moving_i then
self:draw_row_col(cr, moving_i, col, moving_y, moving_h)
end
end
function grid:draw_rows(cr, rows_pane)
local i1, i2 = self:visible_rows_range()
if not i1 then return end
local hot_i, hot_col
if self:cell_hot() then
hot_i, hot_col = self.hot_row_index, self.hot_col
end
self._cell = false
local header = rows_pane.split_pane.header_pane.content
for _,col in ipairs(header) do
if col.isgrid_col and not col.clipped then
local cell = self:cell_at(i1, col)
if cell == self._cell then
self:sync_cell_to_col(cell, col)
end
self:draw_rows_col(cr, i1, i2, col, hot_i, hot_col)
end
end
end
function rows:before_draw_content(cr)
self.grid:draw_rows(cr, self)
end