1 /*=========================================================================
4 Module: $RCSfile: bbtkWxConsole.cxx,v $
6 Date: $Date: 2008/02/14 13:44:25 $
7 Version: $Revision: 1.4 $
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 //================================================================
310 //================================================================
311 void WxConsole::OnCommandEnter(wxCommandEvent& event)
313 wxString line(mwxTextCommand->GetValue());
314 wxString s = _T("> ") + line + _T("\n");
315 mwxTextHistoryAttr->SetTextColour(*wxRED);
316 mwxTextHistory->SetDefaultStyle(*mwxTextHistoryAttr);
317 mwxTextHistory->AppendText(s);
318 // send to standard console also
319 printf("%s",wx2std(s).c_str());
320 mwxTextCommand->Clear();
321 mwxTextCommand->SetDefaultStyle(*mwxTextCommandAttr);
322 mwxTextHistoryAttr->SetTextColour(*wxBLACK);
323 mwxTextHistory->SetDefaultStyle(*mwxTextHistoryAttr);
327 bool insideComment = false;
328 mInterpreter->InterpretLine( wx2std(line), insideComment );
330 catch (bbtk::QuitException)
334 catch (bbtk::Exception e)
338 catch (std::exception& e)
340 std::cout << "* ERROR : "<<e.what()<<" (not in bbtk)"<<std::endl;
344 std::cout << "* UNDEFINED ERROR (not a bbtk nor a std exception)"
348 //================================================================
351 //================================================================
352 void WxConsole::OnMenuQuit(wxCommandEvent& WXUNUSED(event))
356 //================================================================
359 //================================================================
360 void WxConsole::OnMenuAbout(wxCommandEvent& WXUNUSED(event))
363 wxMessageBox(_T(" bbi\nThe Black Box Toolkit interpreter\n(c) CREATIS-LRMN 2007"),
364 _T("About ..."), wxOK | wxICON_INFORMATION,
367 //================================================================
369 //================================================================
370 void WxConsole::OnCommandChar(wxCommandEvent& event)
374 // Command completion
375 std::vector<std::string> commands;
376 wxString sline( wx2std ( mwxTextCommand->GetValue() ) );
377 int ind = sline.size();
378 mInterpreter->FindCommandsWithPrefix( sline.c_str(),ind,commands);
379 if (commands.size()==1)
381 std::string com = *commands.begin();
382 for (; ind<com.size(); ++ind)
384 mwxTextCommand->TextAppend( std2wx ( com[ind]) );
386 mwxTextCommand->TextAppend(_T(" "));
388 else if (commands.size()>1)
390 std::vector<std::string>::iterator i;
392 for (i=commands.begin();i!=commands.end();++i)
394 write(STDOUT_FILENO,(*i).c_str(),strlen((*i).c_str()));
397 write(STDOUT_FILENO,"\n> ",3);
398 //for (int j=0;j<ind;++j)
400 write(STDOUT_FILENO,line,ind);
405 //================================================================
407 void WxConsole::ShowHtmlPage(std::string& page)
409 // std::cout << "WxConsole::ShowHtmlPage('"<<page<<"')"<<std::endl;
410 if (mwxHtmlWindow->GoTo(page))
412 mwxNotebook->ChangeSelection(1);
416 // std::cout << "ERROR html"<<std::endl;
420 //================================================================
421 BEGIN_EVENT_TABLE(WxConsole, wxFrame)
422 EVT_MENU(WxConsole::ID_Menu_Quit, WxConsole::OnMenuQuit)
423 EVT_MENU(WxConsole::ID_Menu_About, WxConsole::OnMenuAbout)
424 EVT_TEXT_ENTER(WxConsole::ID_Text_Command, WxConsole::OnCommandEnter)
425 // EVT_CHAR(WxConsole::ID_Text_Command, WxConsole::OnCommandChar)
427 //================================================================
432 #endif //_USE_WXWIDGETS_