//  EmbeddedGraphic.m
//
//  a simple cell subclass that can be placed into a text object
//
//  You may freely copy, distribute, and reuse the code in this example.
//  NeXT disclaims any warranty of any kind, expressed or  implied, as to its
//  fitness for any particular use.

#import "EmbeddedGraphic.h"

#define THEDIRECTIVE "StevesEGraphic"

@implementation EmbeddedGraphic
//sent when the class receives its first message
+ initialize
{
	[Text registerDirective:THEDIRECTIVE forClass:[EmbeddedGraphic class]];
	return self;
}
- init
{
	self = [super init];
	image = [NXImage findImageNamed:"NXdefaulticon"];
	fileName = NULL;
	
	return self;
}

- free
{
	id tmpImage= [NXImage findImageNamed:"NXdefaulticon"];
	
	if((image != nil) && (image != tmpImage)) [image free];
	if(fileName != NULL) NX_FREE(fileName);
	return [super free];
}

- setFile:(const char *)theFile
{
	image = nil;
	
	if(theFile != NULL){
		fileName = NXCopyStringBuffer(theFile);
		image = [[NXImage alloc] initFromFile:fileName];
		if(image == nil) image = [NXImage findImageNamed:"NXdefaulticon"];
	}
	
	if(image == nil) image = [NXImage findImageNamed:"NXdefaulticon"];
	return self;
}

- calcCellSize:(NXSize *)theSize
{
	if(image != nil) [image getSize:theSize];
	return self;
}

- drawSelf:(const NXRect *)rect inView:view
{
	NXPoint tmpPoint = {0.0,0.0};
	
	[view getLocation:&tmpPoint ofCell:self];//view is the Text object
	
	tmpPoint.y += (rect->size).height;//the coordinates are flipped
	
	[image composite:NX_SOVER toPoint:&tmpPoint];
	return self;
}

- highlight:(const NXRect *)rect inView:view lit:(BOOL)flag
{
	return self;
}

- readRichText:(NXStream *)stream forView:view 
{
	char buffer[FILENAME_MAX+1];	
	NXScanf(stream,"%s\n",buffer); //reads in the path
	
	[self setFile:buffer];
	
	return self;
}

- writeRichText:(NXStream *)stream forView:view
{
	char buffer[FILENAME_MAX];
	
	strcpy(buffer,fileName);
	
	NXPrintf(stream,"%s\n",buffer);//writes out the path
	return self;
}

- (BOOL)trackMouse:(NXEvent *)theEvent inRect:(const NXRect *)rect ofView:view
{
	return NO;
}

@end
