Source

BWToolkit-Copy / BWGradientBox.m

//
//  BWGradientBox.m
//  BWToolkit
//
//  Created by Brandon Walkin (www.brandonwalkin.com)
//  All code is provided under the New BSD license.
//

#import "BWGradientBox.h"
#import "NSColor+BWAdditions.h"

@implementation BWGradientBox

@synthesize fillStartingColor, fillEndingColor, fillColor, topBorderColor, bottomBorderColor;
@synthesize topInsetAlpha, bottomInsetAlpha;
@synthesize hasTopBorder, hasBottomBorder, hasGradient, hasFillColor;

- (id)initWithCoder:(NSCoder *)decoder
{
	if ((self = [super initWithCoder:decoder]) != nil)
	{
		[self setFillStartingColor:[decoder decodeObjectForKey:@"BWGBFillStartingColor"]];
		[self setFillEndingColor:[decoder decodeObjectForKey:@"BWGBFillEndingColor"]];
		[self setFillColor:[decoder decodeObjectForKey:@"BWGBFillColor"]];
		[self setTopBorderColor:[decoder decodeObjectForKey:@"BWGBTopBorderColor"]];
		[self setBottomBorderColor:[decoder decodeObjectForKey:@"BWGBBottomBorderColor"]];
		
		[self setHasTopBorder:[decoder decodeBoolForKey:@"BWGBHasTopBorder"]];
		[self setHasBottomBorder:[decoder decodeBoolForKey:@"BWGBHasBottomBorder"]];
		[self setHasGradient:[decoder decodeBoolForKey:@"BWGBHasGradient"]];
		[self setHasFillColor:[decoder decodeBoolForKey:@"BWGBHasFillColor"]];

		[self setTopInsetAlpha:[decoder decodeFloatForKey:@"BWGBTopInsetAlpha"]];
		[self setBottomInsetAlpha:[decoder decodeFloatForKey:@"BWGBBottomInsetAlpha"]];
		
		if (self.fillStartingColor == nil)
			self.fillStartingColor = [NSColor whiteColor];
		
		if (self.fillEndingColor == nil)
			self.fillEndingColor = [NSColor grayColor];
		
		if (self.fillColor == nil)
			self.fillColor = [NSColor grayColor];
		
		if (self.topBorderColor == nil)
			self.topBorderColor = [NSColor blackColor];
		
		if (self.bottomBorderColor == nil)
			self.bottomBorderColor = [NSColor blackColor];
	}
	return self;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
    [super encodeWithCoder:coder];
	
	[coder encodeObject:[self fillStartingColor] forKey:@"BWGBFillStartingColor"];
	[coder encodeObject:[self fillEndingColor] forKey:@"BWGBFillEndingColor"];
	[coder encodeObject:[self fillColor] forKey:@"BWGBFillColor"];
	[coder encodeObject:[self topBorderColor] forKey:@"BWGBTopBorderColor"];
	[coder encodeObject:[self bottomBorderColor] forKey:@"BWGBBottomBorderColor"];
	
	[coder encodeBool:[self hasTopBorder] forKey:@"BWGBHasTopBorder"];
	[coder encodeBool:[self hasBottomBorder] forKey:@"BWGBHasBottomBorder"];
	[coder encodeBool:[self hasGradient] forKey:@"BWGBHasGradient"];
	[coder encodeBool:[self hasFillColor] forKey:@"BWGBHasFillColor"];
	
	[coder encodeFloat:[self topInsetAlpha] forKey:@"BWGBTopInsetAlpha"];
	[coder encodeFloat:[self bottomInsetAlpha] forKey:@"BWGBBottomInsetAlpha"];
} 

- (void)drawRect:(NSRect)rect 
{
	if (hasGradient)
	{
		NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:fillStartingColor endingColor:fillEndingColor];
		[gradient drawInRect:self.bounds angle:90];
		[gradient release];
	}
	else
	{
		if (hasFillColor)
		{
			[fillColor set];
			NSRectFillUsingOperation(self.bounds, NSCompositeSourceOver);	
		}
	}
	
	if (hasTopBorder)
	{
		[topBorderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:self.bounds inView:self horizontal:YES flip:NO];
		[[[NSColor whiteColor] colorWithAlphaComponent:topInsetAlpha] bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:self.bounds inView:self horizontal:YES flip:NO];
	}
	else
	{
		[[[NSColor whiteColor] colorWithAlphaComponent:topInsetAlpha] bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:self.bounds inView:self horizontal:YES flip:NO];
	}
		
	
	if (hasBottomBorder)
	{
		[bottomBorderColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:self.bounds inView:self horizontal:YES flip:YES];
		[[[NSColor whiteColor] colorWithAlphaComponent:bottomInsetAlpha] bwDrawPixelThickLineAtPosition:1 withInset:0 inRect:self.bounds inView:self horizontal:YES flip:YES];
	}
	else
	{
		[[[NSColor whiteColor] colorWithAlphaComponent:bottomInsetAlpha] bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:self.bounds inView:self horizontal:YES flip:YES];
	}
		
}

- (BOOL)isFlipped
{
	return YES;
}

- (void)setFillColor:(NSColor *)color
{
	if (fillColor != color) 
	{
        [fillColor release];
        fillColor = [color retain];
		
		[self setNeedsDisplay:YES];
    }
}

- (void)setFillStartingColor:(NSColor *)color
{
	if (fillStartingColor != color) 
	{
        [fillStartingColor release];
        fillStartingColor = [color retain];
		
		[self setNeedsDisplay:YES];
    }
}

- (void)setFillEndingColor:(NSColor *)color
{
	if (fillEndingColor != color) 
	{
        [fillEndingColor release];
        fillEndingColor = [color retain];
		
		[self setNeedsDisplay:YES];
    }
}

- (void)setTopBorderColor:(NSColor *)color
{
	if (topBorderColor != color) 
	{
        [topBorderColor release];
        topBorderColor = [color retain];
		
		[self setNeedsDisplay:YES];
    }
}

- (void)setBottomBorderColor:(NSColor *)color
{
	if (bottomBorderColor != color) 
	{
        [bottomBorderColor release];
        bottomBorderColor = [color retain];
		
		[self setNeedsDisplay:YES];
    }
}

- (void)dealloc
{
	[fillColor release];
	[fillStartingColor release];
	[fillEndingColor release];
	[topBorderColor release];
	[bottomBorderColor release];
	
	[super dealloc];
}

@end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.