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

/* Asks for number of plotting device, and call plbeg to divide the */
/* page into nx by ny subpages */

static int mk=0, sp=0;

void plstar(nx,ny)
int nx, ny;
{
   int dev,lev;

   glev(&lev);
   if (lev != 0) plend();
      
   printf("\nPlotting Options:");
   printf("\n < 1> Amiga");
   printf("\n < 2> LaserJet II file    (landscape)");
   printf("\n < 3> LaserJet II file    (portrait)");
   printf("\n < 4> imPRESS     file    (landscape)");
   printf("\n < 5> imPRESS     file    (portrait)");
   printf("\n < 6> Tektronix   file    (landscape)");
   printf("\n < 7> Tektronix   file    (portrait)");
   printf("\n");
   printf("\nEnter device number: ");
   scanf("%d",&dev);
   while(getchar() != '\n') /* Read all of line including new line char */
       ;
   plbeg(dev,nx,ny);
}

/* Initializes the graphics device "dev"  */
void grbeg(dev)
int dev;
{
   int termin, phyxmi, phyxma, phyymi, phyyma;
   void grinit(), grclr(), grcol(), grgra();

   /* Set up device specific stuff */

   /* Use setpxl to set the device dots per mm in the x and y directions */
   /* dpmmx = dots/mm in x direction (float) */
   /* dpmmy = dots/mm in y direction (float) */
   /* setpxl(dpmmx,dpmmy); */

   /* PLPLOT assumes that the plot origin is in the lower left corner */
   /* with y increasing upward and x increasing to the right */
   /* Use setphy to specify how PLPLOT should translate plotting coords */
   /* to device coords */
   /* xmindev = minimum x device coordinate (int) */
   /* xmaxdev = maximum x device coordinate (int) */
   /* ymindev = minimum y device coordinate (int) */
   /* ymaxdev = maximum y device coordinate (int) */
   /* setphy(xmindev,xmaxdev,ymindev,ymaxdev); */

   if (dev == 1) {
     setpxl(2.52,2.25);
     setphy(0,639,0,399);
     termin = 1;
   }
   else if (dev == 2) {
       setpxl(5.905,5.905);  /* 150 dpi mode */
       termin = 0;
       setphy(0,1409,0,1103);
   }
   else if (dev == 3) {
       setpxl(5.905,5.905);  /* 150 dpi mode */
       termin = 0;
       setphy(0,1103,1409,0);
   }
   else if (dev == 4) {
       setpxl(11.81,11.81);  /* 300 dpi */
       termin = 0;
       setphy(0,2999,0,2249);
   }
   else if (dev == 5) {
       setpxl(11.81,11.81);  /* 300 dpi */
       termin = 0;
       setphy(2249,0,0,2999);
   }
   else if (dev == 6 )  {
       setpxl(4.771,4.653);
       termin = 0;
       setphy(0,1023,0,779);
   }
   else if (dev == 7)  {
       setpxl(4.653,4.771);
       termin = 0;
       setphy(779,0,0,1023);
   }
   else {
     printf("\nUnknown output device\n");
     exit(1);
   }

   sdev(dev,termin,0);
   grinit();

   /* Set default sizes, fonts, and colors */
   plschr(4.0,1.0);
   plssym(4.0,1.0);
   plsmaj(3.0,1.0);
   plsmin(1.5,1.0);
   satt(1,1);

   /* Switch to graphics mode and clear screen */
   grgra();
   grclr();
   grcol();
   plstyl(0,&mk,&sp);
      
   gphy(&phyxmi,&phyxma,&phyymi,&phyyma);
   sclp(phyxmi,phyxma,phyymi,phyyma);
}

/* Clears the graphics screen */
void grclr()
{
      if (device == 1)
        amiclr();
      else if (device == 2 || device == 3)
        jetclr();
      else if (device == 4 || device == 5)
        impclr();
      else if (device == 6 || device == 7)
        tekclr();
}

/* Sets up the line colour to value in global variable "colour" */
void grcol()
{
      if (device == 1)
        amicol(colour);
}

/* Initializes the graphics device */
void grinit()
{
      if (device == 1)
        amiini();
      else if (device == 2 || device == 3)
        jetini();
      else if (device == 4 || device == 5)
        impini();
      else if (device == 6 || device == 7)
        tekini();
}

/* Switches to graphics mode */
void grgra()
{
      if (device == 1)
        amigra();
      else if (device == 2 || device == 3)
        jetgra();
      else if (device == 4 || device == 5)
        impgra();
      else if (device == 6 || device == 7)
        tekgra();

      graphx = 1;
}

/* Draws a line from (x1,x2) to (x2,y2), used by genlin() */
/* Notice how x and y are swapped to switch from the default */
/* orientation */
void grline(x1,y1,x2,y2)
int x1,y1,x2,y2;
{
      if (device == 1)
        amilin(x1,y1,x2,y2);
      else if (device == 2)
        jetlin(y1,x1,y2,x2);
      else if (device == 3)
        jetlin(x1,y1,x2,y2);
      else if (device == 4)
        implin(x1,y1,x2,y2);
      else if (device == 5)
        implin(y1,x1,y2,x2);
      else if (device == 6)
        teklin(x1,y1,x2,y2);
      else if (device == 7)
        teklin(y1,x1,y2,x2);
}

/* Switches to text mode */
void grtext()
{
      if (device == 1)
        amitex();
      else if (device == 2 || device == 3)
        jettex();
      else if (device == 4 || device == 5)
        imptex();
      else if (device == 6 || device == 7)
        tektex();

      graphx = 0;
}

/* Called by plend to tidy up graphics device */
void grtidy()
{
      if (device == 1)
        amitid();
      else if (device == 2 || device == 3)
        jettid();
      else if (device == 4 || device == 5)
        imptid();
      else if (device == 6 || device == 7)
        tektid();
}
