]>
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
72 #if defined(__MWERKS__) && wxUSE_UNICODE
76 #include "wx/datetime.h"
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
)
96 if ( !wxStrftime(buf
, WXSIZEOF(buf
), format
, wxLocaltime_r(&t
, &tm
)) )
98 // buffer is too small?
99 wxFAIL_MSG(wxT("strftime() failed"));
101 return wxString(buf
);
102 #else // !wxUSE_DATETIME
103 return wxEmptyString
;
104 #endif // wxUSE_DATETIME/!wxUSE_DATETIME
108 class wxLogDialog
: public wxDialog
111 wxLogDialog(wxWindow
*parent
,
112 const wxArrayString
& messages
,
113 const wxArrayInt
& severity
,
114 const wxArrayLong
& timess
,
115 const wxString
& caption
,
117 virtual ~wxLogDialog();
120 void OnOk(wxCommandEvent
& event
);
122 void OnCopy(wxCommandEvent
& event
);
123 #endif // wxUSE_CLIPBOARD
125 void OnSave(wxCommandEvent
& event
);
126 #endif // CAN_SAVE_FILES
127 void OnListItemActivated(wxListEvent
& event
);
130 // create controls needed for the details display
131 void CreateDetailsControls(wxWindow
*);
133 // if necessary truncates the given string and adds an ellipsis
134 wxString
EllipsizeString(const wxString
&text
)
136 if (ms_maxLength
> 0 &&
137 text
.length() > ms_maxLength
)
140 ret
.Truncate(ms_maxLength
);
148 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
149 // return the contents of the dialog as a multiline string
150 wxString
GetLogMessages() const;
151 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
154 // the data for the listctrl
155 wxArrayString m_messages
;
156 wxArrayInt m_severity
;
159 // the controls which are not shown initially (but only when details
160 // button is pressed)
161 wxListCtrl
*m_listctrl
;
163 // the translated "Details" string
164 static wxString ms_details
;
166 // the maximum length of the log message
167 static size_t ms_maxLength
;
169 DECLARE_EVENT_TABLE()
170 wxDECLARE_NO_COPY_CLASS(wxLogDialog
);
173 BEGIN_EVENT_TABLE(wxLogDialog
, wxDialog
)
174 EVT_BUTTON(wxID_OK
, wxLogDialog::OnOk
)
176 EVT_BUTTON(wxID_COPY
, wxLogDialog::OnCopy
)
177 #endif // wxUSE_CLIPBOARD
179 EVT_BUTTON(wxID_SAVE
, wxLogDialog::OnSave
)
180 #endif // CAN_SAVE_FILES
181 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxLogDialog::OnListItemActivated
)
184 #endif // wxUSE_LOG_DIALOG
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
192 // pass an uninitialized file object, the function will ask the user for the
193 // filename and try to open it, returns true on success (file was opened),
194 // false if file couldn't be opened/created and -1 if the file selection
195 // dialog was cancelled
196 static int OpenLogFile(wxFile
& file
, wxString
*filename
= NULL
, wxWindow
*parent
= NULL
);
198 #endif // CAN_SAVE_FILES
200 // ============================================================================
202 // ============================================================================
204 // ----------------------------------------------------------------------------
205 // wxLogGui implementation (FIXME MT-unsafe)
206 // ----------------------------------------------------------------------------
215 void wxLogGui::Clear()
219 m_bHasMessages
= false;
226 int wxLogGui::GetSeverityIcon() const
228 return m_bErrors
? wxICON_STOP
229 : m_bWarnings
? wxICON_EXCLAMATION
230 : wxICON_INFORMATION
;
233 wxString
wxLogGui::GetTitle() const
235 wxString titleFormat
;
236 switch ( GetSeverityIcon() )
239 titleFormat
= _("%s Error");
242 case wxICON_EXCLAMATION
:
243 titleFormat
= _("%s Warning");
247 wxFAIL_MSG( "unexpected icon severity" );
250 case wxICON_INFORMATION
:
251 titleFormat
= _("%s Information");
254 return wxString::Format(titleFormat
, wxTheApp
->GetAppDisplayName());
258 wxLogGui::DoShowSingleLogMessage(const wxString
& message
,
259 const wxString
& title
,
262 wxMessageBox(message
, title
, wxOK
| style
);
266 wxLogGui::DoShowMultipleLogMessages(const wxArrayString
& messages
,
267 const wxArrayInt
& severities
,
268 const wxArrayLong
& times
,
269 const wxString
& title
,
273 wxLogDialog
dlg(NULL
,
274 messages
, severities
, times
,
277 // clear the message list before showing the dialog because while it's
278 // shown some new messages may appear
281 (void)dlg
.ShowModal();
282 #else // !wxUSE_LOG_DIALOG
283 // start from the most recent message
285 const size_t nMsgCount
= messages
.size();
286 message
.reserve(nMsgCount
*100);
287 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
288 message
<< m_aMessages
[n
- 1] << wxT("\n");
291 DoShowSingleLogMessage(message
, title
, style
);
292 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
295 void wxLogGui::Flush()
299 if ( !m_bHasMessages
)
302 // do it right now to block any new calls to Flush() while we're here
303 m_bHasMessages
= false;
305 // note that this must be done before examining m_aMessages as it may log
306 // yet another message
307 const unsigned repeatCount
= LogLastRepeatIfNeeded();
309 const size_t nMsgCount
= m_aMessages
.size();
311 if ( repeatCount
> 0 )
313 m_aMessages
[nMsgCount
- 1] << " (" << m_aMessages
[nMsgCount
- 2] << ")";
316 const wxString title
= GetTitle();
317 const int style
= GetSeverityIcon();
319 // avoid showing other log dialogs until we're done with the dialog we're
320 // showing right now: nested modal dialogs make for really bad UI!
323 if ( nMsgCount
== 1 )
325 // make a copy before calling Clear()
326 const wxString
message(m_aMessages
[0]);
329 DoShowSingleLogMessage(message
, title
, style
);
331 else // more than one message
333 wxArrayString messages
;
334 wxArrayInt severities
;
337 messages
.swap(m_aMessages
);
338 severities
.swap(m_aSeverity
);
339 times
.swap(m_aTimes
);
343 DoShowMultipleLogMessages(messages
, severities
, times
, title
, style
);
346 // allow flushing the logs again
350 // log all kinds of messages
351 void wxLogGui::DoLogRecord(wxLogLevel level
,
353 const wxLogRecordInfo
& info
)
361 m_aMessages
.Add(msg
);
362 m_aSeverity
.Add(wxLOG_Message
);
363 m_aTimes
.Add((long)info
.timestamp
);
364 m_bHasMessages
= true;
371 wxFrame
*pFrame
= NULL
;
373 // check if the frame was passed to us explicitly
375 if ( info
.GetNumValue(wxLOG_KEY_FRAME
, &ptr
) )
377 pFrame
= static_cast<wxFrame
*>(wxUIntToPtr(ptr
));
380 // find the top window and set it's status text if it has any
381 if ( pFrame
== NULL
) {
382 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
383 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
384 pFrame
= (wxFrame
*)pWin
;
388 if ( pFrame
&& pFrame
->GetStatusBar() )
389 pFrame
->SetStatusText(msg
);
391 #endif // wxUSE_STATUSBAR
396 #if !wxUSE_LOG_DIALOG
397 // discard earlier informational messages if this is the 1st
398 // error because they might not make sense any more and showing
399 // them in a message box might be confusing
403 #endif // wxUSE_LOG_DIALOG
410 // for the warning we don't discard the info messages
414 m_aMessages
.Add(msg
);
415 m_aSeverity
.Add((int)level
);
416 m_aTimes
.Add((long)info
.timestamp
);
417 m_bHasMessages
= true;
422 // let the base class deal with debug/trace messages
423 wxLog::DoLogRecord(level
, msg
, info
);
426 case wxLOG_FatalError
:
428 // fatal errors are shown immediately and terminate the program so
429 // we should never see them here
430 wxFAIL_MSG("unexpected log level");
435 // just ignore those: passing them to the base class would result
436 // in asserts from DoLogText() because DoLogTextAtLevel() would
437 // call it as it doesn't know how to handle these levels otherwise
442 #endif // wxUSE_LOGGUI
444 // ----------------------------------------------------------------------------
445 // wxLogWindow and wxLogFrame implementation
446 // ----------------------------------------------------------------------------
452 class wxLogFrame
: public wxFrame
456 wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
);
457 virtual ~wxLogFrame();
460 void OnClose(wxCommandEvent
& event
);
461 void OnCloseWindow(wxCloseEvent
& event
);
463 void OnSave(wxCommandEvent
& event
);
464 #endif // CAN_SAVE_FILES
465 void OnClear(wxCommandEvent
& event
);
467 // do show the message in the text control
468 void ShowLogMessage(const wxString
& message
)
470 m_pTextCtrl
->AppendText(message
+ wxS('\n'));
474 // use standard ids for our commands!
477 Menu_Close
= wxID_CLOSE
,
478 Menu_Save
= wxID_SAVE
,
479 Menu_Clear
= wxID_CLEAR
482 // common part of OnClose() and OnCloseWindow()
485 wxTextCtrl
*m_pTextCtrl
;
488 DECLARE_EVENT_TABLE()
489 wxDECLARE_NO_COPY_CLASS(wxLogFrame
);
492 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
493 // wxLogWindow menu events
494 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
496 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
497 #endif // CAN_SAVE_FILES
498 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
500 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
503 wxLogFrame::wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
)
504 : wxFrame(pParent
, wxID_ANY
, szTitle
)
508 m_pTextCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
512 // needed for Win32 to avoid 65Kb limit but it doesn't work well
513 // when using RichEdit 2.0 which we always do in the Unicode build
516 #endif // !wxUSE_UNICODE
521 wxMenuBar
*pMenuBar
= new wxMenuBar
;
522 wxMenu
*pMenu
= new wxMenu
;
524 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
525 #endif // CAN_SAVE_FILES
526 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
527 pMenu
->AppendSeparator();
528 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
529 pMenuBar
->Append(pMenu
, _("&Log"));
530 SetMenuBar(pMenuBar
);
531 #endif // wxUSE_MENUS
534 // status bar for menu prompts
536 #endif // wxUSE_STATUSBAR
538 m_log
->OnFrameCreate(this);
541 void wxLogFrame::DoClose()
543 if ( m_log
->OnFrameClose(this) )
545 // instead of closing just hide the window to be able to Show() it
551 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
556 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
562 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
566 int rc
= OpenLogFile(file
, &filename
, this);
575 // retrieve text and save it
576 // -------------------------
577 int nLines
= m_pTextCtrl
->GetNumberOfLines();
578 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
579 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
580 wxTextFile::GetEOL());
587 wxLogError(_("Can't save log contents to file."));
590 wxLogStatus((wxFrame
*)this, _("Log saved to the file '%s'."), filename
.c_str());
593 #endif // CAN_SAVE_FILES
595 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
597 m_pTextCtrl
->Clear();
600 wxLogFrame::~wxLogFrame()
602 m_log
->OnFrameDelete(this);
608 wxLogWindow::wxLogWindow(wxWindow
*pParent
,
609 const wxString
& szTitle
,
613 // Initialize it to NULL to ensure that we don't crash if any log messages
614 // are generated before the frame is fully created (while this doesn't
615 // happen normally, it might, in principle).
618 PassMessages(bDoPass
);
620 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
626 void wxLogWindow::Show(bool bShow
)
628 m_pLogFrame
->Show(bShow
);
631 void wxLogWindow::DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
636 // don't put trace messages in the text window for 2 reasons:
637 // 1) there are too many of them
638 // 2) they may provoke other trace messages (e.g. wxMSW code uses
639 // wxLogTrace to log Windows messages and adding text to the control
640 // sends more of them) thus sending a program into an infinite loop
641 if ( level
== wxLOG_Trace
)
644 m_pLogFrame
->ShowLogMessage(msg
);
647 wxFrame
*wxLogWindow::GetFrame() const
652 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
656 bool wxLogWindow::OnFrameClose(wxFrame
* WXUNUSED(frame
))
662 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
667 wxLogWindow::~wxLogWindow()
669 // may be NULL if log frame already auto destroyed itself
673 #endif // wxUSE_LOGWINDOW
675 // ----------------------------------------------------------------------------
677 // ----------------------------------------------------------------------------
681 wxString
wxLogDialog::ms_details
;
682 size_t wxLogDialog::ms_maxLength
= 0;
684 wxLogDialog::wxLogDialog(wxWindow
*parent
,
685 const wxArrayString
& messages
,
686 const wxArrayInt
& severity
,
687 const wxArrayLong
& times
,
688 const wxString
& caption
,
690 : wxDialog(parent
, wxID_ANY
, caption
,
691 wxDefaultPosition
, wxDefaultSize
,
692 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
694 // init the static variables:
696 if ( ms_details
.empty() )
698 // ensure that we won't loop here if wxGetTranslation()
699 // happens to pop up a Log message while translating this :-)
700 ms_details
= wxTRANSLATE("&Details");
701 ms_details
= wxGetTranslation(ms_details
);
702 #ifdef __SMARTPHONE__
703 ms_details
= wxStripMenuCodes(ms_details
);
707 if ( ms_maxLength
== 0 )
709 ms_maxLength
= (2 * wxGetDisplaySize().x
/3) / GetCharWidth();
712 size_t count
= messages
.GetCount();
713 m_messages
.Alloc(count
);
714 m_severity
.Alloc(count
);
715 m_times
.Alloc(count
);
717 for ( size_t n
= 0; n
< count
; n
++ )
719 m_messages
.Add(messages
[n
]);
720 m_severity
.Add(severity
[n
]);
721 m_times
.Add(times
[n
]);
726 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
728 // create the controls which are always shown and layout them: we use
729 // sizers even though our window is not resizeable to calculate the size of
730 // the dialog properly
731 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
732 wxBoxSizer
*sizerAll
= new wxBoxSizer(isPda
? wxVERTICAL
: wxHORIZONTAL
);
736 wxStaticBitmap
*icon
= new wxStaticBitmap
740 wxArtProvider::GetMessageBoxIcon(style
)
742 sizerAll
->Add(icon
, wxSizerFlags().Centre());
745 // create the text sizer with a minimal size so that we are sure it won't be too small
746 wxString message
= EllipsizeString(messages
.Last());
747 wxSizer
*szText
= CreateTextSizer(message
);
748 szText
->SetMinSize(wxMin(300, wxGetDisplaySize().x
/ 3), -1);
750 sizerAll
->Add(szText
, wxSizerFlags(1).Centre().Border(wxLEFT
| wxRIGHT
));
752 wxButton
*btnOk
= new wxButton(this, wxID_OK
);
753 sizerAll
->Add(btnOk
, wxSizerFlags().Centre());
755 sizerTop
->Add(sizerAll
, wxSizerFlags().Expand().Border());
758 // add the details pane
759 #ifndef __SMARTPHONE__
760 wxCollapsiblePane
* const
761 collpane
= new wxCollapsiblePane(this, wxID_ANY
, ms_details
);
762 sizerTop
->Add(collpane
, wxSizerFlags(1).Expand().Border());
764 wxWindow
*win
= collpane
->GetPane();
765 wxSizer
* const paneSz
= new wxBoxSizer(wxVERTICAL
);
767 CreateDetailsControls(win
);
769 paneSz
->Add(m_listctrl
, wxSizerFlags(1).Expand().Border(wxTOP
));
771 #if wxUSE_CLIPBOARD || CAN_SAVE_FILES
772 wxBoxSizer
* const btnSizer
= new wxBoxSizer(wxHORIZONTAL
);
774 wxSizerFlags flagsBtn
;
775 flagsBtn
.Border(wxLEFT
);
778 btnSizer
->Add(new wxButton(win
, wxID_COPY
), flagsBtn
);
779 #endif // wxUSE_CLIPBOARD
782 btnSizer
->Add(new wxButton(win
, wxID_SAVE
), flagsBtn
);
783 #endif // CAN_SAVE_FILES
785 paneSz
->Add(btnSizer
, wxSizerFlags().Right().Border(wxTOP
));
786 #endif // wxUSE_CLIPBOARD || CAN_SAVE_FILES
788 win
->SetSizer(paneSz
);
789 paneSz
->SetSizeHints(win
);
790 #else // __SMARTPHONE__
791 SetLeftMenu(wxID_OK
);
792 SetRightMenu(wxID_MORE
, ms_details
+ EXPAND_SUFFIX
);
793 #endif // __SMARTPHONE__/!__SMARTPHONE__
795 SetSizerAndFit(sizerTop
);
801 // Move up the screen so that when we expand the dialog,
802 // there's enough space.
803 Move(wxPoint(GetPosition().x
, GetPosition().y
/ 2));
807 void wxLogDialog::CreateDetailsControls(wxWindow
*parent
)
809 wxString fmt
= wxLog::GetTimestamp();
810 bool hasTimeStamp
= !fmt
.IsEmpty();
812 // create the list ctrl now
813 m_listctrl
= new wxListCtrl(parent
, wxID_ANY
,
814 wxDefaultPosition
, wxDefaultSize
,
820 // This makes a big aesthetic difference on WinCE but I
821 // don't want to risk problems on other platforms
825 // no need to translate these strings as they're not shown to the
826 // user anyhow (we use wxLC_NO_HEADER style)
827 m_listctrl
->InsertColumn(0, wxT("Message"));
830 m_listctrl
->InsertColumn(1, wxT("Time"));
832 // prepare the imagelist
833 static const int ICON_SIZE
= 16;
834 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
836 // order should be the same as in the switch below!
837 static const wxChar
* const icons
[] =
844 bool loadedIcons
= true;
846 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
848 wxBitmap bmp
= wxArtProvider::GetBitmap(icons
[icon
], wxART_MESSAGE_BOX
,
849 wxSize(ICON_SIZE
, ICON_SIZE
));
851 // This may very well fail if there are insufficient colours available.
852 // Degrade gracefully.
863 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
866 size_t count
= m_messages
.GetCount();
867 for ( size_t n
= 0; n
< count
; n
++ )
873 switch ( m_severity
[n
] )
887 else // failed to load images
892 wxString msg
= m_messages
[n
];
893 msg
.Replace(wxT("\n"), wxT(" "));
894 msg
= EllipsizeString(msg
);
896 m_listctrl
->InsertItem(n
, msg
, image
);
899 m_listctrl
->SetItem(n
, 1, TimeStamp(fmt
, (time_t)m_times
[n
]));
902 // let the columns size themselves
903 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
905 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
907 // calculate an approximately nice height for the listctrl
908 int height
= GetCharHeight()*(count
+ 4);
910 // but check that the dialog won't fall fown from the screen
912 // we use GetMinHeight() to get the height of the dialog part without the
913 // details and we consider that the "Save" button below and the separator
914 // line (and the margins around it) take about as much, hence double it
915 int heightMax
= wxGetDisplaySize().y
- GetPosition().y
- 2*GetMinHeight();
917 // we should leave a margin
921 m_listctrl
->SetSize(wxDefaultCoord
, wxMin(height
, heightMax
));
924 void wxLogDialog::OnListItemActivated(wxListEvent
& event
)
926 // show the activated item in a message box
927 // This allow the user to correctly display the logs which are longer
928 // than the listctrl and thus gets truncated or those which contains
932 // wxString str = m_listctrl->GetItemText(event.GetIndex());
933 // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW.
934 wxString str
= m_messages
[event
.GetIndex()];
936 // wxMessageBox will nicely handle the '\n' in the string (if any)
937 // and supports long strings
938 wxMessageBox(str
, wxT("Log message"), wxOK
, this);
941 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
946 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
948 wxString
wxLogDialog::GetLogMessages() const
950 wxString fmt
= wxLog::GetTimestamp();
953 // use the default format
957 const size_t count
= m_messages
.GetCount();
960 text
.reserve(count
*m_messages
[0].length());
961 for ( size_t n
= 0; n
< count
; n
++ )
963 text
<< TimeStamp(fmt
, (time_t)m_times
[n
])
966 << wxTextFile::GetEOL();
972 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
976 void wxLogDialog::OnCopy(wxCommandEvent
& WXUNUSED(event
))
978 wxClipboardLocker clip
;
980 !wxTheClipboard
->AddData(new wxTextDataObject(GetLogMessages())) )
982 wxLogError(_("Failed to copy dialog contents to the clipboard."));
986 #endif // wxUSE_CLIPBOARD
990 void wxLogDialog::OnSave(wxCommandEvent
& WXUNUSED(event
))
993 int rc
= OpenLogFile(file
, NULL
, this);
1000 if ( !rc
|| !file
.Write(GetLogMessages()) || !file
.Close() )
1002 wxLogError(_("Can't save log contents to file."));
1006 #endif // CAN_SAVE_FILES
1008 wxLogDialog::~wxLogDialog()
1012 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
1016 #endif // wxUSE_LOG_DIALOG
1020 // pass an uninitialized file object, the function will ask the user for the
1021 // filename and try to open it, returns true on success (file was opened),
1022 // false if file couldn't be opened/created and -1 if the file selection
1023 // dialog was cancelled
1024 static int OpenLogFile(wxFile
& file
, wxString
*pFilename
, wxWindow
*parent
)
1026 // get the file name
1027 // -----------------
1028 wxString filename
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent
);
1036 bool bOk
= true; // suppress warning about it being possible uninitialized
1037 if ( wxFile::Exists(filename
) ) {
1038 bool bAppend
= false;
1040 strMsg
.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
1042 switch ( wxMessageBox(strMsg
, _("Question"),
1043 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
) ) {
1056 wxFAIL_MSG(_("invalid message box return value"));
1060 bOk
= file
.Open(filename
, wxFile::write_append
);
1063 bOk
= file
.Create(filename
, true /* overwrite */);
1067 bOk
= file
.Create(filename
);
1071 *pFilename
= filename
;
1076 #endif // CAN_SAVE_FILES
1078 #endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
1080 #if wxUSE_LOG && wxUSE_TEXTCTRL
1082 // ----------------------------------------------------------------------------
1083 // wxLogTextCtrl implementation
1084 // ----------------------------------------------------------------------------
1086 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
1088 m_pTextCtrl
= pTextCtrl
;
1091 void wxLogTextCtrl::DoLogText(const wxString
& msg
)
1093 m_pTextCtrl
->AppendText(msg
+ wxS('\n'));
1096 #endif // wxUSE_LOG && wxUSE_TEXTCTRL