
/* Generated by Interface Builder */

#import "MyView.h"
#import <stdio.h>
#import <appkit/NXImage.h>
#import <3Dkit/N3DRIBImageRep.h>
#import <objc/Object.h>
#import <appkit/appkit.h>
#import <appkit/OpenPanel.h>
#import <dpsclient/wraps.h>

@implementation MyView

- initFrame:(const NXRect *)myFrame
{
    [super initFrame:myFrame];
    [NXImage registerImageRep:[N3DRIBImageRep class]];
    backgroundColor = NX_COLORWHITE;
    return self;
}

- readFile:sender
{
    id                  openPanel;
    const char         *filename;
    const char         *const types[5] = {"rib", "tiff", "eps", "ps", NULL};

    openPanel = [OpenPanel new];
    [openPanel allowMultipleFiles:NO];
    if ([openPanel runModalForTypes:types] &&
		(theFile = filename = [openPanel filename])) {
		if (filename) {
			[self loadImage:filename];
    		[self display];
			}
    }
    return self;
}

- reloadImage:sender
{

	if (theFile) {
		if ([self loadImage:theFile])
			[self display];
		}
	return self;
}

- loadImage:(const char *)filename
{
//    NXPoint             imagePoint;

    if (filename) {
		if (myImage){
//			[myImageRep free];
			[myImage free];
		}
	myImage = [[NXImage alloc] initFromFile:filename];
	if (myImage) {
		[QuickViewerWindow setTitleAsFilename:filename];
	    [myImage setScalable:YES];
	    [myImage getSize:&imageSize];

	    myImageRep = [myImage bestRepresentation];
		
/* Special for RIB Images */
	    if ([[myImageRep class] instancesRespondTo:(SEL)@selector(setHider:)])
		{
			[myImageRep setSurfaceType:N3D_SmoothSolids];
			[myImageRep setHider:N3D_HiddenRendering];
	    }
/* End of special */

/* Cal center of Image
	    imagePoint.x = bounds.origin.x +
		(bounds.size.width - imageSize.width) / 2.0;
	    imagePoint.y = bounds.origin.y +
		(bounds.size.height - imageSize.height) / 2.0; */

	} else {
	    NXRunAlertPanel("Alert", "Error, cannot load image file.",
		"OK", NULL, NULL);
	    return nil;
		}
    return self;
	} else {
		return nil;
		}
}


- drawPS:(const NXRect *)rects :(int)rectCount
{
    NXSize	scaledSize;
    NXPoint	factor;

    NXRectClip(rects);
    NXSetColor(backgroundColor);
    NXRectFill(&bounds);

    if (myImage) {
/* Size to current view's size */
	factor.x = bounds.size.width / imageSize.width;
	factor.y = bounds.size.height / imageSize.height;
	factor.x = factor.y = (factor.x < factor.y) ? factor.x : factor.y;
	scaledSize = bounds.size;
	scaledSize.width = imageSize.width * factor.x;
 	scaledSize.height = imageSize.height * factor.y;
	[myImage setSize:&scaledSize];
	[myImage composite:NX_COPY toPoint:&bounds.origin];
    }
    return self;
}

- free
{
    [myImageRep free];
    [myImage free];
    [super free];
    return self;
}

@end
