| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | #import "AIAlternatingRowOutlineView.h" |
| 25 | #import "AIOutlineView.h" |
| 26 | #import "AIGradient.h" |
| 27 | #import "AIColorAdditions.h" |
| 28 | |
| 29 | @interface AIAlternatingRowOutlineView (PRIVATE) |
| 30 | - (void)_initAlternatingRowOutlineView; |
| 31 | - (void)outlineViewDeleteSelectedRows:(NSTableView *)tableView; |
| 32 | - (void)_drawGridInClipRect:(NSRect)rect; |
| 33 | - (BOOL)_restoreSelectionFromSavedSelection; |
| 34 | - (void)_saveCurrentSelection; |
| 35 | @end |
| 36 | |
| 37 | @interface NSOutlineView (Undocumented) |
| 38 | - (id)_highlightColorForCell:(NSCell *)cell; |
| 39 | @end |
| 40 | |
| 41 | @implementation AIAlternatingRowOutlineView |
| 42 | |
| 43 | - (id)initWithCoder:(NSCoder *)aDecoder |
| 44 | { |
| 45 | if ((self = [super initWithCoder:aDecoder])) { |
| 46 | [self _initAlternatingRowOutlineView]; |
| 47 | } |
| 48 | return self; |
| 49 | } |
| 50 | |
| 51 | - (id)initWithFrame:(NSRect)frameRect |
| 52 | { |
| 53 | if ((self = [super initWithFrame:frameRect])) { |
| 54 | [self _initAlternatingRowOutlineView]; |
| 55 | } |
| 56 | return self; |
| 57 | } |
| 58 | |
| 59 | - (void)_initAlternatingRowOutlineView |
| 60 | { |
| 61 | drawsAlternatingRows = NO( BOOL ) 0; |
| 62 | drawsBackground = YES( BOOL ) 1; |
| 63 | drawsGradientSelection = NO( BOOL ) 0; |
| 64 | alternatingRowColor = [[NSColor colorWithCalibratedRed:(237.0/255.0) green:(243.0/255.0) blue:(254.0/255.0) alpha:1.0] retain]; |
| 65 | |
| 66 | [[NSNotificationCenter defaultCenter] addObserver:self |
| 67 | selector:@selector(alternatingRowOutlineViewSelectionDidChange:) |
| 68 | name:NSOutlineViewSelectionDidChangeNotification |
| 69 | object:self]; |
| 70 | } |
| 71 | |
| 72 | - (void)dealloc |
| 73 | { |
| 74 | [alternatingRowColor release]; |
| 75 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 76 | |
| 77 | [super dealloc]; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | - (void)setDrawsAlternatingRows:(BOOL)flag |
| 84 | { |
| 85 | drawsAlternatingRows = flag; |
| 86 | [self setNeedsDisplay:YES( BOOL ) 1]; |
| 87 | } |
| 88 | - (BOOL)drawsAlternatingRows{ |
| 89 | return drawsAlternatingRows; |
| 90 | } |
| 91 | |
| 92 | - (void)setDrawsGradientSelection:(BOOL)inDrawsGradientSelection |
| 93 | { |
| 94 | drawsGradientSelection = inDrawsGradientSelection; |
| 95 | [self setNeedsDisplay:YES( BOOL ) 1]; |
| 96 | } |
| 97 | |
| 98 | - (BOOL)drawsGradientSelection |
| 99 | { |
| 100 | return drawsGradientSelection; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | - (void)setAlternatingRowColor:(NSColor *)color |
| 105 | { |
| 106 | if (color != alternatingRowColor) { |
| 107 | [alternatingRowColor release]; |
| 108 | alternatingRowColor = [color retain]; |
| 109 | [self setNeedsDisplay:YES( BOOL ) 1]; |
| 110 | } |
| 111 | } |
| 112 | - (NSColor *)alternatingRowColor{ |
| 113 | return alternatingRowColor; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
| 118 | - (void)setDrawsBackground:(BOOL)inDraw |
| 119 | { |
| 120 | drawsBackground = inDraw; |
| 121 | [self setNeedsDisplay:YES( BOOL ) 1]; |
| 122 | } |
| 123 | - (BOOL)drawsBackground{ |
| 124 | return drawsBackground; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | - (NSColor *)backgroundColorForRow:(int)row |
| 129 | { |
| 130 | return ((row % 2) ? [self backgroundColor] : [self alternatingRowColor]); |
| 131 | } |
| 132 | |
| 133 | #pragma mark Drawing |
| 134 | |
| 135 | |
| 136 | - (void)drawAlternatingRowsInRect:(NSRect)rect |
| 137 | { |
| 138 | if (!drawsBackground || !drawsAlternatingRows) return; |
| 139 | |
| 140 | NSRect rowRect; |
| 141 | int rowHeight; |
| 142 | int numberOfRows; |
| 143 | int row; |
| 144 | int rectNumber = 0; |
| 145 | |
| 146 | |
| 147 | numberOfRows = [self numberOfRows]; |
| 148 | rowHeight = [self rowHeight]; |
| 149 | if (numberOfRows == 0) { |
| 150 | rowRect = NSMakeRect(0,0,rect.size.width,rowHeight); |
| 151 | } else { |
| Value stored to 'rowRect' is never read |
| 152 | rowRect = [self rectOfRow:0]; |
| 153 | } |
| 154 | |
| 155 | NSRect *gridRects = (NSRect *)malloc(sizeof(NSRect) * (numberOfRows + ((int)round(((rect.size.height / rowHeight) / 2) + 0.5)))); |
| 156 | for (row = 0; row < numberOfRows; row += 2) { |
| 157 | if (row < numberOfRows) { |
| 158 | NSRect thisRect = [self rectOfRow:row]; |
| 159 | if (NSIntersectsRect(thisRect, rect)) { |
| 160 | gridRects[rectNumber++] = thisRect; |
| 161 | } else { |
| 162 | NSLog(@"Not drawing because %@ is not in %@",NSStringFromRect(thisRect),NSStringFromRect(rect)); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (rectNumber > 0) { |
| 168 | [[self alternatingRowColor] set]; |
| 169 | NSRectFillList(gridRects, rectNumber); |
| 170 | } |
| 171 | free(gridRects); |
| 172 | } |
| 173 | |
| 174 | - (void)drawRect:(NSRect)rect |
| 175 | { |
| 176 | [super drawRect:rect]; |
| 177 | |
| 178 | if (drawsBackground && drawsAlternatingRows && [self drawsGrid]) { |
| 179 | [self _drawGridInClipRect:rect]; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | #pragma mark Gradient selection and alternating rows |
| 184 | |
| 185 | |
| 186 | |
| 187 | - (AIGradient *)selectedControlGradient |
| 188 | { |
| 189 | return [AIGradient selectedControlGradientWithDirection:AIVertical]; |
| 190 | } |
| 191 | |
| 192 | - (void)highlightSelectionInClipRect:(NSRect)clipRect |
| 193 | { |
| 194 | [self drawAlternatingRowsInRect:clipRect]; |
| 195 | |
| 196 | if (drawsGradientSelection && [[self window] isKeyWindow] && ([[self window] firstResponder] == self)) { |
| 197 | NSIndexSet *indices = [self selectedRowIndexes]; |
| 198 | unsigned int bufSize = [indices count]; |
| 199 | unsigned int *buf = malloc(bufSize * sizeof(unsigned int)); |
| 200 | unsigned int i = 0, j = 0; |
| 201 | |
| 202 | AIGradient *gradient = [self selectedControlGradient]; |
| 203 | |
| 204 | NSRange range = NSMakeRange([indices firstIndex], ([indices lastIndex]-[indices firstIndex]) + 1); |
| 205 | [indices getIndexes:buf maxCount:bufSize inIndexRange:&range]; |
| 206 | |
| 207 | NSRect *selectionLineRects = (NSRect *)malloc(sizeof(NSRect) * bufSize); |
| 208 | |
| 209 | while (i < bufSize) { |
| 210 | int startIndex = buf[i]; |
| 211 | int lastIndex = buf[i]; |
| 212 | |
| 213 | while ((i + 1 < bufSize) && |
| 214 | (buf[i + 1] == lastIndex + 1)){ |
| 215 | i++; |
| 216 | lastIndex++; |
| 217 | } |
| 218 | |
| 219 | NSRect startRow = [self rectOfRow:startIndex]; |
| 220 | NSRect endRow = [self rectOfRow:lastIndex]; |
| 221 | if (!NSIsEmptyRect(startRow)) { |
| 222 | NSRect thisRect; |
| 223 | if (!NSIsEmptyRect(endRow)) { |
| 224 | thisRect = NSUnionRect(startRow, endRow); |
| 225 | } else { |
| 226 | thisRect = startRow; |
| 227 | } |
| 228 | |
| 229 | [gradient drawInRect:thisRect]; |
| 230 | |
| 231 | |
| 232 | thisRect.size.height = 1; |
| 233 | selectionLineRects[j++] = thisRect; |
| 234 | } |
| 235 | |
| 236 | i++; |
| 237 | } |
| 238 | |
| 239 | [[[gradient firstColor] darkenAndAdjustSaturationBy:0.1] set]; |
| 240 | NSRectFillListUsingOperation(selectionLineRects, j, NSCompositeSourceOver); |
| 241 | |
| 242 | free(buf); |
| 243 | free(selectionLineRects); |
| 244 | |
| 245 | } else { |
| 246 | [super highlightSelectionInClipRect:clipRect]; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | |
| 251 | - (id)_highlightColorForCell:(NSCell *)cell |
| 252 | { |
| 253 | if (drawsGradientSelection && [[self window] isKeyWindow] && ([[self window] firstResponder] == self)) { |
| 254 | return nil0; |
| 255 | } else { |
| 256 | return [super _highlightColorForCell:cell]; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | - (void)alternatingRowOutlineViewSelectionDidChange:(NSNotification *)notification |
| 261 | { |
| 262 | if (drawsGradientSelection) { |
| 263 | |
| 264 | [self setNeedsDisplay:YES( BOOL ) 1]; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | #pragma mark Grid |
| 269 | |
| 270 | - (void)drawGridInClipRect:(NSRect)rect |
| 271 | { |
| 272 | if (drawsBackground && drawsAlternatingRows) { |
| 273 | |
| 274 | } else { |
| 275 | [super drawGridInClipRect:rect]; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | - (void)_drawGridInClipRect:(NSRect)rect |
| 280 | { |
| 281 | NSEnumerator *enumerator; |
| 282 | NSTableColumn *column; |
| 283 | float xPos = 0.5; |
| 284 | int intercellWidth = [self intercellSpacing].width; |
| 285 | |
| 286 | [[self gridColor] set]; |
| 287 | [NSBezierPath setDefaultLineWidth:1.0]; |
| 288 | |
| 289 | enumerator = [[self tableColumns] objectEnumerator]; |
| 290 | while ((column = [enumerator nextObject])) { |
| 291 | xPos += [column width] + intercellWidth; |
| 292 | |
| 293 | [NSBezierPath strokeLineFromPoint:NSMakePoint(xPos, rect.origin.y) |
| 294 | toPoint:NSMakePoint(xPos, rect.origin.y + rect.size.height)]; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | @end |