call graphics_lib SetRast ( call Amiga routine )
drop ( don't need return value )
;

: QUIT.DRAWING ( -- , set termination flag )
quit-now on
;

\ (5) Set up Menu and Menu items using EZMENU system.

: MY-MENU.INIT ( -- , initialize menu )
110 menuitem-defwidth ! ( set default item width )
\ Allocate space for 4 menu items with intuitext structures
4 my-menu ezmenu.alloc
\
\ Set name of menu and position in list.
0" Choose" 0 my-menu ezmenu.setup
\
\ Define the text for each menu item.
0"Lines" 0 my-menu ezmenu.text!
0" Boxes" 1 my-menu ezmenu.text!
0" Clear" 2 my-menu ezmenu.text!
0" Quit" 3 my-menu ezmenu.text!
\
\ Set the function to call for each menu item.
\ Pull off stack in reverse order.
' quit.drawing ' clear.window
' use.boxes ' use.lines
4 0 do i my-menu ezmenu.cfa[] ! LOOP
\

\ (6) Set lines and boxes item to have exclusive checkmarks [ BINARY ] ( Use base 2 to express exclusion pattern.)
0010 0 my-menu ezmenu.exclude!
0001 1 my-menu ezmenu.exclude!
CHECKED 0 my-menu ezmanu.set.flag
[ DECIMAL ]
\
\ (7) Set Command Sequence keys for Clear and Quit.
ascii C 2 my-menu ezmenu.commseq!
ascii Q 3 my-menu ezmenu.commseq!
;

\ (8) Code for drawing lines and boxes.
: SAFE.RECT ( xl yl x2 y2 - , sort corners and draw )
>r swap >r 2sort ( sort X values )
r> r> -2sort ( sort Y values )
-r ( - x y x y )
gr.rect
;

: DRAW.NEW.XY ( x y -- , draw either a line or a box }
draw-mode @
use_lines =
IF 2dup gr.draw
ELSE
2dup last-x @ last-y @ safe.rect
THEN
last-y ! last-x !
;

: NEXT.COLOR ( -- , Cycle through colors 1,2,3 )
gr.color@ 1+ dup 3 >
IF drop 1
THEN gr.color!

\ Select random distances for random walk.
: CALC.DELTA.X ( - dx )
41 choose 20 -

: CALC.DELTA.Y ( -- dy )
21 choose 10 -
;

: WANDER.XY ( -- , random walk )
\ Add a number between -20 and +20
\ Clip to 0 and current window size.
\ Note reference to window structure.
0 max gr-curwindow @ ..@ wd_gzzwidth min
\
last-y @ calc.delta.y +
0 max gr-curwindow @ ..@ wd_gzzheight min
draw.new.xy
;

\ (9) Process IDCMP events.
: HANDLE.EVENT ( event class -- )
CASE
\ Call functions set using EXMENU.CFA[] !
MENUPICK
OF ev-last-code @ my-menu ezmenu.exec
ENDOF
\
\ Set quit flag if CLOSEBOX hit.
CLOSEWINDOW
OF quit-now on
ENDOF
\
." Unrecognized event ! " cr
ENDCASE
;

\ Draw lines or boxes until told to quit.
: LOOP.DRAW (-)
quit-now off
BEGIN
wander.xy ( do graphics )
next. color
\
gr-curwindow @ ev.getclass ?dup
IF handle.event
THEN
quit-now @
UNTIL
;

\ Declare new window structure.
NewWindow MY-WINDOW

: EZWALKER.INIT ( - , set everything up )
gr.init ( initialize graphics system )
my-window newwindow.setup ( set defaults )
\
\ Change window title using structure access word .. !
0" EZWAlker in JForth by Phil Burk" >abs
my-window ..! nw_Title
\

\ Set flags for window to allow menus.
CLOSEWINDOW MENUPICK I
my-window ..! nw_IDCMPFlags
\ Make window automatically active.
\
\ Open window based on NewWindow template
my-window gr.openwindow gr.set.curwindow
\ Initialize menu and attach to window.
my-menu.init
gr-curwindow @ my-menu SetMenuStrip ()
\ Start in middle of window
gr_xmax 2/ last-x !
gr ymax 2/ last-y !
use. lines
;
: EZWALKER.TERM ( -- , clean up menus and close window. )
gr-curwindow @ ClearMenuStrip ()
gr. closecurw
my-menu ezmenu.free
;
: EZWALKER ( -- , do it all)
EZWALKER. init
loop. draw
EZWALKER.term
;
cr ." Enter: EZWALKER to see demo . " cr