


#import <libc.h>
#import	<stdio.h>
#import <strings.h>

#import <appkit/Application.h>
#import <appkit/Listener.h>
#import <appkit/Speaker.h>
#import <appkit/View.h>

#import	<appkit/NXImage.h>
#import <appkit/NXBitmapImageRep.h>
#import <appkit/NXEPSImageRep.h>
#import <appkit/tiff.h>	

#import	<dpsclient/dpsNeXT.h>
#import <streams/streams.h>
#import <sys/types.h>


#import "GCell.h"

@implementation GCell

- initFromFile:(char *)file 
{
	char *tiff = NULL;
	int length;
	int	okFlag;
	NXStream *stream;
	id speaker;
	
   	self = [super init];
	speaker = [NXApp appSpeaker];
	[speaker setSendPort:NXPortFromName(NX_WORKSPACEREQUEST, NULL)];
	[speaker 	getFileIconFor: file
				TIFF: &tiff 
				TIFFLength: &length 
				ok: &okFlag];
	if(okFlag || tiff)
	{
		stream = NXOpenMemory(tiff, length, NX_READONLY);
		theImage = [[NXImage alloc] initFromStream: stream ];
		NXCloseMemory(stream, NX_FREEBUFFER);
	}
	else
	{
		theImage = [[NXImage alloc] 
			initFromFile: "/usr/lib/nib/default_app_icon.tiff" ];
	}
	
	if(file != NULL)
		strcpy(name, file);
	else
	{
		strcpy(name, "FooFile");
	}
    return self;
}
	
- calcCellSize:(NXSize *)theSize
{
    NXSize s;
    [theImage getSize: &s];
    s.height += 4.0;
    s.width += 4.0;
    *theSize = s;
    return self;
}

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

- (BOOL)trackMouse:(NXEvent *)theEvent 
	inRect:(const NXRect *)cellFrame 
	ofView:controlView;
{	
	int	aFlag;
	
	if(theEvent->data.mouse.click > 1)
	{
		if(lastmouse)
		{
			id	speaker = [NXApp appSpeaker];
			[speaker setSendPort: 
				NXPortFromName(NX_WORKSPACEREQUEST, NULL)];
			[speaker openFile: name ok:&aFlag];
			lastmouse = NO;
		}
		else
		{
			lastmouse = YES;
		}
		
	}
	else
	{
		NXEvent *e;
		NXRect dragRect;
		e = [NXApp 	getNextEvent: NX_MOUSEUPMASK
					waitFor: .25
					threshold: NX_MODALRESPTHRESHOLD];
		if(e != NULL)
			return YES;
			
		dragRect.origin = cellFrame->origin;
		dragRect.size.height = 48.0;
		dragRect.size.width = 48.0;
		[controlView dragFile: name
			fromRect: &dragRect
			slideBack: YES
			event: theEvent];
	}
	return YES;
}
  
- drawInside: (const NXRect *)cellFrame inView:controlView
{
	NXPoint	compositePlace;
	compositePlace.x = cellFrame->origin.x+2.0;
	compositePlace.y = NX_MAXY(cellFrame)+ 2.0;
	[theImage composite:NX_COPY toPoint:&compositePlace];
	return self;
}

- writeRichText:(NXStream *)stream forView:view
{    
    return self;
}


- readRichText:(NXStream *)stream forView:view
{	
    return self;
}



@end
