require'strings'
require'convert'

coclass 'jautocode'

runcount=: 0

message=: 3 : 0
wd'mb "Grid Wizard" *',y.
)

autocode=: 3 : 0
if. runcount do.
  message '"Grid Wizard is already running."'
  return.
end.
'fn class pid cid styles'=: 5{.<;._2 y.
styles=. <;._1 styles
runcount=: 1
first_run''
)

FIRST=: 0 : 0
pc first;pn "Grid Auto Coder";
xywh 200 8 34 12;cc next button;cn "&Next";
xywh 200 23 34 12;cc back button;cn "&Back";
xywh 200 40 34 12;cc cancel button;cn "Cancel";
xywh 6 6 188 56;cc static static;
xywh 224 70 10 10;cc sizer static;cn "";
pas 6 6;pcenter;
rem form end;
)

firsttext=: 0 : 0
These steps add code to your script to treat the isigraph control as a grid.
)

first_run=: 3 : 0
wd FIRST
wd'set static *',firsttext
wd'setenable back 0'
wd 'pshow;'
)

close=: 3 : 0
wd 'pclose;'
runcount=: 0
)

first_cancel_button=: 3 : 0
close''
)

first_next_button=: 3 : 0
wd'smsel ',fn
t=. pid,'_',cid,'_'
d=.wd'smread'
if. +/t E. d do.
  message'Names of the form ',t,'... are already defined.  These must be removed before Grid Wizard can add its definitions.'
  return.
end.
if. 0=+/'coclass' E. d do.
 message'This wizard only works with a form class and this script does not have a coclass. Use File|New Form to create a form class.'
 return.
end.
run'second_run'
)

SECOND=: 0 : 0
pc second;pn "Grid Auto Coder";
xywh 200 8 34 12;cc next button;cn "&Next";
xywh 200 23 34 12;cc back button;cn "&Back";
xywh 200 40 34 12;cc cancel button;cn "Cancel";
xywh 6 6 188 46;cc static static;
xywh 224 70 10 10;cc sizer static;cn "";
pas 6 6;pcenter;
rem form end;
)

secondtext=: 0 : 0
If you want scrollbars for the grid you need to add them to the form. The grid assumes its scrollbars have ids that start with the isigraph id.

hhhh will be used as the id of the horizontal scrollbar.

vvvv will be used as the id of the vertical scrollbar.
)

second_run=: 3 : 0
wd SECOND
t=.secondtext
t=. t rplc 'hhhh';cid,'sb'
t=. t rplc 'vvvv';cid,'sbv'
wd'set static *',t
wd 'pshow;'
)

second_back_button=: 3 : 0
run'first_run'
)

second_cancel_button=: 3 : 0
close''
)

change=: 3 : 0
's n f q'=.y. NB. old, new, after data
i=. ((<s)-:each (#s){.each q)i.<1 NB. index of line
if. i=#q do.
 message'Line',LF,'   ',s,LF,'was not found in the script,',LF,'So line',LF,'   ',n,'could not be properly placed.',LF,'It has been added to the scrip as an NB.+ comment.'
 q=. q,<'NB.+ ',n
else.
 i=.f+i
 q=. (i{.q),(<n),i}.q
end.
)

second_next_button=: 3 : 0
t=. 1!:1 <jpath '~system\packages\autocode\grid.ijs'
t=. t rplc 'mycontrolsb';cid,'sb'
t=. t rplc 'mycontrolsbv';cid,'sbv'
t=. t rplc 'myform';pid
t=. t rplc 'mycontrol';cid
wd'smsel ',fn
q=. wd'smread'
q=. <;.2 q,LF

NB. add grid create to myform_run verb
n=.pid,'_',cid,'_create'''' NB.+',LF
q =. change 'NB. initialize form here';n;1;<q

NB. add grid destroy to class destroy verb
n=.'destroy__',cid,''''' NB.+',LF
q =. change 'codestroy';n;0;<q

q=. t,~;q
wd'smwrite *',q
wd'smsave'
load fn
run 'third_run'
)

run=: 3 : 0
hold=. wd'qhwndp'
possize=. wd'qformx'
". y.,''''''
hnew=:wd'qhwndp'
wd'psel ',hold
wd'pclose'
wd'psel ',hnew
)

THIRD=: 0 : 0
pc third;pn "Grid Auto Coder";
xywh 200 8 34 12;cc next button;cn "&Next";
xywh 200 23 34 12;cc back button;cn "&Back";
xywh 200 40 34 12;cc cancel button;cn "Cancel";
xywh 6 6 188 56;cc static static;
xywh 224 70 10 10;cc sizer static;cn "";
pas 6 6;pcenter;
rem form end;
)

thirdtext=: 0 : 0
The ws_border and ws_clipchildren styles are recommended.

NB.+ comments mark the code that has been added to your script.

A jtable grid object is created for the isigraph control. You need to change this to the grid class you want.
)

third_run=: 3 : 0
wd THIRD
wd'set static *',thirdtext
wd'setenable back 0'
wd'setenable cancel 0'
wd 'pshow;'
)

third_next_button=: 3 : 0
close''
)
