]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/logg.cpp
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
74 // the suffix we add to the button to show that the dialog can be expanded
75 #define EXPAND_SUFFIX wxT(" >>")
77 #define CAN_SAVE_FILES (wxUSE_FILE && wxUSE_FILEDLG)
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
85 // this function is a wrapper around strftime(3)
86 // allows to exclude the usage of wxDateTime
87 static wxString
TimeStamp(const wxString
& format
, time_t t
)
91 if ( !wxStrftime(buf
, WXSIZEOF(buf
), format
, wxLocaltime_r(&t
, &tm
)) )
93 // buffer is too small?
94 wxFAIL_MSG(wxT("strftime() failed"));
100 class wxLogDialog
: public wxDialog
103 wxLogDialog(wxWindow
*parent
,
104 const wxArrayString
& messages
,
105 const wxArrayInt
& severity
,
106 const wxArrayLong
& timess
,
107 const wxString
& caption
,
109 virtual ~wxLogDialog();
112 void OnOk(wxCommandEvent
& event
);
114 void OnCopy(wxCommandEvent
& event
);
115 #endif // wxUSE_CLIPBOARD
117 void OnSave(wxCommandEvent
& event
);
118 #endif // CAN_SAVE_FILES
119 void OnListItemActivated(wxListEvent
& event
);
122 // create controls needed for the details display
123 void CreateDetailsControls(wxWindow
*);
125 // if necessary truncates the given string and adds an ellipsis
126 wxString
EllipsizeString(const wxString
&text
)
128 if (ms_maxLength
> 0 &&
129 text
.length() > ms_maxLength
)
132 ret
.Truncate(ms_maxLength
);
140 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
141 // return the contents of the dialog as a multiline string
142 wxString
GetLogMessages() const;
143 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
146 // the data for the listctrl
147 wxArrayString m_messages
;
148 wxArrayInt m_severity
;
151 // the controls which are not shown initially (but only when details
152 // button is pressed)
153 wxListCtrl
*m_listctrl
;
155 // the translated "Details" string
156 static wxString ms_details
;
158 // the maximum length of the log message
159 static size_t ms_maxLength
;
161 DECLARE_EVENT_TABLE()
162 wxDECLARE_NO_COPY_CLASS(wxLogDialog
);
165 BEGIN_EVENT_TABLE(wxLogDialog
, wxDialog
)
166 EVT_BUTTON(wxID_OK
, wxLogDialog::OnOk
)
168 EVT_BUTTON(wxID_COPY
, wxLogDialog::OnCopy
)
169 #endif // wxUSE_CLIPBOARD
171 EVT_BUTTON(wxID_SAVE
, wxLogDialog::OnSave
)
172 #endif // CAN_SAVE_FILES
173 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxLogDialog::OnListItemActivated
)
176 #endif // wxUSE_LOG_DIALOG
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
184 // pass an uninitialized file object, the function will ask the user for the
185 // filename and try to open it, returns true on success (file was opened),
186 // false if file couldn't be opened/created and -1 if the file selection
187 // dialog was cancelled
188 static int OpenLogFile(wxFile
& file
, wxString
*filename
= NULL
, wxWindow
*parent
= NULL
);
190 #endif // CAN_SAVE_FILES
192 // ============================================================================
194 // ============================================================================
196 // ----------------------------------------------------------------------------
197 // wxLogGui implementation (FIXME MT-unsafe)
198 // ----------------------------------------------------------------------------
207 void wxLogGui::Clear()
211 m_bHasMessages
= false;
218 int wxLogGui::GetSeverityIcon() const
220 return m_bErrors
? wxICON_STOP
221 : m_bWarnings
? wxICON_EXCLAMATION
222 : wxICON_INFORMATION
;
225 wxString
wxLogGui::GetTitle() const
227 wxString titleFormat
;
228 switch ( GetSeverityIcon() )
231 titleFormat
= _("%s Error");
234 case wxICON_EXCLAMATION
:
235 titleFormat
= _("%s Warning");
239 wxFAIL_MSG( "unexpected icon severity" );
242 case wxICON_INFORMATION
:
243 titleFormat
= _("%s Information");
246 return wxString::Format(titleFormat
, wxTheApp
->GetAppDisplayName());
250 wxLogGui::DoShowSingleLogMessage(const wxString
& message
,
251 const wxString
& title
,
254 wxMessageBox(message
, title
, wxOK
| style
);
258 wxLogGui::DoShowMultipleLogMessages(const wxArrayString
& messages
,
259 const wxArrayInt
& severities
,
260 const wxArrayLong
& times
,
261 const wxString
& title
,
265 wxLogDialog
dlg(NULL
,
266 messages
, severities
, times
,
269 // clear the message list before showing the dialog because while it's
270 // shown some new messages may appear
273 (void)dlg
.ShowModal();
274 #else // !wxUSE_LOG_DIALOG
275 // start from the most recent message
277 const size_t nMsgCount
= messages
.size();
278 message
.reserve(nMsgCount
*100);
279 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
280 message
<< m_aMessages
[n
- 1] << wxT("\n");
283 DoShowSingleLogMessage(message
, title
, style
);
284 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
287 void wxLogGui::Flush()
291 if ( !m_bHasMessages
)
294 // do it right now to block any new calls to Flush() while we're here
295 m_bHasMessages
= false;
297 // note that this must be done before examining m_aMessages as it may log
298 // yet another message
299 const unsigned repeatCount
= LogLastRepeatIfNeeded();
301 const size_t nMsgCount
= m_aMessages
.size();
303 if ( repeatCount
> 0 )
305 m_aMessages
[nMsgCount
- 1] << " (" << m_aMessages
[nMsgCount
- 2] << ")";
308 const wxString title
= GetTitle();
309 const int style
= GetSeverityIcon();
311 // avoid showing other log dialogs until we're done with the dialog we're
312 // showing right now: nested modal dialogs make for really bad UI!
315 if ( nMsgCount
== 1 )
317 // make a copy before calling Clear()
318 const wxString
message(m_aMessages
[0]);
321 DoShowSingleLogMessage(message
, title
, style
);
323 else // more than one message
325 wxArrayString messages
;
326 wxArrayInt severities
;
329 messages
.swap(m_aMessages
);
330 severities
.swap(m_aSeverity
);
331 times
.swap(m_aTimes
);
335 DoShowMultipleLogMessages(messages
, severities
, times
, title
, style
);
338 // allow flushing the logs again
342 // log all kinds of messages
343 void wxLogGui::DoLogRecord(wxLogLevel level
,
345 const wxLogRecordInfo
& info
)
353 m_aMessages
.Add(msg
);
354 m_aSeverity
.Add(wxLOG_Message
);
355 m_aTimes
.Add((long)info
.timestamp
);
356 m_bHasMessages
= true;
363 wxFrame
*pFrame
= NULL
;
365 // check if the frame was passed to us explicitly
367 if ( info
.GetNumValue(wxLOG_KEY_FRAME
, &ptr
) )
369 pFrame
= static_cast<wxFrame
*>(wxUIntToPtr(ptr
));
372 // find the top window and set it's status text if it has any
373 if ( pFrame
== NULL
) {
374 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
375 if ( wxDynamicCast(pWin
, wxFrame
) ) {
376 pFrame
= (wxFrame
*)pWin
;
380 if ( pFrame
&& pFrame
->GetStatusBar() )
381 pFrame
->SetStatusText(msg
);
383 #endif // wxUSE_STATUSBAR
388 #if !wxUSE_LOG_DIALOG
389 // discard earlier informational messages if this is the 1st
390 // error because they might not make sense any more and showing
391 // them in a message box might be confusing
395 #endif // wxUSE_LOG_DIALOG
402 // for the warning we don't discard the info messages
406 m_aMessages
.Add(msg
);
407 m_aSeverity
.Add((int)level
);
408 m_aTimes
.Add((long)info
.timestamp
);
409 m_bHasMessages
= true;
414 // let the base class deal with debug/trace messages
415 wxLog::DoLogRecord(level
, msg
, info
);
418 case wxLOG_FatalError
:
420 // fatal errors are shown immediately and terminate the program so
421 // we should never see them here
422 wxFAIL_MSG("unexpected log level");
427 // just ignore those: passing them to the base class would result
428 // in asserts from DoLogText() because DoLogTextAtLevel() would
429 // call it as it doesn't know how to handle these levels otherwise
434 #endif // wxUSE_LOGGUI
436 // ----------------------------------------------------------------------------
437 // wxLogWindow and wxLogFrame implementation
438 // ----------------------------------------------------------------------------
444 class wxLogFrame
: public wxFrame
448 wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
);
449 virtual ~wxLogFrame();
451 // Don't prevent the application from exiting if just this frame remains.
452 virtual bool ShouldPreventAppExit() const { return false; }
455 void OnClose(wxCommandEvent
& event
);
456 void OnCloseWindow(wxCloseEvent
& event
);
458 void OnSave(wxCommandEvent
& event
);
459 #endif // CAN_SAVE_FILES
460 void OnClear(wxCommandEvent
& event
);
462 // do show the message in the text control
463 void ShowLogMessage(const wxString
& message
)
465 m_pTextCtrl
->AppendText(message
+ wxS('\n'));
469 // use standard ids for our commands!
472 Menu_Close
= wxID_CLOSE
,
473 Menu_Save
= wxID_SAVE
,
474 Menu_Clear
= wxID_CLEAR
477 // common part of OnClose() and OnCloseWindow()
480 wxTextCtrl
*m_pTextCtrl
;
483 DECLARE_EVENT_TABLE()
484 wxDECLARE_NO_COPY_CLASS(wxLogFrame
);
487 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
488 // wxLogWindow menu events
489 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
491 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
492 #endif // CAN_SAVE_FILES
493 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
495 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
498 wxLogFrame::wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
)
499 : wxFrame(pParent
, wxID_ANY
, szTitle
)
503 m_pTextCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
507 // needed for Win32 to avoid 65Kb limit but it doesn't work well
508 // when using RichEdit 2.0 which we always do in the Unicode build
511 #endif // !wxUSE_UNICODE
516 wxMenuBar
*pMenuBar
= new wxMenuBar
;
517 wxMenu
*pMenu
= new wxMenu
;
519 pMenu
->Append(Menu_Save
, _("Save &As..."), _("Save log contents to file"));
520 #endif // CAN_SAVE_FILES
521 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
522 pMenu
->AppendSeparator();
523 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
524 pMenuBar
->Append(pMenu
, _("&Log"));
525 SetMenuBar(pMenuBar
);
526 #endif // wxUSE_MENUS
529 // status bar for menu prompts
531 #endif // wxUSE_STATUSBAR
533 m_log
->OnFrameCreate(this);
536 void wxLogFrame::DoClose()
538 if ( m_log
->OnFrameClose(this) )
540 // instead of closing just hide the window to be able to Show() it
546 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
551 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
557 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
561 int rc
= OpenLogFile(file
, &filename
, this);
570 // retrieve text and save it
571 // -------------------------
572 int nLines
= m_pTextCtrl
->GetNumberOfLines();
573 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
574 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
575 wxTextFile::GetEOL());
582 wxLogError(_("Can't save log contents to file."));
585 wxLogStatus((wxFrame
*)this, _("Log saved to the file '%s'."), filename
.c_str());
588 #endif // CAN_SAVE_FILES
590 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
592 m_pTextCtrl
->Clear();
595 wxLogFrame::~wxLogFrame()
597 m_log
->OnFrameDelete(this);
603 wxLogWindow::wxLogWindow(wxWindow
*pParent
,
604 const wxString
& szTitle
,
608 // Initialize it to NULL to ensure that we don't crash if any log messages
609 // are generated before the frame is fully created (while this doesn't
610 // happen normally, it might, in principle).
613 PassMessages(bDoPass
);
615 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
621 void wxLogWindow::Show(bool bShow
)
623 m_pLogFrame
->Show(bShow
);
626 void wxLogWindow::DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
631 // don't put trace messages in the text window for 2 reasons:
632 // 1) there are too many of them
633 // 2) they may provoke other trace messages (e.g. wxMSW code uses
634 // wxLogTrace to log Windows messages and adding text to the control
635 // sends more of them) thus sending a program into an infinite loop
636 if ( level
== wxLOG_Trace
)
639 m_pLogFrame
->ShowLogMessage(msg
);
642 wxFrame
*wxLogWindow::GetFrame() const
647 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
651 bool wxLogWindow::OnFrameClose(wxFrame
* WXUNUSED(frame
))
657 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
662 wxLogWindow::~wxLogWindow()
664 // may be NULL if log frame already auto destroyed itself
668 #endif // wxUSE_LOGWINDOW
670 // ----------------------------------------------------------------------------
672 // ----------------------------------------------------------------------------
676 wxString
wxLogDialog::ms_details
;
677 size_t wxLogDialog::ms_maxLength
= 0;
679 wxLogDialog::wxLogDialog(wxWindow
*parent
,
680 const wxArrayString
& messages
,
681 const wxArrayInt
& severity
,
682 const wxArrayLong
& times
,
683 const wxString
& caption
,
685 : wxDialog(parent
, wxID_ANY
, caption
,
686 wxDefaultPosition
, wxDefaultSize
,
687 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
689 // init the static variables:
691 if ( ms_details
.empty() )
693 // ensure that we won't loop here if wxGetTranslation()
694 // happens to pop up a Log message while translating this :-)
695 ms_details
= wxTRANSLATE("&Details");
696 ms_details
= wxGetTranslation(ms_details
);
697 #ifdef __SMARTPHONE__
698 ms_details
= wxStripMenuCodes(ms_details
);
702 if ( ms_maxLength
== 0 )
704 ms_maxLength
= (2 * wxGetDisplaySize().x
/3) / GetCharWidth();
707 size_t count
= messages
.GetCount();
708 m_messages
.Alloc(count
);
709 m_severity
.Alloc(count
);
710 m_times
.Alloc(count
);
712 for ( size_t n
= 0; n
< count
; n
++ )
714 m_messages
.Add(messages
[n
]);
715 m_severity
.Add(severity
[n
]);
716 m_times
.Add(times
[n
]);
721 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
723 // create the controls which are always shown and layout them: we use
724 // sizers even though our window is not resizable to calculate the size of
725 // the dialog properly
726 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
727 wxBoxSizer
*sizerAll
= new wxBoxSizer(isPda
? wxVERTICAL
: wxHORIZONTAL
);
731 wxStaticBitmap
*icon
= new wxStaticBitmap
735 wxArtProvider::GetMessageBoxIcon(style
)
737 sizerAll
->Add(icon
, wxSizerFlags().Centre());
740 // create the text sizer with a minimal size so that we are sure it won't be too small
741 wxString message
= EllipsizeString(messages
.Last());
742 wxSizer
*szText
= CreateTextSizer(message
);
743 szText
->SetMinSize(wxMin(300, wxGetDisplaySize().x
/ 3), -1);
745 sizerAll
->Add(szText
, wxSizerFlags(1).Centre().Border(wxLEFT
| wxRIGHT
));
747 wxButton
*btnOk
= new wxButton(this, wxID_OK
);
748 sizerAll
->Add(btnOk
, wxSizerFlags().Centre());
750 sizerTop
->Add(sizerAll
, wxSizerFlags().Expand().Border());
753 // add the details pane
754 #ifndef __SMARTPHONE__
757 wxCollapsiblePane
* const
758 collpane
= new wxCollapsiblePane(this, wxID_ANY
, ms_details
);
759 sizerTop
->Add(collpane
, wxSizerFlags(1).Expand().Border());
761 wxWindow
*win
= collpane
->GetPane();
763 wxPanel
* win
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
766 wxSizer
* const paneSz
= new wxBoxSizer(wxVERTICAL
);
768 CreateDetailsControls(win
);
770 paneSz
->Add(m_listctrl
, wxSizerFlags(1).Expand().Border(wxTOP
));
772 #if wxUSE_CLIPBOARD || CAN_SAVE_FILES
773 wxBoxSizer
* const btnSizer
= new wxBoxSizer(wxHORIZONTAL
);
775 wxSizerFlags flagsBtn
;
776 flagsBtn
.Border(wxLEFT
);
779 btnSizer
->Add(new wxButton(win
, wxID_COPY
), flagsBtn
);
780 #endif // wxUSE_CLIPBOARD
783 btnSizer
->Add(new wxButton(win
, wxID_SAVE
), flagsBtn
);
784 #endif // CAN_SAVE_FILES
786 paneSz
->Add(btnSizer
, wxSizerFlags().Right().Border(wxTOP
|wxBOTTOM
));
787 #endif // wxUSE_CLIPBOARD || CAN_SAVE_FILES
789 win
->SetSizer(paneSz
);
790 paneSz
->SetSizeHints(win
);
791 #else // __SMARTPHONE__
792 SetLeftMenu(wxID_OK
);
793 SetRightMenu(wxID_MORE
, ms_details
+ EXPAND_SUFFIX
);
794 #endif // __SMARTPHONE__/!__SMARTPHONE__
796 SetSizerAndFit(sizerTop
);
802 // Move up the screen so that when we expand the dialog,
803 // there's enough space.
804 Move(wxPoint(GetPosition().x
, GetPosition().y
/ 2));
808 void wxLogDialog::CreateDetailsControls(wxWindow
*parent
)
810 wxString fmt
= wxLog::GetTimestamp();
811 bool hasTimeStamp
= !fmt
.IsEmpty();
813 // create the list ctrl now
814 m_listctrl
= new wxListCtrl(parent
, wxID_ANY
,
815 wxDefaultPosition
, wxDefaultSize
,
821 // This makes a big aesthetic difference on WinCE but I
822 // don't want to risk problems on other platforms
826 // no need to translate these strings as they're not shown to the
827 // user anyhow (we use wxLC_NO_HEADER style)
828 m_listctrl
->InsertColumn(0, wxT("Message"));
831 m_listctrl
->InsertColumn(1, wxT("Time"));
833 // prepare the imagelist
834 static const int ICON_SIZE
= 16;
835 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
837 // order should be the same as in the switch below!
838 static const char* const icons
[] =
845 bool loadedIcons
= true;
847 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
849 wxBitmap bmp
= wxArtProvider::GetBitmap(icons
[icon
], wxART_MESSAGE_BOX
,
850 wxSize(ICON_SIZE
, ICON_SIZE
));
852 // This may very well fail if there are insufficient colours available.
853 // Degrade gracefully.
864 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
867 size_t count
= m_messages
.GetCount();
868 for ( size_t n
= 0; n
< count
; n
++ )
874 switch ( m_severity
[n
] )
888 else // failed to load images
893 wxString msg
= m_messages
[n
];
894 msg
.Replace(wxT("\n"), wxT(" "));
895 msg
= EllipsizeString(msg
);
897 m_listctrl
->InsertItem(n
, msg
, image
);
900 m_listctrl
->SetItem(n
, 1, TimeStamp(fmt
, (time_t)m_times
[n
]));
903 // let the columns size themselves
904 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
906 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
908 // calculate an approximately nice height for the listctrl
909 int height
= GetCharHeight()*(count
+ 4);
911 // but check that the dialog won't fall fown from the screen
913 // we use GetMinHeight() to get the height of the dialog part without the
914 // details and we consider that the "Save" button below and the separator
915 // line (and the margins around it) take about as much, hence double it
916 int heightMax
= wxGetDisplaySize().y
- GetPosition().y
- 2*GetMinHeight();
918 // we should leave a margin
922 m_listctrl
->SetSize(wxDefaultCoord
, wxMin(height
, heightMax
));
925 void wxLogDialog::OnListItemActivated(wxListEvent
& event
)
927 // show the activated item in a message box
928 // This allow the user to correctly display the logs which are longer
929 // than the listctrl and thus gets truncated or those which contains
933 // wxString str = m_listctrl->GetItemText(event.GetIndex());
934 // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW.
935 wxString str
= m_messages
[event
.GetIndex()];
937 // wxMessageBox will nicely handle the '\n' in the string (if any)
938 // and supports long strings
939 wxMessageBox(str
, wxT("Log message"), wxOK
, this);
942 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
947 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
949 wxString
wxLogDialog::GetLogMessages() const
951 wxString fmt
= wxLog::GetTimestamp();
954 // use the default format
958 const size_t count
= m_messages
.GetCount();
961 text
.reserve(count
*m_messages
[0].length());
962 for ( size_t n
= 0; n
< count
; n
++ )
964 text
<< TimeStamp(fmt
, (time_t)m_times
[n
])
967 << wxTextFile::GetEOL();
973 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
977 void wxLogDialog::OnCopy(wxCommandEvent
& WXUNUSED(event
))
979 wxClipboardLocker clip
;
981 !wxTheClipboard
->AddData(new wxTextDataObject(GetLogMessages())) )
983 wxLogError(_("Failed to copy dialog contents to the clipboard."));
987 #endif // wxUSE_CLIPBOARD
991 void wxLogDialog::OnSave(wxCommandEvent
& WXUNUSED(event
))
994 int rc
= OpenLogFile(file
, NULL
, this);
1001 if ( !rc
|| !file
.Write(GetLogMessages()) || !file
.Close() )
1003 wxLogError(_("Can't save log contents to file."));
1007 #endif // CAN_SAVE_FILES
1009 wxLogDialog::~wxLogDialog()
1013 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
1017 #endif // wxUSE_LOG_DIALOG
1021 // pass an uninitialized file object, the function will ask the user for the
1022 // filename and try to open it, returns true on success (file was opened),
1023 // false if file couldn't be opened/created and -1 if the file selection
1024 // dialog was cancelled
1025 static int OpenLogFile(wxFile
& file
, wxString
*pFilename
, wxWindow
*parent
)
1027 // get the file name
1028 // -----------------
1029 wxString filename
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent
);
1037 bool bOk
= true; // suppress warning about it being possible uninitialized
1038 if ( wxFile::Exists(filename
) ) {
1039 bool bAppend
= false;
1041 strMsg
.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
1043 switch ( wxMessageBox(strMsg
, _("Question"),
1044 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
) ) {
1057 wxFAIL_MSG(_("invalid message box return value"));
1061 bOk
= file
.Open(filename
, wxFile::write_append
);
1064 bOk
= file
.Create(filename
, true /* overwrite */);
1068 bOk
= file
.Create(filename
);
1072 *pFilename
= filename
;
1077 #endif // CAN_SAVE_FILES
1079 #endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
1081 #if wxUSE_LOG && wxUSE_TEXTCTRL
1083 // ----------------------------------------------------------------------------
1084 // wxLogTextCtrl implementation
1085 // ----------------------------------------------------------------------------
1087 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
1089 m_pTextCtrl
= pTextCtrl
;
1092 void wxLogTextCtrl::DoLogText(const wxString
& msg
)
1094 m_pTextCtrl
->AppendText(msg
+ wxS('\n'));
1097 #endif // wxUSE_LOG && wxUSE_TEXTCTRL