]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxStreamRedirector.h
7bd19f3a8e59d878e223385a384cfb4069b7ab2e
[bbtk.git] / kernel / src / bbtkWxStreamRedirector.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkWxStreamRedirector.h,v $
4   Language:  C++
5   Date:      $Date: 2008/10/17 08:18:15 $
6   Version:   $Revision: 1.2 $
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  
86     WxStreamRedirector(std::ostream& redirect,
87                                  wxTextCtrl *text, 
88                                  const wxColour& colour = *wxBLACK,
89                                  bool doprintf=true,
90                                  int bufferSize=1000)
91       : mText(text),
92         mPrintf(doprintf),
93         m_ostr(redirect),
94         mColour(colour)
95     {
96       if (bufferSize)
97         {
98           char *ptr = new char[bufferSize];
99           setp(ptr, ptr + bufferSize);
100         }
101       else
102         setp(0, 0);
103       
104       m_sbufOld = m_ostr.rdbuf();
105       m_ostr.rdbuf(this);
106     }
107     
108     ~WxStreamRedirector()
109     {
110       sync();
111       delete[] pbase();
112       m_ostr.rdbuf(m_sbufOld);
113     }
114   
115    virtual void writeString(const std::string &str) 
116     {
117       const wxTextAttr& style = mText->GetDefaultStyle();
118       mText->SetDefaultStyle(mColour);
119       mText->AppendText(std2wx(str));
120       mText->SetDefaultStyle(style);
121      
122       if (mPrintf) 
123         {
124           printf("%s",str.c_str());
125         }
126     }
127     
128   
129   private:
130     wxTextCtrl* mText;
131     // 
132     bool mPrintf;
133     // the stream we're redirecting
134     std::ostream&   m_ostr;
135     // the old streambuf (before we changed it)
136     std::streambuf *m_sbufOld;
137     //
138     wxColour mColour;
139     
140   private:
141     int overflow(int c)
142     {
143       sync();
144       
145       if (c != EOF)
146         {
147           if (pbase() == epptr())
148             {
149               std::string temp;
150               temp += char(c);
151               writeString(temp);
152             }
153           else
154             sputc(c);
155         }
156       
157       return 0;
158     }
159     
160     int sync()
161     {
162       if (pbase() != pptr())
163         {
164           int len = int(pptr() - pbase());
165           std::string temp(pbase(), len);
166           writeString(temp);
167           setp(pbase(), epptr());
168         }
169       return 0;
170     }
171   };
172   //================================================================
173
174   
175
176 } // namespace bbtk
177
178
179 #endif // __bbtkWxStreamRedirector_h__
180
181 #endif //_USE_WXWIDGETS_