forked from rjpcomputing/luaforwindows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.wx.wlua
More file actions
134 lines (115 loc) · 5.32 KB
/
tree.wx.wlua
File metadata and controls
134 lines (115 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
-----------------------------------------------------------------------------
-- 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
-----------------------------------------------------------------------------
-- Load the wxLua module, does nothing if running from wxLua, wxLuaFreeze, or wxLuaEdit
package.cpath = package.cpath..";./?.dll;./?.so;../lib/?.so;../lib/vc_dll/?.dll;../lib/bcc_dll/?.dll;../lib/mingw_dll/?.dll;"
require("wx")
-- 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
function main()
frame = wx.wxFrame( wx.NULL, 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:Connect(wx.wxID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED,
function (event)
frame:Close(true)
end )
-- connect the selection event of the about menu item
frame:Connect(wx.wxID_ABOUT, wx.wxEVT_COMMAND_MENU_SELECTED,
function (event)
wx.wxMessageBox('This is the "About" dialog of the wxLua wxTreeCtrl sample.\n'..
wxlua.wxLUA_VERSION_STRING.." built with "..wx.wxVERSION_STRING,
"About wxLua",
wx.wxOK + wx.wxICON_INFORMATION,
frame)
end )
-- create our treectrl
tree = wx.wxTreeCtrl( frame, wx.wxID_ANY,
wx.wxDefaultPosition, wx.wxSize(-1, 200),
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:Add( tree, 0, wx.wxGROW+wx.wxALIGN_CENTER_HORIZONTAL, 0 )
rootSizer:Add( textCtrl, 0, wx.wxGROW+wx.wxALIGN_CENTER_HORIZONTAL, 0 )
frame:SetSizer( rootSizer )
frame:Layout() -- help sizing the windows before being shown
-- 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
-- connect to some events from the wxTreeCtrl
tree:Connect( 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:Connect( 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:Connect( 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:Connect( 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.wxGetApp():SetTopWindow(frame)
frame:Show(true)
end
main()
-- Call wx.wxGetApp():MainLoop() last to start the wxWidgets event loop,
-- otherwise the wxLua program will exit immediately.
-- Does nothing if running from wxLua, wxLuaFreeze, or wxLuaEdit since the
-- MainLoop is already running or will be started by the C++ program.
wx.wxGetApp():MainLoop()