1

I want to drag and drop a youtube video from a web browser into a Gtk window. The Gtk window receives the URL of the video.

The test script below works only 20% of the time. Sometimes the Gtk window receives the desired URL in plain text. Most of the time it receives something like application/x-moz-file-promise-dest-filename (from Firefox) or chromium/x-renderer-taint (from Chromium).

Some similar scripts work 100% of the time. This one allows the user to drag and drop onto a Gtk.Label. This answer allows the user to drag and drop onto a Gtk.TreeView. Presumably dragging onto either of those widgets filters out everything but the URL, but that doesn't help me; I don't have a Gtk.Label or a Gtk.TreeView.

Besides dropping on to a Gtk.Window, I've tried paneds, vboxes and listboxes, with identical results. I've tried using Gtk.Window.drag_dest_add_text_targets(), which breaks the script completely.

EDIT: The fix is as follows.

#!/usr/bin/env python3

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GObject

class DragDropWindow(Gtk.Window):

    def __init__(self):

        Gtk.Window.__init__(self, title='Drag and drop test')
        self.set_default_size(400, 400)

        # Set up drag and drop
#       self.connect("drag-motion", self.motion_cb)
#       self.connect("drag-drop", self.drop_cb)
        self.connect("drag-data-received", self.got_data_cb)
#       self.drag_dest_set(0, [], 0)
        self.drag_dest_set_target_list(None)
        self.drag_dest_add_text_targets()

#    def motion_cb(self, wid, context, x, y, time):
#        Gdk.drag_status(context,Gdk.DragAction.COPY, time)
#        return True

#    def drop_cb(self, wid, context, x, y, time):
#        wid.drag_get_data(context, context.list_targets()[-1], time)

    def got_data_cb(self, wid, context, x, y, data, info, time):
        if info == 0:
            text = data.get_text()
            datatype = data.get_target()

            print("Received text: %s" % text)
            print("Data type:     %s" % datatype)

        context.finish(True, False, time)

win = DragDropWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
lesrol
  • 175
  • 6
  • Do you want to move your fix to an answer so that you can "answer your own question" to help anybody with the same problem in the future? – ptomato Jan 27 '20 at 02:07

0 Answers0