How to accept clipboard data falling onto a Racket canvas

I am writing a UI application using Racket and would like to be able to drag and drop elements, such as images, from other applications. In particular, I want to drag an image from a web page in a browser (for example, in Chrome or FF) and transfer it to canvas% in the application, resulting in raw image data (the application will consider PNG metadata before accepting the discarded image).

There is no functionality (which I can find) directly in the canvas% class or superclasses that relate to clipboard events.

I am familiar with the dnd clipboard in other environments such as Swing and Win32.

The clipboard-client% class seems to be what is required, but the Racket documentation makes it harder to connect points to the binding to one of the window classes.

Are there any good tutorials or code examples that I can reference?


Update: I looked at the source of Racket (OSX) for clipboard.rkt and seems to have turned to a β€œcommon” file cabinet. OSX has a separate cardboard for drag-n-drop operations.

So, it seems that dnd does not work in Racket on OSX, and I will have to use Objective-C FFI to implement the required functionality and limit my users to those using a Mac.

The completion of this question.

+4
source share
1 answer

As far as I can tell, the Racket GUI library only handles dragging and dropping files using the accept-drop-files and on-drop-file methods of the window<%> interface, which is implemented by canvas% . I am not familiar with the dnd protocols, but, at least on Linux, images dragged from Firefox are not considered files, so they are not accepted, while files dragged from nautilus are.

Here is a small program that I used for testing:

 #lang racket/gui (define my-canvas% (class canvas% (define/override (on-drop-file file) (printf "got file: ~s\n" file) (super on-drop-file file)) (super-new))) (define f (new frame% (width 400) (height 400) (label "frame"))) (define c (new my-canvas% (parent f))) (send c accept-drop-files #t) (send f show #t) 

I recommend requesting the Racket mailing list .

+4
source

Source: https://habr.com/ru/post/1469091/


All Articles