/*
   This creates the window that displays an individual Photo image.
   It is also the delegate for that window.
*/

#import "PhotoWinProcs.h"
#import "ImageView.h"

@implementation PhotoWinProcs

- (enum _NXWindowDepth)getDepthOfNewWindows
{
    return depthOfNewWindows;
}


- setDepthOfNewWindows:(enum _NXWindowDepth)windowDepth
{
    depthOfNewWindows = windowDepth;
    return self;
}


- displayImage:(id)theImage withName:(char *)imName atResolution:(int)imRes
{
    Window	 *win;
    int		pixHigh, pixWide;
    NXRect   imRect = {{10.0, 10.0}, {0.0, 0.0}};
	
    pixHigh = [theImage pixelsHigh];
    pixWide = [theImage pixelsWide];
    [theImage getSize:&imRect.size];	// for reference purposes

    if ( imRes >= 72 ) {
	/* turn image into the requested resolution */
	imRect.size.width  = ( pixWide * 72.0) / (float)imRes;
	imRect.size.height = ( pixHigh * 72.0) / (float)imRes;
	[theImage setSize:&imRect.size];
	}

    win = [[Window alloc] initContent:&imRect
		style:NX_TITLEDSTYLE
		backing:NX_BUFFERED
		buttonMask:NX_CLOSEBUTTONMASK|NX_MINIATURIZEBUTTONMASK
		defer: YES];
    [win setDelegate:self];
    [win setTitleAsFilename:imName];

    /* we have to set the depth here, or cut & paste operations may not
       keep all the desired information (particularly if running this
       on a greyscale station).
    */
    [win setDepthLimit:depthOfNewWindows];
    [win setContentView:[[ImageView alloc] initForImage:theImage :&imRect]];
    [win makeKeyAndOrderFront:self];

    return self;
}

- windowWillClose:sender
{
    id		imageID;
//    NXBitmapImageRep  *imageBitmap;
    u_char	*rgb[5];
    
    /*  have to free up the image that the window is displaying,
        and due to the way we got the bitmap for that image, the
	RGB data planes have to be found and freed too */
    imageID = [[sender contentView] image];
    
    /* the following should be done via a call to freeDataPlanes in
       photoCD_Rdr, but at the moment it isn't... */
    if ( [imageID isPlanar] ) {
	/* a rather sleezy way to distinguish between images that
	   were read in and images which were cut/pasted in...  */
	[imageID getDataPlanes:rgb];
	if ( rgb[0] ) free( rgb[0] );
	if ( rgb[1] ) free( rgb[1] );
	if ( rgb[2] ) free( rgb[2] );
	}
    
    [imageID free];
    
    return self;
}


@end
