/* MiscSwapViewByPopUp.m				 
 *
 * This is a SwapView that can handle popUps either by tag or by title.
 * If a tag is not 0 it will try to choose the viewController by title.
 *
 * For more interface-info see the header file. More in depth information
 * can be found here in the source-code.
 *
 * Written by: 		Thomas Engel
 * Created:    		24.01.1994 (Copyleft)
 * Last modified: 	25.09.1994
 */

//#import <misckit/misckit.h>
#import "MiscSwapViewByPopUp.h"

// The following are for keeping track of versions when archiving instances
// of MiscSwapViewByPopUp. If you change the write: method, make sure to
// bump up the version below and make the appropriate changes to the read:
// method so all previously archived instances will be unarchivable.

#define MISC_SVBP_VERSION 0
#define MISC_SVBP_CLASSNAME "MiscSwapViewByPopUp"


@implementation MiscSwapViewByPopUp

+ initialize 
{
	// Sets the class version for archiving purposes.
	
	if (self == [MiscSwapViewByPopUp class])
		[self setVersion: MISC_SVBP_VERSION];
	
	return self;
}

- setPopUpCover:anObject
{
	// Because it is not possible to set the popUpMenus actionMethod inside
	// IB we have to do it here. But who cares. It's nicer this way anyway.
	// By default there is no selectedCell inside the popUp. We will change
	// that.
	
	int	index;
	id	popUp;
	id	matrix;
	
	popUpCover = anObject;
	popUp = [popUpCover target];
    [popUp setTarget:self];
	[popUp setAction:@selector(swapContentView:)];
	index = [popUp indexOfItem:[popUpCover title]];
	
	if( index > -1 )
	{	
		matrix = [popUp itemList];
		
	 	if( ![matrix selectedCell] )
			[matrix selectCell:[[matrix cellList] objectAt:index]];
	}
	return self;
}

- popUpCover
{
	return popUpCover;
}

- swapContentView:sender
{
	// We find the right popUp item and if really have one we can simply set 
	// the trigger to the right object and use the simple ..byTag/obj methods
	// provided by our super-class. 
	
	id	aPopUpItem;

	aPopUpItem = [self realTrigger];
	if( aPopUpItem ) sender = aPopUpItem;
	
	return [super swapContentView:sender];
}

- realTrigger
{
	// Here we try to find the currently selected popUpItem. This is the
	// real trigger of a action.
	// We assume that there always is a selected cell. If this object is used
	// properly this is true because setPopUpCover causes such a init!
	
	id	realTrigger;
	id	popUpMatrix;
	
	popUpMatrix = [[popUpCover target] itemList];
	realTrigger = [popUpMatrix selectedCell];
	
	// Being envoked by keyboard causes the right popUp item to be
	// selected but does not change the covers title. This is what we do
	// here. Maybe not always needed but who cares.
	
	[popUpCover setTitle:[realTrigger title]];
	
	return realTrigger;	
}

- read: (NXTypedStream *)stream
{
  int  version;
  
	[super read: stream];

	// If you add or remove archived variables, you'll have to
	// change this method so it will be able to read all previous
	// versions. Just add a case for your new version and leave
	// the rest of the switch statement alone.
	 
	version = NXTypedStreamClassVersion (stream, MISC_SVBP_CLASSNAME);
	
	switch (version)
	{
	  case 0:
	  	popUpCover = NXReadObject (stream);
	  	break;
		
	  default:
	  	break;
	}
			
	return self;
}

- write: (NXTypedStream *)stream
{
	[super write: stream];
	NXWriteObjectReference (stream, popUpCover);

	return self;
}

@end

/*
 * History: 25.09.94 Added archiving (read:, write:, +initialize)
 *
 *			24.02.94 Conform to new swapView methods.
 *
 *			25.01.95 Move the popUp init (selectedCell) to setPopUpCover
 *
 *			24.01.94 Made it MiscSwap conform.
 *
 *			08.01.94 Renamed it to swapPopUpController and did some real
 *					 nice redesign.
 *
 *			20.12.93 Enlightened the controller to check the tags if they are
 *					 set. This helps to localize apps.
 *
 *			04.12.93 Added a delegate to this class the enable better 
 *					 command-key handling.
 *
 *			04.11.93 First steps to a general-purpose swapPopManager.
 *
 *
 * Bugs: - does only handle real popUps. PullDowns might not realy work 
 *		   because I always set the title inside the trigger method. Hmm
 *		   Maybe its better to write a seperate pullDown Controller!
 */