//
//	MiscProgressView.m -- a simple view class for displaying progress
//		Written originally by Don Yacktman Copyright (c) 1993 by James Heiser.
//				Modified from an example in the NeXT documentation.
//				This file is maintained by James Heiser, jheiser@adobe.com.
//				Version 1.0.  All rights reserved.
//
//		OPENSTEP conversion and mods:  Todd Anthony Nathan <todd@thoughtful.com>
//
//		This notice may not be removed from this source code.
//
//	This object is included in the MiscKit by permission from the author
//	and its use is governed by the MiscKit license, found in the file
//	"LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
//	for a list of all applicable permissions and restrictions.
//	

#import "MiscProgressView.h"

@implementation MiscProgressView

- initWithFrame:(NSRect)frameRect
{
	[super initWithFrame:frameRect];

	bg = [[NSColor lightGrayColor] retain];
	fg = [[NSColor darkGrayColor] retain];
	bd = [[NSColor blackColor] retain];

	total = MISC_PROGRESS_MAXSIZE;
	stepSize = MISC_PROGRESS_DEFAULTSTEPSIZE;

	count = 0;
	ratio = 0.0;

	return ( self );
}



- (void) dealloc
{
	[bg release];
	[fg release];
	[bd release];
	[super dealloc];

	return;
}


- (void) renderBackground
{
	[bg set];
	NSRectFill([self bounds]);

	return;
}


- (void) renderBar
{
	if ((ratio > 0) && (ratio <= 1.0)) {
		NSRect r = [self bounds];
		r.size.width = [self bounds].size.width * ratio;
		[fg set];
		NSRectFill(r);
	}

	return;
}

- (void) renderBorder
{
	[bd set];
	NSFrameRect([self bounds]);

	return;
}


// Note from Don on why this -drawSelf: is split up --
// The three separate rendering methods make things a little less efficient,
// but make it a lot easier to subclass and retain parts of the original
// rendering.  I wish NeXT had done something like this with the various
// control classes so that subclasses could override parts of the steps
// in the rendering process...or insert things at certain points in time.
- (void) drawRect:(NSRect)rects
{
	[self renderBackground];
	[self renderBar];
	[self renderBorder];

	return;
}


- (void) setStepSize:(int)value
{
	stepSize = value;
	return;
}


- (int) stepSize
{
	return ( stepSize );
}


- (NSColor *) backgroundColor
{
	return ( bg );
}


- (NSColor *) foregroundColor
{
	return ( fg );
}


- (NSColor *) borderColor
{
	return ( bd );
}


- (void) setBackgroundColor:(NSColor *)color
{
	if ( bg != color ) {
		id	tmp;
		tmp = bg;
		bg = [color retain];
		[tmp release];
	}
}


- (void) setForegroundColor:(NSColor *)color
{
	if ( fg != color ) {
		id	tmp;
		tmp = fg;
		fg = [color retain];
		[tmp release];
	}

	return;
}


- (void) setBorderColor:(NSColor *)color
{
	if ( bd != color ) {
		id	tmp;
		tmp = bd;
		bd = [color retain];
		[tmp release];
	}

	return;
}


- (void) setRatio:(float)newRatio
{
	if (newRatio > 1.0) newRatio = 1.0;
	if (ratio != newRatio) {
		ratio = newRatio;
		[self display];
	}

	return;
}


- (void) takeIntValueFrom:sender
{
	int temp;

	temp = [sender intValue];
	if ((temp < 0) || (temp > total)) return;

	count = temp;
	[self setRatio:(float)count/(float)total];

	return;
}


- (void) increment:sender
{
	count += stepSize;
	[self setRatio:(float)count/(float)total]; 

	return;
}


- (void) takeFloatValueFrom:(id)sender
{
	[self setRatio:[sender floatValue]];
	count = ceil(ratio * (float)total);

	return;
}


- (NSString *) inspectorClassName
{
	return @"MiscProgressViewInspector";
}


- (void) clear:(id)sender
{
	count = 0;
	[self setRatio:0];

	return;
}


- (id) initWithCoder:(NSCoder *)aDecoder
{
	[super initWithCoder:aDecoder];

	[aDecoder decodeValuesOfObjCTypes:"i", &total];
	[aDecoder decodeValuesOfObjCTypes:"i", &stepSize];
	bg = [[aDecoder decodeObject] retain];
	fg = [[aDecoder decodeObject] retain];
	bd = [[aDecoder decodeObject] retain];

	return self;
}


- (void) encodeWithCoder:(NSCoder *)aCoder
{
	[super encodeWithCoder:aCoder];

	[aCoder encodeValuesOfObjCTypes:"i", &total];
	[aCoder encodeValuesOfObjCTypes:"i", &stepSize];
	[aCoder encodeObject:bg];
	[aCoder encodeObject:fg];
	[aCoder encodeObject:bd];

	return;
}

@end
