DnD from other application

Hello Christian,

but OnData is never called. So I can say that and where
something has been dropped but I can't access the data. What
am I doing wrong?

Uhm, can you try the slightly modified script I attach to see if it works? Don't know if this is exactly what you asked, but it seems to me it is working...

Andrea.

import wx

class Frame(wx.Frame):
   def __init__(self, parent):
       wx.Frame.__init__(self, parent, -1)

       self.panel = wx.Panel(self, -1)
       sizer = wx.BoxSizer(wx.VERTICAL)

       self.btn = wx.Button(self.panel, -1, "drop here")
       sizer.Add(self.btn, 1, wx.EXPAND)

       self.panel.SetAutoLayout(True)
       self.panel.SetSizer(sizer)

       dt = DropTarget(self.btn)
       self.btn.SetDropTarget(dt)

class DropTarget(wx.PyDropTarget):
   def __init__(self, window):
       wx.PyDropTarget.__init__(self)
       #self.do = wx.TextDataObject()
       self.do = wx.CustomDataObject(wx.DataFormat(wx.DF_TEXT))
       self.SetDataObject(self.do)

   def OnEnter(self, x, y, d):
       print 'on enter'
       return d

   def OnDragOver(self, x, y, d):
       print 'on drag over'
       return d

   def OnLeave(self):
       pass

   def OnDrop(self, x, y):
       print 'on drop'
       return True

   def OnData(self, x, y, d):
       print 'on data'
## self.GetData()
       print self.do.GetData()
       return d

class App(wx.App):
   def OnInit(self):
       self.frame = Frame(None)
       self.frame.Show(True)
       return True

app = App()
app.MainLoop()

···

_________________________________________
Andrea Gavana (gavana@kpo.kz)
Reservoir Engineer
KPDL
4, Millbank
SW1P 3JA London

Direct Tel: +44 (0) 20 717 08936
Mobile Tel: +44 (0) 77 487 70534
Fax: +44 (0) 20 717 08900
Web: http://xoomer.virgilio.it/infinity77
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Gavana, Andrea <gavana <at> kpo.kz> writes:

> but OnData is never called. So I can say that and where
> something has been dropped but I can't access the data. What
> am I doing wrong?

Uhm, can you try the slightly modified script I attach to see if it works?
Don't know if this is exactly what
you asked, but it seems to me it is working...

In spite of your modification OnData is still not called. Does it work on yout
platform?

Christian

Hello Christian,

In spite of your modification OnData is still not called. Does it work on yout
platform?

Yes, it is working here. When I release the mouse over the button
OnData is called. I am on Windows XP, Python 2.4.3, wxPython latest
pre-release.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77/

It wasn't working in the wxPython 2.6.3.3 version I was using, but I was
able to fix it with the code later.

Note that there are three main changes; the use of wx.TextDataObject()
directly (probably not necessary as long as one uses the text custom
object), we call the self.GetData() method on the DropTarget instance
and verify that it returns a non-false value (which transfers the data
from the clipboard to the DropTarget instance, then we use the
TextDataObject's GetText() method to get the text. I remember I have
had troubles trying to do it any other way, but a variant of the below
is essentially what PyPE uses to handle text and filename drops.

- Josiah

import wx

class Frame(wx.Frame):
   def __init__(self, parent):
       wx.Frame.__init__(self, parent, -1)
       self.panel = wx.Panel(self, -1)
       sizer = wx.BoxSizer(wx.VERTICAL)
       self.btn = wx.Button(self.panel, -1, "drop here")
       sizer.Add(self.btn, 1, wx.EXPAND)
       self.panel.SetAutoLayout(True)
       self.panel.SetSizer(sizer)
       dt = DropTarget(self.btn)
       self.btn.SetDropTarget(dt)

class DropTarget(wx.PyDropTarget):
   def __init__(self, window):
       wx.PyDropTarget.__init__(self)
       self.do = wx.TextDataObject()
       self.SetDataObject(self.do)
   def OnEnter(self, x, y, d):
       print 'on enter'
       return d
   def OnDragOver(self, x, y, d):
       print 'on drag over'
       return d
   def OnLeave(self):
       pass
   def OnDrop(self, x, y):
       print 'on drop'
       return True
   def OnData(self, x, y, d):
       if self.GetData():
           print 'on data'
           print self.do.GetText()
           return d

class App(wx.App):
   def OnInit(self):
       self.frame = Frame(None)
       self.frame.Show(True)
       return True

app = App()
app.MainLoop()

···

"Andrea Gavana" <andrea.gavana@gmail.com> wrote:

Hello Christian,

> In spite of your modification OnData is still not called. Does it work on yout
> platform?

Yes, it is working here. When I release the mouse over the button
OnData is called. I am on Windows XP, Python 2.4.3, wxPython latest
pre-release.