Drag & Drop - how to detect drop on Trash

My app iClip needs a 64 bit update.

One of the main areas I could not update yet is drag & drop because, well, back when I wrote that code, Real Studio’s DragItem just offer the functionalities I needed, including full control over the “drag over” behavior. So I ended up writing a lot of declares, using the Carbon DragManager API, and disabling the Xojo framework’s own handler so that I had full control.

Now, with the 64 bit transition, this isn’t working any more (no more Carbon DragManager).

I’m working these out step by step now, but once thing that didn’t work back then is detecting when the user drags my object onto the Trash Dock icon. There should be a way to detect that, but there was none back then.

Has this been improved? Can I detect a drop into the Trash, maybe with declares or with MBS, when I perform a drag operation with the DragItem class?

@Thomas Tempelmann — AFAIK, you need to set the drag type to “promised file”. When the item is dropped, it will pass a path to the drag item to inform you where you should save the file. You can then determine if the path corresponds to the Trash.

I don’t know if it’s going to work with DragItem. Maybe you will need its Cocoa counterpart.

Yeah, this is not about dragging files, though. It’s about regular clips, e.g. text - so I cannot (must not!) set a promised file for that.

In the most basic Xcode Swift project, handling this (detecting drop into Trash) is no issue at all.

@Thomas Tempelmann

Then I don’t understand why you want to detect any drag&drop onto the Trash

Consider what iClip does. User drags clip to trash on dock, user wants to delete clip from iClip :slight_smile:

@Tim Parnell — But who would use that instead of pressing Backspace or Delete?

Stphane, you may not understand why iClip does this, but trust me - it makes sense, and the functionality I am asking for is officially supported by macOS - just not by Xojo’s framework, it appears.

Alright, I’ve solved it using MBS plugins.

[code]// Create a subclass of CustomNSViewMBS, name it DragSession, add a public property
// DragEndedWithOperation as Integer, and implement these two events handler in it:

Function draggingSourceOperationMaskForLocal(flag as boolean) As integer
return NSDraggingInfoMBS.NSDragOperationCopy or NSDraggingInfoMBS.NSDragOperationDelete
End Function

Sub draggingSessionEndedAtPoint(session as NSDraggingSessionMBS, screenPoint as NSPointMBS, operation as integer)
self.DragEndedWithOperation = operation
End Sub

// This code goes inside the MouseDown event of a Canvas control that we want to drag:

dim dragPicture as new Picture (…)
// make an img here that is shown when you drag the data
dim w as Window = self // the window that contains the control
dim c as RectControl = me // the control from which the drag starts
dim baseLoc as new NSPointMBS (c.Left, w.Height - c.Top - c.Height) // convert to inverted coords
dim nsw as NSWindowMBS = w.NSWindowMBS
dim pb as NSPasteboardMBS = NSPasteboardMBS.pasteboardWithName(NSPasteboardMBS.NSDragPboard)
// fill in your Pasteboard contents here, like this:
pb.dataForType (pb.NSPasteboardTypeString) = “some text”
dim tracker as new DragSession
// This call performs the drag and only returns when the user lets go of the dragged item:
nsw.dragImage dragPicture, baseLoc, NSMakeSizeMBS(32,32), nsw.currentEvent, pb, tracker, true
// Now we can check if the item was trashed (or moved, if we support that)
if tracker.DragEndedWithOperation = NSDraggingInfoMBS.NSDragOperationDelete then
// it was moved into the trash
end[/code]