Skip to content

Commit

Permalink
Update code to pull the column and selected row properly
Browse files Browse the repository at this point in the history
  • Loading branch information
gcasa committed Oct 11, 2024
1 parent 245ec26 commit 427b9af
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 45 deletions.
2 changes: 2 additions & 0 deletions Headers/AppKit/NSBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
@class NSArray;
@class NSIndexPath;
@class NSIndexSet;
@class NSMutableDictionary;

@class NSCell;
@class NSEvent;
Expand Down Expand Up @@ -100,6 +101,7 @@ APPKIT_EXPORT_CLASS
NSBrowserColumnResizingType _columnResizing;

BOOL _itemBasedDelegate;
NSMutableDictionary *_columnDictionary;
}

//
Expand Down
136 changes: 91 additions & 45 deletions Source/NSBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -2269,6 +2269,7 @@ - (id) delegate
- (void) setDelegate: (id)anObject
{
BOOL flag = NO;
/*
GSKeyValueBinding *theBinding;
theBinding = [GSKeyValueBinding getBinding: NSContentBinding
Expand All @@ -2280,6 +2281,7 @@ - (void) setDelegate: (id)anObject
}
else
{
*/
/* Default to YES for nil delegate. */
_passiveDelegate = YES;
_itemBasedDelegate = NO;
Expand Down Expand Up @@ -2336,7 +2338,7 @@ - (void) setDelegate: (id)anObject
GSNameFromSelector
(@selector(browser:createRowsForColumn:inMatrix:))];
}
}
// }

_browserDelegate = anObject;
}
Expand Down Expand Up @@ -2580,6 +2582,7 @@ - (id) initWithFrame: (NSRect)rect

// Item based delegate, 10.6+
_itemBasedDelegate = NO;
_columnDictionary = [[NSMutableDictionary alloc] init];

[[NSNotificationCenter defaultCenter]
addObserver: self
Expand All @@ -2605,6 +2608,7 @@ - (void) dealloc
RELEASE(_pathSeparator);
RELEASE(_horizontalScroller);
RELEASE(_browserColumns);
RELEASE(_columnDictionary);
TEST_RELEASE(_charBuffer);

[super dealloc];
Expand Down Expand Up @@ -3031,7 +3035,8 @@ - (id) initWithCoder: (NSCoder*)aDecoder

// Item based delegate, 10.6+
_itemBasedDelegate = NO;

_columnDictionary = [[NSMutableDictionary alloc] init];

// Horizontal scroller
_scrollerRect.origin.x = bs.width;
_scrollerRect.origin.y = bs.height;
Expand Down Expand Up @@ -3318,13 +3323,13 @@ - (void) _remapColumnSubviews: (BOOL)fromFirst
- (id) _itemForColumn: (NSInteger)column
{
id item = nil;

GSKeyValueBinding *theBinding;
theBinding = [GSKeyValueBinding getBinding: NSContentBinding
forObject: self];
// NSNumber *colNum = nil;

if (column == 0)
{
GSKeyValueBinding *theBinding;
theBinding = [GSKeyValueBinding getBinding: NSContentBinding
forObject: self];

if (theBinding != nil)
{
item = [_browserDelegate rootItemForBrowser: self];
Expand All @@ -3351,8 +3356,21 @@ - (id) _itemForColumn: (NSInteger)column
if (selectedCells != nil && [selectedCells count] > 0)
{
id cell = [selectedCells objectAtIndex: 0];

item = [cell objectValue];

if (theBinding != nil)
{
NSNumber *colNum = [NSNumber numberWithInteger: col];
NSArray *array = [_columnDictionary objectForKey: colNum];
if ([array count] > 0)
{
NSInteger row = [self selectedRowInColumn: col];
item = [array objectAtIndex: row];
}
}
else
{
item = [cell objectValue];
}
}
}
}
Expand All @@ -3361,33 +3379,52 @@ - (id) _itemForColumn: (NSInteger)column
return item;
}

- (NSString *) _keyPathForValueBinding
{
NSString *keyPath = nil;
NSDictionary *info = [GSKeyValueBinding infoForBinding: NSContentValuesBinding
forObject: self];
if (info != nil)
{
NSString *ikp = [info objectForKey: NSObservedKeyPathKey];
NSUInteger location = [ikp rangeOfString: @"."].location;

keyPath = (location == NSNotFound ? ikp : [ikp substringFromIndex: location + 1]);
}

return keyPath;
}

/* Loads column 'column' (asking the delegate). */
- (void) _performLoadOfColumn: (NSInteger)column
{
NSBrowserColumn *bc;
NSScrollView *sc;
NSMatrix *matrix;
NSInteger i, rows, cols;
NSBrowserColumn *bc = nil;
NSScrollView *sc = nil;
NSMatrix *matrix = nil;
NSInteger i = 0, rows = 0, cols = 0;
id child = nil;
id item = nil;

NSNumber *colNum = nil;
NSTreeController *tc = nil;
NSArray *children = nil;

if (_itemBasedDelegate)
{
GSKeyValueBinding *theBinding;

theBinding = [GSKeyValueBinding getBinding: NSContentBinding
forObject: self];

item = [self _itemForColumn: column];

if (theBinding != nil)
{
id observedObject = [theBinding observedObject];
NSArray *children = nil;


rows = 0;
colNum = [NSNumber numberWithInteger: column];
if ([observedObject isKindOfClass: [NSTreeController class]])
{
NSTreeController *tc = (NSTreeController *)observedObject;
tc = (NSTreeController *)observedObject;

if (item == nil)
{
Expand Down Expand Up @@ -3427,6 +3464,12 @@ - (void) _performLoadOfColumn: (NSInteger)column
}
}
}

// If the node has children, add them to the column...
if (children != nil)
{
[_columnDictionary setObject: children forKey: colNum];
}
}
}
else
Expand Down Expand Up @@ -3500,39 +3543,42 @@ - (void) _performLoadOfColumn: (NSInteger)column
[sc setDocumentView: matrix];

// Loading is different based upon item/passive/active delegate
if (_itemBasedDelegate && item != nil)
if (_itemBasedDelegate == YES) // && item != nil && tc != nil)
{
GSKeyValueBinding *valueBinding;
NSString *childrenKeyPath = [tc childrenKeyPathForNode: item];

valueBinding = [GSKeyValueBinding getBinding: NSContentValuesBinding
forObject: self];
if (valueBinding != nil)
if (childrenKeyPath != nil)
{
NSTreeController *tc = [valueBinding observedObject];
NSString *childrenKeyPath = [tc childrenKeyPathForNode: item];

if (childrenKeyPath != nil)
NSString *leafKeyPath = [tc leafKeyPathForNode: item];
NSString *keyPath = [self _keyPathForValueBinding];

// Iterate over the children for the item....
for (i = 0; i < rows; i++)
{
NSString *leafKeyPath = [tc leafKeyPathForNode: item];
NSArray *children = [item valueForKeyPath: childrenKeyPath];

// Iterate over the children for the item....
for (i = 0; i < rows; i++)
id aCell = [matrix cellAtRow: i column: 0];
if (![aCell isLoaded])
{
id aCell = [matrix cellAtRow: i column: 0];
if (![aCell isLoaded])
BOOL leaf = YES;
id val = nil;
NSNumber *leafBool = [child valueForKeyPath: leafKeyPath];

child = [children objectAtIndex: i];
leaf = [leafBool boolValue];

// If a content values binding is present, it uses that key path,
// but if one isn't it uses the description... per documentation.
if (keyPath != nil)
{
val = [child valueForKeyPath: keyPath];
}
else
{
BOOL leaf = YES;
id val = nil;
NSNumber *leafBool = [child valueForKeyPath: leafKeyPath];

child = [children objectAtIndex: i];
leaf = [leafBool boolValue];
val = [child valueForKeyPath: @"value"]; // temporary
[aCell setLeaf: leaf];
[aCell setObjectValue: val];
[aCell setLoaded: YES];
val = [child description]; // per documentation.
}

[aCell setLeaf: leaf];
[aCell setObjectValue: val];
[aCell setLoaded: YES];
}
}
}
Expand Down

0 comments on commit 427b9af

Please sign in to comment.