NB. select grid class
NB. built from project: ~Source\classes\grid\sgrid\sgrid
NB. jsgrid.ijs   - j select grid
NB.
NB. globals:
NB.
NB. data
NB. coltypes       (types for each column)
NB. coltitle       (column titles)
NB. coltitletypes  (types for column titles)
NB. colwidth       (list of column widths)
NB. rowheight      (height of single row)
NB.
NB. hilite         (hilite row)


coclass 'jsgrid'
require 'jzgrid'
coextend 'jzgrid'
colstretch=: 1
colwidth=: 50
rowheight=: 20
coltitle=: a:
coltypes=: 15
coltitletypes=: 6
hilitecols=: _1
iftotal=: 0
nohilite=: 0  
sortenable=: 0

j=. 'data dataraw'
j=. j,' colstretch coltitle coltitletypes coltypes colwidth'
j=. j,' filltype font0 font1 gridid hilitecols highlighter iftotal'
CNAMES=: j,' nohilite rowheight sortenable scrollid'
create=: 3 : 0
create_jzgrid_ f. y.
atts=: resetatts''
hilite=: 0
fixc=: 0
highlight=: ''
highlighter=: 1
sizeenable=: 1
fixeddata=: 1
definedata''
)
destroy=: destroy_jzgrid_ f.
last=: 1 : 'x. " 1'
stretch=: [ $ ] , ($ ,: @ {:)
fitcols=: 3 : 0
1 fitcols y.
:
'tot initwid'=. y.

sum=. +/ initwid
if. tot = sum do. initwid return. end.

del=. tot - sum
del=. 1 rounddist (tot - sum) * x. % +/ x.
initwid + del
)
rounddist=: 4 : 0
r=. <.j=. ,y.%x.
($y.)$x.*r+((+/r)+/:\:1|j)<<.0.5++/j
)
screenrows=: 3 : 0
<. ({: glqwh '') % {. gridhs
)

char=: 3 : 0

'sysdata sysmodifiers'=. y.
select. a. i. 0 { sysdata
  
case. 13 do. select_result__COCREATOR hilite
case. 166 do. scroll _1
case. 168 do. scroll 1
case. 161 do. scroll -{. screenrc''
case. 162 do. scroll {. screenrc''
case. 163 do. scroll #data
case. 164 do. scroll -#data
  
end.
)




maskdata=: 3 : 0
msk=. y.
data=: msk # data
dataraw=: msk # dataraw
datandx=: /:/: msk # datandx
hilite=: 0 >. hilite <. _2 + +/ msk
setdata''
1 scroll 0
)

mbldown=: 3 : 0
gridfocus''
nohilite=: 0
'colx rowx col row val type'=. mouse y.
captureval=: val

if. row > #data do. return. end.

shape=. $data

select. type
  
case. 0 do.
  if. row=0 do.
    
    if. sortenable=0 do. return. end.
    dat=. col { last dataraw
    ndx=. /:dat
    if. ndx -: i.#dat do. ndx=. \:dat end.
    pndx=. (#ndx) }. i.#data
    dndx=: ndx, pndx
    data=: dndx{data
    dataraw=: ndx{dataraw
    datandx=: (ndx{datandx), pndx
    
    max=. 0 >. (#data) + fixr - screenrows''
    
    at=. hilite - scrollr
    hilite=: ndx i. hilite
    scrollr=: 0 >. max <. hilite - at
    glgridscroll scrollr, 0
    setscrollbars ''
    setseldata''
    
  else.
    hilite=: row-1
    setselhilite''
  end.
  paintx''
  
case. 1 do.
  if. sizeenable do.
    glcursor IDC_SIZEWE
    glcapture 5    
    capture=: 1
    captureindex=: col - 1
    capturesize=: (colx - 1) { gridws
  end.
end.

mark=: (hilite+1), 0

)
mblup=: 3 : 0
if. 0 = capture do. return. end.
glcapture 0
t=. capture
capture=: 0
n=. {. 0 ". y.
d=. 0 >. n + capturesize - captureval

'tw th'=. glqwh ''
left=. (captureindex{.gridws),d
len=. #left
right=. (len }. colstretch) fitcols (tw - +/left) ; len }. gridws
setwidths colwidth=: left,right

paintx ''
)
mbldbl=: 3 : 0
'colx rowx col row val type'=. mouse y.
if. rowx do.
  select_result__COCREATOR hilite
end.
)
size=: 3 : 0
old=. sizewh
sizewh=: glqwh''
setwidths colstretch fitcols ({.sizewh) ; colwidth
setscrollbar''
fit''
)

scroll=: 0&$: : (4 : 0)

old=. hilite
hilite=: (<:#data) <. 0 >. hilite + y.

if. x. >: hilite=old do. scrollit'' end.
mark=: (hilite+1), 0
)
scrollit=: 3 : 0
old=. scrollr

rws=. _2 + screenrows''
if. hilite < scrollr do.
  scrollr=: hilite
elseif. hilite >: scrollr+rws do.
  scrollr=: hilite-rws
end.

setselhilite''
if. scrollr ~: old do.
  glgridscroll scrollr, 0
  setscrollbarv ''
end.

paintx ''
)

definedata=: 3 : 0
set CNAMES
cls=. {:$data
colstretch=: cls stretch colstretch
coltitle=: cls stretch boxopen coltitle
colwidth=: cls stretch colwidth
if. 1 >: #$coltypes do.
  coltypes=: cls stretch coltypes
end.
datandx=: i.#data
)
refreshdata=: 3 : 0
if. #y. do.
  definedata''
  hilite=: y.
else.
  pos=. hilite { data
  definedata''
  hilite=: (#data) | data i. pos
end.
setdata''
scrollit''
)
setdata=: 3 : 0

'r c'=. shape=. $data
setgrid 'maxr maxc'=: shape+1 0
0 0 1 1 settype 0

setheights maxr$rowheight
setwidths colstretch fitcols ({.sizewh) ; colwidth

(0 0 1,c) settext coltitle
(0 0 1,c) settype c stretch coltitletypes

setseldata''

bc=. 2 * iftotal
setborder r,0 1,c,(4$0),(4{.bc),(4$0),1 0 0 0

setscrollbars''
)
setseldata=: 3 : 0
shape=. $data
(1 0,shape) settext data
setselhilite''
)
setselhilite=: 3 : 0
shape=. $data
if. 0 e. shape do. return. end.

typ=. shape $ ,coltypes
if. nohilite do.
  (1 0,shape) settype typ return.
end.
if. hilite >: #data do. hilite=: 0 end.

if. hilitecols -: _1 do.
  h=. highlighter
else.
  h=. ((i.{:$data) e. hilitecols) { 0,highlighter
end.
typ=. (h+hilite{typ) hilite} typ

(1 0,shape) settype typ
)