From: John L. <jr...@us...> - 2007-06-08 01:36:49
|
Update of /cvsroot/wxlua/wxLua/samples In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv15480/wxLua/samples Added Files: bindings.wx.lua Log Message: Add a simple method to get the binding info, seems small and works well Addd sample lua program bindings.wx.lua to show them in a listctrl Fix incircles to work with new bindings Put the "name" of the struct binding items first always --- NEW FILE: bindings.wx.lua --- ----------------------------------------------------------------------------- -- Name: bindings.wx.lua -- Purpose: Show wxLua bindings in a wxListCtrl or dump them using print -- Author: john Labenski -- Modified by: -- Created: 5/7/2007 -- RCS-ID: -- Copyright: (c) John Labenski -- Licence: wxWidgets licence ----------------------------------------------------------------------------- -- ---------------------------------------------------------------------------- -- Brute force dump of the binding info using print statements for debugging -- ---------------------------------------------------------------------------- function ColumnDumpTable(t, keys) local lens = {} for i = 1, #keys do lens[i] = string.len(keys[i]) end for i = 1, #t do local u = t[i] for k = 1, #keys do local len = string.len(tostring(u[keys[k]])) if (len > lens[k]) then lens[k] = len end end end local s = "" for k = 1, #keys do local val = tostring(keys[k]) local buf = string.rep(" ", lens[k] - string.len(val) + 1) s = s..val..buf end print(s) for i = 1, #t do local u = t[i] local s = "" for k = 1, #keys do local val = tostring(u[keys[k]]) local buf = string.rep(" ", lens[k] - string.len(val) + 1) s = s..val..buf end print(s) end end function DumpBindingInfo(binding) print("Binding Name : "..tostring(binding.GetBindingName)) print("Lua Namespace : "..tostring(binding.GetLuaNamespace)) print("Class Count : "..tostring(binding.GetClassCount)) print("Define Count : "..tostring(binding.GetDefineCount)) print("Event Count : "..tostring(binding.GetEventCount)) print("Object Count : "..tostring(binding.GetObjectCount)) print("Function Count : "..tostring(binding.GetFunctionCount)) if true then print("\nDUMPING binding.GetClassArray ==================================\n") local keys = { "name", "methods", "methods_n", "classInfo", "class_tag", "baseclassName", "baseclass", "enums", "enums_n" } ColumnDumpTable(binding.GetClassArray, keys) end if true then print("\nDUMPING binding.GetFunctionArray ==================================\n") local keys = { "name", "type", "funcs", "funcs_n", "basemethod" } ColumnDumpTable(binding.GetFunctionArray, keys) end if true then print("\nDUMPING binding.GetDefineArray ==================================\n") local keys = { "name", "value" } ColumnDumpTable(binding.GetDefineArray, keys) end if true then print("\nDUMPING binding.GetStringArray ==================================\n") local keys = { "name", "value" } ColumnDumpTable(binding.GetStringArray, keys) end if true then print("\nDUMPING binding.GetEventArray ==================================\n") local keys = { "name", "eventType", "class_tag" } ColumnDumpTable(binding.GetEventArray, keys) end if true then print("\nDUMPING binding.GetObjectArray ==================================\n") local keys = { "name", "object", "class_tag" } ColumnDumpTable(binding.GetObjectArray, keys) end end -- Call DumpBindingInfo(...) on the binding you want to show --DumpBindingInfo(wx.wxLuaBinding_wx) -- ---------------------------------------------------------------------------- -- A wxLua program to view the bindings in a wxListCtrl -- ---------------------------------------------------------------------------- ID_LISTCTRL = 1000 listData = {} -- store the table currently displayed by the listCtrl list_level = 1 img_normal = 0 img_folder = 1 listColWidths = {} -- stored by "object_type" name function SaveListColWidths(level) if not listColWidths[listData[level].object_type] then listColWidths[listData[level].object_type] = {} end for col = 1, listCtrl:GetColumnCount() do listColWidths[listData[level].object_type][col] = listCtrl:GetColumnWidth(col-1) end end function GotoBindingLevel(listCtrl, level) listCtrl:DeleteAllItems() local lc_item = 0 for col = 1, listCtrl:GetColumnCount() do listCtrl:DeleteColumn(0) end for col = 1, #listData[level].cols do listCtrl:InsertColumn(col-1, listData[level].cols[col], wx.wxLIST_FORMAT_LEFT, -1) if listData[level].folder and listData[level].folder[col] then listCtrl:SetColumnImage(col-1, img_folder) end end if listColWidths[listData[level].object_type] then for col = 1, #listColWidths[listData[level].object_type] do listCtrl:SetColumnWidth(col-1, listColWidths[listData[level].object_type][col]) end end for i = 1, #listData[level] do local d = listData[level][i] local info = wx.wxListItem() info:SetId(lc_item+1) info:SetText(tostring(d[1])) lc_item = listCtrl:InsertItem(info) info:SetId(lc_item) if (d[1] ~= "..") then for col = 2, #listData[level].cols do listCtrl:SetItem(lc_item, col-1, tostring(d[col])) end end end end function CreatewxLuaBindClass(tbl) local keys = { "name", "methods", "methods_n", "classInfo", "class_tag", "baseclassName", "baseclass", "enums", "enums_n" } local t = CreatewxLuaBindTable(tbl, keys, "wxLuaBindClass") -- these are expandable t.folder = {} t.folder[2] = true t.folder[7] = true t.folder[8] = true return t end function CreatewxLuaBindMethod(tbl) local keys = { "name", "type", "funcs", "funcs_n", "basemethod" } local t = CreatewxLuaBindTable(tbl, keys, "wxLuaBindMethod") -- these are expandable t.folder = {} t.folder[3] = true t.folder[5] = true return t end function CreatewxLuaBindDefine(tbl) local keys = { "name", "value" } return CreatewxLuaBindTable(tbl, keys, "wxLuaBindDefine") end function CreatewxLuaBindString(tbl) local keys = { "name", "value" } return CreatewxLuaBindTable(tbl, keys, "wxLuaBindString") end function CreatewxLuaBindEvent(tbl) local keys = { "name", "eventType", "class_tag" } return CreatewxLuaBindTable(tbl, keys, "wxLuaBindEvent") end function CreatewxLuaBindObject(tbl) local keys = { "name", "object", "class_tag" } return CreatewxLuaBindTable(tbl, keys, "wxLuaBindObject") end function CreatewxLuaBindCFunc(tbl) local keys = { "func", "type", "minargs", "maxargs", "argtags", "argtags_name" } local t = CreatewxLuaBindTable(tbl, keys, "wxLuaBindCFunc") for i = 2, #t do t[i][5] = table.concat(t[i][5], ", ") t[i][6] = table.concat(t[i][6], ", ") end return t end function CreatewxLuaBindTable(tbl, keys, object_type) local t = { {".."}, ["cols"] = keys, ["object_type"] = object_type } for i = 1, #tbl do local item = {} for k = 1, #keys do -- we need to force there to be something in each col, use a 0 local val = tbl[i][keys[k]] if val ~= nil then table.insert(item, val) else table.insert(item, 0) end end table.insert(t, item) end return t end function main() frame = wx.wxFrame(wx.NULL, wx.wxID_ANY, "wxLua Binding Browser") panel = wx.wxPanel(frame, wx.wxID_ANY) listCtrl = wx.wxListView(panel, ID_LISTCTRL, wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxLC_REPORT + wx.wxLC_SINGLE_SEL + wx.wxLC_HRULES + wx.wxLC_VRULES) imageList = wx.wxImageList(16, 16, true) imageList:Add(wx.wxArtProvider.GetBitmap(wx.wxART_NORMAL_FILE, wx.wxART_MENU)) imageList:Add(wx.wxArtProvider.GetBitmap(wx.wxART_FILE_OPEN, wx.wxART_MENU)) listCtrl:SetImageList(imageList, wx.wxIMAGE_LIST_SMALL); listData[1] = { {"wx", "wxLuaBinding_wx"}, {"wx", "wxLuaBinding_wxstc"}, {"wx", "wxLuaBinding_wxluasocket"}, ["cols"] = { "Lua Namespace", "Binding userdata" }, ["object_type"] = "Root" } GotoBindingLevel(listCtrl, 1) listCtrl:Connect(wx.wxEVT_COMMAND_LIST_ITEM_ACTIVATED, function(event) local index = event:GetIndex() local itemText = listCtrl:GetItemText(index) -- Find what column we're in --local col = event:GetColumn() -- both of these don't work in MSW --local pt = event:GetPoint() local mousePos = wx.wxGetMousePosition() local framePos = frame:GetPosition() local winPos = listCtrl:GetPosition() local scrollPos = listCtrl:GetScrollPos(wx.wxHORIZONTAL) local x = mousePos:GetX() + scrollPos - framePos:GetX() - winPos:GetX() local w = 0 --print(x, mousePos:GetX(), scrollPos, framePos:GetX(), winPos:GetX()) for c = 1, listCtrl:GetColumnCount() do w = w + listCtrl:GetColumnWidth(c-1) if x < w then col = c-1 break end end SaveListColWidths(list_level) if (itemText == "..") then list_level = list_level - 1 GotoBindingLevel(listCtrl, list_level) elseif (list_level == 1) then local binding = _G for i = 1, #listData[1][index+1] do binding = binding[listData[1][index+1][i]] end listData[2] = { {".."}, {"GetBindingName", tostring(binding.GetBindingName)}, {"GetLuaNamespace", tostring(binding.GetLuaNamespace)}, {"GetClassCount", tostring(binding.GetClassCount)}, {"GetDefineCount", tostring(binding.GetDefineCount)}, {"GetEventCount", tostring(binding.GetEventCount)}, {"GetObjectCount", tostring(binding.GetObjectCount)}, {"GetFunctionCount", tostring(binding.GetFunctionCount)}, {"GetClassArray", "Click to view classes"}, {"GetFunctionArray", "Click to view functions"}, {"GetDefineArray", "Click to view defines"}, {"GetStringArray", "Click to view strings"}, {"GetEventArray", "Click to view events"}, {"GetObjectArray", "Click to view objects"}, ["cols"] = {"Function Name", "Value"}, ["folder"] = { false, true }, ["binding"] = binding, ["object_type"] = "wxLuaBinding" } list_level = list_level + 1 GotoBindingLevel(listCtrl, list_level) elseif (list_level == 2) then local binding = listData[2].binding local t = nil if (itemText == "GetClassArray") then t = CreatewxLuaBindClass(binding.GetClassArray) elseif (itemText == "GetFunctionArray") then t = CreatewxLuaBindMethod(binding.GetFunctionArray) elseif (itemText == "GetDefineArray") then t = CreatewxLuaBindDefine(binding.GetDefineArray) elseif (itemText == "GetStringArray") then t = CreatewxLuaBindString(binding.GetStringArray) elseif (itemText == "GetEventArray") then t = CreatewxLuaBindEvent(binding.GetEventArray) elseif (itemText == "GetObjectArray") then t = CreatewxLuaBindObject(binding.GetObjectArray) end if t ~= nil then list_level = list_level + 1 listData[list_level] = t GotoBindingLevel(listCtrl, list_level) end elseif (index > 0) and listData[list_level].object_type == "wxLuaBindClass" then local t = nil if ((col == 0) or (col == 1)) and (type(listData[list_level][index+1][2]) == "table") then t = CreatewxLuaBindMethod(listData[list_level][index+1][2]) elseif (col == 6) and (type(listData[list_level][index+1][col+1]) == "userdata") then t = CreatewxLuaBindClass({listData[list_level][index+1][col+1]}) elseif (col == 7) and (type(listData[list_level][index+1][col+1]) == "table") then t = CreatewxLuaBindDefine(listData[list_level][index+1][col+1]) end if t ~= nil then list_level = list_level + 1 listData[list_level] = t GotoBindingLevel(listCtrl, list_level) end elseif (index > 0) and listData[list_level].object_type == "wxLuaBindMethod" then local t = nil if (col == 2) and (type(listData[list_level][index+1][col+1]) == "table") then t = CreatewxLuaBindCFunc(listData[list_level][index+1][col+1]) elseif (col == 4) and (type(listData[list_level][index+1][col+1]) == "userdata") then t = CreatewxLuaBindMethod({listData[list_level][index+1][col+1]}) end if t ~= nil then list_level = list_level + 1 listData[list_level] = t GotoBindingLevel(listCtrl, list_level) end end event:Skip(); end) rootSizer = wx.wxBoxSizer(wx.wxVERTICAL); rootSizer:Add(listCtrl, 1, wx.wxEXPAND + wx.wxALL, 5); rootSizer:SetMinSize(450, 400); panel:SetSizer(rootSizer); rootSizer:SetSizeHints(frame); frame:Show(true) end main() |