cocoadrag-and-dropnstextfieldnsdragginginfo

How to disable drag-n-drop for NSTextField?


I want to disallow dropping anything into my NSTextField. In my app, users can drag and drop iCal events into a different part of the GUI. Now I've had a test user who accidentally dropped the iCal event into the text field – but he didn't realize this because the text is inserted in the lines above the one that I see in my one-line text field.

(You can reveal the inserted text by clicking into the text field and using the keyboard to go one line up – but a normal user wouldn't do this because he/she wouldn't even have realized that something got inserted in the first place!)

I tried registerForDraggedTypes:[NSArray array]] (doesn't seem to have any effect) as well as implementing the draggingEntered: delegate method returning NSDragOperationNone (the delegate method isn't even invoked).

Any ideas?

EDIT: Of course dropping something onto an NSTextField only works when it has focus, as described by ssp in his blog and in the comments to a blog entry by Daniel Jalkut.


Solution

  • This might work: If you subclass NSTextView and implement -acceptableDragTypes to return nil, then the text view will be disabled as a drag destination. I also had to implement the NSDraggingDestination methods -draggingEntered: and -draggingUpdated: to return NSDragOperationNone.

    @implementation NoDragTextView
    - (NSArray *)acceptableDragTypes
    {
        return nil;
    }
    - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
    {
        return NSDragOperationNone;
    }
    - (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender
    {
        return NSDragOperationNone;
    }
    @end