+#endif // wxUSE_LOGWINDOW
+
+// ----------------------------------------------------------------------------
+// wxLogDialog
+// ----------------------------------------------------------------------------
+
+#if wxUSE_LOG_DIALOG
+
+#ifndef __SMARTPHONE__
+static const size_t MARGIN = 10;
+#else
+static const size_t MARGIN = 0;
+#endif
+
+wxString wxLogDialog::ms_details;
+size_t wxLogDialog::ms_maxLength = 0;
+
+wxLogDialog::wxLogDialog(wxWindow *parent,
+ const wxArrayString& messages,
+ const wxArrayInt& severity,
+ const wxArrayLong& times,
+ const wxString& caption,
+ long style)
+ : wxDialog(parent, wxID_ANY, caption,
+ wxDefaultPosition, wxDefaultSize,
+ wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
+{
+ // init the static variables:
+
+ if ( ms_details.empty() )
+ {
+ // ensure that we won't loop here if wxGetTranslation()
+ // happens to pop up a Log message while translating this :-)
+ ms_details = wxTRANSLATE("&Details");
+ ms_details = wxGetTranslation(ms_details);
+#ifdef __SMARTPHONE__
+ ms_details = wxStripMenuCodes(ms_details);
+#endif
+ }
+
+ if ( ms_maxLength == 0 )
+ {
+ ms_maxLength = (2 * wxGetDisplaySize().x/3) / GetCharWidth();
+ }
+
+ size_t count = messages.GetCount();
+ m_messages.Alloc(count);
+ m_severity.Alloc(count);
+ m_times.Alloc(count);
+
+ for ( size_t n = 0; n < count; n++ )
+ {
+ m_messages.Add(messages[n]);
+ m_severity.Add(severity[n]);
+ m_times.Add(times[n]);
+ }
+
+ m_listctrl = (wxListCtrl *)NULL;
+
+#ifndef __SMARTPHONE__
+
+#if wxUSE_STATLINE
+ m_statline = (wxStaticLine *)NULL;
+#endif // wxUSE_STATLINE
+
+#if wxUSE_FILE
+ m_btnSave = (wxButton *)NULL;
+#endif // wxUSE_FILE
+
+#endif // __SMARTPHONE__
+
+ bool isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
+
+ // create the controls which are always shown and layout them: we use
+ // sizers even though our window is not resizeable to calculate the size of
+ // the dialog properly
+ wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
+ wxBoxSizer *sizerAll = new wxBoxSizer(isPda ? wxVERTICAL : wxHORIZONTAL);
+
+ if (!isPda)
+ {
+ wxStaticBitmap *icon = new wxStaticBitmap
+ (
+ this,
+ wxID_ANY,
+ wxArtProvider::GetMessageBoxIcon(style)
+ );
+ sizerAll->Add(icon, 0, wxALIGN_CENTRE_VERTICAL);
+ }
+
+ // create the text sizer with a minimal size so that we are sure it won't be too small
+ wxString message = EllipsizeString(messages.Last());
+ wxSizer *szText = CreateTextSizer(message);
+ szText->SetMinSize(wxMin(300, wxGetDisplaySize().x / 3), -1);
+
+ sizerAll->Add(szText, 1,
+ wxALIGN_CENTRE_VERTICAL | wxLEFT | wxRIGHT, MARGIN);
+
+ wxButton *btnOk = new wxButton(this, wxID_OK);
+ sizerAll->Add(btnOk, 0, isPda ? wxCENTRE : wxCENTRE|wxBOTTOM, MARGIN/2);
+
+ sizerTop->Add(sizerAll, 0, wxALL | wxEXPAND, MARGIN);
+
+
+ // add the details pane
+
+#ifndef __SMARTPHONE__
+ wxCollapsiblePane *collpane = new wxCollapsiblePane(this, wxID_ANY, ms_details);
+ sizerTop->Add(collpane, 1, wxGROW|wxALL, MARGIN);
+
+ wxWindow *win = collpane->GetPane();
+ wxSizer *paneSz = new wxBoxSizer(wxVERTICAL);
+
+ CreateDetailsControls(win);
+
+ paneSz->Add(m_listctrl, 1, wxEXPAND | wxTOP, MARGIN);
+
+#if wxUSE_FILE && !defined(__SMARTPHONE__)
+ paneSz->Add(m_btnSave, 0, wxALIGN_RIGHT | wxTOP, MARGIN);
+#endif // wxUSE_FILE
+
+ win->SetSizer(paneSz);
+ paneSz->SetSizeHints(win);
+#else // __SMARTPHONE__
+ SetLeftMenu(wxID_OK);
+ SetRightMenu(wxID_MORE, ms_details + EXPAND_SUFFIX);
+#endif // __SMARTPHONE__/!__SMARTPHONE__
+
+ SetSizerAndFit(sizerTop);
+
+ Centre();
+
+ if (isPda)
+ {
+ // Move up the screen so that when we expand the dialog,
+ // there's enough space.
+ Move(wxPoint(GetPosition().x, GetPosition().y / 2));
+ }
+}
+
+void wxLogDialog::CreateDetailsControls(wxWindow *parent)
+{
+#ifndef __SMARTPHONE__
+ // create the save button and separator line if possible
+#if wxUSE_FILE
+ m_btnSave = new wxButton(parent, wxID_SAVE);
+#endif // wxUSE_FILE
+
+#endif // __SMARTPHONE__
+
+ // create the list ctrl now
+ m_listctrl = new wxListCtrl(parent, wxID_ANY,
+ wxDefaultPosition, wxDefaultSize,
+ wxSUNKEN_BORDER |
+ wxLC_REPORT |
+ wxLC_NO_HEADER |
+ wxLC_SINGLE_SEL);
+#ifdef __WXWINCE__
+ // This makes a big aesthetic difference on WinCE but I
+ // don't want to risk problems on other platforms
+ m_listctrl->Hide();
+#endif
+
+ // no need to translate these strings as they're not shown to the
+ // user anyhow (we use wxLC_NO_HEADER style)
+ m_listctrl->InsertColumn(0, _T("Message"));
+ m_listctrl->InsertColumn(1, _T("Time"));
+
+ // prepare the imagelist
+ static const int ICON_SIZE = 16;
+ wxImageList *imageList = new wxImageList(ICON_SIZE, ICON_SIZE);
+
+ // order should be the same as in the switch below!
+ static const wxChar* icons[] =
+ {
+ wxART_ERROR,
+ wxART_WARNING,
+ wxART_INFORMATION
+ };
+
+ bool loadedIcons = true;
+
+ for ( size_t icon = 0; icon < WXSIZEOF(icons); icon++ )
+ {
+ wxBitmap bmp = wxArtProvider::GetBitmap(icons[icon], wxART_MESSAGE_BOX,
+ wxSize(ICON_SIZE, ICON_SIZE));
+
+ // This may very well fail if there are insufficient colours available.
+ // Degrade gracefully.
+ if ( !bmp.Ok() )
+ {
+ loadedIcons = false;
+
+ break;
+ }
+
+ imageList->Add(bmp);
+ }
+
+ m_listctrl->SetImageList(imageList, wxIMAGE_LIST_SMALL);
+
+ // and fill it
+ wxString fmt = wxLog::GetTimestamp();
+ if ( !fmt )
+ {
+ // default format
+ fmt = _T("%c");
+ }
+
+ size_t count = m_messages.GetCount();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ int image;
+
+ if ( loadedIcons )
+ {
+ switch ( m_severity[n] )
+ {
+ case wxLOG_Error:
+ image = 0;
+ break;
+
+ case wxLOG_Warning:
+ image = 1;
+ break;
+
+ default:
+ image = 2;
+ }
+ }
+ else // failed to load images
+ {
+ image = -1;
+ }
+
+ wxString msg = m_messages[n];
+ msg.Replace(wxT("\n"), wxT(" "));
+ msg = EllipsizeString(msg);
+
+ m_listctrl->InsertItem(n, msg, image);
+ m_listctrl->SetItem(n, 1, TimeStamp(fmt, (time_t)m_times[n]));
+ }
+
+ // let the columns size themselves
+ m_listctrl->SetColumnWidth(0, wxLIST_AUTOSIZE);
+ m_listctrl->SetColumnWidth(1, wxLIST_AUTOSIZE);
+
+ // calculate an approximately nice height for the listctrl
+ int height = GetCharHeight()*(count + 4);
+
+ // but check that the dialog won't fall fown from the screen
+ //
+ // we use GetMinHeight() to get the height of the dialog part without the
+ // details and we consider that the "Save" button below and the separator
+ // line (and the margins around it) take about as much, hence double it
+ int heightMax = wxGetDisplaySize().y - GetPosition().y - 2*GetMinHeight();
+
+ // we should leave a margin
+ heightMax *= 9;
+ heightMax /= 10;
+
+ m_listctrl->SetSize(wxDefaultCoord, wxMin(height, heightMax));
+}
+
+void wxLogDialog::OnListSelect(wxListEvent& event)
+{
+ // we can't just disable the control because this looks ugly under Windows
+ // (wrong bg colour, no scrolling...), but we still want to disable
+ // selecting items - it makes no sense here
+ m_listctrl->SetItemState(event.GetIndex(), 0, wxLIST_STATE_SELECTED);
+}
+
+void wxLogDialog::OnListItemActivated(wxListEvent& event)
+{
+ // show the activated item in a message box
+ // This allow the user to correctly display the logs which are longer
+ // than the listctrl and thus gets truncated or those which contains
+ // newlines.
+
+ // NB: don't do:
+ // wxString str = m_listctrl->GetItemText(event.GetIndex());
+ // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW.
+ wxString str = m_messages[event.GetIndex()];
+
+ // wxMessageBox will nicely handle the '\n' in the string (if any)
+ // and supports long strings
+ wxMessageBox(str, wxT("Log message"), wxOK, this);
+}
+
+void wxLogDialog::OnOk(wxCommandEvent& WXUNUSED(event))
+{
+ EndModal(wxID_OK);
+}
+
+#if wxUSE_FILE
+
+void wxLogDialog::OnSave(wxCommandEvent& WXUNUSED(event))
+{
+#if wxUSE_FILEDLG
+ wxFile file;
+ int rc = OpenLogFile(file, NULL, this);
+ if ( rc == -1 )
+ {
+ // cancelled
+ return;
+ }
+
+ bool ok = rc != 0;
+
+ wxString fmt = wxLog::GetTimestamp();
+ if ( !fmt )
+ {
+ // default format
+ fmt = _T("%c");
+ }
+
+ size_t count = m_messages.GetCount();
+ for ( size_t n = 0; ok && (n < count); n++ )
+ {
+ wxString line;
+ line << TimeStamp(fmt, (time_t)m_times[n])
+ << _T(": ")
+ << m_messages[n]
+ << wxTextFile::GetEOL();
+
+ ok = file.Write(line);
+ }
+
+ if ( ok )
+ ok = file.Close();
+
+ if ( !ok )
+ wxLogError(_("Can't save log contents to file."));
+#endif // wxUSE_FILEDLG
+}
+
+#endif // wxUSE_FILE
+
+wxLogDialog::~wxLogDialog()
+{
+ if ( m_listctrl )
+ {
+ delete m_listctrl->GetImageList(wxIMAGE_LIST_SMALL);
+ }
+}
+
+#endif // wxUSE_LOG_DIALOG
+
+#if wxUSE_FILE && wxUSE_FILEDLG
+
+// pass an uninitialized file object, the function will ask the user for the
+// filename and try to open it, returns true on success (file was opened),
+// false if file couldn't be opened/created and -1 if the file selection
+// dialog was cancelled
+static int OpenLogFile(wxFile& file, wxString *pFilename, wxWindow *parent)
+{
+ // get the file name
+ // -----------------
+ wxString filename = wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent);
+ if ( !filename ) {
+ // cancelled
+ return -1;
+ }
+
+ // open file
+ // ---------
+ bool bOk wxDUMMY_INITIALIZE(false);
+ if ( wxFile::Exists(filename) ) {
+ bool bAppend = false;
+ wxString strMsg;
+ strMsg.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
+ filename.c_str());
+ switch ( wxMessageBox(strMsg, _("Question"),
+ wxICON_QUESTION | wxYES_NO | wxCANCEL) ) {
+ case wxYES:
+ bAppend = true;
+ break;
+
+ case wxNO:
+ bAppend = false;
+ break;
+
+ case wxCANCEL:
+ return -1;
+
+ default:
+ wxFAIL_MSG(_("invalid message box return value"));
+ }
+
+ if ( bAppend ) {
+ bOk = file.Open(filename, wxFile::write_append);
+ }
+ else {
+ bOk = file.Create(filename, true /* overwrite */);
+ }
+ }
+ else {
+ bOk = file.Create(filename);
+ }
+
+ if ( pFilename )
+ *pFilename = filename;
+
+ return bOk;
+}
+
+#endif // wxUSE_FILE
+
+#endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
+
+#if wxUSE_LOG && wxUSE_TEXTCTRL
+
+// ----------------------------------------------------------------------------
+// wxLogTextCtrl implementation
+// ----------------------------------------------------------------------------
+
+wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl)
+{
+ m_pTextCtrl = pTextCtrl;
+}
+
+void wxLogTextCtrl::DoLogString(const wxString& szString, time_t WXUNUSED(t))
+{
+ wxString msg;
+ TimeStamp(&msg);
+
+ msg << szString << wxT('\n');
+ m_pTextCtrl->AppendText(msg);
+}
+
+#endif // wxUSE_LOG && wxUSE_TEXTCTRL