Skip to content

Instantly share code, notes, and snippets.

@toomasv
Last active September 24, 2021 03:28
Show Gist options
  • Select an option

  • Save toomasv/1b008bbff02550cbb821378be73e487f to your computer and use it in GitHub Desktop.

Select an option

Save toomasv/1b008bbff02550cbb821378be73e487f to your computer and use it in GitHub Desktop.
Experiment with draw-based table
Red [
Description: "Simple navigation for draw-based table"
]
recycle/off
len-data: 200'000;0
data: make block! len-data
rows: 30
cols: 11
bx: 100x24
current: none
edit: function [ofs txt][
editor/offset: ofs
editor/size: bx
editor/text: txt
editor/visible?: yes
fc/selected: editor
]
;Make just a pageful of data initially
done: cols - 1 * rows
loop done [append data copy random "1234567890"]
draw-block: make block! 9 * cols * rows + (2 * rows)
;Initial fill
repeat i rows [
append draw-block 'fill-pen
append draw-block pick [white snow] odd? i
repeat j cols [
d: i - 1 * (cols - 1) + (j - 1)
if d <= length? data [
repend draw-block [
'clip s: (as-pair j i) - 1 * bx s + bx
'box s s + bx
'text s + 4x2 either j = 1 [form i][data/:d]
]
]
]
]
;Adjust row color and fill texts only
fill: function [][
recycle/off
k: b/extra
system/view/auto-sync?: off
repeat i rows [
k: k + 1
row: at b/draw i - 1 * (cols * 9 + 2) + 1
poke row 2 pick [white snow] odd? k
repeat j cols [
d: sort-index/:k - 1 * (cols - 1) + (j - 1)
if d <= length? data [
row: find/tail row 'text
poke row 2 either j = 1 [form sort-index/:k][data/:d]
]
]
]
show fc
system/view/auto-sync?: on
if editor/visible? [editor/offset/y: editor/offset/y + (current - b/extra * bx/y)]
;recycle
;recycle/on
]
scroll: func [sc-pos [integer!]][
current: b/extra
b/extra: sc-pos - 1
fill
]
adjust-scroller: does [
scr/max-size: rows-total - rows + 1
scr/page-size: rows
]
sorting: function [index col][
sort/compare index func [a b][
d1: a - 1 * (cols - 1) + col
d2: b - 1 * (cols - 1) + col
data/:d1 <= data/:d2
]
]
view/no-wait fc: layout compose/only [
b: base (bx * (as-pair cols rows) + 19x2) extra 0 with [
draw: draw-block
flags: [scrollable all-over]
]
on-scroll [
unless event/key = 'end [
scroll scr/position: min scr/max-size max 1 switch event/key [
track [either event/picked > (rows-total / 2) [event/picked + rows][event/picked]]
up [scr/position - 1]
page-up [scr/position - scr/page-size]
down [scr/position + 1]
page-down [scr/position + scr/page-size]
]
]
]
on-wheel [
current: b/extra
b/extra: min rows-total - rows max 0 b/extra - to-integer (event/picked * either event/ctrl? [rows][3])
scr/position: b/extra + 1
fill
]
on-down [/local [ofs cell y found txt]
if editor/visible? [b/draw: b/draw] ;Update draw in case we edited a field and didn't enter
ofs: as-pair round/down/to event/offset/x bx/x ;Get cell coordinates
round/down/to event/offset/y bx/y
cell: ofs / bx ;Get cell address
if cell/x > 0 [ ;Don't edit autokeys
found: find find b/draw as-pair 0 ofs/y 'text ;Which row do we have? Find autokey (first entry in row)
y: to-integer found/3 ;Autokey's value
txt: data/(y - 1 * (cols - 1) + cell/x) ;Get original entry from data
editor/extra: cell/x + 1 ;Register column
edit ofs + 10 txt ;Compensate offset for VID space
]
]
on-created [
put get-scroller face 'horizontal 'visible? no
scr: get-scroller face 'vertical
]
at 0x0 editor: field hidden
on-enter [/local [idx]
face/visible?: no
if idx: indexes/(face/extra) [
sorting idx face/extra - 1
if col-idx/selected = face/extra [
append clear sort-index indexes/(col-idx/selected)
fill
]
]
b/draw: b/draw
]
return
button "Top" [
current: b/extra
scr/position: 1 + b/extra: 0
fill
]
button "Page Up" [
current: b/extra
scr/position: 1 + b/extra: max b/extra - rows 0
fill
]
button "Up" [
current: b/extra
scr/position: 1 + b/extra: max b/extra - 1 0
fill
]
button "Down" [
current: b/extra
scr/position: 1 + b/extra: min b/extra + 1 rows-total - rows
fill
]
button "Page Down" [
current: b/extra
scr/position: 1 + b/extra: min b/extra + rows rows-total - rows
fill
]
button "Bottom" [
current: b/extra
scr/position: 1 + b/extra: rows-total - rows
fill
]
pad 0x5 text "Sort by column:"
pad 0x-5 col-idx: drop-list 40 select 1 with [data: repeat i cols [append [] form i]]
on-change [/local [i]
recycle/off
either indexes/(col-idx/selected) [
append clear sort-index indexes/(col-idx/selected)
][
indexes/(col-idx/selected): make block! rows-total
c: col-idx/selected - 1
sorting sort-index c
append indexes/(col-idx/selected) sort-index
]
b/extra: 0
scr/position: 1
fill
;recycle
recycle/on
]
button "Recycle" [recycle]
]
;Make rest of original data
random/seed now
loop len-data - done [append data copy random "1234567890"]
rows-total: round/ceiling/to (length? data) / (cols - 1) 1 ;For case it doesn't distribute evenly
;Prepare indexes
indexes: make map! cols ;Room for index for each column
default-index: make block! rows-total ;Room for row numbers
repeat i rows-total [append default-index i] ;Default is just simple sequence
indexes/1: default-index ;Default is for first (autokey) column
sort-index: copy default-index ;Set default as active index
adjust-scroller
recycle/on
do-events
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment