From 427b9af2241398b7e14c7a79d083587e248f36d8 Mon Sep 17 00:00:00 2001 From: Gregory John Casamento Date: Fri, 11 Oct 2024 00:49:22 -0400 Subject: [PATCH] Update code to pull the column and selected row properly --- Headers/AppKit/NSBrowser.h | 2 + Source/NSBrowser.m | 136 +++++++++++++++++++++++++------------ 2 files changed, 93 insertions(+), 45 deletions(-) diff --git a/Headers/AppKit/NSBrowser.h b/Headers/AppKit/NSBrowser.h index 06257b217..3a622e80f 100644 --- a/Headers/AppKit/NSBrowser.h +++ b/Headers/AppKit/NSBrowser.h @@ -37,6 +37,7 @@ @class NSArray; @class NSIndexPath; @class NSIndexSet; +@class NSMutableDictionary; @class NSCell; @class NSEvent; @@ -100,6 +101,7 @@ APPKIT_EXPORT_CLASS NSBrowserColumnResizingType _columnResizing; BOOL _itemBasedDelegate; + NSMutableDictionary *_columnDictionary; } // diff --git a/Source/NSBrowser.m b/Source/NSBrowser.m index 95927fed8..6c8aa255c 100644 --- a/Source/NSBrowser.m +++ b/Source/NSBrowser.m @@ -2269,6 +2269,7 @@ - (id) delegate - (void) setDelegate: (id)anObject { BOOL flag = NO; + /* GSKeyValueBinding *theBinding; theBinding = [GSKeyValueBinding getBinding: NSContentBinding @@ -2280,6 +2281,7 @@ - (void) setDelegate: (id)anObject } else { + */ /* Default to YES for nil delegate. */ _passiveDelegate = YES; _itemBasedDelegate = NO; @@ -2336,7 +2338,7 @@ - (void) setDelegate: (id)anObject GSNameFromSelector (@selector(browser:createRowsForColumn:inMatrix:))]; } - } + // } _browserDelegate = anObject; } @@ -2580,6 +2582,7 @@ - (id) initWithFrame: (NSRect)rect // Item based delegate, 10.6+ _itemBasedDelegate = NO; + _columnDictionary = [[NSMutableDictionary alloc] init]; [[NSNotificationCenter defaultCenter] addObserver: self @@ -2605,6 +2608,7 @@ - (void) dealloc RELEASE(_pathSeparator); RELEASE(_horizontalScroller); RELEASE(_browserColumns); + RELEASE(_columnDictionary); TEST_RELEASE(_charBuffer); [super dealloc]; @@ -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; @@ -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]; @@ -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]; + } } } } @@ -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) { @@ -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 @@ -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]; } } }