bbtkWxStreamRedirector.h

Go to the documentation of this file.
00001 /*=========================================================================                                                                               
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkWxStreamRedirector.h,v $
00004   Language:  C++
00005   Date:      $Date: 2008/10/17 08:18:15 $
00006   Version:   $Revision: 1.2 $
00007 =========================================================================*/
00008 
00009 /* ---------------------------------------------------------------------
00010 
00011 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
00012 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
00013 *
00014 *  This software is governed by the CeCILL-B license under French law and 
00015 *  abiding by the rules of distribution of free software. You can  use, 
00016 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
00017 *  license as circulated by CEA, CNRS and INRIA at the following URL 
00018 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
00019 *  or in the file LICENSE.txt.
00020 *
00021 *  As a counterpart to the access to the source code and  rights to copy,
00022 *  modify and redistribute granted by the license, users are provided only
00023 *  with a limited warranty  and the software's author,  the holder of the
00024 *  economic rights,  and the successive licensors  have only  limited
00025 *  liability. 
00026 *
00027 *  The fact that you are presently reading this means that you have had
00028 *  knowledge of the CeCILL-B license and that you accept its terms.
00029 * ------------------------------------------------------------------------ */                                                                         
00030 
00031 
00048 #ifdef _USE_WXWIDGETS_
00049 
00050         
00051 #ifndef __bbtkWxStreamRedirector_h__
00052 #define __bbtkWxStreamRedirector_h__
00053 
00054 
00055 #include "bbtkWx.h"
00056 
00057 
00058 namespace bbtk
00059 {
00060 
00061 
00062 // On Windows when compiling a dll, wx prevents the compilation
00063 // of the class wxStreamToTextRedirector (why ? it is a nightmare...)
00064 // The blocking symbol is wxHAS_TEXT_WINDOW_STREAM.
00065 // Note also that wxStreamToTextRedirector use the fact that wx is 
00066 // compiled with the option WX_USE_STD_STREAMS in which case 
00067 // wxTextCtrl inherits from std::streambuf and the redirection 
00068 // can be done simply by setting the std::cout buffer to the 
00069 // one of the wxTextCtrl. 
00070 // So on windows, we have to redirect manually std::cout to mwxTextHistory.  
00071 // Finally, on all systems we made our redirection class to redirect both to
00072 // the WxConsole and to printf in order to get a console trace when 
00073 // the appli crashes (we could also imagine to log in a file...)
00074 // This is why we finally wrote our own redirection which is crossplatform
00075 // (drawback : not optimal on Unix platform; we could think of 
00076 // a particular implementation...).
00077 
00078   //================================================================
00080   class WxStreamRedirector   : public std::streambuf
00081   {       
00082     
00083   public:
00084     
00085  
00086     WxStreamRedirector(std::ostream& redirect,
00087                                  wxTextCtrl *text, 
00088                                  const wxColour& colour = *wxBLACK,
00089                                  bool doprintf=true,
00090                                  int bufferSize=1000)
00091       : mText(text),
00092         mPrintf(doprintf),
00093         m_ostr(redirect),
00094         mColour(colour)
00095     {
00096       if (bufferSize)
00097         {
00098           char *ptr = new char[bufferSize];
00099           setp(ptr, ptr + bufferSize);
00100         }
00101       else
00102         setp(0, 0);
00103       
00104       m_sbufOld = m_ostr.rdbuf();
00105       m_ostr.rdbuf(this);
00106     }
00107     
00108     ~WxStreamRedirector()
00109     {
00110       sync();
00111       delete[] pbase();
00112       m_ostr.rdbuf(m_sbufOld);
00113     }
00114   
00115    virtual void writeString(const std::string &str) 
00116     {
00117       const wxTextAttr& style = mText->GetDefaultStyle();
00118       mText->SetDefaultStyle(mColour);
00119       mText->AppendText(std2wx(str));
00120       mText->SetDefaultStyle(style);
00121      
00122       if (mPrintf) 
00123         {
00124           printf("%s",str.c_str());
00125         }
00126     }
00127     
00128   
00129   private:
00130     wxTextCtrl* mText;
00131     // 
00132     bool mPrintf;
00133     // the stream we're redirecting
00134     std::ostream&   m_ostr;
00135     // the old streambuf (before we changed it)
00136     std::streambuf *m_sbufOld;
00137     //
00138     wxColour mColour;
00139     
00140   private:
00141     int overflow(int c)
00142     {
00143       sync();
00144       
00145       if (c != EOF)
00146         {
00147           if (pbase() == epptr())
00148             {
00149               std::string temp;
00150               temp += char(c);
00151               writeString(temp);
00152             }
00153           else
00154             sputc(c);
00155         }
00156       
00157       return 0;
00158     }
00159     
00160     int sync()
00161     {
00162       if (pbase() != pptr())
00163         {
00164           int len = int(pptr() - pbase());
00165           std::string temp(pbase(), len);
00166           writeString(temp);
00167           setp(pbase(), epptr());
00168         }
00169       return 0;
00170     }
00171   };
00172   //================================================================
00173 
00174   
00175 
00176 } // namespace bbtk
00177 
00178 
00179 #endif // __bbtkWxStreamRedirector_h__
00180 
00181 #endif //_USE_WXWIDGETS_

Generated on Wed Nov 12 11:37:08 2008 for BBTK by  doxygen 1.5.6