6

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
siekfried
  • 2,964
  • 1
  • 21
  • 36

1 Answers1

1

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.

Jamon Holmgren
  • 23,738
  • 6
  • 59
  • 75
  • Thank you for your answer, I didn't use the draggingDestinationWindow method but was able to solve the problem by subclassing the webview and overriding the performDragOperation method, helped by this other answer: http://stackoverflow.com/questions/12742276/drag-and-drop-from-finder-to-webview. But you put me on the right way so I'll accept you answer. – siekfried Jun 24 '13 at 07:37