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