22 #include "wx/filename.h"
23 #include "wx/splitter.h"
24 #include "wx/confbase.h"
26 #include "wx/luawraps/wxledit.h"
27 #include "wxlua/debug/wxldebug.h"
28 #include "wxlua/debug/wxlstack.h"
30 #include "wx/stedit/stemenum.h"
31 #include "wx/stedit/steevent.h"
33 #include "../art/wxlua.xpm"
34 #include "../art/open.xpm"
35 #include "../art/save.xpm"
36 #include "../art/play.xpm"
37 #include "../art/stop.xpm"
39 #define STC_DEF_MARGIN_WIDTH 16
41 DEFINE_LOCAL_EVENT_TYPE(wxEVT_LUASHELL_wxLUA_CREATION)
42 DEFINE_LOCAL_EVENT_TYPE(wxEVT_LUAIDE_wxLUA_CREATION)
47 IMPLEMENT_ABSTRACT_CLASS(
wxLuaShell, wxSTEditorShell)
49 BEGIN_EVENT_TABLE(wxLuaShell, wxSTEditorShell)
50 EVT_STESHELL_ENTER ( wxID_ANY, wxLuaShell::OnSTEEvent )
51 EVT_LUA_PRINT ( wxID_ANY, wxLuaShell::OnLua )
52 EVT_LUA_ERROR ( wxID_ANY, wxLuaShell::OnLua )
55 void wxLuaShell::Init()
57 m_wxlstate_static =
false;
60 bool wxLuaShell::Create(wxWindow *parent, wxWindowID
id,
61 const wxPoint& pos,
const wxSize& size,
62 long style,
const wxString& name)
64 if (!wxSTEditorShell::Create(parent,
id, pos, size, style, name))
67 #if wxCHECK_VERSION(2,9,0)
68 SetFileName(wxFileName(name),
false);
69 SetLanguage(wxFileName(wxT(
"a.lua")));
71 SetFileName( name,
false);
72 SetLanguage( wxT(
"a.lua") );
78 wxLuaShell::~wxLuaShell()
80 if (m_wxlState.Ok() && !m_wxlstate_static)
82 m_wxlState.CloseLuaState(
true);
86 void wxLuaShell::SetwxLuaState(
const wxLuaState& wxlState,
bool is_static)
88 if (m_wxlState.Ok() && !m_wxlstate_static)
91 if (m_wxlState.IsRunning())
94 m_wxlState.DebugHookBreak();
97 m_wxlState.CloseLuaState(
true);
100 m_wxlState = wxlState;
101 m_wxlstate_static = is_static;
104 bool wxLuaShell::RecreatewxLuaState(wxEvtHandler *wxlStateEvtHandler,
int win_id)
107 wxLuaEvent event(wxEVT_LUASHELL_wxLUA_CREATION, GetId(), m_wxlState);
108 event.SetEventObject(
this);
110 event.SetExtraLong(win_id);
113 if (GetEventHandler()->ProcessEvent(event))
120 if (event.GetwxLuaState().Ok() && (
event.GetwxLuaState() != m_wxlState))
122 SetwxLuaState(event.GetwxLuaState(),
event.GetInt() == 1);
126 wxLuaState wxlState = wxLuaState(wxlStateEvtHandler, win_id);
127 wxlState.SetLuaDebugHook( LUA_MASKCOUNT, 1000, 50,
false );
128 SetwxLuaState(wxlState,
false);
131 return m_wxlState.Ok();
134 bool wxLuaShell::RunString(
const wxString& string_,
bool append_text)
138 wxString string(string_);
140 if (
string.Trim(
true).Trim(
false).IsEmpty())
145 if (!GetLine(GetLineCount()).IsEmpty())
146 AppendText(wxT(
"\n"));
151 AppendText(wxT(
"\n"));
154 if (
string == wxT(
"reset"))
156 if (m_wxlstate_static)
158 AppendText(wxT(
"Unable to reset lua, interpreter locked by another process.\n"));
162 if (RecreatewxLuaState(GetEventHandler(), GetId()))
164 AppendText(wxT(
"Successfully reset interpreter.\n"));
168 AppendText(wxT(
"Unable to reset interpreter.\n"));
171 else if (m_wxlState.Ok())
174 if (
string.Trim(
true).Find(wxT(
'=')) == 0)
175 string = wxT(
"return ") +
string.AfterFirst(
'=');
177 if (0 != m_wxlState.CompileString(
string))
180 if (0 == m_wxlState.CompileString(wxT(
"return ") +
string))
181 string = wxT(
"return ") + string;
184 int top1 = m_wxlState.lua_GetTop();
185 ret = m_wxlState.RunString(
string, wxT(
"wxLuaShell"), LUA_MULTRET) == 0;
186 int top2 = m_wxlState.lua_GetTop();
188 if (ret && (top2 > top1))
190 m_wxlState.lua_GetGlobal(
"tostring");
192 if (!m_wxlState.lua_IsFunction(-1))
195 AppendText(wxT(
"wxLua ERROR: Unable to print() without the tostring() function. Did you remove it?\n"));
196 m_wxlState.lua_Pop(1);
200 for (
int n = top1+1; n <= top2; n++)
202 m_wxlState.lua_PushValue(-1);
203 m_wxlState.lua_PushValue(n);
204 m_wxlState.lua_Call(1, 1);
206 AppendText(m_wxlState.lua_TowxString(-1) + wxT(
"\n"));
207 m_wxlState.lua_Pop(1);
211 m_wxlState.lua_SetTop(top1);
214 AppendText(wxT(
"The lua interpreter is not available - try running 'reset'.\n"));
221 size_t wxLuaShell::DoGetAutoCompleteKeyWords(
const wxString& root, wxArrayString& words)
223 size_t count = wxSTEditorShell::DoGetAutoCompleteKeyWords(root, words);
228 void wxLuaShell::OnSTEEvent(wxSTEditorEvent& event)
231 if (event.GetEventType() == wxEVT_STESHELL_ENTER)
234 wxString str =
event.GetString().Strip(wxString::both);
236 RunString(str,
false);
240 void wxLuaShell::OnLua(wxLuaEvent &event)
242 if (event.GetEventType() == wxEVT_LUA_PRINT)
244 AppendText(event.GetString() + wxT(
"\n"));
246 else if (event.GetEventType() == wxEVT_LUA_ERROR)
248 AppendText(wxT(
"\nError: ") + event.GetString() + wxT(
"\n"));
252 void wxLuaShell::UpdateItems(wxMenu *menu, wxMenuBar *menuBar, wxToolBar *toolBar)
254 bool is_running = m_wxlState.Ok() && m_wxlState.IsRunning();
256 wxSTEditorMenuManager::DoEnableItem(menu, menuBar, toolBar, ID_WXLUAIDE_RUN_LUA, !is_running);
257 wxSTEditorMenuManager::DoEnableItem(menu, menuBar, toolBar, ID_WXLUAIDE_BREAK_LUA, is_running);
258 wxSTEditorMenuManager::DoEnableItem(menu, menuBar, toolBar, ID_WXLUAIDE_SHOW_STACK, m_wxlState.Ok());
260 wxSTEditor::UpdateItems(menu, menuBar, toolBar);
269 EVT_STC_MARGINCLICK(wxID_ANY, wxLuaEditor::OnMarginClick)
270 EVT_MENU(ID_WXLUAIDE_TOGGLE_BREAKPOINT, wxLuaEditor::OnMenu)
273 void wxLuaEditor::Init()
278 bool wxLuaEditor::Create(wxWindow *parent,
int id,
279 const wxPoint& pos,
const wxSize& size,
280 long style,
const wxString& name)
282 if (!wxSTEditor::Create(parent,
id, pos, size, style, name))
285 SetVisiblePolicy(wxSTC_VISIBLE_SLOP, 3);
286 SetXCaretPolicy(wxSTC_CARET_SLOP, 10);
287 SetYCaretPolicy(wxSTC_CARET_SLOP, 3);
290 MarkerDefine(markerBreak, wxSTC_MARK_SMALLRECT, *wxBLACK, *wxRED);
291 MarkerDefine(markerError, wxSTC_MARK_SHORTARROW, *wxBLACK, *wxRED);
293 SetMarginSensitive(marginBreak,
true);
294 SetMarginType(marginError, wxSTC_MARGIN_SYMBOL);
295 SetMarginWidth(marginError, STC_DEF_MARGIN_WIDTH);
298 #if wxCHECK_VERSION(2,9,0)
299 SetFileName(wxFileName(wxT(
"untitled.lua")));
300 SetLanguage(wxFileName(wxT(
"untitled.lua")));
302 SetFileName( wxT(
"untitled.lua"),
false);
303 SetLanguage( wxT(
"untitled.lua") );
308 void wxLuaEditor::OnMenu(wxCommandEvent& event)
310 HandleMenuEvent(event);
313 bool wxLuaEditor::HandleMenuEvent(wxCommandEvent& event)
315 int line = LineFromPosition(GetInsertionPoint());
317 switch (event.GetId())
319 case ID_WXLUAIDE_TOGGLE_BREAKPOINT :
321 if (HasBreakpoint(line))
322 DeleteBreakpoint(line);
330 return wxSTEditor::HandleMenuEvent(event);
333 void wxLuaEditor::OnMarginClick( wxStyledTextEvent &event )
341 bool wxLuaEditor::HasBreakpoint(
int line)
344 return MarkerNext(markerBreak, 1<<markerBreak) >= 0;
346 return (MarkerGet(line) & (1<<markerBreak)) != 0;
348 void wxLuaEditor::ClearBreakpoints()
350 MarkerDeleteAll(markerBreak);
352 void wxLuaEditor::AddBreakpoint(
int line)
354 if ((line < 0) || (line > GetLineCount()) || HasBreakpoint(line))
357 (void)MarkerAdd(line, markerBreak);
359 void wxLuaEditor::DeleteBreakpoint(
int line)
361 if (!HasBreakpoint(line))
364 MarkerDelete(line, markerBreak);
367 void wxLuaEditor::UpdateItems(wxMenu *menu, wxMenuBar *menuBar, wxToolBar *toolBar)
371 wxlState = GetwxLuaIDE()->GetwxLuaState();
373 bool is_running = wxlState.Ok() && wxlState.IsRunning();
375 wxSTEditorMenuManager::DoEnableItem(menu, menuBar, toolBar, ID_WXLUAIDE_RUN_LUA, !is_running);
376 wxSTEditorMenuManager::DoEnableItem(menu, menuBar, toolBar, ID_WXLUAIDE_BREAK_LUA, is_running);
377 wxSTEditorMenuManager::DoEnableItem(menu, menuBar, toolBar, ID_WXLUAIDE_SHOW_STACK, wxlState.Ok() && !is_running);
379 wxSTEditor::UpdateItems(menu, menuBar, toolBar);
386 IMPLEMENT_DYNAMIC_CLASS(
wxLuaIDE, wxWindow);
388 BEGIN_EVENT_TABLE(
wxLuaIDE, wxWindow)
389 EVT_MENU (ID_WXLUAIDE_LOAD_LUA,
wxLuaIDE::OnMenu)
390 EVT_MENU (ID_WXLUAIDE_SAVEAS_LUA,
wxLuaIDE::OnMenu)
391 EVT_MENU (ID_WXLUAIDE_RUN_LUA,
wxLuaIDE::OnMenu)
392 EVT_MENU (ID_WXLUAIDE_BREAK_LUA,
wxLuaIDE::OnMenu)
393 EVT_MENU (ID_WXLUAIDE_SHOW_STACK,
wxLuaIDE::OnMenu)
395 EVT_STSPLITTER_CREATE_EDITOR(wxID_ANY,
wxLuaIDE::OnCreateEditor)
398 EVT_LUA_PRINT (wxID_ANY,
wxLuaIDE::OnLua)
399 EVT_LUA_ERROR (wxID_ANY,
wxLuaIDE::OnLua)
400 EVT_LUA_DEBUG_HOOK (wxID_ANY,
wxLuaIDE::OnLua)
405 m_wxlstate_static =
false;
408 m_editorNotebook = NULL;
409 m_msgNotebook = NULL;
415 m_show_stack =
false;
420 bool wxLuaIDE::Create( wxWindow *parent,
int id,
421 const wxPoint& pos,
const wxSize& size,
422 long style,
long options,
const wxString& name)
426 if (!wxWindow::Create(parent,
id, pos, size, style, name))
429 if ((m_options & WXLUAIDE_TOOLBAR) != 0)
431 m_toolBar =
new wxToolBar(
this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
432 PopulateToolBar(m_toolBar, m_options);
433 wxSTEditorMenuManager::DoEnableItem(NULL, GetMenuBar(), GetToolBar(), ID_WXLUAIDE_BREAK_LUA,
false);
436 m_splitter =
new wxSplitterWindow(
this, wxID_ANY);
439 m_editorOptions = wxSTEditorOptions(STE_DEFAULT_OPTIONS);
441 m_editorOptions.GetEditorPrefs().SetPrefBool(STE_PREF_VIEW_LINEMARGIN,
true);
442 m_editorOptions.GetEditorPrefs().SetPrefBool(STE_PREF_VIEW_MARKERMARGIN,
true);
443 m_editorOptions.SetToolBar(m_toolBar);
444 m_editorOptions.SetDefaultFileName(wxT(
"untitled.lua"));
445 if (wxConfigBase::Get(
false))
446 m_editorOptions.LoadConfig(*wxConfigBase::Get(
false));
448 m_editorNotebook =
new wxSTEditorNotebook(m_splitter, ID_WXLUAIDE_EDITOR_NOTEBOOK,
449 wxDefaultPosition, wxSize(200,100), wxCLIP_CHILDREN);
450 m_editorNotebook->CreateOptions(m_editorOptions);
451 m_editorNotebook->InsertEditorSplitter(-1, wxID_ANY, wxT(
"untitled.lua"),
true);
453 m_editorNotebook->GetEditor()->MarkerDefine(wxLuaEditor::markerBreak, wxSTC_MARK_SMALLRECT, *wxBLACK, *wxRED);
454 m_editorNotebook->GetEditor()->MarkerDefine(wxLuaEditor::markerError, wxSTC_MARK_SHORTARROW, *wxBLACK, *wxRED);
456 wxMenu *wxluaMenu = CreatewxLuaMenu();
457 m_editorOptions.GetEditorPopupMenu()->AppendSeparator();
458 m_editorOptions.GetEditorPopupMenu()->Append(wxID_ANY, wxT(
"wxLua"), wxluaMenu);
462 m_shellOptions.SetSplitterOptions(STS_NO_EDITOR);
463 m_shellOptions.SetEditorStyles(m_editorOptions.GetEditorStyles());
464 m_shellOptions.SetEditorLangs(m_editorOptions.GetEditorLangs());
465 m_shellOptions.SetMenuManager(m_editorOptions.GetMenuManager(),
true);
466 m_shellOptions.SetEditorPopupMenu(wxSTEditorMenuManager(0, 0, STE_MENU_EDIT_CUTCOPYPASTE|STE_MENU_EDIT_COMPLETEWORD).CreateEditMenu(),
false);
467 m_shellOptions.SetToolBar(m_toolBar);
469 m_msgNotebook =
new wxSTEditorNotebook(m_splitter, ID_WXLUAIDE_MSG_NOTEBOOK,
470 wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN);
471 m_msgNotebook->CreateOptions(m_shellOptions);
475 wxSTEditorSplitter *steSplitter =
new wxSTEditorSplitter(m_msgNotebook, wxID_ANY,
476 wxDefaultPosition, wxDefaultSize, wxSP_3D);
477 steSplitter->CreateOptions(m_shellOptions);
479 m_luaShell =
new wxLuaShell(steSplitter, ID_WXLUAIDE_SHELL, wxDefaultPosition,
481 m_luaShell->CreateOptions(m_shellOptions);
482 m_luaShell->SetWrapMode(wxSTC_WRAP_WORD);
485 m_luaShell->RecreatewxLuaState(m_luaShell->GetEventHandler(), m_luaShell->GetId());
486 m_luaShell->CheckPrompt(
true);
487 steSplitter->Initialize(m_luaShell);
488 m_msgNotebook->InsertEditorSplitter(-1, steSplitter,
true);
492 steSplitter =
new wxSTEditorSplitter(m_msgNotebook, wxID_ANY,
493 wxDefaultPosition, wxDefaultSize, wxSP_3D);
494 steSplitter->CreateOptions(m_shellOptions);
495 m_luaOutput =
new wxLuaShell(steSplitter, ID_WXLUAIDE_OUTPUT_WIN,
496 wxDefaultPosition, wxDefaultSize, 0);
497 m_luaOutput->CreateOptions(m_shellOptions);
498 steSplitter->Initialize(m_luaOutput);
499 m_luaOutput->MarkerDeleteAll(wxLuaShell::PROMPT_MARKER);
500 m_luaOutput->SetwxLuaState(m_wxlState,
true);
501 m_luaOutput->SetWrapMode(wxSTC_WRAP_NONE);
502 #if wxCHECK_VERSION(2,9,0)
503 m_luaOutput->SetFileName(wxFileName(wxT(
"Output")));
505 m_luaOutput->SetFileName(wxT(
"Output"));
508 m_msgNotebook->InsertEditorSplitter( -1, steSplitter,
false );
510 m_msgNotebook->InsertEditorSplitter(-1, steSplitter,
false);
512 m_splitter->SetSashGravity(0.8);
513 m_splitter->SetMinimumPaneSize(50);
514 m_splitter->SplitHorizontally(m_editorNotebook, m_msgNotebook, GetClientSize().y/2);
519 wxLuaIDE::~wxLuaIDE()
522 m_wxlState.CloseLuaState(
true);
524 m_wxlState.Destroy();
527 void wxLuaIDE::OnSize(wxSizeEvent& event)
530 if (!m_splitter)
return;
532 wxSize clientSize(GetClientSize());
533 int width = clientSize.x;
534 int height = clientSize.y;
538 if (m_toolBar && (m_toolBar->GetParent() ==
this))
540 m_toolBar->SetSize(0, 1, width, -1);
541 h = m_toolBar->GetSize().y + 1;
546 m_splitter->SetSize(0, h, width, height-h);
547 int sash = m_splitter->GetSashPosition();
548 if ((sash < 50) || (sash > height - 50))
549 m_splitter->SetSashPosition(wxMin(50, height/2));
553 void wxLuaIDE::SetMenuBar(wxMenuBar *menuBar)
555 m_shellOptions.SetMenuBar(menuBar);
556 m_editorOptions.SetMenuBar(menuBar);
558 if (menuBar && (menuBar->FindMenu(wxT(
"wxLua")) == wxNOT_FOUND))
560 wxMenu *wxluaMenu = CreatewxLuaMenu();
561 int pos = menuBar->FindMenu(wxT(
"Help"));
562 menuBar->Insert(pos, wxluaMenu, wxT(
"wx&Lua"));
565 if (!m_wxlState.Ok() || !m_wxlState.IsRunning())
566 wxSTEditorMenuManager::DoEnableItem(NULL, GetMenuBar(), GetToolBar(), ID_WXLUAIDE_BREAK_LUA,
false);
569 void wxLuaIDE::SetToolBar(wxToolBar *toolBar)
571 m_shellOptions.SetToolBar(toolBar);
572 m_editorOptions.SetToolBar(toolBar);
576 if (!m_wxlState.Ok() || !m_wxlState.IsRunning())
577 wxSTEditorMenuManager::DoEnableItem(NULL, GetMenuBar(), GetToolBar(), ID_WXLUAIDE_BREAK_LUA,
false);
580 void wxLuaIDE::PopulateToolBar(wxToolBar *toolBar,
long options)
const
582 bool add_sep =
false;
584 if ((options & WXLUAIDE_TB_FILE) != 0)
586 toolBar->AddTool( ID_WXLUAIDE_LOAD_LUA, wxT(
"Open file"), wxBitmap(open_xpm), wxT(
"Open lua program") );
587 toolBar->AddTool( ID_WXLUAIDE_SAVEAS_LUA, wxT(
"Save as..."), wxBitmap(save_xpm), wxT(
"Save lua program") );
590 if ((options & WXLUAIDE_TB_LUA) != 0)
592 if (add_sep) toolBar->AddSeparator();
594 toolBar->AddTool( ID_WXLUAIDE_RUN_LUA, wxT(
"Run wxLua program"), wxBitmap(play_xpm), wxT(
"Run the wxLua program in the current editor") );
595 toolBar->AddTool( ID_WXLUAIDE_BREAK_LUA, wxT(
"Stop wxLua program"), wxBitmap(stop_xpm), wxT(
"Stop the running wxLua program") );
596 toolBar->AddSeparator();
597 toolBar->AddTool( ID_WXLUAIDE_SHOW_STACK, wxT(
"Show stack tree"), wxBitmap(LUA_xpm), wxT(
"Show the lua stack tree") );
602 wxMenu* wxLuaIDE::CreatewxLuaMenu()
const
604 wxSTEditorMenuManager s;
605 wxMenu *wxluaMenu =
new wxMenu();
606 wxluaMenu->Append(s.MenuItem(wxluaMenu, ID_WXLUAIDE_RUN_LUA, wxT(
"&Run wxLua program\tCtrl+F5"), wxT(
"Run the wxLua program in the current editor") , wxITEM_NORMAL, wxBitmap(play_xpm)));
607 wxluaMenu->Append(s.MenuItem(wxluaMenu, ID_WXLUAIDE_BREAK_LUA, wxT(
"&Stop wxLua program\tShift+F5"), wxT(
"Stop the running wxLua program") , wxITEM_NORMAL, wxBitmap(stop_xpm)));
608 wxluaMenu->AppendSeparator();
609 wxluaMenu->Append(s.MenuItem(wxluaMenu, ID_WXLUAIDE_SHOW_STACK, wxT(
"Stack &tree"), wxT(
"Show the lua stack tree") , wxITEM_NORMAL, wxBitmap(LUA_xpm)));
610 wxluaMenu->AppendSeparator();
611 wxluaMenu->Append(ID_WXLUAIDE_TOGGLE_BREAKPOINT, wxT(
"Toggle &breakpoint\tF9"));
615 void wxLuaIDE::SetwxLuaState(
const wxLuaState& wxlState,
bool is_static)
617 if (m_wxlState.Ok() && !m_wxlstate_static)
620 if (m_wxlState.IsRunning())
623 m_wxlState.DebugHookBreak();
626 m_wxlState.CloseLuaState(
true);
629 m_wxlState = wxlState;
630 m_wxlstate_static = is_static;
633 bool wxLuaIDE::RecreatewxLuaState(wxEvtHandler *wxlStateEvtHandler,
int win_id)
636 wxLuaEvent event(wxEVT_LUAIDE_wxLUA_CREATION, GetId(), wxNullLuaState);
637 event.SetEventObject(
this);
639 event.SetExtraLong(win_id);
642 if (GetEventHandler()->ProcessEvent(event))
649 if (event.GetwxLuaState().Ok())
651 SetwxLuaState(event.GetwxLuaState(),
event.GetInt() == 1);
655 wxLuaState wxlState = wxLuaState(wxlStateEvtHandler, win_id);
656 wxlState.SetLuaDebugHook( LUA_MASKCOUNT, 1000, 50,
false );
657 SetwxLuaState(wxlState,
false);
660 return m_wxlState.Ok();
663 void wxLuaIDE::OnCreateEditor(wxCommandEvent &event)
665 wxWindow *win = (wxWindow*)event.GetEventObject();
667 wxDefaultPosition, wxDefaultSize, 0);
668 editor->CreateOptions(m_editorOptions);
669 editor->SetwxLuaIDE(
this);
670 event.SetEventObject(editor);
673 void wxLuaIDE::OnMenu(wxCommandEvent &event)
678 if (!HandleMenuEvent(event))
682 bool wxLuaIDE::HandleMenuEvent(wxCommandEvent& event)
688 wxCHECK_MSG(editor,
false, wxT(
"Invalid notebook page"));
690 switch(event.GetId())
692 case ID_WXLUAIDE_LOAD_LUA :
694 wxFileName fn = editor->GetFileName();
695 wxString filename, path;
700 filename = fn.GetFullName();
703 wxFileDialog dlg(
this, wxT(
"Open file"), path, filename,
704 wxT(
"Lua (*.lua)|*.lua|Any file (*)|*"),
705 wxFD_OPEN|wxFD_FILE_MUST_EXIST);
706 if (dlg.ShowModal() == wxID_OK)
708 filename = dlg.GetPath();
710 if (!filename.IsEmpty())
711 editor->LoadFile(filename);
716 case ID_WXLUAIDE_SAVEAS_LUA :
718 editor->SaveFile(
true);
721 case ID_WXLUAIDE_RUN_LUA :
723 wxString program = editor->GetText();
724 if (program.IsEmpty())
return true;
729 m_wxlState.CloseLuaState(
true);
731 m_wxlState.Destroy();
736 wxCHECK_MSG(RecreatewxLuaState(GetEventHandler(), editor->GetId()),
true, wxT(
"Can't recreate lua interpreter"));
738 wxSTEditorMenuManager::DoEnableItem(NULL, GetMenuBar(), GetToolBar(), ID_WXLUAIDE_RUN_LUA,
false);
739 wxSTEditorMenuManager::DoEnableItem(NULL, GetMenuBar(), GetToolBar(), ID_WXLUAIDE_BREAK_LUA,
true);
740 wxSTEditorMenuManager::DoEnableItem(NULL, GetMenuBar(), GetToolBar(), ID_WXLUAIDE_SHOW_STACK,
false);
742 editor->MarkerDeleteAll(wxLuaEditor::markerError);
743 wxString name = m_editorNotebook->GetPageText(m_editorNotebook->GetSelection());
745 m_luaOutput->SetReadOnly(
false);
746 m_luaOutput->ClearAll();
747 m_luaOutput->MarkerDeleteAll(wxLuaShell::PROMPT_MARKER);
748 WriteMessage(m_luaOutput, wxT(
"Running lua script '") + name + wxT(
"' : ") + wxNow() + wxT(
"\n"));
749 wxStopWatch stopWatch;
754 #if wxCHECK_VERSION(2,9,0)
755 if (editor->GetFileName().IsOk())
757 if (editor->GetFileName().IsEmpty())
760 wxFileName fn(editor->GetFileName());
766 m_wxlState.AddLuaPath(fn);
770 m_luaOutput->SetwxLuaState(m_wxlState,
true);
772 if (editor->HasBreakpoint(-1))
773 m_wxlState.SetLuaDebugHook(LUA_MASKCALL|LUA_MASKRET|LUA_MASKLINE|LUA_MASKCOUNT, 1, 50,
true);
775 m_wxlState.SetLuaDebugHook(LUA_MASKCALL|LUA_MASKRET|LUA_MASKLINE|LUA_MASKCOUNT, 1000, 100,
false);
777 m_luaOutput->SetReadOnly(
true);
778 editor->SetReadOnly(
true);
779 m_wxlState.RunString(program, name);
780 editor->SetReadOnly(
false);
781 m_luaOutput->SetReadOnly(
false);
783 WriteMessage(m_luaOutput, wxT(
"End lua script : ") + wxNow() + wxT(
"\n"));
784 WriteMessage(m_luaOutput, wxString::Format(wxT(
"Execution time : %.3lf(s)\n"), stopWatch.Time()/1000.0));
786 m_luaOutput->CheckPrompt(
true);
788 wxSTEditorMenuManager::DoEnableItem(NULL, GetMenuBar(), GetToolBar(), ID_WXLUAIDE_RUN_LUA,
true);
789 wxSTEditorMenuManager::DoEnableItem(NULL, GetMenuBar(), GetToolBar(), ID_WXLUAIDE_BREAK_LUA,
false);
790 wxSTEditorMenuManager::DoEnableItem(NULL, GetMenuBar(), GetToolBar(), ID_WXLUAIDE_SHOW_STACK,
true);
794 case ID_WXLUAIDE_BREAK_LUA :
796 if (m_wxlState.Ok() && m_wxlState.IsRunning())
798 m_wxlState.DebugHookBreak(wxT(
"Interpreter stopped"));
800 else if (m_luaShell && m_luaShell->GetwxLuaState().Ok() && m_luaShell->GetwxLuaState().IsRunning())
802 m_luaShell->GetwxLuaState().DebugHookBreak(wxT(
"Interpreter stopped"));
807 case ID_WXLUAIDE_SHOW_STACK :
816 if (m_msgNotebook->GetSelection() == 0)
818 wxlState = m_luaShell ? m_luaShell->GetwxLuaState() : wxNullLuaState;
819 luaShell = m_luaShell;
823 wxlState = m_wxlState;
824 luaShell = m_luaOutput;
827 if (luaShell && wxlState.Ok())
829 int hook_count = wxlState.GetLuaDebugHookCount();
830 int hook_yield = wxlState.GetLuaDebugHookYield();
831 bool hook_send = wxlState.GetLuaDebugHookSendEvt();
832 int hook_mask = wxlState.GetLuaDebugHook();
834 wxlState.SetLuaDebugHook(LUA_MASKCALL|LUA_MASKRET|LUA_MASKLINE|LUA_MASKCOUNT, 1, -1,
true);
835 wxlState.RunString(wxT(
"print(\"Showing stack\")"));
836 wxlState.SetLuaDebugHook(hook_mask, hook_count, hook_yield, hook_send);
837 luaShell->CheckPrompt(
true);
842 wxMessageBox(wxT(
"Invalid lua interpreter."), wxT(
"Error showing stack"), wxOK,
this);
850 return editor->HandleMenuEvent(event);
853 void wxLuaIDE::OnLua(wxLuaEvent &event)
857 if (event.GetwxLuaState() == m_luaShell->GetwxLuaState())
858 outEditor = m_luaShell;
860 outEditor = m_luaOutput;
862 OutputLuaEvent(event, outEditor);
865 void wxLuaIDE::OutputLuaEvent(wxLuaEvent &event,
wxLuaShell *outEditor)
868 if (outEditor == NULL)
871 if (event.GetEventType() == wxEVT_LUA_DEBUG_HOOK)
875 wxLuaStackDialog StackDialog(event.GetwxLuaState(),
this);
876 StackDialog.ShowModal();
877 m_show_stack =
false;
879 else if (event.GetwxLuaState() == m_wxlState)
885 int line =
event.GetLineNum();
887 if ((line >= 0) && editor->HasBreakpoint(line-1))
888 m_wxlState.DebugHookBreak(wxString::Format(wxT(
"Stopped at breakpoint, line: %d"), line));
892 else if (event.GetEventType() == wxEVT_LUA_PRINT)
894 WriteMessage(outEditor, event.GetString() + wxT(
"\n"));
896 else if (event.GetEventType() == wxEVT_LUA_ERROR)
898 WriteMessage(outEditor, wxT(
"\nError: ") + event.GetString() + wxT(
"\n"));
900 int line =
event.GetLineNum();
903 if (event.GetString().Find(wxT(
"[string \"wxLuaShell\"]")) != wxNOT_FOUND)
907 if ((line >= 0) && (
event.GetwxLuaState() == m_wxlState))
913 editor->MarkerDeleteAll(wxLuaEditor::markerError);
914 editor->MarkerAdd(line-1, wxLuaEditor::markerError);
915 editor->GotoLine(line-1);
916 editor->EnsureCaretVisible();
922 void wxLuaIDE::WriteMessage(
wxLuaShell *outputWin,
const wxString& str)
924 wxCHECK_RET(outputWin, wxT(
"Invalid wxLuaShell to write message to."));
929 int sel = m_msgNotebook->GetSelection();
930 if ((outputWin->GetId() == ID_WXLUAIDE_SHELL) && (sel != 0))
931 m_msgNotebook->SetSelection(0);
932 if ((outputWin->GetId() == ID_WXLUAIDE_OUTPUT_WIN) && (sel != 1))
933 m_msgNotebook->SetSelection(1);
961 outputWin->AppendText(str);
#define wxDynamicCast(obj, className)
Define wxDynamicCast so that it will give a compiler error for unrelated types.