Anonymous avatar Anonymous committed 4c462c7

Rewrote transparent button and pop-up button to support image alignment, text justification & truncation, and other standard cell functionality

Comments (0)

Files changed (4)

BWTransparentButtonCell.h

 
 @interface BWTransparentButtonCell : NSButtonCell 
 {
-	NSColor *interiorColor;
+
 }
 
 @end

BWTransparentButtonCell.m

 static NSImage *buttonLeftN, *buttonFillN, *buttonRightN, *buttonLeftP, *buttonFillP, *buttonRightP;
 static NSColor *disabledColor, *enabledColor;
 
+@interface NSCell (BWTBCPrivate)
+- (NSDictionary *)_textAttributes;
+@end
+
 @interface BWTransparentButtonCell (BWTBCPrivate)
-- (void)drawTitleWithFrame:(NSRect)cellFrame;
-- (void)drawImageWithFrame:(NSRect)cellFrame;
+- (NSColor *)interiorColor;
 @end
 
 @implementation BWTransparentButtonCell
 	disabledColor = [[NSColor colorWithCalibratedWhite:0.6 alpha:1] retain];
 }
 
-- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
+- (void)drawBezelWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
 {
 	cellFrame.size.height = buttonFillN.size.height;
 	
 	if ([self isHighlighted])
 		NSDrawThreePartImage(cellFrame, buttonLeftP, buttonFillP, buttonRightP, NO, NSCompositeSourceOver, 1, YES);
 	else
-		NSDrawThreePartImage(cellFrame, buttonLeftN, buttonFillN, buttonRightN, NO, NSCompositeSourceOver, 1, YES);
+		NSDrawThreePartImage(cellFrame, buttonLeftN, buttonFillN, buttonRightN, NO, NSCompositeSourceOver, 1, YES);	
+}
+
+- (void)drawImage:(NSImage *)image withFrame:(NSRect)frame inView:(NSView *)controlView
+{	
+	frame.origin.y -= 2;
+	
+	if ([[image name] isEqualToString:@"NSActionTemplate"])
+		[image setSize:NSMakeSize(10,10)];
+	
+	NSImage *newImage = image;
+	
+	if ([image isTemplate])
+		newImage = [image tintedImageWithColor:[self interiorColor]];
+	
+	[super drawImage:newImage withFrame:frame inView:controlView];
+}
+
+- (NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView
+{
+	frame.origin.y -= 4;
+	
+	return [super drawTitle:title withFrame:frame inView:controlView];
+}
+
+- (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
+{
+	NSColor *interiorColor;
 	
 	if ([self isEnabled])
 		interiorColor = enabledColor;
 	else
 		interiorColor = disabledColor;
 	
-	if ([self image] == nil)
-		[self drawTitleWithFrame:cellFrame];
-	else
-		[self drawImageWithFrame:cellFrame];
-
-}
-
-- (void)drawTitleWithFrame:(NSRect)cellFrame
-{
-	if (![[self title] isEqualToString:@""])
-	{
-		NSMutableDictionary *attributes = [[[NSMutableDictionary alloc] init] autorelease];
-		[attributes addEntriesFromDictionary:[[self attributedTitle] attributesAtIndex:0 effectiveRange:NULL]];
-		[attributes setObject:interiorColor forKey:NSForegroundColorAttributeName];
-		[attributes setObject:[NSFont systemFontOfSize:11] forKey:NSFontAttributeName];
-		NSMutableAttributedString *string = [[[NSMutableAttributedString alloc] initWithString:[self title] attributes:attributes] autorelease];
-		[self setAttributedTitle:string];
-		
-		cellFrame.origin.y += 2;
-		[[self attributedTitle] drawInRect:cellFrame];
-	}
-}
-
-- (void)drawImageWithFrame:(NSRect)cellFrame
-{
-	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];
-		
-		NSPoint backgroundCenter;
-		backgroundCenter.x = cellFrame.size.width / 2;
-		backgroundCenter.y = cellFrame.size.height / 2;
-		
-		NSPoint drawPoint = backgroundCenter;
-		drawPoint.x -= sourceRect.size.width / 2;
-		drawPoint.y -= sourceRect.size.height / 2 ;
-		
-		drawPoint.x = roundf(drawPoint.x);
-		drawPoint.y = roundf(drawPoint.y) + 1;
-		
-		NSImage *glyphImage = image;
-		
-		if ([image isTemplate])
-		{
-			glyphImage = [image tintedImageWithColor:interiorColor];
-			
-			NSAffineTransform* xform = [NSAffineTransform transform];
-			[xform translateXBy:0.0 yBy:cellFrame.size.height];
-			[xform scaleXBy:1.0 yBy:-1.0];
-			[xform concat];
-		}
-
-		[glyphImage drawAtPoint:drawPoint fromRect:sourceRect operation:NSCompositeSourceOver fraction:1];
-	}	
+	return interiorColor;
 }
 
 - (NSControlSize)controlSize

BWTransparentPopUpButtonCell.h

 
 @interface BWTransparentPopUpButtonCell : NSPopUpButtonCell 
 {
-	NSColor *interiorColor;
+
 }
 
 @end

BWTransparentPopUpButtonCell.m

 static NSImage *popUpFillN, *popUpFillP, *popUpRightN, *popUpRightP, *popUpLeftN, *popUpLeftP, *pullDownRightN, *pullDownRightP;
 static NSColor *disabledColor, *enabledColor;
 
+@interface NSCell (BWTPUBCPrivate)
+- (NSDictionary *)_textAttributes;
+@end
+
 @interface BWTransparentPopUpButtonCell (BWTPUBCPrivate)
-- (void)drawTitleWithFrame:(NSRect)cellFrame;
-- (void)drawImageWithFrame:(NSRect)cellFrame;
+- (NSColor *)interiorColor;
 @end
 
 @implementation BWTransparentPopUpButtonCell
 	disabledColor = [[NSColor colorWithCalibratedWhite:0.6 alpha:1] retain];
 }
 
-- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
+- (void)drawBezelWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
 {
 	cellFrame.size.height = popUpFillN.size.height;
 	
 		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 tintedImageWithColor:[self interiorColor]];
+
+		NSAffineTransform* xform = [NSAffineTransform transform];
+		[xform translateXBy:0.0 yBy:cellFrame.size.height];
+		[xform scaleXBy:1.0 yBy:-1.0];
+		[xform concat];
+		
+		[newImage drawInRect:[self imageRectForBounds:cellFrame] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
+		
+		NSAffineTransform* xform2 = [NSAffineTransform transform];
+		[xform2 translateXBy:0.0 yBy:cellFrame.size.height];
+		[xform2 scaleXBy:1.0 yBy:-1.0];
+		[xform2 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
+{
+	NSColor *interiorColor;
+	
 	if ([self isEnabled])
 		interiorColor = enabledColor;
 	else
 		interiorColor = disabledColor;
 	
-	if ([self image] == nil)
-		[self drawTitleWithFrame:cellFrame];
-	else
-		[self drawImageWithFrame:cellFrame];
-}
-
-- (void)drawTitleWithFrame:(NSRect)cellFrame
-{
-	if (![[self title] isEqualToString:@""])
-	{
-		NSMutableDictionary *attributes = [[[NSMutableDictionary alloc] init] autorelease];
-		[attributes addEntriesFromDictionary:[[self attributedTitle] attributesAtIndex:0 effectiveRange:NULL]];
-		[attributes setObject:interiorColor forKey:NSForegroundColorAttributeName];
-		
-		NSMutableAttributedString *string = [[[NSMutableAttributedString alloc] initWithString:[self title] attributes:attributes] autorelease];
-		
-		[string drawAtPoint:NSMakePoint(8,2)];
-	}	
-}
-
-- (void)drawImageWithFrame:(NSRect)cellFrame
-{
-	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];
-		
-		NSPoint backgroundCenter;
-		backgroundCenter.x = cellFrame.size.width / 2;
-		backgroundCenter.y = cellFrame.size.height / 2;
-		
-		NSPoint drawPoint = backgroundCenter;
-		drawPoint.x -= sourceRect.size.width / 2;
-		drawPoint.y -= sourceRect.size.height / 2;
-		
-		drawPoint.x = 8;
-		drawPoint.y = roundf(drawPoint.y) + 1;
-		
-		NSImage *glyphImage = image;
-		
-		if ([image isTemplate])
-		{
-			glyphImage = [image tintedImageWithColor:interiorColor];
-			
-			NSAffineTransform* xform = [NSAffineTransform transform];
-			[xform translateXBy:0.0 yBy:cellFrame.size.height];
-			[xform scaleXBy:1.0 yBy:-1.0];
-			[xform concat];
-		}
-		
-		[glyphImage drawAtPoint:drawPoint fromRect:sourceRect operation:NSCompositeSourceOver fraction:1];
-	}	
+	return interiorColor;
 }
 
 - (NSControlSize)controlSize
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.