#include <sys/types.h>
#include <sys/time.h>
#import <appkit/appkit.h>

#import "Square.h"

@implementation Square : Cell
/*
  This class represents one square on a chess board.  It has a color
  and may contain a piece.
*/

- free
{
  if( icon )
    free( icon );
  [super free];
}

- (int)type { return type; }
- setIcon: (char const *)i
{
  if( icon )
    free( icon );
  if( !i ){
    icon = 0;
    return self;
  }
  icon = (char *)malloc( strlen(i) + 1 );
  strcpy( icon, i );
}

- (char const *)icon { return icon; }

- setBackground: (float) b { background = b; }

- drawSelf:(const NXRect *)f inView: v
{
  PSsetgray( background );
  PSrectfill( f->origin.x, f->origin.y, f->size.width, f->size.height );
  [self drawInside: f inView: v];
}
- drawBackground:(const NXRect *)f inView: v
{
  PSsetgray( background );
  PSrectfill( f->origin.x, f->origin.y, f->size.width, f->size.height );
}

- highlight: (NXRect *)f inView: v
{
  struct timeval timeout;

  NXInsetRect( f, 1.0, 1.0 );
  PSgsave();
  PSsetlinewidth( 2.0 );
  PSsetgray( NX_WHITE );
  PSrectstroke ( f->origin.x, f->origin.y, f->size.width, f->size.height );
  /***
  PSmoveto( f->origin.x, f->origin.y );
  PSrlineto( f->size.width, 0.0 );
  PSrlineto( 0.0, f->size.height );
  PSrlineto( -1.0 * f->size.width, 0.0 );
  PSclosepath();
  PSstroke();
  ***/
  // PSflushgraphics();
  NXPing();
  
  timeout.tv_sec = 0;
  timeout.tv_usec = 150000;
  select( 0, 0, 0, 0, &timeout );

  PSsetgray( NX_BLACK );
  PSrectstroke ( f->origin.x, f->origin.y, f->size.width, f->size.height );
  /***
  PSmoveto( f->origin.x, f->origin.y );
  PSrlineto( f->size.width, 0.0 );
  PSrlineto( 0.0, f->size.height );
  PSrlineto( -1.0 * f->size.width, 0.0 );
  PSclosepath();
  PSstroke();
  ***/
  // PSflushgraphics();
  NXPing();
  
  timeout.tv_sec = 0;
  timeout.tv_usec = 150000;
  select( 0, 0, 0, 0, &timeout );

  PSsetgray( NX_WHITE );
  PSrectstroke ( f->origin.x, f->origin.y, f->size.width, f->size.height );
  /***
  PSmoveto( f->origin.x, f->origin.y );
  PSrlineto( f->size.width, 0.0 );
  PSrlineto( 0.0, f->size.height );
  PSrlineto( -1.0 * f->size.width, 0.0 );
  PSclosepath();
  PSstroke();
  ***/
  // PSflushgraphics();
  NXPing();
  
  timeout.tv_sec = 0;
  timeout.tv_usec = 150000;
  select( 0, 0, 0, 0, &timeout );

  PSsetgray( background );
  PSrectstroke ( f->origin.x, f->origin.y, f->size.width, f->size.height );
  /***
  PSmoveto( f->origin.x, f->origin.y );
  PSrlineto( f->size.width, 0.0 );
  PSrlineto( 0.0, f->size.height );
  PSrlineto( -1.0 * f->size.width, 0.0 );
  PSclosepath();
  PSstroke();
  ***/
  // PSflushgraphics();
  NXPing();
  
  PSgrestore();
}
  

  
- drawInside:(const NXRect *)r inView:v
/*
  Draw the chess piece.
*/
{
  NXPoint p;
  NXSize s;
  id bitmap;
  
  if( !icon )
    return;
    
  /* Composite the piece icon in the center of the rect. */
  bitmap = [Bitmap findBitmapFor: icon];
  [bitmap getSize: &s];
  p.x = floor(((r->size.width - s.width) / 2.0) + r->origin.x);
  p.y = floor(((r->size.height - s.height) / 2.0) + r->origin.y);
  [bitmap composite: NX_SOVER toPoint: &p];
  return self;
}


@end
