+// ----------------------------------------------------------------------------
+// wxLogWindow and wxLogFrame implementation
+// ----------------------------------------------------------------------------
+
+// log frame class
+// ---------------
+class wxLogFrame : public wxFrame
+{
+public:
+ // ctor & dtor
+ wxLogFrame(wxLogWindow *log, const char *szTitle);
+ virtual ~wxLogFrame();
+
+ // menu callbacks
+ void OnClose(wxCommandEvent& event);
+ void OnCloseWindow(wxCloseEvent& event);
+ void OnSave (wxCommandEvent& event);
+ void OnClear(wxCommandEvent& event);
+
+ void OnIdle(wxIdleEvent&);
+
+ // accessors
+ wxTextCtrl *TextCtrl() const { return m_pTextCtrl; }
+
+private:
+ enum
+ {
+ Menu_Close = 100,
+ Menu_Save,
+ Menu_Clear
+ };
+
+ // instead of closing just hide the window to be able to Show() it later
+ void DoClose() { Show(FALSE); }
+
+ wxTextCtrl *m_pTextCtrl;
+ wxLogWindow *m_log;
+
+ DECLARE_EVENT_TABLE()
+};
+
+BEGIN_EVENT_TABLE(wxLogFrame, wxFrame)
+ // wxLogWindow menu events
+ EVT_MENU(Menu_Close, wxLogFrame::OnClose)
+ EVT_MENU(Menu_Save, wxLogFrame::OnSave)
+ EVT_MENU(Menu_Clear, wxLogFrame::OnClear)
+
+ EVT_CLOSE(wxLogFrame::OnCloseWindow)
+
+ EVT_IDLE(wxLogFrame::OnIdle)
+END_EVENT_TABLE()
+
+wxLogFrame::wxLogFrame(wxLogWindow *log, const char *szTitle)
+ : wxFrame(NULL, -1, szTitle)
+{
+ m_log = log;
+
+ // @@ kludge: wxSIMPLE_BORDER is simply to prevent wxWindows from creating
+ // a rich edit control instead of a normal one we want in wxMSW
+ m_pTextCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition,
+ wxDefaultSize,
+ wxSIMPLE_BORDER |
+ wxTE_MULTILINE |
+ wxHSCROLL |
+ wxTE_READONLY);
+ /*
+ m_pTextCtrl->SetEditable(FALSE);
+ m_pTextCtrl->SetRichEdit(FALSE);
+ */
+
+ // create menu
+ wxMenuBar *pMenuBar = new wxMenuBar;
+ wxMenu *pMenu = new wxMenu;
+ pMenu->Append(Menu_Save, _("&Save..."), _("Save log contents to file"));
+ pMenu->Append(Menu_Clear, _("C&lear"), _("Clear the log contents"));
+ pMenu->AppendSeparator();
+ pMenu->Append(Menu_Close, _("&Close"), _("Close this window"));
+ pMenuBar->Append(pMenu, _("&Log"));
+ SetMenuBar(pMenuBar);
+
+ // status bar for menu prompts
+ CreateStatusBar();
+
+ m_log->OnFrameCreate(this);
+}
+
+void wxLogFrame::OnClose(wxCommandEvent& WXUNUSED(event))
+{
+ DoClose();
+}
+
+void wxLogFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
+{
+ DoClose();
+}
+
+void wxLogFrame::OnIdle(wxIdleEvent& WXUNUSED(event))
+{
+ // if we're the last frame to stay, delete log frame letting the
+ // application to close
+ if ( wxTopLevelWindows.Number() == 1 )
+ Destroy();
+}
+
+void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event))
+{
+ // get the file name
+ // -----------------
+ const char *szFileName = wxSaveFileSelector("log", "txt", "log.txt");
+ if ( szFileName == NULL ) {
+ // cancelled
+ return;
+ }
+
+ // open file
+ // ---------
+ wxFile file;
+ bool bOk = FALSE;
+ if ( wxFile::Exists(szFileName) ) {
+ bool bAppend = FALSE;
+ wxString strMsg;
+ strMsg.Printf(_("Append log to file '%s' "
+ "(choosing [No] will overwrite it)?"), szFileName);
+ switch ( wxMessageBox(strMsg, _("Question"), wxYES_NO | wxCANCEL) ) {
+ case wxYES:
+ bAppend = TRUE;
+ break;
+
+ case wxNO:
+ bAppend = FALSE;
+ break;
+
+ case wxCANCEL:
+ return;
+
+ default:
+ wxFAIL_MSG(_("invalid message box return value"));
+ }
+
+ if ( bAppend ) {
+ bOk = file.Open(szFileName, wxFile::write_append);
+ }
+ else {
+ bOk = file.Create(szFileName, TRUE /* overwrite */);
+ }
+ }
+ else {
+ bOk = file.Create(szFileName);
+ }
+
+ // retrieve text and save it
+ // -------------------------
+#ifdef __WXGTK__
+ // @@@@ TODO: no GetNumberOfLines and GetLineText in wxGTK yet
+ wxLogError(_("Sorry, this function is not implemented under GTK"));
+#else
+ int nLines = m_pTextCtrl->GetNumberOfLines();
+ for ( int nLine = 0; bOk && nLine < nLines; nLine++ ) {
+ bOk = file.Write(m_pTextCtrl->GetLineText(nLine) + wxTextFile::GetEOL());
+ }
+#endif //GTK
+
+ if ( bOk )
+ bOk = file.Close();
+
+ if ( !bOk ) {
+ wxLogError(_("Can't save log contents to file."));
+ return;
+ }
+}
+
+void wxLogFrame::OnClear(wxCommandEvent& WXUNUSED(event))
+{
+ m_pTextCtrl->Clear();
+}
+
+wxLogFrame::~wxLogFrame()
+{
+ m_log->OnFrameDelete(this);
+}
+
+// wxLogWindow
+// -----------
+wxLogWindow::wxLogWindow(const char *szTitle, bool bShow, bool bDoPass)
+{
+ m_bPassMessages = bDoPass;
+
+ m_pLogFrame = new wxLogFrame(this, szTitle);
+ m_pOldLog = wxLog::SetActiveTarget(this);
+
+ if ( bShow )
+ m_pLogFrame->Show(TRUE);
+}
+
+void wxLogWindow::Show(bool bShow)
+{
+ m_pLogFrame->Show(bShow);
+}
+
+void wxLogWindow::Flush()
+{
+ if ( m_pOldLog != NULL )
+ m_pOldLog->Flush();
+
+ m_bHasMessages = FALSE;
+}
+
+void wxLogWindow::DoLog(wxLogLevel level, const char *szString)
+{
+ // first let the previous logger show it
+ if ( m_pOldLog != NULL && m_bPassMessages ) {
+ // @@@ why can't we access protected wxLog method from here (we derive
+ // from wxLog)? gcc gives "DoLog is protected in this context", what
+ // does this mean? Anyhow, the cast is harmless and let's us do what
+ // we want.
+ ((wxLogWindow *)m_pOldLog)->DoLog(level, szString);
+ }
+
+ // and this will format it nicely and call our DoLogString()
+ wxLog::DoLog(level, szString);
+
+ m_bHasMessages = TRUE;
+}
+
+void wxLogWindow::DoLogString(const char *szString)
+{
+ // put the text into our window
+ wxTextCtrl *pText = m_pLogFrame->TextCtrl();
+
+ // remove selection (WriteText is in fact ReplaceSelection)
+ #ifdef __WXMSW__
+ long nLen = pText->GetLastPosition();
+ pText->SetSelection(nLen, nLen);
+ #endif // Windows
+
+ pText->WriteText(szString);
+ pText->WriteText("\n"); // "\n" ok here (_not_ "\r\n")
+
+ // ensure that the line can be seen
+ // @@@ TODO
+}
+
+wxFrame *wxLogWindow::GetFrame() const
+{
+ return m_pLogFrame;
+}
+
+void wxLogWindow::OnFrameCreate(wxFrame *frame)
+{
+}
+
+void wxLogWindow::OnFrameDelete(wxFrame *frame)
+{
+ m_pLogFrame = NULL;
+}
+
+wxLogWindow::~wxLogWindow()
+{
+ // may be NULL if log frame already auto destroyed itself
+ delete m_pLogFrame;
+
+ // delete the old log
+ delete m_pOldLog;
+}
+