Finish/cleanup visible column table view drawing/resizing

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/branches/gnustep_testplant_branch@40374 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Marcian Lytwyn 2017-03-08 19:51:16 +00:00
parent c41ef8cc84
commit 767eaaabcb
3 changed files with 150 additions and 154 deletions

View file

@ -28,8 +28,10 @@
#import "GSThemePrivate.h"
#import "Foundation/NSUserDefaults.h"
#import "Foundation/NSDebug.h"
#import "Foundation/NSIndexSet.h"
#import "Foundation/NSPredicate.h"
#import "Foundation/NSUserDefaults.h"
#import "AppKit/NSAttributedString.h"
#import "AppKit/NSBezierPath.h"
@ -99,6 +101,12 @@
}
@end
@interface NSTableView (ColumnHelper)
- (NSArray*) _visibleColumns;
- (NSArray*) _resizableColumns;
- (NSArray*) _hiddenColumns;
@end
@interface NSCell (GNUstepPrivate)
- (void) _setInEditing: (BOOL)flag;
- (BOOL) _inEditing;
@ -2924,74 +2932,58 @@ typedef enum {
NSTableView *tableView = [tableHeaderView tableView];
NSArray *columns;
int firstColumnToDraw;
int lastColumnToDraw;
NSRect drawingRect;
NSTableColumn *column;
NSTableColumn *highlightedTableColumn;
float width;
int i;
NSCell *cell;
if (tableView == nil)
return;
firstColumnToDraw = [tableHeaderView columnAtPoint: NSMakePoint (aRect.origin.x,
aRect.origin.y)];
if (firstColumnToDraw == -1)
firstColumnToDraw = 0;
lastColumnToDraw = [tableHeaderView columnAtPoint: NSMakePoint (NSMaxX (aRect),
aRect.origin.y)];
if (lastColumnToDraw == -1)
lastColumnToDraw = [tableView numberOfColumns] - 1;
// Draw only visible columns based on hidden setting...
columns = [tableView _visibleColumns];
// If no visible columns then return...
if ([columns count] == 0)
return;
firstColumnToDraw = [[tableView tableColumns] indexOfObject: [columns firstObject]];
drawingRect = [tableHeaderView headerRectOfColumn: firstColumnToDraw];
columns = [tableView tableColumns];
highlightedTableColumn = [tableView highlightedTableColumn];
for (i = firstColumnToDraw; i <= lastColumnToDraw; i++)
NSEnumerator *iter = [columns objectEnumerator];
while ((column = [iter nextObject]))
{
column = [columns objectAtIndex: i];
NSInteger index = [[tableView tableColumns] indexOfObject: column];
width = [column width];
drawingRect.size.width = width;
cell = [column headerCell];
if ((column == highlightedTableColumn) ||
[tableView isColumnSelected: i])
{
[cell setHighlighted: YES];
}
if ((column == highlightedTableColumn) || [tableView isColumnSelected: index])
{
[cell setHighlighted: YES];
}
else
{
[cell setHighlighted: NO];
}
[cell drawWithFrame: drawingRect
inView: tableHeaderView];
{
[cell setHighlighted: NO];
}
[cell drawWithFrame: drawingRect inView: tableHeaderView];
drawingRect.origin.x += width;
}
// Fill out table header to end if needed...
//if ([[[GSTheme theme] name] isEqualToString: @"GSTheme"] == NO)
// This is really here to handle extending the table headers using the
// WinUXTheme (or one equivalent to) that writes directly to the MS windows
// device contexts...
NSRect clipFrame = [(NSClipView*)[tableView superview] documentVisibleRect];
CGFloat maxWidth = NSMaxX(clipFrame);
if (drawingRect.origin.x < maxWidth)
{
if (lastColumnToDraw == [tableView numberOfColumns] - 1)
{
// This is really here to handle extending the table headers using the
// WinUXTheme (or one equivalent to) that writes directly to the MS windows
// device contexts...
NSRect clipFrame = [(NSClipView*)[tableView superview] documentVisibleRect];
CGFloat maxWidth = NSMaxX(clipFrame);
if (drawingRect.origin.x < maxWidth)
{
drawingRect.size.width = maxWidth - drawingRect.origin.x;
column = [columns objectAtIndex: lastColumnToDraw];
cell = AUTORELEASE([[NSTableHeaderCell alloc] initTextCell:@""]);
[cell setHighlighted: NO];
#if 0
NSLog(@"%s:---> i: %d drawRect: %@", __PRETTY_FUNCTION__, -2, NSStringFromRect(drawingRect));
#endif
[cell drawWithFrame: drawingRect inView: tableHeaderView];
}
}
drawingRect.size.width = maxWidth - drawingRect.origin.x;
column = [columns lastObject];
cell = AUTORELEASE([[NSTableHeaderCell alloc] initTextCell:@""]);
[cell setHighlighted: NO];
[cell drawWithFrame: drawingRect inView: tableHeaderView];
}
}
@ -3062,8 +3054,6 @@ typedef enum {
NSInteger startingRow = [tableView rowAtPoint: NSMakePoint(minX, minY)];
NSInteger endingRow = [tableView rowAtPoint: NSMakePoint(minX, maxY)];
NSInteger startingColumn = [tableView columnAtPoint: NSMakePoint(minX, minY)];
NSInteger endingColumn = [tableView columnAtPoint: NSMakePoint(maxX, minY)];
NSGraphicsContext *ctxt = GSCurrentContext ();
NSColor *gridColor = [tableView gridColor];
@ -3074,12 +3064,6 @@ typedef enum {
if (endingRow == -1)
endingRow = numberOfRows - 1;
if (startingColumn == -1)
startingColumn = 0;
if (endingColumn == -1)
endingColumn = numberOfColumns - 1;
DPSgsave (ctxt);
[gridColor set];
@ -3088,25 +3072,33 @@ typedef enum {
{
NSInteger i;
for (i = startingRow; i <= endingRow; i++)
{
NSRect rowRect = [tableView rectOfRow: i];
rowRect.origin.y += rowRect.size.height - 1;
rowRect.size.height = 1;
NSRectFill(rowRect);
}
{
NSRect rowRect = [tableView rectOfRow: i];
rowRect.origin.y += rowRect.size.height - 1;
rowRect.size.height = 1;
NSRectFill(rowRect);
}
}
// Draw vertical lines
if (numberOfColumns > 0)
{
NSInteger i;
for (i = startingColumn; i <= endingColumn; i++)
{
NSRect colRect = [tableView rectOfColumn: i];
colRect.origin.x += colRect.size.width - 1;
colRect.size.width = 1;
NSRectFill(colRect);
}
CGFloat lastX = 0;
CGFloat maxX = NSMaxX([[tableView superview] bounds])-1;
NSTableColumn *column = nil;
NSArray *columns = [tableView _visibleColumns];
NSEnumerator *iter = [columns objectEnumerator];
while ((column = [iter nextObject]) && (lastX < maxX))
{
i = [[tableView tableColumns] indexOfObject: column];
NSRect colRect = [tableView rectOfColumn: i];
colRect.origin.x += colRect.size.width - 1;
colRect.size.width = 1;
NSRectFill(colRect);
lastX = colRect.origin.x;
}
}
DPSgrestore (ctxt);
@ -3237,17 +3229,12 @@ typedef enum {
row = [selectedRows indexGreaterThanIndex: row];
}
}
else // Selecting columns
else if ([selectedColumns count] > 0) // Selecting columns
{
NSUInteger selectedColumnsCount;
NSUInteger selectedColumnsCount = [selectedColumns count];
NSUInteger column;
NSInteger startingColumn, endingColumn;
selectedColumnsCount = [selectedColumns count];
if (selectedColumnsCount == 0)
return;
/* highlight selected columns */
startingColumn = [tableView columnAtPoint: NSMakePoint(NSMinX(clipRect), 0)];
endingColumn = [tableView columnAtPoint: NSMakePoint(NSMaxX(clipRect), 0)];
@ -3272,19 +3259,13 @@ typedef enum {
inView: (NSView *)view
{
NSTableView *tableView = (NSTableView *)view;
// int numberOfRows = [tableView numberOfRows];
int numberOfColumns = [tableView numberOfColumns];
// NSIndexSet *selectedRows = [tableView _selectedRowIndexes];
// NSColor *backgroundColor = [tableView backgroundColor];
id dataSource = [tableView dataSource];
CGFloat *columnOrigins = [tableView _columnOrigins];
int editedRow = [tableView editedRow];
int editedColumn = [tableView editedColumn];
int startingColumn;
int endingColumn;
NSRect drawingRect;
NSCell *cell;
int i;
CGFloat x_pos;
if (dataSource == nil)
@ -3326,34 +3307,14 @@ typedef enum {
{
/* Using columnAtPoint: here would make it called twice per row per drawn
rect - so we avoid it and do it natively */
NSArray *columns = [tableView _visibleColumns];
NSTableColumn *column = nil;
NSEnumerator *iter = [columns objectEnumerator];
/* Determine starting column as fast as possible */
x_pos = NSMinX (clipRect);
i = 0;
while ((i < numberOfColumns) && (x_pos > columnOrigins[i]))
{
i++;
}
startingColumn = (i - 1);
if (startingColumn == -1)
startingColumn = 0;
/* Determine ending column as fast as possible */
x_pos = NSMaxX (clipRect);
// Nota Bene: we do *not* reset i
while ((i < numberOfColumns) && (x_pos > columnOrigins[i]))
{
i++;
}
endingColumn = (i - 1);
if (endingColumn == -1)
endingColumn = numberOfColumns - 1;
/* Draw the row between startingColumn and endingColumn */
for (i = startingColumn; i <= endingColumn; i++)
/* Draw the rows */
while ((column = [iter nextObject]))
{
NSInteger i = [[tableView tableColumns] indexOfObject: column];
cell = [tableView preparedCellAtColumn: i row:rowIndex];
if (i == editedColumn && rowIndex == editedRow)
{

View file

@ -73,6 +73,10 @@
#import "AppKit/NSTableView.h"
#import "GSBindingHelpers.h"
@interface NSTableView (GNUstepPrivate)
- (void) _tableColumnDidChangeState: (NSTableColumn*)column;
@end
/**
<p>
NSTableColumn objects represent columns in NSTableViews.
@ -351,7 +355,13 @@ it resizes. */
Set whether the column is invisible or not. */
- (void) setHidden: (BOOL)hidden
{
_is_hidden = hidden;
// If the flag is changing...
if (_is_hidden != hidden)
{
// Save and notify the table to resize columns based on hidden ones...
_is_hidden = hidden;
[[self tableView] _tableColumnDidChangeState: self];
}
}
/**

View file

@ -142,6 +142,8 @@ static NSDragOperation currentDragOperation;
- (NSArray*) _visibleColumns;
- (NSArray*) _resizableColumns;
- (NSArray*) _hiddenColumns;
- (CGFloat) _columnOriginForColumn: (NSTableColumn*)column;
- (CGFloat) _columnOriginForColumnAtIndex: (NSInteger) columnIndex;
- (CGFloat) _currentColumnWidth: (NSArray*)columns remainingWidth: (CGFloat*)remainingWidth;
@end
@ -2125,7 +2127,6 @@ static void computeNewSelection
{
_columnOrigins = NSZoneMalloc (NSDefaultMallocZone (), sizeof (CGFloat));
}
_lastRemainingWidth = 0;
[self _resizeTableView];
[self tile];
@ -2167,7 +2168,6 @@ static void computeNewSelection
{
NSZoneFree (NSDefaultMallocZone (), _columnOrigins);
}
_lastRemainingWidth = 0;
[self _resizeTableView];
[self tile];
@ -4490,6 +4490,10 @@ static BOOL selectContiguousRegion(NSTableView *self,
return NSZeroRect;
}
// If not visible...
if ([[_tableColumns objectAtIndex: columnIndex] isHidden])
return NSZeroRect;
rect.origin.x = _columnOrigins[columnIndex];
rect.origin.y = _bounds.origin.y;
rect.size.width = [[_tableColumns objectAtIndex: columnIndex] width];
@ -4583,13 +4587,18 @@ This method is deprecated, use -columnIndexesInRect:. */
}
else
{
NSInteger i = 0;
NSInteger i = 1;
NSInteger index = 1;
while ((i < _numberOfColumns) && (aPoint.x >= _columnOrigins[i]))
for (i = 1; i < _numberOfColumns; ++i)
{
i++;
if ([[_tableColumns objectAtIndex: i] isHidden])
continue;
if (aPoint.x <= _columnOrigins[i])
break;
index++;
}
return i - 1;
return index - 1;
}
}
@ -4732,26 +4741,8 @@ This method is deprecated, use -columnIndexesInRect:. */
- (void) _tableColumnDidChangeState: (NSTableColumn*)column
{
if ([column isHidden])
{
if ([_tableColumns indexOfObject: column] == NSNotFound)
{
NSWarnMLog(@"column not found in visible list: %@", [column identifier]);
}
else
{
}
}
else
{
if ([_tableColumnsHidden indexOfObject: column] == NSNotFound)
{
NSWarnMLog(@"column not found in hidden list: %@", [column identifier]);
}
else
{
}
}
_lastRemainingWidth = 0;
[self _resizeTableView];
}
- (void) _resizeTableView
@ -5064,7 +5055,6 @@ This method is deprecated, use -columnIndexesInRect:. */
tb = [columns objectAtIndex: i];
remainingWidth -= [[columns objectAtIndex: i] width];
}
//remainingWidth -= _lastRemainingWidth;
// Avoid hidden/unresizable columns for resizing...
columns = AUTORELEASE([[self _resizableColumns] mutableCopy]);
@ -5100,7 +5090,6 @@ This method is deprecated, use -columnIndexesInRect:. */
percents[ counter ] = [tb maxWidth] - width;
}
totalPrecents += percents[ counter ];
++counter;
}
[columns removeObjectsInArray: removeCols];
@ -5256,23 +5245,38 @@ This method is deprecated, use -columnIndexesInRect:. */
if (_tilingDisabled == YES)
return;
if (_numberOfColumns > 0)
{
NSInteger i;
CGFloat width;
// Tile only VISIBLE columns, setting in hidden ones to zero...
NSArray *columns = [self tableColumns];
_columnOrigins[0] = _bounds.origin.x;
width = [[_tableColumns objectAtIndex: 0] width];
table_width += width;
for (i = 1; i < _numberOfColumns; i++)
if ([columns count] > 0)
{
NSInteger i = 0;
while ([[_tableColumns objectAtIndex: i] isHidden])
{
_columnOrigins[i] = _columnOrigins[i - 1] + width;
width = [[_tableColumns objectAtIndex: i] width];
table_width += width;
_columnOrigins[i++] = 0;
}
if (i < [columns count])
{
CGFloat lastOrigin = _bounds.origin.x;
CGFloat lastWidth = 0;
for ( ; i < [columns count]; i++)
{
if ([[columns objectAtIndex: i] isHidden])
{
_columnOrigins[i] = 0;
continue;
}
_columnOrigins[i] = lastOrigin + lastWidth;
lastWidth = [[columns objectAtIndex: i] width];
table_width += lastWidth;
lastOrigin = _columnOrigins[i];
}
}
}
/* + 1 for the last grid line */
table_height = (_numberOfRows * _rowHeight) + 1;
table_width = fmax(table_width, [_super_view bounds].size.width);
[self setFrameSize: NSMakeSize (table_width, table_height)];
[self setNeedsDisplay: YES];
@ -5323,7 +5327,7 @@ This method is deprecated, use -columnIndexesInRect:. */
- (void) drawBackgroundInClipRect: (NSRect)clipRect
{
[[GSTheme theme] drawTableViewBackgroundInClipRect: clipRect
[[GSTheme theme] drawTableViewBackgroundInClipRect: [_super_view bounds]
inView: self
withBackgroundColor: _backgroundColor];
}
@ -6171,8 +6175,11 @@ This method is deprecated, use -columnIndexesInRect:. */
- (void) _userResizedTableColumn: (NSInteger)index
width: (CGFloat)width
{
_lastRemainingWidth -= (width - [[_tableColumns objectAtIndex: index] width]);
CGFloat lastWidth = [[_tableColumns objectAtIndex: index] width];
[[_tableColumns objectAtIndex: index] setWidth: width];
// Resize the table columns...
//_lastRemainingWidth -= (width - [[_tableColumns objectAtIndex: index] width]);
[self _resizeTableView];
}
@ -6429,8 +6436,7 @@ This method is deprecated, use -columnIndexesInRect:. */
NSString *tableKey;
defaults = [NSUserDefaults standardUserDefaults];
tableKey = [NSString stringWithFormat: @"NSTableView Columns %@",
_autosaveName];
tableKey = [NSString stringWithFormat: @"NSTableView Columns %@", _autosaveName];
config = [defaults objectForKey: tableKey];
if (config != nil)
{
@ -7439,6 +7445,26 @@ For a more detailed explanation, -setSortDescriptors:. */
return [_tableColumns filteredArrayUsingPredicate: predicate];
}
- (CGFloat) _columnOriginForColumn: (NSTableColumn*)column;
{
if ([column isHidden])
return 0;
return [self _columnOriginForColumnAtIndex: [_tableColumns indexOfObject: column]];
}
- (CGFloat) _columnOriginForColumnAtIndex: (NSInteger) columnIndex
{
if (columnIndex == NSNotFound)
return 0;
if ((columnIndex < 0) || (columnIndex >= _numberOfColumns))
return 0;
NSTableColumn *column = [_tableColumns objectAtIndex: columnIndex];
if ([column isHidden])
return 0;
return _columnOrigins[ columnIndex ];
}
- (CGFloat) _currentColumnWidth: (NSArray*)columns remainingWidth: (CGFloat*)remainingWidth
{
NSInteger i = 0;
@ -7456,7 +7482,6 @@ For a more detailed explanation, -setSortDescriptors:. */
}
if (remainingWidth)
*remainingWidth = remWidth;
return currentWidth;
}