/*   */
    signal on syntax ; signal on error
    if arg() ~= 0 then signal Finish
    if ~show('l','rexxarplib.library') then do
     check = addlib('rexxsupport.library',0,-30,0) 
     check = addlib('rexxarplib.library',0,-30,0) 
     end                                          
    address AREXX '"call CreateHost(HO, PORT)"'    
    if ~show('Ports',HO) then address command 'WaitForPort HO' 
    flags = 'BORDERLESS + WINDOWCLOSE'
    idcmp = 'CLOSEWINDOW + GADGETUP + MOUSEBUTTONS'
    height = 240
    call OpenWindow(HO,0,0,640,height,idcmp, flags) 
    call openport(PORT) ; call ActivateWindow(HO)
    call cls() ; call RefreshGadgets(HO)
    call Emboss(132,20,442,46,4,1) ; call SetBPen(HO,4) 
    call pat(150,30,"   Four Demos     Updates etc.   ",7) 
    call pat(150,42,"RexxArpLib.Library     Version 3.3",7)
    call SetBPen(HO,0) ; call FourGads() ; call APen(1)
    
    do forever 
      call GetAction()
      if class = 'GADGETUP' then call Gadget()
      else if class = 'CLOSEWINDOW' then signal 'Finish'
      end
  
    Finish:
      call CloseWindow(HO) ; exit
      syntax: say 'Syntax : ' errortext(rc) '. Line 'sigl ; signal 'Finish'
      error: say "Error " rc sigl ; signal 'Finish'

   Gadget:
    call cls()
    select
      when gadno = 1 then call ListTrial()
      when gadno = 2 then call ArgNTrial()
      when gadno = 3 then call AreaTrial()
      when gadno = 4 then call MouseTrial()
      otherwise
      end
    call Parts() ; call RefreshGadgets(HO)
    return

    pat:       
      if arg() = 4 then call APen(arg(4))
      call Move(HO,arg(1),arg(2)) ; call Text(HO,arg(3))
     return
    APen: call SetAPen(HO,arg(1)) ; return
    style: call SetFont(HO,'topaz.font',8,arg(1)) ; return
    
    Emboss: 
     if arg(5) ~= '' then do 
      call APen(arg(5))
      call RectFill(HO,arg(1),arg(2),arg(3),arg(4))
      end
     call rec(arg(1),arg(2),arg(3),arg(4),2,1)
     if arg() = 6 then 
      call rec(arg(1)-2,arg(2)-1,arg(3)+2,arg(4)+1,2,1)
     return

    rec:
     parse arg lf,up,rt,bot,e1,e2 .
     call line(lf,up,rt,up,e1) ; call line(lf,bot,rt,bot,e2)     
     do u = 0 to 1 ; call line(rt-u,up+u,rt-u,bot,e2) ; end 
     do u = 0 to 1 ; call line(lf+u,bot-u,lf+u,up,e1) ; end 
     return

    line:
     call APen(arg(5)) 
     call Move(HO,arg(1),arg(2)) ; call Draw(HO,arg(3),arg(4))
     return
    
    cls: call APen(0) ; call RectFill(HO,0,0,640,height) ; call APen(1)
         return
    
    par: 
     numlines = arg(1) ; x = arg(2) ; y = arg(3)
     do lin = 1 to numlines ; call pat(x,y+9*(lin-1),a.lin) ; end
     return

    FourGads:
     call Parts()
     call AddGadget(HO,200,70,1, " RequestList    ",'%l %d')
     call AddGadget(HO,200,110,2," Reading arg[n] ",'%l %d')
     call AddGadget(HO,200,150,3," AreaMove       ",'%l %d')
     call AddGadget(HO,200,190,4," Mouse messages ",'%l %d')
     return 

    Parts:
     call style(2)
     call pat(30,60, " A useful addition in release 3.3",1)
     call pat(30,100," Arg[n] : easy when you know how")
     call pat(30,140," Does AreaMove make a difference?")
     call pat(30,180," A detail missing from the docs") ; call style(0)
     return
 
    GetAction:    
      call waitpkt(PORT) ; p = getpkt(PORT)                       
      if p ~== NULL() then do                
        i = getarg(p) ; t = reply(p, 0) 
        parse var i class gadno rest
        end
      if class = 'CLOSEWINDOW' then signal 'Finish'
      return
    
    /*  ------------  RequestList  ------------- */
     ListTrial:
      address command 'version rexxarplib.library >Ram:v'
      o = open('v','Ram:v','r') ; vers = readln('v') ; cl = close('v')
      if index(vers,'3.3') = 0 then do
       parse var vers a1 a2 a3 
       a = "Version 3.3 of rexxarplib.library",
           "\required for RequestList function.",
           "\\You appear to have " || a2 || a3
       res = Request(50,50,a,,"Okay")
       return
       end

     call style(2)
     call pat(20,24,"Demo of RequestList Function",1) ; call style()
     call AddGadget(HO,70,36,5," Your LIBS: list ",'%l %d')
     call AddGadget(HO,240,36,6," Ditto : Sorted  ",'%l %d')
     call AddGadget(HO,410,36,7," Personal list ",'%l %d')
     call AddGadget(HO,530,height-14,8,"Go Back",'%l %d')
     listgad = 0
     do until listgad = 8
      call GetAction()
      listgad = gadno ; flag = ''
      select
       when gadno = 5 then do 
         heading = "Your library list" ; call DoList() ; end
       when gadno = 6 then do 
         heading = "Your sorted library list"
         flag = "SORT" ; call DoList() ; end
       when gadno = 7 then do 
         heading = "A sample list : An easy way to access anything "
         call DoList() ; end
       when gadno = 8 then do
         do g = 5 to 8 ; call RemoveGadget(HO,g) ; end 
         call cls()
         end 
       otherwise
       end        /* of select */
      end         /* of do until */
      return 
    
    DoList:
      call pat(20,24,copies(' ',52),0)
      call Style(2) ; call pat(20,24,heading,1) ; call style()
      if gadno = 5 | gadno = 6 then do
        num = FileList('LIBS:*',fl,,N) ; max = 0
        do f = 1 to num ; max = max(max,length(fl.f)) ; end
        res = RequestList(1,num,fl,70+110*(gadno=6),50,50+max*8,100,,flag)
        end
      else do
        fl.1="QED"; fl.2="DPaint" ; fl.3="Shell";
        fl.4="Palette" ; fl.5="Calculator"; fl.6="Calendar" 
        fl.7="FullView"; fl.8="Browser" ; fl.9="HyperBook";
        fl.10="Clipit" ; fl.11="RexxGuide"; fl.12="Gallery"
        res = RequestList(1,12,fl,410,48,130,124)
        end
      call APen(0) ; call RectFill(HO,20,0,640,12)
      call pat(100,180,copies(' ',70))
      if res ~= '' then
       call pat(100,180,"You clicked on " || res,1)
       else call pat(100,180,"You didn't click on anything!",1)
      call pat(20,24,copies(' ',52),0)
      return    
     /*  -----------   End of RequestList  ------------ */
    
     ArgNTrial:
      call cls()
      a.1 ="Getting at arg[n], with Willy's help. It's so simple, but I just"
      a.2 ="didn't know HOW.  Here's a new gadget, just for the purpose."
      a.3 ="I normally set my gadgets to return '%l %d', that is, the idcmp"
      a.4 ="class GADGETUP to identify the action as a gadget-click, and the"
      a.5 ="gadget id, to know which gadget."
      a.6 ="Here, however, I'm going to get a lot more info, in separately"
      a.7 ="numbered messages, just to show that I know how to, at last."
      call par(7,30,30) 
      a = " Try my new multi-message-sending gadget"
      argstr = '%l %d %1%x %2%y %3%e and %f %4%h and %w %5%t'
      call AddGadget(HO,530,height-14,8,"Go Back",'%l %d')
      call AddGadget(HO,100,90,9,a,argstr)
      do forever
       call waitpkt(PORT) ; p = getpkt(PORT)                       
       if p ~== NULL() then do
         i = getarg(p) 
         parse var i class gadno
         if gadno = 9 then do
          a. = '' ; a.1 = "The message returned in arg[0] was " || getarg(p)
      a.3 = "Arg[1] and arg[2] tell me that the click was at",
            "x =" getarg(p,1) || "and y =" getarg(p,2) 
      a.4="Arg[3] returns TopEdge and LeftEdge of window : " getarg(p,3)
      a.5="Arg[4] tell me that the window height and width are" getarg(p,4) 
      a.6="Arg[5] says the number of seconds since 1/1/1980(?) is",
           getarg(p,5) 
      a.8="In other situations, other info can be obtained, as appropriate."
          call par(8,30,110) ; call pat(220,194,'Thanks Willy',2)
          leave
          end
         else if gadno = 8 then leave
         else call pat(130,128,'Click on the gadget please.')  
         t = reply(p, 0)
       end
      end
      call GetAction() 
      call RemoveGadget(HO,8) ; call RemoveGadget(HO,9) ; call cls()
      return
     /*  -----------   End of Arg[n] trial  ------------ */
    
    MouseTrial:
                  /* Open an inset window */
    
    address AREXX '"call CreateHost(HO2, PORT2)"'
    if ~show('Ports',HO2) then address command 'WaitForPort HO2' 
    
    /* Set 'flags' to contain the elusive 'REPORTMOUSE' */
    
    flags = 'WINDOWCLOSE+WINDOWDRAG+REPORTMOUSE'
    idcmp = 'CLOSEWINDOW + GADGETUP + MOUSEBUTTONS + MOUSEMOVE'
    call OpenWindow(HO2,20,20,300,180,idcmp, flags,"Inset Window 1") 
    call OpenPort(PORT2) ; call ActivateWindow(HO2)
    call ModifyHost(HO2,'MOUSEBUTTONS','%l %b %x %y')
    call ModifyHost(HO2,'MOUSEMOVE','%l %x %y')
    do i = 1 to 7
      call SetAPen(HO2,i) ; x = 20 + 24*i ; y = 166
      call RectFill(HO2,x,y,x+20,10+y)
      call AddGadget(HO2,x,y,i,'  ','%l %d')
      end
    a.='' ; a.1="The inset window has REPORTMOUSE"
    a.2="as a flag, and MOUSEMOVE as one of"
    a.3="its IDCMP settings. Hold down your"
    a.4="LMB and draw with the mouse."
    a.6="Just for fun, I've included colour"
    a.7="setting gadgets, but nothing else."
    a.8="After all, it's only a demo."
    a.10="Returned x and y :"
    call APen(1) ; call par(10,340,30)
    switch = 0 ; call SetAPen(HO2,2)
    
    /* Handle user action within the inset window */

   do forever
      call waitpkt(PORT2) ; p = getpkt(PORT2)                       
      if p ~== NULL() then do                
        i = getarg(p) ; t = reply(p, 0) ; parse var i class rest
        select
         when class = 'CLOSEWINDOW' then do
          call CloseWindow(HO2) ; call ClosePort(PORT2) ; leave ; end
         when class = 'MOUSEBUTTONS' then do
          parse var i class state x y
          if state = 'SELECTDOWN' then do
            prevx = x ; prevy = y ; switch = 1 ; end
          else if state = 'SELECTUP' then switch = 0
          end
         when (class = 'MOUSEMOVE') & (switch = 1) then do
          parse var i class x y
          call Move(HO2,prevx,prevy) ; call Draw(HO2,x,y) ; prevx=x;prevy=y
          call pat(500,111,x y '  ')       /* Outer window x,y update */
          end
         when class = 'GADGETUP' then call SetAPen(HO2,rest)
         otherwise
         end         /* of select          */
        end          /* of if p ~== NULL() */
       end           /* of do forever      */
       call cls() ; call RefreshGadgets(HO)
      return
      /*  ---------  End of MouseTrial  ---------  */
    
    AreaTrial:
      a = "Eight fills, using AreaMove,        Ditto, using only"
      call pat(30,30,a)
      a = "AreaDraw, and AreaEnd               AreaDraw, and AreaEnd"
      call pat(30,40,a)
      call pat(130,180,"Time shown for each in seconds")
      call pat(130,190,"Draw your own conclusions")
      x1 = 30 ; x2 = 80 ; x3 = 130 ; y1 = 00 ; y2 = 40 ; y3 = 80
      t = time('r')
      do i = 4 to 32 by 4
       call AreaMove(HO,100+x1+i,50+y1+i) 
       call AreaDraw(HO,100+x3+i,50+y2+i)
       call AreaDraw(HO,100+x1+i,50+y3+i)
       call AreaDraw(HO,100+x2+i,50+y2+i) ; call AreaEnd(HO,4)
      end
      t2 = time('e') ; call pat(130,100,t2) ; t = time('r')
      do i = 4 to 32 by 4    
       call AreaDraw(HO,300+x1+i,50+y1+i)
       call AreaDraw(HO,300+x3+i,50+y2+i)
       call AreaDraw(HO,300+x1+i,50+y3+i)
       call AreaDraw(HO,300+x2+i,50+y2+i) ; call AreaEnd(HO,4)
      end
      t2 = time('e') ; call pat(330,100,t2)
      call AddGadget(HO,530,height-14,8,"Go Back",'%l %d')
      call GetAction() ; call cls() ; call RemoveGadget(HO,8)
      call RefreshGadgets(HO)
    return