#import "GraphicsView.h"
#import <appkit/NXImage.h>
#import <appkit/Application.h>
#import <defaults/defaults.h>
#import <dpsclient/psops.h>
#import <sys/param.h>


@implementation GraphicsView

- (BOOL) acceptsFirstResponder ;
{ return YES  ;
}

- clear ;
{ [image lockFocus] ;
  NXEraseRect(&bounds) ;
  [image unlockFocus] ;
  [self display] ;
  return self ;
}

- drawSelf: (NXRect *) aRect :(int) count ;
{ // draw my cached image to the screen
  [image composite:  NX_SOVER 
          fromRect:  aRect  
          toPoint:  &aRect->origin ] ;
   return self ;
}

- image ;
{ return image ;
}

- setUp ;
{ // set up...allocating the initial NXImage object is done as a side effect
  // of calling superviewSizeChanged
  NXSize aSize ;
  const char *defaultSize ;
  defaultSize = NXGetDefaultValue([NXApp appName],"GraphicsViewSize") ;
  sscanf(defaultSize,"%f %f", &aSize.width, &aSize.height) ;
  [self sizeTo: aSize.width :aSize.height] ; // make myself same size as default image
  [self superviewSizeChanged: NULL] ; // go see if I need to grow 
  [self clear] ;
  return self ;
}


- superviewSizeChanged: (const NXSize *)oldSize
{ NXRect superBounds, currentBounds ;
  [self getBounds: &currentBounds] ;
  [superview getBounds: &superBounds] ;
  if(!image || (superBounds.size.width > currentBounds.size.width) ||
     (superBounds.size.height > currentBounds.size.height))
  { NXRect newBounds = {{0.0,0.0},{0.0,0.0}} ;
    id newImage ;
    newBounds.size.width = MAX(superBounds.size.width,currentBounds.size.width) ;
    newBounds.size.height = MAX(superBounds.size.height,currentBounds.size.height) ;
    [self sizeTo: newBounds.size.width :newBounds.size.height] ;
    newImage =  [[NXImage  alloc] initSize: &newBounds.size] ;
    if(image)
    { [newImage lockFocus] ;
      [image composite: NX_COPY toPoint: &newBounds.origin] ;
      [newImage unlockFocus] ;
      [image free] ;
    }
    image = newImage ;
    [self display] ;
  }
  return self ;
}

@end
