//*****************************************************************************
//
//	SwapLaterView.m.  
//
//		Subclass of SwapView which allows a control in the view heirarchy
//		to trigger swapping.  Unlike is ancestor class swapping is not
//		performed immediately but scheduled to occur later.
//		
//			by	Felipe A. Rodriguez		
//
//	This code is supplied "as is" the author's makes no warranty as to its 
//	suitability for any purpose.  This code is free and may be distributed 
//	in accordance with the terms of the:
//		
//			GNU GENERAL PUBLIC LICENSE
//			Version 2, June 1991
//			copyright (C) 1989, 1991 Free Software Foundation, Inc.
// 			675 Mass Ave, Cambridge, MA 02139, USA
//
//*****************************************************************************

#import "SwapLaterView.h"



@implementation SwapLaterView

//*****************************************************************************
//
// 		designated initilizer for swapviews
//
//*****************************************************************************

- initFrame:(const NXRect *)theFrame
{
char title[] = {"FirstInspector"};

	[super initFrame:theFrame];
	
	lastInspector = NULL;
	currentInspector = NULL;
	backgroundGray = NX_LTGRAY;
	theTitle = title;	
	[self setBackgroundGray:NX_LTGRAY];		// default

	return self;
}
//*****************************************************************************
//
// 		swaps views in and out of inspector's swapview
//
//*****************************************************************************
 
- swapIt
{
	if([self whatPanel])			
		{							// make sure this is a new view to swap in
		if(lastInspector != inspector)		
			{	 									
			if(lastInspector)						// swap Later
				[self perform:@selector(swapLater) with:self afterDelay:0 	
														cancelPrevious:YES];
			else
				{
				[self addSubview:[inspector contentView]];
				lastInspector = inspector;		// remember panel it came from 	
				[self display];
				}
			}
		}
	else
		{							// no inspector so clean up the background
		[self lockFocus];
		PSsetgray(backgroundGray);			// use the backgroundGray
		NXRectFill(&bounds);
		[self unlockFocus];
		lastInspector = (id)NULL;			// now the last inspector is NULL 
		NXPing();				// let the window server cetch up... (yawn...)
		}							

	return self;
}
//*****************************************************************************
//
// 		delayed swapping of views in the view heirarchy 
//
//*****************************************************************************

- swapLater
{
	[self replaceSubview:[lastInspector contentView] 
												with:[inspector contentView]];
	lastInspector = inspector;		// remember the panel it came from 	
	[self display];

	return self;
}
//*****************************************************************************
//
// 		This should return the id of a panel, which is off screen, and 
//		buffered, but not defered (SwapView needs its gstate).  SwapView will 
//		take the contentView of the panel and swap it in while also removing 
//		the old view, if any, and placing it back into its old panel.
//
//	 WARNING!!!!
//	Don't forget to make sure that the off screen windows are NOT deferred!!!
//	This means make sure that you turn the Deffered switch OFF in IB when
//	looking at the Atributes inspector.  This allows offscreen drawing.  
//	If it was on, then the gstate will be zero!!!!
//
// 		Ideally::  these panels should all be nibs so they have controllers
//				   and the above code should load the nib section if it
//				   already isn't loaded.  It should load here because you only
//				   want to take up memory when it is demanded. 
//
//*****************************************************************************

- whatPanel
{
	if(theTitle) 		// find out what panel to give to the SwapView
		{
		if (!strcmp(theTitle,"FirstInspector"))			// for use by swapViews
			return inspector = firstInspectorPanel;		// w/o a popUpList
		
		if (!strcmp(theTitle,"SecondInspector"))		// for use by swapViews
			return inspector = secondInspectorPanel;	// w/o a popUpList

		switch([popup indexOfItem:theTitle])
			{
			case 0:
			default:
				inspector = firstInspectorPanel;
				break;
				
			case 1:  				
				inspector = secondInspectorPanel;
				break;
			}
		return inspector;
		}
								// the button title has no related inspector, 
	return (id)NULL;			// this should never happen.	
}
//*****************************************************************************
//
// 		this is to allow us to junp to any inspector given its key name 
//
//*****************************************************************************

- inspectName:(const char *)str
{
	if (str) 
		theTitle = str; 
	[self swapIt];
	
	return self;
}

@end
