/**************************************************************************
 * CLASS:		MiscImageDragView
 *
 *	See the header file for more information on this class.
 **************************************************************************/

#import <appkit/Pasteboard.h> 
#import <misckit/MiscImageDragView.h>


@implementation MiscImageDragView

// Register to accept dragging, and setup some defaults.

- initFrame: (NXRect *)frameRect
{	
  const char *const types[] = { NXPostScriptPboardType, NXTIFFPboardType,
  								NXFilenamePboardType };

	[super initFrame: frameRect];
	[self registerForDraggedTypes: (const char *const *)&types count: 3];
	[self setDragIconRepresentation: YES];

	return self;
}



- awake
{
  const char *const types[] = { NXPostScriptPboardType, NXTIFFPboardType, 	
  								NXFilenamePboardType };

	[super awake];
	[self registerForDraggedTypes: (const char *const *)&types count: 3];
	
	return self;
}



//  Sets whether the TIFF itself or its TIFF icon representation is dragged.
 
- setDragIconRepresentation: (BOOL)aBool
{
	dragIconRepresentation = aBool;
	return self;
}



- (BOOL)dragIconRepresentation
{
	return dragIconRepresentation;
}



// Put the image data on the NXTIFFPboardType. Also set the image that is
// going to be used for the drag, depending upon whether 
// dragIconRepresentation is true.

- (BOOL)setupForSourceDrag
{
  id  dragPB = [Pasteboard newName: NXDragPboard];
  NXStream  *imageStream = NXOpenMemory (NULL, 0, NX_WRITEONLY);
  const char *const types[] = { NXTIFFPboardType };
	
	// write the NXImage in TIFF form (it's original size)
	
	[dragPB declareTypes: types num: 1 owner: self];
	
	// put the image back to regular size and write it
	
	[ [self image] setSize: &imageSize];
	[ [self image] writeTIFF: imageStream];
	[dragPB writeType: NXTIFFPboardType fromStream: imageStream];

	NXCloseMemory (imageStream, NX_FREEBUFFER);		

	// if use icon rep. grab the image from somewhere that everyone has
	
	if ([self dragIconRepresentation])
		dragImage = [ [NXImage alloc] initFromFile: 	
				"/NextApps/Preview.app/tiff.tiff"];	
	else
		dragImage = [self image];	

	return YES;
}



// Adjust the dragPoint so that you grab the image in the center.

- calculateDragPoint: (NXPoint *)dragPoint andOffset: (NXPoint *)offset
{
	if ([self dragIconRepresentation])
	{
		dragPoint->x -= 24.0;
		dragPoint->y -= 24.0;
	 }
	else
	{
	  NXSize  imSize;
	  
	  	[ [self image] getSize: &imSize];
		dragPoint->x -= imSize.width / 2.0;
		dragPoint->y -= imSize.height / 2.0;
	 }
	 
	return self;
}



// This sure is a complicated method. Actually NXImage takes care
// of everything. It checks for filenames, correct extensions, data
// on the correct pasteboards, and even accepts non native images if 
// you have the filters.
		
- (BOOL)prepareForDragOperation: sender
{
	if ([NXImage canInitFromPasteboard: [sender draggingPasteboard] ])
	    return YES;

	return NO;
}



// Take the pasteboard data and create the image.

- (BOOL)performDragOperation: sender
{
	[self setImage: [ [NXImage alloc] initFromPasteboard: 
			[sender draggingPasteboard] ] ];

	return YES;
}



// Archiving methods

- read: (NXTypedStream *)stream
{
	[super read: stream];
//	NXReadTypes ("c", &dragIconRepresentation);

	return self;
}



- write: (NXTypedStream *)stream
{
	[super write: stream];
//	NXWriteTypes ("c", &dragIconRepresentation);

	return self;
}

@end
	