NSOutlineView drag image

716 Views Asked by At

I want to change the image used when I drag an item from a view- and datasource-based NSOutlineView, but can't seem to find a hook. I've tried modifying

- (void)dragImage:(NSImage *)anImage at:(NSPoint)viewLocation offset:(NSSize)initialOffset event:(NSEvent *)event pasteboard:(NSPasteboard *)pboard source:(id)sourceObj slideBack:(BOOL)slideFlag

in the rowViews, in the tableCellViews, and in a subclass of NSOutlineView itself, but to no avail.

Does anyone know where the default image (it's obviously taken from the tableCellView's image and textfield) is coming from?

2

There are 2 best solutions below

2
On

It's slightly awkward:
From your data source you can set the images on the NSDraggingSession instance you're passed via parameger e.g. to tableView:draggingSession:willBeginAtPoint:forRowIndexes:.

You can then use the enumerateDraggingItemsWithOptions:forView:classes:searchOptions:usingBlock: method on the NSDraggingSession instance to change the actual drag images of your NSDraggingItems:

   [session enumerateDraggingItemsWithOptions:NSDraggingItemEnumerationConcurrent
                                      forView:tableView
                                      classes:[NSArray arrayWithObject:[NSPasteboardItem class]]
                                searchOptions:nil
                                   usingBlock:^(NSDraggingItem *draggingItem, NSInteger index, BOOL *stop)
    {
       [draggingItem setDraggingFrame:NSMakeRect(0, 0, myWidth, myHeight)
                             contents:myFunkyDragImage];
    }];
0
On

It turns out that the image is obtained from NSTableCellView. So the hook is to override -(NSArray *)draggingImageComponents in a subclass of that.