Search code examples
pythonwindowswxpythonwxwidgets

Drag and drop application-created file to explorer window


I have a ListCtrl containing some items representing (huge and remote) files. I'd like the user to be able to drag an item to an open directory window and thereby create a file (really initiating a download). I'd like my application to get a message like "the user dragged your list item to this path", so that I can then proceed and write the file data to that location.

I know how to do something similar if the source file is available on the local file system, but my files are large and on a remote system, (think FTP client) so I cannot speculatively copy them to disk in case the user wants to drag'n'drop them later.

How can I accomplish this using wxpython? Is it even possible?


Solution

  • I searched the web pretty hard but could not find a way to do this either. Even Robin Dunn says that the drop source app knows nothing about the target when data is dropped into the file system. But I figured out an easy way to do it, at least on Windows. We simply drag a DropSource containing an empty FileDataObject onto an Explorer window. Since there's no data, all this does is bring the Explorer window to the top, which allows us to get the path to the folder the user dragged into. First, be sure to bind the event to the ListCtrl up in the __init__ def of the ListCtrl's parent:

    self.lc.Bind(wx.EVT_LIST_BEGIN_DRAG, self.onDrag)
    

    Then do this in the method called by the event:

    def onDrag(self, event):
        data = wx.FileDataObject()
        obj = event.GetEventObject()
        dropSource = wx.DropSource(obj)
    
        dropSource.SetData(data)
    
        #next line will make the drop target window come to top, allowing us
        #to get the info we need to do the work, if it's Explorer
        result = dropSource.DoDragDrop(0)
    
        #get foreground window hwnd
        h = win32gui.GetForegroundWindow()
    
        #get explorer location
        s = win32com.client.Dispatch("Shell.Application")
        loc, outdir = None, None
        for w in s.Windows():
            if int(w.Hwnd) == h:
                loc = w.LocationURL
        if loc:
            outdir = loc.split('///')[1]
            outdir = urllib.unquote(outdir)
    
        #got what we need, now download to outfol
        if outdir and os.path.isdir(outdir):
            self.dloadItems(event, outdir)
    
    
        return
    

    The dloadItems method gets the selected items from the ListCtrl and then (in this app) downloads the items from a REST server to the outdir.

    This solution requires the pywin32 extensions, of course.

    good luck,

    Mike