#import <dpsclient/wraps.h>	/* for PSxxx functions */
#import <appkit/Control.h>
#import <appkit/TextField.h>
#import <appkit/ScrollView.h>
#import <appkit/tiff.h>		/* NXImageBitmap */
#import <appkit/graphics.h>
#import <appkit/appkit.h>
#import <stdio.h>
#import <stdlib.h>
#import <string.h>
#import "ToyWin.h"
#import "ToyView.h"
#import "TVController.h"
#import "strfunc.h"


@implementation ToyWin (Drawing)

- drawView:(unsigned char **)map info:(commonInfo *)cinf
{
	ToyView *view;
	id tmp;

	[scView setHorizScrollerRequired: YES];
	[scView setVertScrollerRequired: YES];
	[scView setBorderType: NX_NOBORDER];
	tmp = [[ToyView alloc] setCommText: commentText];
	if ((view = [tmp initDataPlanes:map info:cinf]) == nil) {
		[tmp free];
		return nil;
	}
	if ((tmp = [scView setDocView: view]) != nil)
		[tmp free];
	[scView setCopyOnScroll: YES];
	[self scrollProperly];
	[thiswindow display];
	[thiswindow makeKeyAndOrderFront: self];
	[commentText setStringValue: cinf->memo];
	NXPing();
	return self;
}


- (int)drawFromFile:(const char *)fileName or:(NXStream *)stream
{
	id tmp, view;
	commonInfo *cinf;

	[scView setHorizScrollerRequired: YES];
	[scView setVertScrollerRequired: YES];
	[scView setBorderType: NX_NOBORDER];
	tmp = [[ToyView alloc] setCommText: commentText];
	view = stream
		? [tmp initFromStream: stream]
		: [tmp initFromFile: fileName];
	if (view == nil) {
		[tmp free];
		return Err_OPEN;
	}
	cinf = [view commonInfo];
	[self initLocateWindow:fileName width:cinf->width height:cinf->height];
	[self makeComment: cinf];
	[commentText setStringValue: cinf->memo];
	if ((tmp = [scView setDocView: view]) != nil)
		[tmp free];
	[scView setCopyOnScroll: YES];
	[self scrollProperly];
	[thiswindow display];
	[thiswindow makeKeyAndOrderFront: self];
	NXPing();
	return 0;
}

- makeComment:(commonInfo *)cinf
{
	const char *alp = cinf->alpha ? "  Alpha" : "";

	if (cinf->bits == 1 && cinf->numcolors <= 2)
		sprintf(cinf->memo, "%d x %d  Bilevel%s",
			cinf->width, cinf->height, alp);
	else
		sprintf(cinf->memo, "%d x %d  %dbit%s%s%s",
			cinf->width, cinf->height, cinf->bits,
			((cinf->bits > 1) ? "s" : ""), 
			((cinf->numcolors <= 2) ? " gray" : ""), alp);
	return self;
}

- makeComment:(commonInfo *)cinf from:(const commonInfo *)originfo
{
	const char *p;

	[self makeComment: cinf];
	if ((p = begin_comm(originfo->memo, YES)) != NULL) {
		strcat(cinf->memo, " : ");
		comm_cat(cinf->memo, p);
	}
	return self;
}

- (commonInfo *)drawToyWin:(const char *)fileName type:(int)type
	map:(unsigned char **)map err:(int *)err
{
	*err = [self drawFromFile: fileName or: NULL];
	return NULL;
}

@end
