#import <sys/param.h>
#import <strings.h>
#import <appkit/View.h>
#import <appkit/Button.h>
#import <appkit/OpenPanel.h>
#import <appkit/NXImage.h>
#import <appkit/NXBitmapImageRep.h>
#import "TIFFView.h"

@implementation TIFFView

- drawSelf:(NXRect *)rects :(int)count
{
     static NXPoint point = {0.0,0.0};

     if(isCached)
	  [image composite:NX_COPY toPoint:&point];
     else
	  [image draw];

     return self;
}

- openTIFF:sender
{
     char               file[MAXPATHLEN];
     const char         *const *files;
     static const char  *const imageType[4] = {"tiff"};
     id                 openpanel = [[OpenPanel new] allowMultipleFiles:NO];


     if([openpanel runModalForTypes:imageType])
	  if((files = [openpanel filenames]) && *files){
               (void)strcat(strcat(strcpy(file,[openpanel directory]),"/"),*files);
	       if(image)
		    [image free];
	       if(bitmap = image = [[NXBitmapImageRep alloc] initFromFile:file]){
		    [self sizeTo:[image pixelsWide]:[image pixelsHigh]];
		    if(isCached)
			 [[(image = [[NXImage alloc] init]) setDataRetained:YES] useRepresentation:bitmap];
		    [[[self display] window] makeKeyAndOrderFront:self];
	       }
	       else
		    NXRunAlertPanel("Open Image","Error loading image!","Cancel",NULL,NULL);
          }
     
     return self;
}

- toggleCachingMode:sender
{
     if(isCached = !isCached)
	  [[(image = [[NXImage alloc] init]) setDataRetained:YES] useRepresentation:bitmap];
     else
	  image = bitmap;

     return self;
}

@end
