DataObjectComposite questions

23 views
Skip to first unread message

Steven Sproat

unread,
Dec 15, 2009, 2:52:46 PM12/15/09
to wxPytho...@googlegroups.com
Hi all,

I'm looking to implement drag/drop that supports both text and files.
Here's what I have so far:

class FileDropTarget(wx.PyDropTarget):
def __init__(self):
wx.PyDropTarget.__init__(self)
self.do = wx.DataObjectComposite()
self.do.Add(wx.FileDataObject())
self.do.Add(wx.TextDataObject())
self.SetDataObject(self.do)

def OnDrop(self, x, y):
print self.do.GetReceivedFormat()

def OnData(self, x, y, d):
self.GetData()
return d


## somehow forward to these depending on object type

def OnDropText(self, x, y, text):
pass


def OnDropFiles(self, x, y, filenames):
pass

it prints the following, regardless of whether I'm pasting
text/image/bitmap:

<wx._misc.DataFormat; proxy of <Swig Object of type 'wxDataFormat *' at
0x1e49ed0> >

Am I just going about this the wrong way? I can't find any examples of
the DataObjectComposite, apart from the wxWidgets docs saying to use the
object directly, not sub-class it, as I initially did.
Thanks,

--
Steven Sproat, BSc
http://www.basicrpg.com/

Robin Dunn

unread,
Dec 16, 2009, 3:49:54 PM12/16/09
to wxpytho...@googlegroups.com
On 12/15/09 11:52 AM, Steven Sproat wrote:
> Hi all,
>
> I'm looking to implement drag/drop that supports both text and files.
> Here's what I have so far:


> it prints the following, regardless of whether I'm pasting
> text/image/bitmap:
>
> <wx._misc.DataFormat; proxy of<Swig Object of type 'wxDataFormat *' at
> 0x1e49ed0> >
>
> Am I just going about this the wrong way? I can't find any examples of
> the DataObjectComposite, apart from the wxWidgets docs saying to use the
> object directly, not sub-class it, as I initially did.
> Thanks,
>

You need to compare the received data format with known format types.
Once you know the type you can pull the data values from the correct
component data object. I have this example in one of my projects. It
accepts either an image file or a bitmap, and after looking at it today
I see that it shoudl be doing some error checking :-(

def OnData(self, x, y, d):
if self.GetData():
df = self.dataObj.GetReceivedFormat()
if df.GetType() == wx.DF_FILENAME:
filenames = self.fileDO.GetFilenames()
bmp = wx.Bitmap(filenames[0])
self.window.SetBgImage(bmp)
else:
bmp = self.bmpDO.GetBitmap()
self.window.SetBgImage(bmp)

return d




--
Robin Dunn
Software Craftsman
http://wxPython.org

Steven Sproat

unread,
Dec 17, 2009, 10:49:59 AM12/17/09
to wxpytho...@googlegroups.com

2009/12/16 Robin Dunn <ro...@alldunn.com>



You need to compare the received data format with known format types.
Once you know the type you can pull the data values from the correct
component data object. 


ahhh, I see! Thanks, I have it working great now. However, I've noticed some oddities on Linux (I kind of mentioned it yesterday on IRC, but I'll post to the list too) - wx 2.8.10.1, python 2.6.4, gtk2

I have my Drop Target to accept Bitmaps, Files and Text using the DataObjectComposie (great class, very easy to use! I love it)



def OnData(self, x, y, d):
    """
    Handles drag/dropping files/text or a bitmap
    """
    if self.GetData():
        df = self.do.GetReceivedFormat().GetType()

        if df == wx.DF_UNICODETEXT or df == wx.DF_TEXT:
            pass
        elif df == wx.DF_FILENAME:
            pass
        elif df == wx.DF_BITMAP:
            pass

    return d


I've cut out what I'm doing in the if statements as it's irrelevant. Anyhow, when dragging text from Firefox on Linux, my text if statement fails. It appears that it is being dragged in as wx.DF_HTML, however there is no corresponding HTMLDataObject to handle it. On Windows, it registers as plain text somehow

Again, on GTK, dragging in a bitmap (again, say dragging an image from Firefox) will match the if statement of DF_FILENAME, not BITMAP. On Windows, it's recognised as a BITMAP.


I'm aware of how wx is just wrapping the GTK interface, but is there possibly any way to work around these? There's been a few places in my code where I do two different things depending on Windows or Linux, so this is no problem to do again. Many thanks,

Steven Sproat



Robin Dunn

unread,
Dec 17, 2009, 1:15:06 PM12/17/09
to wxpytho...@googlegroups.com
On 12/17/09 7:49 AM, Steven Sproat wrote:
>
> 2009/12/16 Robin Dunn <ro...@alldunn.com <mailto:ro...@alldunn.com>>

>
>
>
> You need to compare the received data format with known format types.
> Once you know the type you can pull the data values from the correct
> component data object.
>
>
>
> ahhh, I see! Thanks, I have it working great now. However, I've noticed
> some oddities on Linux (I kind of mentioned it yesterday on IRC, but
> I'll post to the list too) - wx 2.8.10.1, python 2.6.4, gtk2
>
> I have my Drop Target to accept Bitmaps, Files and Text using the
> DataObjectComposie (great class, very easy to use! I love it)
>
>
> def OnData(self, x, y, d):
> """
> Handles drag/dropping files/text or a bitmap
> """
> if self.GetData():
> df = self.do.GetReceivedFormat().GetType()
>
> if df == wx.DF_UNICODETEXT or df == wx.DF_TEXT:
> pass
> elif df == wx.DF_FILENAME:
> pass
> elif df == wx.DF_BITMAP:
> pass
>
> return d
>
>
> I've cut out what I'm doing in the if statements as it's irrelevant.
> Anyhow, when dragging text from Firefox on Linux, my text if statement
> fails. It appears that it is being dragged in as wx.DF_HTML, however
> there is no corresponding HTMLDataObject to handle it. On Windows, it
> registers as plain text somehow

Have you checked if the html is actually in the TextDataObject?

>
> Again, on GTK, dragging in a bitmap (again, say dragging an image from
> Firefox) will match the if statement of DF_FILENAME, not BITMAP. On
> Windows, it's recognised as a BITMAP.

This could just be a case of the browser working differently on each
platform. Is the filename set to something like a temporary local file
that is the image being dragged? It's also possible that the browser is
sending both a filename and a bitmap, but since you are checking for the
filename first then you are not seeing that there is also a bitmap.

There are some tools around for each platform that can help you diagnose
what is going on here. Typically they will show you the details about
each data object on the clipboard or in a DnD, including the data format
and the data itself. On Windows ClipSpy is a good one. I can't
remember what the name of one on Linux that I used once, but a little
googling will probably locate it for you.

Steven Sproat

unread,
Dec 17, 2009, 2:22:55 PM12/17/09
to wxpytho...@googlegroups.com
Ah no, I didn't think of that. Do the data objects get reset every time
a new item is dragged onto the drop target? e.g. if I'm checking the
length of self.textobject.GetText(), if it contains text, will its
contents be erased if a bitmap or a file is drag/dropped? I'm guessing
it's a feature of the DataObjectComposite to fill/empty the appropriate
DataObjects.

>
>> Again, on GTK, dragging in a bitmap (again, say dragging an image from
>> Firefox) will match the if statement of DF_FILENAME, not BITMAP. On
>> Windows, it's recognised as a BITMAP.
>>
>
> This could just be a case of the browser working differently on each
> platform. Is the filename set to something like a temporary local file
> that is the image being dragged? It's also possible that the browser is
> sending both a filename and a bitmap, but since you are checking for the
> filename first then you are not seeing that there is also a bitmap.
>
> There are some tools around for each platform that can help you diagnose
> what is going on here. Typically they will show you the details about
> each data object on the clipboard or in a DnD, including the data format
> and the data itself. On Windows ClipSpy is a good one. I can't
> remember what the name of one on Linux that I used once, but a little
> googling will probably locate it for you.
>
>
>
oh, whoops - I forgot to mention, that GetFilenames() returns an empty
list after dragging the bitmap. (I too thought perhaps it was a
temporary file; guess not) I'll try reversing the order, see how that
works. Thanks for the pointers, I'll play with a Linux tool. I use a
clipboard manager named "parcellite" on Linux that -may- be interfering
with something - at the moment there's a bug where it will not store
copied bitmap data (but I don't know if DnD uses the clipboard to
perform its operations)

Cheers for the hints :)

Reply all
Reply to author
Forward
0 new messages