#import "BrowserBay.h"
#import "BrowserMatrix.h"
#import <objc/List.h>
#import <appkit/appkit.h>
#import <streams/streams.h>
#import <sys/types.h>
#import <sys/stat.h>
#import <sys/file.h>
#import <stdio.h>
#import <math.h>

#define FIELDHEIGHT 21.0
#define SCROLLERHEIGHT 18.0
#define INTERPANEL 2.0

extern char *strcpy(), *strcat();

@implementation BrowserBay

static id make_button (NXRect *rect, SEL action, id target)
{
	id button;
	
	button = [Button newFrame:rect
			 title:NULL
			 tag:0
			 target:target
			 action:action
			 key:0
			 enabled:YES];
	[button setOpaque:NO];
	[button setIconPosition:NX_ICONONLY];
	[button setType:NX_MOMENTARYPUSH];
	[button setContinuous:YES];
	[button setPeriodicDelay:0.4 andInterval:0.1];
	[button setEnabled:YES];
	return button;
}

+ newFrame:(NXRect *)rect
{
	NXRect r1;
	id box, boldFont, scrollView;
	int rows, i;
	float width;
	char fontname[64];
	
	self = [super newFrame:rect];
	[self setOpaque:YES];
	r1.size.width = rect->size.width;	/* make title header */
	r1.size.height = FIELDHEIGHT;
	r1.origin.x = 0.0;
	r1.origin.y = rect->size.height - r1.size.height;
	bayHeader = [TextField newFrame:&r1];
	[bayHeader setAlignment:NX_CENTERED];
	[bayHeader setBackgroundGray:NX_DKGRAY];
	[bayHeader setTextGray:NX_WHITE];
	[bayHeader setBezeled:YES];
	[bayHeader setSelectable:NO];
	strcpy (fontname, [[bayHeader font] name]);
	strcat (fontname, "-Bold");
	boldFont = [Font newFont:fontname size:[[bayHeader font] pointSize]];
	[bayHeader setFont:boldFont];
	[self addSubview:bayHeader];
	width = rect->size.width / 2.0;
	r1.size.width = floor (width); /* make scroll buttons */
	r1.size.height = SCROLLERHEIGHT;
	r1.origin.x = 0.0;
	r1.origin.y = 0.0;
	downButton = make_button (&r1, @selector(scrollDown:), self);
	[self addSubview:downButton];
	r1.origin.x += width;
	upButton = make_button (&r1, @selector(scrollUp:), self);
	[self addSubview:upButton];
	r1.size.width = rect->size.width;	/* make text box */
	r1.size.height = rect->size.height - 2.0 * INTERPANEL - FIELDHEIGHT -
		SCROLLERHEIGHT;
	r1.origin.x = 0.0;
	r1.origin.y = SCROLLERHEIGHT + INTERPANEL;
	box = [Box newFrame:&r1];
	[box setBorderType:NX_BEZEL];
	[box setOffsets:0.0 :0.0];
	[box setTitlePosition:NX_NOTITLE];
	[self addSubview:box];
	scrollView = [ScrollView new];
	[scrollView setBorderType:NX_NOBORDER];
	[scrollView setHorizScrollerRequired:NO];
	[scrollView setVertScrollerRequired:NO];
	[[box setContentView:scrollView] free];
	[scrollView getContentSize:&r1.size];
	r1.origin.x = r1.origin.y = 0.0;
	bayMatrix = [BrowserMatrix newFrame:&r1];
	[bayMatrix setAction:@selector(touchedEntry:)];
	[bayMatrix setTarget:self];
	[bayMatrix setBackgroundGray:NX_LTGRAY];
	[scrollView setDocView:bayMatrix];
	buttonsEnabled = YES;
	[self updateScrollButtons];
	nextBrowserBay = nil;
	return self;
}

- allowEmptySel:(BOOL)flag
{
	[bayMatrix allowEmptySel:flag];
	return self;
}

- _matrix
{
	return bayMatrix;
}

- addSlave:aBrowserBay
{
	[bayMatrix addSlave:[aBrowserBay _matrix]];
	return self;
}
- entryAt:(int)row
{
	return [bayMatrix cellAt:row :0];
}

- findEntryWithText:(const char *)text
{
	int i, count;
	id cells, aCell;
	
	count = [bayMatrix cellCount];
	cells = [bayMatrix cellList];
	for (i = 0; i < count; i++) {
		aCell = [cells objectAt:i];
		if (strcmp ([aCell stringValue], text) == 0) return aCell;
		}
	return nil;
}

- removeEntry:aCell
{
	[bayMatrix removeEntry:aCell];
	return self;
}

- (int)getRowOfEntry:entry
{
	int row, col;
	[bayMatrix getRow:&row andCol:&col ofCell:entry];
	return row;
}

- selectEntryAt:(int)row
{
	[bayMatrix selectEntryAt:row];
	return self;
}

- setButtonsEnabled:(BOOL)flag
{
	buttonsEnabled = flag;
	return self;
}

- updateScrollButtons
{
	BOOL upEnabled, downEnabled;
	
	if (!buttonsEnabled) return self;
	upEnabled = [upButton isEnabled];
	downEnabled = [downButton isEnabled];
	if (![bayMatrix atTop] && !upEnabled) {
		[upButton setEnabled:YES];
		[upButton setIcon:"scrollMenuUp" position:NX_ICONONLY];
		}
	if ([bayMatrix atTop] && upEnabled) {
		[upButton setEnabled:NO];
		[upButton setIcon:"scrollMenuUpD" position:NX_ICONONLY];
		}
	if (![bayMatrix atBottom] && !downEnabled) {
		[downButton setEnabled:YES];
		[downButton setIcon:"scrollMenuDown" position:NX_ICONONLY];
		}
	if ([bayMatrix atBottom] && downEnabled) {
		[downButton setEnabled:NO];
		[downButton setIcon:"scrollMenuDownD" position:NX_ICONONLY];
		}
	return self;
}

- scrollDown:sender
{
	[bayMatrix scrollDown:sender];
	[self updateScrollButtons];
	return self;
}

- scrollUp:sender
{
	[bayMatrix scrollUp:sender];
	[self updateScrollButtons];
	return self;
}

- setAlphabetize:(BOOL)flag
{
	[bayMatrix setAlphabetize:flag];
	return self;
}

- selectedCell
{
	return [bayMatrix selectedCell];
}

- (char *)title
{
	return (char *) [bayHeader stringValue];
}

- setTitle:(const char *)title
{
	[bayMatrix clear];
	[bayHeader setStringValue:title];
	[nextBrowserBay setTitle:NULL];
	if (title == NULL) [self display];
	return self;
}

- display
{
	[bayMatrix display];
	[super display];
	[self updateScrollButtons];
	return self;
}

- addEntry:(const char *)text leaf:(BOOL)yn
{
	[bayMatrix addEntry:text leaf:yn];
	return self;
}

- cellList
{
	return [bayMatrix cellList];
}

- (int)cellCount
{
	return [bayMatrix cellCount];
}

- listFiles:(const char *)dir suffix:(const char *)sfx
{
	[bayMatrix listFiles:dir suffix:sfx];
	return self;
}

- setAction:(SEL)selector
{
	action = selector;
	return self;
}

- setTarget:anObject
{
	target = anObject;
	return self;
}

- setNextBrowserBay:anObject
{
	nextBrowserBay = anObject;
	return self;
}

- (int)selectedRow
{
	return [bayMatrix selectedRow];
}

- touchedEntry:sender
{
	id aCell;
	
	aCell = [bayMatrix selectedCell];
	if (![aCell isLeaf])
		[nextBrowserBay setTitle:[aCell stringValue]];
	else [nextBrowserBay setTitle:NULL];
	[self sendAction:action to:target];
	return self;
}

@end

