-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYSmartHiliTableView.m
151 lines (120 loc) · 4.6 KB
/
SSYSmartHiliTableView.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#import "SSYSmartHiliTableView.h"
#import "NSColor+Tweak.h"
#import "NSView+ActiveControl.h"
/* Interesting References on this topic and related topics:
http://stoneship.org/journal/2005/using-a-nsoutlineview-as-a-source-list/
http://chanson.livejournal.com/176310.html
http://www.cocoadev.com/index.pl?IconAndTextInTableCell
http://mattgemmell.com/source
The blue-highlight color that NSTableView uses is this:
+ (NSColor *)alternateSelectedControlColor; // Similar to selectedControlColor; for use in lists and tables
Note: for custom cells, you may want to simply return nil from this method to avoid having the NSCell do any background highlight color drawing:
- (NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView;
That only works in NSTableViews; it won't work with cells in NSBrowsers, which actually rely on this method to draw the highlighting (since it is a matrix of cells).
corbin
*/
@interface SmartHiliTextCell : NSTextFieldCell {
}
@end
@implementation SmartHiliTextCell
- (void)drawInteriorWithFrame:(NSRect)cellFrame
inView:(NSView *)controlView {
// Note: When a cell if "highlighted", this is the same as when its
// row or column are "selected". "Highlighted" <==> "Selected".
// ARGHHHH!!! I set the cell to be not editable below, in awakeFromNib,
// but SOMETHING comes by and later sets it to be editable!!!!!!!!!
if(![self isHighlighted]) {
// Cell is not selected --> Let super draw text in black
[super drawInteriorWithFrame:cellFrame
inView:controlView] ;
}
else if ([self isEditable]) {
// Cell is selected and editable --> Draw text in white
NSRect inset = cellFrame;
inset.origin.x += 2;
NSDictionary* oldAttrs = [[self attributedStringValue] attributesAtIndex:0
effectiveRange:NULL] ;
NSMutableDictionary* newAttrs = [oldAttrs mutableCopy] ;
[newAttrs setValue:[NSColor whiteColor]
forKey:@"NSColor"] ;
[[self stringValue] drawInRect:inset
withAttributes:newAttrs] ;
[newAttrs release] ;
}
else {
// Cell is selected but not editable --> Draw nothing
// The editable controls (popup button, date picker)
// will be drawn in our rect.
}
}
@end
@implementation SSYSmartHiliTableView
- (void)highlightSelectionInClipRect:(NSRect)clipRect {
NSInteger selectedRow = [self selectedRow];
if(selectedRow != -1) {
// A row is selected
[self lockFocus];
// Get the appropriate color for the selected row
NSColor* color ;
if([self isTheActiveControl]) {
color = [NSColor selectedTextBackgroundColor] ;
// This is what is set in System Preferences
// > Appearance > Highlight Color (for selected text)
color = [color colorTweakBrightness:-.20] ;
// The -.2 factor was determined by reverse-engineering:
// I set the color in System Preferences to RGB = {255, 0, 255},
// but when I use DigitalColor Meter.app on a highlighted cell
// in a "stock" NSTableView, I measure {204, 0, 204}. Now,
// 204=51*4 and 255=51*5. So it looks like Apple decided to
// reduce the brightness by 20%, because, of course, if someone
// set their "highlight" (sic) color to all white, the white text
// drawn on it by NSTextFieldCell would be invisible.
// Interestingly, in NSTextViews (TextEdit docs, message in
// Mail.app), the text is drawn in black instead of white, and
// the 20% reduction is not applied.
}
else {
color = [NSColor secondarySelectedControlColor] ;
}
color = [color colorUsingColorSpaceName:@"NSDeviceRGBColorSpace"] ;
NSRect rect = [self rectOfRow:selectedRow] ;
NSInteger i = 0 ;
for (NSTableColumn* column in [self tableColumns]) {
if ([column isEditable]) {
rect = NSIntersectionRect(rect, [self rectOfColumn:i]) ;
}
i++ ;
}
if (
(rect.size.width > 0.0)
&&
(rect.size.height > 0.0)
) {
[[NSGraphicsContext currentContext] setCompositingOperation:NSCompositeSourceOver] ;
[color set] ;
[NSBezierPath fillRect:rect] ;
}
[self unlockFocus] ;
}
}
- (void)awakeFromNib {
for (NSTableColumn* column in [self tableColumns]) {
if (![column isEditable]) {
NSFont* font = [[column dataCell] font] ;
NSCell* newCell = [[[SmartHiliTextCell alloc] init] autorelease] ;
[newCell setFont:font] ;
[column setDataCell:newCell] ;
}
}
}
- (BOOL)becomeFirstResponder {
[[NSNotificationCenter defaultCenter] postNotificationName:@"BkmxViewDidBecomeFirstResponder"
object:self] ;
return YES ;
}
- (BOOL)resignFirstResponder {
[[NSNotificationCenter defaultCenter] postNotificationName:@"BkmxViewDidResignFirstResponder"
object:self] ;
return YES ;
}
@end