1 /////////////////////////////////////////////////////////////////////////////
2 // Purpose:     Interface to a console to help debug wxLua
3 // Author:      John Labenski, Francis Irving
4 // Created:     16/01/2002
5 // Copyright:   (c) 2012 John Labenski
6 // Copyright:   (c) 2002 Creature Labs. All rights reserved.
7 // Licence:     wxWidgets licence
8 /////////////////////////////////////////////////////////////////////////////
9 
10 #ifndef WX_LUA_CONSOLE_H
11 #define WX_LUA_CONSOLE_H
12 
13 #include <wx/frame.h>
14 #include <wx/filename.h>
15 
16 #include "wxlua/wxlua.h"
17 
18 class WXDLLIMPEXP_FWD_CORE wxSplitterWindow;
19 class WXDLLIMPEXP_FWD_CORE wxTextCtrl;
20 
21 enum wxLuaConsole_WindowIds
22 {
23     ID_WXLUACONSOLE                  = wxID_HIGHEST + 10,
24     ID_WXLUACONSOLE_SCROLLBACK_LINES,
25     ID_WXLUACONSOLE_BACKTRACE
26 };
27 
28 // ----------------------------------------------------------------------------
29 // wxLuaConsole - define a console class to display print statements
30 // ----------------------------------------------------------------------------
31 
32 class WXDLLIMPEXP_WXLUA wxLuaConsole : public wxFrame
33 {
34 public:
35     wxLuaConsole(wxWindow* parent, wxWindowID id = ID_WXLUACONSOLE,
36                  const wxString& title = wxT("wxLua console"),
37                  const wxPoint& pos = wxDefaultPosition,
38                  const wxSize& size = wxSize(300, 400),
39                  long style = wxDEFAULT_FRAME_STYLE,
40                  const wxString& name = wxT("wxLuaConsole"));
41 
42     virtual ~wxLuaConsole();
43 
44     /// Override from base class.
45     virtual bool Destroy();
46 
47     /// Get the first/current wxLuaConsole as a singleton object.
48     /// Returns NULL if !create_on_demand and there isn't one existing.
49     /// Do not keep a handle to the console past any function call since
50     /// the user may close it and the handle will be invalidated.
51     static wxLuaConsole* GetConsole(bool create_on_demand = false);
52     /// Returns true if there is an active console.
53     static bool HasConsole();
54 
55     /// Get the set wxLuaState.
GetLuaState()56     wxLuaState GetLuaState() const { return m_luaState; }
57     /// Set a wxLuaState to show backtraces from.
SetLuaState(const wxLuaState & wxlState)58     void SetLuaState(const wxLuaState& wxlState ) { m_luaState = wxlState; }
59 
60     /// Display a message in the console.
61     void AppendText(const wxString& msg);
62     /// Display a message in the console with optional wxTextCtrl attribute to display it with.
63     void AppendTextWithAttr(const wxString& msg, const wxTextAttr& attr);
64 
65     /// Remove lines so there are only max_lines, returns false if nothing is changed.
66     bool SetMaxLines(int max_lines = 500);
67     /// Get the maximum number of lines to show in the textcontrol before removing the earliest ones.
GetMaxLines()68     int  GetMaxLines() const { return m_max_lines; }
69 
70     /// Display the stack, but only if there are any items in it.
71     /// This only works while Lua is running.
72     void DisplayStack(const wxLuaState& wxlState);
73 
74     /// Set if wxExit() will be called with this dialog is closed to exit the app.
75     /// Use this when an error has occurred so the program doesn't continue.
SetExitWhenClosed(bool do_exit)76     void SetExitWhenClosed(bool do_exit) { m_exit_when_closed = do_exit; }
77     /// Get whether the program will exit when this dialog is closed.
GetExitWhenClosed()78     bool GetExitWhenClosed() const       { return m_exit_when_closed; }
79 
80 protected:
81     void OnCloseWindow(wxCloseEvent& event);
82     void OnMenu(wxCommandEvent& event);
83 
84     wxTextCtrl          *m_textCtrl;
85     bool                 m_exit_when_closed;
86     int                  m_max_lines;
87     wxFileName           m_saveFilename;
88 
89     wxLuaState           m_luaState;
90 
91     static wxLuaConsole* sm_wxluaConsole;
92 
93 private:
94     DECLARE_EVENT_TABLE()
95 };
96 
97 // ----------------------------------------------------------------------------
98 // Functions
99 // ----------------------------------------------------------------------------
100 
101 /// Reconnect stdin, stdout and stderr to a DOS console that is optionally allocated.
102 /// Normally stdout/stdin/stderr goes nowhere in a MSW GUI app and this corrects that.
103 /// This function does nothing when called from any other OS.
104 void WXDLLIMPEXP_WXLUA wxlua_RedirectIOToDosConsole(bool alloc_new_if_needed,
105 													short max_console_lines = 500);
106 
107 
108 #endif // WX_LUA_CONSOLE_H
109