views:

27

answers:

1

I am trying to write an app. that will allow the user to drag from an NSButton to another. When dropped I would like the Title to be dropped onto the button. I cannot find a way to make NSButton draggable. Is it possible?

+1  A: 

First a warning: this sounds like it would be confusing to a user. Buttons are intended to emulate buttons in the real world. You push them rather than drag them around.

Having said that, it's certainly possible to do in a subclass of NSButton.

Drag source

  1. Implement the dragging source method draggingSourceOperationMaskForLocal:. Sounds like you're copying the title, so you may want to use NSDragOperationCopy.
  2. Call dragImage:at:offset:event:pasteboard:source:slideBack: in your mouseDragged: method. Use the drag pBoard ([NSPasteboard pasteboardWithName:NSDragPboard]) and copy your button title into it. For the image you can draw your button into an NSImage and use that as the drag image, or you might use an icon or even just the title.

Drag destination

  1. Register your custom button to be able to accept the title string using registerForDraggedTypes:.
  2. Implement the drag destination methods draggingEntered: and performDragOperation: to return appropriate values. There are several other methods including draggingUpdated: and draggingExited: among others that you can use to provide visual feedback.

There's lots of info out there on implementing drag & drop. Read Apple's docs: Drag & Drop Programming Topics and Pasteboard Programming Guide

Sean Rich