
/* Generated by Interface Builder */

#import <libc.h>
#import <appkit/PrintInfo.h>
#import <appkit/NXBitmapImageRep.h>
#import <dpsclient/wraps.h>
#import "GraphicApp.h"
#import "gw.h"
#import "ImageView.h"

@implementation ImageView

- initFrame: (const NXRect *)frameRect withImage: (id)image
{
	NXRect 			cacheRect;

	[self initFrame: frameRect];
	bitmap = image;

#ifdef DEBUG
	fprintf(stderr, "Imaging bitmap to cache...\n");
#endif
	cacheRect.origin.x = cacheRect.origin.y = 0.0;
	cacheRect.size.width = [bitmap pixelsWide];
	cacheRect.size.height = [bitmap pixelsHigh];
	cache = [Window 	newContent: 	&cacheRect
					style: 		NX_PLAINSTYLE
					backing: 		NX_RETAINED
					buttonMask: 	0
					defer: 		NO];
	[[cache contentView] lockFocus];
	[bitmap draw];
	[[cache contentView] unlockFocus];
#ifdef DEBUG
	fprintf(stderr, "Bitmap was imaged into cache\n");
#endif
	
	return self;
}

- free
{
	[cache free];
	[bitmap free];
	return [super free];
}

- replaceImageWith: (id)newImage
{
	[bitmap free];
	bitmap = newImage;
	[[cache contentView] lockFocus];
	[bitmap draw];
	[[cache contentView] unlockFocus];
	[self update];
#ifdef DEBUG
	fprintf(stderr, "Image replace and recached.\n");
#endif
	
	return self;
}

- placePrintRect:(const NXRect *)aRect offset:(const NXPoint *)location
{
	((NXPoint *)location)->x = 0.0;
	((NXPoint *)location)->y = 0.0;

	return self;
}

- drawSelf: (const NXRect *)rects : (int)rectCount
{
	const NXRect		*rp;
	int				j, gstate;

	if (rects == NULL) return self;

	if (NXDrawingStatus == NX_DRAWING) {
		gstate = [cache gState];
		for (j = 0; j < rectCount; j++) {
			rp = &(rects[j]);
			PScomposite(NX_X(rp), NX_Y(rp), NX_WIDTH(rp), 
				NX_HEIGHT(rp), gstate, NX_X(rp), NX_Y(rp), NX_COPY);
		}
	} else if (NXDrawingStatus == NX_PRINTING) {
		float			scale;
		NXRect		printFrame;
		const NXRect	*paperRect = [[NXApp printInfo] paperRect];
		float			pWidth = paperRect->size.width;
		float			pHeight = paperRect->size.height;
		float			iWidth = pWidth - (72.0 * 2);
		float			iHeight = pHeight - (72.0 * 2);
		
		fprintf(stderr, "Printing\n");
		scale = (float)[bitmap pixelsWide] / (float)[bitmap pixelsHigh];
		if (abs(iWidth - [bitmap pixelsWide]) > abs(iHeight - [bitmap pixelsHigh])) {
			scale = iHeight / (float)[bitmap pixelsHigh];
			printFrame.size.height = iHeight;
			printFrame.size.width = (float)[bitmap pixelsWide] * scale;
			printFrame.origin.x = (pWidth / 2.0) - (printFrame.size.width / 2.0);
			printFrame.origin.y = 72.0;
		} else {
			scale = iWidth / (float)[bitmap pixelsWide];
			printFrame.size.height = (float)[bitmap pixelsHigh] * scale;
			printFrame.size.width = iWidth;
			printFrame.origin.x = 72.0;
			printFrame.origin.y = (pHeight / 2.0) - (printFrame.size.height / 2.0);
		}
		fprintf(stderr, "Frame = %f %f %f %f\n", printFrame.origin.x, printFrame.origin.y, printFrame.size.width, printFrame.size.height);
		PSinitclip();
		PSgsave();
		[bitmap drawIn: &printFrame];
		PSgrestore();
		PSsetlinewidth(2.0);
		PSsetgray(0.0);
		PSrectstroke(	printFrame.origin.x, printFrame.origin.y, 
					printFrame.size.width, printFrame.size.height);
		PSsetlinewidth(1.0);
		PSrectstroke(	printFrame.origin.x - 3.0, printFrame.origin.y - 3.0,
					printFrame.size.width + 6.0, printFrame.size.height + 6.0);
		PSselectfont("Times-Roman", 12.0);
		PSmoveto(pWidth / 2.0, printFrame.origin.y - 18.0);
		PScshow((char *)[[NXApp mainWindow] title]);
	}
	
	return self;
}

- getBitmap
{
	return bitmap;
}

@end
