#import <appkit/appkit.h>
#import <stdio.h>
#import <stdlib.h>
#import <strings.h>
#import <math.h>
#import "GraphicApp.h"
#import "Converter.h"
#import "ImageView.h"
#import "PictureDocument.h"
#import "DataPanel.h"
#import "Conversions.h"

@implementation PictureDocument

+ newWithName: (const char *)name at: (int *)x and: (int *)y of: (id)image ofType: (char *)n
{
	int			w = ([image pixelsWide] > 700) ? 700 : [image pixelsWide];
	int			h = ([image pixelsHigh] > 600) ? 600 : [image pixelsHigh];
	NXRect		frameRect = { 	*x, 
								*y - h, 
								w + 19, 
								h + 19 };
	NXRect		viewRect = {0, 0, [image pixelsWide], [image pixelsHigh]};

	self = [super new];
	
	fileFormat = (char *)malloc((strlen(n) + 1) * sizeof(char));
	strcpy(fileFormat, n);
	
	maxWidth = [image pixelsWide];
	maxHeight = [image pixelsHigh];
	
	printInfo = [PrintInfo new];
	[printInfo setMarginLeft:36.0 right:36.0 top:36.0 bottom:36.0];

	scrollView = [ScrollView alloc];
	[scrollView initFrame: &frameRect];
	[scrollView setHorizScrollerRequired: YES];
	[scrollView setVertScrollerRequired: YES];
	[scrollView setLineScroll: 1.0];

	window = [Window alloc];
	[window initContent: 	&frameRect
			style:		NX_RESIZEBARSTYLE
			backing:		NX_BUFFERED
			buttonMask:	NX_CLOSEBUTTONMASK |
						NX_MINIATURIZEBUTTONMASK
			defer:		YES];
	
	[window setDelegate: self];
	[window setTitle: "Imaging Bitmap"];
	[window setTitleAsFilename: name];
	[window setContentView: scrollView];
	[window makeKeyAndOrderFront:self];

	imageView = [ImageView alloc];
	[imageView initFrame: &viewRect withImage: image];

	[scrollView setDocView: imageView];
	[window display];

	if ((*x += 14) > 400) *x = 165;
	if ((*y -= 18) < 400) *y = 700;

	return self;
}

- free
{
	free(fileFormat);
	[printInfo free];
	[imageView free];
	[scrollView free];
	[window free];

	return [super free];
}

- print: sender
{
	[imageView printPSCode: sender];

	return self;
}

- getImage
{
	return [imageView getBitmap];
}

- replaceImageWith: (id)newImage
{
	[imageView replaceImageWith: newImage];
	
	return self;
}

- windowWillResize: sender toSize:(NXSize *)frameSize
{
	if (frameSize->width > maxWidth + 21) frameSize->width = maxWidth + 21;
	if (frameSize->width < 0) frameSize->width = 0;
	if (frameSize->height > maxHeight + 51) frameSize->height = maxHeight + 51;
	if (frameSize->height < 66) frameSize->height = 66;
	
	return self;
}

- windowWillClose:sender
{
#ifdef DEBUG
	fprintf(stderr, "Gee, we're closing!\n");
#endif
	[NXApp close: self];
	
	return self;
}

- windowDidBecomeMain:sender
{
	id		dataPanel, conversionPanel;
	id		image;

#ifdef DEBUG
	(stderr, "Gee, I'm the main window now.\n");
#endif
	
	dataPanel = [NXApp getDataPanel];
	image = [imageView getBitmap];
	[dataPanel setWidth: [image pixelsWide]];
	[dataPanel setHeight: [image pixelsHigh]];
	[dataPanel setBitsPerSample: [image bitsPerSample]];
	[dataPanel setSamplesPerPixel: [image samplesPerPixel]];
	[dataPanel setHasAlpha: [image hasAlpha]];
	[dataPanel setIsPlanar: [image isPlanar]];
	[dataPanel setSizeInBytes: [image bytesPerPlane] * [image numPlanes]];
	[dataPanel setColors: pow(pow(2, [image bitsPerSample]), [image samplesPerPixel])];
	[dataPanel setName: fileFormat];
	[dataPanel setPicName: (char *)[[NXApp mainWindow] title]];

	conversionPanel = [NXApp conversionPanel];
	[conversionPanel 	setSPP: 		[image samplesPerPixel]
					withBPS:		[image bitsPerSample]
					inColorSpace:	[image colorSpace]
					andAlpha:	[image hasAlpha]];

	[NXApp reportDidBecomeMain: self];

	return self;
}

@end
