#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <exec/lists.h>
#include <dos/dos.h>
#include <dos/dostags.h>
#include <graphics/rastport.h>
#include <graphics/gfxmacros.h>
#include <intuition/intuition.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/gadtools.h>
#include <proto/graphics.h>
#include <proto/layers.h>
#include <proto/utility.h>
#include <proto/asl.h>
#include <proto/diskfont.h>
#include <devices/printer.h>
#include <devices/prtbase.h>
#include <rexx/storage.h>
#include <rexx/rxslib.h>

#include "simplerexx.h"
#include "plotgui.h"

extern BPTR _Backstdout;      /* keep stdout till we get past startup */

#define __USE_SYSBASE
#define NT_DATA 250;

#define SAIHEIGHT 10
#define SAIWIDTH  15
#define SAIXOFF   -1
#define SAIYOFF    0
#define WAITHEIGHT 9
#define WAITWIDTH 15		/* for our various pointers */
#define WAITXOFF   1
#define WAITYOFF   1
#define XPWIDTH    5
#define XPHEIGHT   7
#define XPXOFF    -3
#define XPYOFF    -3
#define ROBOTHEIGHT 23
#define ROBOTWIDTH  16
#define ROBOTXOFF   -1
#define ROBOTYOFF    0
#define BOOMHEIGHT 20
#define BOOMWIDTH  16
#define BOOMXOFF   -1
#define BOOMYOFF    0
#define IDWIDTH   15
#define IDHEIGHT  15
#define IDXOFF    -8
#define IDYOFF    -7
#define MSWIDTH   15
#define MSHEIGHT  15
#define MSXOFF    -8
#define MSYOFF    -7

extern USHORT __chip SAIPointer[];
extern USHORT __chip WAITPointer[];
extern USHORT __chip XPPointer[]; 
extern USHORT __chip ROBOTPointer[];
extern USHORT __chip IDPointer[]; 
extern USHORT __chip MSPointer[]; 


int OpenGTXScreen( void );

int OpenGTXWindow(int (* )());

struct TextFont *OpencPFont( void );

void Death(int);

struct Point {
     struct Point *NextPoint;
     double  xval;
     double  yval;
};

struct Set {
     struct Node snode;
     struct Point *FirstPoint;
     char  fn[255];
     int    npt;
     double  xmin;
     double  xmax;
     double  ymin;
     double  ymax;
};

struct World {
     double xdelta;
     double ydelta;
     double xmax;
     double xmin;
     double ymax;
     double ymin;
};

struct CurView {
     double xmax;
     double xmin;
     double ymax;
     double ymin;
     double xdelta;
     double ydelta;
     double xtik;
     double ytik;
     WORD xsubt;
     WORD ysubt;
     BOOL xmode;
     BOOL ymode;
     WORD xprec;
     WORD yprec;
     double xfact;
     double yfact;
     LONG xdec;
     LONG ydec;
};

extern struct World wd;
extern struct CurView cv;
extern struct CurView ov;
extern struct List *SetList;
extern struct TextAttr *cPFont;
extern struct TextFont *fontPtr;
extern UBYTE *path;
extern UBYTE *MyName;
extern UBYTE *XLAB;
extern UBYTE *YLAB;
extern UBYTE *TITLE;
extern struct RastPort *rp;
extern struct ViewPort *vp;
extern LONG thin;
extern LONG points;
extern BOOL grid;
extern BOOL LOGX;
extern BOOL LOGY;
extern BOOL MONO;
extern BOOL sym;
extern BOOL DEFS;
extern BOOL CPANEL;
extern BOOL NOFNAME;
extern BOOL BEHIND;
extern BOOL FEED;
extern BOOL RealTime;
extern WORD WIDTH;
extern WORD HEIGHT;
extern WORD xoff;
extern WORD yoff;
extern LONG Disp;
extern UWORD SWIDTH;
extern UWORD SHEIGHT;
extern WORD GADHEIGHT;
extern WORD GADWIDTH;
extern WORD lastcolor;
extern WORD depth;

extern LONG precision;

extern AREXXCONTEXT	RexxStuff;

BOOL AddSet( UBYTE * );

BPTR OpenFile( UBYTE * );

struct Point *File2Set( BPTR , struct Set * );

void FreePoints(struct Point * );

void FreeAllSets( void );

void DrawView( BOOL );

void FindWorld ( void );

void DrawSet ( struct Point * );

void DrawAllSets ( void );

void SymAllSets ( void );

void SymSet ( struct Point * );

void tik(double vmin, double vmax, double *tick, WORD *nsubt, BOOL *mode, WORD *prec);

void DrawColorBox( WORD, WORD, WORD, WORD, WORD);

WORD DrawFileNameBoxes( WORD);

void Erase( BOOL );

int CrossHair( void );

WORD Dump( void );
void DrawLogo( void );

void XAxis( void );
void YAxis( void );
void linXAxis( void );
void linYAxis( void );
void logXAxis( void );
void logYAxis( void );

BOOL AddNewSet( void );

void Zoom( void );

void Grid( void );
void logXGrid( void );
void logYGrid( void );
void linXGrid( void );
void linYGrid( void );

void E2xTen( UBYTE *s);

void Round( double * );

void CalcMode ( double, double *);

void Identify( WORD, WORD , BOOL);

void ticX( double, BOOL);
void ticY( double, BOOL);

LONG ScaleX( double );
LONG ScaleY( double );

void LabelY( UBYTE * );
void LabelTitle( void );

double ZoomX( WORD );
double ZoomY( WORD );

LONG linX( double );
LONG linY( double );

double linZoomX( WORD );
double linZoomY( WORD );

LONG logX( double );
LONG logY( double );

double logZoomX( WORD );
double logZoomY( WORD );

LONG HandleARexxMes( void );

int NewDepth( WORD );
void VertText(struct RastPort *, UBYTE *, LONG );

int UnZoom( void );

int Measure(WORD, WORD);
