Program: bbtk
Module: $RCSfile: bbtkWxConsole.cxx,v $
Language: C++
- Date: $Date: 2008/03/17 11:28:11 $
- Version: $Revision: 1.15 $
+ Date: $Date: 2008/03/18 12:51:26 $
+ Version: $Revision: 1.16 $
Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
l'Image). All rights reserved. See Doc/License.txt or
#include "bbtkWxConsole.h"
#include "bbtkWxBlackBox.h"
#include "bbtkConfigurationFile.h"
+#include "bbtkWxStreamRedirector.h"
namespace bbtk
{
-// On Windows when compiling a dll, wx prevents the compilation
-// of the class wxStreamToTextRedirector (why ? it is a nightmare...)
-// The blocking symbol is wxHAS_TEXT_WINDOW_STREAM.
-// Note also that wxStreamToTextRedirector use the fact that wx is
-// compiled with the option WX_USE_STD_STREAMS in which case
-// wxTextCtrl inherits from std::streambuf and the redirection
-// can be done simply by setting the std::cout buffer to the
-// one of the wxTextCtrl.
-// So on windows, we have to redirect manually std::cout to mwxTextHistory.
-// Finally, on all systems we made our redirection class to redirect both to
-// the WxConsole and to printf in order to get a console trace when
-// the appli crashes (we could also imagine to log in a file...)
-// This is why we finally wrote our own redirection which is crossplatform
-// (drawback : not optimal on Unix platform; we could think of
-// a particular implementation...).
- //================================================================
- /// Redirects std::cout to a wxTextCtrl and optionally to printf also
- class WxTextCtrlStreamRedirector : public std::streambuf
- {
-
- public:
-
-
- WxTextCtrlStreamRedirector(std::ostream& redirect,
- wxTextCtrl *text,
- const wxColour& colour = *wxBLACK,
- bool doprintf=true,
- int bufferSize=1000)
- : mText(text),
- mPrintf(doprintf),
- m_ostr(redirect),
- mColour(colour)
- {
- if (bufferSize)
- {
- char *ptr = new char[bufferSize];
- setp(ptr, ptr + bufferSize);
- }
- else
- setp(0, 0);
-
- m_sbufOld = m_ostr.rdbuf();
- m_ostr.rdbuf(this);
- }
-
- ~WxTextCtrlStreamRedirector()
- {
- sync();
- delete[] pbase();
- m_ostr.rdbuf(m_sbufOld);
- }
-
- virtual void writeString(const std::string &str)
- {
- const wxTextAttr& style = mText->GetDefaultStyle();
- mText->SetDefaultStyle(mColour);
- mText->AppendText(std2wx(str));
- mText->SetDefaultStyle(style);
-
- if (mPrintf)
- {
- printf("%s",str.c_str());
- }
- }
-
-
- private:
- wxTextCtrl* mText;
- //
- bool mPrintf;
- // the stream we're redirecting
- std::ostream& m_ostr;
- // the old streambuf (before we changed it)
- std::streambuf *m_sbufOld;
- //
- wxColour mColour;
-
- private:
- int overflow(int c)
- {
- sync();
-
- if (c != EOF)
- {
- if (pbase() == epptr())
- {
- std::string temp;
- temp += char(c);
- writeString(temp);
- }
- else
- sputc(c);
- }
-
- return 0;
- }
-
- int sync()
- {
- if (pbase() != pptr())
- {
- int len = int(pptr() - pbase());
- std::string temp(pbase(), len);
- writeString(temp);
- setp(pbase(), epptr());
- }
- return 0;
- }
- };
- //================================================================
-
-
//================================================================
WxConsole::WxConsole( wxWindow *parent, wxString title, wxSize size)
// Redirection of std::cout to mwxTextHistory and printf
mRedirect_cout =
- new WxTextCtrlStreamRedirector(std::cout,mwxTextHistory,*wxBLACK,true);
+ new WxStreamRedirector(std::cout,mwxTextHistory,*wxBLACK,true);
mRedirect_cerr =
- new WxTextCtrlStreamRedirector(std::cerr,mwxTextHistory,*wxGREEN,true);
+ new WxStreamRedirector(std::cerr,mwxTextHistory,*wxGREEN,true);
// Creates and sets the parent window of all bbtk windows
wxWindow* top = new wxPanel(this,-1);//,_T("top"));