/*========================================================================= Program: bbtk Module: $RCSfile: bbtkWxStreamRedirector.h,v $ Language: C++ Date: $Date: 2008/03/18 12:51:27 $ Version: $Revision: 1.1 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*//** * \brief Short description in one line * * Long description which * can span multiple lines */ /** * \file * \brief */ /** * \class bbtk:: * \brief */ #ifdef _USE_WXWIDGETS_ #ifndef __bbtkWxStreamRedirector_h__ #define __bbtkWxStreamRedirector_h__ #include "bbtkWx.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 WxStreamRedirector : public std::streambuf { public: WxStreamRedirector(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); } ~WxStreamRedirector() { 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; } }; //================================================================ } // namespace bbtk #endif // __bbtkWxStreamRedirector_h__ #endif //_USE_WXWIDGETS_