/* SwitchView.m
 * Written By:  Thomas Burkholder
 *
 * You may freely copy, distribute, and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
 * fitness for any particular use.
 */


#import "SwitchView.h"
#import <apps/InterfaceBuilder.h>

@implementation SwitchView

- initFrame:(const NXRect *)frameRect
{
	[super initFrame:frameRect];
	[self setTitle:"SwitchView"];
	views = [[List alloc] init];
	valueSelector = (SEL)0;
	initialViewNumber = 0;
	return self;
}

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

- awakeFromNib
{
	[[self setContentView:[views objectAt:initialViewNumber]] free];
	return self;
}

- views
{
	return views;
}

- takeViewNumberFrom:sender
{
	int i;
	id v,old;
	SEL vs;

	if (!valueSelector && sender)
		vs = [self guessValueSelectorForSwitchSource:sender];
	else
		vs = valueSelector;

	i = (int)[sender perform:vs];
	v = [views objectAt:i];

	if (!v) return self;
	old = [self setContentView:v];
	if (NX_NOT_IN_LIST == [views indexOf:old]) {
		[old free];
	}
	[self display];
	return self;
}

- takeTagFrom:sender
{
	int i;
	id old,v;

	i = [sender tag];
	v = [views objectAt:i];

	if (!v) return self;
	old = [self setContentView:v];
	if (NX_NOT_IN_LIST == [views indexOf:old]) {
		[old free];
	}
	[self display];
	return self;
}

- takeIntValueFrom:sender
{
	int i;
	id old,v;

	i = [sender intValue];
	v = [views objectAt:i];

	if (!v) return self;
	old = [self setContentView:v];
	if (NX_NOT_IN_LIST == [views indexOf:old]) {
		[old free];
	}
	[self display];
	return self;
}

- takeSelectedRowFrom:sender
{
	int i;
	id old,v;

	i = [sender selectedRow];
	v = [views objectAt:i];

	if (!v) return self;
	old = [self setContentView:v];
	if (NX_NOT_IN_LIST == [views indexOf:old]) {
		[old free];
	}
	[self display];
	return self;
}

- takeSelectedColFrom:sender
{
	int i;
	id old,v;

	i = [sender selectedCol];
	v = [views objectAt:i];

	if (!v) return self;
	old = [self setContentView:v];
	if (NX_NOT_IN_LIST == [views indexOf:old]) {
		[old free];
	}
	[self display];
	return self;
}


- setValueSelector:(SEL)aSelector
{
	valueSelector = aSelector;
	return self;
}

- (SEL)valueSelector
{
	return valueSelector;
}

- setInitialViewNumber:(int)num
{
	initialViewNumber = num;
	return self;
}

- (int)initialViewNumber
{
	return initialViewNumber;
}

- (SEL)guessValueSelectorForSwitchSource:sender
{
	if ([sender isKindOf:[Matrix class]]) { int r, c;
		[sender getNumRows:&r numCols:&c];
		if (r >= c)
			return @selector(selectedRow);
		else
			return @selector(selectedCol);
	} else if ([sender respondsTo:@selector(isEditable)] &&
			([sender isEditable]) &&
			([sender respondsTo:@selector(intValue)])) {
		return @selector(intValue);
	} else if ([sender isKindOf:[Button class]] ||
						[sender isKindOf:[ButtonCell class]]) {
		return @selector(tag);
	} else if ([sender respondsTo:@selector(intValue)]) {
		return @selector(intValue);
	} else if ([sender respondsTo:@selector(tag)]) {
		return @selector(tag);
	}
	return (SEL)0;
}

- read:(NXTypedStream *)stream
{
	[super read:stream];
	NXReadType(stream,":",&valueSelector);
	views = NXReadObject(stream);
	return self;
}

- write:(NXTypedStream *)stream
{
	[super write:stream];
	NXWriteType(stream,":",&valueSelector);
	NXWriteObject(stream,views);
	return self;
}
