Skip to content

Instantly share code, notes, and snippets.

@siannopollo
Created November 3, 2012 14:47
Show Gist options
  • Save siannopollo/4007554 to your computer and use it in GitHub Desktop.
Save siannopollo/4007554 to your computer and use it in GitHub Desktop.
NSTableView subclass to handle being printed and paginated properly
#import <Cocoa/Cocoa.h>
@interface PaginatedTable : NSTableView {
NSMutableArray *topBorderRows;
NSMutableArray *bottomBorderRows;
}
@end
@implementation PaginatedTable
// Taken from here: http://lists.apple.com/archives/cocoa-dev/2002/Nov/msg01710.html
// Ensures rows in the table aren't cut off when printing
- (void)adjustPageHeightNew:(CGFloat *)newBottom top:(CGFloat)oldTop bottom:(CGFloat)oldBottom limit:(CGFloat)bottomLimit {
if (!topBorderRows) {
topBorderRows = [NSMutableArray array];
bottomBorderRows = [NSMutableArray array];
}
int cutoffRow = [self rowAtPoint:NSMakePoint(0, oldBottom)];
NSRect rowBounds;
*newBottom = oldBottom;
if (cutoffRow != -1) {
rowBounds = [self rectOfRow:cutoffRow];
if (oldBottom < NSMaxY(rowBounds)) {
*newBottom = NSMinY(rowBounds);
NSNumber *row = [NSNumber numberWithInt:cutoffRow];
NSNumber *previousRow = [NSNumber numberWithInt:cutoffRow - 1];
// Mark which rows need which border, ignore ones we've already seen, and adjust ones that need different borders
if (![[topBorderRows lastObject] isEqual:row]) {
if ([[bottomBorderRows lastObject] isEqual:row]) {
[topBorderRows removeLastObject];
[bottomBorderRows removeLastObject];
}
[topBorderRows addObject:row];
[bottomBorderRows addObject:previousRow];
}
}
}
}
// Draw the row as normal, and add any borders to cells that were pushed down due to pagination
- (void)drawRow:(NSInteger)rowIndex clipRect:(NSRect)clipRect {
[super drawRow:rowIndex clipRect:clipRect];
if ([topBorderRows count] == 0) return;
NSRect rowRect = [self rectOfRow:rowIndex];
NSBezierPath *gridPath = [NSBezierPath bezierPath];
NSColor *color = [NSColor darkGrayColor];
for (int i=0; i<[topBorderRows count]; i++) {
int rowNeedingTopBorder = [(NSNumber *)[topBorderRows objectAtIndex:i] integerValue];
if (rowNeedingTopBorder == rowIndex) {
[gridPath moveToPoint:rowRect.origin];
[gridPath lineToPoint:NSMakePoint(rowRect.origin.x + rowRect.size.width, rowRect.origin.y)];
[color setStroke];
[gridPath stroke];
}
int rowNeedingBottomBorder = [(NSNumber *)[bottomBorderRows objectAtIndex:i] integerValue];
if (rowNeedingBottomBorder == rowIndex) {
[gridPath moveToPoint:NSMakePoint(rowRect.origin.x, rowRect.origin.y + rowRect.size.height)];
[gridPath lineToPoint:NSMakePoint(rowRect.origin.x + rowRect.size.width, rowRect.origin.y + rowRect.size.height)];
[color setStroke];
[gridPath stroke];
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment