From: John L. <jla...@gm...> - 2006-04-23 15:37:34
|
On 4/22/06, Leandro Motta Barros <lmb...@gm...> wrote: > I'm having a problem using a 'wxTreeCtrl' in my project. More > specifically, I have to associate some data to every tree item, and be > able to retrieve this data (like "what's the data associated with the > selected item?") > > With wxLua, I itended to create a table mapping 'wxTreeItemId's to the > data, something like: > > T =3D { } > local id =3D tree:AppendItem ("Foo") > T[id] =3D "this is my very important data" > > For my surprise, 'wxTreeItemId's seem to be "variable". If I select > the same tree item many times, I get a different 'wxTreeItemId' every > time. You are storing the pointer (the memory location) to the specific wxTreeItemId instance that is returned. You need to use T =3D { } local id =3D tree:AppendItem ("Foo") T[id:GetValue()] =3D "this is my very important data" > I also tried to add a dummy 'wxTreeItemData' to every item, and use it > to index a table, like above. But, again, the 'wxTreeItemData' seems > to not be preserved... > > In C++, I used to create a class derived from 'wxTreeItemData' and use > 'wxTreeCtrl::SetItemData()', but I'd like very much to avoid using C++ > for this. So, is there a "pure Lua" solution for this? There is a class wxLuaTreeItemData that you can use instead of wxTreeItemData that stores a double valued number. It has the methods double GetValue() and SetValue(double num). You can use this to create more complicated mappings if you like. Regards, John Labenski ---------------------------------------------------------------------------= -- Here's an updated sample program which shows what you want to do I think (works in GTK at least). I've pasted it here since the Sourceforge CVS takes so long ---------------------------------------------------------------------------= -- -- Name: tree.wx.lua -- Purpose: wxTreeCtrl wxLua sample -- Author: J Winwood -- Modified by: -- Created: 16/11/2001 -- RCS-ID: -- Copyright: (c) 2001 J Winwood. All rights reserved. -- Licence: wxWidgets licence ---------------------------------------------------------------------------= -- function main() frame =3D wx.wxFrame( wx.wxNull, wx.wxID_ANY, "wxLua wxTreeCtrl Sample", wx.wxDefaultPosition, wx.wxSize(450, 400), wx.wxDEFAULT_FRAME_STYLE ) -- create the menubar and attach it local fileMenu =3D wx.wxMenu() fileMenu:Append(wx.wxID_EXIT, "E&xit", "Quit the program") local helpMenu =3D wx.wxMenu() helpMenu:Append(wx.wxID_ABOUT, "&About", "About the wxLua wxTreeCtrl Sample") local menuBar =3D wx.wxMenuBar() menuBar:Append(fileMenu, "&File") menuBar:Append(helpMenu, "&Help") frame:SetMenuBar(menuBar) -- connect the selection event of the exit menu item to an -- event handler that closes the window frame:ConnectEvent(wx.wxID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) frame:Close(true) end ) -- connect the selection event of the about menu item frame:ConnectEvent(wx.wxID_ABOUT, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) wx.wxMessageBox('This is the "About" dialog of the wxLua wxTreeCtrl sample.', "About wxLua", wx.wxOK + wx.wxICON_INFORMATION, frame) end ) -- create our treectrl tree =3D wx.wxTreeCtrl( frame, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxTR_LINES_AT_ROOT + wx.wxTR_HAS_BUTTONS ) -- create our log window textCtrl =3D wx.wxTextCtrl( frame, wx.wxID_ANY, "", wx.wxDefaultPosition, wx.wxSize(-1, 200), wx.wxTE_READONLY + wx.wxTE_MULTILINE ) rootSizer =3D wx.wxFlexGridSizer(0, 1, 0, 0) rootSizer:AddGrowableCol(0) rootSizer:AddGrowableRow(0) rootSizer:AddWindow( tree, 0, wx.wxGROW+wx.wxALIGN_CENTER_HORIZONTAL, 0= ) rootSizer:AddWindow( textCtrl, 0, wx.wxGROW+wx.wxALIGN_CENTER_HORIZONTAL, 0 ) frame:SetSizer( rootSizer ) -- create a table to store any extra information for each node like thi= s -- you don't have to store the id in the table, but it might be useful -- treedata[id] =3D { id=3Dwx.wxTreeCtrlId, data=3D"whatever data we wa= nt" } treedata =3D {} local root_id =3D tree:AddRoot( "Root" ) treedata[root_id:GetValue()] =3D { id =3D root_id:GetValue(), data =3D "I'm the root item" } for idx =3D 0, 10 do local parent_id =3D tree:AppendItem( root_id, "Parent ("..idx..")" = ) treedata[parent_id:GetValue()] =3D { id =3D parent_id:GetValue(), data =3D "I'm the data for Parent ("..idx..")" } for jdx =3D 0, 5 do local child_id =3D tree:AppendItem( parent_id, "Child ("..idx..", "..jdx..")" ) treedata[child_id:GetValue()] =3D { id =3D child_id:GetValue(), data =3D "I'm the child data for Parent ("..idx..", "..jdx..")" } end if (idx =3D=3D 2) or (idx =3D=3D 5) then tree:Expand(parent_id) end end -- create a nice string using the wxTreeItemId and our table of "data" function CreateLogString(treeitem_id) local value =3D treeitem_id:GetValue() local str =3D "wxTreeItemId:GetValue():"..tostring(value) str =3D str.." Data: '"..treedata[value].data.."'" return str end -- connect to some events from the wxTreeCtrl tree:ConnectEvent( wx.wxEVT_COMMAND_TREE_ITEM_EXPANDING, function( event ) local item_id =3D event:GetItem() local str =3D "Item expanding : "..CreateLogString(item_id).."\= n" textCtrl:AppendText(str) end ) tree:ConnectEvent( wx.wxEVT_COMMAND_TREE_ITEM_COLLAPSING, function( event ) local item_id =3D event:GetItem() local str =3D "Item collapsing : "..CreateLogString(item_id).."= \n" textCtrl:AppendText(str) end ) tree:ConnectEvent( wx.wxEVT_COMMAND_TREE_ITEM_ACTIVATED, function( event ) local item_id =3D event:GetItem() local str =3D "Item activated : "..CreateLogString(item_id).."\= n" textCtrl:AppendText(str) end ) tree:ConnectEvent( wx.wxEVT_COMMAND_TREE_SEL_CHANGED, function( event ) local item_id =3D event:GetItem() local str =3D "Item sel changed : "..CreateLogString(item_id)..= "\n" textCtrl:AppendText(str) end ) tree:Expand(root_id) wx.wxGetBaseApp():SetTopWindow(frame) frame:Show(true) end main() |