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

#import "Inspector.h"

#import "Folder.h"
#import "FolderViewer.h"
#import "Globals.h"
#import "Group.h"
#import "GroupInsPane.h"
#import "InspectorPane.h"
#import "ItemCell.h"
#import "ItemList.h"
#import "SwapView.h"

#import <strings.h>


// Inspector mode strings -- for modeMenu items
char *inspectorTitles[] = { "None", "Item", "Group", "Folder", NULL };


@implementation Inspector


// -------------------------------------------------------------------------
//   Creating, Initializing
// -------------------------------------------------------------------------


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



- init
{
	[super init];
	
	[NXApp loadNibSection:"Inspector.nib" owner:self withNames:NO];
	[panel moveTo:inspectorLocation.x :inspectorLocation.y];
	[panel setBecomeKeyOnlyIfNeeded:YES];
	
	currentPane = noInspectorPane;
	previousMode = INSPECT_GROUP;
	if ( inspectorMode != INSPECT_NONE ) [self setInspectorMode:inspectorMode];

	return ( self );
}



- awakeFromNib
/*
	Perform final initializations
*/
{
	currentPane = noInspectorPane;
	previousMode = INSPECT_GROUP;
	if ( inspectorMode != INSPECT_NONE ) [self setInspectorMode:inspectorMode];

	return ( self );
}



- free
{
	return ( [super free] );
}



// -------------------------------------------------------------------------
//   Showing the inspector and setting the mode
// -------------------------------------------------------------------------


- showInspector:sender
{
	if ( inspectorMode == INSPECT_NONE ) {
		[self setInspectorMode:previousMode];
	}

	[panel disableFlushWindow];
	
	[paneView swap];
	[currentPane inspect:target];
	[currentPane showCurrent:self];
	[panel display];
	
	[[panel reenableFlushWindow] flushWindow];

	if ( ![panel isVisible] ) {
		[panel moveTo:inspectorLocation.x :inspectorLocation.y];
		[panel orderFront:self];
	}
	return ( self );
}




- panel
{
	return ( panel );
}



- setFolderMode:sender
{
	return ( [self setInspectorMode:INSPECT_FOLDER] );
}



- setGroupMode:sender
{
	return ( [self setInspectorMode:INSPECT_GROUP] );
}



- setItemMode:sender
{
	return ( [self setInspectorMode:INSPECT_ITEM] );
}



- setInspectorMode:(int)aMode
{
	int oldMode;

	oldMode = inspectorMode;
	
	switch ( aMode ) {
		case INSPECT_ITEM:
		case INSPECT_GROUP:
		case INSPECT_FOLDER:
			inspectorMode = aMode;
			if ( ![panel isVisible] ) {
				[panel moveTo:inspectorLocation.x :inspectorLocation.y];
				[panel orderFront:self];
			}
			break;
		case INSPECT_NONE:
		default:
			// Save the current mode so it can be restored later
			previousMode = inspectorMode;
			inspectorMode = INSPECT_NONE;
			[panel orderOut:self]; // Make sure the inspector gets hidden
			break;
	}
	
	// Save the inspectorMode to defaults database if it has changed
	if ( inspectorMode != oldMode ) {
		NXWriteDefault ( [NXApp appName], PREF_INSPECTORMODE,
			inspectorTitles[inspectorMode] );
	}
	
	[modeCover setTitle:inspectorTitles[inspectorMode]];
	[paneView swap];	// Make sure we have the current pane for this mode
	[self update];			// Then make sure we have a current target
	return ( self );
}



- (int)inspectorMode
{
	return ( inspectorMode );
}



- (BOOL)canInspect:anObject
{
	if ( [currentPane canInspect:anObject] || !anObject )
		return ( YES );
	switch ( inspectorMode ) {
		case INSPECT_FOLDER: return ( [anObject isKindOf:[Folder class]] );
		case INSPECT_GROUP: return ( [anObject isKindOf:[Group class]] );
		case INSPECT_ITEM: return ( [anObject isKindOf:[ItemCell class]] ||
			[anObject isKindOf:[ItemList class]] );
	}
	return ( NO );
}



- inspect:anObject
{
	if ( [self canInspect:anObject] ) {
		target = anObject;
		[self showInspector:self];
		return ( self );
	} else
		return ( nil );
}
	


- inspecting
{
	return ( target );
}



- update
{	
	[NXApp sendAction:@selector(updateInspector:)
		to:[NXApp calcTargetForAction:@selector(updateInspector:)]
		from:self];
	return ( self );
}



// -------------------------------------------------------------------------
//   Selecting the Group Name field
// -------------------------------------------------------------------------


- selectGroupName:sender
/*
This method is called when a new group is created.  It makes the InspectorPanel the key window and selects the Name field in the Group pane, so that the user can immediately type in the name for the new group.
*/
{
	[self setGroupMode:self];
	[groupPane selectNameField:sender];
	return ( self );
}



// -------------------------------------------------------------------------
//   SwapView delegate methods
// -------------------------------------------------------------------------


- swapPaneFor:sender
{
	if ( !target )
		currentPane = noInspectorPane;
	else switch ( inspectorMode ) {
		case INSPECT_FOLDER:	currentPane = folderPane; break;
		case INSPECT_GROUP:	currentPane = groupPane; break;
		case INSPECT_ITEM:	
			currentPane = [target isKindOf:[ItemList class]] 
				? selectionPane : itemPane;
			break;
	}
	return ( currentPane );
}



// -------------------------------------------------------------------------
//   Window Delegate methods
// -------------------------------------------------------------------------


- windowDidMove:sender
{
	if ( sender == panel ) {
		// Save new location in defaults database
		char buf[21];
		NXRect rect;
		[panel getFrame:&rect];
		inspectorLocation = rect.origin;
		sprintf ( buf, "%.0f %.0f", rect.origin.x, rect.origin.y );
		NXWriteDefault ( [NXApp appName], PREF_INSPECTORLOCATION, buf );
	}
	
	return ( self );
}



- windowWillClose:sender
{
	if ( sender == panel ) {
		[self setInspectorMode:INSPECT_NONE];
	}
	return ( self );
}



@end


