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

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

#define HPXMIN  -4500
#define HPXMAX   4500
#define HPYMIN  -2790
#define HPYMAX   2790
#define ESC    27

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

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

/* Open file.  Set up for graphics. */
void hp7580init()
{
      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(HPXMIN,HPXMAX,HPYMIN,HPYMAX);
      else
         setphy(HPYMIN,HPYMAX,HPXMIN,HPXMAX);

      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);
      fprintf(OutFile,"ro 90;ip;sp 4;pa;");
}

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

/* Sets to text mode */
void hp7580text()
{
}

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

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

static int xold, yold;

void hp7580page()
{
   xold=-100000; yold=-100000;
}

/* May put something here someday */
void hp7580color(colour)
PLINT colour;
{
  if(colour<1 || colour>8)
    fprintf(stderr,"\nInvalid pen selection.");
  else
    fprintf(OutFile,"sp%d\n",colour);
}

void hp7580width(width)
PLINT width;
{
}

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

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

