1 /*=========================================================================
4 Module: $RCSfile: bbtkWxConsole.cxx,v $
6 Date: $Date: 2008/03/17 07:04:08 $
7 Version: $Revision: 1.14 $
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"
43 // On Windows when compiling a dll, wx prevents the compilation
44 // of the class wxStreamToTextRedirector (why ? it is a nightmare...)
45 // The blocking symbol is wxHAS_TEXT_WINDOW_STREAM.
46 // Note also that wxStreamToTextRedirector use the fact that wx is
47 // compiled with the option WX_USE_STD_STREAMS in which case
48 // wxTextCtrl inherits from std::streambuf and the redirection
49 // can be done simply by setting the std::cout buffer to the
50 // one of the wxTextCtrl.
51 // So on windows, we have to redirect manually std::cout to mwxTextHistory.
52 // Finally, on all systems we made our redirection class to redirect both to
53 // the WxConsole and to printf in order to get a console trace when
54 // the appli crashes (we could also imagine to log in a file...)
55 // This is why we finally wrote our own redirection which is crossplatform
56 // (drawback : not optimal on Unix platform; we could think of
57 // a particular implementation...).
59 //================================================================
60 /// Redirects std::cout to a wxTextCtrl and optionally to printf also
61 class WxTextCtrlStreamRedirector : public std::streambuf
67 WxTextCtrlStreamRedirector(std::ostream& redirect,
69 const wxColour& colour = *wxBLACK,
79 char *ptr = new char[bufferSize];
80 setp(ptr, ptr + bufferSize);
85 m_sbufOld = m_ostr.rdbuf();
89 ~WxTextCtrlStreamRedirector()
93 m_ostr.rdbuf(m_sbufOld);
96 virtual void writeString(const std::string &str)
98 const wxTextAttr& style = mText->GetDefaultStyle();
99 mText->SetDefaultStyle(mColour);
100 mText->AppendText(std2wx(str));
101 mText->SetDefaultStyle(style);
105 printf("%s",str.c_str());
114 // the stream we're redirecting
115 std::ostream& m_ostr;
116 // the old streambuf (before we changed it)
117 std::streambuf *m_sbufOld;
128 if (pbase() == epptr())
143 if (pbase() != pptr())
145 int len = int(pptr() - pbase());
146 std::string temp(pbase(), len);
148 setp(pbase(), epptr());
153 //================================================================
157 //================================================================
158 WxConsole::WxConsole( wxWindow *parent, wxString title, wxSize size)
159 : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
161 // m_mgr = new wxAuiManager(this);
162 m_mgr.SetManagedWindow(this);
164 mInterpreter = new bbtk::Interpreter();
165 mInterpreter->SetWxConsole(this);
166 mInterpreter->SetCommandLine(true);
169 wxInitAllImageHandlers();
171 wxMenu *menuFile = new wxMenu;
172 menuFile->Append( ID_Menu_Quit, _T("&Quit") );
174 wxMenu *menuAbout = new wxMenu;
175 menuAbout->Append( ID_Menu_About, _T("&About...") );
177 wxMenu *menuTools = new wxMenu;
178 menuTools->Append( ID_Menu_EditConfig, _T("&Edit bbtk config") );
179 menuTools->Append( ID_Menu_CreatePackage, _T("Create &package") );
180 menuTools->Append( ID_Menu_CreateBlackBox, _T("Create &blackbox") );
181 menuTools->Append( ID_Menu_ShowImageGraph, _T("&Show last image graph") );
182 menuTools->Append( ID_Menu_CreateIndex, _T("&Generate index") );
185 wxMenuBar *menuBar = new wxMenuBar;
186 menuBar->Append( menuFile, _T("&File") );
187 menuBar->Append( menuTools, _T("&Tools") );
188 menuBar->Append( menuAbout, _T("About") );
190 SetMenuBar( menuBar );
193 SetStatusText( _T("Welcome to bbi !") );
198 // wxFlexGridSizer *sizer = new wxFlexGridSizer(1);
200 //EED wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
201 // mwxNotebook = new wxNotebook(this,-1,wxDefaultPosition, wxDefaultSize, 0);
202 mwxNotebook = new wxAuiNotebook(this,
206 wxAUI_NB_TAB_SPLIT | wxAUI_NB_TAB_EXTERNAL_MOVE | wxNO_BORDER);
208 mwxPageCommand = new wxPanel(mwxNotebook,-1);
209 mwxPageHelp = new wxPanel(mwxNotebook,-1);
212 wxBoxSizer *cmdsizer = new wxBoxSizer(wxVERTICAL);
214 mwxPageCommand->SetAutoLayout(true);
215 mwxPageCommand->SetSizer(cmdsizer);
216 cmdsizer->Fit(mwxPageCommand);
217 cmdsizer->SetSizeHints(mwxPageCommand);
219 wxBoxSizer *helpsizer = new wxBoxSizer(wxVERTICAL);
221 mwxPageHelp->SetAutoLayout(true);
222 mwxPageHelp->SetSizer(helpsizer);
223 helpsizer->Fit(mwxPageHelp);
224 helpsizer->SetSizeHints(mwxPageHelp);
226 mwxHtmlWindow = new WxBrowser(mwxPageHelp,
227 //EED wxSize(1200,0));
230 // mwxHtmlWindow->SetSize(wxSize(800,1000));
231 helpsizer->Add (mwxHtmlWindow,1, wxGROW |wxLEFT | wxRIGHT | wxBOTTOM );
232 // helpsizer->Add ( new wxButton(mwxPageHelp,-1,"perro"), 0, wxEXPAND );
238 new wxTextCtrl(mwxPageCommand,
240 _T(""),wxDefaultPosition,
241 wxDefaultSize, //HistorySize,
245 wxFont* FixedFont = new wxFont(10,
251 mwxTextHistoryAttr = new wxTextAttr;
252 mwxTextHistoryAttr->SetFont(*FixedFont);
255 new wxTextCtrl(mwxPageCommand,
257 _T(""),wxDefaultPosition,
264 mwxTextCommandAttr = new wxTextAttr;
265 mwxTextCommandAttr->SetFont(*FixedFont);
266 mwxTextCommand->SetDefaultStyle(*mwxTextCommandAttr);
269 new wxComboBox(mwxPageCommand,
276 // | wxTE_PROCESS_TAB
278 // //| wxTAB_TRAVERSAL
282 mwxTextCommand->SetFocus();
284 wxPanel *btnsCtrlPanel = CreateBtnsCtrlPanel(mwxPageCommand);
286 wxButton *btnGo = new wxButton( mwxPageCommand,-1,_T("Go") );
287 Connect(btnGo->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnGo );
289 wxFlexGridSizer *sizerCommand= new wxFlexGridSizer(2);
290 sizerCommand->AddGrowableCol(0);
291 sizerCommand->Add(mwxTextCommand,1,wxGROW);
292 sizerCommand->Add(btnGo);
294 cmdsizer->Add ( mwxTextHistory, 1, wxALL | wxGROW, 10);
295 //EED cmdsizer->Add ( mwxTextCommand, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxGROW, 10 );
296 cmdsizer->Add ( sizerCommand, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxGROW, 10 );
297 cmdsizer->Add ( btnsCtrlPanel, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxGROW, 10 );
300 // cmdsizer->AddGrowableCol(0);
301 // cmdsizer->AddGrowableRow(0);
302 // cmdsizer->AddGrowableRow(1);
303 // cmdsizer->SetFlexibleDirection(wxBOTH);
305 //=============================
309 Connect( mwxTextCommand->GetId(),
310 wxEVT_COMMAND_TEXT_ENTER,
311 (wxObjectEventFunction)& WxConsole::OnCommandEnter );
313 Connect( mwxTextCommand->GetId(),
315 //wxEVT_COMMAND_TEXT_UPDATED,
316 (wxObjectEventFunction)& WxConsole::OnCommandChar );
321 // Redirection of std::cout to mwxTextHistory and printf
323 new WxTextCtrlStreamRedirector(std::cout,mwxTextHistory,*wxBLACK,true);
325 new WxTextCtrlStreamRedirector(std::cerr,mwxTextHistory,*wxGREEN,true);
327 // Creates and sets the parent window of all bbtk windows
328 wxWindow* top = new wxPanel(this,-1);//,_T("top"));
330 //new wxFrame(this,-1,_T("bbtk"),
331 // wxDefaultPosition,
333 // wxFRAME_TOOL_WINDOW) ;//wxMINIMIZE_BOX);
335 Wx::SetTopWindow(top);
341 //EED SetSizer(sizer);
343 mwxNotebook->AddPage( mwxPageCommand, _T("Command"));
344 mwxNotebook->AddPage( mwxPageHelp, _T("Help"));
345 m_mgr.AddPane(mwxNotebook, wxAuiPaneInfo().Name(wxT("notebook_content")).CenterPane().PaneBorder(false));
354 //================================================================
356 //================================================================
357 WxConsole::~WxConsole()
360 delete mRedirect_cout;
361 delete mRedirect_cerr;
363 //================================================================
366 //================================================================
367 void WxConsole::OnCommandEnter(wxCommandEvent& event)
369 wxString line(mwxTextCommand->GetValue());
372 //================================================================
374 //================================================================
375 void WxConsole::OnBtnGo(wxCommandEvent& event)
377 wxString line(mwxTextCommand->GetValue());
380 //================================================================
382 //================================================================
383 void WxConsole::CommandString(wxString line )
385 //printf("WxConsole::CommandString 01 \n");
386 wxString s = _T("> ") + line + _T("\n");
387 mwxTextHistoryAttr->SetTextColour(*wxRED);
388 mwxTextHistory->SetDefaultStyle(*mwxTextHistoryAttr);
389 mwxTextHistory->AppendText(s);
390 // send to standard console also
391 //printf("WxConsole::CommandString 02 \n");
392 // printf("%s",wx2std(s).c_str());
393 //EED mwxTextCommand->Clear();
394 mwxTextCommand->SetValue(_T(""));
395 mwxTextCommand->Append(line);
397 //EED mwxTextCommand->SetDefaultStyle(*mwxTextCommandAttr);
398 mwxTextHistoryAttr->SetTextColour(*wxBLACK);
399 mwxTextHistory->SetDefaultStyle(*mwxTextHistoryAttr);
401 //printf("WxConsole::CommandString 03 \n");
404 bool insideComment = false;
406 mInterpreter->InterpretLine( wx2std(line), insideComment );
408 catch (bbtk::QuitException)
412 catch (bbtk::Exception e)
416 catch (std::exception& e)
418 std::cout << "* ERROR : "<<e.what()<<" (not in bbtk)"<<std::endl;
422 std::cout << "* UNDEFINED ERROR (not a bbtk nor a std exception)"
425 //printf("WxConsole::CommandString 06 \n");
427 //================================================================
430 //================================================================
431 void WxConsole::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
435 //================================================================
438 //================================================================
439 void WxConsole::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
443 wxMessageBox(_T(" bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS-LRMN 2008"),
444 _T("About ..."), wxOK | wxICON_INFORMATION,
447 //================================================================
450 //================================================================
451 void WxConsole::OnMenuEditConfig(wxCommandEvent& WXUNUSED(event))
453 std::string commandStr;
454 std::string configFile = ConfigurationFile::GetInstance().Get_config_xml_full_path();
456 commandStr = "notepad.exe ";
458 commandStr = "gedit ";
460 commandStr = commandStr + configFile;
461 std::cout << "system: " << commandStr << std::endl;
462 system ( commandStr.c_str() );
464 //================================================================
467 //================================================================
468 void WxConsole::OnMenuCreatePackage(wxCommandEvent& WXUNUSED(event))
470 std::cout << "bbi: include CreatePackage" << std::endl;
471 Interpreter* I = new Interpreter;
472 I->InterpretFile("CreatePackage");
475 //================================================================
478 //================================================================
479 void WxConsole::OnMenuCreateBlackBox(wxCommandEvent& WXUNUSED(event))
481 std::cout << "bbi: include CreateBlackBox" << std::endl;
482 Interpreter* I = new Interpreter;
483 I->InterpretFile("CreateBlackBox");
486 //================================================================
488 //================================================================
489 void WxConsole::OnMenuShowImageGraph(wxCommandEvent& WXUNUSED(event))
491 std::string default_temp_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
494 std::string strappli="start ";
496 std::string strappli="gnome-open ";
498 std::string strcommand = strappli +default_temp_dir+"/temp_dir/workspace_workspacePrototype.png";
499 std::cout << "system: " << strcommand << std::endl;
500 system ( strcommand.c_str() );
503 //================================================================
506 //================================================================
507 void WxConsole::OnMenuCreateIndex(wxCommandEvent& WXUNUSED(event))
509 std::string doc_path = ConfigurationFile::GetInstance().Get_doc_path();
510 std::string filepath = doc_path+"/bbdoc/make-index.bbs";
511 Interpreter* I = new Interpreter;
513 //EED std::cout << "bbi: include "<<filepath<<std::endl;
514 //EED I->InterpretFile( filepath );
516 bool insideComment = false; // for multiline comment
517 I->InterpretLine( "exec freeze" ,insideComment );
518 I->InterpretLine( "include *" ,insideComment );
519 I->InterpretLine( "index "+doc_path+"/bbdoc/index-alpha.html Initials" ,insideComment );
520 I->InterpretLine( "index "+doc_path+"/bbdoc/index-package.html Packages" ,insideComment );
521 I->InterpretLine( "index "+doc_path+"/bbdoc/index-category.html Categories" ,insideComment );
522 I->InterpretLine( "index "+doc_path+"/bbdoc/index-adaptors.html Adaptors",insideComment );
526 //================================================================
531 //================================================================
532 void WxConsole::OnCommandChar(wxCommandEvent& event)
536 // Command completion
537 std::vector<std::string> commands;
538 wxString sline( wx2std ( mwxTextCommand->GetValue() ) );
539 int ind = sline.size();
540 mInterpreter->FindCommandsWithPrefix( sline.c_str(),ind,commands);
541 if (commands.size()==1)
543 std::string com = *commands.begin();
544 for (; ind<com.size(); ++ind)
546 mwxTextCommand->TextAppend( std2wx ( com[ind]) );
548 mwxTextCommand->TextAppend(_T(" "));
550 else if (commands.size()>1)
552 std::vector<std::string>::iterator i;
554 for (i=commands.begin();i!=commands.end();++i)
556 write(STDOUT_FILENO,(*i).c_str(),strlen((*i).c_str()));
559 write(STDOUT_FILENO,"\n> ",3);
560 //for (int j=0;j<ind;++j)
562 write(STDOUT_FILENO,line,ind);
567 //================================================================
569 void WxConsole::ShowHtmlPage(std::string& page)
571 // std::cout << "WxConsole::ShowHtmlPage('"<<page<<"')"<<std::endl;
572 if (mwxHtmlWindow->GoTo(page))
574 //EED mwxNotebook->ChangeSelection(1);
575 mwxNotebook->SetSelection(1);
579 // std::cout << "ERROR html"<<std::endl;
585 //================================================================
586 wxPanel* WxConsole::CreateBtnsCtrlPanel(wxWindow *parent)
588 wxPanel *btnsCtrlPanel = new wxPanel(parent,-1);
589 wxBoxSizer *btnsSizer = new wxBoxSizer(wxHORIZONTAL);
591 wxButton *btnInclude = new wxButton( btnsCtrlPanel,-1,_T("Include") );
592 wxButton *btnReset = new wxButton( btnsCtrlPanel,-1,_T("Reset") );
593 wxButton *btnConfig = new wxButton( btnsCtrlPanel,-1,_T("Config") );
594 wxButton *btnGraphS = new wxButton( btnsCtrlPanel,-1,_T("Graph simple") );
595 wxButton *btnGraphD = new wxButton( btnsCtrlPanel,-1,_T("Graph detailed") );
596 wxButton *btnHelp = new wxButton( btnsCtrlPanel,-1,_T("Help") );
598 btnsSizer->Add( btnInclude );
599 btnsSizer->Add( btnReset );
600 btnsSizer->Add( btnConfig );
601 btnsSizer->Add( btnGraphS );
602 btnsSizer->Add( btnGraphD );
603 btnsSizer->Add( btnHelp );
604 btnsCtrlPanel->SetSizer(btnsSizer);
606 Connect(btnInclude->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnInclude );
607 Connect(btnReset->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnReset );
608 Connect(btnConfig->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnConfig );
609 Connect(btnGraphS->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnGraphS );
610 Connect(btnGraphD->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnGraphD );
611 Connect(btnHelp->GetId() , wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxConsole::OnBtnHelp );
612 return btnsCtrlPanel;
614 //================================================================
617 //================================================================
618 void WxConsole::OnBtnInclude(wxCommandEvent& event)
620 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
621 std::string stdDir = default_doc_dir+"/share/bbtk/bbs";
622 wxString defaultDir(stdDir.c_str(), wxConvUTF8);
624 wxFileDialog dialog(this, _T("Choose a file"),defaultDir, _T(""), _T("*.bbs"), wxOPEN );
625 if (dialog.ShowModal() == wxID_OK)
627 // std::string command(_T("include "));
628 // std::string pathfilename = (const char *)(dialog.GetFilename().mb_str());
629 wxString command(_T("include "));
630 wxString pathfilename = dialog.GetPath();
631 command += pathfilename;
632 CommandString( command );
636 //================================================================
639 //================================================================
640 void WxConsole::OnBtnReset(wxCommandEvent& event)
642 CommandString(_T("reset"));
644 //================================================================
647 //================================================================
648 void WxConsole::OnBtnConfig(wxCommandEvent& event)
650 CommandString(_T("config"));
652 //================================================================
656 //================================================================
657 void WxConsole::OnBtnGraphS(wxCommandEvent& event)
659 CommandString(_T("graph"));
661 //================================================================
663 //================================================================
664 void WxConsole::OnBtnGraphD(wxCommandEvent& event)
666 CommandString(_T("graph . 1"));
668 //================================================================
670 //================================================================
671 void WxConsole::OnBtnHelp(wxCommandEvent& event)
673 CommandString(_T("help"));
675 //================================================================
678 //================================================================
679 BEGIN_EVENT_TABLE(WxConsole, wxFrame)
680 EVT_MENU(WxConsole::ID_Menu_Quit, WxConsole::OnMenuQuit)
681 EVT_MENU(WxConsole::ID_Menu_About, WxConsole::OnMenuAbout)
682 EVT_MENU(WxConsole::ID_Menu_EditConfig, WxConsole::OnMenuEditConfig)
683 EVT_MENU(WxConsole::ID_Menu_CreatePackage, WxConsole::OnMenuCreatePackage)
684 EVT_MENU(WxConsole::ID_Menu_CreateBlackBox, WxConsole::OnMenuCreateBlackBox)
685 EVT_MENU(WxConsole::ID_Menu_ShowImageGraph, WxConsole::OnMenuShowImageGraph)
686 EVT_MENU(WxConsole::ID_Menu_CreateIndex, WxConsole::OnMenuCreateIndex)
687 EVT_TEXT_ENTER(WxConsole::ID_Text_Command, WxConsole::OnCommandEnter)
688 // EVT_CHAR(WxConsole::ID_Text_Command, WxConsole::OnCommandChar)
690 //================================================================
695 #endif //_USE_WXWIDGETS_