]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxStreamRedirector.h
c324a4baf2fab2221bf1539f1b12f0afd78395c1
[bbtk.git] / kernel / src / bbtkWxStreamRedirector.h
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbtkWxStreamRedirector.h,v $
5   Language:  C++
6   Date:      $Date: 2008/03/18 12:51:27 $
7   Version:   $Revision: 1.1 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*//**
18  * \brief Short description in one line
19  * 
20  * Long description which 
21  * can span multiple lines
22  */
23 /**
24  * \file 
25  * \brief 
26  */
27 /**
28  * \class bbtk::
29  * \brief 
30  */
31
32
33 #ifdef _USE_WXWIDGETS_
34
35         
36 #ifndef __bbtkWxStreamRedirector_h__
37 #define __bbtkWxStreamRedirector_h__
38
39
40 #include "bbtkWx.h"
41
42
43 namespace bbtk
44 {
45
46
47 // On Windows when compiling a dll, wx prevents the compilation
48 // of the class wxStreamToTextRedirector (why ? it is a nightmare...)
49 // The blocking symbol is wxHAS_TEXT_WINDOW_STREAM.
50 // Note also that wxStreamToTextRedirector use the fact that wx is 
51 // compiled with the option WX_USE_STD_STREAMS in which case 
52 // wxTextCtrl inherits from std::streambuf and the redirection 
53 // can be done simply by setting the std::cout buffer to the 
54 // one of the wxTextCtrl. 
55 // So on windows, we have to redirect manually std::cout to mwxTextHistory.  
56 // Finally, on all systems we made our redirection class to redirect both to
57 // the WxConsole and to printf in order to get a console trace when 
58 // the appli crashes (we could also imagine to log in a file...)
59 // This is why we finally wrote our own redirection which is crossplatform
60 // (drawback : not optimal on Unix platform; we could think of 
61 // a particular implementation...).
62
63   //================================================================
64   /// Redirects std::cout to a wxTextCtrl and optionally to printf also
65   class WxStreamRedirector   : public std::streambuf
66   {       
67     
68   public:
69     
70  
71     WxStreamRedirector(std::ostream& redirect,
72                                  wxTextCtrl *text, 
73                                  const wxColour& colour = *wxBLACK,
74                                  bool doprintf=true,
75                                  int bufferSize=1000)
76       : mText(text),
77         mPrintf(doprintf),
78         m_ostr(redirect),
79         mColour(colour)
80     {
81       if (bufferSize)
82         {
83           char *ptr = new char[bufferSize];
84           setp(ptr, ptr + bufferSize);
85         }
86       else
87         setp(0, 0);
88       
89       m_sbufOld = m_ostr.rdbuf();
90       m_ostr.rdbuf(this);
91     }
92     
93     ~WxStreamRedirector()
94     {
95       sync();
96       delete[] pbase();
97       m_ostr.rdbuf(m_sbufOld);
98     }
99   
100    virtual void writeString(const std::string &str) 
101     {
102       const wxTextAttr& style = mText->GetDefaultStyle();
103       mText->SetDefaultStyle(mColour);
104       mText->AppendText(std2wx(str));
105       mText->SetDefaultStyle(style);
106      
107       if (mPrintf) 
108         {
109           printf("%s",str.c_str());
110         }
111     }
112     
113   
114   private:
115     wxTextCtrl* mText;
116     // 
117     bool mPrintf;
118     // the stream we're redirecting
119     std::ostream&   m_ostr;
120     // the old streambuf (before we changed it)
121     std::streambuf *m_sbufOld;
122     //
123     wxColour mColour;
124     
125   private:
126     int overflow(int c)
127     {
128       sync();
129       
130       if (c != EOF)
131         {
132           if (pbase() == epptr())
133             {
134               std::string temp;
135               temp += char(c);
136               writeString(temp);
137             }
138           else
139             sputc(c);
140         }
141       
142       return 0;
143     }
144     
145     int sync()
146     {
147       if (pbase() != pptr())
148         {
149           int len = int(pptr() - pbase());
150           std::string temp(pbase(), len);
151           writeString(temp);
152           setp(pbase(), epptr());
153         }
154       return 0;
155     }
156   };
157   //================================================================
158
159   
160
161 } // namespace bbtk
162
163
164 #endif // __bbtkWxStreamRedirector_h__
165
166 #endif //_USE_WXWIDGETS_