1 /*=========================================================================
4 Module: $RCSfile: bbtkWxStreamRedirector.h,v $
6 Date: $Date: 2008/03/18 12:51:27 $
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 #ifndef __bbtkWxStreamRedirector_h__
37 #define __bbtkWxStreamRedirector_h__
47 // On Windows when compiling a dll, wx prevents the compilation
48 // of the class wxStreamToTextRedirector (why ? it is a nightmare...)
49 // The blocking symbol is wxHAS_TEXT_WINDOW_STREAM.
50 // Note also that wxStreamToTextRedirector use the fact that wx is
51 // compiled with the option WX_USE_STD_STREAMS in which case
52 // wxTextCtrl inherits from std::streambuf and the redirection
53 // can be done simply by setting the std::cout buffer to the
54 // one of the wxTextCtrl.
55 // So on windows, we have to redirect manually std::cout to mwxTextHistory.
56 // Finally, on all systems we made our redirection class to redirect both to
57 // the WxConsole and to printf in order to get a console trace when
58 // the appli crashes (we could also imagine to log in a file...)
59 // This is why we finally wrote our own redirection which is crossplatform
60 // (drawback : not optimal on Unix platform; we could think of
61 // a particular implementation...).
63 //================================================================
64 /// Redirects std::cout to a wxTextCtrl and optionally to printf also
65 class WxStreamRedirector : public std::streambuf
71 WxStreamRedirector(std::ostream& redirect,
73 const wxColour& colour = *wxBLACK,
83 char *ptr = new char[bufferSize];
84 setp(ptr, ptr + bufferSize);
89 m_sbufOld = m_ostr.rdbuf();
97 m_ostr.rdbuf(m_sbufOld);
100 virtual void writeString(const std::string &str)
102 const wxTextAttr& style = mText->GetDefaultStyle();
103 mText->SetDefaultStyle(mColour);
104 mText->AppendText(std2wx(str));
105 mText->SetDefaultStyle(style);
109 printf("%s",str.c_str());
118 // the stream we're redirecting
119 std::ostream& m_ostr;
120 // the old streambuf (before we changed it)
121 std::streambuf *m_sbufOld;
132 if (pbase() == epptr())
147 if (pbase() != pptr())
149 int len = int(pptr() - pbase());
150 std::string temp(pbase(), len);
152 setp(pbase(), epptr());
157 //================================================================
164 #endif // __bbtkWxStreamRedirector_h__
166 #endif //_USE_WXWIDGETS_