// MOImageDocController.m
//
// by Mike Ferris
// Part of MOKit
// Copyright 1993, all rights reserved.

// ABOUT MOKit
// 
// MOKit is a collection of useful and general objects.  Permission is 
// granted by the author to use MOKit in your own programs in any way 
// you see fit.  All other rights to the kit are reserved by the author 
// including the right to sell these objects as part of a LIBRARY or as 
// SOURCE CODE.  In plain English, I wish to retain rights to these 
// objects as objects, but allow the use of the objects as pieces in a 
// fully functional program.  NO WARRANTY is expressed or implied.  The author 
// will under no circumstances be held responsible for ANY consequences to 
// you from the use of these objects.  Since you don't have to pay for 
// them, and full source is provided, I think this is perfectly fair.

#import "MOImageDocController.h"
#import "MOKit/MOPathString.h"

#define CLASS_VERSION			0

#define CLASS_NIB				"MOImageDocController.nib"

@implementation MOImageDocController

+ initialize
// Set the version.  Configure the class.
{
	if (self == [MOImageDocController class])  {
		[self setVersion:CLASS_VERSION];
		
		[self setClassNib:CLASS_NIB];
		[self setControllerName:"Image"];
		[self addDocType:1 name:"TIFF Image" extension:"tiff" 
			openSelector:@selector(doRevertImage:) 
			saveSelector:@selector(doSaveTiff:)];
		[self addDocType:2 name:"EPS Image" extension:"eps" 
			openSelector:@selector(doRevertImage:) 
			saveSelector:NULL];
	}
	return self;
}

- nibDidLoad
// We don't really need this.
{
	[super nibDidLoad];
	
	return self;
}

- doSaveTiff:(MODocType *)type
// The saveSelector for tiff.
{
	NXStream *strm = NULL;
	id ret = self;

	if (![self image])  {
		return nil;
	}
	
	strm = NXOpenMemory(NULL, 0, NX_WRITEONLY);
	if (strm)  {
		[[self image] writeTIFF:strm allRepresentations:YES 
					usingCompression:NX_TIFF_COMPRESSION_LZW 
					andFactor:0.0];
		if (NXSaveToFile(strm, [self file]) == -1)  {
			ret = nil;
		}
		
		NXCloseMemory(strm, NX_FREEBUFFER);
	}  else  {
		ret = nil;
	}
	
	return ret;
}

- doRevertImage:(MODocType *)type
// The openSelector for both types.
{
	id ret = self;
	
	id newImage = [[NXImage allocFromZone:[self zone]] 
				initFromFile:[self file]];
	if (newImage)  {
		[self setImage:newImage];
	}  else  {
		ret = nil;
	}
	
	return ret;
}

- (BOOL)doClear
// Clear the image view.
{
	[self setImage:nil];
	
	return YES;
}

- (BOOL)doPrint
// print the image view.
{
	[imageView printPSCode:self];
	return YES;
}

- imageView
// Return the image view.
{
	return imageView;
}

- setImage:(NXImage *)image
// Set the image view to display an NXImage.
{
	id oldImage = [self image];
	[imageView setImage:image];
	if (oldImage)  [oldImage free];
	return self;
}

- image
// Return the image view's NXImage.
{
	return [imageView image];
}

@end

