     
    /*         An ARexx and Graphics Interface                 */
    /*         John Collett  Hamilton, NZ  1993                */
    
    /*     The accompanying article contains                   */
    /*     discussion, explanations, and comments.             */
    
    /* I dedicate this program to all Greek rocket scientists. */
 
    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(HOST, PORT)"'    
    if ~show('Ports','HOST') then address command 'WaitForPort HOST' 
    flags = 'WINDOWCLOSE+WINDOWDRAG+WINDOWDEPTH+WINDOWSIZING'
    idcmp = 'CLOSEWINDOW+MENUPICK+GADGETDOWN+GADGETUP+MOUSEBUTTONS'
    left = 20 ; top = 10 ; width = 600 ; height = 200
    call OpenWindow(HOST,left,top,width,height,idcmp,flags)
    call openport(PORT) ; call ActivateWindow(HOST)
  
    call AddMenu(HOST,'Sample menu   ')
    call AddItem(HOST,'Date and time ','%l %i')
    call AddItem(HOST,'Quit          ','%l %i')

    x = 100 ; y = 40 ; gadno = 1 
    text = 'You can edit this text' ; hsize = (length(text) + 10) * 8
    call AddGadget(HOST,x,y,gadno,text,'%l %d %g',hsize)
    
    x.2 = 100 ; y.2 = 70 ; x.3 = 280 ; y.3 = 70 ; text. = '  '
    text.2 = 'Open a CLI window' ; text.3 = 'Place\Text' 
     do g = 2 to 3
     call AddGadget(HOST,x.g,y.g,g,text.g,'%l %d') ;  end
    text.7 = ''
    do g = 4 to 7
     call AddGadget(HOST,(g-2) * 50,100,g,text.g,'%l %d') ;  end

    call SetAPen(HOST,0) ; call RectFill(HOST,248,102,256,110) 
    call SetAPen(HOST,1) 
    call Move(HOST,250,102) ; call Draw(HOST,255,102)
    call SetAPen(HOST,3)
    call Flood(HOST,1,202,100) ; call Flood(HOST,1,252,100)
    call ModifyHost(HOST,'MOUSEBUTTONS','%b %x %y')

    call SetAPen(HOST,1) ; x = 50 ; y = 120 ; call Move(HOST,x,y)
    call Draw(HOST,x+400,y) ; call Draw(HOST,x+400,y+50)
    call Draw(HOST,x,y+50) ; call Draw(HOST,x,y)
    call SetAPen(HOST,2) ; call Flood(HOST,1,x+10,y+10)
    
    do forever  
     call waitpkt(PORT) ; p = getpkt(PORT)
     if p ~== NULL() then do    
       msg = getarg(p) ;  t = reply(p, 0)
       call DisplayMessage(msg) 
       parse var msg class remainder . 
       select
         when class = 'CLOSEWINDOW' then do       
           call CloseWindow(HOST) ; exit ; end
         when class = 'MENUPICK' then call Menu()
         when class = 'GADGETUP' then call Gad()
         when class = 'SELECTDOWN' then call Mouse()
         otherwise
       end            /* of 'select'     */
     end              /* of 'then do'    */
    end               /* of 'do forever' */
    
    exit

    DisplayMessage:
     text = arg(1)
     text = text || copies(' ',76 - length(text))   
     call SetAPen(HOST,1) ; call SetBPen(HOST,3)   
     call Move(HOST,32,8) ; call Text(HOST,text)
     return
        
    Menu:
     if remainder = 0 then do
       ptxt = 'The Request() function is\being used to display this :\\'||,
              '  Date : ' || Date() || '\  Time : ' || Time('c') || '\'
       call Request(120,50,ptxt,,'Okay') ; end
     else do ; call CloseWindow(HOST) ; exit ; end
    return
    
    Gad:
    parse var msg class gadno tx
    select
      when gadno = 1 then do
        call AddGadget(HOST,100,52,8,tx,'%l %d')
        eraselen = (length(tx)+2 ) * 8 ; end
      when gadno = 2 then address command newcli  
      when gadno = 3 then do
        call SetBPen(HOST,0) ; tx.1 = 'These lines are placed'
        tx.2 = 'here by the Move' ; tx.3 = 'and Text functions.'
        do i = 1 to 3
          call Move(HOST,380,44+i*10) ; call Text(HOST,tx.i) ; end
        end
      when gadno = 4 then do 
        call DrawEllipse(HOST,110,105,20,10)
        call SetAPen(HOST,3) ; call Flood(HOST,1,94,105) 
        call SetAPen(HOST,2) ; x = 102 ; y = 101 ; call Move(HOST,x,y)
        call Draw(HOST,x+15,y) ; call Draw(HOST,x+15,y+7) 
        call Draw(HOST,x,y+7) ; call Draw(HOST,x,y) ; call SetAPen(HOST,3) 
        end
      when gadno = 5 then do
        call SetBPen(HOST,2)
        tx = 'Choose a pattern\number 0 - 15.' ; x = 370 ; y = 20
        do forever
          patt = Request(30,20,tx,'',,'No more')
          if patt = '' then leave
          call AreaDraw(HOST,x,y) ; call AreaDraw(HOST,x+190,y)
          call AreaDraw(HOST,x+190,y+80) ; call AreaDraw(HOST,x,y+80)
          call AreaEnd(HOST,256 + patt)
          end
        end
      when gadno = 6 then req = GetFile(120,40)
      when gadno = 7 then do
        opts = 'BACKPEN DRAWMODE FRONTPEN STYLE' 
       req = getFont(120,20,'topaz.font',8,,,opts) ; end
      when gadno = 8 then do
         call SetAPen(HOST,0) ; call RectFill(HOST,98,50,98+eraselen,64)
         call SetAPen(HOST,1) ; call RemoveGadget(HOST,gadno) ; end
      otherwise
      end
    return
    
    Mouse:
     parse var msg class x1 y1 .
     if x1 < 50 | x1 > 450 | y1 < 120 | y1 > 170 then return
     call Move(HOST,x1,y1)
     call waitpkt(PORT) ; p = getpkt(PORT)
     if p ~== NULL() then do                                  
       msg = getarg(p) ;  t = reply(p, 0) ;  parse var msg class x2 y2
       call Draw(HOST,x2,y2)
       end 
     return
    
             /* ------------------------- */
    