1 /*=========================================================================
4 Module: $RCSfile: bbtkWxConsole.cxx,v $
6 Date: $Date: 2008/02/12 12:55:16 $
7 Version: $Revision: 1.3 $
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,
225 _T(""),wxDefaultPosition,
226 wxDefaultSize, //HistorySize,
230 wxFont* FixedFont = new wxFont(10,
236 mwxTextHistoryAttr = new wxTextAttr;
237 mwxTextHistoryAttr->SetFont(*FixedFont);
239 new wxTextCtrl(mwxPageCommand,
241 _T(""),wxDefaultPosition,
250 mwxTextCommandAttr = new wxTextAttr;
251 mwxTextCommandAttr->SetFont(*FixedFont);
252 mwxTextCommand->SetDefaultStyle(*mwxTextCommandAttr);
253 mwxTextCommand->SetFocus();
257 cmdsizer->Add ( mwxTextHistory, 1, wxALL | wxGROW, 10);
259 cmdsizer->Add ( mwxTextCommand, 0, wxLEFT | wxRIGHT | wxBOTTOM
262 // cmdsizer->AddGrowableCol(0);
263 // cmdsizer->AddGrowableRow(0);
264 // cmdsizer->AddGrowableRow(1);
265 // cmdsizer->SetFlexibleDirection(wxBOTH);
267 //=============================
272 Connect( mwxTextCommand->GetId(),
273 wxEVT_COMMAND_TEXT_ENTER,
274 (wxObjectEventFunction)& WxConsole::OnCommandEnter );
275 Connect( mwxTextCommand->GetId(),
277 //wxEVT_COMMAND_TEXT_UPDATED,
278 (wxObjectEventFunction)& WxConsole::OnCommandChar );
283 // Redirection of std::cout to mwxTextHistory and printf
285 new WxTextCtrlStreamRedirector(std::cout,mwxTextHistory,*wxBLACK,true);
287 new WxTextCtrlStreamRedirector(std::cerr,mwxTextHistory,*wxGREEN,true);
289 // Sets the console as the parent window of all bbtk windows
290 Wx::SetTopWindow(this);
299 //================================================================
301 //================================================================
302 WxConsole::~WxConsole()
304 delete mRedirect_cout;
305 delete mRedirect_cerr;
307 //================================================================
309 //================================================================
310 void WxConsole::OnCommandEnter(wxCommandEvent& event)
312 wxString line(mwxTextCommand->GetValue());
313 wxString s = _T("> ") + line + _T("\n");
314 mwxTextHistoryAttr->SetTextColour(*wxRED);
315 mwxTextHistory->SetDefaultStyle(*mwxTextHistoryAttr);
316 mwxTextHistory->AppendText(s);
317 // send to standard console also
318 printf("%s",wx2std(s).c_str());
319 mwxTextCommand->Clear();
320 mwxTextCommand->SetDefaultStyle(*mwxTextCommandAttr);
321 mwxTextHistoryAttr->SetTextColour(*wxBLACK);
322 mwxTextHistory->SetDefaultStyle(*mwxTextHistoryAttr);
326 bool insideComment = false;
327 mInterpreter->InterpretLine( wx2std(line), insideComment );
329 catch (bbtk::QuitException)
333 catch (bbtk::Exception e)
337 catch (std::exception& e)
339 std::cout << "* ERROR : "<<e.what()<<" (not in bbtk)"<<std::endl;
343 std::cout << "* UNDEFINED ERROR (not a bbtk nor a std exception)"
347 //================================================================
350 //================================================================
351 void WxConsole::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
355 //================================================================
358 //================================================================
359 void WxConsole::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
362 wxMessageBox(_T(" bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS-LRMN 2007"),
363 _T("About ..."), wxOK | wxICON_INFORMATION,
366 //================================================================
368 //================================================================
369 void WxConsole::OnCommandChar(wxCommandEvent& event)
373 // Command completion
374 std::vector<std::string> commands;
375 wxString sline( wx2std ( mwxTextCommand->GetValue() ) );
376 int ind = sline.size();
377 mInterpreter->FindCommandsWithPrefix( sline.c_str(),ind,commands);
378 if (commands.size()==1)
380 std::string com = *commands.begin();
381 for (; ind<com.size(); ++ind)
383 mwxTextCommand->TextAppend( std2wx ( com[ind]) );
385 mwxTextCommand->TextAppend(_T(" "));
387 else if (commands.size()>1)
389 std::vector<std::string>::iterator i;
391 for (i=commands.begin();i!=commands.end();++i)
393 write(STDOUT_FILENO,(*i).c_str(),strlen((*i).c_str()));
396 write(STDOUT_FILENO,"\n> ",3);
397 //for (int j=0;j<ind;++j)
399 write(STDOUT_FILENO,line,ind);
404 //================================================================
406 void WxConsole::ShowHtmlPage(std::string& page)
408 // std::cout << "WxConsole::ShowHtmlPage('"<<page<<"')"<<std::endl;
409 if (mwxHtmlWindow->GoTo(page))
411 mwxNotebook->ChangeSelection(1);
415 // std::cout << "ERROR html"<<std::endl;
419 //================================================================
420 BEGIN_EVENT_TABLE(WxConsole, wxFrame)
421 EVT_MENU(WxConsole::ID_Menu_Quit, WxConsole::OnMenuQuit)
422 EVT_MENU(WxConsole::ID_Menu_About, WxConsole::OnMenuAbout)
423 EVT_TEXT_ENTER(WxConsole::ID_Text_Command, WxConsole::OnCommandEnter)
424 // EVT_CHAR(WxConsole::ID_Text_Command, WxConsole::OnCommandChar)
426 //================================================================
431 #endif //_USE_WXWIDGETS_