#include "graph.h"

USHORT quit_flag = FALSE;

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

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;

struct Window *OpenWindow();
struct Screen *OpenScreen();
struct MenuItem *ItemAddress();

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

struct Window *wG;    /* we fetch the RastPort pointer from here */

int main(void)
{
        UWORD code;
        ULONG class;
        APTR object;

#ifdef NEWSCREENSTRUCTURE
        struct Screen *sC;
        struct ViewPort vP;
#endif
        struct RastPort *rpG;
        struct IntuiMessage *message;   /* the message the IDCMP sends us */

        initialize();
 
        IntuitionBase = (struct IntuitionBase *)OpenLibrary((UBYTE *)"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
        vP = sC->ViewPort;
        LoadRGB4(&vP,&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

        button_init();

        size_init();
        grids_init();
        minors_init();
        load_settings("s:", "GraphPaperDefaults");
        initmessage();
        RefreshGadgets(wG->FirstGadget, wG, NULL); /* needed? */

        do
        {
                WaitPort(wG->UserPort);
                while( (message = (struct IntuiMessage *)
                        GetMsg(wG->UserPort) ) != NULL)
                {
                        code = message->Code;  /* MENUNUM */
                        object = message->IAddress;  /* Gadget */
                        class = message->Class;
                        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 0
                        if ( class == MOUSEMOVE) {
                            printf("Mouse moved\n"); /* experimental */
                        }
#endif
                }
        } while (quit_flag == FALSE);

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

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

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

}
