Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable drag and drop for WebView in favor for one of its superviews

I'm working on an application featured like Mac Mail. I have a WebView which allow the user to write a new message. I implemented the drag and drop feature so that a user can add an attachment to the message that way.

To make it simple, I have a main view which contains a WebView and other views. I implemented the drag and drop on this main view (with the draggingEntered: and performDragOperation: methods) and it works as expected.

The problem is that by default, when dragging a file inside a WebView, an image for instance, the image will be displayed in the WebView. But I don't want that, I want it to be added as an attachment, that's why I disabled the drag and drop inside the WebView:

  def webView(sender, dragDestinationActionMaskForDraggingInfo:draggingInfo)
    WebDragDestinationActionNone
  end

But now my file will be added as an attachment if I drag it anywhere inside the main view, except in the WebView (the draggingEntered: and performDragOperation: methods are not called in this case).

I don't know if my question is clear enough to find an answer, I am still new in Cocoa development, so don't hesitate to tell me if you need more details. Another thing, i'm working with Rubymotion but if you got a solution in Objective-C, that would also be perfect!

Thanks for any help or suggestion.

Solution

I subclassed the WebView and overrode the performDragOperation method to make it work:

def performDragOperation(sender)
  self.UIDelegate.mainView.performDragOperation(sender)
end
like image 975
siekfried Avatar asked Jun 20 '13 12:06

siekfried


1 Answers

You could check the sender (which implements the NSDraggingInfo protocol) for the destination window:

http://developer.apple.com/library/Mac/#documentation/Cocoa/Reference/ApplicationKit/Protocols/NSDraggingInfo_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSDraggingInfo/draggingDestinationWindow

def performDragOperation(sender)
  if sender.draggingDestinationWindow == @web_view
    # Attach file
  else
    # Let it do the normal drag operation
  end
  true
end

That's just a guess, but you should be able to figure out a solution from here.

like image 190
Jamon Holmgren Avatar answered Nov 09 '22 22:11

Jamon Holmgren