views:

596

answers:

2

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.

A: 

Have you tried

- (void)unregisterDraggedTypes
from NSView?

Mark Thalman
I've finally come around to try this (I agree that the documentation does sound as if this should do the job).So in my controller's awakeFromNib, I call unregisterDraggedTypes on my NSTextField outlet. However this doesn't seem to have any effect... I still can drag stuff into my field.
Yang
Have you checked you outlet in the debugger to verify that it is connected and not NULL when unregisterDraggedTypes is called?
Mark Thalman
+2  A: 

Hi Yang - I am glad you discovered the comments in my blog post. I think they are the tip of the iceberg to discovering how to achieve what you're looking for.

You need to keep in mind that the reason dragging to an NSTextField works when it has focus, is that the NSTextField has itself been temporarily obscured by a richer, more powerful view (an NSTextView), which is called the "Field Editor."

Check out this section of Apple's documentation on the field editor:

http://developer.apple.com/documentation/Cocoa/Conceptual/TextEditing/Tasks/FieldEditor.html

To achieve what you're striving for, I think you might need to intercept the standard provision of the field editor for your NSTextFields, by implementing the window delegate method:

windowWillReturnFieldEditor:toObject:

This gives you the opportunity to either tweak the configuration on the NSTextView, or provide a completely new field editor object.

In the worst case scenario, you could provide your own NSTextView subclass as the field editor, which was designed to reject all drags.

danielpunkass