
#import "UHSelectView.h"

@implementation UHSelectView

+ initialize
{
	[UHSelectView setVersion:UHSELECTVIEW_CURRENT_VERSION];
	return self;
}

- positionView
{
	NXRect subviewFrame;
	float w2,h2;
	
	[currentView getFrame:&subviewFrame];
	w2 = subviewFrame.size.width;
	h2 = subviewFrame.size.height;
	
	switch (position)
	{
	case UHSV_TOPLEFT:
		[currentView moveTo:0.0:(bounds.size.height-h2)];
		break;
		
	case UHSV_TOPCENTER:
		[currentView moveTo:((bounds.size.width-w2)/2.0):(bounds.size.height-h2)];
		break;
		
	case UHSV_TOPRIGHT:
		[currentView moveTo:(bounds.size.width-w2):(bounds.size.height-h2)];
		break;
	
	case UHSV_LEFT:
		[currentView moveTo:0.0:((bounds.size.height-h2)/2.0)];
		break;

	case UHSV_CENTER:
		[currentView moveTo:((bounds.size.width - w2)/2.0) :((bounds.size.height - h2)/2.00)];
		break;

	case UHSV_RIGHT:
		[currentView moveTo:(bounds.size.width-w2):((bounds.size.height-h2)/2.00)];
		break;
		
	case UHSV_BOTTOMLEFT:
		[currentView moveTo:0.0:0.0];
		break;

	case UHSV_BOTTOMCENTER:
		[currentView moveTo:((bounds.size.width - w2)/2.0):0.0];
		break;

	case UHSV_BOTTOMRIGHT:
		[currentView moveTo:(bounds.size.width - w2):0.0];
		break;
	}
	return self;
}

- initFrame:(const NXRect *)frameRect
{
	[super initFrame:frameRect];
	position = UHSV_CENTER;
	isFramed=YES;
	if(!views){
		viewNumber=0;
		views=[[List alloc] initCount:0];
		viewNos=[[Storage alloc]initCount:0 elementSize:sizeof(int) description:"i"];
	}
	[self display];
	return self;
}

- (int)viewCount
{
	return [views count];
}

-awakeFromNib
{
	[self selectViewNumber:0];
	return self;
}
	
- setView:anObject number:(int)viewNo
{
	int index;
	
	if(viewNo<0)return nil;		//only positive view nos allowed
	
	if(!anObject || ![anObject isKindOfClassNamed:"View"])return nil;
	
	/* If we already have that view number, then replace the view at that index. */
	for(index=0; index<[viewNos count]; index++){
		if( *(int *)[viewNos elementAt:index] == viewNo ){
			[views replaceObjectAt:index with:anObject];
			return self;
		}
	}
	
	/* If can't find that view number, then add it at the next index. */
	[viewNos addElement:&(int)viewNo];
	[views addObject:anObject];
	return self;
}

-setView00:anObject
{
	view00=anObject;
	[self setView:anObject number:0];
	return self;	
}

-setView01:anObject
{
	view01=anObject;
	[self setView:anObject number:1];
	return self;
}


-setView02:anObject
{
	view02=anObject;
	[self setView:anObject number:2];
	return self;
}

-setView03:anObject
{
	view03=anObject;
	[self setView:anObject number:3];
	return self;
}

-setView04:anObject
{
	view04=anObject;
	[self setView:anObject number:4];
	return self;
}

-setView05:anObject
{
	view05=anObject;
	[self setView:anObject number:5];
	return self;
}

-setView06:anObject
{
	view06=anObject;
	[self setView:anObject number:6];
	return self;
}

-setView07:anObject
{
	view07=anObject;
	[self setView:anObject number:7];
	return self;
}

-setView08:anObject
{
	view08=anObject;
	[self setView:anObject number:8];
	return self;
}

-setView09:anObject
{
	view09=anObject;
	[self setView:anObject number:9];
	return self;
}

-setView10:anObject
{
	view10=anObject;
	[self setView:anObject number:10];
	return self;
}

-setView11:anObject
{
	view11=anObject;
	[self setView:anObject number:11];
	return self;
}

-setView12:anObject
{
	view12=anObject;
	[self setView:anObject number:12];
	return self;
}

-setView13:anObject
{
	view13=anObject;
	[self setView:anObject number:13];
	return self;
}

-setView14:anObject
{
	view14=anObject;
	[self setView:anObject number:14];
	return self;
}

-setView15:anObject
{
	view15=anObject;
	[self setView:anObject number:15];
	return self;
}

-setView16:anObject
{
	view16=anObject;
	[self setView:anObject number:16];
	return self;
}
-setView17:anObject
{
	view17=anObject;
	[self setView:anObject number:17];
	return self;
}
-setView18:anObject
{
	view18=anObject;
	[self setView:anObject number:18];
	return self;
}

-setView19:anObject
{
	view19=anObject;
	[self setView:anObject number:19];
	return self;
}

-setView20:anObject
{
	view20=anObject;
	[self setView:anObject number:20];
	return self;
}

-setView21:anObject
{
	view21=anObject;
	[self setView:anObject number:21];
	return self;
}

-setView22:anObject
{
	view22=anObject;
	[self setView:anObject number:22];
	return self;
}

-setView23:anObject
{
	view23=anObject;
	[self setView:anObject number:23];
	return self;
}

-setView24:anObject
{
	view24=anObject;
	[self setView:anObject number:24];
	return self;
}

-setView25:anObject
{
	view25=anObject;
	[self setView:anObject number:25];
	return self;
}

- takeTagFrom:sender
{
	[self selectViewNumber:[sender selectedTag]];	return self;
}

- takeIntValueFrom:sender
{
	[self selectViewNumber:[sender intValue]];
	return self;
}

- deactivateColorWellsIn:view
{
	id subviewList;
	int i;
	
	/* If this is a colorwell, deactivate it and leave. */
	if([view isKindOfClassNamed:"NXColorWell"]){
		[(NXColorWell *)view deactivate];
		return self;
	}
	
	/* Else, if this view has no subviews, leave. */
	subviewList=[view subviews];
	if( !subviewList || [subviewList count]==0 ) return self;
	
	/* Else, recurse through the subviews doing the same thing. */
	for(i=0; i<[subviewList count]; i++)
		 [self deactivateColorWellsIn:[subviewList objectAt:i]];
	return self;
}

- selectViewNumber:(int)viewNo
{		
	/* First put the current view back where it came from. */
	if(currentView){
		[currentView removeFromSuperview];   //remove view from this selectView
		[currentView moveTo:currentFrame.origin.x :currentFrame.origin.y];
		[currentSuperview addSubview:currentView]; //put it back
		[self deactivateColorWellsIn:currentView]; //now take care of this
		currentView=nil;
	}
	
	/* Look for that view number and select the view at that index. */
	currentView=[self viewWithNumber:viewNo];
	if(currentView)
		viewNumber=viewNo;
	else {
		/* If not found, select the default view. */
		viewNumber=-1;
		currentView=defaultView;
	}
	
	/* Now install the currentView (unless it is the default and there is no defaultView). */
	if(currentView){
		currentSuperview=[currentView superview];   //save the superview
		[currentView getFrame:&currentFrame];    //save location in superview
		[currentView removeFromSuperview];    //now get it from its superview
		[self addSubview:currentView];   	//add to this selectView
		[self positionView];
		}	

	/* Now notify the delegate that the view has changed */
	if (delegate && [delegate respondsTo:@selector(viewWasSelected:)] )
		[delegate viewWasSelected:self];
				
	[self display];
	[window flushWindow];
	return self;
}

- viewWithNumber:(int)viewNo
{
	int index;
	/* Look for that view number and select the view at that index. */
	for(index=0; index<[views count]; index++){
		if( *(int *)[viewNos elementAt:index] == viewNo )
			return [views objectAt:index];
	}
	return nil;			//return nil if that view number is not found
} 

- (int)viewNumber
{
	if(currentView) return viewNumber;
	else return -1;
}

- drawDefault
{
	char* name="UHSelectView";
	float width,height;
	PSsetgray(NX_LTGRAY);
	NXRectFill(&bounds);
	PSsetgray(NX_BLACK);
	PSsetfont([[[Font newFont:"Helvetica" size:12.0 matrix:NX_IDENTITYMATRIX] set] fontNum]);
	PSstringwidth(name,&width,&height);
	height=6.0;
	PSmoveto((bounds.size.width-width)/2.0, (bounds.size.height-height)/2.0);
	PSshow(name);
	return self;
}

- setDefaultView:aView
{
	if([aView isKindOfClassNamed:"View"]){
		defaultView=aView;
		return self;
	}
	else return nil;
}

- defaultView
{
	return defaultView;
}

- drawSelf:(const NXRect *)rects :(int)rectCount
{
	/* Here we only draw peripheral stuff.  The selected view will be displayed as a subview. */
	if(currentView){
		PSsetgray(NX_LTGRAY);
		NXRectFill(&bounds);
	}
	else [self drawDefault];
	if(isFramed){ 
		PSsetgray(NX_BLACK);
		NXFrameRect(&bounds);
	}
	return self;
}

- (int)position
{
	return position;
}

- setPosition:(int)aPosition
{
 	position=aPosition;
	return self;
}

- makeFramed:(BOOL)framed
{
	isFramed=framed;
	[self display];
	[window flushWindow];
	return self;
}

- (BOOL)isFramed
{
	return isFramed;
}

- (const char*)getInspectorClassName
{
	return "UHSelectViewInspector";
}

- free
{
	[views free];
	[viewNos free];
	return [super free];
}

- read:(NXTypedStream*)stream
{
	[super read:stream];
	/* If latest version...    */
	if(NXTypedStreamClassVersion(stream,"UHSelectView")==[UHSelectView version])
		NXReadTypes(stream,"ic@@",&position,&isFramed,&views,&viewNos);
	/* else read old version...     */
	else
		NXReadTypes(stream,"ii@@",&position,&isFramed,&views,&viewNos);
	return self;
}

- write:(NXTypedStream*)stream
{
	[super write:stream];
	NXWriteTypes(stream,"ic@@",&position,&isFramed,&views,&viewNos);
	return self;
}


@end






