On 4/22/06, Leandro Motta Barros <[EMAIL PROTECTED]> 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 = { }
> local id = tree:AppendItem ("Foo")
> T[id] = "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 = { }
local id = tree:AppendItem ("Foo")
T[id:GetValue()] = "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 = 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 = wx.wxMenu()
fileMenu:Append(wx.wxID_EXIT, "E&xit", "Quit the program")
local helpMenu = wx.wxMenu()
helpMenu:Append(wx.wxID_ABOUT, "&About", "About the wxLua
wxTreeCtrl Sample")
local menuBar = 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 = wx.wxTreeCtrl( frame, wx.wxID_ANY,
wx.wxDefaultPosition, wx.wxDefaultSize,
wx.wxTR_LINES_AT_ROOT + wx.wxTR_HAS_BUTTONS )
-- create our log window
textCtrl = wx.wxTextCtrl( frame, wx.wxID_ANY, "",
wx.wxDefaultPosition, wx.wxSize(-1, 200),
wx.wxTE_READONLY + wx.wxTE_MULTILINE )
rootSizer = 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 this
-- you don't have to store the id in the table, but it might be useful
-- treedata[id] = { id=wx.wxTreeCtrlId, data="whatever data we want" }
treedata = {}
local root_id = tree:AddRoot( "Root" )
treedata[root_id:GetValue()] = { id = root_id:GetValue(), data =
"I'm the root item" }
for idx = 0, 10 do
local parent_id = tree:AppendItem( root_id, "Parent ("..idx..")" )
treedata[parent_id:GetValue()] = { id = parent_id:GetValue(),
data = "I'm the data for Parent ("..idx..")" }
for jdx = 0, 5 do
local child_id = tree:AppendItem( parent_id, "Child
("..idx..", "..jdx..")" )
treedata[child_id:GetValue()] = { id =
child_id:GetValue(), data = "I'm the child data for Parent ("..idx..",
"..jdx..")" }
end
if (idx == 2) or (idx == 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 = treeitem_id:GetValue()
local str = "wxTreeItemId:GetValue():"..tostring(value)
str = 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 = event:GetItem()
local str = "Item expanding : "..CreateLogString(item_id).."\n"
textCtrl:AppendText(str)
end )
tree:ConnectEvent( wx.wxEVT_COMMAND_TREE_ITEM_COLLAPSING,
function( event )
local item_id = event:GetItem()
local str = "Item collapsing : "..CreateLogString(item_id).."\n"
textCtrl:AppendText(str)
end )
tree:ConnectEvent( wx.wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
function( event )
local item_id = event:GetItem()
local str = "Item activated : "..CreateLogString(item_id).."\n"
textCtrl:AppendText(str)
end )
tree:ConnectEvent( wx.wxEVT_COMMAND_TREE_SEL_CHANGED,
function( event )
local item_id = event:GetItem()
local str = "Item sel changed : "..CreateLogString(item_id).."\n"
textCtrl:AppendText(str)
end )
tree:Expand(root_id)
wx.wxGetBaseApp():SetTopWindow(frame)
frame:Show(true)
end
main()
-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Wxlua-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wxlua-users