local fh = assert(io.open(arg[1] .. '.cpp')) local content = fh:read('*a') fh:close() local fh = assert(io.open(arg[1] .. '.h')) local hcontent = fh:read('*a') fh:close() io.output(arg[1] .. '.lua') function print(...) -- uncomment the following line for 5.1 -- local arg = {..., n = select('#', ...)} for i = 1, arg.n - 1 do io.write(tostring(arg[i]), "\t") end io.write(tostring(arg[arg.n]), "\n") end function split(str, pat) local t = {} -- NOTE: use {n = 0} in Lua-5.0 local fpat = '(.-)' .. pat local last_end = 1 local s, e, cap = str:find(fpat, 1) while s do if s ~= 1 or cap ~= '' then table.insert(t, cap) end last_end = e+1 s, e, cap = str:find(fpat, last_end) end if last_end <= #str then cap = str:sub(last_end) table.insert(t, cap) end return t end local firstFrame = nil function doConvertion(funcName, content) if funcName:find('~', 1, true) then --skip destructor return '' end if not firstFrame then firstFrame = funcName end local events = {} content = content:sub(2, -2) content = content:gsub('wxT%(([^)]+)%)', '_(%1)') content = content:gsub('wx%S+EventHandler%( [^:]+::([^%s]+) %)[^)]+', function(m) events[m] = true return 'function(event) self:' .. m .. '(event) end' end) content = content:gsub('wxArrayString (%S+);', 'self.%1 = {};') content = content:gsub('wxString (%S+)%[%] = {', 'self.%1 = {') content = content:gsub('int (%S+) = sizeof%( (%S+) %).-;', 'self.%1 = #self.%2;') content = content:gsub('\n[^\n]+NChoices =[^\n]+\n', '\n') content = content:gsub(',[^,]+NChoices,', ',') content = content:gsub('(%s+)(m_toolBar%S*) =', '%1self.%2 =') content = content:gsub('%( this,', '( lframe,') content = content:gsub('this%->', 'lframe:') content = content:gsub('wxNORMAL_FONT%->', 'wxNORMAL_FONT:') content = content:gsub('(%S+)%->', 'self.%1:') content = content:gsub('(wx[A-Z]+)', 'wx.%1') content = content:gsub('\n[^*\n]+%* %S+;\n', '\n') content = content:gsub('(%S+) = new ', 'self.%1 = ') content = content:gsub(' new ', ' ') content = content:gsub('(%S+%s?|[^,)]+)', ' bit.bor(%1)') content = content:gsub('|', ',') content = content:gsub('//', '--') content = content:gsub(':Add%( (%S+),', ':Add( self.%1,') content = content:gsub(' m_', ' self.m_') content = content:gsub(':SetSizer%( ', ':SetSizer( self.') content = content:gsub('%S+::', 'self.') content = content:gsub('m_statusBar', 'self.m_statusBar') content = content:gsub('NULL', 'nil') content = content:gsub('(wxComboBox.-), 0, nil, 0 %);', '%1);') print(string.format([[ %s = { frame = nil, } function %s:New(obj) obj = obj or {} -- create object if user does not provide one setmetatable(obj, self) self.__index = self return obj end function %s:Show(opt) if not self.frame then self:Init() end self.frame:Show(opt) end ]], funcName, funcName, funcName)) print('function ' .. funcName .. ':Init(parent)\n') hcontent:gsub('%s+' .. funcName .. '(%b())', function(m) --constructor = m --local constructor = m:sub(2,-2) local to, from, id, title, pos, size, style to, from, id = m:find('wxWindowID id = (%S+),') to, from, title = m:find('const wxString& title = wxT(%b()),') to, from, pos = m:find('const wxPoint& pos = (%S+),') to, from, size = m:find('const wxSize& size = wxSize(%b()),') to, from, style = m:find('long style = (.-) %)') --print(m) --print(id, title, pos, size, style) print([[ -- create the frame window self.frame = wx.wxFrame( parent or wx.NULL,]]) if id == 'wxID_ANY' then id = 'wx.' .. id end print('\t', id .. ',') if title then print('\t', '_' .. title .. ',') else print('\t\twx.wxEmptyString,') end print('\t\twx.wxDefaultPosition,') print('\t', 'wx.wxSize' .. size .. ',') local styleList = split(style, '|') local k, v, style2 style2 = {} for k,v in pairs(styleList) do style2[k] = 'wx.' .. v:gsub('%s+', '') end style = table.concat(style2, ' + ') print('\t', style) print('\t)') end) print('\tlocal lframe = self.frame') print(content) print('end\n') local k, v for k,v in pairs(events) do print('function ' .. funcName .. ':' .. k .. '(event)\n\tevent:Skip()\nend\n') end return '' end print(string.format([[ ----------------------------------------------------------------------------- -- Name: %s.wx.lua -- Purpose: My application purpose -- Author: Domingo Alvarez Duarte -- Modified by: -- Created: 16/04/2010 -- RCS-ID: $Id$ -- Copyright: (c) 2010 Domingo Alvarez Duarte. 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") -- Global functions/variables rawset( _G, "WIN32", os.getenv("WINDIR") ~= nil ) -- otherwise Posix function split(str, pat) local t = {} -- NOTE: use {n = 0} in Lua-5.0 local fpat = '(.-)' .. pat local last_end = 1 local s, e, cap = str:find(fpat, 1) while s do if s ~= 1 or cap ~= '' then table.insert(t, cap) end last_end = e+1 s, e, cap = str:find(fpat, last_end) end if last_end <= #str then cap = str:sub(last_end) table.insert(t, cap) end return t end wx.myIDCounter = wx.wxID_HIGHEST function wx.myNewID() local id = wx.myIDCounter + 1 wx.myIDCounter = id return id end function memoize(f) local mem = {} -- memoizing table setmetatable(mem, {__mode = "kv"}) -- make it weak return function (x) -- new version of �f�, with memoizing local r = mem[x] if r == nil then -- no previous result? r = f(x) -- calls original function mem[x] = r -- store result for reuse end return r end end -- Pick some reasonable fixed width fonts to use for the editor if wx.__WXMSW__ then wx.myFontSize = 12 wx.myFontBaseName = 'Verdana' else wx.myFontSize = 12 wx.myFontBaseName = '' end wx.myFontBase = wx.wxFont(wx.myFontSize, wx.wxFONTFAMILY_MODERN, wx.wxFONTSTYLE_NORMAL, wx.wxFONTWEIGHT_NORMAL, false, wx.myFontBaseName) wx.myFontBaseItalic = wx.wxFont(wx.myFontSize, wx.wxFONTFAMILY_MODERN, wx.wxFONTSTYLE_ITALIC, wx.wxFONTWEIGHT_NORMAL, false, wx.myFontBaseName) wx.myFontBaseBold = wx.wxFont(wx.myFontSize, wx.wxFONTFAMILY_MODERN, wx.wxFONTSTYLE_NORMAL, wx.wxFONTWEIGHT_BOLD , false, wx.myFontBaseName) wx.myFontBaseBoldItalic = wx.wxFont(wx.myFontSize, wx.wxFONTFAMILY_MODERN, wx.wxFONTSTYLE_ITALIC, wx.wxFONTWEIGHT_BOLD , false, wx.myFontBaseName) function wx.myShowMessage(msg) wx.wxMessageBox(msg, "My Application", wx.wxOK + wx.wxICON_EXCLAMATION, wx.NULL) end function dbBool(abool) if abool then return 't' else return 'f' end end function _(s) return s end _ = function(s) return s end wx.wxEmptyString = '' ]], arg[1])) content:gsub('::([^%s(]+)[^{]+(%b{})', doConvertion) print(string.format([[ function main() -- create the frame window local frame = %s:New() -- show the frame window 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() ]], firstFrame)) fho:close()