/* Generated by tgr */

#import "PageView.h"
#import "SheetView.h"
#import <dpsclient/wraps.h>
#import "TVWindow.h"
#import "dviAccessors.h"
#import <appkit/NXCursor.h>
#import <appkit/Application.h>

@implementation SheetView

SheetView *myView ;
PageView *myPageView ;

extern shalfword hh, vv ;
extern shalfword hhoffset, vvoffset ;
extern shalfword hhmin, hhmax, vvmin, vvmax ;
extern shalfword hhsize, vvsize ;
extern shalfword rhhsize, rvvsize ;
extern shalfword hhmarg, vvmarg ;
extern Boolean orientationset ;
extern Boolean landscape ;
extern Boolean dualpage ;
extern FILE *dvifile ;
extern integer pagenum, seq ;
extern fontdesctype *curfnt ;
extern char realname[] ;
Boolean pagedrawn ;

extern id fpageinit() ;
extern void makeraster(), outline() ;
extern void dopage(), error() ;
extern void free(), pagereport() ;
extern void tolandscape() ;
extern void reportname() ;
extern void TPSinit(), TPSselfont(), TPSshow(), TPSQshow(), TPSmoveto() ;
extern void TPShmove(), TPSxshow() ;
extern void fixalertbug() ;
static int offset = 0 ;

+ newFrame:(const NXRect *)theFrame
{
   NXSize nxs ;
   self = [super newFrame:theFrame];
   nxs.width = mhhsize = 1036 ;
   nxs.height = mvvsize = 1336 ;
   cache = [NXImage newSize:&nxs] ;
   [cache setFlipped:YES] ;
   return self ;
}

id makenewdoc(hsize, vsize)
int hsize, vsize ;
{
   return [myView makeNewDoc:hsize:vsize] ;
}

- makeNewDoc:(int)hsize:(int)vsize
{
   if (hsize == ohhsize && vsize == ovvsize)
      return 0 ;
   ohhsize = hsize ;
   ovvsize = vsize ;
   [myPageView noScrollUpdates] ;
   [myPageView cachePosition:hsize:vsize] ;
   if (hsize > mhhsize || vsize > mvvsize) {
      NXSize nxs ;
      if (hsize > mhhsize)
         mhhsize = hsize ;
      if (vsize > mvvsize)
         mvvsize = vsize ;
      nxs.width = mhhsize ;
      nxs.height = mvvsize ;
      [cache setSize:&nxs] ;
   }
   offset = mvvsize - vsize ;
   [self sizeTo:hsize:vsize] ;
   [myPageView unCachePosition:hsize:vsize] ;
   [myPageView scrollUpdates] ;
   return self ;
}

- update
{
   if ([self needsDisplay])
      [self display] ;
   return(self) ;
}

static int dontbother = 1 ;

void display()
{
   dontbother = 1 ;
   (void)fpageinit() ;
   dontbother = 0 ;
   [myView display] ;
}

- drawSelf:(NXRect *)rects :(int)rectCount
{
    NXRect r ;
    if (dontbother)
       return self ;
    if ((! pagedrawn)) {
       [cache lockFocus] ;
       if (offset)
          PStranslate(0, offset) ;
/*     PSsetgray(1.0) ; */
       NX_WIDTH(&r) = rhhsize ;
       NX_HEIGHT(&r) = rvvsize ;
       NX_X(&r) = 0 ;
       NX_Y(&r) = 0 ;
       NXEraseRect(&r) ;
/*     PSclippath() ;
       PSfill() ; */
       PSsetgray(0.0) ;
       outline() ;
       if (dvifile != NULL)
          dopage() ;
       [cache unlockFocus] ;
#ifdef TRACKCACHE
       { int bsize, bmax, msize ;
         PScachestatus(&bsize, &bmax, &msize) ;
         printf("bsize=%d bmax=%d msize=%d\n", bsize, bmax, msize) ;
       }
       { int used, max, level ;
         PSvmstatus(&level, &used, &max) ;
         printf("vmused=%d\n", used) ;
       }
#endif
    }
    PSflush() ;
/*  [self lockFocus] ; */
    [cache composite:NX_COPY fromRect:rects toPoint:&rects->origin];
/*  [self unlockFocus] ; */
    return self;
}

- free
{
   if (cache) [cache free];
   return [super free];
}

#define SHOWBUFSIZE (2000)
static char showbuf[SHOWBUFSIZE] ;
static char *showbufp = showbuf ;
static short showx[SHOWBUFSIZE] ;
static short *showxp = showx ;
static int rhh = -3200, rvv = -3200 ;

void strflush() {
   if (showx[0])
      TPShmove(showx[0]) ;
   if (showbufp > showbuf) {
      if (rhh > -3000) {
         *showxp++ = hh - rhh ;
         rhh = hh ;
      } else
         *showxp++ = 0 ;
      TPSxshow(showbuf, &(showx[1]), showbufp - showbuf) ;
/*    TPSshow(showbuf, showbufp - showbuf) ; */
      showbufp = showbuf ;
      showxp = showx ;
   }
}

void texflush() {
   strflush() ;
   rhh = -3200 ;
   rvv = -3200 ;
}

void fdrawrule(xx, yy)
halfword xx, yy ;
{
   register shalfword hmin, hmax, vmin, vmax ;

   if (xx>0 && yy>0) {
      hmin = hh+hhoffset ;
      hmax = hmin + xx - 1 ;
      vmax = vv+vvoffset ;
      vmin = vmax - yy + 1 ;
      [myView drawRule:xx:yy] ;
   }
}

- drawRule:(int)x:(int)y
{
   PSrectfill(hh + hhoffset, vv + vvoffset - y, x, y) ;
   return(self) ;
}

@end

void hvpos() {
   texflush() ;
   TPSmoveto(hh + hhoffset, vv + vvoffset) ;
   rhh = hh ;
   rvv = vv ;
}

void fdrawchar(c, e)
char c ;
int e ;
{
   if (rvv == vv) {
/*    if (rhh != hh) {
         if (showbufp > showbuf) {
            TPSQshow(showbuf, showbufp-showbuf, hh-rhh) ;
            showbufp = showbuf ;
         } else
            TPSmoveto(hh + hhoffset, vv + vvoffset) ;
         rhh = hh ;
      } */
      if (rhh < -3000)
         hvpos() ;
   } else {
      strflush() ;
      TPSmoveto(hh + hhoffset, vv + vvoffset) ;
      rhh = hh ;
      rvv = vv ;
   }
   *showbufp++ = c ;
   *showxp++ = hh - rhh ;
   rhh = hh ;
}
/*
 *   Set things up for a new page, including calculating the number of
 *   raster rows we have in the buffer.  Everything must be 16-bit
 *   aligned.
 */
id fpageinit() {
   if (landscape && !orientationset)
      tolandscape() ;
   return makenewdoc(rhhsize, rvvsize) ;
}

void outline() {
   int safehh, safevv ;

   pagereport(pagenum, seq) ;
   safehh = hh ; safevv = vv ;
   hh = - hhmarg - 2 ; vv = - vvmarg - 1 ;
   fdrawrule(hhsize + 4, 2) ;
   vv = vvsize - vvmarg ;
   fdrawrule(2, vvsize + 2) ;
   fdrawrule(hhsize + 4, 2) ;
   hh = hhsize - hhmarg ;
   fdrawrule(2, vvsize + 2) ;
   if (dualpage) {
      hh = hhsize / 2 - hhmarg - 1 ;
      fdrawrule(2, vvsize + 2) ;
   }
   hh = safehh ;
   vv = safevv ;
   orientationset = 1 ;
}
