]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxStreamRedirector.h
c560f5dc50897ce3eb487be39ae7e339e07edad9
[bbtk.git] / kernel / src / bbtkWxStreamRedirector.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkWxStreamRedirector.h,v $
4   Language:  C++
5   Date:      $Date: 2010/01/14 13:17:27 $
6   Version:   $Revision: 1.4 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
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.
20 *
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
25 *  liability. 
26 *
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 * ------------------------------------------------------------------------ */                                                                         
30
31
32 /**
33  * \brief Short description in one line
34  * 
35  * Long description which 
36  * can span multiple lines
37  */
38 /**
39  * \file 
40  * \brief 
41  */
42 /**
43  * \class bbtk::
44  * \brief 
45  */
46
47
48 #ifdef USE_WXWIDGETS
49
50         
51 #ifndef __bbtkWxStreamRedirector_h__
52 #define __bbtkWxStreamRedirector_h__
53
54
55 #include "bbtkWx.h"
56
57
58 namespace bbtk
59 {
60
61
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...).
77
78   //================================================================
79   /// Redirects std::cout to a wxTextCtrl and optionally to printf also
80   class WxStreamRedirector   : public std::streambuf
81   {       
82
83   public:
84
85     WxStreamRedirector(std::ostream& redirect,
86                                  wxTextCtrl *text, 
87                                  const wxColour& colour = *wxBLACK,
88                                  bool doprintf=true,
89                                  int bufferSize=1000)
90       : mText(text),
91         mPrintf(doprintf),
92         m_ostr(redirect),
93         mColour(colour)
94     {
95       if (bufferSize)
96         {
97           char *ptr = new char[bufferSize];
98           setp(ptr, ptr + bufferSize);
99         }
100       else
101         setp(0, 0);
102       
103       m_sbufOld = m_ostr.rdbuf();
104       m_ostr.rdbuf(this);
105     }
106     
107     ~WxStreamRedirector()
108     {
109       sync();
110       delete[] pbase();
111       m_ostr.rdbuf(m_sbufOld);
112     }
113
114    virtual void writeString(const std::string &str) 
115     {
116       const wxTextAttr& style = mText->GetDefaultStyle();
117       mText->SetDefaultStyle(mColour);
118       mText->AppendText(std2wx(str));
119       mText->SetDefaultStyle(style);
120      
121       if (mPrintf) 
122         {
123           printf("%s",str.c_str());
124         }
125     }
126
127   private:
128     wxTextCtrl* mText;
129     // 
130     bool mPrintf;
131     // the stream we're redirecting
132     std::ostream&   m_ostr;
133     // the old streambuf (before we changed it)
134     std::streambuf *m_sbufOld;
135     //
136     wxColour mColour;
137
138   private:
139     int overflow(int c)
140     {
141       sync();
142       
143       if (c != EOF)
144         {
145           if (pbase() == epptr())
146             {
147               std::string temp;
148               temp += char(c);
149               writeString(temp);
150             }
151           else
152             sputc(c);
153         }
154       
155       return 0;
156     }
157
158     int sync()
159     {
160       if (pbase() != pptr())
161         {
162           int len = int(pptr() - pbase());
163           std::string temp(pbase(), len);
164           writeString(temp);
165           setp(pbase(), epptr());
166         }
167       return 0;
168     }
169   };
170
171   //================================================================
172
173 } // namespace bbtk
174
175 #endif // __bbtkWxStreamRedirector_h__
176
177 #endif //USE_WXWIDGETS