#include <graphics/display.h>
#include <libraries/dosextens.h>
#include <libraries/diskfont.h>
#include <exec/exec.h>
#include <intuition/intuitionbase.h>
#include <graphics/regions.h>
#include <devices/keymap.h>
#include <stdio.h>
#include <dos.h>
#include <workbench/startup.h>
#include <graphics/gfxmacros.h>
#include <graphics/gfxbase.h>
#include <math.h>

#include "struct.h"
#include "plot.h"

extern int MAXVERT;

struct Window *OpenWindow();
struct InputEvent *Intuition();
struct Screen *OpenScreen();
int WrtPlt();

struct RastPort *rp;
struct ViewPort *vp;
struct Window *window;
struct IntuiMessage *msg;
struct Screen *screen;

struct NewScreen newscreen = {
        0,0,                    /* start pos.*/
        MAXHORIZ,MaxVERT,4,     /* width height depth (4 bit planes) */
        2,1,                    /* detail pen, block pen */
        HIRES | LACE,           /* viewing mode (640x400 interlaced) */
        CUSTOMSCREEN,           /* screen type */
        NULL,                   /* font */
        "MultiPlot XLN            © T Mooney 1986,1988 and © AG Baxter 1989",            /* screen title */
        NULL                    /* gadget pointer */
        };

struct NewWindow newwindow = {
        0,0,                                    /* Starting corner */
        MAXHORIZ,MaxVERT,                       /* Width, height */
        1,14,                                   /* detail, block pens */
        MENUPICK | MOUSEMOVE | MOUSEBUTTONS,    /* IDCMP flags */
        ACTIVATE | BORDERLESS | NOCAREREFRESH | REPORTMOUSE,
        NULL,                                   /* Pointer to first gadget */
        NULL,                                   /* Pointer to checkmark */
        NULL,                                   /* title */
        NULL,                                   /* screen pointer */
        NULL,                                   /* bitmap pointer */
        0,0,0,0,                                /* sizing limits */
        CUSTOMSCREEN                            /* type of screen */
        };


void InitColors()
{
   SetRGB4(vp,0,15,10,5);   SetRGB4(vp,1,0,0,0);    SetRGB4(vp,2,15,15,15);
   SetRGB4(vp,3,15,0,0);    SetRGB4(vp,4,0,0,0);    SetRGB4(vp,5,15,0,0);
   SetRGB4(vp,6,0,12,0);    SetRGB4(vp,7,0,5,15);   SetRGB4(vp,8,13,13,0);
   SetRGB4(vp,9,0,13,13);   SetRGB4(vp,10,15,0,15); SetRGB4(vp,11,10,10,0);
   SetRGB4(vp,12,8,8,8);    SetRGB4(vp,13,5,5,0);   SetRGB4(vp,14,0,10,15);
   SetRGB4(vp,15,15,8,0);
}


struct IntuiText MText1 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "White BG",     /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Whitebg = {
        NULL,   /* next MenuItem structure */
        0,45,   /* XY of Item hitbox relative to TopLeft of parent hitbox */
        120,8,  /* hit box width and height */
        ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText1,  /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        'w',    /* alternate command-key */
        NULL,   /* SubItem list */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define MI_WHITEBG 5

struct IntuiText MText2 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "Axes", /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Axes = {
        &Whitebg,     /* next MenuItem structure */
        0,36,   /* XY of Item hitbox relative to TopLeft of parent hitbox */
        120,8,  /* hit box width and height */
        ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText2,  /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        'a',    /* alternate command-key */
        NULL,   /* SubItem list */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define MI_AXES 4

struct IntuiText MText3 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "Grid", /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Grid = {
        &Axes,     /* next MenuItem structure */
        0,27,   /* XY of Item hitbox relative to TopLeft of parent hitbox */
        120,8,  /* hit box width and height */
        ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText3,  /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        's',    /* alternate command-key */
        NULL,   /* SubItem list */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define MI_GRID 3

struct IntuiText MText4 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "Cross Hair",   /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Crosshair = {
        &Grid,     /* next MenuItem structure */
        0,18,   /* XY of Item hitbox relative to TopLeft of parent hitbox */
        120,8,  /* hit box width and height */
        ITEMTEXT+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText4,  /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        NULL,   /* alternate command-key */
        NULL,   /* SubItem list */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define MI_CROSSHAIR 2

struct IntuiText MText5 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "Get How To",   /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Gethowto = {
        &Crosshair,     /* next MenuItem structure */
        0,9,    /* XY of Item hitbox relative to TopLeft of parent hitbox */
        120,8,  /* hit box width and height */
        ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText5,  /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        'g',    /* alternate command-key */
        NULL,   /* SubItem list */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define MI_GETHOWTO 1

struct IntuiText MText6 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "Redraw",       /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Redraw = {
        &Gethowto,     /* next MenuItem structure */
        0,0,    /* XY of Item hitbox relative to TopLeft of parent hitbox */
        120,8,  /* hit box width and height */
        ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText6,  /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        'r',    /* alternate command-key */
        NULL,   /* SubItem list */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define MI_REDRAW 0

struct Menu Options = {
        NULL,   /* next Menu structure */
        137,0,  /* XY origin of Menu hit box relative to screen TopLeft */
        75,0,   /* Menu hit box width and height */
        MENUENABLED,    /* Menu flags */
        "Options",      /* text of Menu name */
        &Redraw      /* MenuItem linked list pointer */
};
#define M_OPTIONS 2

struct IntuiText MText7 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,   /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "XY Region",    /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Xyregion = {
        NULL,   /* next SubItem structure */
        97,24,   /* XY of Item hitbox relative to TopLeft of parent hitbox */
        72,8,   /* hit box width and height */
        ITEMTEXT+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText7,  /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        NULL,   /* alternate command-key */
        NULL,   /* no SubItem list for SubItems */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define SI_XYREGION 2

UBYTE YTextOff[]="Y Region";
UBYTE YTextOn[]= "YLock on";

struct IntuiText MText8 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,   /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        YTextOff,     /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Yregion = {
        &Xyregion,      /* next SubItem structure */
        97,16,   /* XY of Item hitbox relative to TopLeft of parent hitbox */
        72,8,   /* hit box width and height */
        ITEMTEXT+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText8,  /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        NULL,   /* alternate command-key */
        NULL,   /* no SubItem list for SubItems */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define SI_YREGION 1

UBYTE XTextOff[]="X Region";
UBYTE XTextOn[]= "XLock on";

struct IntuiText MText9 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,   /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        XTextOff,     /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Xregion = {
        &Yregion,      /* next SubItem structure */
        97,8,  /* XY of Item hitbox relative to TopLeft of parent hitbox */
        72,8,   /* hit box width and height */
        ITEMTEXT+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText9,  /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        NULL,   /* alternate command-key */
        NULL,   /* no SubItem list for SubItems */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define SI_XREGION 0

struct IntuiText MText10 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "Locks",        /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Locks = {
        NULL,   /* next MenuItem structure */
        0,27,   /* XY of Item hitbox relative to TopLeft of parent hitbox */
        112,8,  /* hit box width and height */
        ITEMTEXT+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText10, /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        NULL,   /* alternate command-key */
        &Xregion,      /* SubItem list */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define MI_LOCKS 3

struct IntuiText MText11 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "Full Plot",    /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Fullplot = {
        &Locks,    /* next MenuItem structure */
        0,18,   /* XY of Item hitbox relative to TopLeft of parent hitbox */
        112,8,  /* hit box width and height */
        ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText11, /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        'f',    /* alternate command-key */
        NULL,   /* SubItem list */
        MENUNULL        /* filled in by Intuition for drag selections */
};

#define MI_FULLPLOT 2

struct IntuiText MText12 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "Slide",        /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Slide = {
        &Fullplot,     /* next MenuItem structure */
        0,9,    /* XY of Item hitbox relative to TopLeft of parent hitbox */
        112,8,  /* hit box width and height */
        ITEMTEXT+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText12, /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        NULL,   /* alternate command-key */
        NULL,   /* SubItem list */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define MI_SLIDE 1

struct IntuiText MText13 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "Zoom", /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Zoom = {
        &Slide,     /* next MenuItem structure */
        0,0,    /* XY of Item hitbox relative to TopLeft of parent hitbox */
        112,8,  /* hit box width and height */
        ITEMTEXT+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText13, /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        NULL,   /* alternate command-key */
        NULL,   /* SubItem list */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define MI_ZOOM 0

struct Menu Edit = {
        &Options, /* next Menu structure */
        82,0,   /* XY origin of Menu hit box relative to screen TopLeft */
        48,0,   /* Menu hit box width and height */
        MENUENABLED,    /* Menu flags */
        "Edit", /* text of Menu name */
        &Zoom      /* MenuItem linked list pointer */
};
#define M_EDIT 1

struct IntuiText MText14 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "Quit", /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Quit = {
        NULL,   /* next MenuItem structure */
        0,27,   /* XY of Item hitbox relative to TopLeft of parent hitbox */
        80,8,   /* hit box width and height */
        ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText14, /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        'x',    /* alternate command-key */
        NULL,   /* SubItem list */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define MI_QUIT 3

struct IntuiText MText15 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "Print",        /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Print = {
        &Quit,    /* next MenuItem structure */
        0,18,   /* XY of Item hitbox relative to TopLeft of parent hitbox */
        80,8,   /* hit box width and height */
        ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText15, /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        'p',    /* alternate command-key */
        NULL,   /* SubItem list */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define MI_PRINT 2

struct IntuiText MText16 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "IntroCAD",     /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Introcad = {
        NULL,   /* next SubItem structure */
        65,40,  /* XY of Item hitbox relative to TopLeft of parent hitbox */
        104,8,  /* hit box width and height */
        ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText16, /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        'c',    /* alternate command-key */
        NULL,   /* no SubItem list for SubItems */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define SI_ICAD 4

struct IntuiText MText17 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "mCAD", /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Mcad = {
        &Introcad,      /* next SubItem structure */
        65,32,  /* XY of Item hitbox relative to TopLeft of parent hitbox */
        104,8,  /* hit box width and height */
        ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText17, /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        'm',    /* alternate command-key */
        NULL,   /* no SubItem list for SubItems */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define SI_MCAD 3

struct IntuiText MText18 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "Draw", /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Plot = {
        &Mcad,      /* next SubItem structure */
        65,24,   /* XY of Item hitbox relative to TopLeft of parent hitbox */
        104,8,  /* hit box width and height */
        ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText18, /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        'd',    /* alternate command-key */
        NULL,   /* no SubItem list for SubItems */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define SI_DRAW 2

struct IntuiText MText19 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "HPLG", /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Hplg = {
        &Plot,      /* next SubItem structure */
        65,16,   /* XY of Item hitbox relative to TopLeft of parent hitbox */
        104,8,  /* hit box width and height */
        ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText19, /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        'h',    /* alternate command-key */
        NULL,   /* no SubItem list for SubItems */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define SI_HPLG 1

struct IntuiText MText20 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "IFF",  /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Iff = {
        &Hplg,      /* next SubItem structure */
        65,8,  /* XY of Item hitbox relative to TopLeft of parent hitbox */
        104,8,  /* hit box width and height */
        ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText20, /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        'i',    /* alternate command-key */
        NULL,   /* no SubItem list for SubItems */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define SI_IFF 0

struct IntuiText MText21 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "Save", /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem Save = {
        &Print,    /* next MenuItem structure */
        0,9,    /* XY of Item hitbox relative to TopLeft of parent hitbox */
        80,8,   /* hit box width and height */
        ITEMTEXT+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText21, /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        NULL,   /* alternate command-key */
        &Iff,      /* SubItem list */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define MI_SAVE 1

struct IntuiText MText22 = {
        3,1,COMPLEMENT, /* front and back text pens, drawmode and fill byte */
        0,0,    /* XY origin relative to container TopLeft */
        NULL,   /* font pointer or NULL for default */
        "New",  /* pointer to text */
        NULL    /* next IntuiText structure */
};

struct MenuItem New = {
        &Save,    /* next MenuItem structure */
        0,0,    /* XY of Item hitbox relative to TopLeft of parent hitbox */
        80,8,   /* hit box width and height */
        ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,  /* Item flags */
        0,      /* each bit mutually-excludes a same-level Item */
        (APTR)&MText22, /* Item render  (IntuiText or Image or NULL) */
        NULL,   /* Select render */
        'n',    /* alternate command-key */
        NULL,   /* SubItem list */
        MENUNULL        /* filled in by Intuition for drag selections */
};
#define MI_NEW 0
struct Menu Project = {
        &Edit, /* next Menu structure */
        0,0,    /* XY origin of Menu hit box relative to screen TopLeft */
        75,0,   /* Menu hit box width and height */
        MENUENABLED,    /* Menu flags */
        "Project",      /* text of Menu name */
        &New     /* MenuItem linked list pointer */
};
#define M_PROJECT 0


/********/
InitWind()
{
   newwindow.Screen = screen; window = OpenWindow(&newwindow);
   rp = window->RPort; vp = &window->WScreen->ViewPort;
   SetDrMd(rp,JAM1); SetAPen(rp,1);
   InitColors();
   SetMenuStrip(window,&Project); ShowTitle(screen,FALSE);
   return(1);
}


/************/
void CleanUp()
{ClearMenuStrip(window); CloseWindow(window);}


/************************/
void AttrOnOff(Pict, item)
struct Pict *Pict;
int item;
{
   ClearMenuStrip(window);
   switch(item) {
   case SI_XREGION:
      MText9.IText = (Pict->XRegionLock ? XTextOn : XTextOff);
      break;
   case SI_YREGION:
      MText8.IText = (Pict->YRegionLock ? YTextOn : YTextOff);
      break;
   default: break;
   }
   SetMenuStrip(window, &Project);
}


/**************************************************************************/
void PToU();

USHORT InvisPtr[] = {
   0x0,0x0, 0x0,0x0, 0x0,0x0, 0x0,0x0, 0x0,0x0, 0x0,0x0, 0x0,0x0,
   0x0,0x0, 0x0,0x0, 0x0,0x0, 0x0,0x0};

static short XHair = FALSE;
static short WhiteBG = FALSE;

/*****************/
void PutXHair(x,y)
short x,y;
{Move(rp,0,y); Draw(rp,MAXHORIZ,y); Move(rp,x,0); Draw(rp,x,MAXVERT);}

/**************************/
void GetXHair(x,y)
short *x, *y;
{SetPointer(window,InvisPtr,9,9,0,0); SetDrMd(rp,COMPLEMENT); PutXHair(*x,*y);}

/***********************/
void KillXHair(x,y)
short x,y;
{ClearPointer(window); PutXHair(x,y); SetDrMd(rp,JAM1);}

/*****************************/
void RubberBox(x1, y1, x2, y2)
short *x1, *y1, *x2, *y2;
{
   Move(rp,x1,y1);
   if (XHair) {Draw(rp,x1,y2); Move(rp,x1,y1); Draw(rp,x2,y1);}
   else {Draw(rp,x1,y2); Draw(rp,x2,y2); Draw(rp,x2,y1); Draw(rp,x1,y1);}
}

#define NO_OBJECT 0
#define REGION 1
#define LINE 2
#define BOX 3
#define SLIDE 4
extern USHORT chip WaitSprite[];
extern int KEEP_GOING;
extern char filename[150];
extern char StartDir[150];


/*************/
CheckUser(Pict)
struct Pict *Pict;
{
   ULONG class;
   USHORT code;
   int retval = NOACTION, MouseMoved;
   int screensave();
   short x, y, x0, y0, x1, y1;
   static short RubberObj = NO_OBJECT;
   short RubberBand = FALSE;
   FFP XMin, YMin, XMax, YMax;
   struct PlotRegion *Reg;
   struct Process  *OurTask;
   struct Window   *old_pr_WindowPtr;
   char plotname[150], def_drive[150], def_path[100], def_node[30],def_extn[20];
   char Command[180];
   char convert_tool[150];  /* string descibing path to current directory  */
                            /* when started from workbench to locate tools */

   AttrOnOff(Pict, SI_XREGION);
   AttrOnOff(Pict, SI_YREGION);
   x = y = 0;
   if (XHair) {SetDrMd(rp, COMPLEMENT); GetXHair(&x,&y); x1 = x; y1 = y;}
   if (WhiteBG) SetRGB4(vp,0,15,15,15);
   do {
      Wait(1 << window->UserPort->mp_SigBit);

      while (msg = (struct IntuiMessage *) GetMsg(window->UserPort) ) {
         class = msg->Class;
         MouseMoved = (class == MOUSEMOVE);
         code = msg->Code; x = msg->MouseX; y = msg->MouseY;
         ReplyMsg(msg);

         if ((class == MENUPICK) && (code != MENUNULL)) {
            switch (MENUNUM(code)) {
            case M_PROJECT:
               switch(ITEMNUM(code)) {
               case MI_QUIT:        KEEP_GOING=FALSE; retval = QUIT; break;
               case MI_PRINT:       WrtPlt(Pict,FALSE); break;
               case MI_NEW:         KEEP_GOING=TRUE; retval = QUIT; break;
               case MI_SAVE:
                  switch (SUBNUM(code)) {
                  case SI_HPLG:
                     WrtPlt(Pict,TRUE);
                     break;
                  case SI_MCAD:
                     SetPointer(window,WaitSprite,26,14,-4,-4);
                     To_mCAD(Pict,TRUE);
                     ClearPointer(window);
                     break;
                  case SI_IFF:
                     SetPointer(window,WaitSprite,26,14,-4,-4);
                     screensave();
                     ClearPointer(window);
                     break;
                  case SI_DRAW:
                     SetPointer(window,WaitSprite,26,14,-4,-4);
                     strcpy(plotname,filename);
                     strsfn(plotname,def_drive,def_path,def_node,def_extn);
                     strcat(def_drive,def_path);
                     strcat(def_node,".draw");

                     OurTask = (struct Process *)FindTask(0L);
                     old_pr_WindowPtr = (struct Window *)OurTask->pr_WindowPtr;
                     OurTask->pr_WindowPtr = (APTR)window;
                     if (get_fname(window,screen,"Save File As...",def_node,def_drive)==NULL)
                          {
                             OurTask->pr_WindowPtr = (APTR)old_pr_WindowPtr;
                             break;
                           }

                     OurTask->pr_WindowPtr = (APTR)old_pr_WindowPtr;
                     strmfp(plotname,def_drive,def_node);
                     if (To_mCAD(Pict,FALSE))
                       {
                         strmfp(convert_tool,StartDir,"plot2draw");
                         sprintf(Command,"\"%s\" t:tempfile \"%s\"",convert_tool,plotname);
                         Execute(Command,0,0);
                         DeleteFile("t:tempfile");
                       }
                     ClearPointer(window);
                     break;
                  case SI_ICAD:


                     SetPointer(window,WaitSprite,26,14,-4,-4);
                     strcpy(plotname,filename);
                     strsfn(plotname,def_drive,def_path,def_node,def_extn);
                     strcat(def_drive,def_path);
                     strcat(def_node,".iCAD");

                     OurTask = (struct Process *)FindTask(0L);
                     old_pr_WindowPtr = (struct Window *)OurTask->pr_WindowPtr;
                     OurTask->pr_WindowPtr = (APTR)window;
                     if (get_fname(window,screen,"Save File As...",def_node,def_drive)==NULL)
                          {
                             OurTask->pr_WindowPtr = (APTR)old_pr_WindowPtr;
                             break;
                           }
                     OurTask->pr_WindowPtr = (APTR)old_pr_WindowPtr;
                     strmfp(plotname,def_drive,def_node);
                     if (To_mCAD(Pict,FALSE))
                       {
                         strmfp(convert_tool,StartDir,"txt_2_icad");
                         sprintf(Command,"\"%s\" t:tempfile \"%s\"",convert_tool,plotname);
                         Execute(Command,0,0);
                         DeleteFile("t:tempfile");
                       }
                     ClearPointer(window);
                     break;
                  default: break;
                  }
                  break;
               default: break;
               }
               break;

            case M_EDIT:
               switch (ITEMNUM(code)) {
               case MI_ZOOM:        RubberObj = REGION; break;
               case MI_SLIDE:       RubberObj = SLIDE; break;
               case MI_FULLPLOT:    retval = GETDATALIMITS; break;
               case MI_LOCKS:
                  switch (SUBNUM(code)) {
                  case SI_XYREGION:
                     Pict->XRegionLock=Pict->YRegionLock=TRUE;
                     AttrOnOff(Pict, SI_XREGION);
                     AttrOnOff(Pict, SI_YREGION);
                     break;
                  case SI_YREGION:
                     Pict->YRegionLock=!Pict->YRegionLock;
                     AttrOnOff(Pict, SI_YREGION);
                     break;
                  case SI_XREGION:
                     Pict->XRegionLock=!Pict->XRegionLock;
                     AttrOnOff(Pict, SI_XREGION);
                     break;
                  default: break;
                  }
                  break;
               default: break;
               }
               break;
            case M_OPTIONS:
               switch (ITEMNUM(code)) {
               case MI_REDRAW:      retval = REPLOT; break;
               case MI_CROSSHAIR:
                  if (XHair = !XHair) {GetXHair(&x,&y); x1=x; y1=y;}
                  else KillXHair(x1,y1);
                  break;
               case MI_GETHOWTO:     retval = GETHOWTO; break;
               case MI_GRID:        Pict->Grid = !Pict->Grid; retval=REPLOT; break;
               case MI_AXES:        Pict->Axes = !Pict->Axes; retval=REPLOT; break;
               case MI_WHITEBG:
                  if (WhiteBG = !WhiteBG) SetRGB4(vp,0,15,15,15);
                  else SetRGB4(vp,0,15,10,5);
                  break;
               default: break;
               }
               break;

            default: break;

            } /* switch */
         } /* if ((class == MENUPICK) ...*/
         else if (class == MOUSEBUTTONS) {
               switch (code) {
               case SELECTUP: break;
               case SELECTDOWN:
                  if (RubberObj && !RubberBand) { /* BEGIN RUBBERBAND */
                     RubberBand = TRUE; x0 = x1 = x; y0 = y1 = y;
                     SetDrMd(rp,COMPLEMENT); window->Flags |= RMBTRAP;
                     if (RubberObj == REGION) {
                        RubberBox(x0,y0,x,y);
                     }
                     else if (RubberObj == SLIDE) {
                        Move(rp,x,y); Draw(rp,x,y);
                     }
                  }
                  else { /* END RUBBERBAND */
                     RubberBand = FALSE;
                     if (RubberObj == REGION) {
                        RubberBox(x0,y0,x1,y1);
                        Reg = Pict->CurrReg;
                        if ((abs(x-x0) > 2) && (abs(y-y0) > 2)) {
                           /* zoom in to new region */
                           PToU(Pict,min(x0,x),MAXVERT-max(y0,y),&XMin,&YMin);
                           PToU(Pict,max(x0,x),MAXVERT-min(y0,y),&XMax,&YMax);
                        }
                        else {
                           /* zoom out X 2 */
                           XMin = Reg->XMin - (Reg->XMax - Reg->XMin)/2;
                           XMax = Reg->XMax + (Reg->XMin - XMin);
                           YMin = Reg->YMin - (Reg->YMax - Reg->YMin)/2;
                           YMax = Reg->YMax + (Reg->YMin - YMin);
                        }
                        if (!Pict->XRegionLock)
                           {Reg->XMin = XMin; Reg->XMax = XMax;}
                        if (!Pict->YRegionLock)
                           {Reg->YMin = YMin; Reg->YMax = YMax;}
                        retval = REPLOT;
                     }
                     else if (RubberObj == SLIDE) {
                        Move(rp,x0,y0); Draw(rp,x1,y1);
                        PToU(Pict,x0,MAXVERT-y0,&XMin,&YMin);
                        PToU(Pict,x,MAXVERT-y,&XMax,&YMax);
                        Reg = Pict->CurrReg;
                        if (!Pict->XRegionLock)
                           {Reg->XMin += XMin-XMax; Reg->XMax += XMin-XMax;}
                        if (!Pict->YRegionLock)
                           {Reg->YMin += YMin-YMax; Reg->YMax += YMin-YMax;}
                        retval = REPLOT;
                     }
                     if (!XHair) SetDrMd(rp,JAM1);
                     window->Flags &= 0xFFFFFFFF ^ RMBTRAP;
                  }
                  break;
               case MENUDOWN: /* ABORT RUBBERBAND */
                  RubberBand = FALSE; window->Flags &= 0xFFFFFFFF ^ RMBTRAP;
                  if (RubberObj == REGION) {
                     RubberBox(x0,y0,x1,y1);
                  }
                  else if (RubberObj == SLIDE) {
                     Move(rp,x0,y0); Draw(rp,x1,y1);
                  }
                  break;
               default: break;
            } /* switch (code) */
            break;
         } /* else if (class == MENUBUTTONS) */
      } /* while */

      if (MouseMoved && ((x1!=x) || (y1!=y)) ) {
         if (XHair)
               {PutXHair(x1,y1); PutXHair(x,y);}
         if (RubberBand) {
            if (RubberObj == REGION)
               {RubberBox(x0,y0,x1,y1); RubberBox(x0,y0,x,y);}
            else if (RubberObj == SLIDE)
               {Move(rp,x0,y0); Draw(rp,x1,y1); Move(rp,x0,y0); Draw(rp,x,y);}
         }
         x1 = x; y1 = y;
      }
   } while(retval == NOACTION);

   SetDrMd(rp, JAM1);
   return(retval);
}
