
/*
	Copyright 1993  Jeremy Slade.  All rights reserved.
*/

#import "ItemInsPane.h"

#import "ItemCell.h"
#import "SwapView.h"
#import "Globals.h"


@implementation ItemInsPane


// -------------------------------------------------------------------------
//   Creating, initializing
// -------------------------------------------------------------------------


+ initialize
{
	[self setVersion:ItemInsPane_VERSION];
	return ( self );
}



- initContent:(const NXRect *)contentRect
	style:(int)aStyle
	backing:(int)bufferingType
	buttonMask:(int)mask
	defer:(BOOL)flag
{
	[super initContent:contentRect
		style:aStyle
		backing:bufferingType
		buttonMask:mask
		defer:flag];
	
	return ( self );
}



- awakeFromNib
/*
	Perform final initializations
*/
{
	return ( self );
}



- free
{
	fileTypePane = [fileTypePane free];
	folderTypePane = [folderTypePane free];
	appTypePane = [appTypePane free];
	return ( [super free] );
}



// -------------------------------------------------------------------------
//   General Item stuff
// -------------------------------------------------------------------------


- (BOOL)canInspect:anObject
{
	return ( [anObject isKindOf:[ItemCell class]] );
}



- showCurrent:sender
{
	[[swapView window] disableFlushWindow];

	[pathField setTextDelegate:nil];
	[pathField setStringValue:[target path]];
	[pathField setTextDelegate:self];
	
	[groupLaunchSwitch setState:[target isGroupLaunch]];
	[autoLaunchSwitch setState:[target isAutoLaunch]];
	
	[swapView swap]; // Make sure we have the correct pane for the target type
	switch ( [target fileType] ) {
		case FT_FILE:
			[specificAppSwitch setState:[target hasSpecificApp]];
			[specificAppField setEnabled:[target hasSpecificApp]];
			[specificAppField setTextDelegate:nil];
			[specificAppField setStringValue:[target appName]];
			[specificAppField setTextDelegate:self];
			break;
			
		case FT_SUBDIR:
			[createBrowserSwitch setState:[target createBrowser]];
			[rootedAtField setTextDelegate:nil];
			[rootedAtField setStringValue:[target browserRootedAt]];
			[rootedAtField setTextDelegate:self];
			break;
			
		case FT_APPLICATION:
			[hideIconSwitch setState:[target hideIcon]];
			break;
		
		case FT_UNKNOWN:
			break;	
	}
	
	[[[swapView window] reenableFlushWindow] flushWindow];
	return ( [super showCurrent:self] );
}



- swapPaneFor:sender
{
	switch ( [target fileType] ) {
		case FT_FILE: return ( fileTypePane );
		case FT_SUBDIR: return ( folderTypePane );
		case FT_APPLICATION: return ( appTypePane );
	}
	return ( nil );
}



- changeItemPath:sender
/*
Called by pathField when editing finshes on that field
*/
{
	if ( !strlen ( [pathField stringValue] ) ) {
		[pathField selectText:self];
		return ( self );
	}
	
	[target setPath:[pathField stringValue]];
	
	return ( self );
}



- changeItemGroupLaunch:sender
/*
Called by groupLaunchSwitch when it is clicked
*/
{
	[target setGroupLaunch:[groupLaunchSwitch state]];
	return ( self );
}



- changeItemAutoLaunch:sender
/*
Called by autoLaunchSwitch when it is clicked
*/
{
	[target setAutoLaunch:[autoLaunchSwitch state]];
	return ( self );
}



// -------------------------------------------------------------------------
//   File-type Items
// -------------------------------------------------------------------------


- changeUseSpecificApp:sender
/*
Called by the specificAppSwitch when it is clicked
*/
{
	if ( [specificAppSwitch state] ) {
		[specificAppField setEnabled:YES];
		[specificAppField selectText:self];
	} else {
		[specificAppField setEnabled:NO];
		[target setAppName:NULL];
		[self showCurrent:self];
	}
	
	return ( self );
}



- changeSpecificApp:sender
/*
Called by the specificAppField when editing in the field finishes
*/
{
	[target setAppName:[specificAppField stringValue]];
	return ( self );
}



- changeHideIcon:sender
/*
Called by the hideIconSwitch when it is clicked
*/
{
	[target setHideIcon:[hideIconSwitch state]];
	return ( self );
}



// -------------------------------------------------------------------------
//   Folder-type items
// -------------------------------------------------------------------------


- changeCreateBrowser:sender
/*
Called by createBrowserSwitch when it is clicked
*/
{
	[target setCreateBrowser:[createBrowserSwitch state]];
	if ( [createBrowserSwitch state] ) {
		const char *s = [rootedAtField stringValue];
		if ( !(s && strlen ( s )) ) [rootedAtField setStringValue:"/"];
		[rootedAtField selectText:self];
	}
	return ( self );
}



- changeRootedAt:sender
/*
Called by rootedAtField when editing finishes in the field
*/
{
	[target rootBrowserAt:[rootedAtField stringValue]];
	return ( self );
}



// -------------------------------------------------------------------------
//   Text delegate methods
// -------------------------------------------------------------------------


- textDidChange:sender
{
	if ( [sender isKindOf:[Text class]] ) sender = [sender delegate];

	if ( sender == pathField ) {
	} else
	if ( sender == specificAppField ) {
	} else
	if ( sender == rootedAtField ) {
	}
	return ( self );
}



- textDidEnd:sender endChar:(unsigned short)endChar
/*
Make sure changes made in field get saved.  If the field was exited with a return, then it will automatically send the action (which is where it gets saved).  If the field was exited with a tab, we need to send the action ourselves.
*/
{
	if ( [sender isKindOf:[Text class]] ) sender = [sender delegate];

	if ( endChar == NX_TAB ) [sender sendAction];
		else [[NXApp mainWindow] makeKeyWindow];
	return ( self );
}


@end
