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


#define MAXSIZE 1.0e38	// Maximum size of a text object
#define TITLESIZE 20

@implementation ClientScroller

- initFrame:(const NXRect *)frameRect
{
	NXSize      textSize = {MAXSIZE, MAXSIZE};
	NXRect      aFrame = {0.0, 0.0, 0.0, 0.0};
	ClientText *newText;
	
	[super initFrame:frameRect];

	[self setHorizScrollerRequired:NO];
	[self setVertScrollerRequired:YES];
	[self setBorderType:NX_BEZEL];
	
	[self getContentSize:&aFrame.size];
	newText = [[ClientText alloc] initFrame:&aFrame];
	[newText setVertResizable:YES];
	[newText setHorizResizable:NO];
	[newText setGraphicsImportEnabled:YES];
	[newText setEditable:NO];
	[newText setMonoFont:NO];
	[newText setOpaque:YES];
	[newText notifyAncestorWhenFrameChanged:YES];
	[newText setAutosizing:NX_WIDTHSIZABLE];
	[newText setMinSize:&aFrame.size];
	[newText setMaxSize:&textSize];
	[newText setFontPanelEnabled:NO];
	[self setDocView:newText];
	[contentView setAutoresizeSubviews:YES];
	[scrollButton setTarget:newText];
	[scrollButton setAction:@selector(setAutoscrollFrom:)];
	[newText setAutoscrollFrom:scrollButton];
	[newText setDelegate:self];
	clientUName = NULL;
	
	return self;
}

- free
{
	if (clientUName)
		free(clientUName);
	if (topic)
		free(topic);
	return [super free];
}

- delayedFree:sender
{
	return [self free];
}

- freeScroller:sender
{
	if (NXRunAlertPanel(NXBLS("Close")
		, NXBLS("ReallyCloseConnect-1-User")
		, NXBLS("Close"), NXBLS("Don't close"), 0, [nameTextField stringValue])
		== NX_ALERTDEFAULT)
	{
		[self perform:@selector(freeDocument:) with:sender
			afterDelay:1 cancelPrevious:NO];
	}
	return self;
}


- freeDocument:sender
{
	if ([[window delegate] clientIsDying:[self docView]])
	{
		[self free];
		return nil;
	}
	return self;
}


- ringServerText:sender
{
	id    server;
	
	server = [(ClientText *)[self docView] serverText];
	if ([server respondsTo:@selector(ring:)])
		[server ring:self];
	return self;
}

- tile
{
	NXRect  scrollerRect;
	NXRect  buttonRect;
	
	[super tile];
	
	if (!nameTextField)
	{
		NXRect  rect = {24, 4, 200, 15};
		nameTextField = [[TextField alloc] initFrame:&rect];
		[nameTextField setStringValue:""];
		[nameTextField setBackgroundGray:NX_LTGRAY];
		[nameTextField setEditable:NO];
		[nameTextField setBordered:NO];
		[self addSubview:nameTextField];
	}
	
	if (!closeButton)
	{
		NXRect  rect = {4, 4, 15, 15};
		closeButton = [[Button alloc] initFrame:&rect];
		[closeButton setIcon:"NTKclose"];
		[closeButton setAltIcon:"NTKcloseH"];
		[closeButton setType:NX_MOMENTARYCHANGE];
		[closeButton setTarget:self];
		[closeButton setAction:@selector(freeScroller:)];
		[closeButton setBordered:NO];
		[self addSubview:closeButton];
	}
	
	if (!ringButton)
	{
		NXRect  rect = {4, 4, 15, 15};
		ringButton = [[Button alloc] initFrame:&rect];
		[ringButton setIcon:"NTKbell"];
		[ringButton setAltIcon:"NTKbellH"];
		[ringButton setType:NX_MOMENTARYCHANGE];
		[ringButton setTarget:self];
		[ringButton setAction:@selector(ringServerText:)];
		[ringButton setBordered:NO];
		[self addSubview:ringButton];
	}
	
	[[self vertScroller] getFrame:&scrollerRect];

	if (!scrollButton)
	{
		buttonRect.origin.x = 0;
		buttonRect.origin.y = 0;
		buttonRect.size.width = scrollerRect.size.width;
		buttonRect.size.height = scrollerRect.size.width;
		scrollButton = [[Button alloc] initFrame:&buttonRect];
		[scrollButton setIcon:"NTKscroll"];
		[scrollButton setAltIcon:"NTKscrollH"];
		[scrollButton setBordered:NO];
		[scrollButton setType:NX_TOGGLE];
		[scrollButton setState:1];
		[self addSubview:scrollButton];
	}
	
	NXDivideRect(&scrollerRect, &buttonRect, scrollerRect.size.width, NX_YMAX);
	scrollerRect.origin.y += TITLESIZE;
	scrollerRect.size.height -= TITLESIZE;
	[[self vertScroller] setFrame:&scrollerRect];
	NXInsetRect(&buttonRect, 1.0, 1.0);
	[scrollButton setFrame:&buttonRect];
	
	return self;
}

- drawSelf:(const NXRect *)rects :(int)nRects
{	
	[super drawSelf:rects :nRects];
	PSsetgray(NX_LTGRAY);
	PSmoveto(20, 2);
	PSlineto(20, TITLESIZE);
	PSstroke();
	PSsetgray(NX_DKGRAY);
	PSmoveto(2, TITLESIZE+1);
	PSlineto(bounds.size.width-4, TITLESIZE+1);
	PSstroke();

	return self;
}

- sizeTo:(NXCoord)width :(NXCoord)height
{
	NXSize     contentSize;
	[super sizeTo:width :height];
	[nameTextField sizeTo:width-68 :15];
	[ringButton moveTo:width-39 :4];
	[closeButton moveTo:width-19 :4];
	[self getContentSize:&contentSize];
	contentSize.height -= TITLESIZE;
	[[contentView sizeTo:contentSize.width :contentSize.height]
		moveBy:0 :TITLESIZE];
	[[self docView] setMinSize:&contentSize];
	[[self docView] sizeToFit];
	return self;
}


- setClientUName:(const char *)aClientUName
{
	if (clientUName)
		free(clientUName);
	clientUName = NULL;
	if (aClientUName)
	{
		clientUName = malloc(strlen(aClientUName)+1);
		strcpy(clientUName, aClientUName);
	}
	return self;
}

- (const char *)clientUName
{
	return clientUName;
}


- setConnectionCode:(const char *)aCode
{
	if (connectionCode)
		free(connectionCode);
	connectionCode = NULL;
	if (aCode)
	{
		connectionCode = malloc(strlen(aCode)+1);
		strcpy(connectionCode, aCode);
	}
	return self;
}

- (const char *)connectionCode
{
	return connectionCode;
}

- setTopic:(const char *)aTopic
{
	if (topic)
		free(topic);
	topic = NULL;
	if (aTopic)
	{
		topic = malloc(strlen(aTopic)+1);
		strcpy(topic, aTopic);
	}
	return self;
}

- (const char *)topic
{
	return topic;
}

- setUserName:(const char *)aUserName
{
	if (nameTextField)
		[nameTextField setStringValue:aUserName];
	return self;
}

@end
