
/* 	STSwapBoxManager.m
	created by Robert Vasvari - July 1994

	This class is an abstract superclass that is
	supposed to make it easier to add or modify
	multi screen preferences panels.
	The steps to take to add a new swapBox:
	1: make a new box in IB that contains the matrices,
	   textfields etc, and fit it into the mid-section
	   of the panel. At runtime the subviews of the panel
	   will be parsed. All boxes that have a title in the form:
	   "screen box %d" will be hooked up to the popup cell with
	   tag %d.
	2: set the necessary pointers to the box and its contents.
	3: subclass the following methods:
		- loadDataFor:(int)boxTag;
		- dataChanged:sender;
		- set:sender;
	Hopefully it will just work...
*/

#import "STSwapBoxManager.h"
#import "STAppController.h"
#import "STUtil.h"

@implementation STSwapBoxManager

- init
{	[super init];
	currentBoxTag=0;
	needsDisplay=YES;
	return self;
}

- awakeFromNib
{
int t, i=0, count=0;
id view=nil, cell=nil, matrix=[[mainPopup target] itemList];
id list;
char *p;

	maxIndex= -1;
	list=[[panel contentView] subviews];
	count=[list count];
	if(!list) return self;
	
	for(i=0;i<count;i++)
	 { p=NULL;
	   t=0;
	   view=[list objectAt:i];
	   if([view isKindOf:[Box class]])
	    { if(!(p=(char *)[view title])) continue;
		  if(strncmp(p,"screen box ",11)) continue;
		  sscanf(p,"screen box %d",&t);
		  if(!(cell=[matrix findCellWithTag:t]))
		   { printf(
		      "bad boxtag in SwapBoxManager setupBoxes:%d\n",t); 
			 continue;
		   }
		  [cell setTarget:self];
		  [cell setAction:@selector(changeBox:)];
		  boxes[t]=view; 
		  [view setTitlePosition:NX_NOTITLE];
		  if(t>maxIndex) maxIndex=t;
		  t++;
		}
	 }

	i=0;
	while(boxes[i])
	 { [boxes[i++] removeFromSuperview];
	 }
	return self;
}

- setNeedsDisplay:(BOOL)flag
{	needsDisplay=flag;
	return self;
}

- (BOOL)needsDisplay
{	return needsDisplay;
}

- (int)currentBoxTag
{	return currentBoxTag;
}

- loadDataFor:(int)boxTag
{
	/* general purpose method to display a box specified by the tag
   		in the preferences panel 
 	 */
	/* here should be a switch statement to load in all
	   data for a particular screen 
	 */
	return self;
}

- dataChanged:sender
{
	/* this method figures out if there is any change in the currently
	   selected box and enables/disables the set/revert buttons */
	
	/* here should be a switch statement to figure out 
	   for a particular screen if there was a change
	 */
	return self;
}

- textDidGetKeys:textObj isEmpty:(BOOL)flag
{	[self dataChanged:self];
	return self;
}

- set:sender
{	
	/* here should be a switch statement to
	   save all the changes
	 */
	return self;
}

- changeBox:sender
{
	if([panel isDocEdited])
	 { switch(NXRunAlertPanel("Alert",
	    "Recent changes that haven't been saved will be lost!",
	    "Save Changes","Close Anyway","Cancel"))
		  { case NX_ALERTDEFAULT: [self set:setButton]; break;
		    case NX_ALERTALTERNATE: break;
			case NX_ALERTOTHER: return nil;
		    default: return nil;
		  }
	 }
	[panel disableFlushWindow];
	[self getPanelWithScreen:[[sender selectedCell] tag]];
	[panel reenableFlushWindow];
	[panel flushWindow];
	return self;
}

- getPanelWithScreen:(int)index
{
	[[[mainPopup target] itemList] selectCellWithTag:index];
	[mainPopup setTitle:
	 [[[[mainPopup target] itemList] selectedCell] title]];
	[self displayBox:index];
	[revertButton setEnabled:NO];
	[setButton setEnabled:NO];
	[panel setDocEdited:NO];
	[panel display];
	[panel makeKeyAndOrderFront:nil];
	return self;
}

- displayBox:(int)boxTag
{
	if(!boxes[boxTag])
	 { fprintf(stderr,"displayBox: bad boxTag:%d\n",boxTag);
	   return self;
	 }

	/* remove the old box */
	if([boxes[currentBoxTag] superview])
		[boxes[currentBoxTag] removeFromSuperview];

	currentBoxTag=boxTag;
	 
	/* add the new box :
	   if the noSelectionBox variable is set, and there is no currentDoc,
	   use it
	 */ 
	if(noSelectionBox)
	 {	if([[NXApp delegate] currentDoc])
			{ 	if([noSelectionBox superview])
					[noSelectionBox removeFromSuperview];
			if(![boxes[currentBoxTag] superview])
				[[panel contentView] addSubview:boxes[currentBoxTag]];
			}
		else
			{ if(![noSelectionBox superview])
					[[panel contentView] addSubview:noSelectionBox];
			}
	 }
	else
	 { if(![boxes[currentBoxTag] superview])
				[[panel contentView] addSubview:boxes[currentBoxTag]];
	 }
	[self loadDataFor:boxTag];
	return self;
}

- revert:sender
{	
	[panel disableDisplay];
	[panel disableFlushWindow];
	[self loadDataFor:[[[[mainPopup target] itemList] selectedCell] tag]];
 	if([revertButton isEnabled]) [revertButton setEnabled:NO];
	if([setButton isEnabled]) [setButton setEnabled:NO];
	[panel setDocEdited:NO];
	[panel reenableDisplay];
	[panel display];
	[panel reenableFlushWindow];
	[panel flushWindow];
	return self;
}

- windowWillClose:sender 
{	if([panel isDocEdited])
	 { switch(NXRunAlertPanel("Alert",
	    "Recent changes that haven't been saved will be lost!",
	    "Save Changes","Close Anyway","Cancel"))
		  { case NX_ALERTDEFAULT: [self set:setButton]; break;
		    case NX_ALERTALTERNATE: break;
			case NX_ALERTOTHER: return nil;
		    default: return nil;
		  }
	 }
	return self;
}

- windowDidUpdate:sender
{	
	//printf("in Inspector windowDidUpdate\n");
	if([panel isVisible]) [self displayBox:currentBoxTag];
	return self;
}

- free
{	[[mainPopup target] free]; //a window
	[panel free];
	return [super free];
}



@end

