#import "InspectMeDoc.h"
#import "InspectMeApp.h"
#import "InspectorManager.h"
#import "ThingInspector.h"
#import "ColorInspector.h"
#import <appkit/Window.h>		// for disableFlushWindow
#import "Thing1.h"
#import "Tank.h"


@implementation InspectMeDoc

static int tileCountMain = 0;	// just one per class 

- init
{
	NXRect	tempFrame;
	char	buffer[30];

	[super init];
	[NXApp loadNibSection:"InspectMeDoc.nib" owner:self];
	[window getFrame:&tempFrame];
	tempFrame.origin.x += ((tileCountMain%8)*23);
	tempFrame.origin.y -= ((tileCountMain%8)*23);
	if (tempFrame.origin.y < 0) 
		tempFrame.origin.y = 0;
	tileCountMain++;
	[window moveTo:tempFrame.origin.x :tempFrame.origin.y];
	sprintf(buffer,"Window # %d", tileCountMain);
	[window setTitle:buffer];
	[window makeKeyAndOrderFront:self];
	return self;
}

- windowDidBecomeKey:sender
{
	// while this window is main, receive messages from inspector
	[[NXApp inspectorManager] setDelegate:self];
	[self groupChanged:self to:[[NXApp inspectorManager] group]];
	return self;
}

- groupChanged:sender to:(int)newGroup 
{
	id	im;
	BOOL	inspectorShown=NO;
	im=[NXApp inspectorManager];
	[[ColorInspector new] setCurrentParameterReceiver:selectedObject];
	[[ThingInspector new] setCurrentParameterReceiver:selectedObject];
	if (newGroup == ATTRIBUTESGROUP) {
		[[im panel] disableFlushWindow];
		if([selectedObject isKindOf:[Thing1 class]]) {
				[im switchToInspector:
						[[ThingInspector new] thing1InspectorNum]];
				[[ThingInspector new] reflectThing1Attributes:selectedObject];
				inspectorShown=YES;
			}
		if([selectedObject isKindOf:[Tank class]]) {
				[im switchToInspector:
						[[ThingInspector new] tankInspectorNum]];
				[[ThingInspector new] reflectTankAttributes:selectedObject];
				inspectorShown=YES;
			}
		if(!inspectorShown)
			[im showMessage:"No Selection"];
		[[[im panel] reenableFlushWindow] flushWindowIfNeeded];
		}
	else 
		if([selectedObject isKindOf:[Thing1 class]]) {
			[im switchToInspector:
					[[ColorInspector new] bkgndColorInspectorNum]];
			[[ColorInspector new] reflectbkgndColorAttributes:selectedObject];
			inspectorShown=YES;
			}
		else
			[im showMessage:"Not\nApplicable"];
	return self;
}

- setSelectedObject:theObject;
{
	if (selectedObject != theObject) {
		[selectedObject setSelected:NO];
		[theObject setSelected:YES];
		selectedObject = theObject;
		[self groupChanged:self to: [[NXApp inspectorManager] group]];
		}
	return self;
}

@end
