1 /*=========================================================================
3 Module: $RCSfile: bbtkWxStreamRedirector.h,v $
5 Date: $Date: 2008/10/17 08:18:15 $
6 Version: $Revision: 1.2 $
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
48 #ifdef _USE_WXWIDGETS_
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
86 WxStreamRedirector(std::ostream& redirect,
88 const wxColour& colour = *wxBLACK,
98 char *ptr = new char[bufferSize];
99 setp(ptr, ptr + bufferSize);
104 m_sbufOld = m_ostr.rdbuf();
108 ~WxStreamRedirector()
112 m_ostr.rdbuf(m_sbufOld);
115 virtual void writeString(const std::string &str)
117 const wxTextAttr& style = mText->GetDefaultStyle();
118 mText->SetDefaultStyle(mColour);
119 mText->AppendText(std2wx(str));
120 mText->SetDefaultStyle(style);
124 printf("%s",str.c_str());
133 // the stream we're redirecting
134 std::ostream& m_ostr;
135 // the old streambuf (before we changed it)
136 std::streambuf *m_sbufOld;
147 if (pbase() == epptr())
162 if (pbase() != pptr())
164 int len = int(pptr() - pbase());
165 std::string temp(pbase(), len);
167 setp(pbase(), epptr());
172 //================================================================
179 #endif // __bbtkWxStreamRedirector_h__
181 #endif //_USE_WXWIDGETS_