/* Generated by Interface Builder */

#import "TeXviewObject.h"
#import "structures.h"
#import "dviAccessors.h"
#import <dpsclient/wraps.h>
#import <strings.h>

@implementation TeXviewObject

TeXviewObject *myTeXviewObject ;
extern int existsize100[] ;
extern void todpi(), error() ;
extern void recalculate(), resetsizes() ;
extern void display() ;
extern void saveconfig() ;
extern float screendpi ;
extern Boolean generatefonts, pagedrawn ;

static char text[20] ;
char *dpistring(i)
int i ;
{
   sprintf(text, "%3d", i) ;
   return(text) ;
}

void reportnewdpi() {
   [myTeXviewObject setDpiTitle:dpistring((int)(screendpi+0.5))] ;
}

- setDpiTitle:(char *)s
{
   int i ;

   i = [popup indexOfItem:s] ;
   if (i != -1) {
      [[popup itemList] selectCellAt:i:0] ;
      [dpiPopup setTitle:s] ;
   }
   return self ;
}

- setSizeMatrix:anObject
{
   sizeMatrix = anObject ;
   [self setsizes] ;
   return self ;
}

- setGenerateButtons:anObject
{
   generateButtons = anObject ;
   [self setsizes] ;
   return self ;
}

- setStatus:anObject
{
   status = anObject ;
   return self ;
}

- setZoomed:anObject
{
   zoomed = anObject ;
   return self ;
}

- zoom
{
   return [zoomed setState:1] ;
}

- unzoom
{
   return [zoomed setState:0] ;
}

- getStatus
{
   return status ;
}

extern void writeconsole() ;

void setstatus(s)
const char *s ;
{
   writeconsole(s) ;
   [[myTeXviewObject getStatus] setStringValue:s] ;
   PSflush() ;
}

void qstatus(s)
const char *s ;
{
   [[myTeXviewObject getStatus] setStringValue:s] ;
   PSflush() ;
}

- setDpiPopup:anObject
{
    id popuplist ;
    int i ;

    dpiPopup = anObject;
    myTeXviewObject = self ;
    popup = [PopUpList new] ;
    popuplist = popup ;
    [popup changeButtonTitle:YES] ; /* we'll do it ourselves */
    for (i=1; i<13; i++)
       [popuplist addItem:dpistring(existsize100[i])] ;
    [popuplist addItem:dpistring(300)] ;
    [popuplist addItem:dpistring(400)] ;
    NXAttachPopUpList(anObject, popuplist) ;
    [popup setAction:@selector(cdpi:)] ;
    [popup setTarget:myTeXviewObject] ;
    return self;
}

- popIt:anObject
{
    [popup popUp:anObject] ;
    return self ;
}

- setPageForm:anObject
{
    pageForm = anObject;
    return self;
}

- setPageNum:(char *)p
{
    [pageForm setStringValue:p at:0] ;
    return self ;
}

static NXEvent ne ;

- cdpi:anObject
{
    ne.type = NX_APPDEFINED ;
    strcpy(ne.data.compound.misc.C, [[anObject selectedCell] title]) ;
    DPSPostEvent(&ne, 1) ;
    return self ;
}

- firePage
{
    [pageForm selectTextAt:0] ;
    return self ;
}

NXEvent nw ;
- startRandom:(NXEvent *)e
{
    [pageForm setStringValue:"" at:0] ;
    [pageForm selectTextAt:0] ;
    nw = *e ;
    DPSPostEvent(&nw, 1) ;
    return self ;
}

- changeGenerate:(Matrix *)anObject
{
    generatefonts = ! [anObject selectedRow] ;
    return self ;
}
extern real defhsize, defvsize, defhmarg, defvmarg ;
extern real hsize, vsize, hmarg, vmarg ;
static int error_inc ;
static real check(siz, low, high, def)
real siz, low, high, def ;
{
   error_inc = 0 ;
   if (siz < low) {
      error("Size too small") ;
      return(def) ;
   } else if (siz > high) {
      error("Size too large") ;
      return(def) ;
   } else {
      error_inc = 1 ;
      return(siz) ;
   }
}
int checksizes(anObject)
id anObject ;
{
   float newsize ;
   int i ;

   for (i=0; i<4; i++) {
      newsize = [[anObject cellAt:i:0] floatValue] ;
      switch (i) {
case 0:   hsize = check(newsize, 1.0, 50.0, 8.5) ; break ;
case 1:   vsize = check(newsize, 1.0, 50.0, 11.0) ; break ;
case 2:   hmarg = check(newsize, -5.0, 5.0, 1.0) ; break ;
case 3:   vmarg = check(newsize, -5.0, 5.0, 1.0) ; break ;
      }
      if (error_inc == 0) {
         [anObject selectCellAt:i:0] ;
         return 1 ;
      }
   }
   recalculate() ;
   return 0 ;
}
- changeSize:(Matrix *)anObject
{
    float newsize ;
    int i ;

    newsize = [[anObject selectedCell] floatValue] ;
    switch (i=[anObject selectedRow]) {
case 0:   hsize = check(newsize, 1.0, 50.0, 8.5) ; break ;
case 1:   vsize = check(newsize, 1.0, 50.0, 11.0) ; break ;
case 2:   hmarg = check(newsize, -5.0, 5.0, 1.0) ; break ;
case 3:   vmarg = check(newsize, -5.0, 5.0, 1.0) ; break ;
    }
    recalculate() ;
    [anObject selectCellAt:(i + error_inc) % 4:0] ;
    return self ;
}

- setsizes
{
   if (generatefonts)
      [generateButtons selectCellAt:0:0] ;
   else
      [generateButtons selectCellAt:1:0] ;
   [[sizeMatrix cellAt:0:0] setFloatValue:hsize] ;
   [[sizeMatrix cellAt:1:0] setFloatValue:vsize] ;
   [[sizeMatrix cellAt:2:0] setFloatValue:hmarg] ;
   [[sizeMatrix cellAt:3:0] setFloatValue:vmarg] ;
   return self ;
}
- saveConfig:(Matrix *)anObject
{
    if (checksizes(sizeMatrix))
       return self ;
    defhsize = hsize ;
    defvsize = vsize ;
    defhmarg = hmarg ;
    defvmarg = vmarg ;
    if ([anObject selectedRow] == 0)
       saveconfig() ;
    [[anObject window] orderOut:anObject] ;
    pagedrawn = 0 ;
    display() ;
    return self ;
}
- defaultSize:anObject
{
    resetsizes() ;
    pagedrawn = 0 ;
    return self ;
}

void notifysizes() {
   if (myTeXviewObject)
      [myTeXviewObject setsizes] ;
}

@end
