#import "MergedColumnTableView.h"

// See if dataSource responds to 
// - (int)tableView:(NSTableView *)tableView spanForTableColumn:(NSTableColumn *)tableColumn row:(int)row

/* 
???? What does this mean?

WARN ONCE: Not live resizing [MergedColumnTableView] <MergedColumnTableView: 0x1791f30> because: might do custom drawing.

*/


@implementation MergedColumnTableView

/*"	This category contains methods used to support Watson tool functionality, specifically for managing a table view in which some columns span across more than one cell.  The original motivation for this is a TV show timeline grid.

	The data source must respond to the following selector:
	!{- (int)tableView:(NSTableView *)tableView spanForTableColumn:(NSTableColumn *)tableColumn row:(int)row}
	as defined in SpanningDataTableSource.h

"*/

- (void)drawRow:(int)inRow
clipRect:(NSRect)inClipRect
{
	NSRect newClipRect = inClipRect;
	if ([[self dataSource] respondsToSelector:@selector(tableView:spanForTableColumn:row:)])
	{
		int colspan = 0;
		int firstColumn = [self columnsInRect:inClipRect].location;
		// Does the FIRST one of these have a zero-colspan?  If so, we need to extend range.
		while (0 == colspan)
		{
			colspan = [[self dataSource]
							tableView:self
							spanForTableColumn:[[self tableColumns] objectAtIndex:firstColumn]
							row:inRow];
			if (0 == colspan)
			{
				firstColumn--;
				newClipRect = NSUnionRect(newClipRect, [self frameOfCellAtColumn:firstColumn row:inRow]);
			}
		}
	}
	[super drawRow:inRow clipRect:newClipRect];
}


// See if dataSource responds to 
// - (int)tableView:(NSTableView *)tableView spanForTableColumn:(NSTableColumn *)tableColumn row:(int)row

/*"	Return the frame.  Used by #CellTableView.
"*/

- (NSRect)frameOfCellAtColumn:(int)column row:(int)row
{
	int colspan;
	if (![[self dataSource] respondsToSelector:@selector(tableView:spanForTableColumn:row:)])
	{
		return [super frameOfCellAtColumn:column row:row];
	}
	colspan = [[self dataSource]
				tableView:self
				spanForTableColumn:[[self tableColumns] objectAtIndex:column]
				row:row];
	if (0 == colspan)
	{
		return NSZeroRect;
	}
	if (1 == colspan)
	{
		return [super frameOfCellAtColumn:column row:row];
	}
	else		// 2 or more, it's responsibility of data source to provide reasonable number
	{
		NSRect merged = [super frameOfCellAtColumn:column row:row];	// start out with this one
		int i;
		for (i = 1; i < colspan; i++ )	// start from next one
		{
			NSRect next = [super frameOfCellAtColumn:column+i row:row];
			merged = NSUnionRect(merged,next);
		}
		return merged;
	}
}

- (void)drawGridInClipRect:(NSRect)rect
{
	if (![[self dataSource] respondsToSelector:@selector(tableView:spanForTableColumn:row:)])
	{
		[super drawGridInClipRect:rect];
	}
	else
    {
        NSRange rowRange = [self rowsInRect:rect];
		NSRange columnRange = [self columnsInRect:rect];
		int row;

		// Adjust column range, always go from zero, so we can gather columns even to the left of
		// what we are supposed to draw.
		columnRange = NSMakeRange(0,NSMaxRange(columnRange));	// FIX; START AT ZERO ALWAYS.
		
		NSLog(@"Grid rows %@, cols %@", NSStringFromRange(rowRange), NSStringFromRange(columnRange));
		[[NSColor grayColor] set];

		for (	row = rowRange.location ;
				row < NSMaxRange(rowRange) ;
				row++ )
		{
			int col = columnRange.location;
			int oldLeftEdge = 0.5 + [self rectOfColumn:col].origin.x;
			NSRect rowRect = [self rectOfRow:row];
			// here, frame not the top and not the left, but the bottom
			[NSBezierPath strokeLineFromPoint:NSMakePoint(rowRect.origin.x, -0.5+rowRect.origin.y+rowRect.size.height) toPoint:NSMakePoint(rowRect.origin.x + rowRect.size.width, -0.5+rowRect.origin.y+rowRect.size.height)];

			while (	col < NSMaxRange(columnRange) )
			{
				int colspan = [[self dataSource] tableView:self spanForTableColumn:[[self tableColumns] objectAtIndex:col] row:row];
				NSRect gridRect = NSZeroRect;
				if (0 == colspan)
				{
					col++;		// no grid here, move along
				}
				else	// Now gather up the next <colspan> rectangles
				{
					int i, rightEdge, leftEdge;
					for ( i = 0 ; i < colspan ; i++ )
					{
						NSRect thisRect = NSIntersectionRect([self rectOfColumn:col+i], [self rectOfRow:row]);
						gridRect = NSUnionRect(gridRect,thisRect);
					}
					col += colspan;

					// left edge.  Only draw if this left edge isn't one we just drew.
					leftEdge = (int) 0.5 + gridRect.origin.x;
					if (leftEdge != oldLeftEdge)
					{
						[NSBezierPath strokeLineFromPoint:
							NSMakePoint(-0.5+leftEdge, -0.5+gridRect.origin.y)
													toPoint:
							NSMakePoint(-0.5+leftEdge, -0.5+gridRect.origin.y + gridRect.size.height)];
					}
					// right edge
					rightEdge = (int) 0.5 + gridRect.origin.x + gridRect.size.width;
					[NSBezierPath strokeLineFromPoint:
						NSMakePoint(-0.5+rightEdge, -0.5+gridRect.origin.y)
						toPoint:
						NSMakePoint(-0.5+rightEdge, -0.5+gridRect.origin.y + gridRect.size.height)];
					oldLeftEdge = rightEdge;	// save this edge for next pass through.
				}
			}

		
		}
    }
}

@end
