/* 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: 	24.02.1994
 */

#import <misckit/misckit.h>

@implementation MiscSwapViewByPopUp

- 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;	
}

@end

/*
 * History: 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!
 */