]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/logg.cpp
f03ba5cc39b6bf067e7796680029c9feb2788db2
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/logg.cpp
3 // Purpose: wxLog-derived classes which need GUI support (the rest is in
5 // Author: Vadim Zeitlin
7 // Created: 20.09.99 (extracted from src/common/log.cpp)
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
30 #include "wx/button.h"
35 #include "wx/filedlg.h"
36 #include "wx/msgdlg.h"
37 #include "wx/textctrl.h"
39 #include "wx/statbmp.h"
40 #include "wx/settings.h"
41 #include "wx/wxcrtvararg.h"
44 #if wxUSE_LOGGUI || wxUSE_LOGWINDOW
47 #include "wx/clipbrd.h"
48 #include "wx/dataobj.h"
49 #include "wx/textfile.h"
50 #include "wx/statline.h"
51 #include "wx/artprov.h"
52 #include "wx/collpane.h"
53 #include "wx/arrstr.h"
54 #include "wx/msgout.h"
57 // for OutputDebugString()
58 #include "wx/msw/private.h"
67 #include "wx/listctrl.h"
68 #include "wx/imaglist.h"
70 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
72 #if defined(__MWERKS__) && wxUSE_UNICODE
78 // the suffix we add to the button to show that the dialog can be expanded
79 #define EXPAND_SUFFIX wxT(" >>")
81 #define CAN_SAVE_FILES (wxUSE_FILE && wxUSE_FILEDLG)
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
89 // this function is a wrapper around strftime(3)
90 // allows to exclude the usage of wxDateTime
91 static wxString
TimeStamp(const wxString
& format
, time_t t
)
95 if ( !wxStrftime(buf
, WXSIZEOF(buf
), format
, wxLocaltime_r(&t
, &tm
)) )
97 // buffer is too small?
98 wxFAIL_MSG(wxT("strftime() failed"));
100 return wxString(buf
);
104 class wxLogDialog
: public wxDialog
107 wxLogDialog(wxWindow
*parent
,
108 const wxArrayString
& messages
,
109 const wxArrayInt
& severity
,
110 const wxArrayLong
& timess
,
111 const wxString
& caption
,
113 virtual ~wxLogDialog();
116 void OnOk(wxCommandEvent
& event
);
118 void OnCopy(wxCommandEvent
& event
);
119 #endif // wxUSE_CLIPBOARD
121 void OnSave(wxCommandEvent
& event
);
122 #endif // CAN_SAVE_FILES
123 void OnListItemActivated(wxListEvent
& event
);
126 // create controls needed for the details display
127 void CreateDetailsControls(wxWindow
*);
129 // if necessary truncates the given string and adds an ellipsis
130 wxString
EllipsizeString(const wxString
&text
)
132 if (ms_maxLength
> 0 &&
133 text
.length() > ms_maxLength
)
136 ret
.Truncate(ms_maxLength
);
144 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
145 // return the contents of the dialog as a multiline string
146 wxString
GetLogMessages() const;
147 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
150 // the data for the listctrl
151 wxArrayString m_messages
;
152 wxArrayInt m_severity
;
155 // the controls which are not shown initially (but only when details
156 // button is pressed)
157 wxListCtrl
*m_listctrl
;
159 // the translated "Details" string
160 static wxString ms_details
;
162 // the maximum length of the log message
163 static size_t ms_maxLength
;
165 DECLARE_EVENT_TABLE()
166 wxDECLARE_NO_COPY_CLASS(wxLogDialog
);
169 BEGIN_EVENT_TABLE(wxLogDialog
, wxDialog
)
170 EVT_BUTTON(wxID_OK
, wxLogDialog::OnOk
)
172 EVT_BUTTON(wxID_COPY
, wxLogDialog::OnCopy
)
173 #endif // wxUSE_CLIPBOARD
175 EVT_BUTTON(wxID_SAVE
, wxLogDialog::OnSave
)
176 #endif // CAN_SAVE_FILES
177 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxLogDialog::OnListItemActivated
)
180 #endif // wxUSE_LOG_DIALOG
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
188 // pass an uninitialized file object, the function will ask the user for the
189 // filename and try to open it, returns true on success (file was opened),
190 // false if file couldn't be opened/created and -1 if the file selection
191 // dialog was cancelled
192 static int OpenLogFile(wxFile
& file
, wxString
*filename
= NULL
, wxWindow
*parent
= NULL
);
194 #endif // CAN_SAVE_FILES
196 // ============================================================================
198 // ============================================================================
200 // ----------------------------------------------------------------------------
201 // wxLogGui implementation (FIXME MT-unsafe)
202 // ----------------------------------------------------------------------------
211 void wxLogGui::Clear()
215 m_bHasMessages
= false;
222 int wxLogGui::GetSeverityIcon() const
224 return m_bErrors
? wxICON_STOP
225 : m_bWarnings
? wxICON_EXCLAMATION
226 : wxICON_INFORMATION
;
229 wxString
wxLogGui::GetTitle() const
231 wxString titleFormat
;
232 switch ( GetSeverityIcon() )
235 titleFormat
= _("%s Error");
238 case wxICON_EXCLAMATION
:
239 titleFormat
= _("%s Warning");
243 wxFAIL_MSG( "unexpected icon severity" );
246 case wxICON_INFORMATION
:
247 titleFormat
= _("%s Information");
250 return wxString::Format(titleFormat
, wxTheApp
->GetAppDisplayName());
254 wxLogGui::DoShowSingleLogMessage(const wxString
& message
,
255 const wxString
& title
,
258 wxMessageBox(message
, title
, wxOK
| style
);
262 wxLogGui::DoShowMultipleLogMessages(const wxArrayString
& messages
,
263 const wxArrayInt
& severities
,
264 const wxArrayLong
& times
,
265 const wxString
& title
,
269 wxLogDialog
dlg(NULL
,
270 messages
, severities
, times
,
273 // clear the message list before showing the dialog because while it's
274 // shown some new messages may appear
277 (void)dlg
.ShowModal();
278 #else // !wxUSE_LOG_DIALOG
279 // start from the most recent message
281 const size_t nMsgCount
= messages
.size();
282 message
.reserve(nMsgCount
*100);
283 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
284 message
<< m_aMessages
[n
- 1] << wxT("\n");
287 DoShowSingleLogMessage(message
, title
, style
);
288 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
291 void wxLogGui::Flush()
295 if ( !m_bHasMessages
)
298 // do it right now to block any new calls to Flush() while we're here
299 m_bHasMessages
= false;
301 // note that this must be done before examining m_aMessages as it may log
302 // yet another message
303 const unsigned repeatCount
= LogLastRepeatIfNeeded();
305 const size_t nMsgCount
= m_aMessages
.size();
307 if ( repeatCount
> 0 )
309 m_aMessages
[nMsgCount
- 1] << " (" << m_aMessages
[nMsgCount
- 2] << ")";
312 const wxString title
= GetTitle();
313 const int style
= GetSeverityIcon();
315 // avoid showing other log dialogs until we're done with the dialog we're
316 // showing right now: nested modal dialogs make for really bad UI!
319 if ( nMsgCount
== 1 )
321 // make a copy before calling Clear()
322 const wxString
message(m_aMessages
[0]);
325 DoShowSingleLogMessage(message
, title
, style
);
327 else // more than one message
329 wxArrayString messages
;
330 wxArrayInt severities
;
333 messages
.swap(m_aMessages
);
334 severities
.swap(m_aSeverity
);
335 times
.swap(m_aTimes
);
339 DoShowMultipleLogMessages(messages
, severities
, times
, title
, style
);
342 // allow flushing the logs again
346 // log all kinds of messages
347 void wxLogGui::DoLogRecord(wxLogLevel level
,
349 const wxLogRecordInfo
& info
)
357 m_aMessages
.Add(msg
);
358 m_aSeverity
.Add(wxLOG_Message
);
359 m_aTimes
.Add((long)info
.timestamp
);
360 m_bHasMessages
= true;
367 wxFrame
*pFrame
= NULL
;
369 // check if the frame was passed to us explicitly
371 if ( info
.GetNumValue(wxLOG_KEY_FRAME
, &ptr
) )
373 pFrame
= static_cast<wxFrame
*>(wxUIntToPtr(ptr
));
376 // find the top window and set it's status text if it has any
377 if ( pFrame
== NULL
) {
378 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
379 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
380 pFrame
= (wxFrame
*)pWin
;
384 if ( pFrame
&& pFrame
->GetStatusBar() )
385 pFrame
->SetStatusText(msg
);
387 #endif // wxUSE_STATUSBAR
392 #if !wxUSE_LOG_DIALOG
393 // discard earlier informational messages if this is the 1st
394 // error because they might not make sense any more and showing
395 // them in a message box might be confusing
399 #endif // wxUSE_LOG_DIALOG
406 // for the warning we don't discard the info messages
410 m_aMessages
.Add(msg
);
411 m_aSeverity
.Add((int)level
);
412 m_aTimes
.Add((long)info
.timestamp
);
413 m_bHasMessages
= true;
418 // let the base class deal with debug/trace messages
419 wxLog::DoLogRecord(level
, msg
, info
);
422 case wxLOG_FatalError
:
424 // fatal errors are shown immediately and terminate the program so
425 // we should never see them here
426 wxFAIL_MSG("unexpected log level");
431 // just ignore those: passing them to the base class would result
432 // in asserts from DoLogText() because DoLogTextAtLevel() would
433 // call it as it doesn't know how to handle these levels otherwise
438 #endif // wxUSE_LOGGUI
440 // ----------------------------------------------------------------------------
441 // wxLogWindow and wxLogFrame implementation
442 // ----------------------------------------------------------------------------
448 class wxLogFrame
: public wxFrame
452 wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
);
453 virtual ~wxLogFrame();
456 void OnClose(wxCommandEvent
& event
);
457 void OnCloseWindow(wxCloseEvent
& event
);
459 void OnSave(wxCommandEvent
& event
);
460 #endif // CAN_SAVE_FILES
461 void OnClear(wxCommandEvent
& event
);
463 // do show the message in the text control
464 void ShowLogMessage(const wxString
& message
)
466 m_pTextCtrl
->AppendText(message
+ wxS('\n'));
470 // use standard ids for our commands!
473 Menu_Close
= wxID_CLOSE
,
474 Menu_Save
= wxID_SAVE
,
475 Menu_Clear
= wxID_CLEAR
478 // common part of OnClose() and OnCloseWindow()
481 wxTextCtrl
*m_pTextCtrl
;
484 DECLARE_EVENT_TABLE()
485 wxDECLARE_NO_COPY_CLASS(wxLogFrame
);
488 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
489 // wxLogWindow menu events
490 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
492 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
493 #endif // CAN_SAVE_FILES
494 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
496 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
499 wxLogFrame::wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
)
500 : wxFrame(pParent
, wxID_ANY
, szTitle
)
504 m_pTextCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
508 // needed for Win32 to avoid 65Kb limit but it doesn't work well
509 // when using RichEdit 2.0 which we always do in the Unicode build
512 #endif // !wxUSE_UNICODE
517 wxMenuBar
*pMenuBar
= new wxMenuBar
;
518 wxMenu
*pMenu
= new wxMenu
;
520 pMenu
->Append(Menu_Save
, _("Save &As..."), _("Save log contents to file"));
521 #endif // CAN_SAVE_FILES
522 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
523 pMenu
->AppendSeparator();
524 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
525 pMenuBar
->Append(pMenu
, _("&Log"));
526 SetMenuBar(pMenuBar
);
527 #endif // wxUSE_MENUS
530 // status bar for menu prompts
532 #endif // wxUSE_STATUSBAR
534 m_log
->OnFrameCreate(this);
537 void wxLogFrame::DoClose()
539 if ( m_log
->OnFrameClose(this) )
541 // instead of closing just hide the window to be able to Show() it
547 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
552 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
558 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
562 int rc
= OpenLogFile(file
, &filename
, this);
571 // retrieve text and save it
572 // -------------------------
573 int nLines
= m_pTextCtrl
->GetNumberOfLines();
574 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
575 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
576 wxTextFile::GetEOL());
583 wxLogError(_("Can't save log contents to file."));
586 wxLogStatus((wxFrame
*)this, _("Log saved to the file '%s'."), filename
.c_str());
589 #endif // CAN_SAVE_FILES
591 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
593 m_pTextCtrl
->Clear();
596 wxLogFrame::~wxLogFrame()
598 m_log
->OnFrameDelete(this);
604 wxLogWindow::wxLogWindow(wxWindow
*pParent
,
605 const wxString
& szTitle
,
609 // Initialize it to NULL to ensure that we don't crash if any log messages
610 // are generated before the frame is fully created (while this doesn't
611 // happen normally, it might, in principle).
614 PassMessages(bDoPass
);
616 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
622 void wxLogWindow::Show(bool bShow
)
624 m_pLogFrame
->Show(bShow
);
627 void wxLogWindow::DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
632 // don't put trace messages in the text window for 2 reasons:
633 // 1) there are too many of them
634 // 2) they may provoke other trace messages (e.g. wxMSW code uses
635 // wxLogTrace to log Windows messages and adding text to the control
636 // sends more of them) thus sending a program into an infinite loop
637 if ( level
== wxLOG_Trace
)
640 m_pLogFrame
->ShowLogMessage(msg
);
643 wxFrame
*wxLogWindow::GetFrame() const
648 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
652 bool wxLogWindow::OnFrameClose(wxFrame
* WXUNUSED(frame
))
658 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
663 wxLogWindow::~wxLogWindow()
665 // may be NULL if log frame already auto destroyed itself
669 #endif // wxUSE_LOGWINDOW
671 // ----------------------------------------------------------------------------
673 // ----------------------------------------------------------------------------
677 wxString
wxLogDialog::ms_details
;
678 size_t wxLogDialog::ms_maxLength
= 0;
680 wxLogDialog::wxLogDialog(wxWindow
*parent
,
681 const wxArrayString
& messages
,
682 const wxArrayInt
& severity
,
683 const wxArrayLong
& times
,
684 const wxString
& caption
,
686 : wxDialog(parent
, wxID_ANY
, caption
,
687 wxDefaultPosition
, wxDefaultSize
,
688 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
690 // init the static variables:
692 if ( ms_details
.empty() )
694 // ensure that we won't loop here if wxGetTranslation()
695 // happens to pop up a Log message while translating this :-)
696 ms_details
= wxTRANSLATE("&Details");
697 ms_details
= wxGetTranslation(ms_details
);
698 #ifdef __SMARTPHONE__
699 ms_details
= wxStripMenuCodes(ms_details
);
703 if ( ms_maxLength
== 0 )
705 ms_maxLength
= (2 * wxGetDisplaySize().x
/3) / GetCharWidth();
708 size_t count
= messages
.GetCount();
709 m_messages
.Alloc(count
);
710 m_severity
.Alloc(count
);
711 m_times
.Alloc(count
);
713 for ( size_t n
= 0; n
< count
; n
++ )
715 m_messages
.Add(messages
[n
]);
716 m_severity
.Add(severity
[n
]);
717 m_times
.Add(times
[n
]);
722 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
724 // create the controls which are always shown and layout them: we use
725 // sizers even though our window is not resizable to calculate the size of
726 // the dialog properly
727 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
728 wxBoxSizer
*sizerAll
= new wxBoxSizer(isPda
? wxVERTICAL
: wxHORIZONTAL
);
732 wxStaticBitmap
*icon
= new wxStaticBitmap
736 wxArtProvider::GetMessageBoxIcon(style
)
738 sizerAll
->Add(icon
, wxSizerFlags().Centre());
741 // create the text sizer with a minimal size so that we are sure it won't be too small
742 wxString message
= EllipsizeString(messages
.Last());
743 wxSizer
*szText
= CreateTextSizer(message
);
744 szText
->SetMinSize(wxMin(300, wxGetDisplaySize().x
/ 3), -1);
746 sizerAll
->Add(szText
, wxSizerFlags(1).Centre().Border(wxLEFT
| wxRIGHT
));
748 wxButton
*btnOk
= new wxButton(this, wxID_OK
);
749 sizerAll
->Add(btnOk
, wxSizerFlags().Centre());
751 sizerTop
->Add(sizerAll
, wxSizerFlags().Expand().Border());
754 // add the details pane
755 #ifndef __SMARTPHONE__
756 wxCollapsiblePane
* const
757 collpane
= new wxCollapsiblePane(this, wxID_ANY
, ms_details
);
758 sizerTop
->Add(collpane
, wxSizerFlags(1).Expand().Border());
760 wxWindow
*win
= collpane
->GetPane();
761 wxSizer
* const paneSz
= new wxBoxSizer(wxVERTICAL
);
763 CreateDetailsControls(win
);
765 paneSz
->Add(m_listctrl
, wxSizerFlags(1).Expand().Border(wxTOP
));
767 #if wxUSE_CLIPBOARD || CAN_SAVE_FILES
768 wxBoxSizer
* const btnSizer
= new wxBoxSizer(wxHORIZONTAL
);
770 wxSizerFlags flagsBtn
;
771 flagsBtn
.Border(wxLEFT
);
774 btnSizer
->Add(new wxButton(win
, wxID_COPY
), flagsBtn
);
775 #endif // wxUSE_CLIPBOARD
778 btnSizer
->Add(new wxButton(win
, wxID_SAVE
), flagsBtn
);
779 #endif // CAN_SAVE_FILES
781 paneSz
->Add(btnSizer
, wxSizerFlags().Right().Border(wxTOP
));
782 #endif // wxUSE_CLIPBOARD || CAN_SAVE_FILES
784 win
->SetSizer(paneSz
);
785 paneSz
->SetSizeHints(win
);
786 #else // __SMARTPHONE__
787 SetLeftMenu(wxID_OK
);
788 SetRightMenu(wxID_MORE
, ms_details
+ EXPAND_SUFFIX
);
789 #endif // __SMARTPHONE__/!__SMARTPHONE__
791 SetSizerAndFit(sizerTop
);
797 // Move up the screen so that when we expand the dialog,
798 // there's enough space.
799 Move(wxPoint(GetPosition().x
, GetPosition().y
/ 2));
803 void wxLogDialog::CreateDetailsControls(wxWindow
*parent
)
805 wxString fmt
= wxLog::GetTimestamp();
806 bool hasTimeStamp
= !fmt
.IsEmpty();
808 // create the list ctrl now
809 m_listctrl
= new wxListCtrl(parent
, wxID_ANY
,
810 wxDefaultPosition
, wxDefaultSize
,
816 // This makes a big aesthetic difference on WinCE but I
817 // don't want to risk problems on other platforms
821 // no need to translate these strings as they're not shown to the
822 // user anyhow (we use wxLC_NO_HEADER style)
823 m_listctrl
->InsertColumn(0, wxT("Message"));
826 m_listctrl
->InsertColumn(1, wxT("Time"));
828 // prepare the imagelist
829 static const int ICON_SIZE
= 16;
830 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
832 // order should be the same as in the switch below!
833 static const char* const icons
[] =
840 bool loadedIcons
= true;
842 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
844 wxBitmap bmp
= wxArtProvider::GetBitmap(icons
[icon
], wxART_MESSAGE_BOX
,
845 wxSize(ICON_SIZE
, ICON_SIZE
));
847 // This may very well fail if there are insufficient colours available.
848 // Degrade gracefully.
859 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
862 size_t count
= m_messages
.GetCount();
863 for ( size_t n
= 0; n
< count
; n
++ )
869 switch ( m_severity
[n
] )
883 else // failed to load images
888 wxString msg
= m_messages
[n
];
889 msg
.Replace(wxT("\n"), wxT(" "));
890 msg
= EllipsizeString(msg
);
892 m_listctrl
->InsertItem(n
, msg
, image
);
895 m_listctrl
->SetItem(n
, 1, TimeStamp(fmt
, (time_t)m_times
[n
]));
898 // let the columns size themselves
899 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
901 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
903 // calculate an approximately nice height for the listctrl
904 int height
= GetCharHeight()*(count
+ 4);
906 // but check that the dialog won't fall fown from the screen
908 // we use GetMinHeight() to get the height of the dialog part without the
909 // details and we consider that the "Save" button below and the separator
910 // line (and the margins around it) take about as much, hence double it
911 int heightMax
= wxGetDisplaySize().y
- GetPosition().y
- 2*GetMinHeight();
913 // we should leave a margin
917 m_listctrl
->SetSize(wxDefaultCoord
, wxMin(height
, heightMax
));
920 void wxLogDialog::OnListItemActivated(wxListEvent
& event
)
922 // show the activated item in a message box
923 // This allow the user to correctly display the logs which are longer
924 // than the listctrl and thus gets truncated or those which contains
928 // wxString str = m_listctrl->GetItemText(event.GetIndex());
929 // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW.
930 wxString str
= m_messages
[event
.GetIndex()];
932 // wxMessageBox will nicely handle the '\n' in the string (if any)
933 // and supports long strings
934 wxMessageBox(str
, wxT("Log message"), wxOK
, this);
937 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
942 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
944 wxString
wxLogDialog::GetLogMessages() const
946 wxString fmt
= wxLog::GetTimestamp();
949 // use the default format
953 const size_t count
= m_messages
.GetCount();
956 text
.reserve(count
*m_messages
[0].length());
957 for ( size_t n
= 0; n
< count
; n
++ )
959 text
<< TimeStamp(fmt
, (time_t)m_times
[n
])
962 << wxTextFile::GetEOL();
968 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
972 void wxLogDialog::OnCopy(wxCommandEvent
& WXUNUSED(event
))
974 wxClipboardLocker clip
;
976 !wxTheClipboard
->AddData(new wxTextDataObject(GetLogMessages())) )
978 wxLogError(_("Failed to copy dialog contents to the clipboard."));
982 #endif // wxUSE_CLIPBOARD
986 void wxLogDialog::OnSave(wxCommandEvent
& WXUNUSED(event
))
989 int rc
= OpenLogFile(file
, NULL
, this);
996 if ( !rc
|| !file
.Write(GetLogMessages()) || !file
.Close() )
998 wxLogError(_("Can't save log contents to file."));
1002 #endif // CAN_SAVE_FILES
1004 wxLogDialog::~wxLogDialog()
1008 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
1012 #endif // wxUSE_LOG_DIALOG
1016 // pass an uninitialized file object, the function will ask the user for the
1017 // filename and try to open it, returns true on success (file was opened),
1018 // false if file couldn't be opened/created and -1 if the file selection
1019 // dialog was cancelled
1020 static int OpenLogFile(wxFile
& file
, wxString
*pFilename
, wxWindow
*parent
)
1022 // get the file name
1023 // -----------------
1024 wxString filename
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent
);
1032 bool bOk
= true; // suppress warning about it being possible uninitialized
1033 if ( wxFile::Exists(filename
) ) {
1034 bool bAppend
= false;
1036 strMsg
.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
1038 switch ( wxMessageBox(strMsg
, _("Question"),
1039 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
) ) {
1052 wxFAIL_MSG(_("invalid message box return value"));
1056 bOk
= file
.Open(filename
, wxFile::write_append
);
1059 bOk
= file
.Create(filename
, true /* overwrite */);
1063 bOk
= file
.Create(filename
);
1067 *pFilename
= filename
;
1072 #endif // CAN_SAVE_FILES
1074 #endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
1076 #if wxUSE_LOG && wxUSE_TEXTCTRL
1078 // ----------------------------------------------------------------------------
1079 // wxLogTextCtrl implementation
1080 // ----------------------------------------------------------------------------
1082 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
1084 m_pTextCtrl
= pTextCtrl
;
1087 void wxLogTextCtrl::DoLogText(const wxString
& msg
)
1089 m_pTextCtrl
->AppendText(msg
+ wxS('\n'));
1092 #endif // wxUSE_LOG && wxUSE_TEXTCTRL