/* Generated by Interface Builder */

#import "TVPrint.h"
#import "FaxView.h"
#import "stdio.h"
#import "strings.h"
#import "appkit/SavePanel.h"

FaxView *faxView ;

@implementation TVPrint

/******************************************************************************/
struct printVals printVals = {
   1270, 0, 1, 0, 0, 0, 1, ""
} ;
extern char realnameoffile[] ;
extern FILE *dvifile, *search() ;
extern char *configpath ;
extern void mysystem(const char *s) ;
extern void configure() ;
extern void error() ;
extern void qstatus() ;

- setUpRealPanel
{
   static int inited = 0 ;

   if (inited == 0) {
      realPanel = [TVPPanel new] ;
      [realPanel setAccessoryView:[self setContentView:nil]] ;
      configure() ;
      pi = [NXApp printInfo] ;
      [self loadPanel] ;
      inited = 1 ;
   }
   return self ;
}

- makeKeyAndOrderFront:anObject;
{
   if (dvifile == NULL) {
      error("no file in window") ;
      return self ;
   }
   [self setUpRealPanel] ;
   [self callDvips:[realPanel runModal]] ;
   [realPanel orderOut:anObject] ;
   return self ;
}

- setReversed:anObject
{
    Reversed = anObject;
    return self;
}

- setSynch:anObject
{
   Synch = anObject;
   return self ;
}

- setOptions:anObject
{
    Options = anObject;
    return self;
}

- setCustomDPI:anObject
{
    CustomDPI = anObject;
    return self;
}

- setCollated:anObject
{
    Collated = anObject;
    return self;
}

- setCompressed:anObject
{
    Compressed = anObject;
    return self;
}

static const char *numberp(const char *s, int *p)
{
   int negative = 0 ;
   int val = 0 ;

   while (*s && *s <= ' ')
      s++ ;
   if (*s == '-') {
      negative = 1 ;
      s++ ;
   }
   while ('0' <= *s && *s <= '9')
      val = val * 10 + (*s++ - '0') ;
   while (*s && *s <= ' ')
      s++ ;
   *p = val ;
   if (*s)
      return s ;
   else
      return 0 ;
}

/*
static const char *pagep(const char *s, int *p, int *seq)
{
   const char *pp = numberp(s, p) ;

   if (pp == 0) {
      *seq = -1 ;
      return pp ;
   }
   if (*pp != '.')
      return pp ;
   return numberp(pp+1, seq) ;
}
 */

- SetCustomdpi:sender
{
    printVals.customchosen = [sender state] ;
    if (numberp([CustomDPI stringValue], &printVals.customdpi) == 0)
       ;
    else
       [CustomDPI selectText:sender] ;
    return self;
}

- SetOptions:sender
{
    strcpy(printVals.options, [Options stringValue]) ;
    return self;
}

- SetCompressed:sender
{
    printVals.compressed = [sender state] ;
    return self;
}

- SetCustomResolution:sender
{
    if (numberp([CustomDPI stringValue], &printVals.customdpi) == 0)
       ;
    else
       [CustomDPI selectText:sender] ;
    return self;
}

- SetReversed:sender
{
    printVals.reversed = [sender state] ;
    return self;
}

- SetSync:sender
{
    printVals.sync = [sender state] ;
    return self;
}

- SetCollated:sender
{
    printVals.collated = [sender state] ;
    return self;
}

- SetResolution:sender
{
    return self;
}

const char *faxname ;

- callDvips:(int)tofile;
{
#define EOSB (sysbuffer+strlen(sysbuffer))
   char sysbuffer[1024] ;
   int resolution = 0 ;
   const char *p ;
   char tempname[40] ;
   extern char *mktemp() ;
   extern void unlink() ;

/*  We get these two values to be sure they are up to date */
   strcpy(printVals.options, [Options stringValue]) ;
   (void)numberp([CustomDPI stringValue], &printVals.customdpi) ;
   if (tofile == NX_CANCELTAG || tofile == NX_PREVIEWTAG)
      return self ;
   if (realnameoffile[0] == 0)
      return self ;
   strcpy(sysbuffer, "dvips") ;
   if ([pi copies] > 1) {
      if (printVals.collated)
         strcat(sysbuffer, " -C ") ;
      else
         strcat(sysbuffer, " -c ") ;
      sprintf(EOSB, "%d", [pi copies]) ;
   }
   if (! [pi isAllPages]) {
      if ([pi firstPage] > -2000000000) {
         strcat(sysbuffer, " -p ") ;
         sprintf(EOSB, "%d", [pi firstPage]) ;
      }
      if ([pi lastPage] < 2000000000) {
         strcat(sysbuffer, " -l ") ;
         sprintf(EOSB, "%d", [pi lastPage]) ;
      }
   }
   if (p = [pi valueForJobFeature:"Resolution"])
      if (sscanf(p, "%d", &resolution) != 1)
         resolution = 0 ;
   if (resolution == 0) {
      if (p = [[pi printer] stringForKey:"DefaultResolution" inTable:"PPD"])
         if (sscanf(p, "%d", &resolution) != 1)
            resolution = 0 ;
   }
   if (printVals.customchosen || resolution) {
      strcat(sysbuffer, " -D ") ;
      sprintf(EOSB, "%d",
         printVals.customchosen ? printVals.customdpi : resolution) ;
   }
   if (printVals.reversed)
      strcat(sysbuffer, " -r") ;
   else
      strcat(sysbuffer, " -r0") ;
   p = [pi paperFeed] ;
   if (p && strcmp(p, "NXManual") == 0)
      strcat(sysbuffer, " -m") ;
   if (printVals.compressed)
      strcat(sysbuffer, " -Z") ;
   else
      strcat(sysbuffer, " -Z0") ;
   if (tofile == NX_SAVETAG) {
/* this is a kludge; anyone got a better way? */
      strcat(sysbuffer, " -o \"") ;
      strcat(sysbuffer, [[SavePanel new] filename]) ;
      strcat(sysbuffer, "\"") ;
   } else if (tofile == NX_OKTAG) {
      p = [[pi printer] name] ;
      if (p) {
         FILE *f ;
         char configname[200] ;

         sprintf(configname, "config.%s", p) ;
         if (f=search(configpath, configname)) {
            fclose(f) ;
            strcat(sysbuffer, " -P ") ;
            strcat(sysbuffer, p) ;
         }
         strcat(sysbuffer, " -o \"\\!lpr -P ") ;
         strcat(sysbuffer, p) ;
         strcat(sysbuffer, "\"") ;
      } else
         strcat(sysbuffer, " -o \\!lpr") ;
   } else if (tofile == NX_FAXTAG) {
      strcat(sysbuffer, " -P fax") ;
      strcpy(tempname, "TeXFAX.XXXXXX") ;
      p = mktemp(tempname) ;
      strcat(sysbuffer, " -o "); 
      strcat(sysbuffer, p) ;
      faxname = p ;
   }
   if (printVals.options[0]) {
      strcat(sysbuffer, " ") ;
      strcat(sysbuffer, printVals.options) ;
   }
   strcat(sysbuffer, " \"") ;
   strcat(sysbuffer, realnameoffile) ;
   strcat(sysbuffer, "\"") ;
   if (printVals.sync == 0)
      strcat(sysbuffer, " &") ;
   qstatus(sysbuffer) ;
   mysystem(sysbuffer) ;
   if (tofile == NX_FAXTAG) {
      if (faxView == 0)
         faxView = [FaxView new] ;
      [faxView faxPSCode:self] ;
      unlink(faxname) ;
   }
   return self ;
}

- loadPanel
{
   [CustomDPI setIntValue:printVals.customdpi] ;
   [Collated setState:printVals.collated] ;
   [Reversed setState:printVals.reversed] ;
   [Compressed setState:printVals.compressed] ;
   [Synch setState:printVals.sync] ;
   [Options setStringValue:printVals.options] ;
   return self ;
}

@end

@implementation TVPPanel

+ new
{
   return [super new] ;
}

@end
