2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
28 /*=========================================================================
30 Module: $RCSfile: bbtkWxStreamRedirector.h,v $
32 Date: $Date: 2012/11/16 08:49:01 $
33 Version: $Revision: 1.5 $
34 =========================================================================*/
40 * \brief Short description in one line
42 * Long description which
43 * can span multiple lines
58 #ifndef __bbtkWxStreamRedirector_h__
59 #define __bbtkWxStreamRedirector_h__
69 // On Windows when compiling a dll, wx prevents the compilation
70 // of the class wxStreamToTextRedirector (why ? it is a nightmare...)
71 // The blocking symbol is wxHAS_TEXT_WINDOW_STREAM.
72 // Note also that wxStreamToTextRedirector use the fact that wx is
73 // compiled with the option WX_USE_STD_STREAMS in which case
74 // wxTextCtrl inherits from std::streambuf and the redirection
75 // can be done simply by setting the std::cout buffer to the
76 // one of the wxTextCtrl.
77 // So on windows, we have to redirect manually std::cout to mwxTextHistory.
78 // Finally, on all systems we made our redirection class to redirect both to
79 // the WxConsole and to printf in order to get a console trace when
80 // the appli crashes (we could also imagine to log in a file...)
81 // This is why we finally wrote our own redirection which is crossplatform
82 // (drawback : not optimal on Unix platform; we could think of
83 // a particular implementation...).
85 //================================================================
86 /// Redirects std::cout to a wxTextCtrl and optionally to printf also
87 class WxStreamRedirector : public std::streambuf
92 WxStreamRedirector(std::ostream& redirect,
94 const wxColour& colour = *wxBLACK,
104 char *ptr = new char[bufferSize];
105 setp(ptr, ptr + bufferSize);
110 m_sbufOld = m_ostr.rdbuf();
114 ~WxStreamRedirector()
118 m_ostr.rdbuf(m_sbufOld);
121 virtual void writeString(const std::string &str)
123 const wxTextAttr& style = mText->GetDefaultStyle();
124 mText->SetDefaultStyle(mColour);
125 mText->AppendText(std2wx(str));
126 mText->SetDefaultStyle(style);
130 printf("%s",str.c_str());
138 // the stream we're redirecting
139 std::ostream& m_ostr;
140 // the old streambuf (before we changed it)
141 std::streambuf *m_sbufOld;
152 if (pbase() == epptr())
167 if (pbase() != pptr())
169 int len = int(pptr() - pbase());
170 std::string temp(pbase(), len);
172 setp(pbase(), epptr());
178 //================================================================
182 #endif // __bbtkWxStreamRedirector_h__
184 #endif //USE_WXWIDGETS