/*  
 *
 *                              wind.c
 *
 *
 *          Procedures for setting proper window size
 *
 *   Marek Krawus,  PCC,  March '89
 *
 */

#include "defn.h"

maxwindow()
{
   end_draw();
   fprintf(outfile, "%g %g %g %g SetWindowClip   \n", FULLPAGE, FULLPAGE, FULLPAGE, FULLPAGE);
}

setwindow()
{
   float  XL, YL, XU, YU;
   float  xx;

   end_draw();
   if (test_token())
      {
	XL = getval() * XSCALE_ABS;
	if (XL < 0.0)  XL = 0.0;
      }
    else
      { fprintf(stderr, "ERROR: wrong number of parameters in IW instruction\n");
	return;
     }
   if (test_token())
      {
	YL = getval() * YSCALE_ABS;
	if (YL < 0.0)  YL = 0.0;
      }
    else
      { fprintf(stderr, "ERROR: wrong number of parameters in IW instruction\n");
	return;
      }
   if (test_token())
      {
	XU = getval() * XSCALE_ABS;
	if (XU < 0.0)  XU = 0.0;
      }
    else
      { fprintf(stderr, "ERROR: wrong number of parameters in IW instruction\n");
	return;
      }
   if (test_token())
      {
       YU = getval() * YSCALE_ABS;
       if (YU < 0.0)  YU = 0.0;
      }
    else
      { fprintf(stderr, "ERROR: wrong number of parameters in IW instruction\n");
	return;
      }

   if ( XL > XU ) 
      {
	xx = XL;
	XL = XU;
	XU = xx;
      }
   if ( YL > YU )
      {
        xx = YL;
	YL = YU;
	YU = xx;
      }
  
   fprintf(outfile, "%g %g %g %g SetWindowClip\n", XL, YL, XU, YU);
 }


rotate_coordinate()
{
   float  angle;

   end_draw();
   if (test_token())
	angle = getval();
    else
	angle = 0.0;

   if ((angle != 0.0) && (angle != 90.0))  return;

   set_rotate( (int)angle );
}

set_rotate( angle )
int angle;
{
 float  xmov;
 float  ymov;

  xmov = 0.0;
  ymov = 0.0;

  if ( ROTATE_STATE == 0 )  {
    if ( angle == 90 )   
     { 
	 fprintf(outfile, "0 0 moveto \n");
         fprintf(outfile, "%g mm %g mm translate \n", - Xshift, - Yshift ); 

	 if (strcmp (PaperSize, "A3") == 0)
	 {
	   xmov = MaxA3RangeX * RESOLUTION * XSCALE_ABS;
         }
	 else   /*  PaperSize == A4   */
	 {
	   xmov = MaxA4RangeX * RESOLUTION * XSCALE_ABS;
         }

         fprintf(outfile, "%g mm %g mm translate \n", xmov, ymov ); 
         fprintf(outfile, "-270 rotate \n");
         fprintf(outfile, "%g mm %g mm translate \n", Xshift, Yshift ); 

         fprintf(outfile, "AdjWindowClip \n");
         fprintf(outfile, "%g mm %g mm moveto \n");

	 ROTATE_STATE = 1;
    }
  }
  else  /*  ROTATE_STATE == 1 */
  {
    if ( angle == 0 )  
    {
	 fprintf(outfile, "0 0 moveto \n");
         fprintf(outfile, "%g mm %g mm translate \n", - Xshift, - Yshift ); 

	 if (strcmp (PaperSize, "A3") == 0)
	 {
	   ymov = MaxA3RangeX * RESOLUTION * XSCALE_ABS;
         }
	 else   /*  PaperSize == A4   */
	 {
	   ymov = MaxA4RangeX * RESOLUTION * XSCALE_ABS;
         }

         fprintf(outfile, "%g mm %g mm translate \n", xmov, ymov ); 
         fprintf(outfile, "-90 rotate \n");
         fprintf(outfile, "%g mm %g mm translate \n", Xshift, Yshift ); 

         fprintf(outfile, "AdjWindowClip \n");
         fprintf(outfile, "%g mm %g mm moveto \n");

	 ROTATE_STATE = 0;
    }
  }
}
