
#import <appkit/appkit.h>
#import <foundation/foundation.h>

#import "graphicCellProtocol.h"		// Stolen from the appkit source

#import "ImageCell.h"

@implementation ImageCell

- (id)initIconCell:(const char *)iconName;
{
	self = [super initIconCell:iconName];
	_path = nil;
	_image = nil;
	return self;
}

- free;
{
	[_image free];
	[_path release];
	return [super free];
}

- (NXImage *)image;
{
	return _image;
}

- (void)setImage:(NXImage *)anImage;
{
	[_image free];
	_image = anImage;
}

- (NSString *)imagePath;
{
	return _path;
}

- (void)setImagePath:(NSString *)aPathName;
{
	[_path release];
	_path = [aPathName copy];
	
	[self setImage:[[NXImage alloc] initFromFile:[_path cString]]];
}

//-----------------------------------------------------------------------------

- drawSelf:(const NXRect *) cellFrame inView:controlView
{
    NXPoint point = cellFrame->origin;
    point.y += cellFrame->size.height;
    [_image composite:NX_SOVER toPoint:&point];

    return self;
}

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

- calcCellSize:(NXSize *) theSize
{
    [_image getSize:theSize];
    return self;
}

//-----------------------------------------------------------------------------

- (BOOL)trackMouse:(NXEvent *)theEvent 
	inRect:(const NXRect *)cellFrame 
	ofView:controlView;
{
	if (theEvent->data.mouse.click == 1)
		{
			NXEvent	buf, *ev;
			
			ev = [NXApp peekNextEvent:NX_MOUSEDOWNMASK|NX_MOUSEUPMASK
	    		into:&buf waitFor:0.3 threshold:NX_MODALRESPTHRESHOLD];

			if (!ev || ev->type == NX_MOUSEDRAGGED)
				{
				Pasteboard	*pboard = [Pasteboard newName:NXDragPboard];
				const char	*pboard_types[] = 
				{
					NXTIFFPboardType, NXFilenamePboardType
				};
				NXPoint		point = theEvent->location;
				NXPoint		nullPoint = { 0.0, 0.0 };
				NXSize		size;
						
				[pboard declareTypes:&NXFilenamePboardType num:1 owner:nil];
				
				[pboard writeType:NXFilenamePboardType data:[_path cString] 
						length:[_path cStringLength]];
//				[pboard writeType:NXTIFFPboardType data:...
								
				[_image getSize:&size];
				point.x -= size.width / 2;
				point.y -= size.height / 2;
				[controlView convertPoint:&point fromView:nil];
				[controlView dragImage:_image at:&point offset:&nullPoint
					event:theEvent pasteboard:pboard source:controlView 
					slideBack:YES];
		
				return YES;
				}
		}
	else if (theEvent->data.mouse.click > 1)
		{
		[[[NXApp class] workspace] openFile:[_path cString] fromImage:_image
		 at:&theEvent->location inView:controlView];

		return YES;		
		}
	
	return NO;
}

//-----------------------------------------------------------------------------

/* Read Image (name and) data from the RTF-Stream generated by the ArticleViewControl */
- readRichText:(NXStream *) stream forView:(id <NXGraphicCellProtocol>)view
{
	char			name[MAXPATHLEN + 1];
	
	NXScanf(stream, "%[^}]", name);
	[self setImagePath:[NSString stringWithCString:name]];
	
#ifdef DEBUG
	{
	NXSize			size;

	[_image getSize:&size];
	fprintf(stderr, "image '%s' (%f, %f)\n", name, size.width, size.height);
	}
#endif
	
    return self;
}


@end
