#import "Book.h"
#import "Nutation.h"
// Superclass file goes on next line:
#pragma .h #import "Glyph.h"

@implementation Book:Glyph
{ char *name ;
  int currentPage ;
  BOOL stealHits ;
}

- currentPage: (int) pageNum ;
{ // make pageNum the current page...does
  // not redisplay, only sets it. 0-based.
  currentPage = pageNum ;
  return self ;
}

- (int) currentPage ;
{ return currentPage ;
}

- display: (NXRect *) aRect ;
{ // only display: my currentPage'th immediate
  // ancestor...do this by temporarily mangling
  // the tree, displaying, then restoring.
  Glyph *saveIs = is  ;
  int i = 0;
  if(!is->flags.isTerminator)
  { for(; i < currentPage ; i++)
    { // (re)number the pages
       if([is isKindOf: objc_getClass("Page")])
          [is pageNum: i] ;
      if(is->then->flags.isTerminator)
        break ; // at last page
      else
        is = is->then ;
    }
  }
  currentPage = i ;
  [super display: aRect] ;
  is = saveIs ;
  return self ;
}

- drawSelf: (NXRect *) aRect ;
{ // Draw frame and the name
  int i ;
  NXRect bRect = frame ;
  bRect.origin.x = bRect.origin.y = 0.0 ;
  // draw a button
  [Glyph drawButton: &bRect] ;
  // display the book's name 
  [Nu ps:
      "/Helvetica findfont 14 scalefont setfont\n"
      "10 10 moveto (%s) show\n",
      [self name], frame.size.width] ;
  // Draw the buttons for prev,next,first,last pages
  bRect.size.height = 20.0 ;
  bRect.size.width = 40.0 ;
  bRect.origin.x = frame.size.width - 163.0 ;
  bRect.origin.y = 5.0 ;
  for(i = 0 ; i < 4 ; i++)
  { [Glyph drawButton: &bRect] ;
     bRect.origin.x += 40.0 ;
  }
  // Put labels in the buttons
  [Nu ps:
    "/Helvetica findfont 12 scalefont setfont\n"
    "%f %f moveto (Prev) show\n"
    "16 0 rmoveto (Next) show\n"
    "16 0  rmoveto (First) show\n"
    "16 0  rmoveto (Last) show\n",
    frame.size.width - 158.0, bRect.origin.y + 7.0 ] ;
    
  return self ;
}

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


- hitTest: (NXPoint *) aPnt ;
{ // only hitTest on the current page
  Glyph *saveIs = is  ;
  Glyph *saveThen  ;
  Glyph *rval ;
  int i = 0 ;
  if(stealHits) // then don't look any deeper...
  { if(NXPointInRect(aPnt,&frame))
      return self ;
    else
      return [then hitTest: aPnt] ;
  }
  if(!is->flags.isTerminator)
  { for(; i < currentPage ; i++)
    { if(is->then->flags.isTerminator)
        break ; // at last page
      else
        is = is->then ;
    }
    currentPage = i ;
    saveThen = is->then ;
    is->then = [Glyph terminator] ; 
    rval = [super hitTest: aPnt] ;
    is->then = saveThen ;
    is = saveIs ;
    return rval ;
  }
  else
    return [super hitTest: aPnt] ; 
} 

- init: (float) x :(float) y ;
{ self = [super init: x :y] ;
  currentPage = 0 ;
  stealHits = NO ;
  return self ; 
}

- isBook ;
{ return self ;
}

- mouseDown: (NXEvent *) anEvent ;
{ // find out if mouse is in our control area; take
  // appropriate action if it is...i.e.change the
  // current page, without changing the current
  // target
  NXPoint location = anEvent->location ;
  [[Nu glyphView] convertPoint: &location fromView: nil] ;
  [self convertFromRootGlyph: &location] ;
  if(location.y < 25.0)
  { location.x = location.x - frame.size.width + 160.0 ;
    if(location.x > 0)
    { switch( (int) (location.x / 40.0))
      { case 0: [self showPage: currentPage - 1] ; break ;
        case 1: [self showPage: currentPage + 1] ; break ;
        case 2: [self showPage: 0] ; break ;
        case 3: [self showPage: 99999999] ; break ;
        default: break ;
      }
      return [Nu targetGlyph] ;
    }
  }
  return [super mouseDown: anEvent] ;
}

- (char *) name ;
{ // return "Untitled" if no name set
  if(!name)
    return "Untitled" ;
  return name ;
}

- name: (char *) aString ;
{ // set my name to a copy of aString
  if(name)
   free(name) ;
  name = malloc(strlen(aString) + 1) ;
  strcpy(name,aString) ;
  return self ;
}

- showPage: (int) pageNum ;
{ // show page number pageNum, and make it the current page.
  // Pages are numbered starting at 0.
  currentPage = pageNum ;
  if(currentPage < 0)
    currentPage = 0 ;
  [[Nu rootGlyph] display: nil from: self] ;
  return self ;
}

- stealHits: (BOOL) YESorNO ;
{ stealHits = YESorNO ;
  return self ;
}

@end

