//
//	IBMole.h -- a way to play with IB via disributed objects
//		Written by Montgomery Zukowski
//		Copyright (c) 1994 by Montgomery Zukowski.
//				Version 1.0.  All rights reserved.
//
//		This notice may not be removed from this source code.
//
//	This object is included in the MiscKit by permission from the author
//	and its use is governed by the MiscKit license, found in the file
//	"LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
//	for a list of all applicable permissions and restrictions.
//	

#import "IBMole.h"

@interface Object(Superfy)
-becomeSuperclass;
@end 

@implementation Object(Superfy)
-becomeSuperclass
{	//this was helpful in turning SmartFields into TextFields..
	//in general isa hacking is frowned upon...
	//changing yourself to a subclass can really screw things up!
	self->isa = [self superclass];
	return self;
}
@end

@interface WindowTemplate:Object
{
    struct _NXRect windowRect;
    int windowStyle;
    int windowBacking;
    int windowButtonMask;
    int windowEventMask;
    STR windowTitle;
    STR viewClass;
    STR windowClass;
    id windowView;
    id realObject;
    struct _windowFlags
    {
	unsigned hideOnDeactivate:1;
	unsigned dontFreeWhenClosed:1;
	unsigned defer:1;
	unsigned oneShot:1;
	unsigned visible:1;
	unsigned wantsToBeColor:1;
	unsigned dynamicDepthLimit:1;
	unsigned autoPositionMask:6;
	unsigned savePosition:1;
	unsigned _PADDING:2;
    }  windowFlags;
    id extension;
    struct _NXSize minSize;
}

+ initialize;
- read:(void *)fp16;
- write:(void *)fp16;
- awake;
- nibInstantiate;
- free;

@end

@interface WindowTemplate(WindowFlags)

- setFreeWhenClosed:(BOOL)yesNo;
- setHideOnDeactivate:(BOOL)yesNo;
- setVisibleAtLaunchTime:(BOOL)yesNo;
- setDeferred:(BOOL)yesNo;
- setOneShot:(BOOL)yesNo;
- setDynamicDepthLimit:(BOOL)yesNo;
- setWantsToBeColor:(BOOL)yesNo;

@end

@implementation WindowTemplate(WindowFlags)

- setFreeWhenClosed:(BOOL)yesNo;
{
	windowFlags.dontFreeWhenClosed = (unsigned)(!yesNo);
	return self;
}

- setHideOnDeactivate:(BOOL)yesNo;
{
	windowFlags.hideOnDeactivate = (unsigned)yesNo;
	return self;
}

- setVisibleAtLaunchTime:(BOOL)yesNo;
{
	windowFlags.visible = (unsigned)yesNo;
	return self;
}

- setDeferred:(BOOL)yesNo;
{
	windowFlags.defer = (unsigned)yesNo;
	return self;
}

- setOneShot:(BOOL)yesNo;
{
	windowFlags.oneShot = (unsigned)yesNo;
	return self;
}

- setDynamicDepthLimit:(BOOL)yesNo;
{
	windowFlags.dynamicDepthLimit = (unsigned)yesNo;
	return self;
}

- setWantsToBeColor:(BOOL)yesNo;
{
	windowFlags.wantsToBeColor = (unsigned)yesNo;
	return self;
}

@end

@implementation  IBMole

-init
{
	tunnel = [NXConnection registerRoot:self withName:"IBMole"];
	[tunnel runFromAppKit];
	return self;
}

- free
{
	if (docList != nil)
	{
		docList = [docList free];
	}
	return [super free];
}

-ibApp
{
	return NXApp;
}

-docList
{
	id windowList;
	id delegate;
	int z;
	
	if (docList == nil)
	{
		docList = [[List alloc] init];
	}
	else
	{
		[docList empty];
	}
	windowList = [NXApp windowList];
	z = [windowList count];
	while (z--)
	{
		delegate = [[windowList objectAt:z] delegate];
		if ([delegate respondsTo:@selector(document)])
		{
			[docList addObjectIfAbsent:[delegate document]];
		}
	}
	return docList;
}

- getClass:(const char *)aClassName
{
	return objc_getClass(aClassName);
}
	

@end
