Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 4, 2011, 11:39 PM   #1
Avicenna
macrumors member
 
Join Date: Jul 2009
Cocoa NSView subview blocking drag/drop

I have an NSView subclass which registers for drag files in init method like this:

[self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];

The drag drop works perfectly fine, but if I add a subview to this view with the exact same frame, it doesn't work any more. My guess is that the subview is blocking the drag event to go to super view. How can I avoid that? Thanks

Also, when I am dragging, my cursor doesn't change to the "+" sign like with other drags in Finder or other applications, how can I do that? Thanks again.
Avicenna is offline   0 Reply With Quote
Old May 4, 2011, 11:55 PM   #2
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by Avicenna View Post
Also, when I am dragging, my cursor doesn't change to the "+" sign like with other drags in Finder or other applications, how can I do that? Thanks again.
Try adding this to your NSView subclass
Code:
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
{
  return NSDragOperationCopy;
}
jiminaus is offline   0 Reply With Quote
Old May 5, 2011, 08:08 AM   #3
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by Avicenna View Post
I have an NSView subclass which registers for drag files in init method like this:

[self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];

The drag drop works perfectly fine, but if I add a subview to this view with the exact same frame, it doesn't work any more. My guess is that the subview is blocking the drag event to go to super view. How can I avoid that? Thanks
So after lots of playing with bowels of the Cocoa event system, what I've discovered is that the whole of a drag and drop operation happens within the window server. This includes determining which view is under the mouse and therefore the target of the d'n'd operation. The target application doesn't get any events or messages, so it's impossible for your application to change this determination.

But the behaviour is strange. If a subview never registers dragged types, it is completely transparent to the window server's process for determining the target view. In this case, the super view will remain the d'n'd target even if the mouse moves over the subview.

If a subview ever registers dragged types, it will become the d'n'd target when the mouse moves over it. Even sending unregisterDraggedTypes to the subview doesn't stop this. The super view will still be sent a draggedExited message and will stop being the d'n'd target even though the subview cannot accept any type of d'n'd operation.

So the "best" solution I came up with is to put a glass view over the top of your super view and subview. A glass view is an NSView subclass who's drawRect: does nothing and who's hitTest: always returns nil. Move the drag'n'drop code from your super view to the glass view.

You need to be careful that the glass view has the highest Z order. In Interface Builder, it needs to be at the bottom of the document tree. See the attached image. The "Glass Drop View" is positioned over the top of the "Drop Target View". You'll probably need to use the cursor keys to move it over without becoming embedded as a subview.

To confirm, put something like the following into your glass view's drawRect:. Then your super view should be covered by red. If not, then the glass view is behind your super view, not in front of it.
Code:
    [[NSColor redColor] setFill];
    [NSBezierPath fillRect:[self bounds]];
Attached Images
 
jiminaus is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
problem after adding a subview on a UITableView ashwinr87 iPhone/iPad Programming 1 Jul 19, 2011 01:13 PM
Rotate a NSView and make it's origin point starts from the bottom left corner smokyonion Mac Programming 2 May 25, 2011 01:33 PM
How to check if an image is within a particular subview. BarryK88 iPhone/iPad Programming 2 Apr 15, 2011 03:18 AM
drag images in & out of UIScrollview BarryK88 iPhone/iPad Programming 3 Mar 23, 2011 05:49 AM
Adding NSSplitView and Drag/Drop for NSTableView into iProcrastinate! Littleodie914 Mac Programming 10 Aug 16, 2006 06:32 PM


All times are GMT -5. The time now is 03:32 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC