/* GNUPLOT - amiga.trm */
/*
 * Copyright (C) 1991, 1992, 1995
 *
 * Permission to use, copy, and distribute this software and its
 * documentation for any purpose with or without fee is hereby granted, 
 * provided that the above copyright notice appear in all copies and 
 * that both that copyright notice and this permission notice appear 
 * in supporting documentation.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the modified code.  Modifications are to be distributed 
 * as patches to released version.
 *  
 * This software  is provided "as is" without express or implied warranty.
 * 
 * This file is included by ../term.c.
 *
 * This terminal driver supports:
 *   Amiga Workbench window
 *
 * AUTHORS
 *   Carsten Steger
 * 
 *   Pat R. Empleo      Slightly modified for Aztec C v5.2a (beta); sort of
 *   08/27/91           supports overscan; for large WB 2.0 virtual screens,
 *                      we limit the plot size so we don't have to scroll
 *                      around (not fun).
 *
 *   Carsten Steger     Modified to support Kickstart 2.0.
 *   09/11/91           Opens a text overscan screen when used with WB 2.0.
 *                      Discerns between NTSC and PAL Amigas when used with
 *                      WB 1.3 and lower.
 *
 *   Pat R. Empleo      Defined some 2.0 stuff in order to get Aztec C to
 *   09/20/91           work with Carsten's new code (see above).  When
 *                      KS/WB 2.0 support gets implemented in Aztec C, this
 *                      kludge will get deleted!
 *                      (Aztec C release 5.2 beta)
 *
 *   George Coulouris   Significant rewrite. Now opens a 512x320 window on
 *   glc5@cornell.edu   the Workbench screen, allocates pens with
 *   12/12/95           ObtainBestPen(). Requires AmigaDOS 3.0.
 *                      Source is commented a little better than before.
 *
 * send your comments or suggestions to (info-gnuplot@dartmouth.edu).
 * 
 */
#include <stdio.h>

#include <exec/types.h>
#include <intuition/intuitionbase.h>
#include <graphics/gfxbase.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/exec.h>

#define AMIGA_VTF(y) (AMIGA_ymax-1-(y))
#define AMIGA_XMAX 512
#define AMIGA_YMAX 320
#define AMIGA_VCHAR (12)
#define AMIGA_HCHAR (8)
#define AMIGA_VTIC (AMIGA_YMAX/80)
#define AMIGA_HTIC (AMIGA_XMAX/80)

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Window *plotwin;

static enum JUSTIFY AMIGA_justify = LEFT;
static WORD AMIGA_bsl, AMIGA_cht, AMIGA_vadj;
static unsigned int AMIGA_xmax, AMIGA_ymax;

/*
 * This  is the palette.  Values are stored as 0xrrggbb, where rr, gg, and, bb
 * are big-endian 8-bit intensities for red, green, and blue, respectively.
 */

static unsigned int palette[10] =
	{
	0xffffff,	/* white */
	0x000000,	/* black */
	0xff0000,	/* red */
	0x00ff00,	/* green */
	0x0000ff,	/* blue */
	0x00ffff,	/* cyan */
	0xff00ff,	/* magenta */
	0xffff00,	/* yellow */
	0x7f007f,	/* purple */
	0xff7f00,	/* orange */
	};

/* This is the color look-up table, indexed in the same order as
the above palette. The values stored in this table are pen numbers;
e.g clut[2] is the pen which represents the color "red". */

static unsigned int clut[10];

AMIGA_reset()
{
int i;

/* Free the pens */
for(i=0;i<11;i++)
	if ( clut[i] != -1 )
		ReleasePen(plotwin->WScreen->ViewPort.ColorMap, clut[i]);

/* Close the window */
  if (plotwin != NULL) CloseWindow(plotwin);

/* Close libraries */
  if (IntuitionBase != NULL) CloseLibrary(IntuitionBase);
  if (GfxBase != NULL) CloseLibrary(GfxBase);
}

AMIGA_init()
{
int i, r,g,b;

/* Initialize the clut */
for(i=0;i<11;i++)
	clut[i] = -1;

/* Open libraries */

GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",39);

if (GfxBase == NULL)
	{
	fprintf(stderr,"*** Could not load graphics.library 39\n\n");
	AMIGA_reset();
	exit(20);
	}

IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",36);

if (IntuitionBase == NULL)
	{
	fprintf(stderr,"*** Could not load intuition.library 36\n\n");
	AMIGA_reset();
	exit(20);
	}

/* Open the plot window */

plotwin = (struct Window *) OpenWindowTags(NULL,
	WA_InnerWidth, 512,
	WA_InnerHeight, 320,
	WA_Title, "gnuplot",
	WA_DragBar, TRUE,
	WA_DepthGadget, TRUE,
	WA_WBenchWindow, TRUE,
	WA_SmartRefresh, TRUE,
	WA_GimmeZeroZero, TRUE,
	WA_AutoAdjust, TRUE,
	TAG_DONE);

if (plotwin == NULL)
	{
	fprintf(stderr,"*** Could not open window\n\n");
	AMIGA_reset();
	exit(20);
	}

AMIGA_xmax = 512;
AMIGA_ymax = 320;

term_tbl[term].xmax = AMIGA_xmax;
term_tbl[term].ymax = AMIGA_ymax;

AMIGA_bsl = plotwin->RPort->TxBaseline; /* Reference line */
AMIGA_cht = plotwin->RPort->TxHeight; /* Height of characters */
AMIGA_vadj = AMIGA_bsl / 2;


/* Allocate pens */

for(i=0;i<11;i++)
	{
	r = (palette[i] << 8 ) & 0xFF000000;
	g = (palette[i] << 16) & 0xFF000000;
	b = (palette[i] << 24) & 0xFF000000;
	clut[i] = ObtainBestPenA(plotwin->WScreen->ViewPort.ColorMap,r,g,b,NULL);
	}

  SetDrMd(plotwin->RPort,JAM1);
}


/* Shove the plot window to the back */

AMIGA_text()
{
  char c;

  c = getc(stdin);
  ungetc(c,stdin);
  WindowToBack(plotwin);
}

/* Bring the plot window to the front */

AMIGA_graphics()
{
  SetRast(plotwin->RPort,0);

/* clear the window */

SetAPen(plotwin->RPort,clut[0]);
RectFill(plotwin->RPort,0,0,640,400);

  SetAPen(plotwin->RPort,clut[1]);
  WindowToFront(plotwin);
}

AMIGA_move(x,y)
unsigned int x,y;
{
  Move(plotwin->RPort,x,AMIGA_VTF(y));
}

AMIGA_vector(x,y)
unsigned int x,y;
{
  Draw(plotwin->RPort,x,AMIGA_VTF(y));
}

AMIGA_linetype(int linetype)
{
  if (linetype >= 0) linetype = (linetype % 9) + 1;
  if (linetype < 0) linetype = 1;
  SetAPen(plotwin->RPort,clut[linetype] );
}

AMIGA_put_text(x,y,str)
unsigned int x,y;
char *str;
{
  LONG len,tx_len;
  WORD xmin,xmax,ymin,ymax;

  len = strlen(str);
  tx_len = TextLength(plotwin->RPort,str,len);
  switch (AMIGA_justify) {
    case LEFT:
      xmin = x;
      xmax = x + tx_len;
      break;
    case CENTRE:
      xmin = x - tx_len / 2;
      xmax = x + tx_len - tx_len / 2; /* aviod roundoff errors ! */
      break;
    case RIGHT:
      xmin = x - tx_len;
      xmax = x;
      break;
  }
  ymin = AMIGA_VTF(y) - AMIGA_vadj;
  ymax = ymin + AMIGA_cht;
  /* Check if character-string lies completely within the screen: */
  if ((xmax >= AMIGA_xmax) || (ymin < 0) || (ymax >= AMIGA_ymax)) return;
  Move(plotwin->RPort,xmin,ymin+AMIGA_bsl);
  Text(plotwin->RPort,str,len);
}

int AMIGA_justify_text(mode)
enum JUSTIFY mode;
{
  AMIGA_justify = mode;
  return TRUE;
}
