/*
   Shelf.m
   
   Object that contain a window, shelf and content area

   Copyright (C) 2001 Free Software Foundation, Inc.
   
   Author:  Felipe A. Rodriguez <far@pcmagic.net>
   Date:	November 2001
   
   This file is part of the mySTEP Library and is provided
   under the terms of the GNU Library General Public License.
*/

#include <AppKit/AppKit.h>

#include "Matrix.h"
#include "Shelf.h"
#include "Cell.h"


@implementation SplitView

- (id) initWithFrame:(NSRect)frameRect
{	
	_textCell = [[NSTextFieldCell alloc] initTextCell: @""];
	[_textCell setAlignment:NSRightTextAlignment];
	[_textCell setFont:[NSFont userFontOfSize:8]];
	[_textCell setTextColor: [NSColor darkGrayColor]];
	[_textCell setBackgroundColor: [NSColor lightGrayColor]];
	[_textCell setBordered: NO];

	return [super initWithFrame:frameRect];
}

- (void) dealloc
{
	[_textCell release];

	[super dealloc];
}

- (void) setFileSize:(NSObject *)fsize
{
	[_textCell setObjectValue:fsize];
	[self lockFocus];
	[_textCell drawWithFrame:_lastTextRect inView:self];
	[self unlockFocus];
	[window flushWindow];
}

- (void) drawDividerInRect:(NSRect)aRect
{
	_lastTextRect = aRect;

	[super drawDividerInRect:aRect];

	NSMinX(_lastTextRect) = NSMidX(aRect) + 10;
	NSWidth(_lastTextRect) -= ((NSWidth(aRect)/2) + 20);
	[_textCell drawInteriorWithFrame:_lastTextRect inView:self];
	NSHeight(_lastTextRect) += 1;
}

@end /* SplitView */


@implementation Shelf

- (id) initWithFrame:(NSRect)winRect
				rows:(int)rows
				cols:(int)cols
				subView:(NSView *)subView
{
NSSplitView *splitView;
id shelfCell, cell;
NSRect shelfRect = {{0, 380}, { NSWidth(winRect) - 30, 170}};
NSRect cvFrame;

	[self initWithContentRect:winRect							// create main
		  styleMask:GSAllWindowMask								// window
		  backing:NSBackingStoreBuffered
		  defer:NO];

	shelfCell = [[[ShelfCell alloc] init] autorelease];
	[shelfCell setImage:nil];												  
	_shelf = [[Matrix alloc] initWithFrame:shelfRect			// create shelf
							 mode:NSHighlightModeMatrix			// matrix
							 prototype:shelfCell
							 numberOfRows:rows
							 numberOfColumns:cols];
	cell = [_shelf cellAtRow:0 column:0];
	[cell setBranchImage: [NSImage imageNamed: @"host.tiff"]];
	[cell setStringValue: [[NSProcessInfo processInfo] hostName]];
	[cell _setPath: @"/"];
																// configure 
	[_shelf setCellSize: (NSSize){CELL_WIDTH, CELL_HEIGHT}];	// shelf matrix
	[_shelf setIntercellSpacing: (NSSize){7,5}];	
	[_shelf sizeToCells];	
	[_shelf setDrawsBackground:YES];
	[_shelf setBackgroundColor:[NSColor lightGrayColor]];
	[_shelf setDelegate: subView];
	[_shelf setDoubleAction: @selector(doDoubleClick:)];
	[_shelf setTarget: [NSApp delegate]];

	cvFrame = [_contentView frame];								// configure 
	cvFrame.origin = (NSPoint){10,5};							// and create
	cvFrame.size.width -= (2 * cvFrame.origin.x);				// splitview    
	cvFrame.size.height -= (2 * cvFrame.origin.y);
	splitView = [[SplitView alloc] initWithFrame:cvFrame];
	[splitView addSubview: subView]; 
	[splitView addSubview: _shelf]; 
	[_contentView addSubview: splitView];

	[splitView setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)]; 
	[_shelf setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)]; 
	[subView setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)]; 
	_browser = subView;

	return self;
}

- (Matrix *) matrix								{ return _shelf; }
- (Browser *) browser							{ return _browser; }

@end /* Shelf */
