1 /*=========================================================================
4 Module: $RCSfile: bbtkWxConsole.cxx,v $
6 Date: $Date: 2008/03/10 10:01:14 $
7 Version: $Revision: 1.10 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*//**
18 * \brief Short description in one line
20 * Long description which
21 * can span multiple lines
33 #ifdef _USE_WXWIDGETS_
36 #include "bbtkWxConsole.h"
37 #include "bbtkWxBlackBox.h"
38 #include "bbtkConfigurationFile.h"
44 // On Windows when compiling a dll, wx prevents the compilation
45 // of the class wxStreamToTextRedirector (why ? it is a nightmare...)
46 // The blocking symbol is wxHAS_TEXT_WINDOW_STREAM.
47 // Note also that wxStreamToTextRedirector use the fact that wx is
48 // compiled with the option WX_USE_STD_STREAMS in which case
49 // wxTextCtrl inherits from std::streambuf and the redirection
50 // can be done simply by setting the std::cout buffer to the
51 // one of the wxTextCtrl.
52 // So on windows, we have to redirect manually std::cout to mwxTextHistory.
53 // Finally, on all systems we made our redirection class to redirect both to
54 // the WxConsole and to printf in order to get a console trace when
55 // the appli crashes (we could also imagine to log in a file...)
56 // This is why we finally wrote our own redirection which is crossplatform
57 // (drawback : not optimal on Unix platform; we could think of
58 // a particular implementation...).
60 //================================================================
61 /// Redirects std::cout to a wxTextCtrl and optionally to printf also
62 class WxTextCtrlStreamRedirector : public std::streambuf
68 WxTextCtrlStreamRedirector(std::ostream& redirect,
70 const wxColour& colour = *wxBLACK,
80 char *ptr = new char[bufferSize];
81 setp(ptr, ptr + bufferSize);
86 m_sbufOld = m_ostr.rdbuf();
90 ~WxTextCtrlStreamRedirector()
94 m_ostr.rdbuf(m_sbufOld);
97 virtual void writeString(const std::string &str)
99 const wxTextAttr& style = mText->GetDefaultStyle();
100 mText->SetDefaultStyle(mColour);
101 mText->AppendText(std2wx(str));
102 mText->SetDefaultStyle(style);
106 printf("%s",str.c_str());
115 // the stream we're redirecting
116 std::ostream& m_ostr;
117 // the old streambuf (before we changed it)
118 std::streambuf *m_sbufOld;
129 if (pbase() == epptr())
144 if (pbase() != pptr())
146 int len = int(pptr() - pbase());
147 std::string temp(pbase(), len);
149 setp(pbase(), epptr());
154 //================================================================
158 //================================================================
159 WxConsole::WxConsole( wxWindow *parent, wxString title, wxSize size)
160 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
163 mInterpreter = new bbtk::Interpreter();
164 mInterpreter->SetWxConsole(this);
165 mInterpreter->SetCommandLine(true);
168 wxInitAllImageHandlers();
170 wxMenu *menuFile = new wxMenu;
171 menuFile->Append( ID_Menu_Quit, _T("&Quit") );
173 wxMenu *menuAbout = new wxMenu;
174 menuAbout->Append( ID_Menu_About, _T("&About...") );
176 wxMenu *menuTools = new wxMenu;
177 menuTools->Append( ID_Menu_CreatePackage, _T("&Create package") );
178 menuTools->Append( ID_Menu_CreateBlackBox, _T("&Create blackbox") );
179 menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
182 wxMenuBar *menuBar = new wxMenuBar;
183 menuBar->Append( menuFile, _T("&File") );
184 menuBar->Append( menuTools, _T("&Tools") );
185 menuBar->Append( menuAbout, _T("About") );
187 SetMenuBar( menuBar );
190 SetStatusText( _T("Welcome to bbi !") );
195 // wxFlexGridSizer *sizer = new wxFlexGridSizer(1);
197 //EED wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
198 // mwxNotebook = new wxNotebook(this,-1,wxDefaultPosition, wxDefaultSize, 0);
199 mwxNotebook = new wxAuiNotebook(this,
203 wxAUI_NB_TAB_SPLIT | wxAUI_NB_TAB_EXTERNAL_MOVE | wxNO_BORDER);
205 mwxPageCommand = new wxPanel(mwxNotebook,-1);
206 mwxNotebook->AddPage( mwxPageCommand, _T("Command"));
208 mwxPageHelp = new wxPanel(mwxNotebook,-1);
209 mwxNotebook->AddPage( mwxPageHelp, _T("Help"));
213 //EED sizer->Add ( mwxNotebook, 1, wxEXPAND /*| wxALIGN_BOTTOM*/ );
214 wxAuiManager *m_mgr = new wxAuiManager(this);
215 m_mgr->AddPane(mwxNotebook, wxAuiPaneInfo().Name(wxT("notebook_content")).CenterPane().PaneBorder(false));
218 wxBoxSizer *cmdsizer = new wxBoxSizer(wxVERTICAL);
220 mwxPageCommand->SetAutoLayout(true);
221 mwxPageCommand->SetSizer(cmdsizer);
222 cmdsizer->Fit(mwxPageCommand);
223 cmdsizer->SetSizeHints(mwxPageCommand);
225 wxBoxSizer *helpsizer = new wxBoxSizer(wxVERTICAL);
227 mwxPageHelp->SetAutoLayout(true);
228 mwxPageHelp->SetSizer(helpsizer);
229 helpsizer->Fit(mwxPageHelp);
230 helpsizer->SetSizeHints(mwxPageHelp);
232 mwxHtmlWindow = new WxBrowser(mwxPageHelp,
233 //EED wxSize(1200,0));
236 // mwxHtmlWindow->SetSize(wxSize(800,1000));
237 helpsizer->Add (mwxHtmlWindow,1, wxGROW |wxLEFT | wxRIGHT | wxBOTTOM );
238 // helpsizer->Add ( new wxButton(mwxPageHelp,-1,"perro"), 0, wxEXPAND );
244 new wxTextCtrl(mwxPageCommand,
246 _T(""),wxDefaultPosition,
247 wxDefaultSize, //HistorySize,
251 wxFont* FixedFont = new wxFont(10,
257 mwxTextHistoryAttr = new wxTextAttr;
258 mwxTextHistoryAttr->SetFont(*FixedFont);
261 new wxTextCtrl(mwxPageCommand,
263 _T(""),wxDefaultPosition,
270 mwxTextCommandAttr = new wxTextAttr;
271 mwxTextCommandAttr->SetFont(*FixedFont);
272 mwxTextCommand->SetDefaultStyle(*mwxTextCommandAttr);
275 new wxComboBox(mwxPageCommand,
278 // wxDefaultPosition,
280 // wxTE_PROCESS_ENTER
281 // | wxTE_PROCESS_TAB
283 // //| wxTAB_TRAVERSAL
287 mwxTextCommand->SetFocus();
289 wxPanel *btnsCtrlPanel = CreateBtnsCtrlPanel(mwxPageCommand);
291 wxButton *btnGo = new wxButton( mwxPageCommand,-1,_T("Go") );
292 Connect(btnGo->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnGo );
294 wxFlexGridSizer *sizerCommand= new wxFlexGridSizer(2);
295 sizerCommand->AddGrowableCol(0);
296 sizerCommand->Add(mwxTextCommand,1,wxGROW);
297 sizerCommand->Add(btnGo);
299 cmdsizer->Add ( mwxTextHistory, 1, wxALL | wxGROW, 10);
300 //EED cmdsizer->Add ( mwxTextCommand, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxGROW, 10 );
301 cmdsizer->Add ( sizerCommand, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxGROW, 10 );
302 cmdsizer->Add ( btnsCtrlPanel, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxGROW, 10 );
305 // cmdsizer->AddGrowableCol(0);
306 // cmdsizer->AddGrowableRow(0);
307 // cmdsizer->AddGrowableRow(1);
308 // cmdsizer->SetFlexibleDirection(wxBOTH);
310 //=============================
315 Connect( mwxTextCommand->GetId(),
316 wxEVT_COMMAND_TEXT_ENTER,
317 (wxObjectEventFunction)& WxConsole::OnCommandEnter );
318 Connect( mwxTextCommand->GetId(),
320 //wxEVT_COMMAND_TEXT_UPDATED,
321 (wxObjectEventFunction)& WxConsole::OnCommandChar );
326 // Redirection of std::cout to mwxTextHistory and printf
328 new WxTextCtrlStreamRedirector(std::cout,mwxTextHistory,*wxBLACK,true);
330 new WxTextCtrlStreamRedirector(std::cerr,mwxTextHistory,*wxGREEN,true);
332 // Creates and sets the parent window of all bbtk windows
333 wxFrame* top = new wxFrame(this,-1,_T("bbtk"),
336 wxFRAME_TOOL_WINDOW) ;//wxMINIMIZE_BOX);
337 Wx::SetTopWindow(top);
342 //EED SetSizer(sizer);
347 //================================================================
349 //================================================================
350 WxConsole::~WxConsole()
352 delete mRedirect_cout;
353 delete mRedirect_cerr;
355 //================================================================
358 //================================================================
359 void WxConsole::OnCommandEnter(wxCommandEvent& event)
361 wxString line(mwxTextCommand->GetValue());
364 //================================================================
366 //================================================================
367 void WxConsole::OnBtnGo(wxCommandEvent& event)
369 wxString line(mwxTextCommand->GetValue());
372 //================================================================
374 //================================================================
375 void WxConsole::CommandString(wxString line )
377 printf("WxConsole::CommandString 01 \n");
378 wxString s = _T("> ") + line + _T("\n");
379 mwxTextHistoryAttr->SetTextColour(*wxRED);
380 mwxTextHistory->SetDefaultStyle(*mwxTextHistoryAttr);
381 mwxTextHistory->AppendText(s);
382 // send to standard console also
383 printf("WxConsole::CommandString 02 \n");
384 printf("%s",wx2std(s).c_str());
385 //EED mwxTextCommand->Clear();
386 mwxTextCommand->SetValue(_T(""));
387 mwxTextCommand->Append(line);
389 //EED mwxTextCommand->SetDefaultStyle(*mwxTextCommandAttr);
390 mwxTextHistoryAttr->SetTextColour(*wxBLACK);
391 mwxTextHistory->SetDefaultStyle(*mwxTextHistoryAttr);
393 printf("WxConsole::CommandString 03 \n");
396 bool insideComment = false;
397 printf("WxConsole::CommandString 04 \n");
398 mInterpreter->InterpretLine( wx2std(line), insideComment );
399 printf("WxConsole::CommandString 05 \n");
401 catch (bbtk::QuitException)
405 catch (bbtk::Exception e)
409 catch (std::exception& e)
411 std::cout << "* ERROR : "<<e.what()<<" (not in bbtk)"<<std::endl;
415 std::cout << "* UNDEFINED ERROR (not a bbtk nor a std exception)"
418 printf("WxConsole::CommandString 06 \n");
420 //================================================================
423 //================================================================
424 void WxConsole::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
428 //================================================================
431 //================================================================
432 void WxConsole::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
435 wxMessageBox(_T(" bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS-LRMN 2007"),
436 _T("About ..."), wxOK | wxICON_INFORMATION,
439 //================================================================
442 //================================================================
443 void WxConsole::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
446 wxMessageBox(_T(" Creating Package"),
447 _T("Creating Package ..."), wxOK | wxICON_INFORMATION,
450 //================================================================
453 //================================================================
454 void WxConsole::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
457 wxMessageBox(_T(" Creating blackbox"),
458 _T("Creating blackbox ..."), wxOK | wxICON_INFORMATION,
461 //================================================================
463 //================================================================
464 void WxConsole::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
466 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
469 std::string strappli="start ";
471 std::string strappli="gnome-open ";
473 std::string strcommand = strappli +default_doc_dir+"/temp_dir/workspace_workspacePrototype.png";
474 system ( strcommand.c_str() );
476 //================================================================
480 //================================================================
481 void WxConsole::OnCommandChar(wxCommandEvent& event)
485 // Command completion
486 std::vector<std::string> commands;
487 wxString sline( wx2std ( mwxTextCommand->GetValue() ) );
488 int ind = sline.size();
489 mInterpreter->FindCommandsWithPrefix( sline.c_str(),ind,commands);
490 if (commands.size()==1)
492 std::string com = *commands.begin();
493 for (; ind<com.size(); ++ind)
495 mwxTextCommand->TextAppend( std2wx ( com[ind]) );
497 mwxTextCommand->TextAppend(_T(" "));
499 else if (commands.size()>1)
501 std::vector<std::string>::iterator i;
503 for (i=commands.begin();i!=commands.end();++i)
505 write(STDOUT_FILENO,(*i).c_str(),strlen((*i).c_str()));
508 write(STDOUT_FILENO,"\n> ",3);
509 //for (int j=0;j<ind;++j)
511 write(STDOUT_FILENO,line,ind);
516 //================================================================
518 void WxConsole::ShowHtmlPage(std::string& page)
520 // std::cout << "WxConsole::ShowHtmlPage('"<<page<<"')"<<std::endl;
521 if (mwxHtmlWindow->GoTo(page))
523 //EED mwxNotebook->ChangeSelection(1);
524 mwxNotebook->SetSelection(1);
528 // std::cout << "ERROR html"<<std::endl;
534 //================================================================
535 wxPanel* WxConsole::CreateBtnsCtrlPanel(wxWindow *parent)
537 wxPanel *btnsCtrlPanel = new wxPanel(parent,-1);
538 wxBoxSizer *btnsSizer = new wxBoxSizer(wxHORIZONTAL);
540 wxButton *btnInclude = new wxButton( btnsCtrlPanel,-1,_T("Include") );
541 wxButton *btnReset = new wxButton( btnsCtrlPanel,-1,_T("Reset") );
542 wxButton *btnConfig = new wxButton( btnsCtrlPanel,-1,_T("Config") );
543 wxButton *btnGraphS = new wxButton( btnsCtrlPanel,-1,_T("Graph S.") );
544 wxButton *btnGraphD = new wxButton( btnsCtrlPanel,-1,_T("Graph D.") );
545 wxButton *btnHelp = new wxButton( btnsCtrlPanel,-1,_T("Help") );
547 btnsSizer->Add( btnInclude );
548 btnsSizer->Add( btnReset );
549 btnsSizer->Add( btnConfig );
550 btnsSizer->Add( btnGraphS );
551 btnsSizer->Add( btnGraphD );
552 btnsSizer->Add( btnHelp );
553 btnsCtrlPanel->SetSizer(btnsSizer);
555 Connect(btnInclude->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnInclude );
556 Connect(btnReset->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnReset );
557 Connect(btnConfig->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnConfig );
558 Connect(btnGraphS->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnGraphS );
559 Connect(btnGraphD->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnGraphD );
560 Connect(btnHelp->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnHelp );
561 return btnsCtrlPanel;
563 //================================================================
566 //================================================================
567 void WxConsole::OnBtnInclude(wxCommandEvent& event)
569 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
570 std::string stdDir = default_doc_dir+"/share/bbtk/bbs";
571 wxString defaultDir(stdDir.c_str(), wxConvUTF8);
573 wxFileDialog dialog(this, _T("Choose a file"),defaultDir, _T(""), _T("*.bbs"), wxOPEN );
574 if (dialog.ShowModal() == wxID_OK)
576 // std::string command(_T("include "));
577 // std::string pathfilename = (const char *)(dialog.GetFilename().mb_str());
578 wxString command(_T("include "));
579 wxString pathfilename = dialog.GetPath();
580 command += pathfilename;
581 CommandString( command );
585 //================================================================
588 //================================================================
589 void WxConsole::OnBtnReset(wxCommandEvent& event)
591 printf("WxConsole::OnBtnReset 01 \n");
592 CommandString(_T("reset"));
593 printf("WxConsole::OnBtnReset 02 \n");
595 //================================================================
598 //================================================================
599 void WxConsole::OnBtnConfig(wxCommandEvent& event)
601 CommandString(_T("config"));
603 //================================================================
607 //================================================================
608 void WxConsole::OnBtnGraphS(wxCommandEvent& event)
610 CommandString(_T("graph"));
612 //================================================================
614 //================================================================
615 void WxConsole::OnBtnGraphD(wxCommandEvent& event)
617 CommandString(_T("graph . 1"));
619 //================================================================
621 //================================================================
622 void WxConsole::OnBtnHelp(wxCommandEvent& event)
624 CommandString(_T("help"));
626 //================================================================
629 //================================================================
630 BEGIN_EVENT_TABLE(WxConsole, wxFrame)
631 EVT_MENU(WxConsole::ID_Menu_Quit, WxConsole::OnMenuQuit)
632 EVT_MENU(WxConsole::ID_Menu_About, WxConsole::OnMenuAbout)
633 EVT_MENU(WxConsole::ID_Menu_CreatePackage, WxConsole::OnMenuCreatePackage)
634 EVT_MENU(WxConsole::ID_Menu_CreateBlackBox, WxConsole::OnMenuCreateBlackBox)
635 EVT_MENU(WxConsole::ID_Menu_ShowImageGraph, WxConsole::OnMenuShowImageGraph)
636 EVT_TEXT_ENTER(WxConsole::ID_Text_Command, WxConsole::OnCommandEnter)
637 // EVT_CHAR(WxConsole::ID_Text_Command, WxConsole::OnCommandChar)
639 //================================================================
644 #endif //_USE_WXWIDGETS_