5 // Created by Alex Rozanski on 05/09/2009.
6 // Copyright 2009-10 Alex Rozanski http://perspx.com
8 // GC-enabled code revised by Stefan Vogt http://byteproject.net
11 #import "PXSourceList.h"
12 #import "SideBarItem.h"
15 #define MIN_BADGE_WIDTH 22.0 //The minimum badge width for each item (default 22.0)
16 #define BADGE_HEIGHT 14.0 //The badge height for each item (default 14.0)
17 #define BADGE_MARGIN 5.0 //The spacing between the badge and the cell for that row
18 #define ROW_RIGHT_MARGIN 5.0 //The spacing between the right edge of the badge and the edge of the table column
19 #define ICON_SPACING 2.0 //The spacing between the icon and it's adjacent cell
20 #define DISCLOSURE_TRIANGLE_SPACE 18.0 //The indentation reserved for disclosure triangles for non-group items
23 #define BADGE_BACKGROUND_COLOR [NSColor colorWithCalibratedRed:(152/255.0) green:(168/255.0) blue:(202/255.0) alpha:1]
24 #define BADGE_HIDDEN_BACKGROUND_COLOR [NSColor colorWithDeviceWhite:(180/255.0) alpha:1]
25 #define BADGE_SELECTED_TEXT_COLOR [NSColor keyboardFocusIndicatorColor]
26 #define BADGE_SELECTED_UNFOCUSED_TEXT_COLOR [NSColor colorWithCalibratedRed:(153/255.0) green:(169/255.0) blue:(203/255.0) alpha:1]
27 #define BADGE_SELECTED_HIDDEN_TEXT_COLOR [NSColor colorWithCalibratedWhite:(170/255.0) alpha:1]
28 #define BADGE_FONT [NSFont boldSystemFontOfSize:11]
30 //Delegate notification constants
31 NSString * const PXSLSelectionIsChangingNotification = @"PXSourceListSelectionIsChanging";
32 NSString * const PXSLSelectionDidChangeNotification = @"PXSourceListSelectionDidChange";
33 NSString * const PXSLItemWillExpandNotification = @"PXSourceListItemWillExpand";
34 NSString * const PXSLItemDidExpandNotification = @"PXSourceListItemDidExpand";
35 NSString * const PXSLItemWillCollapseNotification = @"PXSourceListItemWillCollapse";
36 NSString * const PXSLItemDidCollapseNotification = @"PXSourceListItemDidCollapse";
37 NSString * const PXSLDeleteKeyPressedOnRowsNotification = @"PXSourceListDeleteKeyPressedOnRows";
40 @interface PXSourceList ()
42 - (NSSize)sizeOfBadgeAtRow:(NSInteger)rowIndex;
43 - (void)drawBadgeForRow:(NSInteger)rowIndex inRect:(NSRect)badgeFrame;
44 - (void)registerDelegateToReceiveNotification:(NSString*)notification withSelector:(SEL)selector;
49 @implementation PXSourceList
51 @synthesize iconSize = _iconSize;
55 #pragma mark Init/Dealloc/Finalize
57 - (id)initWithCoder:(NSCoder*)decoder
59 if(self=[super initWithCoder:decoder])
61 [self setDelegate:(id<PXSourceListDelegate>)[super delegate]];
62 [super setDelegate:self];
63 [self setDataSource:(id<PXSourceListDataSource>)[super dataSource]];
64 [super setDataSource:self];
66 _iconSize = NSMakeSize(16,16);
74 //Remove ourselves as the delegate and data source to be safe
75 [super setDataSource:nil];
76 [super setDelegate:nil];
78 //Unregister the delegate from receiving notifications
79 [[NSNotificationCenter defaultCenter] removeObserver:_secondaryDelegate name:nil object:self];
86 //Remove ourselves as the delegate and data source to be safe
87 [super setDataSource:nil];
88 [super setDelegate:nil];
90 //Unregister the delegate from receiving notifications
91 [[NSNotificationCenter defaultCenter] removeObserver:_secondaryDelegate name:nil object:self];
97 #pragma mark Custom Accessors
99 - (void)setDelegate:(id<PXSourceListDelegate>)aDelegate
101 //Unregister the old delegate from receiving notifications
102 [[NSNotificationCenter defaultCenter] removeObserver:_secondaryDelegate name:nil object:self];
104 _secondaryDelegate = aDelegate;
105 //Register the new delegate to receive notifications
106 [self registerDelegateToReceiveNotification:PXSLSelectionIsChangingNotification withSelector:@selector(sourceListSelectionIsChanging:)];
107 [self registerDelegateToReceiveNotification:PXSLSelectionDidChangeNotification withSelector:@selector(sourceListSelectionDidChange:)];
108 [self registerDelegateToReceiveNotification:PXSLItemWillExpandNotification withSelector:@selector(sourceListItemWillExpand:)];
109 [self registerDelegateToReceiveNotification:PXSLItemDidExpandNotification withSelector:@selector(sourceListItemDidExpand:)];
110 [self registerDelegateToReceiveNotification:PXSLItemWillCollapseNotification withSelector:@selector(sourceListItemWillCollapse:)];
111 [self registerDelegateToReceiveNotification:PXSLItemDidCollapseNotification withSelector:@selector(sourceListItemDidCollapse:)];
112 [self registerDelegateToReceiveNotification:PXSLDeleteKeyPressedOnRowsNotification withSelector:@selector(sourceListDeleteKeyPressedOnRows:)];
116 - (void)setDataSource:(id<PXSourceListDataSource>)aDataSource
118 _secondaryDataSource = aDataSource;
123 - (void)setIconSize:(NSSize)newIconSize
125 _iconSize = newIconSize;
126 CGFloat rowHeight = [self rowHeight];
128 //Make sure icon height does not exceed row height; if so constrain, keeping width and height in proportion
129 if(_iconSize.height>rowHeight)
131 _iconSize.width = _iconSize.width * (rowHeight/_iconSize.height);
132 _iconSize.height = rowHeight;
137 #pragma mark Data Management
143 //Expand items that are displayed as always expanded
144 if([_secondaryDataSource conformsToProtocol:@protocol(PXSourceListDataSource)] &&
145 [_secondaryDelegate respondsToSelector:@selector(sourceList:isGroupAlwaysExpanded:)])
147 for(NSUInteger i=0;i<[self numberOfGroups];i++)
149 id item = [_secondaryDataSource sourceList:self child:i ofItem:nil];
151 if([self isGroupAlwaysExpanded:item]) {
152 [self expandItem:item expandChildren:NO];
157 //If there are selected rows and the item hierarchy has changed, make sure a Group row isn't
159 if([self numberOfSelectedRows]>0) {
160 NSIndexSet *selectedIndexes = [self selectedRowIndexes];
161 NSUInteger firstSelectedRow = [selectedIndexes firstIndex];
163 //Is a group item selected?
164 if([self isGroupItem:[self itemAtRow:firstSelectedRow]]) {
165 //Work backwards to find the first non-group row
167 for(NSUInteger i=firstSelectedRow;i>0;i--)
169 if(![self isGroupItem:[self itemAtRow:i]]) {
170 [self selectRowIndexes:[NSIndexSet indexSetWithIndex:i] byExtendingSelection:NO];
176 //If there is no non-group row preceding the currently selected group item, remove the selection
177 //from the Source List
179 [self deselectAll:self];
183 else if(![self allowsEmptySelection]&&[self numberOfSelectedRows]==0)
185 //Select the first non-group row if no rows are selected, and empty selection is disallowed
186 for(NSUInteger i=0;i<[self numberOfRows];i++)
188 if(![self isGroupItem:[self itemAtRow:i]]) {
189 [self selectRowIndexes:[NSIndexSet indexSetWithIndex:i] byExtendingSelection:NO];
196 - (NSUInteger)numberOfGroups
198 if([_secondaryDataSource respondsToSelector:@selector(sourceList:numberOfChildrenOfItem:)]) {
199 return [_secondaryDataSource sourceList:self numberOfChildrenOfItem:nil];
205 - (BOOL)isGroupItem:(id)item
207 //Groups are defined as root items (at level 0)
208 return 0==[self levelForItem:item];
212 - (BOOL)isGroupAlwaysExpanded:(id)group
214 //Make sure that the item IS a group to prevent unwanted queries sent to the data source
215 if([self isGroupItem:group]) {
216 //Query the data source
217 if([_secondaryDelegate respondsToSelector:@selector(sourceList:isGroupAlwaysExpanded:)]) {
218 return [_secondaryDelegate sourceList:self isGroupAlwaysExpanded:group];
226 - (BOOL)itemHasBadge:(id)item
228 if([_secondaryDataSource respondsToSelector:@selector(sourceList:itemHasBadge:)]) {
229 return [_secondaryDataSource sourceList:self itemHasBadge:item];
235 - (NSInteger)badgeValueForItem:(id)item
237 //Make sure that the item has a badge
238 if(![self itemHasBadge:item]) {
242 if([_secondaryDataSource respondsToSelector:@selector(sourceList:badgeValueForItem:)]) {
243 return [_secondaryDataSource sourceList:self badgeValueForItem:item];
250 #pragma mark Selection Handling
252 - (void)selectRowIndexes:(NSIndexSet*)indexes byExtendingSelection:(BOOL)extend
254 NSUInteger numberOfIndexes = [indexes count];
256 //Prevent empty selection if we don't want it
257 if(![self allowsEmptySelection]&&0==numberOfIndexes) {
261 //Would use blocks but we're also targeting 10.5...
262 //Get the selected indexes
263 NSUInteger *selectedIndexes = malloc(sizeof(NSUInteger)*numberOfIndexes);
264 [indexes getIndexes:selectedIndexes maxCount:numberOfIndexes inIndexRange:nil];
266 //Loop through the indexes and only add non-group row indexes
267 //Allows selection across groups without selecting the group rows
268 NSMutableIndexSet *newSelectionIndexes = [NSMutableIndexSet indexSet];
269 for(NSInteger i=0;i<numberOfIndexes;i++)
271 if(![self isGroupItem:[self itemAtRow:selectedIndexes[i]]]) {
272 [newSelectionIndexes addIndex:selectedIndexes[i]];
276 //If there are any non-group rows selected
277 if([newSelectionIndexes count]>0) {
278 [super selectRowIndexes:newSelectionIndexes byExtendingSelection:extend];
281 //C memory management... *sigh*
282 free(selectedIndexes);
288 - (NSRect)frameOfOutlineCellAtRow:(NSInteger)row
290 //Return a zero-rect if the item is always expanded (a disclosure triangle will not be drawn)
291 if([self isGroupAlwaysExpanded:[self itemAtRow:row]]) {
295 return [super frameOfOutlineCellAtRow:row];
299 - (NSRect)frameOfCellAtColumn:(NSInteger)column row:(NSInteger)row
301 id item = [self itemAtRow:row];
303 NSCell *cell = [self preparedCellAtColumn:column row:row];
304 NSSize cellSize = [cell cellSize];
305 if (!([cell type] == NSImageCellType) && !([cell type] == NSTextCellType))
306 cellSize = [cell cellSizeForBounds:[super frameOfCellAtColumn:column row:row]];
308 NSRect cellFrame = [super frameOfCellAtColumn:column row:row];
309 NSRect rowRect = [self rectOfRow:row];
311 if([self isGroupItem:item])
313 CGFloat minX = NSMinX(cellFrame);
315 //Set the origin x-coord; if there are no children of the group at current, there will still be a
316 //margin to the left of the cell (in cellFrame), which we don't want
317 if([self isGroupAlwaysExpanded:[self itemAtRow:row]]) {
321 return NSMakeRect(minX, NSMidY(cellFrame)-(cellSize.height/2.0), NSWidth(rowRect)-minX, cellSize.height);
325 CGFloat leftIndent = ([self levelForRow:row] -1)*[self indentationPerLevel]+DISCLOSURE_TRIANGLE_SPACE;
327 //Calculate space left for a badge if need be
328 CGFloat rightIndent = [self sizeOfBadgeAtRow:row].width+ROW_RIGHT_MARGIN;
330 //Allow space for an icon if need be
331 if(![self isGroupItem:item]&&[_secondaryDataSource respondsToSelector:@selector(sourceList:itemHasIcon:)])
333 if([_secondaryDataSource sourceList:self itemHasIcon:item]) {
334 leftIndent += [self iconSize].width+(ICON_SPACING*2);
337 return NSMakeRect(leftIndent, NSMidY(rowRect)-(cellSize.height/2.0), NSWidth(rowRect)-rightIndent-leftIndent, cellSize.height);
342 //This method calculates and returns the size of the badge for the row index passed to the method. If the
343 //row for the row index passed to the method does not have a badge, then NSZeroSize is returned.
344 - (NSSize)sizeOfBadgeAtRow:(NSInteger)rowIndex
346 id rowItem = [self itemAtRow:rowIndex];
348 //Make sure that the item has a badge
349 if(![self itemHasBadge:rowItem]) {
353 NSAttributedString *badgeAttrString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%ld", [self badgeValueForItem:rowItem]] attributes:[NSDictionary dictionaryWithObjectsAndKeys:BADGE_FONT, NSFontAttributeName, nil]];
354 NSSize stringSize = [badgeAttrString size];
356 //Calculate the width needed to display the text or the minimum width if it's smaller
357 CGFloat width = stringSize.width+(2*BADGE_MARGIN);
359 if(width<MIN_BADGE_WIDTH) {
360 width = MIN_BADGE_WIDTH;
363 [badgeAttrString release];
365 return NSMakeSize(width, BADGE_HEIGHT);
372 - (void)drawRow:(NSInteger)rowIndex clipRect:(NSRect)clipRect
374 [super drawRow:rowIndex clipRect:clipRect];
375 id item = [self itemAtRow:rowIndex];
377 //Draw an icon if the item has one
378 if(![self isGroupItem:item]&&[_secondaryDataSource respondsToSelector:@selector(sourceList:itemHasIcon:)])
380 if([_secondaryDataSource sourceList:self itemHasIcon:item])
382 NSRect cellFrame = [self frameOfCellAtColumn:0 row:rowIndex];
383 NSSize iconSize = [self iconSize];
384 NSRect iconRect = NSMakeRect(NSMinX(cellFrame)-iconSize.width-ICON_SPACING, NSMidY(cellFrame)-(iconSize.width/2.0f), iconSize.width, iconSize.height);
386 if([_secondaryDataSource respondsToSelector:@selector(sourceList:iconForItem:)])
388 NSImage *icon = [_secondaryDataSource sourceList:self iconForItem:item];
391 NSSize actualIconSize = [icon size];
392 //If the icon is *smaller* than the size retrieved from the -iconSize property, make sure we
393 //reduce the size of the rectangle to draw the icon in, so that it is not stretched.
394 if((actualIconSize.width<iconSize.width)||(actualIconSize.height<iconSize.height))
396 iconRect = NSMakeRect(NSMidX(iconRect)-(actualIconSize.width/2.0f), NSMidY(iconRect)-(actualIconSize.height/2.0f), actualIconSize.width, actualIconSize.height);
399 //Use 10.6 NSImage drawing if we can
400 if(NSAppKitVersionNumber >= 1115.2) { // Lion
401 [icon drawInRect:iconRect
403 operation:NSCompositeSourceOver
405 respectFlipped:YES hints:nil];
408 [icon setFlipped:[self isFlipped]];
409 [icon drawInRect:iconRect
411 operation:NSCompositeSourceOver
419 //Draw the badge if the item has one
420 if([self itemHasBadge:item])
422 NSRect rowRect = [self rectOfRow:rowIndex];
423 NSSize badgeSize = [self sizeOfBadgeAtRow:rowIndex];
425 NSRect badgeFrame = NSMakeRect(NSMaxX(rowRect)-badgeSize.width-ROW_RIGHT_MARGIN,
426 NSMidY(rowRect)-(badgeSize.height/2.0),
429 [self drawBadgeForRow:rowIndex inRect:badgeFrame];
433 - (void)drawBadgeForRow:(NSInteger)rowIndex inRect:(NSRect)badgeFrame
435 id rowItem = [self itemAtRow:rowIndex];
436 NSBezierPath *badgePath = [NSBezierPath bezierPathWithRoundedRect:badgeFrame
437 xRadius:(BADGE_HEIGHT/2.0)
438 yRadius:(BADGE_HEIGHT/2.0)];
440 //Get window and control state to determine colours used
441 BOOL isVisible = [[NSApp mainWindow] isVisible];
442 BOOL isFocused = [[[self window] firstResponder] isEqual:self];
443 NSInteger rowBeingEdited = [self editedRow];
445 //Set the attributes based on the row state
446 NSDictionary *attributes;
447 NSColor *backgroundColor;
449 if([[self selectedRowIndexes] containsIndex:rowIndex])
451 backgroundColor = [NSColor whiteColor];
452 //Set the text color based on window and control state
454 if(isVisible && (isFocused || rowBeingEdited==rowIndex)) {
455 textColor = BADGE_SELECTED_TEXT_COLOR;
457 else if(isVisible && !isFocused) {
458 textColor = BADGE_SELECTED_UNFOCUSED_TEXT_COLOR;
461 textColor = BADGE_SELECTED_HIDDEN_TEXT_COLOR;
464 attributes = [[NSDictionary alloc] initWithObjectsAndKeys:BADGE_FONT, NSFontAttributeName, textColor, NSForegroundColorAttributeName, nil];
468 //Set the text colour based on window and control state
469 NSColor *badgeColor = [NSColor whiteColor];
472 //If the data source returns a custom colour..
473 if([_secondaryDataSource respondsToSelector:@selector(sourceList:badgeBackgroundColorForItem:)]) {
474 backgroundColor = [_secondaryDataSource sourceList:self badgeBackgroundColorForItem:rowItem];
476 if(backgroundColor==nil)
477 backgroundColor = BADGE_BACKGROUND_COLOR;
479 else { //Otherwise use the default (purple-blue colour)
480 backgroundColor = BADGE_BACKGROUND_COLOR;
483 //If the delegate wants a custom badge text colour..
484 if([_secondaryDataSource respondsToSelector:@selector(sourceList:badgeTextColorForItem:)]) {
485 badgeColor = [_secondaryDataSource sourceList:self badgeTextColorForItem:rowItem];
488 badgeColor = [NSColor whiteColor];
492 backgroundColor = BADGE_HIDDEN_BACKGROUND_COLOR;
494 attributes = [[NSDictionary alloc] initWithObjectsAndKeys:BADGE_FONT, NSFontAttributeName, badgeColor, NSForegroundColorAttributeName, nil];
497 [backgroundColor set];
500 //Draw the badge text
501 NSAttributedString *badgeAttrString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%ld", [self badgeValueForItem:rowItem]] attributes:attributes];
503 NSSize stringSize = [badgeAttrString size];
504 NSPoint badgeTextPoint = NSMakePoint(NSMidX(badgeFrame)-(stringSize.width/2.0), //Center in the badge frame
505 NSMidY(badgeFrame)-(stringSize.height/2.0)); //Center in the badge frame
506 [badgeAttrString drawAtPoint:badgeTextPoint];
507 [attributes release];
508 [badgeAttrString release];
512 #pragma mark Keyboard Handling
514 - (void)keyDown:(NSEvent *)theEvent
516 NSIndexSet *selectedIndexes = [self selectedRowIndexes];
517 NSString *keyCharacters = [theEvent characters];
519 //Make sure we have a selection
520 if([selectedIndexes count]>0)
522 if([keyCharacters length]>0)
524 unichar firstKey = [keyCharacters characterAtIndex:0];
525 if(firstKey==NSUpArrowFunctionKey||firstKey==NSDownArrowFunctionKey)
527 //Handle keyboard navigation across groups
528 if([selectedIndexes count]==1&&!([theEvent modifierFlags] & NSShiftKeyMask))
530 int delta = firstKey==NSDownArrowFunctionKey?1:-1;
531 //Search "backwards" if up arrow, "forwards" if down
533 NSInteger newRow = [selectedIndexes firstIndex];
534 //Keep incrementing/decrementing the row until a non-header row is reached
538 //If out of bounds of the number of rows..
539 if(newRow<0||newRow==[self numberOfRows])
541 } while([self isGroupItem:[self itemAtRow:newRow]]);
543 [self selectRowIndexes:[NSIndexSet indexSetWithIndex:newRow] byExtendingSelection:NO];
548 else if(firstKey==NSDeleteCharacter||firstKey==NSBackspaceCharacter)
550 //Post the notification
551 [[NSNotificationCenter defaultCenter] postNotificationName:PXSLDeleteKeyPressedOnRowsNotification object:self userInfo:[NSDictionary dictionaryWithObject:selectedIndexes forKey:@"rows"]];
561 //We don't care about it
562 [super keyDown:theEvent];
566 #pragma mark Menu Handling
569 - (NSMenu *)menuForEvent:(NSEvent *)theEvent
573 if([_secondaryDelegate respondsToSelector:@selector(sourceList:menuForEvent:item:)]) {
574 NSPoint clickPoint = [self convertPoint:[theEvent locationInWindow] fromView:nil];
575 NSInteger row = [self rowAtPoint:clickPoint];
576 id clickedItem = [self itemAtRow:row];
578 if ([clickedItem sdtype] > 0)
579 m = [_secondaryDelegate sourceList:self menuForEvent:theEvent item:clickedItem];
581 m = [super menuForEvent:theEvent];
585 m = [super menuForEvent:theEvent];
592 #pragma mark NSOutlineView Data Source methods
594 - (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
596 if([_secondaryDataSource conformsToProtocol:@protocol(PXSourceListDataSource)]) {
597 return [_secondaryDataSource sourceList:self numberOfChildrenOfItem:item];
604 - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
606 if([_secondaryDataSource conformsToProtocol:@protocol(PXSourceListDataSource)]) {
607 return [_secondaryDataSource sourceList:self child:index ofItem:item];
614 - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
616 if([_secondaryDataSource conformsToProtocol:@protocol(PXSourceListDataSource)]) {
617 return [_secondaryDataSource sourceList:self isItemExpandable:item];
624 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
626 if([_secondaryDataSource conformsToProtocol:@protocol(PXSourceListDataSource)]) {
627 return [_secondaryDataSource sourceList:self objectValueForItem:item];
634 - (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
636 if([_secondaryDataSource conformsToProtocol:@protocol(PXSourceListDataSource)]) {
637 [_secondaryDataSource sourceList:self setObjectValue:object forItem:item];
642 - (id)outlineView:(NSOutlineView *)outlineView itemForPersistentObject:(id)object
644 if([_secondaryDataSource respondsToSelector:@selector(sourceList:itemForPersistentObject:)]) {
645 return [_secondaryDataSource sourceList:self itemForPersistentObject:object];
651 - (id)outlineView:(NSOutlineView *)outlineView persistentObjectForItem:(id)item
653 if([_secondaryDataSource respondsToSelector:@selector(sourceList:persistentObjectForItem:)]) {
654 return [_secondaryDataSource sourceList:self persistentObjectForItem:item];
660 - (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pasteboard
662 if([_secondaryDataSource respondsToSelector:@selector(sourceList:writeItems:toPasteboard:)]) {
663 return [_secondaryDataSource sourceList:self writeItems:items toPasteboard:pasteboard];
669 - (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
671 if([_secondaryDataSource respondsToSelector:@selector(sourceList:validateDrop:proposedItem:proposedChildIndex:)]) {
672 return [_secondaryDataSource sourceList:self validateDrop:info proposedItem:item proposedChildIndex:index];
675 return NSDragOperationNone;
678 - (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index
680 if([_secondaryDataSource respondsToSelector:@selector(sourceList:acceptDrop:item:childIndex:)]) {
681 return [_secondaryDataSource sourceList:self acceptDrop:info item:item childIndex:index];
686 - (NSArray *)outlineView:(NSOutlineView *)outlineView namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination forDraggedItems:(NSArray *)items
688 if([_secondaryDataSource respondsToSelector:@selector(sourceList:namesOfPromisedFilesDroppedAtDestination:forDraggedItems:)]) {
689 return [_secondaryDataSource sourceList:self namesOfPromisedFilesDroppedAtDestination:dropDestination forDraggedItems:items];
697 #pragma mark NSOutlineView Delegate methods
699 - (BOOL)outlineView:(NSOutlineView *)outlineView shouldExpandItem:(id)item
701 if([_secondaryDelegate respondsToSelector:@selector(sourceList:shouldExpandItem:)]) {
702 return [_secondaryDelegate sourceList:self shouldExpandItem:item];
708 - (BOOL)outlineView:(NSOutlineView *)outlineView shouldCollapseItem:(id)item
710 //Make sure the item isn't displayed as always expanded
711 if([self isGroupItem:item])
713 if([self isGroupAlwaysExpanded:item]) {
718 if([_secondaryDelegate respondsToSelector:@selector(sourceList:shouldCollapseItem:)]) {
719 return [_secondaryDelegate sourceList:self shouldCollapseItem:item];
725 - (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item
727 if([_secondaryDelegate respondsToSelector:@selector(sourceList:dataCellForItem:)]) {
728 return [_secondaryDelegate sourceList:self dataCellForItem:item];
731 NSInteger row = [self rowForItem:item];
733 //Return the default table column
734 return [[[self tableColumns] objectAtIndex:0] dataCellForRow:row];
737 - (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
739 if([_secondaryDelegate respondsToSelector:@selector(sourceList:willDisplayCell:forItem:)]) {
740 [_secondaryDelegate sourceList:self willDisplayCell:cell forItem:item];
744 - (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
746 //Make sure that the item isn't a group as they can't be selected
747 if(![self isGroupItem:item]) {
748 if([_secondaryDelegate respondsToSelector:@selector(sourceList:shouldSelectItem:)]) {
749 return [_secondaryDelegate sourceList:self shouldSelectItem:item];
760 - (NSIndexSet *)outlineView:(NSOutlineView *)outlineView selectionIndexesForProposedSelection:(NSIndexSet *)proposedSelectionIndexes
762 //The outline view will try to select the first row if -[allowsEmptySelection:] is set to NO – if this is a group row
763 //stop it from doing so and leave it to our implementation of-[reloadData] which will select the first non-group row
765 if([self numberOfSelectedRows]==0) {
766 if([self isGroupItem:[self itemAtRow:[proposedSelectionIndexes firstIndex]]]) {
767 return [NSIndexSet indexSet];
771 if([_secondaryDelegate respondsToSelector:@selector(sourceList:selectionIndexesForProposedSelection:)]) {
772 return [_secondaryDelegate sourceList:self selectionIndexesForProposedSelection:proposedSelectionIndexes];
775 //Since we implement this method, something must be returned to the outline view
776 return proposedSelectionIndexes;
779 - (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
781 //Group titles can't be edited
782 if([self isGroupItem:item])
785 if([_secondaryDelegate respondsToSelector:@selector(sourceList:shouldEditItem:)]) {
786 return [_secondaryDelegate sourceList:self shouldEditItem:item];
793 - (BOOL)outlineView:(NSOutlineView *)outlineView shouldTrackCell:(NSCell *)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
795 if([_secondaryDelegate respondsToSelector:@selector(sourceList:shouldTrackCell:forItem:)]) {
796 return [_secondaryDelegate sourceList:self shouldTrackCell:cell forItem:item];
802 - (CGFloat)outlineView:(NSOutlineView *)outlineView heightOfRowByItem:(id)item
804 if([_secondaryDelegate respondsToSelector:@selector(sourceList:heightOfRowByItem:)]) {
805 return [_secondaryDelegate sourceList:self heightOfRowByItem:item];
808 return [self rowHeight];
811 - (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item
813 return [self isGroupItem:item];
817 #pragma mark Notification handling
819 /* Notification wrappers */
820 - (void)outlineViewSelectionIsChanging:(NSNotification *)notification
822 [[NSNotificationCenter defaultCenter] postNotificationName:PXSLSelectionIsChangingNotification object:self];
826 - (void)outlineViewSelectionDidChange:(NSNotification *)notification
828 [[NSNotificationCenter defaultCenter] postNotificationName:PXSLSelectionDidChangeNotification object:self];
831 - (void)outlineViewItemWillExpand:(NSNotification *)notification
833 [[NSNotificationCenter defaultCenter] postNotificationName:PXSLItemWillExpandNotification object:self userInfo:[notification userInfo]];
836 - (void)outlineViewItemDidExpand:(NSNotification *)notification
838 [[NSNotificationCenter defaultCenter] postNotificationName:PXSLItemDidExpandNotification object:self userInfo:[notification userInfo]];
841 - (void)outlineViewItemWillCollapse:(NSNotification *)notification
843 [[NSNotificationCenter defaultCenter] postNotificationName:PXSLItemWillCollapseNotification object:self userInfo:[notification userInfo]];
846 - (void)outlineViewItemDidCollapse:(NSNotification *)notification
848 [[NSNotificationCenter defaultCenter] postNotificationName:PXSLItemDidCollapseNotification object:self userInfo:[notification userInfo]];
851 - (void)registerDelegateToReceiveNotification:(NSString*)notification withSelector:(SEL)selector
853 NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
855 //Set the delegate as a receiver of the notification if it implements the notification method
856 if([_secondaryDelegate respondsToSelector:selector]) {
857 [defaultCenter addObserver:_secondaryDelegate selector:selector name:notification object:self];