[wxPython] wxTreeCtrl::GetItemData() returns wxTreeItemData??

Why does wxTreeCtrl::GetItemData() returns wxTreeItemData rather than my
subclass of wxTreeItemData?

I've inherited from wxTreeItemData as follows:

class CNsiData( wx.wxTreeItemData ):

Now I handle tree expanding events as follows:

    def OnItemExpanding(self,event):
        ParItemId = event.GetItem();
        ParItemData = self.GetItemData(ParItemId);

But I find that the instance returned to ParItemData by GetItemData is of
type wxTreeItemDataPtr (which I assume I can read as wxTreeItemData), rather
than my user class CNsiData.

In case it's relevant, here's the ctor of my wxTreeCtrl derived class:

    def __init__(self, parent, id ):
        wx.wxTreeCtrl.__init__(self, parent, id);
        RootPyObj = ns.ObjStore.GetRoot();
        RootItemData = CNsiData( "Names", RootPyObj );
        self.Root = self.AddRoot( RootItemData.GetDispStr(), -1, -1,
RootItemData );
        self.SetItemHasChildren(self.Root, wx.TRUE);
        wx.EVT_TREE_ITEM_EXPANDING(self, self.GetId(),
self.OnItemExpanding);

Thanks for the help,

Tom.

Sorry, let me modify that question. I've added an assert to the code I
listed before to show where my problem is:

   def OnItemExpanding(self,event):
        ParItemId = event.GetItem();
        ParItemData = self.GetItemData(ParItemId);
        assert ParItemId == ParItemData.GetId();

The assert trips. I don't understand why. The item returned by
'event.GetItem()', was created with the following line of code (from the
__init__ fn that I listed below):

self.Root = self.AddRoot( RootItemData.GetDispStr(), -1, -1, RootItemData );

where RootItemData is an instance of a class derived from wxTreeItemData.
I'm assuming that this fn will set the id attribute of RootItemData.

Any idea what I'm doing wrong?

Thanks again,

Tom.

···

-----Original Message-----
From: wxpython-users-admin@wxwindows.org
[mailto:wxpython-users-admin@wxwindows.org]On Behalf Of Tom Malcolmson
Sent: August 28, 2000 3:34 PM
To: wxpython-users@wxwindows.org
Subject: [wxPython] wxTreeCtrl::GetItemData() returns wxTreeItemData??

Why does wxTreeCtrl::GetItemData() returns wxTreeItemData rather than my
subclass of wxTreeItemData?

I've inherited from wxTreeItemData as follows:

class CNsiData( wx.wxTreeItemData ):

Now I handle tree expanding events as follows:

    def OnItemExpanding(self,event):
        ParItemId = event.GetItem();
        ParItemData = self.GetItemData(ParItemId);

But I find that the instance returned to ParItemData by GetItemData is of
type wxTreeItemDataPtr (which I assume I can read as
wxTreeItemData), rather
than my user class CNsiData.

In case it's relevant, here's the ctor of my wxTreeCtrl derived class:

    def __init__(self, parent, id ):
        wx.wxTreeCtrl.__init__(self, parent, id);
        RootPyObj = ns.ObjStore.GetRoot();
        RootItemData = CNsiData( "Names", RootPyObj );
        self.Root = self.AddRoot( RootItemData.GetDispStr(), -1, -1,
RootItemData );
        self.SetItemHasChildren(self.Root, wx.TRUE);
        wx.EVT_TREE_ITEM_EXPANDING(self, self.GetId(),
self.OnItemExpanding);

Thanks for the help,

Tom.

_______________________________________________
wxPython-users mailing list wxPython-users@wxwindows.org
http://wxwindows.org/mailman/listinfo/wxpython-users

Why does wxTreeCtrl::GetItemData() returns wxTreeItemData rather than my
subclass of wxTreeItemData?

This is one of the "features" of SWIG that has to be worked around,
(although I have ideas for a solution...)

The root of the issue is that when a C++ method returns a pointer to an
object, there is no mechanism to associate it with the Python object that
originally wrapped it, so a new shadow object is created to wrap the
pointer. You can see this in action in code like this:

    def GetItemData(self, *_args, **_kwargs):
        val = apply(controls2c.wxTreeCtrl_GetItemData,(self,) + _args,
_kwargs)
        if val: val = wxTreeItemDataPtr(val)
        return val

Since needing to get at the original object in the tree data is common,
there is an easy workaround in place for wxTreeItemData. You don't have to
derive from wxTreeItemData at all. It's been extended to accept an
arbitrary Python object as a parameter to the contstructor, and added
methods GetData/SetData(obj).

    # creating/associating
    tree.SetItemData(itemID, wxTreeItemId(myObj))

    # fetching the data
    obj = tree.GetItemData(itemId).GetData()

The workaround then goes one step further. You don't have to use
wxTreeItemData at all! (At least explicitly, it's still used behind the
scenes for you.) wxTreeCtrl has a couple convenience methods added,
GetPyData(itemID) and SetPyData(itemID, obj), that allow you to hook python
objects to tree items without having to mess with a wxTreeItemData.

    # creating/associating
    tree.SetPyData(itemID, myObj)

    # fetching the data
    obj = tree.GetPyData(itemId)

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com
http://wxpython.org Java give you jitters?
http://wxpros.com Relax with wxPython!

Sorry, let me modify that question. I've added an assert to the code I
listed before to show where my problem is:

   def OnItemExpanding(self,event):
        ParItemId = event.GetItem();
        ParItemData = self.GetItemData(ParItemId);
        assert ParItemId == ParItemData.GetId();

The assert trips. I don't understand why.

I'm not sure either. It may be that the __cmp__ method is not implemented
correctly, I'll have to trace through it later to be sure.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com
http://wxpython.org Java give you jitters?
http://wxpros.com Relax with wxPython!