-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathImageTextCell.m
131 lines (107 loc) · 4.49 KB
/
ImageTextCell.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// ImageTextCell.m
// SofaControl
//
// Created by Martin Kahr on 10.10.06.
// Copyright 2006 CASE Apps. All rights reserved.
//
#import "ImageTextCell.h"
@implementation ImageTextCell
- (void)dealloc {
[self setDataDelegate: nil];
[self setIconKeyPath: nil];
[self setPrimaryTextKeyPath: nil];
[self setSecondaryTextKeyPath: nil];
[super dealloc];
}
- copyWithZone:(NSZone *)zone {
ImageTextCell *cell = (ImageTextCell *)[super copyWithZone:zone];
cell->delegate = nil;
[cell setDataDelegate: delegate];
return cell;
}
- (void) setIconKeyPath: (NSString*) path {
[iconKeyPath autorelease];
iconKeyPath = [path retain];
}
- (void) setPrimaryTextKeyPath: (NSString*) path {
[primaryTextKeyPath autorelease];
primaryTextKeyPath = [path retain];
}
- (void) setSecondaryTextKeyPath: (NSString*) path {
[secondaryTextKeyPath autorelease];
secondaryTextKeyPath = [path retain];
}
- (void) setDataDelegate: (NSObject*) aDelegate {
[aDelegate retain];
[delegate autorelease];
delegate = aDelegate;
}
- (id) dataDelegate {
if (delegate) return delegate;
return self; // in case there is no delegate we try to resolve values by using key paths
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
[self setTextColor:[NSColor blackColor]];
NSObject* data = [self objectValue];
// give the delegate a chance to set a different data object
if ([[self dataDelegate] respondsToSelector: @selector(dataElementForCell:)]) {
data = [[self dataDelegate] dataElementForCell:self];
}
//TODO: Selection with gradient and selection color in white with shadow
// check out http://www.cocoadev.com/index.pl?NSTableView
BOOL elementDisabled = NO;
if ([[self dataDelegate] respondsToSelector: @selector(disabledForCell:data:)]) {
elementDisabled = [[self dataDelegate] disabledForCell: self data: data];
}
NSColor* primaryColor = [self isHighlighted] ? [NSColor alternateSelectedControlTextColor] : (elementDisabled? [NSColor disabledControlTextColor] : [NSColor textColor]);
NSString* primaryText = [[self dataDelegate] primaryTextForCell:self data: data];
NSDictionary* primaryTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys: primaryColor, NSForegroundColorAttributeName,
[NSFont systemFontOfSize:13], NSFontAttributeName, nil];
[primaryText drawAtPoint:NSMakePoint(cellFrame.origin.x+cellFrame.size.height+10, cellFrame.origin.y) withAttributes:primaryTextAttributes];
NSColor* secondaryColor = [self isHighlighted] ? [NSColor alternateSelectedControlTextColor] : [NSColor disabledControlTextColor];
NSString* secondaryText = [[self dataDelegate] secondaryTextForCell:self data: data];
NSDictionary* secondaryTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys: secondaryColor, NSForegroundColorAttributeName,
[NSFont systemFontOfSize:10], NSFontAttributeName, nil];
[secondaryText drawAtPoint:NSMakePoint(cellFrame.origin.x+cellFrame.size.height+10, cellFrame.origin.y+cellFrame.size.height/2)
withAttributes:secondaryTextAttributes];
[[NSGraphicsContext currentContext] saveGraphicsState];
float yOffset = cellFrame.origin.y;
if ([controlView isFlipped]) {
NSAffineTransform* xform = [NSAffineTransform transform];
[xform translateXBy:0.0 yBy: cellFrame.size.height];
[xform scaleXBy:1.0 yBy:-1.0];
[xform concat];
yOffset = 0-cellFrame.origin.y;
}
NSImage* icon = [[self dataDelegate] iconForCell:self data: data];
NSImageInterpolation interpolation = [[NSGraphicsContext currentContext] imageInterpolation];
[[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh];
[icon drawInRect:NSMakeRect(cellFrame.origin.x+5,yOffset+3,cellFrame.size.height-6, cellFrame.size.height-6)
fromRect:NSMakeRect(0,0,[icon size].width, [icon size].height)
operation:NSCompositeSourceOver
fraction:1.0];
[[NSGraphicsContext currentContext] setImageInterpolation: interpolation];
[[NSGraphicsContext currentContext] restoreGraphicsState];
}
#pragma mark -
#pragma mark Delegate methods
- (NSImage*) iconForCell: (ImageTextCell*) cell data: (NSObject*) data {
if (iconKeyPath) {
return [data valueForKeyPath: iconKeyPath];
}
return nil;
}
- (NSString*) primaryTextForCell: (ImageTextCell*) cell data: (NSObject*) data {
if (primaryTextKeyPath) {
return [data valueForKeyPath: primaryTextKeyPath];
}
return nil;
}
- (NSString*) secondaryTextForCell: (ImageTextCell*) cell data: (NSObject*) data {
if (primaryTextKeyPath) {
return [data valueForKeyPath: secondaryTextKeyPath];
}
return nil;
}
@end