/* This file contains the IMPRESS device dependent subroutines for */
/* use with plplot. */

#include "plplot.h"
#include <stdio.h>

#define ESC          27
#define HP7470X   10299
#define HP7470Y    7649

static FILE *OutFile;
static int porient;
static int select=0;
static char FileName[80];
static int curpen, curwid;

void hp7470setup(xdpi,ydpi,xwid,ywid)
PLINT xwid, ywid;
PLFLT xdpi, ydpi;
{
}

/* Open file.  Set up for graphics. */
void hp7470init()
{
      char response[80];
      int ori;

      smod(0);  /* not an interactive terminal */
      scol(1);
      swid(1);
      setpxl(40.,40.);
      
      if(!select) {
         printf("Landscape or portrait orientation? (0 or 1): ");
         fgets(response,sizeof(response),stdin);
         if(sscanf(response,"%d",&ori) != 1)
            ori = 0;   /* carriage return defaults to landscape */
      }
      
      porient = ori;
      if(!porient)
         setphy(0,HP7470X,0,HP7470Y);
      else
         setphy(0,HP7470Y,0,HP7470X);

      OutFile = NULL;
      while(!OutFile) {
         if(!select) {
            printf("Enter graphics command storage file name. ");
            fgets(response,sizeof(response),stdin);
            if(sscanf(response,"%s",FileName) != 1) {
                printf("Invalid entry.n");
                continue;
            }
         }
         if ((OutFile = fopen(FileName,"w")) == NULL) 
             printf("Can't open %s.\n",FileName);
         select=0;
      }
      fprintf(OutFile,"%c.I200;;17:%c.N;19:%c.M;;;10:in;\n",ESC,ESC,ESC);
}

void hp7470select(ori,file)
PLINT ori;
char *file;
{
   porient = ori;
   strncpy(FileName,file,79);
   FileName[79]='\0';
   select = 1;
}

/* Sets the IMPRESS to text mode */
void hp7470text()
{
}

/* Sets the IMPRESS to graphics mode */
void hp7470graph()
{
}

/* Form feed */
void hp7470clear()
{
}

static PLINT xold, yold;

void hp7470page()
{
   fprintf(OutFile,"pg;\n");
   xold = -100000;
   yold = -100000;
}

void hp7470color(colour)
PLINT colour;
{
  if(colour<1 || colour>8)
    fprintf(stderr,"\nInvalid pen selection.");
  else {
    fprintf(OutFile,"sp%d %d\n",colour,curwid);
    curpen = colour;
  }
}

void hp7470width(width)
PLINT width;
{
  if(width<1 || width>48)
    fprintf(stderr,"\nInvalid pen width selection.");
  else {
    fprintf(OutFile,"sp%d %d\n",curpen,width);
    curwid = width;
  }
}

void hp7470line(x1,y1,x2,y2)
PLINT x1,y1,x2,y2;
{

      if(!porient) {
         if(x1 == xold && y1 == yold) 
           /* Add new point to path */
           fprintf(OutFile,"pd%d %d\n",x2,y2);
         else 
           /* Write out old path */
           fprintf(OutFile,"pu%d %d pd%d %d\n",x1,y1,x2,y2);
      }
      else {
         if(x1 == xold && y1 == yold) 
           /* Add new point to path */
           fprintf(OutFile,"pd%d %d\n",HP7470X-y2,x2);
         else 
           /* Write out old path */
           fprintf(OutFile,"pu%d %d pd%d %d\n",HP7470X-y1,x1,HP7470X-y2,x2);
      }
        
      xold = x2;
      yold = y2;
}
      
/* Close graphics file */
void hp7470tidy()
{
      fprintf(OutFile,"sp0\n");
      fclose(OutFile);
      select = 0;
}

