#import "RankCell.h"

static NSImage *sRankPatternImage = nil;

@implementation RankCell

+ (void) initialize
{
	sRankPatternImage = [[NSImage imageNamed:@"stripe"] retain];
	// This should be a 2-pixel by 1-pixel image with the following colors:
	// For dark stripe:  r = g = b = 0, a = 136/255 = 0.533
	// For light stripe: r = g = b = 0, a = 183/255 = 0.718
	// (I didn't really get this value right, if somebody has a correct image,
	// feel free to send it to me...)
}

- (float) floatValue
{
	float result = 0.0;
	id objectValue = [self objectValue];
	if ([objectValue respondsToSelector:@selector(floatValue)])
	{
		result = [(NSNumber *)objectValue floatValue];
	}
	return result;
}

- (void) setFloatValue:(float)inValue
{
	float value = inValue;
	if (value > 1.0) value = 1.0;
	if (value < 0.0) value = 0.0;
	[self setObjectValue:[NSNumber numberWithFloat:value]];
}

/*"	Draw the cell's contents.
"*/
- (void) drawInteriorWithFrame: (NSRect)inFrame inView: (NSView*)inView;
{
	float drawWidth;
	NSRect fillFrame, eraseFrame;

	// Constrain the frame's height
	float yInset = (NSSmallControlSize == [self controlSize]) ? 4.0 : 3.0;
	NSRect newFrame = NSInsetRect(inFrame, 3.0, yInset);

	// Calculate width of filled part
	drawWidth = floor([self floatValue] * newFrame.size.width);
	if (drawWidth < 1)
	{
		drawWidth = 1;	//  at least 1 pixel wide, so we see something!
	}

	NSDivideRect(newFrame, &fillFrame, &eraseFrame, drawWidth, NSMinXEdge);

	[[NSColor colorWithPatternImage:sRankPatternImage] set];
	[NSBezierPath fillRect:fillFrame];
}



@end
