Graphical Remote Lua Debugger, a debugger for the lua programming language.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

376 lines
12 KiB

-- see copyright notice in wxLdb.lua
local print = print
local wx = require( "wx" )
_G.print = print -- override wx print function with original one
local socket = require( "socket" )
local mainthread = require( "mainthread" )
local ui =
{
threads = require( "ui.threads" ),
callstack = require( "ui.callstack" ),
sourcePage = require( "ui.sourcePage" ),
promptMountPath = require( "ui.promptMountPath" ),
id = require( "ui.id" ),
luaExplorer = require( "ui.luaExplorer" ),
notification = require( "ui.notification" ),
about = require( "ui.about" ),
}
local setmetatable = setmetatable
local table = table
local debug = debug
local xpcall = xpcall
local pairs = pairs
local assert = assert
local string = string
local type = type
local os = os
local io = io
module( "ui.mainWindow" )
ID_BREAK = ui.id.new()
ID_CONTINUE = ui.id.new()
ID_STEP_OVER = ui.id.new()
ID_STEP_INTO = ui.id.new()
ID_STEP_OUT = ui.id.new()
ID_TOGGLE_BREAKPOINT = ui.id.new()
ID_FILE_OPEN = ui.id.new()
ID_FILE_CLOSE = ui.id.new()
ID_HELP_MANUAL = ui.id.new()
ID_HELP_ABOUT = ui.id.new()
ID_EXIT = wx.wxID_EXIT
local meta = { __index = {} }
function new()
local res = {}
setmetatable( res, meta )
res:init()
return res
end
function meta.__index:init()
self.frame = wx.wxFrame( wx.NULL, wx.wxID_ANY, "GRLD server", wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxDEFAULT_FRAME_STYLE + wx.wxMAXIMIZE)
mainthread.init( self.frame )
8 years ago
self:initLayout_()
8 years ago
self.mountPathPopup = ui.promptMountPath.new()
self.notificationPopup = ui.notification.new()
8 years ago
self.idleUpdates = {}
self.frame:Connect( wx.wxEVT_IDLE, function( event ) self:onIdleUpdate_( event ) end )
8 years ago
self.events = {
onBreakPointChanged = {},
onFileOpen = {},
onFileClosed = {},
onApplicationExiting = {},
onScrollChanged = {}
}
end
function meta.__index:show( show )
self.frame:Show( show )
end
function meta.__index:close()
self.frame:Close()
end
function meta.__index:initLayout_()
self.root = wx.wxSplitterWindow( self.frame, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize, 0 ) -- root widget
self.sourceBook = wx.wxNotebook( self.root, wx.wxID_ANY ) -- book of source code pages
self.debugRoot = wx.wxSplitterWindow( self.root, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize, 0 )
8 years ago
-- threads window
self.threads = ui.threads.new( self.debugRoot, self.frame )
8 years ago
self.debugBooks = wx.wxSplitterWindow( self.debugRoot, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize, 0 )
self.debugBookL = wx.wxNotebook( self.debugBooks, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxNB_BOTTOM )
self.debugBookR = wx.wxNotebook( self.debugBooks, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxNB_BOTTOM )
8 years ago
-- callstack window
self.callstack = ui.callstack.new( self.debugBookL )
self.debugBookL:AddPage( self.callstack:getRoot(), "Call stack" )
8 years ago
-- automatic variables window
self.auto = ui.luaExplorer.new( self.debugBookR, false )
self.debugBookR:AddPage( self.auto:getRoot(), "Automatic variables" )
8 years ago
-- watch window
self.watch = ui.luaExplorer.new( self.debugBookR, true )
self.debugBookR:AddPage( self.watch:getRoot(), "Watch" )
8 years ago
self.root:SplitHorizontally( self.sourceBook, self.debugRoot, -200 )
self.debugRoot:SplitVertically( self.threads:getRoot(), self.debugBooks, 250 )
self.debugBooks:SplitVertically( self.debugBookL, self.debugBookR )
8 years ago
self.sourcePages = {}
8 years ago
local fileMenu = wx.wxMenu()
fileMenu:Append( ID_FILE_OPEN, "&Open\tCtrl-O", "Open a source file" )
fileMenu:Append( ID_FILE_CLOSE, "&Close\tCtrl-F4", "Close the current source file" )
fileMenu:Append( ID_EXIT, "E&xit\tAlt-F4", "Exit the GRLD server" )
8 years ago
local debugMenu = wx.wxMenu()
debugMenu:Append( ID_BREAK, "&Break\tF12", "Stop execution of the program at the next executed line of code" )
debugMenu:Append( ID_CONTINUE, "&Continue\tF5", "Run the program at full speed" )
debugMenu:Append( ID_STEP_OVER, "&Step over\tF10", "Step over a line" )
debugMenu:Append( ID_STEP_INTO, "Step &into\tF11", "Step into a line" )
debugMenu:Append( ID_STEP_OUT, "Step &out\tShift-F11", "Step out of the current function" )
debugMenu:Append( ID_TOGGLE_BREAKPOINT, "&Toggle breakpoint\tF9", "Toggle the breakpoint on the current line" )
8 years ago
local helpMenu = wx.wxMenu()
helpMenu:Append( ID_HELP_MANUAL, "&Manual", "Send the GRLD manual to your web browser" )
helpMenu:Append( ID_HELP_ABOUT, "&About", "Open a window with various information about GRLD" )
8 years ago
local menuBar = wx.wxMenuBar()
menuBar:Append( fileMenu, "&File" )
menuBar:Append( debugMenu, "&Debug" )
menuBar:Append( helpMenu, "&Help" )
self.frame:SetMenuBar( menuBar )
8 years ago
self.frame:Connect( ID_FILE_OPEN, wx.wxEVT_COMMAND_MENU_SELECTED, function( ... ) self:onFileOpen_( ... ) end )
self.frame:Connect( ID_FILE_CLOSE, wx.wxEVT_COMMAND_MENU_SELECTED, function( ... ) self:onFileClose_( ... ) end )
self.frame:Connect( ID_HELP_MANUAL, wx.wxEVT_COMMAND_MENU_SELECTED, function( ... ) self:onHelpManual_( ... ) end )
self.frame:Connect( ID_HELP_ABOUT, wx.wxEVT_COMMAND_MENU_SELECTED, function( ... ) self:onHelpAbout_( ... ) end )
self.frame:Connect( ID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED, function( ... ) self:onExitCommand_( ... ) end )
self.frame:Connect( wx.wxEVT_CLOSE_WINDOW, function( ... ) self:onWindowClosed_( ... ) end )
end
function meta.__index:registerEvent( ID, callback )
if type( ID ) == "string" then
assert( self.events[ID] ~= nil, "Unknown event name "..ID )
table.insert( self.events[ID], callback )
else
if self.events[ID] == nil then
self.events[ID] = {}
mainthread.execute( function()
self.frame:Connect( ID, wx.wxEVT_COMMAND_MENU_SELECTED, function( ... )
for _, callback in ipairs( self.events[ID] ) do
callback( ... )
end
end )
end )
end
table.insert( self.events[ID], callback )
end
end
function meta.__index:runEvents_( eventName, ... )
for _, callback in pairs( self.events[eventName] ) do
callback( ... )
end
end
function meta.__index:onFileOpen_( event )
local fullPath = nil
local fileDialog = wx.wxFileDialog( self.frame, "Open file", "", "", "Lua files (*.lua)|*.lua|Text files (*.txt)|*.txt|All files (*)|*", wx.wxOPEN + wx.wxFILE_MUST_EXIST )
if fileDialog:ShowModal() == wx.wxID_OK then
fullPath = fileDialog:GetPath()
end
fileDialog:Destroy()
8 years ago
if fullPath ~= nil then
self:runEvents_( "onFileOpen", fullPath )
end
end
function meta.__index:onFileClose_( event )
local idx = self.sourceBook:GetSelection()
if idx >= 0 and idx < self.sourceBook:GetPageCount() then
local page = nil
local source = nil
for s, p in pairs( self.sourcePages ) do
if p.pageIdx == idx then
page = p
source = s
elseif p.pageIdx > idx then
p.pageIdx = p.pageIdx - 1
end
end
assert( page ~= nil )
self.sourceBook:DeletePage( page.pageIdx )
page:destroy()
self.sourcePages[source] = nil
self:runEvents_( "onFileClosed", source )
end
end
function meta.__index:onHelpManual_()
local docPath = "../doc/index.html"
8 years ago
-- get the command to start the default web browser from the registry (MS Windows only)
local pipe = io.popen( "reg query HKEY_CLASSES_ROOT\\HTTP\\shell\\open\\command" )
local data = pipe:read( "*a" )
pipe:close()
8 years ago
-- example of returned data (Windows XP):
--! REG.EXE VERSION 3.0
--
--HKEY_CLASSES_ROOT\HTTP\shell\open\command
-- <SANS NOM> REG_SZ "E:\Outils\Firefox\firefox.exe" -requestPending -osint -url "%1"
8 years ago
-- other example (on Vista)
--HKEY_CLASSES_ROOT\HTTP\shell\open\command
-- (par d<EFBFBD>faut) REG_SZ "C:\Program Files\Mozilla Firefox\firefox.exe" -requestPending -osint -url "%1"
8 years ago
-- parse returned data
local _, _, cmd = string.find( data, "REG_SZ%s+(.+)" )
local result = -1
if cmd ~= nil then
if string.find( cmd, "%%1" ) ~= nil then
cmd = string.gsub( cmd, "%%1", docPath )
else
if string.sub( cmd, -1 ) ~= " " then
cmd = cmd.." "
end
cmd = cmd.."\""..docPath.."\""
end
print( cmd )
8 years ago
-- start the default browser with the GRLD documentation as parameter
result = os.execute( "start \"GRLD documentation\" "..cmd )
end
if result ~= 0 then
wx.wxMessageBox( "Unable to start your default web browser to display the GRLD manual. You can instead open the following file with any HTML or text viewer: "..string.sub(docPath,4), "Error" )
end
end
function meta.__index:onHelpAbout_()
ui.about.popup()
end
function meta.__index:onExitCommand_( event )
self.frame:Close()
end
function meta.__index:onWindowClosed_( event )
print( "Main window closed" )
self:runEvents_( "onApplicationExiting" )
event:Skip( true ) -- allow it to really exit
self.mountPathPopup:destroy()
self.notificationPopup:destroy()
self.frame:Destroy()
end
function meta.__index:getSourcePages()
return self.sourcePages
end
function meta.__index:getSourcePage( source )
local page = self.sourcePages[source]
if page == nil then
page = mainthread.execute( function() return ui.sourcePage.new( self.sourceBook, source ) end )
self.sourcePages[source] = page
local _, _, name = string.find( source, ".*[/\\](.*)" )
if name == nil then name = source end
local maxLen = 16
if #name > 16 then
name = string.sub( name, 1, 7 ).."..."..string.sub( name, -7 )
end
page.pageIdx = self.sourceBook:GetPageCount()
self.sourceBook:AddPage( page:getRoot(), name )
page:registerEvent( "onBreakPointChanged", function( ... ) self:runEvents_( "onBreakPointChanged", source, ... ) end )
page:registerEvent( "onScrollChanged", function( ... ) self:runEvents_( "onScrollChanged", source, ... ) end )
end
return page
end
function meta.__index:findSourcePage( source )
return self.sourcePages[source]
end
function meta.__index:setSourcePageFocus( source )
local page = self:getSourcePage( source )
self.sourceBook:ChangeSelection( page.pageIdx )
end
function meta.__index:findSourcePageFocus()
local idx = self.sourceBook:GetSelection()
if idx >= 0 and idx < self.sourceBook:GetPageCount() then
local page = nil
local source = nil
for s, p in pairs( self.sourcePages ) do
if p.pageIdx == idx then
return s, p:getFocus()
end
end
end
return nil
end
function meta.__index:clearMarkers()
for _, page in pairs( self.sourcePages ) do
page:clearMarkers()
end
end
function meta.__index:clearOtherLines()
for _, page in pairs( self.sourcePages ) do
page:clearOtherLines()
end
end
function meta.__index:clearBreakPoints()
for _, page in pairs( self.sourcePages ) do
page:clearBreakPoints()
end
end
function meta.__index:setCurrentLine( source, line )
for _, page in pairs( self.sourcePages ) do
page:setCurrentLine( nil )
end
local page = self:getSourcePage( source )
page:setCurrentLine( line )
end
function meta.__index:addIdleUpdate( func )
table.insert( self.idleUpdates, func )
end
function meta.__index:promptMountPath( ... )
return self.mountPathPopup:run( ... )
end
function meta.__index:notification( text )
return self.notificationPopup:run( text )
end
function meta.__index:setActive()
self.active = true
end
function meta.__index:raise()
self.frame:Raise()
end
function meta.__index:onIdleUpdate_( event )
local currentPageIdx = self.sourceBook:GetSelection()
for _, page in pairs( self.sourcePages ) do
if currentPageIdx == page.pageIdx then
if page:update() then
assert( string.sub( page.source, 1, 1 ) == "@" )
self:runEvents_( "onFileOpen", string.sub( page.source, 2 ) )
end
end
end
8 years ago
self.active = false
for _, func in pairs( self.idleUpdates ) do
local ok, msg = xpcall( func, debug.traceback )
if not ok then print( "Error in idle update: "..msg ) end
end
if not self.active then
--socket.sleep( 0.05 )
end
event:RequestMore( true )
end