[Drawkit] drag and drop

Graham Cox graham.cox at bigpond.com
Fri Oct 31 15:52:50 PDT 2008


On 1 Nov 2008, at 5:33 am, James Maxwell wrote:

> okay, I think I found it... I'm going to mess around with overriding  
> performDragOperation: in my custom layer class. Maybe someone can  
> post me a quick heads-up if this is a bad idea for some reason.
>
> J.
>
>
> On 31-Oct-08, at 10:59 AM, James Maxwell wrote:
>
>> I'm somewhat baffled by DK's wrapping of drag and drop. Where do I  
>> start with trying to add a new file type? Specifically, I want to  
>> be able to "import" midi files. I'm guessing this is just a matter  
>> of getting the path, which should already be a handled type, from  
>> what I can see... But I can't seem to get it to see midi files at  
>> all. I'd like to just get the path to the file, then forward it to  
>> my own midi file parsing class.
>


You need to do two things:

a) tell the system what file types it can accept, and
b) accept them.

The drop is handled primarily at the layer level, though you can also  
drop "into" objects if you wish - but this is still largely handled at  
the layer level (since the layer is able to discover targets for a  
drag and select them).

For a) you need to override:

- (NSArray*)		pasteboardTypesForOperation:(DKPasteboardOperationType)  
op;

This returns a list of drag types that the layer will allow. The <op>  
parameter allows you to distinguish between drag in/drag out and cut/ 
paste if necessary, though to get it working you can probably ignore  
that. So in this list you'd return the NSFilenamesPboardType or  
whatever.

Apart from this one deviation from the usual way of programming drag/ 
drop, everything else is standard. In other words, a layer implements  
the NSDraggingDestination protocol just as a view does, so you can  
override any of those standard methods to make it do what you want.  
The method you mention, -performDragOperation: is part of that  
protocol so it is the right thing to override to handle the drop.

DKObjectOwnerLayer already overrides these methods to allow drag/drop  
of images, text and objects between documents, and to draw a highlight  
frame around the edge.

By default you can only drag into the active layer, and not if it's  
locked or hidden. If you need some other behaviour, such as dragging  
into some other layer, then you'll have to  work on overriding stuff  
at the view level.

cheers, Graham




More information about the Drawkit mailing list