#import "Page.h"
#import "Nutation.h"
#import <dpsclient/psops.h>

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

// set to my book during mouseDown->mouseUp
// event loop

Glyph *currentBook_ = nil ;

@implementation Page:Glyph
{ unsigned pageNum ;
}

- (BOOL) precedes: aGlyph ;
{ // I come before aGlyph is aGlyph is
  // not a kind of page, or if my
  // pageNum is less than aGlyph's pageNum 
  if([aGlyph isKindOf: [self class]])
     return (pageNum < [aGlyph pageNum]) ;
  return NO ;
}


- display: (NXRect *) aRect ;
{ // if I am planted in a Book, I
  // don't display my "then"
  Glyph *saveThen = then  ;
  if(ancestor == [ancestor isBook]) // I must be on a Book
    then = [objc_getClass("Terminator") new] ;
  [super display: aRect] ;
  then = saveThen ;
  return self ;
}

- drawSelf: (NXRect *) bRect ;
{ NXRect aRect = frame ;
  aRect.origin.x = aRect.origin.y = 0.0 ;
  // draw a button...
  [Glyph drawButton: &aRect] ;
  // put a page number at bottom left
  [Nu ps:
    "/Helvetica findfont 12 scalefont setfont\n"
    "5 5 moveto (%d) show\n",
      pageNum] ;
  return self ;
}

- init: (float) width :(float) height ;
{ self = [super init: width :height] ;
  pageNum = 999999999 ; // default page Number
}

- mouseDown: (NXEvent *) anEvent ;
{ // if my ancestor is a book, tell that
  // book to "steal" the hittest
  if(ancestor == [ancestor isBook])
    [(currentBook_ = ancestor) stealHits: YES] ;
  return [super mouseDown: anEvent] ;
}

- mouseUp: (NXEvent *) anEvent ;
{ // tell the book to "unsteal" the hittest
  if(currentBook_)
  { [currentBook_ stealHits: NO] ;
    currentBook_ = nil ;
  }
  return [super mouseUp: anEvent] ;
}

- (unsigned) pageNum ;
{ return pageNum ;
}

- pageNum: (unsigned) aNum ;
{ pageNum = aNum ;
  return self ;
}

- plant: (Glyph *) aGlyph ;
{ // When I am planted in a Book, pages which
  // are planted on me "drop" through me to the
  // book.
  if([aGlyph isKindOf: [self class]] &&
    ancestor == [ancestor isBook])
  { // translate Page's origin to ancestor's coords
    NXPoint aPnt = aGlyph->frame.origin ;
    [self convertToAncestor: &aPnt] ;
    aGlyph->frame.origin = aPnt ;
    // plant it in ancestor
    return [ancestor plant: aGlyph] ;
  }
  else
    return [super plant: aGlyph] ;
}

@end

