// A simple view class that responds to mouseDown event by 
// opening a stream and sending a message to a 'streamProvider'
// to draw postscript code into the stream.  It then writes the
// stream back to the file (DRAGGEDFILENAME) and invokes the view
// method dragFile:fromRect:slideBack:event:
// You must connect the streamProvider outlet to an object that 
// responds to writeToStream: by writing the contents of the file
// represented by the icon to the given stream.
// DragPSView.h provides an interface to a 'streamProvider'
// category (of the Object class) with this method.
// Note: This class does not remove DRAGGEDFILENAME.
// DragPSView composites an image (IMAGENAME) to draw itself.

#import "DragPSView.h"
#import <appkit/NXImage.h>
#import <sys/file.h>
#import <appkit/nextstd.h>	    // imports math.h,stdio.h,libc.h

@implementation DragPSView

- drawSelf:(NXRect *)rects :(int)rectCount
{
	NXPoint	point;
	NXImage *theImage;
	
	point.x = 0.0;
	point.y = 0.0;
	theImage = [NXImage findImageNamed:IMAGENAME];
	[theImage composite:NX_SOVER toPoint:&point];
	
	return self;
}

- mouseDown:(NXEvent *)theEvent
{
	int	fd;
	NXStream *stream;
	NXRect iconRect;
	
	fd = open(DRAGGEDFILENAME, O_CREAT | O_WRONLY | O_TRUNC, 0666);
	stream = NXOpenFile(fd, NX_WRITEONLY);

	[streamProvider writeToStream:(NXStream *)stream];
	// printViewToStream: is a method that writes postscript
	// code to the given stream.

	NXFlush(stream);
	NXClose(stream);
	close(fd);

	NXSetRect(&iconRect, (NXCoord) 0, (NXCoord) 0, (NXCoord) 48, (NXCoord) 48);

	[self dragFile:DRAGGEDFILENAME
		fromRect:(NXRect *)&iconRect
		slideBack:(BOOL) YES
		event:(NXEvent *)theEvent];

	return self;
}

@end
