Re: Drag and drop in NSOutlineView
Re: Drag and drop in NSOutlineView
- Subject: Re: Drag and drop in NSOutlineView
- From: John Brownlow <email@hidden>
- Date: Thu, 14 Apr 2005 10:14:24 -0400
awesome, thank you
On Apr 14, 2005, at 3:55 AM, Lorenzo wrote:
Hi,
firstly define your own pasteboard type like here below
MyItemsPboardType
#define MyItemsPboardType @"MyItemsPboardType"
--------
then in the awakeFromNib call, set the outlineView can drag this type
NSArray *myTypes = [NSArray arrayWithObjects:MyItemsPboardType,
nil];
[outlineView registerForDraggedTypes:myTypes];
[outlineView setVerticalMotionCanBeginDrag:YES];
--------
then, when the drag begins, in the writeItems call, you have to fill
the pasteboard with data regarding the items dragged, for the type
MyItemsPboardType
- (BOOL)outlineView:(NSOutlineView*)olv writeItems:(NSArray*)items
toPasteboard:(NSPasteboard*)pboard
{
NSArray *myTypes = [NSArray arrayWithObjects:MyItemsPboardType,
nil];
[pboard declareTypes:myTypes owner:self];
NSData *itemsData = [items GetItemsData];
[pboard setData:itemsData forType:MyItemsPboardType];
return YES;
}
--------
Now we are at the destination. Here you have to detect whether the
current
dragging pasteboard contains that type MyItemsPboardType.
If so, you authorize the drop. Take care of the ancestors.
- (NSDragOperation)outlineView:(NSOutlineView*)outlineView
validateDrop:(id <NSDraggingInfo>)info
proposedItem:(id)item proposedChildIndex:(int)index
{
NSPasteboard *pboard = [info draggingPasteboard];
NSArray *myTypes = [NSArray arrayWithObjects:MyItemsPboardType,
nil];
if([pboard availableTypeFromArray:myTypes] != nil){
//control here ancestors...
return NSDragOperationGeneric;
}
return NSDragOperationNone;
}
--------
Here thr drop ends. If earlier "validateDrop" said ok, now here you
get the
data from the pasteboard and create a new item (and subItems), then
delete
the old items dragged. Yes, it's not so immediate...
- (BOOL)outlineView:(NSOutlineView *)outlineView
acceptDrop:(id <NSDraggingInfo>)info item:(id)targetItem
childIndex:(int)childIndex
{
NSPasteboard *pboard = [info draggingPasteboard];
NSArray *myTypes = [NSArray arrayWithObjects:MyItemsPboardType,
nil];
if([pboard availableTypeFromArray:myTypes] != nil){
// add here your items and delete the old one...
return YES;
}
return NO;
}
Best Regards
--
Lorenzo
email: email@hidden
------------------------------
Message: 11
Date: Thu, 14 Apr 2005 00:58:50 -0400
From: Johnny Deadman <email@hidden>
Subject: Drag and drop in NSOutlineView
To: Cocoa <email@hidden>
Message-ID: <email@hidden>
Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed
Hi all.
I have a simple test app which has an NSOutlineView in a window. The
outline view is correctly populated, editable, and all the add, add
child, remove etc operations work perfectly. I have implemented the
drag-related delegate methods. However, I can't drag and drop. I
wonder if my expectations are wrong?
I want to be able to click on a cell and drag it to another position
in the outline, and have it take all its children with it when it
goes.
I've poked through all the documentation I could find and all the
settings in Interface Builder but can't see any clues.
(I am not using a conventional glue code datasource here but a little
present from our friend with the big paws and orange stripes...
however I don't think this is having any effect on the behavior of
the NSOutlineView. What happens when I try to drag is that the
selection simply changes to follow the mouse.)
--
John Brownlow
Deep Fried Films, Inc
http://www.johnbrownlow.com
http://www.pinkheadedbug.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden