/************************/
/* Including the C libs */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
/* #include <sys/file.h> */

/************************/
/* Including the X libs */

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/keysymdef.h>
#include <X11/cursorfont.h>

/********************************************/
/* Including the XGRAFIX bitmap and strings */

#include "xgrafix.ico"
#include "xgrafix.str"

/**********************************************/
/* Definition of machine type SUN (also for SGI) ddd Helmut */

#define    SUN

/**********************************************/
/* Definition of constants used in XGRAFIX    */

#define    TRUE                             1
#define    FALSE                            0
#define    MAX_BUFFER_SIZE                 40

#define    DEFAULT_THREED_WIN_HEIGHT      400
#define    DEFAULT_THREED_WIN_WIDTH       475

#define    DEFAULT_WIN_HEIGHT             300
#define    DEFAULT_WIN_WIDTH              400
#define    DEFAULT_WIN_BORDER_WIDTH         2

#define    DEFAULT_DIALOGBOX_X            400
#define    DEFAULT_DIALOGBOX_Y            400

/****  The Main Menu default parameters  ****/

#define    DEFAULT_MAIN_MENU_X             10
#define    DEFAULT_MAIN_MENU_Y             40
#define    DEFAULT_MAIN_MENU_WIDTH        200

#define	   MAX_MAIN_MENU_BUTTON		    6
#define    MAIN_MENU_RUN                    1
#define    MAIN_MENU_STOP                   2
#define    MAIN_MENU_STEP                   3
#define    MAIN_MENU_SAVE                   4
#define    MAIN_MENU_QUIT                   5

/****  The Window Menu default parameters ***/

#define	   MAX_2D_WIN_BUTTON		    4
#define	   MAX_3D_WIN_BUTTON		    5
#define    WINDOW_MENU_RESCALE              0
#define    WINDOW_MENU_TRACE                1
#define    WINDOW_MENU_PRINT                2
#define    WINDOW_MENU_CROSSHAIR            3
#define    WINDOW_MENU_3D_VIEW              4

/****  The Crosshair default parameters   ***/
#define	   MAX_CROSSHAIR_BUTTON		    2
#define    CROSSHAIR_X_FIELD                0
#define    CROSSHAIR_Y_FIELD                1

#define    NULLTYPE                         0
#define	   FLOAT			    3
#define	   INTEGER			    5
#define	   CHAR				    7

/******   Window states                 ****/
#define	   OPEN				    1
#define	   OPEN_BEFORE			    2
#define	   TRACE			    4
#define    Y_LOG                            8
#define    X_LOG                           16
#define    Y_AUTORESCALE		   32
#define    X_AUTORESCALE		   64
#define    Y_AUTORESCALE_BEFORE	          128
#define    X_AUTORESCALE_BEFORE	          256
#define    CROSSHAIR                      512
#define    Z_AUTORESCALE		 1024
#define    SHADING                       2048
#define    COLORCODE                  	 4096
#define    GRID                 	 8192
#define    THREED                       16384
#define    TWOD                             0
#define    Z_LOG                        32768

/******   Data states                   ****/
#define    CURVE                            0
#define    SURFACE                          0
#define    SCATTER                          1

/******    Plot types                   ****/
#define    LIN_LIN                          0
#define    LIN_LOG                          1
#define    LOG_LOG                          2
#define    LOG_LIN                          3
#define    LIN_LIN_LIN                      4
#define    LIN_LIN_LOG                      5
#define    LIN_LOG_LIN                      6
#define    LIN_LOG_LOG                      7
#define    LOG_LIN_LIN                      8
#define    LOG_LIN_LOG                      9
#define    LOG_LOG_LIN                     10
#define    LOG_LOG_LOG                     11

/******    Initial window states        ****/
#define    OPEN_STATE                       0
#define    ICONIC_STATE                     1

/******    Button states                ****/
#define    NORMAL_STATE                     0
#define    PUSHED_STATE                     1
#define    GRAYED_STATE                     2

/******  MAX NUMBER OF FORTRAN 3D PLOTS ****/
#define    MAX_3D_PLOTS                    20

#ifndef    DBL_MIN
#define	   DBL_MIN		       1E-200
#endif

/**********************************************/
/* Definition of structures used in XGRAFIX   */

typedef struct data *DataType;
struct data {
  float  **z;
  float   *zscat;
  float   *y;
  float   *x;
  int     *mpoints;
  int     *npoints;
  int      color;
  int      state;
  DataType next;
}; 

typedef struct data1 *DataType1;        /* ddd */
struct data1 {
  float  **z;
  float   *zscat;
  float   *y;
  double  *x;                           /* ddd */
  int     *mpoints;
  int     *npoints;
  int      color;
  int      state;
  DataType1 next;                       /* ddd */
};

typedef struct label *LabelType;
struct label {
  float   Z_Scale;
  float   Z_Min;
  float   Z_Max;
  char   *Z_Label;
  int     Z_Auto_Rescale;
  float   Y_Scale;
  float   Y_Min;
  float   Y_Max;
  char   *Y_Label;
  int     Y_Auto_Rescale;
  float   X_Scale;
  float   X_Min;
  float   X_Max;
  char   *X_Label;
  int     X_Auto_Rescale;
};

typedef struct win_button *ButtonType;
struct win_button {
  int        state;
  int        id;
  Window     xwin;
  GC         xwingc;
  char       Label[MAX_BUFFER_SIZE];
  ButtonType next;
};

typedef struct window_struct *WindowType;
struct window_struct {
  int        height;
  int        width;
  int        ulxc;
  int        ulyc;
  int        lrxc;
  int        lryc;
  int        state;
  DataType   data;
  LabelType  label;
  float      theta;
  float      phi;
  char       ps_file_name[MAX_BUFFER_SIZE];
  char       plot_title[MAX_BUFFER_SIZE];
  ButtonType button_list;
  Window     xwin;
  GC         xwingc;
  Pixmap     pixmap_buffer;
  int      (*paint_function)();
  int      (*print_function)();
  WindowType next;
};

typedef struct window_struct1 *WindowType1;  /* ddd */
struct window_struct1 {
  int        height;
  int        width;
  int        ulxc;
  int        ulyc;
  int        lrxc;
  int        lryc;
  int        state;
  DataType1  data1;                         /* ddd */
  LabelType  label;
  float      theta;
  float      phi;
  char       ps_file_name[MAX_BUFFER_SIZE];
  char       plot_title[MAX_BUFFER_SIZE];
  ButtonType button_list;
  Window     xwin;
  GC         xwingc;
  Pixmap     pixmap_buffer;
  int      (*paint_function)();
  int      (*print_function)();
  WindowType1 next;                         /* ddd */
};

typedef struct menu_struct MenuType;
struct menu_struct {
  Window      xwin;
  ButtonType  button_list;
};

typedef struct dialogbox_item {
  int         type;
  char       *name;
  char       *ptr;
  ButtonType  xbutton;
} *DialogBoxItemType;

typedef struct dialogbox {
  char             *name;
  char              msg[100];
  int               width;
  Window            xwin;
  GC                xwingc;
  int               number;
  DialogBoxItemType button_list;
} *DialogBoxType;

typedef struct mesh_struct{
  float x;
  float y;
} MeshType;

typedef struct string_struct{
  char buffer[20];
  float x;
  float y;
} StringType;

/********************************************/
/********************************************/
/* Global variables used in XGRAFIX         */

int           theRunFlag;
int           thePrevRunFlag;
int           theStepFlag;

double       *theTimeStep;
ButtonType    theTimeStepButtonPtr;

WindowType    theWindowList;
WindowType1   theWindowList1;          /* ddd */
WindowType    theOpenDialogBoxWindow;
MenuType      theMainMenu;
Cursor        theButtonCursor;
int           theCurrentCursorPosition;

float         theCrosshairmx, theCrosshairbx;
float         theCrosshairmy, theCrosshairby;
int           theCrosshairx1, theCrosshairx2;
int           theCrosshairy1, theCrosshairy2;
WindowType    theCrosshairedWindow;
MenuType      theXYCrosshairWindow;
Cursor        theCrosshairCursor;

Font          theFont;
XFontStruct  *theFontStruct;
int           theFontAscent;
int           theFontDescent;
int           theFontHeight;

Pixmap        theIcon;
int           theIconULX;
int           theIconULY;

char          theCodeName[80];
char          theInputFile[80];
char          theDumpFile[80];
char          thePSFile[80];

char          Sound1[80];
char          Sound2[80];
char          Sound3[80];
char          Sound4[80];

char          ReqDisp[120];
Display      *theDisplay;
int           theDisplayWidth;
int           theDisplayHeight;

Window        theRootWindow;
int           theScreen;
int           theDepth;

unsigned long theBlackPixel;
unsigned long theWhitePixel;
Colormap      theColormap;
int           theWinTextColor;
int           theDBTextColor;
int           theBorderColor;
int           theTickMarkColor;
int           theBKGDColor;
int           GrayShade[MAXGRAYSHADES];
int           Color[MAXCOLORS];
int           ThreeDColor[MAXTHREEDCOLORS];
float         init_theta, init_phi;

int           theDialogBoxButtonLabelX;
int           theDialogBoxButtonLabelY;
int           theDialogBoxButtonSpacing;
int           theDialogBoxButtonStart;

DialogBoxType theOpenDialogBox;  
DialogBoxType theRescaleDB;  
DialogBoxType thePSDB;
DialogBoxType theDFDB;
DialogBoxType theMessageDB;
DialogBoxType theThreeDViewDB;

DataType      SetupDataStruct();
DataType1     SetupDataStruct1();            /* ddd */
LabelType     SetupLabelStruct();
int           Paint_Window();
int           Paint_ThreeD_Window();
int           PostScriptOpenWindow();
int	      PostScript_ThreeD_Window(); 

int           index_3d_plots;
int          *g_nx[MAX_3D_PLOTS]; 
int          *g_ny[MAX_3D_PLOTS]; 
int          *g_nz1dim[MAX_3D_PLOTS]; 
float        *g_z_plot[MAX_3D_PLOTS]; 
float       **zplot3d[MAX_3D_PLOTS]; 

/***************************************************/
/* Defining macros used in XGRAFIX macros         */

#define Is_Zeroth_Set(x)	(((x)&  1)  ==    1)
#define Is_First_Set(x)		(((x)&  2)  ==    2)
#define Is_Second_Set(x)	(((x)&  4)  ==    4)
#define Is_Third_Set(x)		(((x)&  8)  ==    8)
#define Is_Fourth_Set(x)	(((x)& 16)  ==   16)
#define Is_Fifth_Set(x)		(((x)& 32)  ==   32)
#define Is_Sixth_Set(x)		(((x)& 64)  ==   64)
#define Is_Seventh_Set(x)	(((x)&128)  ==  128)
#define Is_Eighth_Set(x)	(((x)&256)  ==  256)
#define Is_Ninth_Set(x)         (((x)&512)  ==  512)
#define Is_Tenth_Set(x)         (((x)&1024) == 1024)
#define Is_Eleventh_Set(x)      (((x)&2048) == 2048)
#define Is_Twelveth_Set(x)      (((x)&4096) == 4096)
#define Is_Thirteenth_Set(x)    (((x)&8192) == 8192)
#define Is_Fourteenth_Set(x)    (((x)&16384)==16384)
#define Is_Fifteenth_Set(x)     (((x)&32768)==32768)

#define Turn_On(x, flag)	((x) |= (flag))
#define Turn_Off(x, flag) 	((x) &= ~(flag))

/*************************************************/
/* XGRAFIX  macros.                              */

#define Is_Open(object)			    Is_Zeroth_Set((object)->state)
#define Was_Open_Before(object)		    Is_First_Set((object)->state)
#define Is_Trace_On(object)		    Is_Second_Set((object)->state)

#define Is_Z_Log(object)		    Is_Fifteenth_Set((object)->state)
#define Is_Y_Log(object)		    Is_Third_Set((object)->state)
#define Is_X_Log(object)		    Is_Fourth_Set((object)->state)

#define Is_Y_AutoRescale(object)            Is_Fifth_Set((object)->state)
#define Is_X_AutoRescale(object)            Is_Sixth_Set((object)->state)

#define Is_Y_AutoRescale_Before(object)     Is_Seventh_Set((object)->state)
#define Is_X_AutoRescale_Before(object)     Is_Eighth_Set((object)->state)

#define Is_Crosshair_On(object)             Is_Ninth_Set((object)->state)

#define Is_Z_AutoRescale(object)            Is_Tenth_Set((object)->state)
#define Is_Shading_On(object)               Is_Eleventh_Set((object)->state)
#define Is_ColorCode_On(object)             Is_Twelveth_Set((object)->state)
#define Is_Grid_On(object)                  Is_Thirteenth_Set((object)->state)
#define Is_ThreeD(object)                   Is_Fourteenth_Set((object)->state)

#define Open(object)			    Turn_On((object)->state,  OPEN)
#define Iconify(object)			    Turn_Off((object)->state, OPEN)

#define Open_Before(object)		    Turn_On((object)->state,  OPEN_BEFORE)
#define Iconic_Before(object)		    Turn_Off((object)->state, OPEN_BEFORE)

#define Trace_On(object)		    Turn_On((object)->state,  TRACE)
#define Trace_Off(object)		    Turn_Off((object)->state, TRACE)

#define Set_Z_Log(object)		    Turn_On((object)->state,  Z_LOG)
#define Set_Y_Log(object)		    Turn_On((object)->state,  Y_LOG)
#define Set_X_Log(object)		    Turn_On((object)->state,  X_LOG)

#define Y_AutoRescale(object)               Turn_On((object)->state,  Y_AUTORESCALE)
#define X_AutoRescale(object)               Turn_On((object)->state,  X_AUTORESCALE)
#define Y_AutoRescale_Before(object)        Turn_On((object)->state,  Y_AUTORESCALE_BEFORE)
#define X_AutoRescale_Before(object)        Turn_On((object)->state,  X_AUTORESCALE_BEFORE)

#define Y_AutoRescale_Off(object)           Turn_Off((object)->state, Y_AUTORESCALE)
#define X_AutoRescale_Off(object)           Turn_Off((object)->state, X_AUTORESCALE)
#define Y_AutoRescale_Off_Before(object)    Turn_Off((object)->state, Y_AUTORESCALE_BEFORE)
#define X_AutoRescale_Off_Before(object)    Turn_Off((object)->state, X_AUTORESCALE_BEFORE)

#define Crosshair_On(object)                Turn_On((object)->state,  CROSSHAIR)
#define Crosshair_Off(object)               Turn_Off((object)->state, CROSSHAIR)

#define Z_AutoRescale(object)               Turn_On((object)->state,  Z_AUTORESCALE)
#define Z_AutoRescale_Off(object)           Turn_Off((object)->state, Z_AUTORESCALE)

#define Shading_On(object)                  Turn_On((object)->state,  SHADING)
#define Shading_Off(object)                 Turn_Off((object)->state, SHADING)

#define ColorCode_On(object)                Turn_On((object)->state,  COLORCODE)
#define ColorCode_Off(object)               Turn_Off((object)->state, COLORCODE)

#define Grid_On(object)                     Turn_On((object)->state,  GRID)
#define Grid_Off(object)                    Turn_Off((object)->state, GRID)

#define Set_ThreeD(object)                  Turn_On((object)->state,  THREED)

#define Is_Scatter(object)                  Is_Zeroth_Set((object)->state)
#define Set_Scatter(object)                 Turn_On((object)->state,  SCATTER)

#ifndef max                                 /* ddd was max() */
#define max(x, y)                           (((x) > (y)) ? (x) : (y))
#endif

#ifndef min                                 /*  ddd was min() */
#define min(x, y)                           (((x) < (y)) ? (x) : (y))
#endif


/******************* Declarations *** ddd ************************/

void XGInit(int, char *[], double *);
void XGEvent();
void XGUpdate();
void XGCurve(float *, float *, int *, int);
void XGScat2D(float *, float *, int *, int);
void XGScat3D(float *, float *, float *, int *, int);
void XGSurf(float *, float *, float **, int *, int *, int);
void XGCont(float *, float *, float **, int *, int *, int);
void SetupWindow(int, int, LabelType, DataType, int, int, int);
void Init_Main_Menu();
void Event_Manager();
void Process_One_Event();
void Refresh_Screen();
void DrawWall(WindowType, MeshType *, float, float, float, float, int, int);
void Paint_Button(ButtonType);
void Local_Window_Menu(WindowType, ButtonType);
void Main_Menu(ButtonType);
void RescaleWindow();
void PostScriptWindow();
void Adjust3DViewWindow();
void ExitMessage(char *);
void Init_Crosshair(WindowType);
void UpdateCrosshair(int, int);
void SetCrosshairParameters();
void DestroyCrosshair(WindowType);
void Init_Rescale_DialogBox();
void Init_ThreeDView_DialogBox();
void Init_DF_DialogBox();
void Init_PS_DialogBox();
void Init_Message_DialogBox();
void SorryMessage();
void DialogBox(DialogBoxType);
void Paint_DialogBox(DialogBoxType);
void DialogBox_Menu(DialogBoxType);
void DispatchMessage(DialogBoxType); 
void DestroyDialogBox(DialogBoxType);
void ReadFromKeyborad(XKeyEvent *);
int  PostScriptOpenWindow();
void PSHeader();
int  PostScript_ThreeD_Window();
void PaintWall(MeshType *, float, float, float, float, int, int);
void sun_write(char *, int, int, FILE *);
void sun_read(char *, int, int, FILE *);
void XGWrite(void *, int, int, FILE *, char *);
void XGRead(void *, int, int, FILE *, char *);

/******** When used, others also should be declared !!! **********/
