
/* written by Joe Freeman of NeXT Computer Inc.
 *	Version 2
 *	As with all my code, use at your own risk
 *
 *
 * MMGraphicCell.[hm]
 *	Cell used by the multi media text object to implement multimedia text
 *
 * Notes:
 *	1)	This object generates embeded RTF similar to Edit.  This
 *		format is different from the rtf format used by mail.
 *		Mail does not put the height and width operators in the stream
 *
 * Text objects expect the embeded graphic objects to implement
 *	highlight:inView:lit:		- not implemented 
 *	drawSelf:inView:
 *	trackMouse:inRect:ofView:	- not implemented
 *	calcCellSize:
 *	readRichText:forView:
 *	writeRichText:forView:
 *
 */

/* Generated by Interface Builder */

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

#import <libc.h>			/* MAXPATHLEN */
#import	<stdio.h>
#import <strings.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 <objc/List.h>			/* list object */
#import <streams/streams.h>

@implementation MMGraphicCell:MMCell


static	int imageCount = 0 ;

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

- initFromFile: (const char *)filename copy:(BOOL)flag
{
	char *extent;
	
	self = [super initFromFile: filename copy:flag];

	theImage = [NXImage findImageNamed: filename];
	if (!theImage){
		theImage = [[NXImage alloc] initFromFile: filename];
		[theImage setName:filename];
	}
	if (		filename &&
			(strlen(filename)>5) && 
			(extent = rindex(filename,'.'))){
		if (!strcmp(extent,".eps")){
			sprintf(saveName,"EPS%d.eps", imageCount++);
		} else if (!strcmp(extent,".ps")){
			sprintf(saveName,"PS%d.ps", imageCount++);
		} else if (!strcmp(extent,".tiff") || !strcmp(extent,".tif")){
			sprintf(saveName,"TIFF%d.tiff", imageCount++);
		} else {
			sprintf(saveName,"FREEMAN%d.tmp",imageCount++);
		}
	}
	
	return self;
}

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

- initFromStream:(NXStream *)aStream  copy:(BOOL)flag
{
    self = [super init];
    copyFlag = flag;
    
    /* there is a note in the NXImage man page that hints that stream reads
     * may be handled differently, using the default setting of dataRetained no
     * this is a problem because the image will be converted to cached
     */
    theImage = [[NXImage alloc] init];
    [theImage setDataRetained:YES];
    [theImage loadFromStream: aStream];
    if ([[theImage lastRepresentation] class] == [NXBitmapImageRep class]){
	sprintf(saveName,"TIFF%d.tiff", imageCount++);
    } else 
    	if ([[theImage lastRepresentation] class] == [NXEPSImageRep class]){
	sprintf(saveName,"EPS%d.eps", imageCount++);
    }
    [theImage setName:saveName];	/* kludge, we don't have a file name */
    return self;
}

/* figure out the best representation to save 
 * could look at the file name and figure out but we will be stupid 
 * scan list of representations looking for NXEPSImageRep and NXBitmapImageRep
 * use EPS image rep if we find it, then bitmap if we cannot find it
 * 	write nothing if neither (should be cached)
 *
 */
- writeImageToStream: (NXStream *)stream
{
    int 	i;
    id  	epsRep=NULL;
    id  	tiffRep = NULL;
    char	*epsString;
    int 	epsLength;
    List	*repList = [theImage representationList];
    
    if (repList && [repList count]){
    	for ( i = 0 ; i < [repList count] ; i++){
		if ([[repList objectAt:i] class] == [NXBitmapImageRep class])
			tiffRep = [repList objectAt:i];
		else if ([[repList objectAt:i] class] == [NXEPSImageRep class])
			epsRep = [repList objectAt:i];
	}
    } 
    if (epsRep){
    	[epsRep getEPS:&epsString length:&epsLength];
	NXWrite(stream, epsString, epsLength);
    } else if (tiffRep){
    	[tiffRep writeTIFF:stream usingCompression:NX_TIFF_COMPRESSION_NONE];
    }
    return self;
}

/* when copying, just write out a pointer to the graphic that this came from
 * write out the necessary coding to embed graphic in rtf file 
 * when saving have eps/tiffimage written out to file in the rtfd directory 
 */
- writeRichText:(NXStream *)stream forView:view
{
    NXSize imageSize;
    NXStream *aStream;			/* to write image out to a file */
    char fileName[MAXPATHLEN+1];	/* file name to save to */
    
    [theImage getSize:&imageSize];
    
    if (!copyFlag){
	NXPrintf(stream, "%s \\width%d \\height%d\n", 
    		[theImage name],(int)imageSize.width,(int)imageSize.height);
	
    } else {
    
	NXPrintf(stream, "%s \\width%d \\height%d", 
    		saveName,(int)imageSize.width,(int)imageSize.height);
	sprintf(fileName,"%s/%s",[view filename], saveName);
	
	aStream = NXOpenMemory(NULL,0, NX_WRITEONLY);
	[self writeImageToStream:aStream];	
	NXSaveToFile(aStream, fileName);
	NXCloseMemory(aStream, NX_FREEBUFFER);
	/* BUG 
	 * this updates the name for any images that have save name == name
	 * this would normally happen if we did a paste from the pasteboard
	 * thus, for the duration of this session, all rtf pastes retain their
	 * original names.
	 */
	if (!strcmp([theImage name], saveName))
		[theImage setName:fileName];
    }
    
    return self;
}

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


@end
