Fix encoding/decoding, start rewriting refresh to align cells.

This commit is contained in:
Gregory John Casamento 2021-02-18 19:25:07 -05:00
parent eaf0351b78
commit b5c6e92dda
2 changed files with 63 additions and 86 deletions

View file

@ -66,8 +66,8 @@ APPKIT_EXPORT const CGFloat NSGridViewSizeForContent;
NSMutableArray *_cells; NSMutableArray *_cells;
CGFloat _columnSpacing; CGFloat _columnSpacing;
CGFloat _rowSpacing; CGFloat _rowSpacing;
CGFloat _xPlacement; NSUInteger _xPlacement;
CGFloat _yPlacement; NSUInteger _yPlacement;
} }
+ (instancetype) gridViewWithNumberOfColumns: (NSInteger)columnCount rows: (NSInteger)rowCount; + (instancetype) gridViewWithNumberOfColumns: (NSInteger)columnCount rows: (NSInteger)rowCount;

View file

@ -36,40 +36,16 @@
} }
} }
- (void) _redrawCells - (void) _refreshCells
{ {
NSUInteger r = 0, c = 0; NSUInteger i = 0;
FOR_IN(NSGridCell*, c, _cells)
NSDebugLog(@"Refresh cells in NSGridView");
for (r = 0; r < [self numberOfRows]; r++)
{ {
for (c = 0; c < [self numberOfColumns]; c++) NSView *v = [c contentView];
{ [self addSubview: v];
NSGridCell *cell = [self cellAtColumnIndex: c i++;
rowIndex: r];
if (cell != nil)
{
NSView *v = [cell contentView];
if (v != nil)
{
NSDebugLog(@"v = %@", v);
if ([v superview] == nil)
{
NSDebugLog(@"Add to view");
[self addSubview: v];
}
}
else
{
NSDebugLog(@"No view");
}
}
else
{
NSDebugLog(@"No cell");
}
}
} }
END_FOR_IN(_cells)
} }
- (instancetype) initWithFrame: (NSRect)frameRect - (instancetype) initWithFrame: (NSRect)frameRect
@ -82,7 +58,7 @@
_columns = [[NSMutableArray alloc] init]; _columns = [[NSMutableArray alloc] init];
} }
[self _redrawCells]; [self _refreshCells];
return self; return self;
} }
@ -93,8 +69,10 @@
NSUInteger c = 0; NSUInteger c = 0;
NSUInteger r = 0; NSUInteger r = 0;
if (self != nil) if (self != nil)
{ {
_cells = [[NSMutableArray alloc] init];
FOR_IN(NSArray*, row, rows) FOR_IN(NSArray*, row, rows)
{ {
NSArray *columns = [row objectAtIndex: c]; NSArray *columns = [row objectAtIndex: c];
@ -103,6 +81,7 @@
NSView *v = [column objectAtIndex: c]; NSView *v = [column objectAtIndex: c];
NSGridCell *cell = [[NSGridCell alloc] init]; NSGridCell *cell = [[NSGridCell alloc] init];
[cell setContentView: v]; [cell setContentView: v];
[_cells addObject: cell];
c++; c++;
} }
END_FOR_IN(columns); END_FOR_IN(columns);
@ -123,7 +102,7 @@
{ {
[_columns addObject: [[NSGridColumn alloc] init]]; [_columns addObject: [[NSGridColumn alloc] init]];
} }
[self _redrawCells]; [self _refreshCells];
return self; return self;
} }
@ -186,33 +165,25 @@
- (NSGridCell *) cellAtColumnIndex: (NSInteger)columnIndex rowIndex: (NSInteger)rowIndex - (NSGridCell *) cellAtColumnIndex: (NSInteger)columnIndex rowIndex: (NSInteger)rowIndex
{ {
return [[_cells objectAtIndex: rowIndex] objectAtIndex: columnIndex]; NSUInteger idx = rowIndex * [self numberOfColumns] + columnIndex;
return [_cells objectAtIndex: idx];
} }
- (NSGridCell *) cellForView: (NSView*)view - (NSGridCell *) cellForView: (NSView*)view
{ {
NSGridCell *result = nil; NSGridCell *result = nil;
if (view != nil) FOR_IN(NSGridCell*, c, _cells)
{ {
NSUInteger c = 0; NSView *v = [c contentView];
NSUInteger r = 0; if (v == view)
for(r = 0; r < [self numberOfRows]; r++)
{ {
for(c = 0; c < [self numberOfColumns]; c++) result = c;
{ break;
NSGridCell *cell = [self cellAtColumnIndex: c rowIndex: r];
NSView *v = [cell contentView];
if (v == view)
{
result = cell;
break;
}
}
} }
} }
END_FOR_IN(_cells);
return result; return result;
} }
@ -241,19 +212,19 @@
END_FOR_IN(views); END_FOR_IN(views);
// Refresh... // Refresh...
[self _redrawCells]; [self _refreshCells];
return gr; return gr;
} }
- (void) moveRowAtIndex: (NSInteger)fromIndex toIndex: (NSInteger)toIndex - (void) moveRowAtIndex: (NSInteger)fromIndex toIndex: (NSInteger)toIndex
{ {
[self _redrawCells]; [self _refreshCells];
} }
- (void) removeRowAtIndex: (NSInteger)index - (void) removeRowAtIndex: (NSInteger)index
{ {
[_rows removeObjectAtIndex: index]; [_rows removeObjectAtIndex: index];
[self _redrawCells]; [self _refreshCells];
} }
- (NSGridColumn *) addColumnWithViews: (NSArray*)views - (NSGridColumn *) addColumnWithViews: (NSArray*)views
@ -265,19 +236,19 @@
- (NSGridColumn *) insertColumnAtIndex: (NSInteger)index withViews: (NSArray *)views - (NSGridColumn *) insertColumnAtIndex: (NSInteger)index withViews: (NSArray *)views
{ {
NSGridColumn *gc = [[NSGridColumn alloc] init]; NSGridColumn *gc = [[NSGridColumn alloc] init];
[self _redrawCells]; [self _refreshCells];
return gc; return gc;
} }
- (void) moveColumnAtIndex: (NSInteger)fromIndex toIndex: (NSInteger)toIndex - (void) moveColumnAtIndex: (NSInteger)fromIndex toIndex: (NSInteger)toIndex
{ {
[self _redrawCells]; [self _refreshCells];
} }
- (void) removeColumnAtIndex: (NSInteger)index - (void) removeColumnAtIndex: (NSInteger)index
{ {
[_columns removeObjectAtIndex: index]; [_columns removeObjectAtIndex: index];
[self _redrawCells]; [self _refreshCells];
} }
- (NSGridCellPlacement) xPlacement - (NSGridCellPlacement) xPlacement
@ -332,7 +303,7 @@
- (void) mergeCellsInHorizontalRange: (NSRange)hRange verticalRange: (NSRange)vRange - (void) mergeCellsInHorizontalRange: (NSRange)hRange verticalRange: (NSRange)vRange
{ {
[self _redrawCells]; [self _refreshCells];
} }
// coding // coding
@ -361,17 +332,17 @@
{ {
[coder encodeValueOfObjCType:@encode(NSUInteger) [coder encodeValueOfObjCType:@encode(NSUInteger)
at:&_rowAlignment]; at:&_rowAlignment];
[coder encodeObject: _columns];
[coder encodeObject: _rows];
[coder encodeObject: _cells];
[coder encodeValueOfObjCType:@encode(CGFloat) [coder encodeValueOfObjCType:@encode(CGFloat)
at:&_columnSpacing]; at:&_columnSpacing];
[coder encodeObject: _columns];
[coder encodeValueOfObjCType:@encode(CGFloat) [coder encodeValueOfObjCType:@encode(CGFloat)
at:&_rowSpacing]; at:&_rowSpacing];
[coder encodeObject: _rows];
[coder encodeValueOfObjCType:@encode(NSUInteger) [coder encodeValueOfObjCType:@encode(NSUInteger)
at:&_xPlacement]; at:&_xPlacement];
[coder encodeValueOfObjCType:@encode(NSUInteger) [coder encodeValueOfObjCType:@encode(NSUInteger)
at:&_yPlacement]; at:&_yPlacement];
[coder encodeObject: _cells];
} }
} }
@ -380,63 +351,69 @@
self = [super initWithCoder: coder]; self = [super initWithCoder: coder];
if (self != nil) if (self != nil)
{ {
NSDebugLog(@"%@ %@",NSStringFromClass([self class]), NSStringFromSelector(_cmd)); NSLog(@"%@ %@",NSStringFromClass([self class]), NSStringFromSelector(_cmd));
if ([coder allowsKeyedCoding]) if ([coder allowsKeyedCoding])
{ {
if ([coder containsValueForKey: @"NSGrid_alignment"]) if ([coder containsValueForKey: @"NSGrid_alignment"])
{ {
_rowAlignment = [coder decodeIntegerForKey: @"NSGrid_alignment"]; _rowAlignment = (NSGridRowAlignment)[coder decodeIntegerForKey: @"NSGrid_alignment"];
} NSLog(@"_rowAlignment = %ld", _rowAlignment);
if ([coder containsValueForKey: @"NSGrid_columnSpacing"])
{
_columnSpacing = [coder decodeFloatForKey: @"NSGrid_columnSpacing"];
} }
if ([coder containsValueForKey: @"NSGrid_columns"]) if ([coder containsValueForKey: @"NSGrid_columns"])
{ {
ASSIGN(_columns, [coder decodeObjectForKey: @"NSGrid_columns"]); ASSIGN(_columns, [coder decodeObjectForKey: @"NSGrid_columns"]);
// NSDebugLog(@"_columns = %@", _columns); NSLog(@"_columns = %@", _columns);
}
if ([coder containsValueForKey: @"NSGrid_rowSpacing"])
{
_rowSpacing = [coder decodeFloatForKey: @"NSGrid_rowSpacing"];
} }
if ([coder containsValueForKey: @"NSGrid_rows"]) if ([coder containsValueForKey: @"NSGrid_rows"])
{ {
ASSIGN(_rows, [coder decodeObjectForKey: @"NSGrid_rows"]); ASSIGN(_rows, [coder decodeObjectForKey: @"NSGrid_rows"]);
// NSDebugLog(@"_rows = %@", _rows); NSLog(@"_rows = %@", _rows);
}
if ([coder containsValueForKey: @"NSGrid_xPlacement"])
{
_xPlacement = [coder decodeIntegerForKey: @"NSGrid_xPlacement"];
}
if ([coder containsValueForKey: @"NSGrid_yPlacement"])
{
_yPlacement = [coder decodeIntegerForKey: @"NSGrid_yPlacement"];
} }
if ([coder containsValueForKey: @"NSGrid_cells"]) if ([coder containsValueForKey: @"NSGrid_cells"])
{ {
ASSIGN(_cells, [coder decodeObjectForKey: @"NSGrid_cells"]); ASSIGN(_cells, [coder decodeObjectForKey: @"NSGrid_cells"]);
NSLog(@"_cells = %@", _cells);
}
if ([coder containsValueForKey: @"NSGrid_columnSpacing"])
{
_columnSpacing = [coder decodeFloatForKey: @"NSGrid_columnSpacing"];
NSLog(@"_columnSpacing = %f", _columnSpacing);
}
if ([coder containsValueForKey: @"NSGrid_rowSpacing"])
{
_rowSpacing = [coder decodeFloatForKey: @"NSGrid_rowSpacing"];
NSLog(@"_rowSpacing = %f", _rowSpacing);
}
if ([coder containsValueForKey: @"NSGrid_xPlacement"])
{
_xPlacement = [coder decodeIntegerForKey: @"NSGrid_xPlacement"];
NSLog(@"_xPlacement = %ld", _xPlacement);
}
if ([coder containsValueForKey: @"NSGrid_yPlacement"])
{
_yPlacement = [coder decodeIntegerForKey: @"NSGrid_yPlacement"];
NSLog(@"_yPlacement = %ld", _yPlacement);
} }
} }
else else
{ {
[coder decodeValueOfObjCType:@encode(NSUInteger) [coder decodeValueOfObjCType:@encode(NSUInteger)
at:&_rowAlignment]; at:&_rowAlignment];
ASSIGN(_columns, [coder decodeObject]);
ASSIGN(_rows, [coder decodeObject]);
ASSIGN(_cells, [coder decodeObject]);
[coder decodeValueOfObjCType:@encode(CGFloat) [coder decodeValueOfObjCType:@encode(CGFloat)
at:&_columnSpacing]; at:&_columnSpacing];
ASSIGN(_columns, [coder decodeObject]);
[coder decodeValueOfObjCType:@encode(CGFloat) [coder decodeValueOfObjCType:@encode(CGFloat)
at:&_rowSpacing]; at:&_rowSpacing];
ASSIGN(_rows, [coder decodeObject]);
[coder decodeValueOfObjCType:@encode(NSUInteger) [coder decodeValueOfObjCType:@encode(NSUInteger)
at:&_xPlacement]; at:&_xPlacement];
[coder decodeValueOfObjCType:@encode(NSUInteger) [coder decodeValueOfObjCType:@encode(NSUInteger)
at:&_yPlacement]; at:&_yPlacement];
ASSIGN(_cells, [coder decodeObject]);
} }
} }
[self _redrawCells]; [self _refreshCells];
return self; return self;
} }