! **************************************************************************
! *  THIS CODE IS IN NO WAY COPYRIGHT BY INOVATRONICS, INC.  IN FACT, YOU  *
! *  CAN DO ANYTHING WITH IT THAT YOU WANT TO DO.  JUST REMEMBER,          *
! *  INOVATRONICS, INC. WILL BEAR ABSOLUTELY NO RESPONSIBILITY FOR THE USE,*
! *  MISUSE, INABILITY TO USE OR INABLITY TO UNDERSTAND ANY OR ALL PARTS OF*
! *  THIS CODE.  ENJOY IT IN GOOD HEALTH.                                  *
! **************************************************************************
! **************************************************************************
! *  THE PURPOSE OF THE CODE IS TO LET YOU SEE WHAT YOUR PowerWindows2     *
! *  GENERATED SOURCE CODE WILL LOOK LIKE IN A PROGRAM.  IT DEFAULTS TO    *
! *  TERMINATING WHEN THE CLOSE GADGET IS HIT, BUT IF YOU LOOK BELOW       *
! *  YOU'LL SEE HOW TO MAKE IT TERMINATE ON ANY EVENT AT ALL.              *
! *  PLEASE NOTE: IT IS DESIGNED FOR ONLY 1 WINDOW.  IT WILL LOAD A SCREEN *
! *  AND A PALETTE IF THEY ARE PRESENT, BUT ONLY WINDOW NUMBER 1 AND ITS   *
! *  PROGENY.                                                              *
! **************************************************************************

LIBRARY "pw2tb:PW2TBsupport*", "toolkit:libs/intuition*", "toolkit:libs/graphics*", "toolkit:libs/exec*", "toolkit:libs/amiga*"

DECLARE DEF ALLOCREMEMBER, PeekW, PeekL
DECLARE DEF MakeNewScreenStructure, MakeNewWindowStructure
DECLARE DEF MakeITextStructure, MakeMenuStructure
DECLARE DEF MakeMenuItemStructure, MakeGadgetStructure
DECLARE DEF MakeRequesterStructure, MakeBoolInfoStructure
DECLARE DEF MakeStringInfoStructure, MakePropInfoStructure
DECLARE DEF MakeImageStructure, MakeTextAttrStructure
DECLARE DEF MakeBorderStructure, MakeNullTString
DECLARE DEF OpenScreen, FreeRemember, OpenWindow, ReplyMsg
DECLARE DEF SetMenuStrip, CloseWindow, CloseScreen
DECLARE DEF ClearMenuStrip, WaitPort, ItemAddress, GetMsg
DECLARE DEF DrawBorder, DrawImage, PrintIText, LoadRGB4

        CALL example  !  allocate all the structures

        IF PW2EnoughMemory=1 THEN  !  proceed only if allocation succeeded

                IF NewScreenStructure<>0 THEN  !  open the screen if there
                        LET Screen = OpenScreen(NewScreenStructure)
                        IF Screen=0 THEN STOP
                        LET nothing = LoadRGB4(Screen+44, Palette, PaletteColorCount)
                        CALL PokeL(NewWindowStructure1+30,Screen)
                END IF

REM  Open the window
                LET Window = OpenWindow(NewWindowStructure1)
                IF MenuList1<>0 THEN  !  attach the Menu if present
                        LET success = SetMenuStrip(Window,MenuList1)
                END IF

REM  Draw the various goodies if they are there
                LET rp = PeekL(Window+50)  !  get the RastPort address
                IF BorderList1<>0 THEN
                        LET nothing = DrawBorder(rp,BorderList1,0,0)
                END IF
                IF ImageList1<>0 THEN
                        LET nothing = DrawImage(rp,ImageList1,0,0)
                END IF
                IF ITextList1<>0 THEN
                        LET nothing = PrintIText(rp,ITextList1,0,0)
                END IF

REM  Now start the input-event loop
                LET up = PeekL(Window+86)  !  get the UserPort address
                DO
                        LET nothing = WaitPort(up)  !  wait for an IDCMP message
                        LET message = GetMsg(up)  !  get the message
                        LET class = PeekL(message+20)  !  get the class of the message
                        LET menunum = PeekW(message+24)  !  get the message code for menu processing
                        LET address = PeekL(message+28)  !  if a Gadget, get its address
                        LET nothing = ReplyMsg(message) !  Reply the message

REM  Comment or remove the next 2 IF sections if no event-handler is used
                        IF class=256 THEN
                                LET address = ItemAddress(MenuList1,menunum)
                                CALL HandleEvent(address)
                        END IF
                        IF class=32 or class=64 THEN
                                CALL HandleEvent(address)
                        END IF

                LOOP UNTIL class=512  !  terminate at CLOSEWINDOW event

REM  Do all deinitialization
                LET nothing = ClearMenuStrip(Window)  !  clear any menu
                LET nothing = CloseWindow(Window)  !  close the window
                IF Screen<>0 THEN  !  close the screen if open
                        LET nothing = CloseScreen(Screen)
                END IF

                LET nothing = FreeRemember(PW2RememberPtr,-1)  !  free all memory used

        END IF

REM  This is the only event handler routine required by the example files
SUB quit(object)
        LET class = 512  !  simulate a CLOSEWINDOW event to terminate loop
END SUB

