drag and drop files, URL's, text problem

Hi there,

I want to be able to drag and drop URL’s, files and
text into the same window (wx.ListCtrl). I can currently drag and drop each of
these separately by:

  1. instantiating wx.TextDropTarget to get text data

  2. instantiating wx.FileDropTarget to get file data

  3. instantiating wx.PyDropTarget to get URL’s

However I can’t work out how to create a drop target
for all three data types

I tried to use an instance of wx.PyDropTarget as follows:

class dropTarget(wx.PyDropTarget):

def init(self,
window):

wx.PyDropTarget.init(self)

self.window = window

self.comp =
wx.DataObjectComposite()

self.comp.Add(wx.FileDataObject())

self.comp.Add(wx.URLDataObject())

self.SetDataObject(self.comp)

This gives the following ‘Type’ error:

TypeError: in method
‘DataObjectComposite_Add’, expected argument 2 of type ‘wxDataObjectSimple *’

I then tried to Add() wx.FileDataObject() to wx.URLDataObject()
because it is composite data object but that gave the following error:

AttributeError: ‘URLDataObject’ object has
no attribute ‘Add’

I would really appreciate any advice on this matter.

Thanks,

Andrew

self.comp =
wx.DataObjectComposite()

self.comp.Add(wx.FileDataObject())

self.comp.Add(wx.URLDataObject())

self.SetDataObject(self.comp)

This gives the following ‘Type’ error:

TypeError: in method
‘DataObjectComposite_Add’, expected argument 2 of type ‘wxDataObjectSimple *’

This is the right approach, I believe, but Add() takes two arguments. The first a data object, the second a boolean flag “preferred”.

So you’d do:

self.comp.Add(wx.FileDataObject(), False)

etc.

–Stephen

Thanks Stephen, I’ll give than a try.

···

From: Stephen Hansen
[mailto:apt.shansen@gmail.com]
Sent: Sunday, December 28, 2008
6:24 AM
To: wxpython-users@lists.wxwidgets.org
Subject: Re: [wxpython-users] drag
and drop files, URL’s, text problem

    *self*.comp = wx.DataObjectComposite()

self.comp.Add(wx.FileDataObject())

self.comp.Add(wx.URLDataObject())

self.SetDataObject(self.comp)

This
gives the following ‘Type’ error:

TypeError: in method ‘DataObjectComposite_Add’, expected
argument 2 of type ‘wxDataObjectSimple *’

This is the right approach, I believe, but Add() takes two arguments.
The first a data object, the second a boolean flag “preferred”.

So you’d do:

self.comp.Add(wx.FileDataObject(), False)

etc.

–Stephen

Hi there,

I want to be able to drag and drop URL’s,
files and text into the same window (wx.ListCtrl). I can currently drag and
drop each of these separately by:

  1. instantiating wx.TextDropTarget to get text data
  1. instantiating wx.FileDropTarget to get file data
  1. instantiating wx.PyDropTarget to get URL’s

However I can’t work out how to create a drop
target for all three data types

I tried to use an instance of wx.PyDropTarget as
follows:

class dropTarget(wx.PyDropTarget):

def init(self, window):

wx.PyDropTarget.init(self)

   *self*.window = window
   *self*.comp = wx.DataObjectComposite()

self.comp.Add(wx.FileDataObject())

   self.comp.Add(wx.URLDataObject())

self.SetDataObject(self.comp)

This gives the following ‘Type’ error:

TypeError:
in method ‘DataObjectComposite_Add’, expected argument 2 of type
‘wxDataObjectSimple *’

I then tried to Add() wx.FileDataObject() to
wx.URLDataObject() because it is composite data object but that gave the
following error:

AttributeError:
‘URLDataObject’ object has no attribute ‘Add’

···

This is the right approach, I believe, but Add() takes
two arguments. The first a data object, the second a boolean flag
“preferred”.

So you’d do:

self.comp.Add(wx.FileDataObject(), False)

etc.

–Stephen


Added boolean flags as Stephen suggested, unfortunately I am
still getting the same error:

TypeError: in method
‘DataObjectComposite_Add’, expected argument 2 of type ‘wxDataObjectSimple *’

The Add() method doesn’t seem to be accepting wx.URLDataObject() or wx.FileDataObject() because
they aren’t ‘wxDataObjectSimple’

Here’s the offending code (with Boolean flags):

class dropTarget(wx.PyDropTarget):

def __init__(self, window):

wx.PyDropTarget.init(self)

    self.window =

window

    self.data =

wx.DataObjectComposite()

self.data.Add(wx.URLDataObject(),False)

self.data.Add(wx.FileDataObject(),False)

self.SetDataObject(self.data)

Thanks in advance for any suggestions,

Andrew

The docs of wxURLDataObject say this:

Derived from
Under MSW:

wxDataObjectComposite
wxDataObject

Under the other platforms:

wxTextDataObject
wxDataObjectSimple
wxDataObject

So I guess you are running on msw, wxURLDataObject is not a subclass of wxDataObjectSimple and thus your code does not work. It seems the msw version of wxURLDataObject is already a composite object itself. So maybe you want to do something like

         self.data = wx.URLDataObject()
         self.data.Add(wx.FileDataObject(),False)
         self.SetDataObject(self.data)

I've never tried this myself, just a guess from what I gathered from the docs.

-Matthias

···

Am 27.12.2008, 21:52 Uhr, schrieb Andrew Carswell <andrew@ese3d.co.nz>:

Added boolean flags as Stephen suggested, unfortunately I am still getting
the same error:

TypeError: in method 'DataObjectComposite_Add', expected argument 2 of type
'wxDataObjectSimple *'

Matthias wrote:

···

The docs of wxURLDataObject say this:
Derived from
Under MSW:
wxDataObjectComposite
wxDataObject

Under the other platforms:
wxTextDataObject
wxDataObjectSimple
wxDataObject

So I guess you are running on msw, wxURLDataObject is not a subclass of
wxDataObjectSimple and thus your code does not work. It seems the msw
version of wxURLDataObject is already a composite object itself. So maybe

you want to do something like

         self.data = wx.URLDataObject()
         self.data.Add(wx.FileDataObject(),False)
         self.SetDataObject(self.data)

I've never tried this myself, just a guess from what I gathered from the
docs.

_____________________________________

Thanks Matthais, I remember reading that 'wxURLDataObject is already a
composite object itself' in a post from Robin Dunn and have tried your
approach. However it gives the following error:
AttributeError: 'URLDataObject' object has no attribute 'Add'

The code below works fine when the Add() method is not used:

class dropTarget(wx.PyDropTarget):
    def __init__(self, window):
        wx.PyDropTarget.__init__(self)
        self.window = window

        self.data = wx.URLDataObject()
        self.data.Add(wx.FileDataObject(),False) <--- doesn't work
        self.SetDataObject(self.data)

You are correct in assuming that I am using msw (windows XP). As you stated,
given that wxURLDataObject is derived from wxDataObjectComposite under MSW,
I should be able to Add() to it. Don't suppose there are any other ideas?

Thanks for your advice Matthais,
Andrew