// Tue Oct  8 10:09:42 MET 1996 by Daniel Bðhringer

// Built using the very educative ListInspector of Ralph Zazula (zazula@running-start.com) as
// template

/*	toDo: create a contents inspector that handles a NXDefaultsVector which is registered in
 *		  DefaultController's awakeFromNib- message
 *
 */

#import <apps/InterfaceBuilder.h>
#import <appkit/NXImage.h>
#import "DefConnectionInspector.h"
#import "DefaultController.h"

@interface Button(PopUpButtonSel)
-(void)selectCellAt:(int)position;
-(int) selectedTag;
@end

@implementation Button(PopUpButtonSel)
-(void)selectCellAt:(int)position
{	Matrix		*matrix=[[self target] itemList];
	[matrix selectCellAt:position :0];
	[self setTitle:[[matrix selectedCell] title]];
}
- (int)selectedTag
{	Matrix		*matrix=[[self target] itemList];

	if(![matrix selectedCell])
		[matrix selectCellAt:[[self target] indexOfItem:[self title]] :0];

	return [[matrix selectedCell] tag];
}
@end


@implementation DefConnectionInspector

- init
{	char path[MAXPATHLEN+1];
	NXBundle *bundle = [NXBundle bundleForClass:[self class]];

	[bundle getPath:path forResource:"DefConnectionInspector" ofType:"nib"];
	[NXApp loadNibFile:path owner:self withNames:NO fromZone:[self zone]];
	return self;
}

- ok:sender
{	if([NXApp isConnecting])
	{	[object addObjectIfAbsent:[NXApp connectDestination]];
		[self revert:nil];	// display¼
	} return [super ok:sender];
}

- revert:sender
{	if (!sender)
	{	[okButton setTitle:"Connect"];
		[okButton setEnabled:([NXApp isConnecting] && 
			 [object indexOf:[NXApp connectDestination]] == NX_NOT_IN_LIST)];
		
		[revertButton setTitle:"Disconnect"];
		[revertButton setEnabled:([NXApp isConnecting] && 
			[object indexOf:[NXApp connectDestination]] != NX_NOT_IN_LIST)];
			
	} else	// disconnecting
	{	[object removeObjectAt:[[browser matrixInColumn:0] selectedRow]];
		[NXApp stopConnecting];
	} [browser loadColumnZero];	//update

	/*
	 * This next bit checks to see if the user has dragged out a connection
	 * that duplicates one we've already stored in the list.  If so, reflect
	 * this by selecting the appropriate cell in the browser.
	 */
	if([NXApp isConnecting] && 
		([object indexOf:[NXApp connectDestination]] != NX_NOT_IN_LIST))
	{	[[browser matrixInColumn:0] 
				selectCellAt:[object indexOf:[NXApp connectDestination]]:0];
		[self displayDBStuff:browser];
	} return [super revert:sender];
}

- (BOOL)wantsButtons
{	return YES;
}

- dbTextChanged:sender
{	[object registerName:[defName stringValue] andType:[typePopup selectedTag]
					 for:[object objectAt:[[browser matrixInColumn:0] selectedRow]]];
	return self;
}
- displayDBStuff:sender
{	const char 				*aName;
	DefaultControllerType_t  aType;
	id	  					 uiObjc=[object objectAt:[[browser matrixInColumn:0] selectedRow]];

	if([object getName:&aName andType:&aType for:uiObjc])
	{	[defName setStringValue:aName];
		[typePopup selectCellAt:aType];
	} return self;
}
- displayConnection:sender
{	id uiObjc=[object objectAt:[[browser matrixInColumn:0] selectedRow]];

	[self displayDBStuff:sender];
	[NXApp displayConnectionBetween:object and:uiObjc];
	
	return self;
}

// NXBrowser delegation¼
-(int) browser:sender fillMatrix:matrix inColumn:(int)column
{	int		i;
	id		cellList, theCell;
	char	buf[128];
	id		item;
	
	[matrix renewRows:[object count] cols:1];	//Set the matrix to have the right number of cells.
	cellList = [matrix cellList];	// Get list of cells from the matrix.

	/*
	 * For each cell set its value, set whether it is a leaf
	 * or not and mark it loaded.
	 */
	for(i=0; i<[cellList count]; i++)
	{	item = [object objectAt:i];
		theCell = [cellList objectAt:i];
		if([item respondsTo:@selector(title)] && [item title] && strlen([item title]))
		{	sprintf(buf, "%s (%s)", [[item class] name], [item title]);
		} else sprintf(buf, "%s", [[item class] name]);
		[theCell setStringValue:buf];
		[theCell setLeaf:YES];
		[theCell setLoaded:YES];
	} return [object count];	//Return the number of rows.
}

@end


@implementation DefaultController(IBInfoStuff)

- getIBImage
{	char path[1024];
	NXImage *image = [NXImage findImageNamed:"defaultController"];
	if(!image) {
		NXBundle *bundle = [NXBundle bundleForClass:[self class]];
		if([bundle getPath:path forResource:"defaultController" ofType:"tiff"])
			image = [[NXImage alloc] initFromFile:path];
	} return image;
}

- (const char *)getConnectInspectorClassName
{	return "DefConnectionInspector";
}

@end
