-// On Windows when compiling a dll, wx prevents the compilation
-// of the class wxStreamToTextRedirector (why ? it is a nightmare...)
-// The blocking symbol is wxHAS_TEXT_WINDOW_STREAM.
-// Note also that wxStreamToTextRedirector use the fact that wx is
-// compiled with the option WX_USE_STD_STREAMS in which case
-// wxTextCtrl inherits from std::streambuf and the redirection
-// can be done simply by setting the std::cout buffer to the
-// one of the wxTextCtrl.
-// So on windows, we have to redirect manually std::cout to mwxTextHistory.
-// Finally, on all systems we made our redirection class to redirect both to
-// the WxConsole and to printf in order to get a console trace when
-// the appli crashes (we could also imagine to log in a file...)
-// This is why we finally wrote our own redirection which is crossplatform
-// (drawback : not optimal on Unix platform; we could think of
-// a particular implementation...).
-
- //================================================================
- /// Redirects std::cout to a wxTextCtrl and optionally to printf also
- class WxTextCtrlStreamRedirector : public std::streambuf
- {
-
- public:
-
-
- WxTextCtrlStreamRedirector(std::ostream& redirect,
- wxTextCtrl *text,
- const wxColour& colour = *wxBLACK,
- bool doprintf=true,
- int bufferSize=1000)
- : mText(text),
- mPrintf(doprintf),
- m_ostr(redirect),
- mColour(colour)
- {
- if (bufferSize)
- {
- char *ptr = new char[bufferSize];
- setp(ptr, ptr + bufferSize);
- }
- else
- setp(0, 0);
-
- m_sbufOld = m_ostr.rdbuf();
- m_ostr.rdbuf(this);
- }
-
- ~WxTextCtrlStreamRedirector()
- {
- sync();
- delete[] pbase();
- m_ostr.rdbuf(m_sbufOld);
- }
-
- virtual void writeString(const std::string &str)
- {
- const wxTextAttr& style = mText->GetDefaultStyle();
- mText->SetDefaultStyle(mColour);
- mText->AppendText(std2wx(str));
- mText->SetDefaultStyle(style);
-
- if (mPrintf)
- {
- printf("%s",str.c_str());
- }
- }
-
-
- private:
- wxTextCtrl* mText;
- //
- bool mPrintf;
- // the stream we're redirecting
- std::ostream& m_ostr;
- // the old streambuf (before we changed it)
- std::streambuf *m_sbufOld;
- //
- wxColour mColour;
-
- private:
- int overflow(int c)
- {
- sync();
-
- if (c != EOF)
- {
- if (pbase() == epptr())
- {
- std::string temp;
- temp += char(c);
- writeString(temp);
- }
- else
- sputc(c);
- }
-
- return 0;
- }
-
- int sync()
- {
- if (pbase() != pptr())
- {
- int len = int(pptr() - pbase());
- std::string temp(pbase(), len);
- writeString(temp);
- setp(pbase(), epptr());
- }
- return 0;
- }
- };
- //================================================================
-
-