1 /*=========================================================================
4 Module: $RCSfile: bbtkWxConsole.cxx,v $
6 Date: $Date: 2008/01/22 15:02:00 $
7 Version: $Revision: 1.1 $
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"
42 WxConsole* WxConsole::mInstance = 0;
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)
162 mInterpreter = new bbtk::Interpreter();
163 mInterpreter->SetCommandLine(true);
166 wxInitAllImageHandlers();
168 wxMenu *menuFile = new wxMenu;
169 menuFile->Append( ID_Menu_Quit, _T("&Quit") );
171 wxMenu *menuAbout = new wxMenu;
172 menuAbout->Append( ID_Menu_About, _T("&About...") );
174 wxMenuBar *menuBar = new wxMenuBar;
175 menuBar->Append( menuFile, _T("&File") );
176 menuBar->Append( menuAbout, _T("About") );
178 SetMenuBar( menuBar );
181 SetStatusText( _T("Welcome to bbi !") );
186 // wxFlexGridSizer *sizer = new wxFlexGridSizer(1);
187 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
189 mwxNotebook = new wxNotebook(this, -1,
190 wxDefaultPosition, wxDefaultSize, 0);
191 mwxPageCommand = new wxPanel(mwxNotebook,-1);
192 mwxNotebook->AddPage( mwxPageCommand, _T("Command"));
194 mwxPageHelp = new wxPanel(mwxNotebook,-1);
195 mwxNotebook->AddPage( mwxPageHelp, _T("Help"));
197 sizer->Add ( mwxNotebook, 1, wxGROW | wxALIGN_BOTTOM );
199 wxBoxSizer *cmdsizer = new wxBoxSizer(wxVERTICAL);
201 mwxPageCommand->SetAutoLayout(true);
202 mwxPageCommand->SetSizer(cmdsizer);
203 cmdsizer->Fit(mwxPageCommand);
204 cmdsizer->SetSizeHints(mwxPageCommand);
206 wxBoxSizer *helpsizer = new wxBoxSizer(wxVERTICAL);
208 mwxPageHelp->SetAutoLayout(true);
209 mwxPageHelp->SetSizer(helpsizer);
210 helpsizer->Fit(mwxPageHelp);
211 helpsizer->SetSizeHints(mwxPageHelp);
213 mwxHtmlWindow = new WxBrowser(mwxPageHelp,
216 // mwxHtmlWindow->SetSize(wxSize(800,1000));
217 helpsizer->Add (mwxHtmlWindow, wxALL | wxGROW );
223 new wxTextCtrl(mwxPageCommand,0,_T(""),wxDefaultPosition,
224 wxDefaultSize, //HistorySize,
228 wxFont* FixedFont = new wxFont(10,
234 mwxTextHistoryAttr = new wxTextAttr;
235 mwxTextHistoryAttr->SetFont(*FixedFont);
237 new wxTextCtrl(mwxPageCommand,0,_T(""),wxDefaultPosition,
238 wxDefaultSize,//CommandSize,
242 wxTE_PROCESS_TAB | wxWANTS_CHARS
247 mwxTextCommandAttr = new wxTextAttr;
248 mwxTextCommandAttr->SetFont(*FixedFont);
249 mwxTextCommand->SetDefaultStyle(*mwxTextCommandAttr);
250 mwxTextCommand->SetFocus();
254 cmdsizer->Add ( mwxTextHistory, 1, wxALL | wxGROW, 10);
256 cmdsizer->Add ( mwxTextCommand, 0, wxLEFT | wxRIGHT | wxBOTTOM
259 // cmdsizer->AddGrowableCol(0);
260 // cmdsizer->AddGrowableRow(0);
261 // cmdsizer->AddGrowableRow(1);
262 // cmdsizer->SetFlexibleDirection(wxBOTH);
264 //=============================
268 Connect( mwxTextCommand->GetId(),
269 wxEVT_COMMAND_TEXT_ENTER,
270 (wxObjectEventFunction)& WxConsole::OnCommandEnter );
271 Connect( mwxTextCommand->GetId(),
273 //wxEVT_COMMAND_TEXT_UPDATED,
274 (wxObjectEventFunction)& WxConsole::OnCommandChar );
278 // Redirection of std::cout to mwxTextHistory and printf
280 new WxTextCtrlStreamRedirector(std::cout,mwxTextHistory,*wxBLACK,true);
282 new WxTextCtrlStreamRedirector(std::cerr,mwxTextHistory,*wxGREEN,true);
284 // Sets the console as the parent window of all bbtk windows
285 WxBlackBox::bbGlobalSetTopWindow(this);
294 //================================================================
296 //================================================================
297 WxConsole::~WxConsole()
299 delete mRedirect_cout;
300 delete mRedirect_cerr;
302 //================================================================
304 //================================================================
305 void WxConsole::OnCommandEnter(wxCommandEvent& event)
307 wxString line(mwxTextCommand->GetValue());
308 wxString s = _T("> ") + line + _T("\n");
309 mwxTextHistoryAttr->SetTextColour(*wxRED);
310 mwxTextHistory->SetDefaultStyle(*mwxTextHistoryAttr);
311 mwxTextHistory->AppendText(s);
312 // send to standard console also
313 printf("%s",wx2std(s).c_str());
314 mwxTextCommand->Clear();
315 mwxTextCommand->SetDefaultStyle(*mwxTextCommandAttr);
316 mwxTextHistoryAttr->SetTextColour(*wxBLACK);
317 mwxTextHistory->SetDefaultStyle(*mwxTextHistoryAttr);
321 bool insideComment = false;
322 mInterpreter->InterpretLine( wx2std(line), insideComment );
324 catch (bbtk::QuitException)
328 catch (bbtk::Exception e)
332 catch (std::exception& e)
334 std::cout << "* ERROR : "<<e.what()<<" (not in bbtk)"<<std::endl;
338 std::cout << "* UNDEFINED ERROR (not a bbtk nor a std exception)"
342 //================================================================
345 //================================================================
346 void WxConsole::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
350 //================================================================
353 //================================================================
354 void WxConsole::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
357 wxMessageBox(_T(" bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS-LRMN 2007"),
358 _T("About ..."), wxOK | wxICON_INFORMATION,
361 //================================================================
363 //================================================================
364 void WxConsole::OnCommandChar(wxCommandEvent& event)
368 // Command completion
369 std::vector<std::string> commands;
370 wxString sline( wx2std ( mwxTextCommand->GetValue() ) );
371 int ind = sline.size();
372 mInterpreter->FindCommandsWithPrefix( sline.c_str(),ind,commands);
373 if (commands.size()==1)
375 std::string com = *commands.begin();
376 for (; ind<com.size(); ++ind)
378 mwxTextCommand->TextAppend( std2wx ( com[ind]) );
380 mwxTextCommand->TextAppend(_T(" "));
382 else if (commands.size()>1)
384 std::vector<std::string>::iterator i;
386 for (i=commands.begin();i!=commands.end();++i)
388 write(STDOUT_FILENO,(*i).c_str(),strlen((*i).c_str()));
391 write(STDOUT_FILENO,"\n> ",3);
392 //for (int j=0;j<ind;++j)
394 write(STDOUT_FILENO,line,ind);
399 //================================================================
401 void WxConsole::ShowHtmlPage(std::string& page)
403 // std::cout << "WxConsole::ShowHtmlPage('"<<page<<"')"<<std::endl;
404 if (!mwxHtmlWindow->GoTo(page))
406 // std::cout << "ERROR html"<<std::endl;
410 //================================================================
411 BEGIN_EVENT_TABLE(WxConsole, wxFrame)
412 EVT_MENU(WxConsole::ID_Menu_Quit, WxConsole::OnMenuQuit)
413 EVT_MENU(WxConsole::ID_Menu_About, WxConsole::OnMenuAbout)
415 //================================================================
420 #endif //_USE_WXWIDGETS_