Source

Murky / 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;
}
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.