
#import "OV_Window.h"
#import "../Controller.h"	// class that our "File's Owner" is...

@implementation OV_Window

- closeOverviewPanel:sender
{
    [ovWindow close];
    return self;
}


- setImageArray:(ovImageArray *)arrayPtr imageCount:(int)count
{
    id		cell;
    int		startRangeTag, rangeInc, endRangeTag;
    BOOL	enableCell;
    
    ovImageCount = count;
    ovImageIds = arrayPtr;
    ovOpenImageAtSize = is_GlobalPref_TAG;
    ovOpenImageAsResolution = ir_GlobalPref_TAG;

    initialOverviewPict = 1;
    startRangeTag = 1;
    rangeInc = [self getMaxImageTag];
    endRangeTag = [self getMaxRangeTag];
    
    //  enable (or disable) options in the OverviewRange matrix
    //  to match the count of images in the array.
    cell = [ovRangeMatrix findCellWithTag:startRangeTag];
    [cell setEnabled:YES];
    [ovRangeMatrix selectCell:cell];

    for ( ; startRangeTag <= endRangeTag; startRangeTag += rangeInc) {
	enableCell = YES;
	if (count < startRangeTag) enableCell = NO;
	[[ovRangeMatrix findCellWithTag: startRangeTag] 
				setEnabled:enableCell];
	}

    return self;
}


- (int)getMaxImageTag
{
    //  There are two cells in the RangeMatrix for *this*
    //      overview window which we do not use
    return [ovImageMatrix cellCount] - 2;
}


- (int)getMaxRangeTag
{
    return 81;
}


- getMyButtonImageSize:(NXSize *)aSize;
{
    NXSize	ovButtonSize = {76.6, 50.6};

    *aSize = ovButtonSize;
    return self;
}


- selectOverviewRange:sender
{
    initialOverviewPict = [sender selectedTag];
    if ( initialOverviewPict < 1 ) initialOverviewPict = 1;
	
    [self showOverview];

    return self;
}


- setOpenImageSize:sender
{
    ovOpenImageAtSize = [sender selectedTag];
    return self;
}


- setOpenImageResolution:sender
{
    ovOpenImageAsResolution = [sender selectedTag];
    return self;
}


- showOverviewImage:sender
{
    int		pictNumber;

    pictNumber = [sender selectedTag];
    if ( pictNumber < 0 ) {
	//  this happens if mouse-up happens outside the button
	//	     that the mouse-down was done in...
	return self;
	}
    pictNumber += initialOverviewPict;
    [mainController showImage:pictNumber atSize:ovOpenImageAtSize
    			atRes:ovOpenImageAsResolution];

    return self;
}


- showOverview
{
    id	cell;
    int	cellTag, maxCellTag, pictNumber;
    NXSize	cellButtonSize = {96, 64};
    NXBitmapImageRep	*bm;
    id		image = nil;
    
    maxCellTag = [self getMaxImageTag];
    [self getMyButtonImageSize:&cellButtonSize];
    
    // set all cells to have no images.
    for( cellTag=0; cellTag < maxCellTag; cellTag++ ) {
	cell = [ovImageMatrix findCellWithTag:cellTag];
	[cell setImage:nil];	// must set image before disabling...
	[cell setEnabled:NO];
	}

    cellTag = 0;
    while ( cellTag < maxCellTag ) {
	pictNumber = cellTag + initialOverviewPict;
	if ( pictNumber > ovImageCount ) break;

	/* setup image with SetDataRetained as YES, so each overview
	   panel can set it's own preferred cellButtonSize
	*/
	bm = (*ovImageIds)[pictNumber];
	[bm setSize:&cellButtonSize];
	image = [[[NXImage alloc] initSize:&cellButtonSize]
		setDataRetained:YES];
	[image useRepresentation:bm];

	cell = [ovImageMatrix findCellWithTag:cellTag];
	[cell setImage: image];
	[cell setEnabled:YES];
	cellTag++;
	}

    [ovWindow setFreeWhenClosed:NO];
    [ovWindow makeKeyAndOrderFront:self];

    return self;
}


@end
