
/* Generated by Interface Builder */

#import "ColorMapView.h"
#import "ColorMap.h"
#import <stdio.h>
#import <stdlib.h>
#import <strings.h>
#import <defaults.h>
#import <appkit/tiff.h>
#import <appkit/color.h>
#import <appkit/graphics.h>
#import <appkit/Control.h>
#import <appkit/OpenPanel.h>
#import <appkit/Application.h>
#import <dpsclient/psops.h>
#import <dpsclient/wraps.h>

#define COLCONV (NUMCOLORS - 1.0)

@implementation ColorMapView

- initFrame:(const NXRect *)frm		// designated initializer
{
	char *slashPos;

	[super initFrame:frm];
	first = YES;
	colorMap = [[ColorMap alloc] init];
	
// get .app wrapper pathname

    appPath = (char *)malloc(256);
    strcpy(appPath, NXArgv[0]);
    if (slashPos = strrchr(appPath, '/')) {
    	sprintf(slashPos+1, "\0");
    } else {
        strcpy(appPath, "./");
    }
	return self;
}

- colorMap
{
	return colorMap;
}

- loadMap:sender
{
	const char *const *files;
	char *file, *mapPath;
	const char *dir;
	static const char *const ft[2] = {"map", NULL};
    
	id openPanel = [OpenPanel new];
	[openPanel allowMultipleFiles:NO];
	if (first) {
		mapPath = (char *)malloc(1024);
		strcpy(mapPath, appPath);
		strcat(mapPath,"maps");
		[openPanel runModalForDirectory:mapPath file:NULL types:ft];
		first = NO;
		free(mapPath);
	}
	else  [openPanel runModalForTypes:ft];
	files = [openPanel filenames];
	dir = [openPanel directory];
	if (!files) return self;
	if (!files[0]) return self;
	file = malloc(strlen(files[0]) + strlen(dir) + 8);
	strcpy(file, dir);
	strcat(file,"/");
	strcat(file, files[0]);
	strcat(file, "\0");
	[colorMap initFromFile:file];
	[self update];
	return self;
}

- drawSelf:(NXRect *)rects :(int)rectCount	// internal method
{	// this is super simple, but doing a wrap would still help.
	register int i;
	int r, g, b;
	
	NXDrawGrayBezel(&bounds, rects);
	for (i=0; i<NUMCOLORS; i++) {
		[colorMap colorFor:i :&r :&g :&b];
		NXSetColor(NXConvertRGBToColor(r/COLCONV, g/COLCONV, b/COLCONV));
		PSmoveto(i + NX_X(&bounds) + OFFSET, NX_Y(&bounds) + OFFSET);
		PSlineto(i + NX_X(&bounds) + OFFSET,
			NX_Y(&bounds) + NX_HEIGHT(&bounds) - OFFSET);
		PSstroke();
	}
	return self;
}

- free
{
	[colorMap free];
	return self;
}

@end
