/* NewInspector.m	by Imran Qureshi	November 1991
 *
 * Copyright (C)  1991  The Board of Trustees of
 * The Leland Stanford Junior University.  All Rights Reserved.
 */

#import "NewInspector.h"

const char NewInspector_h_rcsid[] = NEWINSPECTOR_H_ID;
const char NewInspector_m_rcsid[] = "$Id: NewInspector.m,v 2.9.2.1 1993/10/25 18:14:10 pfkeb Exp $";

#import <appkit/appkit.h>
#import "InspectBase.h"

@interface NewInspector(Private)
- showPanel:(int)insp;
 /*
  * put up the requested panel.
  */
@end

@implementation NewInspector

static id theInspector = nil;

/* makes sure there is only one instantiation of this object */
+ new
{
	if (!theInspector) {
		theInspector = self = [super new];
		[self init];
	} else
		self = theInspector;
		
    return self;
}

-init
{
    id                  protoCell, matrix;
    NXBundle           *bundle;
    char                buffer[MAXPATHLEN + 1];

    bundle = [NXBundle bundleForClass:[self class]];
    if ([bundle getPath:buffer forResource:"NewInspector" ofType:"nib"]) {
	[NXApp loadNibFile:buffer owner:self
	 withNames:NO fromZone:[self zone]];
    }
    ViewsList = [[List allocFromZone:[self zone]] init];
    SupervisorList = [[List allocFromZone:[self zone]] init];


    thePopUpList = [thePopUpListButton target];
    [thePopUpList setTarget:self];
    [thePopUpList setAction:@selector(toggleInspectorPanels:)];
    [thePopUpList removeItemAt:0];
 /*
  * IB comes up with atleast one item in the popupList. We do this to get rid
  * of that. 
  */

    matrix = [thePopUpList itemList];
    protoCell = [matrix prototype];
    [protoCell setIcon:NULL];
    curInsp = 0;
//    [InspectorPanel setBecomeKeyOnlyIfNeeded:YES];
    return self;
}

-inspectorPanel
{
	return InspectorPanel;
}

-free
{
	[ViewsList free];
	[SupervisorList free];
	//[InspectorPanel orderOut:self];
	return [super free];
}
- orderFrontPanel:sender
{
    [InspectorPanel setFloatingPanel:YES];
    [InspectorPanel orderFront:self];
    return self;
}
- orderBackPanel:sender
{
	[InspectorPanel orderBack:self];
    return self;
}

-setTitle:(const char *)theTitle
{
	[InspectorPanel setTitle:theTitle];
	return self;
}
-addView:(id)aView withName:(char *)name withSupervisor:(id)aSupervisor
{
	[ViewsList addObject:aView];
	[SupervisorList addObject:aSupervisor];
	
	[thePopUpList addItem:name];
        curInsp = [thePopUpList indexOfItem:name];
	[InspectorBox setContentView:aView];
	[InspectorBox display];
	
	[thePopUpListButton setTitle:name];
	return self;
}

-remove:(char *)name
{
	int	i;
	
	i = [thePopUpList indexOfItem:name];
	[thePopUpList removeItemAt:i];
	[ViewsList removeObjectAt:i];
	return self;
}

-replace:(char *)name with:(char *)someName andView:(id)aView
{
	int	i;
	
	i = [thePopUpList indexOfItem:name];
	
	/* if new name is desired, change the name */
	if (someName) {
		[thePopUpList removeItemAt:i];
		[ thePopUpList insertItem: someName at:i];
	}
	[ViewsList replaceObjectAt:i with:aView];
	return self;
}
-showPanelWithName:(char *)name
{
    curInsp = [thePopUpList indexOfItem:name];
    [thePopUpListButton setTitle:name];
    [self showPanel:curInsp];
    return self;
}

-updateDisplay
{
	[InspectorBox display];
	return self;
}
- windowDidUpdate:sender
{
     id inspector = [self currentInspector];
     [inspector windowDidUpdate:sender];
     lastSender = sender;
     
     return self;
}
-toggleInspectorPanels:sender
{
        lastSender = sender;
	curInsp = [sender selectedRow];
	[self showPanel:curInsp];
	return self;
}

- currentInspector
{
     return [SupervisorList objectAt:curInsp];
}

@end

/* Private Methods */

@implementation NewInspector(Private)

-showPanel:(int)insp
{
     InspectBase* inspector;
	
     inspector = [SupervisorList objectAt:insp];
     if ([inspector respondsTo:@selector(comingForward)]) 
     		[inspector comingForward];
     [inspector windowDidUpdate:lastSender];
     [InspectorBox setContentView:[ViewsList objectAt:insp]];
     [InspectorBox display];
     return self;
 }     
      
