1 /*=========================================================================
3 Module: $RCSfile: bbtkWxStreamRedirector.h,v $
5 Date: $Date: 2010/01/14 13:17:27 $
6 Version: $Revision: 1.4 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
14 * This software is governed by the CeCILL-B license under French law and
15 * abiding by the rules of distribution of free software. You can use,
16 * modify and/ or redistribute the software under the terms of the CeCILL-B
17 * license as circulated by CEA, CNRS and INRIA at the following URL
18 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 * or in the file LICENSE.txt.
21 * As a counterpart to the access to the source code and rights to copy,
22 * modify and redistribute granted by the license, users are provided only
23 * with a limited warranty and the software's author, the holder of the
24 * economic rights, and the successive licensors have only limited
27 * The fact that you are presently reading this means that you have had
28 * knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
33 * \brief Short description in one line
35 * Long description which
36 * can span multiple lines
51 #ifndef __bbtkWxStreamRedirector_h__
52 #define __bbtkWxStreamRedirector_h__
62 // On Windows when compiling a dll, wx prevents the compilation
63 // of the class wxStreamToTextRedirector (why ? it is a nightmare...)
64 // The blocking symbol is wxHAS_TEXT_WINDOW_STREAM.
65 // Note also that wxStreamToTextRedirector use the fact that wx is
66 // compiled with the option WX_USE_STD_STREAMS in which case
67 // wxTextCtrl inherits from std::streambuf and the redirection
68 // can be done simply by setting the std::cout buffer to the
69 // one of the wxTextCtrl.
70 // So on windows, we have to redirect manually std::cout to mwxTextHistory.
71 // Finally, on all systems we made our redirection class to redirect both to
72 // the WxConsole and to printf in order to get a console trace when
73 // the appli crashes (we could also imagine to log in a file...)
74 // This is why we finally wrote our own redirection which is crossplatform
75 // (drawback : not optimal on Unix platform; we could think of
76 // a particular implementation...).
78 //================================================================
79 /// Redirects std::cout to a wxTextCtrl and optionally to printf also
80 class WxStreamRedirector : public std::streambuf
85 WxStreamRedirector(std::ostream& redirect,
87 const wxColour& colour = *wxBLACK,
97 char *ptr = new char[bufferSize];
98 setp(ptr, ptr + bufferSize);
103 m_sbufOld = m_ostr.rdbuf();
107 ~WxStreamRedirector()
111 m_ostr.rdbuf(m_sbufOld);
114 virtual void writeString(const std::string &str)
116 const wxTextAttr& style = mText->GetDefaultStyle();
117 mText->SetDefaultStyle(mColour);
118 mText->AppendText(std2wx(str));
119 mText->SetDefaultStyle(style);
123 printf("%s",str.c_str());
131 // the stream we're redirecting
132 std::ostream& m_ostr;
133 // the old streambuf (before we changed it)
134 std::streambuf *m_sbufOld;
145 if (pbase() == epptr())
160 if (pbase() != pptr())
162 int len = int(pptr() - pbase());
163 std::string temp(pbase(), len);
165 setp(pbase(), epptr());
171 //================================================================
175 #endif // __bbtkWxStreamRedirector_h__
177 #endif //USE_WXWIDGETS