1 | //
|
---|
2 | // NJRTableView.m
|
---|
3 | // Pester
|
---|
4 | //
|
---|
5 | // Created by Nicholas Riley on Sun Nov 17 2002.
|
---|
6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
7 | //
|
---|
8 |
|
---|
9 | #import "NJRTableView.h"
|
---|
10 | #import "NSTableView-NJRExtensions.h"
|
---|
11 | #import "NSCharacterSet-NJRExtensions.h"
|
---|
12 |
|
---|
13 | /* only defined in 10.2, but we want to be able to compile without warnings on 10.1.x */
|
---|
14 | @interface NSColor (JaguarExtras)
|
---|
15 | + (NSColor *)alternateSelectedControlColor;
|
---|
16 | @end
|
---|
17 |
|
---|
18 | @implementation NJRTableView
|
---|
19 |
|
---|
20 | - (id)typeSelectDisplay;
|
---|
21 | {
|
---|
22 | return typeSelectDisplay;
|
---|
23 | }
|
---|
24 |
|
---|
25 | - (void)moveToEndOfDocument:(id)sender {
|
---|
26 | [self scrollRowToVisible: [self numberOfRows] - 1];
|
---|
27 | }
|
---|
28 |
|
---|
29 | - (void)moveToBeginningOfDocument:(id)sender {
|
---|
30 | [self scrollRowToVisible: 0];
|
---|
31 | }
|
---|
32 |
|
---|
33 | - (void)keyDown:(NSEvent *)theEvent;
|
---|
34 | {
|
---|
35 | NSString *characters;
|
---|
36 | unichar firstCharacter;
|
---|
37 | characters = [theEvent characters];
|
---|
38 | firstCharacter = [characters characterAtIndex: 0];
|
---|
39 | switch (firstCharacter) {
|
---|
40 | case 0177: // delete key
|
---|
41 | case NSDeleteFunctionKey:
|
---|
42 | case NSDeleteCharFunctionKey:
|
---|
43 | if ([self selectedRow] >= 0 && [[self dataSource] respondsToSelector: @selector(removeSelectedRowsFromTableView:)]) {
|
---|
44 | [[self dataSource] removeSelectedRowsFromTableView: self];
|
---|
45 | }
|
---|
46 | return;
|
---|
47 | case NSHomeFunctionKey:
|
---|
48 | [self moveToBeginningOfDocument: nil];
|
---|
49 | return;
|
---|
50 | case NSEndFunctionKey:
|
---|
51 | [self moveToEndOfDocument: nil];
|
---|
52 | return;
|
---|
53 | }
|
---|
54 | if ([[NSCharacterSet typeSelectSet] characterIsMember: firstCharacter]) {
|
---|
55 | // invoking -[NSResponder interpretKeyEvents:] will cause insertText: to be invoked, and allows function keys to still work.
|
---|
56 | [self interpretKeyEvents: [NSArray arrayWithObject: theEvent]];
|
---|
57 | } else {
|
---|
58 | [super keyDown: theEvent];
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | - (void)insertText:(id)inString;
|
---|
63 | {
|
---|
64 | if (![[self delegate] respondsToSelector:@selector(selectString:inTableView:)]) {
|
---|
65 | // For consistency with List Manager as documented, reset the typeahead buffer after twice the delay until key repeat (in ticks).
|
---|
66 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
---|
67 | int keyRepeatTicks = [defaults integerForKey: @"InitialKeyRepeat"];
|
---|
68 | NSTimeInterval resetDelay;
|
---|
69 |
|
---|
70 | if (keyRepeatTicks == 0) keyRepeatTicks = 35; // default may be missing; if so, set default
|
---|
71 |
|
---|
72 | resetDelay = MIN(2.0 / 60.0 * keyRepeatTicks, 2.0);
|
---|
73 |
|
---|
74 | if (typed == nil) typed = [[NSMutableString alloc] init];
|
---|
75 | [typed appendString: inString];
|
---|
76 |
|
---|
77 | // Cancel any previously queued future invocations of _resetTypeSelect
|
---|
78 | [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(_resetTypeSelect) object: nil];
|
---|
79 |
|
---|
80 | // queue an invocation of clearAccumulatingTypeahead for the near future.
|
---|
81 | [self performSelector: @selector(_resetTypeSelect) withObject: nil afterDelay: resetDelay];
|
---|
82 |
|
---|
83 | // Use stringWithString to make an autoreleased copy, since we may clear out the original string below before it can be used.
|
---|
84 | [[self delegate] tableView: self selectRowMatchingString: [NSString stringWithString: typed]];
|
---|
85 |
|
---|
86 | // Show the current typeahead string in the optional display field, like CodeWarrior does (well, not really, CW is much more elegant because it doesn't select anything until you stop typing)
|
---|
87 | [typeSelectDisplay setObjectValue: typed];
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | - (void)_resetTypeSelect;
|
---|
92 | {
|
---|
93 | [typed setString: @""];
|
---|
94 | [typeSelectDisplay setObjectValue: nil];
|
---|
95 | }
|
---|
96 |
|
---|
97 | - (void)resetTypeSelect;
|
---|
98 | {
|
---|
99 | [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(_resetTypeSelect) object: nil];
|
---|
100 | [self _resetTypeSelect];
|
---|
101 | }
|
---|
102 |
|
---|
103 | #pragma mark row coloring
|
---|
104 |
|
---|
105 | - (void)drawGridInClipRect:(NSRect)rect;
|
---|
106 | {
|
---|
107 | NSRange columnRange = [self columnsInRect: rect];
|
---|
108 | int i;
|
---|
109 | // match iTunesÕ grid color
|
---|
110 | [[[NSColor gridColor] blendedColorWithFraction: 0.70 ofColor: [NSColor whiteColor]] set];
|
---|
111 | for (i = columnRange.location ; i < NSMaxRange(columnRange) ; i++) {
|
---|
112 | NSRect colRect = [self rectOfColumn:i];
|
---|
113 | int rightEdge = (int) 0.5 + colRect.origin.x + colRect.size.width;
|
---|
114 | [NSBezierPath strokeLineFromPoint: NSMakePoint(-0.5 + rightEdge, -0.5 + rect.origin.y)
|
---|
115 | toPoint: NSMakePoint(-0.5 + rightEdge, -0.5 + rect.origin.y + rect.size.height)];
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | - (void)highlightSelectionInClipRect:(NSRect)clipRect;
|
---|
120 | {
|
---|
121 | NSColor *evenColor, *oddColor = [self backgroundColor];
|
---|
122 | float cellHeight = [self cellHeight];
|
---|
123 | NSRect visibleRect = [self visibleRect];
|
---|
124 | NSRect highlightRect;
|
---|
125 |
|
---|
126 | if ([NSColor respondsToSelector: @selector(alternateSelectedControlColor)])
|
---|
127 | evenColor = [[NSColor alternateSelectedControlColor] highlightWithLevel:0.90];
|
---|
128 | else // match iTunesÕ row background color
|
---|
129 | evenColor = [NSColor colorWithCalibratedRed: 0.929 green: 0.953 blue: 0.996 alpha:1.0];
|
---|
130 |
|
---|
131 | highlightRect.origin = NSMakePoint(NSMinX(visibleRect), (int)(NSMinY(clipRect) / cellHeight) * cellHeight);
|
---|
132 | highlightRect.size = NSMakeSize(NSWidth(visibleRect), cellHeight);
|
---|
133 |
|
---|
134 | while (NSMinY(highlightRect) < NSMaxY(clipRect)) {
|
---|
135 | NSRect clippedHighlightRect = NSIntersectionRect(highlightRect, clipRect);
|
---|
136 | int row = (int)((NSMinY(highlightRect) + cellHeight / 2.0) / cellHeight);
|
---|
137 | NSColor *rowColor = (row % 2 == 0) ? evenColor : oddColor;
|
---|
138 | [rowColor set];
|
---|
139 | NSRectFill(clippedHighlightRect);
|
---|
140 | highlightRect.origin.y += cellHeight;
|
---|
141 | }
|
---|
142 |
|
---|
143 | [super highlightSelectionInClipRect: clipRect];
|
---|
144 | }
|
---|
145 |
|
---|
146 | @end
|
---|