/*Tutorial 5 for AWNPipe*/

/* uniconify and front of old window it tutorial 5 is already running*/
if show('P','TUTORIAL5') then do
  address TUTORIAL5 'front'
  exit
end

delaybub=0
call setdefaults()
/* override defaults and get window size and position */
call tooltypes()
/*open GUI*/
call buildgui()
if (tticonify) then call iconify(1)
/*main loop*/
do while ~eof(ca)
  call topipe('tick 50')
  in= readln(ca)
  parse var in in1 in2 in3 in4 in5 .
  /*handle help display delay*/
  if delaybub>0 then do
    delaybub=delaybub-1
    /* if we reach zero display the help bubble*/
    if delaybub=0 then do
      bubleon=newbub
      call topipe('bubble top 'bubx 'left 'buby 'gt "'help.newbub'"')
    end
  end

  if (in1='help' & helpon~=0 ) then call bubble(in2)

  /*close help bubble if active state of window changes*/
  if in1='active' then call bubble(0)
  if in1='gadget' then call gadget()
  if in1='menu' then call menu()
  if in1='close' then call windowclosed()
  if in1='iconify' then call iconify(in2)
  if in1='app' then call app()
  if in1='arexx' then call rxhst()
  if in1='cx' then call commodity()

end
/*end of main loop*/

exit

gadget:
if in2=agegad then age=in3
if in2=sexgad then sex=in3
if in2=knogad then knowledge=in3
if in2=basgad then basic=in3
if in2=aregad then arexx=in3
if in2=cgad then c=in3
if in2=asmgad then asm=in3
/* the return from a string gadget is parsed differently since it can be more
then 1 word. */
if in2=namegad then do
  parse var in . . in3
  /*strip it since arexx parsing adds the leading space to the last string
  assignment*/
  name=strip(in3)
  if name='' then call topipe('id 'savegad' dis 1 ref')
  else call topipe('id 'savegad' dis 0 ref')
end

if in2=dongad then do
  showtx(0, 'Normal exit *nname' name '*nage' age '*nsex' sex '*nknowledge' knowledge '*nbasic' basic 'arexx' arexx 'c' c 'asm' asm)
  exit
end

if in2=resgad then call resetform()
if in2=savegad then call saveform()
if in2=loadgad then call loadform()

if in2=cangad then do
  showtx(300,'user canceled')
  exit
end

return

windowclosed:
showtx(300,'User aborted with CTRL BackSlash or closed window.')
exit
return

menu:
if in2=0 then do
  if in3=0 then do
    if in4=0 then call settooltypes(1)
    if in4=1 then call settooltypes(0)
  end
  if in3=1 then do
    helpon=in5
    if helpon=0 then call bubble(0)
  end
  if in3=3 then showtx(500,' Tutorial 4 for AWNP *n  by William Parker')
end


if in2=1 then do
  if in3=0 then do
    showtx(0, 'name' name '*nage' age '*nsex' sex '*nknowledge' knowledge '*nbasic' basic 'arexx' arexx 'c' c 'asm' asm)
  end
  if in3=1 then do
    if in4=0 then showtx(0, 'name' name '*nage' age '*nsex' sex)
    if in4=1 then showtx(0, 'knowledge' knowledge '*nbasic' basic 'arexx' arexx 'c' c 'asm' asm)
  end
end
return


setdefaults:
name=''
age=30
sex=0
knowledge=0
basic=0
c=0
asm=0
arexx=0
tticonify=0
helpon=1
return

buildgui:

/* Open pipe 'tut2' with GUI creation option '/xc' */
call open(ca,"awnpipe:tut5/xc")

/* define the window */

/* The first line oF every GUI is the window definition. The window is titled
"Tutorial 2" and its elements will be laid out verticaly (v). It has a
closegadget (cg) , depthgadget (dg) , and dragbar (db). It will have spaces
inbetween its gadgets (si). It will open on the topleft (tl) of the screen
becoming active (a) when opened. The window can be icoified (ig) and modified
(m). It is an app window (app) . It can be sized (SG) but not verticaly (fh)*/

call topipe('"Tutorial 5" v cg dg db si a help state ig sg fh m app ii "tutorial4.rx" 'ttwindow)

/* define the gadgets*/

call topipe(' layout b 0 v')

/* Labels are used to tell the user what information to enter in each gadget.
These labels are unatached (ua) when they are defined so don't go directly
into the GUI. Instead they are attached to the following gadget by the
childlabel (chl) keyword. */

call topipe(' label  gt "Name: " ua')
namegad=topipe('string lj chl gt "'name'"')
help.namegad='Enter a name here. This feild must be valid*n before you can save the information.'

call topipe(' label gt "Age: " ua')
agegad=topipe('integer chl minn 5 maxn 115 arrows defn 'age'  weiw 0')
help.agegad='Enter an age between 5 and 115'

call topipe(' label gt "Sex: " ua')
sexgad=topipe('radiobutton rl "Male|Female" chl s 'sex)
help.sexgad='You may only choose one sex.'

call topipe(' label gt "Knowledge: " ua')
knogad=topipe('chooser pu cl "Novice|Average|Good|Expert" chl s 'knowledge)
help.knogad='Enter your programming knowledge level.'

call topipe(' label gt "Language(s): " ua')

call topipe(' layout b 0 chl')
basgad=topipe('checkbox gt "Basic " chl')
help.basgad='Select if you program in Basic.'

aregad=topipe('checkbox gt "Arexx " chl')
help.aregad='Select if you program in Arexx.'

cgad=topipe('checkbox gt "C " chl')
help.cgad='Select if you program in C.'

asmgad=topipe('checkbox gt "ASM " chl')
call topipe(' le')
help.asmgad='Select if you program in Assembler.'

call topipe(' le')

call topipe(' layout si so')
savegad= topipe('button gt "Save" dis 1')
help.savegad='Save data to a file.*n(You must enter a name first)'

loadgad= topipe('button gt "Load" ')
help.loadgad='Read a data file.'

resgad= topipe('button gt "Reset Form" ')
help.resgad='Reset the form to defaults.'

dongad= topipe('button gt "Done" c')
help.dongad='Display data and quit the demo.'

cangad= topipe('button gt "Cancel" c')
help.cangad='Quit the demo without displaying the data.'

call topipe(' le')


men0=topipe(' menu gt "Project|Window|$@SSnapshot|$@UUnSnapshot|^&Bubble Help|-|About"')
men1=topipe(' menu gt "Data|@AShow all data|Show part|$@PPersonal|$@SSkill"')
call topipe(' arexx gt "TUTORIAL5|age|sex|knowledge|front|quit"')

getfilegad=topipe('getfile gt "Select Information File" fn "t:" ua pat "#?.tut3"')
call topipe('commodity cxname Demo5 cxtitle="AWNPipe demo 5" cxdesc="Disable Enable ignored..." cxhotkey="ctrl alt 5"')
/*open the GUI window*/
call topipe("open")
call topipe('id 'savegad' dis 1 ref')
if (~helpon) then call topipe('id 'men0' tar 3 s 0 ')
return


topipe:
/* this routine does error checking on lines written to pipe.*/

/*get line to output*/
parse arg out

/* write to the pipe*/
call writeln(ca,out)

/*get responce and parse it.*/
res=readln(ca)
parse var res res1 res2 .

/* if all is ok return the second part of the responce (usualy the GID)*/
if res1='ok' then return(res2)

/* something went wrong, we notify the user then exit */
/*show problem line and responce (reponce may be just a blank line)*/
say 'error from: 'out
say '  responce: ' res
exit

resetform:
call topipe('id 'agegad' defn 30 ref')
call topipe('id 'sexgad' s 0 ref')
call topipe('id 'knogad' s 0 ref ')
call topipe('id 'basgad' s 0 ref')
call topipe('id 'aregad' s 0 ref')
call topipe('id 'cgad' s 0 ref ')
call topipe('id 'asmgad' s 0 ref')
call topipe('id 'namegad' gt "" ref')
topipe('id 'savegad' dis 1 ref')
call setdefaults()
return

saveform:
call open(form,'t:'name'.tut3','W')
call writech(form,age sex knowledge basic arexx c asm name)
call close(form)
return

loadform:
call writeln(ca,'id 'getfilegad' fn "t:" s 1')
res=readln(ca);
parse var res res1 '"' filename '"'
loadapp:
if res1=0 then return
call open(form,filename,'R')
formin=readch(form,1000)
parse var formin age sex knowledge basic arexx c asm name
name=substr(name,2)
call close(form)
/*uniconify in case its needed*/
update:

call topipe('id 'agegad' defn 'age' ref')
call topipe('id 'sexgad' s 'sex' ref')
call topipe('id 'knogad' s 'knowledge' ref ')
call topipe('id 'basgad' s 'basic' ref')
call topipe('id 'aregad' s 'arexx' ref')
call topipe('id 'cgad' s 'c' ref ')
call topipe('id 'asmgad' s 'asm' ref')
call topipe('id 'namegad' gt "'name'" ref')
if name='' then call topipe('id 'savegad' dis 1 ref')
else call topipe('id 'savegad' dis 0 ref')
return

app:
parse var in . in2
filename=strip(in2)
if (right(filename,5)='.tut3') then do
  res1=1
  call loadapp()
end
return

/* send a modify command to iconify/uniconify window and remember the current
state of the window*/
iconify:
iconified=arg(1)
if iconified=1 then call topipe('id 0 s 32')
else  call topipe('id 0 s 64')
call bubble(0)
return(0)

showtx:
call open(ptx,'awnpipe:tut3txt/xc')
call writeln(ptx,'db dg "Tutorial 4" q cg cm m a so si ')
call writeln(ptx,'label lj gt "'arg(2)'"')
call writeln(ptx,'open')
if arg(1)~=0 then call writeln(ptx,'tick 'arg(1))
else call writeln(ptx,'m')
call close(ptx)
return(0)

bubble:
/* get help gadget number*/
newbub=arg(1)
if newbub=-1 then newbub=0
/* return if it has not changed*/
if (bubbleon=newbub) then return()
/* close old bubble if we have one*/
if bubbleon~=0 then call  topipe('bubble')
/* if its not a valid gadget kill count down to display*/
if newbub=0 then delaybub=0
/*else remeber mouse position and start new countdown*/
else do
  bubx=in3
  buby=in4
  delaybub=3
end
return

rxhst:

/*read the external command from the pipe, it is in3 characters long*/
rxcmd=readch(ca,in3)
/* return an error if unrecognized command */
if in2>4 then call topipe("rc 10")
/*front*/
if in2=3 then do
  call topipe('rc 0 result "Window brought to front"')
  call topipe('id 0 s 66')
end
/*quit*/
if in2=4 then do
  call topipe('rc 0 result "tutrial 5 exiting"')
  exit
end
/* other*/
parse var rxcmd rx1 rx2 .
/* if no new value set return current value in result*/
if rx2='' then do
  if in2=0 then call topipe('rc 0 result "'age'"')
  if in2=1 then call topipe('rc 0 result "'sex'"')
  if in2=2 then call topipe('rc 0 result "'knowledge'"')
end
/*new value given so put it where it belongs*/
else do
  if in2=0 then age=rx2
  if in2=1 then sex=rx2
  if in2=2 then knowledge=rx2
  call topipe('rc 0')
  call update()
end
return

commodity:
/* self explanitory */
if in2='show' then call iconify(0)
if in2='hide' then call iconify(1)
if in2='kill' then exit
if in2='hotkey' then call iconify(0)
return

tooltypes:
/* find the path and name to the script */
parse source . . . resolved .
/* open a pipe to query the tooltypes */
if open(ttfh,'awnpipe:tt/xt'resolved) then do

  /* check the age tool type */
  call writeln(ttfh,'age')
  /* parse the responce to our query */
  parse value readln(ttfh) with tt1 tt2
  /* if the tooltype was found we get 'ok VALUE' back from pipe */
  /* we check for 'ok' and a valid value*/
  if tt1='ok' & datatype(tt2,'N') then  age=tt2

  /* check the sex tooltype */
  call writeln(ttfh,'sex')
  parse value readln(ttfh) with tt1 tt2
  if tt1='ok' & datatype(tt2,'N') then sex=tt2

  /* check the knowledge tooltype*/
  call writeln(ttfh,'knowledge')
  parse value readln(ttfh) with tt1 tt2
  if tt1='ok' & datatype(tt2,'N') then knowledge=tt2

  /* check the name tooltype*/
  call writeln(ttfh,'name')
  parse value readln(ttfh) with tt1 tt2
  if tt1='ok' then name=strip(tt2)

  /* check the bubble tool type */
  call writeln(ttfh,'bubble')
  parse upper value readln(ttfh) with tt1 tt2
  /* check for the 'ok' then for 'on' 'ON' or '1'*/
if tt1='OK' then do
  if (tt2='ON' | tt2=1) then helpon=1
  else helpon=0
end
  /* check the iconify tool type */
  call writeln(ttfh,'iconify')
  parse value readln(ttfh) with tt1 tt2
  /* iconify has no value so just check for the 'ok' */
  if tt1='ok' then tticonify=1

  /* check the window tool type (height is ignored for this GUI !)*/
  call writeln(ttfh,'window')
  parse value readln(ttfh) with tt1 wl wt ww .
  /* check window definition validity AND the 'ok' */
  if (tt1='ok' & datatype(wt,N) &datatype(wl,N) &datatype(ww,N) )
 then ttwindow='left' wl 'top' wt 'width' ww 'height' 1
  /*if no tool type we just open top left */
else ttwindow='tl'

  call close(ttfh)
end
return


settooltypes:
/* find the path and name to the script */
parse source . . . resolved .
/* open a pipe to read icon information */
if open(ttfh,'awnpipe:tt/xi'resolved) then do
  /*get the first 3 lines they are always present*/
  icondata=readln(ttfh)
  /* a null first line means we have no icon*/
  if icondata~='' then do
    icondata=icondata'0a'x||readln(ttfh)
    icondata=icondata'0a'x||readln(ttfh)
    /* read all tooltypes removing window and bubble tooltype*/
    do while ~eof(ttfh)
      tt1=readln(ttfh)
select
when upper(left(tt1,6))='WINDOW'  then nop
when upper(left(tt1,6))='BUBBLE'  then nop
otherwise icondata=icondata'0a'x||tt1
end
    end
  end
  /* no icon so set default */
  else icondata='4 -2147483648 -2147483648 10000'||'0a'x||'rx'||'0a0a'x
  call close(ttfh)
  /* add window info if arg1=1, if arg1=0 then don't (unsnapshot)*/
if arg(1)=1 then do
  call writeln(ca,'id 0 read')
  windowr=readln(ca)
icondata=icondata'window='windowr'0a'x
icondata=icondata'bubble='helpon'0a'x
end
  /*fh to write the new icon definition*/
  if open(ttfh,'awnpipe:tt/xs'resolved) then do
    /* write old info plus new window definition */
    call writech(ttfh,icondata)
    call close(ttfh)
  end
end
return
