#import "RTFDChat.h"
#import "ServerText.h"
#import "ClientText.h"
#import "ClientScroller.h"
#import <appkit/appkit.h>
#import <sys/param.h>


#define XOFFSET 10.0    // Offset of subsequent windows
#define YOFFSET -20.0
#define MAXSIZE 1.0e38  // Maximum size of a text object


@implementation RTFDChat

+ newFromNib
{
	RTFDChat   *instance = [[RTFDChat alloc] init];
	NXBundle   *myBundle = [NXBundle bundleForClass:[self class]];
	char        nibPath[MAXPATHLEN+1];

	[myBundle getPath:nibPath forResource:"RTFDChat" ofType:"nib"];
	if ([NXApp loadNibFile:nibPath owner:instance withNames:NO])
	{
		return instance;
	}
	[instance free];
	return nil;
}

- setup
{
	NXSize  textSize = {MAXSIZE, MAXSIZE};
	Window *talkWin = [splitView window];
	NXRect  winFrame, winLoc;

	[[document superview] setAutoresizeSubviews:YES];
	[(ServerText *)document setVertResizable:YES];
	[(ServerText *)document setHorizResizable:NO];
	[(ServerText *)document setGraphicsImportEnabled:YES];
	[(ServerText *)document setMonoFont:NO];
	[(ServerText *)document setOpaque:YES];
	[(ServerText *)document setAutosizing:NX_WIDTHSIZABLE];
	[(ServerText *)document setMaxSize:&textSize];

	[splitView setDelegate:self];
	[splitView addSubview:scrollText :NX_BELOW relativeTo:nil];
	if ([NXApp mainWindow])
	{
		[[NXApp mainWindow] getFrame:&winFrame];
		[[talkWin class] getContentRect:&winLoc
			forFrameRect:&winFrame style:[talkWin style]];
		[talkWin moveTo:NX_X(&winLoc) + XOFFSET :NX_Y(&winLoc) + YOFFSET];
	}
	[talkWin makeKeyAndOrderFront:self];
	[talkWin setMiniwindowIcon:"NTKwindow"];

	return self;
}


- (BOOL)clientIsDying:sender
{	
	[[[sender superview] superview] removeFromSuperview];
	[splitView display];
	[self removeClientUName:[[[sender superview] superview] clientUName]
		forCode:[[[sender superview] superview] connectionCode]];
	[[document window] setDocEdited:NO];
	return YES;
}

- (View *)createClientViewWith:(View *)aDocument
{
	NXRect          frame;
	ClientScroller *newScroller;
	
	if (![aDocument respondsTo:@selector(addClientText:)])
		return nil;
	[splitView getFrame:&frame];
	newScroller = [[ClientScroller alloc] initFrame:&frame];
	[(ServerText *)aDocument addClientText:[newScroller docView]];
//	[[newScroller docView] sizeToFit];
	
	return newScroller;
}


- (View *)addClientViewWith:(View *)aDocument
		from:(const char *)aClientUName
		forCode:(const char *)aCode
{
	ClientScroller *newScroller;
	char           *key = [self keyFor:aClientUName:aCode];
	
	newScroller = (ClientScroller *)[self createClientViewWith:aDocument];
	[newScroller setClientUName:aClientUName];
	[splitView addSubview:newScroller :NX_BELOW relativeTo:nil];
	[splitView display];
	strcpy(connectionUName, aClientUName);
	[[splitView window] setTitle:topic];
	[[splitView window] setDocEdited:YES];
	[newScroller setConnectionCode:aCode];
	[clientTable insertKey:key value:newScroller];
//	free(key);   we should really do that! Leaks will appear.
	
	return newScroller;
}

@end

@implementation RTFDChat(NXSplitViewDelegate)

- splitView:sender
		getMinY:(NXCoord *)minY maxY:(NXCoord *)maxY
		ofSubviewAt:(int)offset
{
	*minY += 80.0;
	*maxY -= 80.0;
	return self;
}

@end
