4

It's been a while since I've done any Cocoa development, and I'm trying to get this very quick and dirty (and simple) app wrapped up. I decided to use MacRuby because it was a good excuse to learn it, and the app is simple enough that it made sense.

I'm having trouble getting a custom view to respond to drag events, though.

class ImportPanel < Panel
  def initWithFrame(frame)
    registerForDraggedTypes(NSArray.arrayWithObjects(NSPasteboardTypeSound, nil))
    super(frame)
  end

  def mouseDown(event)
    NSLog('click')
  end

  def draggingEntered(sender)
    NSLog('drag')
  end
end

Panel, in this case, is just an NSView that adds a border. This custom view (ImportPanel) is responding correctly to click events, but not reacting at all to drag events. I have tried several different pasteboard types and configurations for registerForDraggedTypes:, but none seem to produce any results.

Farski
  • 1,670
  • 3
  • 15
  • 30

1 Answers1

0

This code worked for me.

AppDelegate.rb

class AppDelegate
    attr_accessor :window
    attr_accessor :panel

    def applicationDidFinishLaunching(a_notification)
        # nothing special here
    end

    def initialize
        @panel = Panel.new

        # Just for debug
        puts @panel
    end
end

And this is my player.rb:

class Panel < NSView
    def awakeFromNib
        registerForDraggedTypes(NSArray.arrayWithObjects(NSFilenamesPboardType, NSURLPboardType, NSStringPboardType, nil))
    end

    def mouseDown(event)
        NSLog('click')
    end

    def draggingEntered(sender)
        NSLog('drag')
        return NSDragOperationNone
    end
end

The array of dragged types it's that way to help me testing different dragging operations (A URL, a file etc.). Note that draggingEntered needs to return an NSDragOperation i used NSDragOperationNone for just to see if it works.

microspino
  • 7,693
  • 3
  • 48
  • 49