/* written by Joe Freeman of NeXT Computer Inc.
 *	Version 2
 *	As with all my code, use at your own risk
 *
 *
 * MMCell.[hm]
 *	Cell used by the multi media text object to implement file attachments
 *
 * Notes:
 *	1)	This object generates attachments similar to mail.
 *		The format is identical ... see note in MMText for
 *		incompatible explanations
 *
 * Text objects expect the embeded graphic objects to implement
 *	highlight:inView:lit:
 *	drawSelf:inView:
 *	trackMouse:inRect:ofView:
 *	calcCellSize:
 *	readRichText:forView:
 *	writeRichText:forView:
 *
 */


#import "MMCell.h"
#import "MMText.h"				  /* for the filename method */

#import <libc.h>					  /* MAXPATHLEN */
#import <stdio.h>
#import <strings.h>

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

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

#import <dpsclient/dpsNeXT.h>	  /* NX_COPY */
#import <streams/streams.h>


@implementation MMCell:Cell


static char rcsstring[] = 
    "@(#)Object:MMCell.m Developer:Joe Freeman Version:2 July, 1991";


/* assume that we just want the image of the file and we save the pointer */
- initFromFile: (const char *)filename copy:(BOOL)flag
{
	self = [super init];
	copyFlag = flag;
	return self;
}

- initFromFile: (const char *)filename requestedByView: aView
{
	[self initFromFile:filename copy:NO];
	return self;
}

- free
{
	/* probably don't want to free cause could be used by other filecell */
	//[theImage free];
	[super free];
	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;
{
#ifdef DEBUG
    fprintf(stderr,"MMCell.m  highlight inView lit\n");
#endif DEBUG
    return self;
}

/* this is really ugly and should sit on top of the built in cell tracking
 * but that doesn't seem to be working 
 */
- (BOOL)trackMouse:(NXEvent *)theEvent 
	inRect:(const NXRect *)cellFrame 
	ofView:controlView;
{
	int	aFlag;
	static	long	lastDown;	  /* should initialize to 0 */
	id	speaker = [NXApp appSpeaker];

	/* if the user clicks down in less than .33 secs (20 intervals)
	 *	then lets treat it as a double click
	 */
	if (theEvent->time-lastDown < 20){
		[speaker setSendPort: 
				NXPortFromName(NX_WORKSPACEREQUEST, NULL)];
		[speaker openFile: [theImage name] ok:&aFlag];
	}
	lastDown = theEvent->time;
	/* should allow drag out here */
	return YES;
}


/*==================================================
 *
 *
 *==================================================*/
 
- write:(NXTypedStream *)stream
{
	[super write:stream];
	NXWriteObject(stream , theImage);
	NXWriteTypes(stream,"*i",&saveName,&copyFlag);

	return self;
}

- read:(NXTypedStream *)stream
{
	[super read:stream];
	theImage = NXReadObject(stream);
	NXReadTypes(stream,"*i",&saveName,&copyFlag);
	return self;
}


/* 
 * just write out the name of the attached file
 * good for links
 */
- writeRichText:(NXStream *)stream forView:view
{
	char fileName[MAXPATHLEN+1];	/* file name to save to */
	
	if (copyFlag) {
    	NXPrintf(stream,"%s\n",saveName);
		sprintf(fileName,"cp %s %s/%s",
				  [theImage name], [view filename], saveName);
		system(fileName);
	} else {
		NXPrintf(stream, "%s\n",[theImage name]);
	}
	return self;
}

- readPrivate:(NXStream *)stream name:(char *)name
{
    NXScanf(stream, "%s\n", name); 
    return self;
}

/* read the attached file name out of the stream
 * then go get the icon for the file
 * GraphicCells do something different
 */
- readRichText:(NXStream *)stream forView:view
{
    char name[MAXPATHLEN+1];
    char fileName[MAXPATHLEN+1];

    [self readPrivate:stream name:name];
    if (!index(name,'/'))
    	sprintf(fileName,"%s/%s",[view filename],name);
    else
    	strcpy(fileName,name);
    [self initFromFile: fileName requestedByView: view ];
    return self;
}


/*==================================================
 *
 *
 *==================================================*/
 
- drawInside: (const NXRect *)cellFrame inView:controlView
{
	NXPoint	compositePlace;
#ifdef DEBUG
	fprintf(stderr,"MMCell drawInside:inView:\n");
#endif DEBUG
	compositePlace.x = cellFrame->origin.x+2.0;
	compositePlace.y = NX_MAXY(cellFrame)+ 2.0;
	[theImage composite:NX_COPY toPoint:&compositePlace];
	return self;
}



@end
