
#import "STNibOwner.h"

@implementation STNibOwner

- init
{
    [super init];
    window = nil;
    nibFileName = NULL;
    nibLoaded = NO;
    return self;
}

- setNibFile:(const char *)fileName
{
    if (fileName)
    {
	if (nibFileName == NULL)
	    free(nibFileName);

	nibFileName = (char *)malloc(sizeof(char)*(strlen(fileName) + 1));

	strcpy(nibFileName, fileName);
    }
    return self;
}

- showWindow: (const char *)windowName
{
    if (nibFileName && nibLoaded == NO)
    {
	if ([NXApp loadNibFile: nibFileName owner: self withNames: YES])
	{
		window = NXGetNamedObject(windowName, self);
	    [window makeKeyAndOrderFront: self];
	    nibLoaded = YES;
	}
    }
    return self;
}

- window
{
    return window;
}

- viewWithTag:(int)number
{
    return [[window contentView] findViewWithTag: number];
}

- viewWithName:(char *)name
{
    return NXGetNamedObject(name, self);
}

- awake
{
    [super awake];
    nibLoaded = NO;
    return self;
}

- read:(NXTypedStream *)stream
{
    [super read: stream];
    window = NXReadObject(stream);
    NXReadTypes(stream, "*i", nibFileName, &nibLoaded);
    return self;
}

- write:(NXTypedStream *)stream
{
    [super write: stream];
    NXWriteObject(stream, window);
    NXWriteTypes(stream, "*i", nibFileName, &nibLoaded);
    return self;
}

@end
