public
Description: A Gitk-like application written in RubyCocoa that looks like it belongs on a Mac. See the wiki for downloads and screenshots.
Homepage: http://alternateidea.com
Clone URL: git://github.com/Caged/gitnub.git
Click here to lend your support to: gitnub and make a donation at www.pledgie.com !
commit  6b17a04c8898bdde250fd8a1125d68627a84d116
tree    2878c0e66debe83f9768cb140380d85548923e83
parent  905cadbed9d74c88353ac88e403cc84bc1c9e269
gitnub / ImageTextCell.m
100644 137 lines (112 sloc) 4.809 kb
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
132
133
134
135
136
137
//
// 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];
    NSMutableParagraphStyle* style = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
    [style setLineBreakMode:NSLineBreakByTruncatingTail];
 
  NSDictionary* primaryTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
   primaryColor, NSForegroundColorAttributeName,
    [NSFont systemFontOfSize:12], NSFontAttributeName,
    style, NSParagraphStyleAttributeName,
    nil];  
  [primaryText drawAtPoint:NSMakePoint(cellFrame.origin.x+cellFrame.size.height+9, 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+9, cellFrame.origin.y+cellFrame.size.height/2.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+3,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