Commits

Anonymous committed 543f3ba

Rewrote Anchored Pop Up Button

Comments (0)

Files changed (1)

BWAnchoredPopUpButtonCell.m

 #import "NSColor+BWAdditions.h"
 #import "NSImage+BWAdditions.h"
 
-#define IMAGE_INSET 8;
-#define ARROW_INSET 11;
-
 static NSColor *fillStop1, *fillStop2, *fillStop3, *fillStop4;
 static NSColor *topBorderColor, *bottomBorderColor, *sideBorderColor, *sideInsetColor, *pressedColor;
-static NSColor *textColor, *textShadowColor, *imageColor, *imageShadowColor;
+static NSColor *enabledTextColor, *disabledTextColor, *enabledImageColor, *disabledImageColor;
 static NSColor *borderedSideBorderColor, *borderedTopBorderColor;
 static NSGradient *fillGradient;
 static NSImage *pullDownArrow;
-static float scaleFactor = 1.0f;
+static NSShadow *contentShadow;
+static float arrowInset = 11.0;
+
+@interface NSCell (BWAPUBCPrivate)
+- (NSDictionary *)_textAttributes;
+@end
 
 @interface BWAnchoredPopUpButtonCell (BWAPUBCPrivate)
-- (void)drawTitleInFrame:(NSRect)cellFrame;
-- (void)drawImageInFrame:(NSRect)cellFrame;
+- (NSColor *)textColor;
+- (NSColor *)imageColor;
+- (NSRect)highlightRectForBounds:(NSRect)cellFrame;
 - (void)drawArrowInFrame:(NSRect)cellFrame;
 @end
 
 	
 	pressedColor			= [[NSColor colorWithCalibratedWhite:(0.0f / 255.0f) alpha:0.35] retain];
 	
-	textColor				= [[NSColor colorWithCalibratedWhite:(10.0f / 255.0f) alpha:1] retain];
-	textShadowColor			= [[NSColor colorWithCalibratedWhite:(255.0f / 255.0f) alpha:0.75] retain];
+	enabledTextColor	= [[NSColor colorWithCalibratedWhite:(10.0f / 255.0f) alpha:1] retain];
+	disabledTextColor	= [[enabledTextColor colorWithAlphaComponent:0.6] retain];
 	
-	imageColor				= [[NSColor colorWithCalibratedWhite:(70.0f / 255.0f) alpha:1] retain];
-	imageShadowColor		= [[NSColor colorWithCalibratedWhite:(240.0f / 255.0f) alpha:1] retain];
+	enabledImageColor	= [[NSColor colorWithCalibratedWhite:(72.0f / 255.0f) alpha:1] retain];
+	disabledImageColor	= [[enabledImageColor colorWithAlphaComponent:0.6] retain];
 	
 	borderedSideBorderColor	= [[NSColor colorWithCalibratedWhite:(0.0f / 255.0f) alpha:0.25] retain];
 	borderedTopBorderColor	= [[NSColor colorWithCalibratedWhite:(190.0f / 255.0f) alpha:1] retain];
 	
-	if([BWAnchoredPopUpButtonCell class] == [self class])
+	contentShadow = [[NSShadow alloc] init];
+	[contentShadow setShadowOffset:NSMakeSize(0,-1)];
+	[contentShadow setShadowColor:[NSColor colorWithCalibratedWhite:(255.0f / 255.0f) alpha:0.75]];
+
+	NSBundle *bundle = [NSBundle bundleForClass:[BWAnchoredPopUpButtonCell class]];		
+	pullDownArrow = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"ButtonBarPullDownArrow.pdf"]];
+}
+
+- (NSControlSize)controlSize
+{
+	return NSSmallControlSize;
+}
+
+- (void)setControlSize:(NSControlSize)size
+{
+	
+}
+
+#pragma mark Draw Bezel & Arrows
+
+- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
+{
+	[super drawWithFrame:cellFrame inView:controlView];
+
+	if ([self isHighlighted])
 	{
-		NSBundle *bundle = [NSBundle bundleForClass:[BWAnchoredPopUpButtonCell class]];
-		
-		pullDownArrow = [[[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"ButtonBarPullDownArrow.pdf"]] retain];
+		[pressedColor set];
+		NSRectFillUsingOperation([self highlightRectForBounds:cellFrame], NSCompositeSourceOver);
 	}
 }
 
-- (void)awakeFromNib
+- (NSRect)highlightRectForBounds:(NSRect)bounds
 {
-	scaleFactor = [[NSScreen mainScreen] userSpaceScaleFactor];
+	return bounds;
 }
 
-- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
-{		
+- (void)drawBorderAndBackgroundWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
+{
 	BOOL inBorderedBar = YES;
 	
 	if ([[[self controlView] superview] respondsToSelector:@selector(isAtBottom)])
 	{
 		if ([(BWAnchoredButtonBar *)[[self controlView] superview] isAtBottom])
-			inBorderedBar = NO;
+			inBorderedBar = NO;			
 	}
 	
 	[fillGradient drawInRect:cellFrame angle:90];
 		[sideBorderColor bwDrawPixelThickLineAtPosition:0 withInset:1 inRect:cellFrame inView:[self controlView] horizontal:NO flip:NO];
 		[sideBorderColor bwDrawPixelThickLineAtPosition:0 withInset:1 inRect:cellFrame inView:[self controlView] horizontal:NO flip:YES];
 	}
-	
+
 	if (inBorderedBar && [[self controlView] respondsToSelector:@selector(isAtLeftEdgeOfBar)])
 	{
 		if ([(BWAnchoredPopUpButton *)[self controlView] isAtLeftEdgeOfBar])
 			[bottomBorderColor bwDrawPixelThickLineAtPosition:0 withInset:1 inRect:cellFrame inView:[self controlView] horizontal:NO flip:YES];
 	}
 	
-	if ([self image] == nil)
-		[self drawTitleInFrame:cellFrame];
-	else
-		[self drawImageInFrame:cellFrame];
-	
 	[self drawArrowInFrame:cellFrame];
-	
-	if ([self isHighlighted])
-	{
-		[pressedColor set];
-		NSRectFillUsingOperation(cellFrame, NSCompositeSourceOver);
-	}
 }
 
-- (void)drawTitleInFrame:(NSRect)cellFrame
+- (void)drawArrowInFrame:(NSRect)cellFrame
 {
-	if (![[self title] isEqualToString:@""])
+	NSPoint drawPoint;
+	drawPoint.x = NSMaxX(cellFrame) - arrowInset;
+	
+	NSImage *arrow = [pullDownArrow bwTintedImageWithColor:[self imageColor]];
+	
+	NSAffineTransform* transform = [NSAffineTransform transform];
+	[transform translateXBy:0.0 yBy:cellFrame.size.height];
+	[transform scaleXBy:1.0 yBy:-1.0];
+	[transform concat];
+	
+	[contentShadow set];
+	
+	if ([self pullsDown])
 	{
-		NSColor *localTextColor = textColor;
+		// Draw pull-down arrow
+		drawPoint.y = roundf(cellFrame.size.height / 2) - 2;
+		[arrow drawAtPoint:drawPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
+	}
+	else
+	{
+		// Draw bottom pop-up arrow
+		drawPoint.y = roundf(cellFrame.size.height / 2) - 4;
+		[arrow drawAtPoint:drawPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
 		
-		if (![self isEnabled])
-		{
-			localTextColor = [textColor colorWithAlphaComponent:0.6];
-		}
+		// Draw top pop-up arrow
+		drawPoint.y -= 1;
 		
-		NSMutableDictionary *attributes = [[[NSMutableDictionary alloc] init] autorelease];
-		[attributes addEntriesFromDictionary:[[self attributedTitle] attributesAtIndex:0 effectiveRange:NULL]];
-		[attributes setObject:localTextColor forKey:NSForegroundColorAttributeName];
-		[attributes setObject:[NSFont systemFontOfSize:11] forKey:NSFontAttributeName];
+		NSAffineTransform* transform2 = [NSAffineTransform transform];
+		[transform2 translateXBy:0.0 yBy:cellFrame.size.height];
+		[transform2 scaleXBy:1.0 yBy:-1.0];
+		[transform2 concat];
 		
-		NSShadow *shadow = [[[NSShadow alloc] init] autorelease];
-		[shadow setShadowOffset:NSMakeSize(0,-1)];
-		[shadow setShadowColor:textShadowColor];
-		[attributes setObject:shadow forKey:NSShadowAttributeName];
+		[arrow drawAtPoint:drawPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
 		
-		NSMutableAttributedString *string = [[[NSMutableAttributedString alloc] initWithString:[self title] attributes:attributes] autorelease];
-		
-		// Draw title
-		NSRect boundingRect = [string boundingRectWithSize:cellFrame.size options:0];
-		
-		NSPoint cellCenter;
-		cellCenter.y = cellFrame.size.height / 2;
-		
-		NSPoint drawPoint = cellCenter;
-		drawPoint.y -= boundingRect.size.height / 2;
-		drawPoint.y = roundf(drawPoint.y);
-		
-		drawPoint.x = IMAGE_INSET;
-	
-		[string drawAtPoint:drawPoint];
+		[transform2 invert];
+		[transform2 concat];
 	}
+
+	[transform invert];
+	[transform concat];	
 }
 
-- (void)drawImageInFrame:(NSRect)cellFrame
+#pragma mark Draw Title
+
+- (NSColor *)textColor
 {
+	return [self isEnabled] ? enabledTextColor : disabledTextColor;
+}
+
+- (NSDictionary *)_textAttributes
+{
+	NSMutableDictionary *attributes = [[[NSMutableDictionary alloc] init] autorelease];
+	[attributes addEntriesFromDictionary:[super _textAttributes]];
+	[attributes setObject:[self textColor] forKey:NSForegroundColorAttributeName];
+	[attributes setObject:[NSFont systemFontOfSize:11] forKey:NSFontAttributeName];
+	[attributes setObject:contentShadow forKey:NSShadowAttributeName];
+	
+	return attributes;
+}
+
+- (NSRect)titleRectForBounds:(NSRect)bounds
+{
+	NSRect titleRect = [super titleRectForBounds:bounds];
+	
+	titleRect.origin.y -= 1;
+	titleRect = NSInsetRect(titleRect, 3, 0);
+	
+	return titleRect;
+}
+
+#pragma mark Draw Image
+
+- (NSColor *)imageColor
+{
+	return [self isEnabled] ? enabledImageColor : disabledImageColor;
+}
+
+- (NSRect)imageRectForBounds:(NSRect)theRect
+{
+	NSRect imageRect = [super imageRectForBounds:theRect];
+	
+	if (imageRect.size.width < [self image].size.width)
+		imageRect.size.width += 1;
+	
+	imageRect.origin.x += 1;
+	
+	return imageRect;
+}
+
+- (void)drawImageWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
+{	
 	NSImage *image = [self image];
 	
 	if (image != nil)
 	{
-		[image setScalesWhenResized:NO];
-		NSRect sourceRect = NSZeroRect;
-		
 		if ([[image name] isEqualToString:@"NSActionTemplate"])
 			[image setSize:NSMakeSize(10,10)];
 		
-		sourceRect.size = [image size];
+		NSImage *newImage = image;
 		
-		NSPoint backgroundCenter;
-		backgroundCenter.y = cellFrame.size.height / 2;
-		
-		NSPoint drawPoint = backgroundCenter;
-		drawPoint.y -= sourceRect.size.height / 2 ;//+ 0.5;
-		drawPoint.y = roundf(drawPoint.y);
-		
-		drawPoint.x = IMAGE_INSET;
-		
+		if ([image isTemplate])
+			newImage = [image bwTintedImageWithColor:[self imageColor]];
+
 		NSAffineTransform* transform = [NSAffineTransform transform];
 		[transform translateXBy:0.0 yBy:cellFrame.size.height];
 		[transform scaleXBy:1.0 yBy:-1.0];
 		[transform concat];
 		
-		if ([image isTemplate])
-		{
-			NSImage *glyphImage = [image bwTintedImageWithColor:imageColor];
-			NSImage *shadowImage = [image bwTintedImageWithColor:imageShadowColor];
-			NSPoint shadowPoint = drawPoint;
-			shadowPoint.y--;
-			
-			[shadowImage drawAtPoint:shadowPoint fromRect:sourceRect operation:NSCompositeSourceOver fraction:1];		
-			
-			if ([self isEnabled])
-				[glyphImage drawAtPoint:drawPoint fromRect:sourceRect operation:NSCompositeSourceOver fraction:1];
-			else
-				[glyphImage	drawAtPoint:drawPoint fromRect:sourceRect operation:NSCompositeSourceOver fraction:0.5];
-			
-			[transform invert];
-			[transform concat];
-		}
-		else
-		{
-			if ([self isEnabled])
-				[image drawAtPoint:drawPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
-			else
-				[image drawAtPoint:drawPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:0.5];
-			
-			// Run the flip transform again so the arrow doesn't draw upside-down
-			NSAffineTransform* transform = [NSAffineTransform transform];
-			[transform translateXBy:0.0 yBy:cellFrame.size.height];
-			[transform scaleXBy:1.0 yBy:-1.0];
-			[transform concat];
-		}
-	}
-}
-- (void)drawArrowInFrame:(NSRect)cellFrame
-{
-	if ([self pullsDown])
-	{
-		NSPoint drawPoint;
-		drawPoint.x = NSMaxX(cellFrame) - ARROW_INSET;
-		drawPoint.y = roundf(cellFrame.size.height / 2) - 2;
+		[newImage drawInRect:[self imageRectForBounds:cellFrame] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
 
-		NSImage *glyphImage = [pullDownArrow bwTintedImageWithColor:imageColor];
-		NSImage *shadowImage = [pullDownArrow bwTintedImageWithColor:imageShadowColor];
-		NSPoint shadowPoint = drawPoint;
-		shadowPoint.y--;
-		
-		NSAffineTransform* transform = [NSAffineTransform transform];
-		[transform translateXBy:0.0 yBy:cellFrame.size.height];
-		[transform scaleXBy:1.0 yBy:-1.0];
-		[transform concat];
-		
-		[shadowImage drawAtPoint:shadowPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
-		
-		if ([self isEnabled])
-			[glyphImage drawAtPoint:drawPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
-		else
-			[glyphImage drawAtPoint:drawPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:0.5];
-		
 		[transform invert];
 		[transform concat];
-	}
-	else
-	{
-		// Doesn't support pop-up style yet
-	}
-}
-
-- (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.