/* The code in this module is almost straight from PowerWindows example
   which they say is in no way copyrighted and free to use.  Incidentally
   it is a very good example of a clean event handler. /*

/* ******************** Enthusiastic Product Endorsement *************** */

/* I think Power Windows is the greatest Amiga programming tool on the
       market.  It makes it EASY to produce great-looking programs that
       make full use of the Intuition environment. */

/* INCLUDES ********************************************************** */

#include <exec/types.h>
#include <exec/io.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <intuition/intuition.h>
#include <libraries/dosextens.h>

   USHORT quit_flag = FALSE;

/* This is for the event handler */
void quit(object)
APTR object;
{
   quit_flag = TRUE;
}

SHORT mousex, mousey;

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;


/* get the PowerWindows 2.0 code */
#include "PatEdit.h"

   struct Window *wG;  /* we fetch the RastPort pointer from here */
   struct RastPort *rpG;
struct Process *OurTask;
struct Window  *old_pr_WindowPtr;

static char    def_name[50] = "Meps";
static char    def_dir[50] = "df1:";

#ifdef NEWSCREENSTRUCTURE
   struct Screen *sC;
   struct ViewPort vP;
#endif
main()
{
   UWORD code;
   ULONG class;
   APTR object;

   struct IntuiMessage *message;   /* the message the IDCMP sends us */
 
   IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0L);
   if (IntuitionBase == NULL)
   {
       printf("intuition is not here.  where are we?\n");
       goto cleanup1;
   }
   GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0L);

#ifdef NEWSCREENSTRUCTURE
   sC = OpenScreen(&NewScreenStructure);   /* open screen if present */
   NewWindowStructure1.Screen = sC;
#ifdef PALETTE

/* note *** the original PW example had a bug here, since it failed to
   coerce the PaletteColorCount variable to long (made a wierd bug) */
   
   LoadRGB4(&sC->ViewPort ,&Palette,(long)PaletteColorCount);
#endif
#endif

   wG = OpenWindow(&NewWindowStructure1);  /* open the window */
   if ( wG == NULL )
   {
       printf ("open window failed\n");
       goto cleanup1;
   }

   rpG = wG->RPort;    /* get a rastport pointer for the window */

#ifdef MenuList1
   SetMenuStrip(wG,&MenuList1);    /* attach any Menu */
#endif

#ifdef IntuiTextList1
   PrintIText(rpG,&IntuiTextList1,0L,0L);  /* Print the text if there is
                       any */
#endif

#ifdef BorderList1
   DrawBorder(rpG,&BorderList1,0L,0L); /* Draw the borders if there are
                       any */
#endif

#ifdef ImageList1
   DrawImage(rpG,&ImageList1,0L,0L);   /* Draw the images if there are any */
#endif

   init();
   
   do
   {
       WaitPort(wG->UserPort);
           while( (message = (struct IntuiMessage *)
               GetMsg(wG->UserPort) ) != NULL)
           {
               code = message->Code;  /* MENUNUM */
               object = message->IAddress;  /* Gadget */
               class = message->Class;
               mousex=message->MouseX;
               mousey=message->MouseY;
               ReplyMsg(message);
               if ( class == CLOSEWINDOW ) (quit_flag = TRUE);
#ifdef HANDLEEVENT
               if (( class == GADGETUP ) ||    /* Gagdets */
                   ( class == GADGETDOWN ))
                   HandleEvent(object);
#ifdef MenuList1
               if ( class == MENUPICK )    /* MenuItems */
               HandleEvent(ItemAddress(&MenuList1,(LONG)code));
#endif
#endif
               if (class==REQCLEAR) SetSize();
           }
   } while (quit_flag == FALSE);

cleanup3:
#ifdef MenuList1
   ClearMenuStrip(wG);
#endif

cleanup2:
    if (old_pr_WindowPtr) OurTask->pr_WindowPtr = old_pr_WindowPtr;

   CloseWindow(wG);
#ifdef NEWSCREENSTRUCTURE
   CloseScreen(sC);
#endif

cleanup1:
   if (GfxBase != NULL) CloseLibrary(GfxBase);
   if (IntuitionBase != NULL) CloseLibrary(IntuitionBase);
   return(0);

}
