#ifndef IVIEWDEF_H
#define IVIEWDEF_H

#ifndef AMIGA
#ifndef UNIX
#ifndef MACINTOSH
#define MACINTOSH
#endif
#endif
#endif

#ifndef AMIGA
#ifndef _IVIEWWINDOW_
# define WindowPtr long
# define Window long
# define MenuHandle long
# define Menu long
#else
#ifdef MACINTOSH
# ifdef MPWC
#  include <Quickdraw.h>
#  include <Windows.h>
#  include <Menus.h>
#  include <Dialogs.h>
#  include <ToolUtils.h>
#  include <Events.h>
#  include <OSEvents.h>
#  include <Controls.h>
#  include <Lists.h>
#  include <Memory.h>
#  include <Fonts.h>
#  include <Script.h>
#  define thePort qd.thePort
#  define arrow qd.arrow
#  define MBarHeight GetMBarHeight()
#  define MemErr MemError()
#  define RETURNCHAR '\n'
# else
# undef FALSE /* to avoid some conflicts */
# undef TRUE
#  include <WindowMgr.h>	/* pulls in MacTypes, QuickDraw */
#  include <MenuMgr.h>
#  include <ToolboxUtil.h>
#  include <EventMgr.h>
#  include <DialogMgr.h>
#  include <ControlMgr.h>
#  include <FontMgr.h>
#  include <MemoryMgr.h>
#  include <FontMgr.h>
#  include <pascal.h>
#  define FALSE 0
#  define TRUE 1
#  define ModalFilterProcPtr ProcPtr
#  define RETURNCHAR '\r'
# endif MPWC
#endif MACINTOSH
#endif _IVIEWWINDOW_
#endif AMIGA

#ifdef MACINTOSH
#define IVIEW_WINDOW WindowPtr
#define IVIEW_MENU MenuHandle
#endif MACINTOSH
#ifdef UNIX
#define IVIEW_WINDOW Window
#define IVIEW_MENU Menu
#endif UNIX
#ifdef AMIGA
#include <proto/intuition.h>
typedef struct Window * IVIEW_WINDOW;
typedef struct Menu   * IVIEW_MENU;
#endif AMIGA

#ifndef MACINTOSH
#ifdef SUNVIEW
#undef SUNVIEW  /* to avoid a conflict in include files */
#include <suntool/sunview.h>
#include <suntool/canvas.h>
#include <suntool/panel.h>
#include <pixrect/pixrect_hs.h>

typedef struct {
  int h, v;
} Point;

#else
#ifdef AMIGA
#define Point POINT
#endif AMIGA
typedef struct {
  int h, v;
} Point;
typedef struct {
  int left, top, width, height; /* right changed to top JKL */
} Rect;
#endif SUNVIEW
#endif MACINTOSH

#define nil 0L

typedef short ColorCode; /* changed JKL */

typedef enum {
  MouseClick = 0,
  MouseMove = 1
} MouseEventType;

typedef enum {
  NoModifiers = 0,
  ExtendModifier = 1,
  OptionModifier = 2,
  OptionExtendModifier = 3
} MouseClickModifier;

typedef enum {
  pointInvisible,
  pointNormal,
  pointHilited,
  pointSelected
} PointState;

typedef enum {
  selecting,
  brushing,
  usermode
} MouseMode;

#define ARROW_CURSOR      0
#define WATCH_CURSOR      1
#define CROSS_CURSOR      2
#define BRUSH_CURSOR      3
#define HAND_CURSOR       4
#define FINGER_CURSOR     5
#define HOUR_GLASS_CURSOR 6
#define TRASH_BAG_CURSOR  7
#define TRASH_CAN_CURSOR  8

#define BASIC_IVIEW 0

#ifndef AMIGA
typedef char *IVIEW_WINDOW;
#endif AMIGA

typedef void *IViewReallocData;

#ifdef MACINTOSH  /* moved from stmem.c   JKL */
typedef char **StReallocData;
#else
typedef struct{
  int size;
  char *data; /* cannot be void because of bzero in stmem.c JKL */
} realloc_data, *StReallocData;
#endif MACINTOSH

/**************************************************************************/
/**************************************************************************/
/**                                                                      **/
/**                         IViewBasicPoints Package                     **/
/**                                                                      **/
/**************************************************************************/
/**************************************************************************/

typedef struct basic_points {
  int num_points, num_variables;
  double *scale, *location, **transformation;
  StReallocData *data, *screen_data;
  int recalculateScreenPoints, fixed;
} *IViewBasicPoints;

/**************************************************************************/
/**************************************************************************/
/**                                                                      **/
/**                            IViewData Package                         **/
/**                                                                      **/
/**************************************************************************/
/**************************************************************************/

#define color_index ColorCode /* added JKL */

typedef short color_index; /* changed JKL */

typedef struct point_symbol {
  int normal, highlighted;
} PointSymbol;

typedef struct point_info {
  PointState state, screen_state;
  char marked, masked;
  color_index color;
  PointSymbol symbol;
  char *label;
} PointInfo;

typedef struct line_info {
  int next, type;
  char masked, width;
  color_index color;
} LineInfo;

typedef struct string_info {
  char *string;
  char masked, up, h, v;
  color_index color;
} StringInfo;

typedef struct iview_data {
  IViewBasicPoints points, lines, strings;
  IViewReallocData pointInfo, lineInfo, stringInfo;
  double *mins, *maxes, *scale, *location;
  int *screen_mins, *screen_maxes;
  char **variableLabels;
  int recalculateScreenPoints, transformed;
  double **transformation;
} *IViewDATA; /* changed because IViewData defined as a function   JKL */

typedef struct brush {
  int left, top, width, height, showing;
} Brush;

typedef struct clickrange {
  int width, height;
} ClickRange;

typedef struct content {
  int left, top, width, height, origin_x, origin_y, x_variable, y_variable;
} Content;

typedef struct {
  int left, top, right, bottom;
} Margin;

typedef struct {
  int showing, labeled, ticks, height, edge;
} Axis;

typedef struct iview {
  /* char * */IViewDATA data; /* changed JKL */
  Content content;
  Margin margin;
  Axis x_axis, y_axis;
  Brush brush;
  ClickRange clickrange;
  MouseMode mouseMode;
  int showingLabels, fixed_aspect, dirty;
  long links;
  double *scale, *shift;
} *IView;

/* General Data Macros */
#define IViewNumVariables(w) IViewDataNumVariables(IViewData(w))
#define IViewSetVariableLabel(w, var, s) IViewDataSetVariableLabel(IViewData(w), var, s)
#define IViewVariableLabel(w, var) IViewDataVariableLabel(IViewData(w), var)
/* following two changed to Scaled as in iview.c JKL */
#define IViewSetScaledRange(w, var, low, high) IViewDataSetRange(IViewData(w), var, low, high)
#define IViewGetScaledRange(w, var, low, high) IViewDataGetRange(IViewData(w), var, low, high)
#define IViewSetScreenRange(w, var, low, high) IViewDataSetScreenRange(IViewData(w), var, low, high)
#define IViewGetScreenRange(w, var, low, high) IViewDataGetScreenRange(IViewData(w), var, low, high)
#define IViewSetIdentityTransformation(w) IViewDataSetIdentityTransformation(IViewData(w))
#define IViewSetTransformation(w, a) IViewDataSetTransformation(IViewData(w), a)
#define IViewTransformation(w) IViewDataTransformation(IViewData(w))
#define IViewIsTransformed(w) IViewDataIsTransformed(IViewData(w))
#define IViewApplyTransformation(w, a, inbasis) IViewDataApplyTransformation(IViewData(w), a, inbasis)

/* Point Data Macros */
#define IViewAddPoints(w, n) IViewDataAddPoints(IViewData(w), n)
#define IViewClearPoints(w) IViewDataClearPoints(IViewData(w))
#define IViewNumPoints(w) IViewDataNumPoints(IViewData(w))
/* next two changed to agree with iview.c JKL */
#define IViewSetPointValue(w, var, point, value) \
        IViewDataSetPointValue(IViewData(w), var, point, IViewEncodeValue(w,value,var))
#define IViewPointValue(w, var, point) IViewDecodeValue(w, \
                   IViewDataPointValue(IViewData(w), var, point),var)
/* next three added from iview.c JKL */
#define IViewSetPointScaledValue(w, var, point, value) IViewDataSetPointValue(IViewData(w), \
                   var, point, value)
#define IViewPointScaledValue(w, var, point) IViewDataPointValue(IViewData(w), var, point)
#define IViewPointTransformedValue(w, var, point) IViewDataPointTransformedValue(IViewData(w), \
                   var, point)
#define IViewPointScreenValue(w, var, point) IViewDataPointScreenValue(IViewData(w), var, point)
#define IViewGetScreenPointValues(w, point, x) IViewDataGetScreenPointValues(IViewData(w), point, x)
#define IViewSetPointMask(w, point, masked) IViewDataSetPointMask(IViewData(w), point, masked)
#define IViewPointMasked(w, point) IViewDataPointMasked(IViewData(w), point)
/* next two added from iview.c JKL */
#define IViewSetPointColor(w, point, color) IViewDataSetPointColor(IViewData(w), point, color)
#define IViewPointColor(w, point) IViewDataPointColor(IViewData(w),  point)
/* left as a function in iview.c JKL
#ifdef AMIGA   does not work with Lattice ?? JKL
#define IViewSetPointState(w, point, state) IViewDataSetPointState(IViewData(w), point, state); \
  if (IViewIsLinked(w)) IViewMatchPointState(w, point)
#else
#define IViewSetPointState(w, point, state) (IViewDataSetPointState(IViewData(w), point, state), \
  if (IViewIsLinked(w)) IViewMatchPointState(w, point))
#endif AMIGA
*/
#define IViewPointState(w, point) IViewDataPointState(IViewData(w),  point)
#define IViewSetPointScreenState(w, point, state) IViewDataSetPointScreenState(IViewData(w), point, state)
#define IViewPointScreenState(w, point) IViewDataPointScreenState(IViewData(w),  point)
#define IViewResetScreenStates(w) IViewDataResetScreenStates(IViewData(w))
#define IViewSetPointMark(w, point, marked) IViewDataSetPointMark(IViewData(w), point, marked)
#define IViewPointMarked(w, point) IViewDataPointMarked(IViewData(w),  point)
#define IViewClearPointMarks(w) IViewDataClearPointMarks(IViewData(w))
#define IViewSetPointLabel(w, point, s) IViewDataSetPointLabel(IViewData(w), point, s)
#define IViewPointLabel(w, point) IViewDataPointLabel(IViewData(w), point)
#define IViewSetPointSymbol(w, point, sym, hsym) IViewDataSetPointSymbol(IViewData(w),  point, sym, hsym)
#define IViewGetPointSymbol(w, point, sym, hsym) IViewDataGetPointSymbol(IViewData(w),  point, sym, hsym)

/* Line Data Macros */
#define IViewNumLines(w) IViewDataNumLines(IViewData(w))
#define IViewAddLines(w, n) IViewDataAddLines(IViewData(w), n)
#define IViewClearLines(w) IViewDataClearLines(IViewData(w))
/* next two changed as in iview.c JKL */
#define IViewSetLineValue(w, var, line, value) IViewDataSetLineValue(IViewData(w), var, line, \
                     IViewEncodeValue(w, value,var))
#define IViewLineValue(w, var, line) IViewDecodeValue(w,IViewDataLineValue(IViewData(w), \
                     var, line), var)
/* next three added as in iview.c JKL */
#define IViewSetLineScaledValue(w, var, line, value) IViewDataSetLineValue(IViewData(w), \
                     var, line, value)
#define IViewLineScaledValue(w, var, line) IViewDataLineValue(IViewData(w), var, line)
#define IViewLineTransformedValue(w, var, line) IViewDataLineTransformedValue(IViewData(w), \
                     var, line)
#define IViewLineScreenValue(w, var, line) IViewDataLineScreenValue(IViewData(w), var, line)
#define IViewSetLineMask(w, line, masked) IViewDataSetLineMask(IViewData(w), line, masked)
#define IViewLineMasked(w, line) IViewDataLineMasked(IViewData(w), line)
/* next two added as in iview.c JKL */
#define IViewSetLineColor(w, line, color) IViewDataSetLineColor(IViewData(w), line, color)
#define IViewLineColor(w, line) IViewDataLineColor(IViewData(w), line)
#define IViewSetNextLine(w, line, next) IViewDataSetNextLine(IViewData(w), line, next)
#define IViewNextLine(w, line) IViewDataNextLine(IViewData(w), line)
#define IViewSetLineType(w, line, type) IViewDataSetLineType(IViewData(w), line, type)
#define IViewLineType(w, line) IViewDataLineType(IViewData(w), line)
/* next two macros added as in iview.c JKL */
#define IViewSetLineWidth(w, line, width) IViewDataSetLineWidth(IViewData(w), line, width)
#define IViewGetLineWidth(w, line, width) IViewDataGetLineWidth(IViewData(w), line, width)

#ifdef USESTRINGS
/* String Data Macros */
#define IViewNumStrings(w) IViewDataNumStrings(IViewData(w))
#define IViewAddStrings(w, n) IViewDataAddStrings(IViewData(w), n)
#define IViewClearStrings(w) IViewDataClearStrings(IViewData(w))
#define IViewSetStringValue(w, var, string, value) IViewDataSetStringValue(IViewData(w), var, string, value)
#define IViewStringValue(w, var, string) IViewDataStringValue(IViewData(w), var, string)
#define IViewStringScreenValue(w, var, string) IViewDataStringScreenValue(IViewData(w), var, string)
#define IViewSetStringMask(w, string, masked) IViewDataSetStringMask(IViewData(w), string, masked)
#define IViewStringMasked(w, string) IViewDataStringMasked(IViewData(w),  string)
#define IViewSetStringString(w, string, str) IViewDataSetStringString(IViewData(w), string, str)
#define IViewStringString(w, string) IViewDataStringString(IViewData(w), string)
#define IViewSetStringModifiers(w, string, up, h, v) IViewDataSetStringModifiers(IViewData(w), string, up, h, v)
#define IViewGetStringModifiers(w, string, up, h, v) IViewDataSetStringModifiers(IViewData(w), string, up, h, v)
#endif USESTRINGS

/* dialog item types */
#define NULL_ITEM        0
#define BUTTON_ITEM      1
#define TOGGLE_ITEM      2
#define CHOICE_ITEM      3
#define MESSAGE_ITEM     4
#define TEXT_ITEM        5
#define SCROLL_ITEM      6
#define REAL_SCROLL_ITEM 7
#define LIST_ITEM        8
#define ITEM_LIST        9

#define has_item_location(i) (slot_value(i, s_location) != NIL)
#define has_item_size(i) (slot_value(i, s_size) != NIL)
#define check_dialog_address(d) valid_dialog_address(slot_value(d, s_hardware_address))

/***********************************************************************/
/*                    Machine Dependent Definitions                    */
/***********************************************************************/

#ifdef MACINTOSH
typedef struct {
  LVAL object;
  int count;
} DialogData;

typedef struct {
  int type;
  int itemNumber, clusterLeader, clusterSize;
  Handle itemHandle;
  LVAL object;
} DialogItemData;

#define DialogItemCount(d) (**(short **)(((DialogPeek) d)->items) + 1)
#define GetDialogObject(d) ((*((DialogData **) GetWRefCon(d)))->object)
#define SetDialogObject(d, obj) (GetDialogObject(d) = obj)
#define GetDialogData(d) ((DialogData **) GetWRefCon(d))

extern DialogItemData *GetDialogItemData(), FindItemData();

typedef struct {
  long Object;                                /* elements of window_data */
  int idleOn, frontOnly;                      /* elements of window_data */
  int mouse_x, mouse_y;                       /* elements of window_data */
  WindowPtr window;
  int (*FreeMem)();
  ColorCode backColor, drawColor;
  int canvasWidth, canvasHeight;
  int lineType, drawMode, lineWidth;
  long RefCon;
  int use_color;
  int hasHscroll, hasVscroll, view_h, view_v;
  int v_scroll_inc[2], h_scroll_inc[2];
  ControlHandle hscroll, vscroll;
  int initialized;
  int symbolMode;
  int cursor;
  Rect clip_rect;
  int clipped;
} StGWWinInfo;

#endif MACINTOSH

#ifdef AMIGA

typedef struct {
   int type,itemNumber,clusterLeader,clusterSize;
   struct Gadget *itemPtr;
   LVAL object;}
DialogItemData;

typedef struct {
   LVAL object;
   int count;
   struct Gadget *gadl;
   struct DialogItemData *pid;}
DialogData;

typedef struct Window *DialogPtr;

#define GetDialogData(d)       ((DialogData *)(d->UserData))
#define DialogItemCount(d)     count_hardware_items(slot_value(GetDialogObject(d),s_items))
#define GetDialogObject(d)     (((StGWWinInfo *)GetDialogData(d))->Object)
#define SetDialogObject(d,obj) (GetDialogObject(d)=obj)

typedef struct {
  LVAL Object;                               /* elements of window_data */
  int idleOn,frontOnly;                      /* elements of window_data */
  int mouse_x,mouse_y;                       /* elements of window_data */
  struct Window *window;
/*  void (*FreeMem)(IVIEW_WINDOW);*/
  ColorCode backColor,drawColor;
  int canvasWidth,canvasHeight;
  int lineType,drawMode,lineWidth;
  IView RefCon;
  int use_color;
  int hasHscroll,hasVscroll,view_h,view_v;
  int v_scroll_inc[2],h_scroll_inc[2];
  struct Gadget *hscroll,*vscroll;
  int initialized;
  int symbolMode;
  int cursor;
  int clipped;
  Rect clip_rect;
} StGWWinInfo;

#endif AMIGA

#endif IVIEWDEF_H
