BWToolkit / BWTransparentPopUpButtonCell.m

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

#import "BWTransparentPopUpButtonCell.h"
#import "NSImage+BWAdditions.h"

static NSImage *popUpFillN, *popUpFillP, *popUpRightN, *popUpRightP, *popUpLeftN, *popUpLeftP, *pullDownRightN, *pullDownRightP;
static NSColor *disabledColor, *enabledColor;

@interface NSCell (BWTPUBCPrivate)
- (NSDictionary *)_textAttributes;
@end

@interface BWTransparentPopUpButtonCell (BWTPUBCPrivate)
- (NSColor *)interiorColor;
@end

@implementation BWTransparentPopUpButtonCell

+ (void)initialize;
{
	NSBundle *bundle = [NSBundle bundleForClass:[BWTransparentPopUpButtonCell class]];
	
	popUpFillN = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TransparentPopUpFillN.tiff"]];
	popUpFillP = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TransparentPopUpFillP.tiff"]];
	popUpRightN = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TransparentPopUpRightN.tiff"]];
	popUpRightP = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TransparentPopUpRightP.tiff"]];
	popUpLeftN = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TransparentPopUpLeftN.tiff"]];
	popUpLeftP = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TransparentPopUpLeftP.tiff"]];
	pullDownRightN = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TransparentPopUpPullDownRightN.tif"]];
	pullDownRightP = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TransparentPopUpPullDownRightP.tif"]];
	
	enabledColor = [[NSColor whiteColor] retain];
	disabledColor = [[NSColor colorWithCalibratedWhite:0.6 alpha:1] retain];
}

- (void)drawBezelWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
	cellFrame.size.height = popUpFillN.size.height;
	
	if ([self isHighlighted])
	{
		if ([self pullsDown])
			NSDrawThreePartImage(cellFrame, popUpLeftP, popUpFillP, pullDownRightP, NO, NSCompositeSourceOver, 1, YES);
		else
			NSDrawThreePartImage(cellFrame, popUpLeftP, popUpFillP, popUpRightP, NO, NSCompositeSourceOver, 1, YES);
	}
	else
	{
		if ([self pullsDown])
			NSDrawThreePartImage(cellFrame, popUpLeftN, popUpFillN, pullDownRightN, NO, NSCompositeSourceOver, 1, YES);
		else
			NSDrawThreePartImage(cellFrame, popUpLeftN, popUpFillN, popUpRightN, NO, NSCompositeSourceOver, 1, YES);
	}
}

- (void)drawImageWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{	
	NSImage *image = [self image];
	
	if (image != nil)
	{
		[image setScalesWhenResized:NO];

		if ([[image name] isEqualToString:@"NSActionTemplate"])
			[image setSize:NSMakeSize(10,10)];
		
		NSImage *newImage = image;
		
		if ([image isTemplate])
			newImage = [image bwTintedImageWithColor:[self interiorColor]];

		NSAffineTransform* transform = [NSAffineTransform transform];
		[transform translateXBy:0.0 yBy:cellFrame.size.height];
		[transform scaleXBy:1.0 yBy:-1.0];
		[transform concat];
		
		[newImage drawInRect:[self imageRectForBounds:cellFrame] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
		
		[transform invert];
		[transform concat];
	}	
}

- (NSRect)imageRectForBounds:(NSRect)bounds;
{
	NSRect rect = [super imageRectForBounds:bounds];
	
	rect.origin.y += 3;
	
	if ([self imagePosition] == NSImageOnly || [self imagePosition] == NSImageOverlaps || [self imagePosition] == NSImageAbove || [self imagePosition] == NSImageBelow)
	{
		rect.origin.x += 4;
	}
	else if ([self imagePosition] == NSImageRight)
	{
		rect.origin.x += 3;
	}
	else if ([self imagePosition] == NSImageLeft || [self imagePosition] == NSNoImage)
	{
		rect.origin.x -= 1;
	}
	
	return rect;
}

- (NSRect)titleRectForBounds:(NSRect)cellFrame
{
	NSRect titleRect = [super titleRectForBounds:cellFrame];
	
	titleRect.origin.y -= 1;
	titleRect.origin.x -= 2;
	titleRect.size.width += 6;
	
	if ([self image] != nil)
	{
		if ([self imagePosition] == NSImageOnly || [self imagePosition] == NSImageOverlaps || [self imagePosition] == NSImageAbove || [self imagePosition] == NSImageBelow)
		{
			
		}
		else if ([self imagePosition] == NSImageRight)
		{
			if ([self alignment] == NSRightTextAlignment)
				titleRect.origin.x -= 3;
		}
		else if ([self imagePosition] == NSImageLeft || [self imagePosition] == NSNoImage)
		{
			titleRect.origin.x += 2;
		}
	}
		
	return titleRect;
}

- (NSDictionary *)_textAttributes
{
	NSMutableDictionary *attributes = [[[NSMutableDictionary alloc] init] autorelease];
	[attributes addEntriesFromDictionary:[super _textAttributes]];
	[attributes setObject:[NSFont systemFontOfSize:11] forKey:NSFontAttributeName];
	[attributes setObject:[self interiorColor] forKey:NSForegroundColorAttributeName];
	
	return attributes;
}

- (NSColor *)interiorColor
{
	return [self isEnabled] ? enabledColor : disabledColor;
}

- (NSControlSize)controlSize
{
	return NSSmallControlSize;
}

- (void)setControlSize:(NSControlSize)size
{
	
}

@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.