/* -	-	mandy -	-	*/
#define xedvars 0

#include "stdio.h"
#include "types.h"
#include "math.h"
#include "intuition/intuition.h"
#include "intuition/screens.h"
#include "graphics/gfxbase.h"
#include "graphics/text.h"
#include "libraries/dos.h"
#include "libraries/dosextens.h"
#include "exec/memory.h"

/* Define the size of the screen */
#define SC_HEIGHT 200
#define SC_WIDTH	320
#define T_WIDTH	640
#define SC_TOP		0

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct NewScreen gfxscn;
struct Screen *gfxscreen;
struct NewWindow frame;
struct Window *gxwin, *txwin;
struct IntuiMessage *txmsg,*gxmsg;
struct RastPort *gxrp, *txrp;
struct MsgPort *xport;
struct IOStdReq *iport;
struct Device *ConsoleDevice;

struct InputEvent ievent = /*raw key input event*/
{
	NULL,					/*next event*/
	IECLASS_RAWKEY,	/*class*/
	0,0,0
};

int MaxStLen = 256;
#define MaxSt1 256  /*maxstlen */

int TOP = 1; /*Cls*/
int BOT = 23;
int PW = 77;
int LLen; /* X Y */
int GX, GY;
ulong COLOR=3, BCOLOR=0; /* 0 brn 1 ylo 2 bk 3 grn*/
ulong Tstyle;
char buff[10];
char func;
char Nstr[82],Path[258];
char *Msg;

/* -	-	-	-	-	-	-	*/
/* back to front */
#define EDIT_MENU 0

#define PLOT_ITEM 0
#define FAST_ITEM 1
#define CONTR_ITEM 2
#define QUIT_ITEM 3

static struct IntuiText quit_text =
{
   15,8,JAM1,0,1,NULL,"Quit",NULL
};

struct MenuItem quit_item =
{
	NULL,
	0,31,75,10,
	ITEMTEXT |ITEMENABLED |HIGHCOMP,
	0,(APTR) &quit_text,
	NULL,0,NULL,MENUNULL
};

static struct IntuiText contr_text =
{
   15,8,JAM1,0,1,NULL,"Controls",NULL
};

struct MenuItem contr_item =
{
	&quit_item,
	0,21,75,10,
	ITEMTEXT |ITEMENABLED |HIGHCOMP,
	0,(APTR) &contr_text,
	NULL,0,NULL,MENUNULL
};

static struct IntuiText fast_text =
{
   15,8,JAM1,LOWCHECKWIDTH,1,NULL,"fast",NULL
};

struct MenuItem fast_item =
{
	&contr_item,
	0,11,75,10,
	ITEMTEXT |ITEMENABLED |CHECKIT |HIGHCOMP,
	1,(APTR) &fast_text,
	NULL,0,NULL,MENUNULL
};

static struct IntuiText plot_text =
{
   15,8,JAM1,LOWCHECKWIDTH,1,NULL,"Plot",NULL
};

struct MenuItem plot_item =
{
	&fast_item,
	0,1,75,10,
	ITEMTEXT |ITEMENABLED |CHECKIT |HIGHCOMP,
	2,(APTR) &plot_text,
	NULL,0,NULL,MENUNULL
};

struct Menu edit_menu =
{
	NULL,
	0,0,75,0,
	MENUENABLED,
   "Mandelbrot",
	&plot_item
};
/* -	-	-	-	-	-	-	*/

short colours[16] =
{
	/* RGB */
	0x0000, 0x0DDD, 0x0a80, 0x0EE0, 0x0800, 0x0F00, 0x0808, 0x0E0E,
	0x0008, 0x000F, 0x0088, 0x00EE, 0x0071, 0x00e0, 0x0555, 0x0888
};
#define BLK 0
#define BRN 2
#define YLO 3
#define BLU 9
#define GRN 13

BOOL OpenGFXScreen()
{
	gfxscn.LeftEdge = 0;
	gfxscn.TopEdge	 = 0;
	gfxscn.Width	 = SC_WIDTH;
	gfxscn.Height	 = SC_HEIGHT;
	gfxscn.Depth	 = 4;
	gfxscn.DetailPen= BRN;
	gfxscn.BlockPen = YLO;
	gfxscn.ViewModes= 0;
	gfxscn.Type		 = 0;
	gfxscn.Font		 = 0;
   gfxscn.DefaultTitle = "";
	gfxscn.Gadgets	 = 0;
	gfxscreen = (struct Screen *)OpenScreen(&gfxscn);
	return(BOOL)(gfxscreen != 0);
}

BOOL OpenGFXWindow()
{
	frame.LeftEdge = 0;
	frame.TopEdge	= SC_TOP;
	frame.Width		= SC_WIDTH;
	frame.Height	= SC_HEIGHT-SC_TOP;
	frame.DetailPen= BRN;
	frame.BlockPen = YLO;
	frame.IDCMPFlags = MENUPICK| GADGETDOWN| GADGETUP| REQCLEAR| REQSET;
	frame.Flags		= WINDOWDRAG| BORDERLESS| SMART_REFRESH| ACTIVATE;
   frame.Title    = "MandelBrot";
	frame.Screen	= gfxscreen;
	frame.MinWidth = 0;
	frame.MaxWidth = -1;
	frame.MinHeight= 0;
	frame.MaxHeight= -1;
	frame.Type		= CUSTOMSCREEN;
	gxwin = (struct Window *)OpenWindow(&frame);
	return(BOOL)(gxwin != 0);
}
BOOL OpenTextWindow()
{
	frame.LeftEdge = 0;
	frame.TopEdge	= 0;
	frame.Width		= T_WIDTH;
	frame.Height	= SC_HEIGHT-SC_TOP;
	frame.DetailPen= 0;
	frame.BlockPen = 1;
	frame.IDCMPFlags = RAWKEY;
	frame.Flags		= WINDOWDRAG| WINDOWSIZING| SMART_REFRESH| ACTIVATE;
   frame.Title    = "VARIABLES";
	frame.Screen	= NULL;
	frame.MinWidth = 0;
	frame.MaxWidth = -1;
	frame.MinHeight= 0;
	frame.MaxHeight= -1;
	frame.Type		= WBENCHSCREEN;
	txwin = (struct Window *)OpenWindow(&frame);
	return(BOOL)(txwin != 0);
}

ClGs()
{
	SetAPen(gxrp,0L);
	RectFill(gxrp,0L,10L,(long)SC_WIDTH,(long)SC_HEIGHT);
}

#include "SCREEN.P";

/* - - SCREEN - - */
char GetKey() /* keys convert to WS control codes. del = ^G */
{
	int C = -1, class, len;
	do {
		if(1 << txwin->UserPort->mp_SigBit)
		{
			while(!(txmsg = (struct IntuiMessage *)GetMsg(txwin->UserPort))) { ; }
		}
		class = txmsg->Class;
		if(class != RAWKEY) ReplyMsg((struct Message *)txmsg);
		else
		{
			if(txmsg->Code & IECODE_UP_PREFIX) C = -1;
			else /* convert arrow keys et al to char code */
			{
				buff[0] = buff[1] = buff[2] = 0;
				ievent.ie_Code = txmsg->Code;
				ievent.ie_Qualifier = txmsg->Qualifier;
				ievent.ie_position.ie_addr = * (APTR *)txmsg->IAddress;
				len = (RawKeyConvert(&ievent,buff,10,NULL));
				ReplyMsg((struct Message *) txmsg);

				if(len > 0)
				{
					if(len == 1) { C = buff[0]; if(C == 0x7f) C = 7; }
               else                              /*"DEL .. ^code*/
					{
						C = buff[1];
						if(buff[2] == '~') /* help & function keys */
						{
							func = '?';
							if((C >= '0') && (C <= '9')) func = C + 1;
							C = 31;
						}
						switch(C) /* arrow keys */
						{
						case 'A': C = 5;	break; /*^E ^*/
						case 'B': C = 24; break; /*^X v*/
						case 'C': C = 4;	break; /*^D >*/
						case 'D': C = 19; break; /*^S <*/
						case 'T': C = 18; break; /*^R ^^*/
						case 'S': C = 3;	break; /*^C vv*/
						case ' ':
						{
							if(buff[2] == 'A') C = 1; /*^A <<*/
							if(buff[2] == '@') C = 6; /*^F >>*/ break;
						}
						}
					} /*len >1*/
				}/*len >0*/
			}/*upkey*/
		}
	} while(C == -1);
	return(C);
}

SetAttr(S) /* Normal = 0 Uline = 1/2 Bold = 3/4 italic = 5/6 */
int S;
{
	ulong en;
	switch(S)
	{
	case 0: Tstyle = 0; break;
	case 1: Tstyle = Tstyle | 1; break;
	case 2: Tstyle = Tstyle & 6; break;
	case 3: Tstyle = Tstyle | 2; break;
	case 4: Tstyle = Tstyle & 5; break;
	case 5: Tstyle = Tstyle | 4; break;
	case 6: Tstyle = Tstyle & 3; break;
	}
	en = AskSoftStyle(txrp); SetSoftStyle(txrp,Tstyle,en);
}

SetCols() /* 0 brn 1 ylo 2 bk 3 grn*/
{	SetAPen(txrp,COLOR); SetBPen(txrp,BCOLOR); }
YR()
{	COLOR = 1; BCOLOR = 0; SetCols(); SetAttr(0); }
UYR()
{	COLOR = 1; BCOLOR = 0; SetCols(); Tstyle = 0; SetAttr(1); }
YK()
{	COLOR = 1; BCOLOR = 2; SetCols(); SetAttr(0); }
KY()
{	COLOR = 2; BCOLOR = 1; SetCols(); SetAttr(0); }
KR()
{	COLOR = 2; BCOLOR = 0; SetCols(); SetAttr(0); }
UKR()
{	COLOR = 2; BCOLOR = 0; SetCols(); Tstyle = 0; SetAttr(1); }
KG()
{	COLOR = 2; BCOLOR = 3; SetCols(); SetAttr(0); }
GK()
{	COLOR = 3; BCOLOR = 2; SetCols(); SetAttr(0); }

 /*L1() { GX = 4; GY = 18; }*/
 /*L2() { GX = 4; GY = 26; }*/
Nl() { GX = 4; GY += 8; }
 /*Bs() { GX -= 8; if(GX < 4) GX = 4; }*/
 /*BsN(L) int L; { GX -=(L * 8); if(GX < 4) GX = 4; }*/

int SetStLen(str)
char *str;
{
	char C; int E = 0, L = -1;
	do { ++L; C = str[L]; if((C == 0) || (C == 10) || (L >= MaxSt1)) E = 1; } while(E == 0);
	str[L] = 0;
	if(L >= MaxSt1)
	{
		E = 0; do { --L; C = str[L]; if((C > 32) || (L < 0)) E = 1; } while(E == 0);
		++L; str[L] = 0;
	}
	return(L);
}

WriteStr(str)
char *str;
{
	ulong L;
	Move(txrp,(long)GX,(long)GY);
	L = SetStLen(str);
	Text(txrp,str,L); GX +=(L * 8);
}

WriteStN(str,L)
char *str;
int L;
{
	Move(txrp,(long)GX,(long)GY); Text(txrp,str,(ulong)L); GX +=(L * 8);
}

WriteCh(C) /* due to the extra Moves it's faster to compose a str */
char C;
{
	Move(txrp,(long)GX,(long)GY); Nstr[0] = C; Text(txrp,Nstr,(ulong)1); GX += 8;
}

WriteCN(C,N)
char C;
int N;
{
	int L;
	for(L = 0; L < N; L++) Nstr[L] = C; WriteStN(Nstr,N);
}

WriteLong(W)
long W;
{
	int L = 79, S = 0;
	Nstr[L--] = 0;
	if(W < 0) { W = -W; S = 1; }
	do { Nstr[L--] = ((W % 10) + 48); W /= 10; } while((W > 0) && (L > 1));
	if(S == 1) Nstr[L--] = '-';
	WriteStr(&Nstr[(L + 1)]);
}
WriteInt(N) int N;
{	WriteLong((long)N); }

GotoY(L) int L; /*origin 1,1*/
{	GX = 4; GY = (L * 8) + 10; /*if(GY == 18) GY = 17;*/ }
GotoXY(N,L) int N,L;
{	GX = (N * 8) - 4; GY = (L * 8) + 10; }
 /*Cel() { int N; N = (PW - (GX / 8)); WriteCN(' ',N); GX -= N * 8; }*/
Cll(L) int L; { GotoY(L); WriteCN(' ',PW); GX = 4; }
Cls()
{
	/*SetAPen(txrp,(ulong)0);*/
	RectFill(txrp,2L,10L,(long)T_WIDTH-4,(long)(SC_HEIGHT-2));
}
/*
char UPcase(C) char C;
{	if((C < 97) || (C > 122)) {;} else C -= 32; return(C); }
*/
int StoI(str)
char *str;
{
	char C; int L, M = 0, N = -1;
	L = SetStLen(str);
	if((L > 0) && (str[0] != 10))
	{
		N = 0;
		do {
			C = str[M] - 48;
			if((C >= 0) && (C <= 9)) N = (N * 10) + C; else N = -1;
			++M;
		}
		while((M < L) && (N != -1));
	}
	return(N);
}

StJoin(str,str2) /*str(LLen) + str2*/
char *str; char *str2;
{
	char C; int E = 0, L, P = 0;
	L = --LLen;
	do { C = str2[P++]; str[++L] = C; if((C == 0) || (L >= MaxStLen)) E = 1; } while(E == 0);
	str[L] = 0; LLen = L;
}

StCat(str,str2) /*str + str2 sets LLen*/
char *str; char *str2;
{	LLen = SetStLen(str); StJoin(str,str2); }

StCopy(str,str2) /*str << str2 sets LLen */
char *str; char *str2;
{	LLen = 0; StJoin(str,str2); }

/*char *AllocStr(N)
int N;
{
	ulong S;
	S = ((N / 4) + 2); Gp1 = (char *) calloc(S,(ulong)4);
	if(Gp1 == 0) { Gp1 = MT; AF = 0; MemErr = 1; }
	return(Gp1);
}
*/
/* - - SCREEN - - */

CLLMsg(N) int N; { UKR(); Cll(N); KY(); WriteStr(Msg); KR(); }

_getback() { if(LLen > 0) { WriteCh(' '); GX -= 16; --LLen; } else LLen = 0; }
GetStM(L) /* L=posn Path << kb ESC LLen*/
int L;
{
	char C;
	Path[0] = 0; CLLMsg(L);
	LLen = 0;
	do {
		KG(); WriteCh(' '); GX -= 8; C = GetKey(); YR();
		if(C > 31) {
			if((C < 127) || (C > 159)) WriteCh(C); else WriteCh(169);
			Path[LLen] = C; ++LLen;
		} else {
			switch (C)
			{
			case 30:/* BS */
			case	8:/* ^H */ { _getback(); break; }
			case 27:/*ESC*/  { C = 13; Path[0] = 10; LLen = 1; break; }
			default : break;
			}
		}/* C > 31*/
		if(LLen >= MaxSt1) { do { _getback(); } while(LLen >= MaxSt1); }
	}
	while(C != 13);
	Path[LLen] = 0;
}

double ReadReal(L) int L;
{
	double R = 0.0; int F = 0, M, N, S = 0; char C;
	GetStM(L);
	if(Path[0] == 10) return(-1);
	else
	{
		if(Path[0] == '-') { Path[0] = '0'; S = 1; }
		M = -1; do { ++M; C = Path[M]; if((C == 0) || (C == '.')) F = 1; } while(F == 0);
		if(C == '.')
		{
			F = 0; N = M; do { ++N; C = Path[N]; if(C == 0) F = 1; } while(F == 0);
			N -= M;
			if(N > 1)
			{
				R = StoI(&Path[M + 1]);
				if(R != -1) { for(; N > 1; N--) R /= 10; } else R = 0;
			}
		}
		Path[M] = 0;
		R += StoI(Path); if(S == 1) R = -R;
	}
	return(R);
}

WriteReal(R,P) double R; int P; /* Num, Dec Pl */
{
	double D; long W; int N = 0;
	D = R;
	if(D < 0.0) { D = -D; WriteCh('-'); }
	W = (long)D; WriteLong(W); WriteCh('.');
	D -= (double)W;
	do { D *= 10; W = (long)D; Nstr[N++] = (W + 48); D -=(double)W; --P; } while(P > 0);
	Nstr[N] = 0; WriteStr(Nstr);
}

/* - - Mandy - - */

double X = 0, Y = 0, Magn = 1;
int FSCR = 1, FASTPREV = 0, LIONS = 0;
int DataOK = 0;

char data[317][189];

#define HI 188
#define MH 94
#define H 47
#define WI 316
#define MW 158
#define W 79

OpenGFX()
{ ScreenToFront(gfxscreen); WindowToFront(gxwin); ActivateWindow(gxwin); }

OpenTxw()
{ ScreenToBack(gfxscreen); WindowToFront(txwin); ActivateWindow(txwin); }

WriteGStr(txt)
char *txt;
{
	ulong len;
	len = SetStLen(txt);
	SetAPen(gxrp,(ulong)1); Move(gxrp,(long)160,(long)191); Text(gxrp,txt,len);
}

TrmScn()
{
	SetAPen(gxrp,(ulong)BLK);
	RectFill(gxrp,0L,8L,(long)SC_WIDTH,11L);
	RectFill(gxrp,316L,8L,320L,(long)(SC_HEIGHT));
}

PlotData()
{
	long a,b;
	TrmScn();
   WriteGStr("Loading file");
	b = 0;
	do {
		a = 0;
		do {
			SetAPen(gxrp,(ulong)data[a][b]); WritePixel(gxrp,a++,(b + 11));
		} while(a <= WI);
		b++;
	}	while(b <= HI);
}

ReadData(fname)
char *fname;
{
	int a,b = 0;
	FILE *src;
   if((src = fopen(fname,"r")) != 0)
	{
		do {
			a = 0;
			do { data[a++][b] = fgetc(src); } while(a <= WI);
			b++;
		}	while(b <= HI);
		fclose(src); DataOK = 1;
	} else DataOK = 0;
}

WriteData(fname)
char *fname;
{
	int a,b = 0; char c;
	FILE *src;
   if((src = fopen(fname,"w")) != 0)
	{
		do {
			a = 0;
			do { c = data[a++][b]; if(c > 31) c = 64; fputc(c,src); } while(a <= WI);
			b++;
		}	while(b <= HI);
		fclose(src);
	}
	return(b);
}

Plot3d()
{
int a,b,iteration,x,y;
	SetAPen(gxrp,(ulong)BRN);
	RectFill(gxrp,0L,11L,(long)SC_WIDTH,(long)(SC_HEIGHT));
	TrmScn();
	b = 0;
	do {
		a = 0;
		do {
			iteration = data[a][b];
			x = a + (b/3) - 10;
			y = 130 - (a - b)/3;
			SetAPen(gxrp,(ulong)iteration);
			Move(gxrp,(long)x,(long)y-iteration);
			Draw(gxrp,(long)x,(long)y-2);	 /* plot height data */
			a++;
		} while(a <= WI);
		b++;
	} while(b <= HI);
	b = 0;
	do {
		a = 0;
		do {
			iteration = data[a][b];
			if(iteration > 31)
			{
				x = a + (b/3) - 10;
				y = 130 - (a - b)/3;
				SetAPen(gxrp,(ulong)14);
				WritePixel(gxrp,(long)x,(long)y-iteration);	 /* mark cut-off top */
			}
			a++;
		} while(a <= WI);
		b++;
	} while(b <= HI);
	FASTPREV = 0;
}

GenScr() /* X Y Magn FSCR FASTPREV */
{
double t,u,p,q,pnew,qnew;
double IX,IY,IXW,IYH;
int a,b,iteration;
	TrmScn();
	if(DataOK) PlotData();
	else
	{
	if((Magn != 0) && (X >= -2.0) && (X <= 2.0) && (Y >= -2.0) && (Y <= 2.0))
	{
		if(FSCR == 1) {
			DataOK = 0;
			if(FASTPREV == 0) { ClGs(); FASTPREV = 1; }
			IXW = (double)X * W; IYH = (double)Y * H;
			a = 0;
			do {
				IX = IXW + (double)(a - MW) / Magn;
				b = 0;
				do {
					IY = IYH + (double)(MH - b) / Magn;
					p = 0.0; q = 0.0; iteration = 0;
					do {
						t = p*p; u = q*q;
						pnew = t - u + ((double)IX / W);
						qnew = (2.0 * p * q) + ((double)IY / H);
						p = pnew; q = qnew;
						++iteration;
					} while(((t + u) < 4) && (iteration < 32));
					data[a][b] = iteration;
					SetAPen(gxrp,(ulong)iteration);
					WritePixel(gxrp,(long)a,(long)(b + 11));
					b += 4;
				} while(b <= HI);
				a += 4;
			} while(a <= WI);
		} else {
			DataOK = 1; FASTPREV = 0;
			IXW = X * W; IYH = Y * H;
			a = 0;
			do {
				IX = IXW + (double)(a - MW) / Magn;
				b = 0;
				do {
					IY = IYH + (double)(MH - b) / Magn;
					p = 0.0; q = 0.0; iteration = 0;
					do {
						t = p * p; u = q * q;
						pnew = t - u + ((double) IX / W);
						qnew = (2.0 * p * q) + ((double) IY / H);
						p = pnew; q = qnew;
						++iteration;
					} while(((t + u) < 4) && (iteration < 32));
					data[a][b] = iteration;
					SetAPen(gxrp,(ulong)iteration);
					WritePixel(gxrp,(long)a,(long)(b + 11));
					++b;
				} while(b <= HI);
				++a;
			} while(a <= WI);
		}
	}
	}
}

Nlt() { GX = 28; GY += 8; }

GetCmd()
{
	int a,b; long d,e;
	double t,IX,IY,IMagn; char Ch;
	OpenTxw();
	SetAPen(txrp,(ulong)0); Cls();

	b = 0; e = 16;
	do {
		a = 0; d = 300;
		do {
			SetAPen(txrp,(ulong)data[a][b]); WritePixel(txrp,d,e);
			a +=4; d +=4;
		} while(a <= WI); /*316*/
		b +=4; e +=2;	  /* 0 brn 1 ylo 2 bk 3 grn*/
	}	while(b <= HI); /*188*/
	SetAPen(txrp,(ulong)2); /* border */
	Move(txrp,(long)300,(long)16);
	Draw(txrp,(long)616,(long)16); Draw(txrp,(long)616,(long)110);
	Draw(txrp,(long)300,(long)110); Draw(txrp,(long)300,(long)16);
	SetAPen(txrp,(ulong)3); /* cross */
	Move(txrp,(long)290,(long)63); Draw(txrp,(long)621,(long)63);
   SetAPen(txrp,(ulong)1); Text(txrp,(char *)"X",(ulong)1);
	SetAPen(txrp,(ulong)3);
	Move(txrp,(long)458,(long)13); Draw(txrp,(long)458,(long)115);
   SetAPen(txrp,(ulong)1); Text(txrp,(char *)"Y",(ulong)1);

	IX = X; IY = Y; IMagn = Magn;
	do {
		YR(); GX = 28; GY = 26; e = 0;
      WriteStr("X = "); WriteReal(IX,10); Nlt();
      WriteStr("Y = "); WriteReal(IY,10); Nlt();
      WriteStr("Mag = "); WriteReal(IMagn,2); Nlt(); Nlt();
      WriteStr("X: absolute X & Y"); Nlt();
      WriteStr("I: relative X & Y"); Nlt();
      WriteStr("M: Magnification"); Nlt();
      WriteStr("R: Read data file"); Nlt();
      WriteStr("W: Write data file"); Nlt();
      WriteStr("Esc: quit data entry "); Nlt(); Nlt();
      WriteStr("Command »"); Ch = toupper(GetKey());
		if(Ch == 27) Ch = 'Q';
		if(Ch == 'I') {
         Msg = "Input X (-100..100):"; t = ReadReal(16);
			if(t > 200) t = 200; if(t < -200) t = -200;
			IX = X + ((double)t / (Magn * 50));
         Msg = "Input Y (-100..100):"; t = ReadReal(18);
			if(t > 200) t = 200; if(t < -200) t = -200;
			IY = Y + ((double)t / (Magn * 50)); DataOK = 0;
		}
		if(Ch == 'X') {
         Msg = "Input X (-2.0..2.0):"; IX = ReadReal(16);
			if(IX > 4.0) IX = 4.0; if(IX < -4.0) IX = -4.0;
         Msg = "Input Y (-2.0..2.0):"; IY = ReadReal(18);
			if(IY > 4.0) IY = 4.0; if(IY < -4.0) IY = -4.0; DataOK = 0;
		}
      if(Ch == 'M') { Msg = "Input Magn:"; IMagn = ReadReal(16); DataOK = 0; }
		if(Ch == 'R') {
         Msg = "Input filename:"; GetStM(16); ReadData(Path);
			if(DataOK) { OpenGFX(); PlotData(); OpenTxw(); Ch = 'Q'; } else e = 1;
		}
		if((Ch == 'W') && (DataOK)) {
         Msg = "Output filename:"; GetStM(16);
			if((d = WriteData(Path)) == 0) e = 1;
		}

		if((Ch == '3') && (DataOK)) { OpenGFX(); Plot3d(); OpenTxw(); Ch = 'Q'; }
		if(e != 0) {
         Nlt(); Nlt(); GK(); WriteStr(Path); WriteStr(" error!");
		} else { Cll(16); Cll(18); }
	}	while(Ch != 'Q');
	X = IX; Y = IY; Magn = IMagn;
	OpenGFX();
}
/* - - Mandy - - */

MenuCmd()
{
	ulong	 class;
	ushort code, qualifier, done = 0;
	Wait( 1 << gxwin->UserPort->mp_SigBit );
	while(!done)
	{
		while(((gxmsg = (struct IntuiMessage *)GetMsg(gxwin->UserPort)) != NULL))
		{
			class = gxmsg->Class;
			code	= gxmsg->Code;
			qualifier = gxmsg->Qualifier;
			ReplyMsg(gxmsg); /* OK now free the message */
			if(class == MENUPICK)
			{
				if( code != MENUNULL )
				{
					switch(MENUNUM(code))
					{
						case EDIT_MENU:
						{
							switch(ITEMNUM(code))
							{
								case PLOT_ITEM: FSCR = 0; GenScr(); break;
								case FAST_ITEM: FSCR = 1; GenScr(); break;
								case CONTR_ITEM: GetCmd(); break;
								case QUIT_ITEM: done = 1; break;
								default: break;
							}
						}
						default: break;
					}
				}
			}
		}
	}
}

Start()
{
   char *fname = "MandyData";
	ClGs();
	SetAPen(gxrp,(ulong)1); Move(gxrp,10L,191L); Text(gxrp,fname,(ulong)9);
	ReadData(fname);
	if(DataOK) PlotData();
   else { WriteGStr("Data file not found"); Delay(100); GenScr(); }
	MenuCmd();
}

 int error;
main(argc,argv)
int argc;
char *argv[];
{
	long d;
   if((IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0)) != 0)
	{
      if((GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)) != 0)
		{
			if(OpenTextWindow())
			{		 
				if(OpenGFXScreen())
				{
					LoadRGB4(&gfxscreen->ViewPort,colours,16L); /* Set the colours */
					if(OpenGFXWindow())
					{
						SetMenuStrip(gxwin, &edit_menu ); /* Set up the menu strip */
							
						gxrp = gxwin->RPort;
						txrp = txwin->RPort;
                  if((xport = (struct MsgPort *)CreatePort("conread",0)) != 0L)
						{
							if((iport = (struct IOStdReq *)CreateStdIO(xport)) != 0L)
							{
                        if((d = OpenDevice("console.device",-1,iport,0)) == 0)
								{
									ConsoleDevice = iport->io_Device;
							
									Start();

									if((d = CheckIO(iport)) == FALSE) AbortIO(iport);
								} else error = 32;
								DeleteStdIO(iport);
							} else error = 16;
							DeletePort(xport);
						} else error = 8;
						ClearMenuStrip(gxwin);
						CloseWindow(gxwin);
					}
					CloseScreen(gfxscreen);
				}
				CloseWindow(txwin);
			}
		}
	}
	/*
	if(error)
	{
		switch(error)
		{
         case  1: fputs("intui",stderr);  break;
         case  2: fputs("gfx",stderr);    break;
         case  4: fputs("window",stderr); break;
         case  8: fputs("msgport",stderr);break;
         case 16: fputs("iport",stderr);  break;
         case 32: fputs("condev",stderr); break;
		}
      fputs(" error",stderr);
	}
	*/
}
/* -	-	mandy -	-	*/

