Commits

David Keegan  committed a6d76f3

adding a new icon/text cell class, this one sets the text to the vertical center off the cell

  • Participants
  • Parent commits 5668041

Comments (0)

Files changed (2)

File Source/IconTextCell.h

+//
+//  IconTextCell.h
+//  Murky
+//
+//  Created by David Keegan on 1/5/10.
+//  Copyright 2010 InScopeApps{+}. All rights reserved.
+//
+//  By:   Jim McGowan
+//  From: http://www.cocoadev.com/index.pl?VerticallyCenteringTableViewItems
+//
+
+#import <Cocoa/Cocoa.h>
+
+@interface IconTextCell : NSTextFieldCell {
+	NSImage *icon;
+}
+
+- (void)setIcon:(NSImage *)newIcon;
+
+@end

File Source/IconTextCell.m

+//
+//  IconTextCell.m
+//  Murky
+//
+//  Created by David Keegan on 1/5/10.
+//  Copyright 2010 InScopeApps{+}. All rights reserved.
+//
+
+#import "IconTextCell.h"
+
+void defineTextAndIconRects(NSRect cellRect, NSRect *imageRect, NSRect *stringRect, float stringHeight);
+
+@implementation IconTextCell
+
+- (id)init {
+    self = [super initTextCell:@""];
+	
+	icon = nil;
+	
+    [self setEditable:YES];
+    [self setScrollable:YES];
+    [self setLineBreakMode:NSLineBreakByTruncatingTail];
+    
+    return self;
+}
+
+- (id)copyWithZone:(NSZone *)zone {
+	IconTextCell *copy = [[IconTextCell allocWithZone:zone] init];
+	[copy setAttributedStringValue:[self attributedStringValue]];
+	[copy setIcon:icon];
+	return copy;
+}
+
+- (NSColor *)textColor {
+	if([self isHighlighted])
+	{
+		return [NSColor selectedMenuItemTextColor];
+	}
+	else
+	{
+		return [NSColor controlTextColor];
+	}
+}
+
+- (void)drawInteriorWithFrame:(NSRect)aRect inView:(NSView *)controlView {
+	NSRect imgRect, strRect;
+	float strHeight = [[self attributedStringValue] size].height;
+	defineTextAndIconRects(aRect, &imgRect, &strRect, strHeight);
+	
+	if([[self controlView] isFlipped])
+	{
+		[icon setFlipped:YES];
+	}
+	[icon drawInRect:imgRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f];
+	
+	[[self attributedStringValue] drawInRect:strRect];
+}
+
+- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength {
+	NSRect imgRect, strRect;
+	float strHeight = [[self attributedStringValue] size].height;
+	defineTextAndIconRects(aRect, &imgRect, &strRect, strHeight);
+	
+	[super selectWithFrame:strRect inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
+	[textObj setTextColor:[NSColor controlTextColor]];
+}
+
+- (void)setObjectValue:(id <NSCopying, NSObject>)object {
+	// change this part to suit your own use...
+	if([object isKindOfClass:[NSDictionary class]])
+	{
+		[self setStringValue:[(NSDictionary *)object valueForKey:@"name"]];
+		[self setIcon:[NSUnarchiver unarchiveObjectWithData:[(NSDictionary *)object valueForKey:@"image"]]];
+	}
+	else
+	{
+		[super setObjectValue:object];
+	}
+	
+}
+
+- (void)setIcon:(NSImage *)newIcon {
+	if(newIcon != icon)
+	{
+		[icon release];
+		icon = [newIcon copy];
+	}
+}
+
+- (void)dealloc {
+	[icon release];
+	[super dealloc];
+}
+
+@end
+
+void defineTextAndIconRects(NSRect cellRect, NSRect *imageRect, NSRect *stringRect, float stringHeight) {
+	float cellHeight = cellRect.size.height;
+	
+	NSDivideRect(cellRect, imageRect, stringRect, cellHeight, NSMinXEdge);
+	
+	*imageRect = NSInsetRect(*imageRect, 2.0f, 2.0f);
+	*stringRect = NSInsetRect(*stringRect, 2.0f, 0.0f);
+	
+	stringRect -> origin.y += (stringRect -> size.height - stringHeight) / 2.0f;
+	stringRect -> size.height = stringHeight;
+}