Skip to content

Commit 555eb6f

Browse files
committed
PythonInterpreter: Add a simple notebook tab as a button for adding new interpreters and correctly handle the process termination event
1 parent 2813b7b commit 555eb6f

6 files changed

Lines changed: 52 additions & 14 deletions

File tree

PythonInterpreter/PythonInterpCtrl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ BEGIN_EVENT_TABLE(PythonInterpCtrl, wxPanel)
179179
EVT_XMLRPC_RESPONSE(wxID_ANY, PythonInterpCtrl::OnPyNotify)
180180
// EVT_COMMAND(0, wxEVT_PY_NOTIFY_UI_CODEOK, PythonInterpCtrl::OnPyCode)
181181
// EVT_COMMAND(0, wxEVT_PY_NOTIFY_UI_INPUT, PythonInterpCtrl::OnLineInputRequest)
182-
EVT_COMMAND(0, wxEVT_PY_PROC_END, PythonInterpCtrl::OnEndProcess)
182+
EVT_COMMAND(0, wxEVT_XMLRPC_PROC_END, PythonInterpCtrl::OnEndProcess)
183183

184184
EVT_SIZE (PythonInterpCtrl::OnSize)
185185
END_EVENT_TABLE()
@@ -211,10 +211,10 @@ long PythonInterpCtrl::LaunchProcess(const wxString &processcmd, const wxArraySt
211211
{
212212
if(!IsDead())
213213
return -1;
214+
//TODO: Option to use XMLRPC over localhost or pipe, currently always use localhost
214215
m_port=m_portalloc.RequestPort();
215216
if(m_port<0)
216217
return -1;
217-
//TODO: XmlRpc over pipe doesn't work because of the way interp.py is implemented
218218
// m_port = -1; //Use XmlRpc over pipe
219219
//TODO: get the command and working dir from config
220220
#ifdef __WXMSW__
@@ -243,7 +243,7 @@ long PythonInterpCtrl::LaunchProcess(const wxString &processcmd, const wxArraySt
243243
if(!global&&!local) //No interpreter script found, return failure.
244244
return -2; //TODO: Return meaningful messages (or at least use the codeblocks logger)
245245

246-
m_pyinterp = new XmlRpcInstance(cmd,m_port);
246+
m_pyinterp = new XmlRpcInstance(cmd,m_port,_T("localhost"),this);
247247
if(m_pyinterp->IsDead())
248248
{
249249
Manager::Get()->GetLogManager()->Log(_("Error Starting Interpreter"));
@@ -373,7 +373,7 @@ void PythonInterpCtrl::OnPyJobAbort(wxCommandEvent& event)
373373
m_code=_T("");
374374
}
375375

376-
void PythonInterpCtrl::OnEndProcess(wxCommandEvent &ce)
376+
void PythonInterpCtrl::OnEndProcess(wxCommandEvent& event)
377377
{
378378
m_portalloc.ReleasePort(m_port);
379379
if(m_shellmgr)

PythonInterpreter/PythonInterpCtrl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class PythonInterpCtrl : public ShellCtrlBase
136136
void OnPyCode(wxCommandEvent& event);
137137
void OnPyJobDone(wxCommandEvent& event);
138138
void OnPyJobAbort(wxCommandEvent& event);
139-
void OnEndProcess(wxCommandEvent &ce);
139+
void OnEndProcess(wxCommandEvent& event);
140140

141141
bool DispatchCode(const wxString &code);
142142

PythonInterpreter/PythonInterpreter.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ BEGIN_EVENT_TABLE(PythonInterpreter, cbPlugin)
4040
// add any events you want to handle here
4141
EVT_MENU(ID_INTERP_WINDOW_TOGGLE,PythonInterpreter::OnToggleInterpreterWindow)
4242
EVT_UPDATE_UI(ID_INTERP_WINDOW_TOGGLE, PythonInterpreter::OnUpdateUI)
43+
EVT_COMMAND(0,wxEVT_SHELL_ADD_CLICKED, PythonInterpreter::AddNewInterpreter)
4344
END_EVENT_TABLE()
4445

4546

@@ -100,11 +101,18 @@ void PythonInterpreter::OnAttach()
100101
Manager::Get()->ProcessEvent(evt);
101102

102103
//TODO: Add UI to open a terminal instead of opening on attach
104+
// wxArrayString as;
105+
// m_shellmgr->LaunchProcess(_T(""),_T("Python"),_("Python Interpreter"),as);
106+
#endif
107+
}
108+
109+
void PythonInterpreter::AddNewInterpreter(wxCommandEvent &event)
110+
{
103111
wxArrayString as;
104112
m_shellmgr->LaunchProcess(_T(""),_T("Python"),_("Python Interpreter"),as);
105-
#endif
106113
}
107114

115+
108116
void PythonInterpreter::OnRelease(bool appShutDown)
109117
{
110118
#ifndef TOOLSPLUSLINK

PythonInterpreter/PythonInterpreter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class PythonInterpreter : public cbPlugin
4848
public:
4949

5050
private:
51+
void AddNewInterpreter(wxCommandEvent &event);
5152
void OnToggleInterpreterWindow(wxCommandEvent &event);
5253
void OnUpdateUI(wxUpdateUIEvent& event);
5354
wxMenu *m_ViewMenu;

PythonInterpreter/ShellCtrlBase.cpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,15 @@ ShellCtrlBase::ShellCtrlBase(wxWindow* parent, int id, const wxString &name, She
7272
}
7373

7474

75+
DEFINE_EVENT_TYPE(wxEVT_SHELL_ADD_CLICKED)
76+
7577
////////////////////////////////////// ShellManager /////////////////////////////////////////////
7678

7779
BEGIN_EVENT_TABLE(ShellManager, wxPanel)
7880
EVT_CHAR(ShellManager::OnUserInput)
7981
EVT_TIMER(ID_SHELLPOLLTIMER, ShellManager::OnPollandSyncOutput)
8082
EVT_AUINOTEBOOK_PAGE_CLOSE(ID_SHELLMGR, ShellManager::OnPageClosing)
83+
EVT_AUINOTEBOOK_PAGE_CHANGING(ID_SHELLMGR, ShellManager::OnPageChanging)
8184
END_EVENT_TABLE()
8285

8386

@@ -87,10 +90,25 @@ void ShellManager::OnPageClosing(wxAuiNotebookEvent& event)
8790
// LOGSTREAM << wxString::Format(_T("OnPageClosing(): ed=%p, title=%s\n"), eb, eb ? eb->GetTitle().c_str() : _T(""));
8891
if (!QueryClose(sh))
8992
event.Veto();
93+
m_nb->SetSelection(0);
9094
// event.Skip(); // allow others to process it too
9195
}
9296

9397

98+
void ShellManager::OnPageChanging(wxAuiNotebookEvent& event)
99+
{
100+
if(event.GetSelection() == int(m_nb->GetPageCount()-1))
101+
{
102+
wxCommandEvent pe(wxEVT_SHELL_ADD_CLICKED,0);
103+
AddPendingEvent(pe);
104+
event.Veto();
105+
return;
106+
}
107+
event.Skip();
108+
}
109+
110+
111+
94112
bool ShellManager::QueryClose(ShellCtrlBase* sh)
95113
{
96114
if(!sh)
@@ -134,10 +152,11 @@ long ShellManager::LaunchProcess(const wxString &processcmd, const wxString &nam
134152
delete shell; //TODO: GlobalShellRegistry.FreeControl() ???
135153
return -1;
136154
}
137-
m_nb->AddPage(shell,name);
138-
m_nb->SetSelection(m_nb->GetPageCount()-1);
155+
m_nb->InsertPage(m_nb->GetPageCount()-1,shell,name);
156+
m_nb->SetSelection(m_nb->GetPageCount()-2);
139157
// shell->Show();
140158
return procid;
159+
141160
}
142161

143162
ShellCtrlBase *ShellManager::GetPage(size_t i)
@@ -147,7 +166,7 @@ ShellCtrlBase *ShellManager::GetPage(size_t i)
147166

148167
ShellCtrlBase *ShellManager::GetPage(const wxString &name)
149168
{
150-
for(unsigned int i=0;i<m_nb->GetPageCount();i++)
169+
for(unsigned int i=0;i<m_nb->GetPageCount()-1;i++)
151170
{
152171
ShellCtrlBase *sh=GetPage(i);
153172
if(name==sh->GetName())
@@ -169,7 +188,7 @@ void ShellManager::KillWindow(int /*id*/)
169188
void ShellManager::RemoveDeadPages()
170189
{
171190
unsigned int i=0;
172-
while(i<m_nb->GetPageCount())
191+
while(i<m_nb->GetPageCount()-1)
173192
{
174193
ShellCtrlBase *shell=GetPage(i);
175194
if(shell->IsDead())
@@ -182,7 +201,7 @@ void ShellManager::RemoveDeadPages()
182201

183202
size_t ShellManager::GetTermNum(ShellCtrlBase *term)
184203
{
185-
for(unsigned int i=0;i<m_nb->GetPageCount();i++)
204+
for(unsigned int i=0;i<m_nb->GetPageCount()-1;i++)
186205
{
187206
ShellCtrlBase *shell=GetPage(i);
188207
if(shell==term)
@@ -194,7 +213,7 @@ size_t ShellManager::GetTermNum(ShellCtrlBase *term)
194213
int ShellManager::NumAlive()
195214
{
196215
int count=0;
197-
for(unsigned int i=0;i<m_nb->GetPageCount();i++)
216+
for(unsigned int i=0;i<m_nb->GetPageCount()-1;i++)
198217
count+=!GetPage(i)->IsDead();
199218
return count;
200219
}
@@ -203,15 +222,16 @@ int ShellManager::NumAlive()
203222
void ShellManager::OnShellTerminate(ShellCtrlBase *term)
204223
{
205224
size_t i=GetTermNum(term);
206-
m_nb->SetPageText(i,_("[DONE]")+m_nb->GetPageText(i));
225+
if(i<m_nb->GetPageCount()-1)
226+
m_nb->SetPageText(i,_("[DONE]")+m_nb->GetPageText(i));
207227
if(NumAlive()==0)
208228
m_synctimer.Stop();
209229
}
210230

211231

212232
void ShellManager::OnPollandSyncOutput(wxTimerEvent& /*te*/)
213233
{
214-
for(unsigned int i=0;i<m_nb->GetPageCount();i++)
234+
for(unsigned int i=0;i<m_nb->GetPageCount()-1;i++)
215235
{
216236
GetPage(i)->SyncOutput();
217237
}
@@ -230,6 +250,9 @@ ShellManager::ShellManager(wxWindow* parent)
230250
wxBoxSizer* bs = new wxBoxSizer(wxVERTICAL);
231251
m_nb = new wxAuiNotebook(this, ID_SHELLMGR, wxDefaultPosition, wxDefaultSize, wxAUI_NB_SCROLL_BUTTONS|wxAUI_NB_CLOSE_ON_ACTIVE_TAB);
232252
bs->Add(m_nb, 1, wxEXPAND | wxALL);
253+
wxBitmap bm = wxArtProvider::GetBitmap(wxART_NEW);
254+
wxButton *wnd = new wxButton(this, wxID_ANY);
255+
m_nb->AddPage(wnd,_(""),false,bm);
233256
SetAutoLayout(TRUE);
234257
SetSizer(bs);
235258
}

PythonInterpreter/ShellCtrlBase.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ class ShellCtrlBase : public wxPanel //TODO: make wxPanel a member, not a base??
114114
};
115115

116116

117+
BEGIN_DECLARE_EVENT_TYPES()
118+
DECLARE_LOCAL_EVENT_TYPE(wxEVT_SHELL_ADD_CLICKED, -1)
119+
END_DECLARE_EVENT_TYPES()
120+
121+
117122
class ShellManager : public wxPanel
118123
{
119124
friend class ShellCtrlBase;
@@ -133,6 +138,7 @@ class ShellManager : public wxPanel
133138
void OnUserInput(wxKeyEvent& ke);
134139
void OnPollandSyncOutput(wxTimerEvent& te);
135140
void OnPageClosing(wxAuiNotebookEvent& event);
141+
void OnPageChanging(wxAuiNotebookEvent& event);
136142
bool QueryClose(ShellCtrlBase* sh);
137143
//Responders to friend class ShellCtrlBase
138144
size_t GetTermNum(ShellCtrlBase *term);

0 commit comments

Comments
 (0)