1
votes

I am using Apple's example "Outline View" as a file browser. I have a DataSource & FileSystemItem exactly as Apple has written them. I am unable to click-hold and then drag a filename or folder name. When I click, the selectedRow turns blue. As I hold, nothing happens. If I move as I hold the blue line scrolls through the file list.

I have gone through Apple's "Drag N Drop Outline View" code and it is not what I am looking for. I do not want to reorder the files and drag them from within the OutlineView. I want to drag a filename (ultimately its path) to an NSTextfield. I have not been able to get the NSOutlineView to register drag operations.

I've subclassed NSOutlineView and tried:

- (void)awakeFromNib {
    [_outlineView registerForDraggedTypes:[NSArray arrayWithObjects:LOCAL_REORDER_PASTEBOARD_TYPE, NSStringPboardType, NSFilenamesPboardType, nil]];
    [_outlineView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:YES];
}

as well as -(NSDragOperation), draggingEntered, -(void)registerForDraggedTypes, etc...

I have no trouble dragging text from an NSScrollView to this NSTextField without any special code. Apparently an NSOutlineView does not inherit the native drag ability of NSScrollView?

So how can I get an NSOutlineView to allow me to drag. Again, Apple's DragNDropOutlineView does way more than I want, doesn't do what I want, and hasn't helped me figure out how to get that drag ability.

1
I have gone through DragNDropOutlineView.m and it has "- (void)outlineView:(NSOutlineView *)outlineView draggingSession:(NSDraggingSession *)session willBeginAtPoint:(NSPoint)screenPoint forItems:(NSArray *)draggedItems {" in it that gets called when you click and hold. I have this in my code and when I click and hold it DOES NOT get called. So something is not setup right. I know my awakeFromNib is being called but nothing else seems to be called. What am I missing?Iron Mike
When I click on the row, this method is called: - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item { return (item == nil) ? @"/" : (id)[item relativePath]; } - I believe this is overriding any dragging procedure. So the question is, how to I add dragging and have this method work?Iron Mike
FYI, I commented out the method above and I still have the select behavior where the blue highlight simply follows my cursor while the mouse button is down. So maybe it doesn't override dragging. Something is not set right. Anyone at all have any ideas/help?Iron Mike

1 Answers

1
votes

You can declare your registerForDraggedTypes like this

[outline_view registerForDraggedTypes:[NSArray arrayWithObjects:LOCAL_REORDER_PASTEBOARD_TYPE,NSColorPboardType,NSDragPboard, nil]];

And You need to implement this method to perform drag operation in NSOutlineView

- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pasteboard
{

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:items];
    [pasteboard declareTypes:[NSArray arrayWithObject:LOCAL_REORDER_PASTEBOARD_TYPE] owner:self];
    [pasteboard setData:data forType:LOCAL_REORDER_PASTEBOARD_TYPE];
    return YES;   
}

this will allow to drag NSOutlineView rows..