]>
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)
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/button.h"
34 #include "wx/filedlg.h"
35 #include "wx/msgdlg.h"
36 #include "wx/textctrl.h"
38 #include "wx/statbmp.h"
39 #include "wx/settings.h"
40 #include "wx/wxcrtvararg.h"
43 #if wxUSE_LOGGUI || wxUSE_LOGWINDOW
46 #include "wx/clipbrd.h"
47 #include "wx/dataobj.h"
48 #include "wx/textfile.h"
49 #include "wx/statline.h"
50 #include "wx/artprov.h"
51 #include "wx/collpane.h"
52 #include "wx/arrstr.h"
53 #include "wx/msgout.h"
56 // for OutputDebugString()
57 #include "wx/msw/private.h"
66 #include "wx/listctrl.h"
67 #include "wx/imaglist.h"
69 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
73 // the suffix we add to the button to show that the dialog can be expanded
74 #define EXPAND_SUFFIX wxT(" >>")
76 #define CAN_SAVE_FILES (wxUSE_FILE && wxUSE_FILEDLG)
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
84 // this function is a wrapper around strftime(3)
85 // allows to exclude the usage of wxDateTime
86 static wxString
TimeStamp(const wxString
& format
, time_t t
)
90 if ( !wxStrftime(buf
, WXSIZEOF(buf
), format
, wxLocaltime_r(&t
, &tm
)) )
92 // buffer is too small?
93 wxFAIL_MSG(wxT("strftime() failed"));
99 class wxLogDialog
: public wxDialog
102 wxLogDialog(wxWindow
*parent
,
103 const wxArrayString
& messages
,
104 const wxArrayInt
& severity
,
105 const wxArrayLong
& timess
,
106 const wxString
& caption
,
108 virtual ~wxLogDialog();
111 void OnOk(wxCommandEvent
& event
);
113 void OnCopy(wxCommandEvent
& event
);
114 #endif // wxUSE_CLIPBOARD
116 void OnSave(wxCommandEvent
& event
);
117 #endif // CAN_SAVE_FILES
118 void OnListItemActivated(wxListEvent
& event
);
121 // create controls needed for the details display
122 void CreateDetailsControls(wxWindow
*);
124 // if necessary truncates the given string and adds an ellipsis
125 wxString
EllipsizeString(const wxString
&text
)
127 if (ms_maxLength
> 0 &&
128 text
.length() > ms_maxLength
)
131 ret
.Truncate(ms_maxLength
);
139 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
140 // return the contents of the dialog as a multiline string
141 wxString
GetLogMessages() const;
142 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
145 // the data for the listctrl
146 wxArrayString m_messages
;
147 wxArrayInt m_severity
;
150 // the controls which are not shown initially (but only when details
151 // button is pressed)
152 wxListCtrl
*m_listctrl
;
154 // the translated "Details" string
155 static wxString ms_details
;
157 // the maximum length of the log message
158 static size_t ms_maxLength
;
160 DECLARE_EVENT_TABLE()
161 wxDECLARE_NO_COPY_CLASS(wxLogDialog
);
164 BEGIN_EVENT_TABLE(wxLogDialog
, wxDialog
)
165 EVT_BUTTON(wxID_OK
, wxLogDialog::OnOk
)
167 EVT_BUTTON(wxID_COPY
, wxLogDialog::OnCopy
)
168 #endif // wxUSE_CLIPBOARD
170 EVT_BUTTON(wxID_SAVE
, wxLogDialog::OnSave
)
171 #endif // CAN_SAVE_FILES
172 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxLogDialog::OnListItemActivated
)
175 #endif // wxUSE_LOG_DIALOG
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
183 // pass an uninitialized file object, the function will ask the user for the
184 // filename and try to open it, returns true on success (file was opened),
185 // false if file couldn't be opened/created and -1 if the file selection
186 // dialog was cancelled
187 static int OpenLogFile(wxFile
& file
, wxString
*filename
= NULL
, wxWindow
*parent
= NULL
);
189 #endif // CAN_SAVE_FILES
191 // ============================================================================
193 // ============================================================================
195 // ----------------------------------------------------------------------------
196 // wxLogGui implementation (FIXME MT-unsafe)
197 // ----------------------------------------------------------------------------
206 void wxLogGui::Clear()
210 m_bHasMessages
= false;
217 int wxLogGui::GetSeverityIcon() const
219 return m_bErrors
? wxICON_STOP
220 : m_bWarnings
? wxICON_EXCLAMATION
221 : wxICON_INFORMATION
;
224 wxString
wxLogGui::GetTitle() const
226 wxString titleFormat
;
227 switch ( GetSeverityIcon() )
230 titleFormat
= _("%s Error");
233 case wxICON_EXCLAMATION
:
234 titleFormat
= _("%s Warning");
238 wxFAIL_MSG( "unexpected icon severity" );
241 case wxICON_INFORMATION
:
242 titleFormat
= _("%s Information");
245 return wxString::Format(titleFormat
, wxTheApp
->GetAppDisplayName());
249 wxLogGui::DoShowSingleLogMessage(const wxString
& message
,
250 const wxString
& title
,
253 wxMessageBox(message
, title
, wxOK
| style
);
257 wxLogGui::DoShowMultipleLogMessages(const wxArrayString
& messages
,
258 const wxArrayInt
& severities
,
259 const wxArrayLong
& times
,
260 const wxString
& title
,
264 wxLogDialog
dlg(NULL
,
265 messages
, severities
, times
,
268 // clear the message list before showing the dialog because while it's
269 // shown some new messages may appear
272 (void)dlg
.ShowModal();
273 #else // !wxUSE_LOG_DIALOG
274 // start from the most recent message
276 const size_t nMsgCount
= messages
.size();
277 message
.reserve(nMsgCount
*100);
278 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
279 message
<< m_aMessages
[n
- 1] << wxT("\n");
282 DoShowSingleLogMessage(message
, title
, style
);
283 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
286 void wxLogGui::Flush()
290 if ( !m_bHasMessages
)
293 // do it right now to block any new calls to Flush() while we're here
294 m_bHasMessages
= false;
296 // note that this must be done before examining m_aMessages as it may log
297 // yet another message
298 const unsigned repeatCount
= LogLastRepeatIfNeeded();
300 const size_t nMsgCount
= m_aMessages
.size();
302 if ( repeatCount
> 0 )
304 m_aMessages
[nMsgCount
- 1] << " (" << m_aMessages
[nMsgCount
- 2] << ")";
307 const wxString title
= GetTitle();
308 const int style
= GetSeverityIcon();
310 // avoid showing other log dialogs until we're done with the dialog we're
311 // showing right now: nested modal dialogs make for really bad UI!
314 if ( nMsgCount
== 1 )
316 // make a copy before calling Clear()
317 const wxString
message(m_aMessages
[0]);
320 DoShowSingleLogMessage(message
, title
, style
);
322 else // more than one message
324 wxArrayString messages
;
325 wxArrayInt severities
;
328 messages
.swap(m_aMessages
);
329 severities
.swap(m_aSeverity
);
330 times
.swap(m_aTimes
);
334 DoShowMultipleLogMessages(messages
, severities
, times
, title
, style
);
337 // allow flushing the logs again
341 // log all kinds of messages
342 void wxLogGui::DoLogRecord(wxLogLevel level
,
344 const wxLogRecordInfo
& info
)
352 m_aMessages
.Add(msg
);
353 m_aSeverity
.Add(wxLOG_Message
);
354 m_aTimes
.Add((long)info
.timestamp
);
355 m_bHasMessages
= true;
362 wxFrame
*pFrame
= NULL
;
364 // check if the frame was passed to us explicitly
366 if ( info
.GetNumValue(wxLOG_KEY_FRAME
, &ptr
) )
368 pFrame
= static_cast<wxFrame
*>(wxUIntToPtr(ptr
));
371 // find the top window and set it's status text if it has any
372 if ( pFrame
== NULL
) {
373 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
374 if ( wxDynamicCast(pWin
, wxFrame
) ) {
375 pFrame
= (wxFrame
*)pWin
;
379 if ( pFrame
&& pFrame
->GetStatusBar() )
380 pFrame
->SetStatusText(msg
);
382 #endif // wxUSE_STATUSBAR
387 #if !wxUSE_LOG_DIALOG
388 // discard earlier informational messages if this is the 1st
389 // error because they might not make sense any more and showing
390 // them in a message box might be confusing
394 #endif // wxUSE_LOG_DIALOG
401 // for the warning we don't discard the info messages
405 m_aMessages
.Add(msg
);
406 m_aSeverity
.Add((int)level
);
407 m_aTimes
.Add((long)info
.timestamp
);
408 m_bHasMessages
= true;
413 // let the base class deal with debug/trace messages
414 wxLog::DoLogRecord(level
, msg
, info
);
417 case wxLOG_FatalError
:
419 // fatal errors are shown immediately and terminate the program so
420 // we should never see them here
421 wxFAIL_MSG("unexpected log level");
426 // just ignore those: passing them to the base class would result
427 // in asserts from DoLogText() because DoLogTextAtLevel() would
428 // call it as it doesn't know how to handle these levels otherwise
433 #endif // wxUSE_LOGGUI
435 // ----------------------------------------------------------------------------
436 // wxLogWindow and wxLogFrame implementation
437 // ----------------------------------------------------------------------------
443 class wxLogFrame
: public wxFrame
447 wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
);
448 virtual ~wxLogFrame();
450 // Don't prevent the application from exiting if just this frame remains.
451 virtual bool ShouldPreventAppExit() const { return false; }
454 void OnClose(wxCommandEvent
& event
);
455 void OnCloseWindow(wxCloseEvent
& event
);
457 void OnSave(wxCommandEvent
& event
);
458 #endif // CAN_SAVE_FILES
459 void OnClear(wxCommandEvent
& event
);
461 // do show the message in the text control
462 void ShowLogMessage(const wxString
& message
)
464 m_pTextCtrl
->AppendText(message
+ wxS('\n'));
468 // use standard ids for our commands!
471 Menu_Close
= wxID_CLOSE
,
472 Menu_Save
= wxID_SAVE
,
473 Menu_Clear
= wxID_CLEAR
476 // common part of OnClose() and OnCloseWindow()
479 wxTextCtrl
*m_pTextCtrl
;
482 DECLARE_EVENT_TABLE()
483 wxDECLARE_NO_COPY_CLASS(wxLogFrame
);
486 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
487 // wxLogWindow menu events
488 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
490 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
491 #endif // CAN_SAVE_FILES
492 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
494 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
497 wxLogFrame::wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
)
498 : wxFrame(pParent
, wxID_ANY
, szTitle
)
502 m_pTextCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
506 // needed for Win32 to avoid 65Kb limit but it doesn't work well
507 // when using RichEdit 2.0 which we always do in the Unicode build
510 #endif // !wxUSE_UNICODE
515 wxMenuBar
*pMenuBar
= new wxMenuBar
;
516 wxMenu
*pMenu
= new wxMenu
;
518 pMenu
->Append(Menu_Save
, _("Save &As..."), _("Save log contents to file"));
519 #endif // CAN_SAVE_FILES
520 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
521 pMenu
->AppendSeparator();
522 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
523 pMenuBar
->Append(pMenu
, _("&Log"));
524 SetMenuBar(pMenuBar
);
525 #endif // wxUSE_MENUS
528 // status bar for menu prompts
530 #endif // wxUSE_STATUSBAR
533 void wxLogFrame::DoClose()
535 if ( m_log
->OnFrameClose(this) )
537 // instead of closing just hide the window to be able to Show() it
543 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
548 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
554 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
558 int rc
= OpenLogFile(file
, &filename
, this);
567 // retrieve text and save it
568 // -------------------------
569 int nLines
= m_pTextCtrl
->GetNumberOfLines();
570 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
571 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
572 wxTextFile::GetEOL());
579 wxLogError(_("Can't save log contents to file."));
582 wxLogStatus((wxFrame
*)this, _("Log saved to the file '%s'."), filename
.c_str());
585 #endif // CAN_SAVE_FILES
587 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
589 m_pTextCtrl
->Clear();
592 wxLogFrame::~wxLogFrame()
594 m_log
->OnFrameDelete(this);
600 wxLogWindow::wxLogWindow(wxWindow
*pParent
,
601 const wxString
& szTitle
,
605 // Initialize it to NULL to ensure that we don't crash if any log messages
606 // are generated before the frame is fully created (while this doesn't
607 // happen normally, it might, in principle).
610 PassMessages(bDoPass
);
612 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
618 void wxLogWindow::Show(bool bShow
)
620 m_pLogFrame
->Show(bShow
);
623 void wxLogWindow::DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
628 // don't put trace messages in the text window for 2 reasons:
629 // 1) there are too many of them
630 // 2) they may provoke other trace messages (e.g. wxMSW code uses
631 // wxLogTrace to log Windows messages and adding text to the control
632 // sends more of them) thus sending a program into an infinite loop
633 if ( level
== wxLOG_Trace
)
636 m_pLogFrame
->ShowLogMessage(msg
);
639 wxFrame
*wxLogWindow::GetFrame() const
644 bool wxLogWindow::OnFrameClose(wxFrame
* WXUNUSED(frame
))
650 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
655 wxLogWindow::~wxLogWindow()
657 // may be NULL if log frame already auto destroyed itself
661 #endif // wxUSE_LOGWINDOW
663 // ----------------------------------------------------------------------------
665 // ----------------------------------------------------------------------------
669 wxString
wxLogDialog::ms_details
;
670 size_t wxLogDialog::ms_maxLength
= 0;
672 wxLogDialog::wxLogDialog(wxWindow
*parent
,
673 const wxArrayString
& messages
,
674 const wxArrayInt
& severity
,
675 const wxArrayLong
& times
,
676 const wxString
& caption
,
678 : wxDialog(parent
, wxID_ANY
, caption
,
679 wxDefaultPosition
, wxDefaultSize
,
680 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
682 // init the static variables:
684 if ( ms_details
.empty() )
686 // ensure that we won't loop here if wxGetTranslation()
687 // happens to pop up a Log message while translating this :-)
688 ms_details
= wxTRANSLATE("&Details");
689 ms_details
= wxGetTranslation(ms_details
);
690 #ifdef __SMARTPHONE__
691 ms_details
= wxStripMenuCodes(ms_details
);
695 if ( ms_maxLength
== 0 )
697 ms_maxLength
= (2 * wxGetDisplaySize().x
/3) / GetCharWidth();
700 size_t count
= messages
.GetCount();
701 m_messages
.Alloc(count
);
702 m_severity
.Alloc(count
);
703 m_times
.Alloc(count
);
705 for ( size_t n
= 0; n
< count
; n
++ )
707 m_messages
.Add(messages
[n
]);
708 m_severity
.Add(severity
[n
]);
709 m_times
.Add(times
[n
]);
714 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
716 // create the controls which are always shown and layout them: we use
717 // sizers even though our window is not resizable to calculate the size of
718 // the dialog properly
719 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
720 wxBoxSizer
*sizerAll
= new wxBoxSizer(isPda
? wxVERTICAL
: wxHORIZONTAL
);
724 wxStaticBitmap
*icon
= new wxStaticBitmap
728 wxArtProvider::GetMessageBoxIcon(style
)
730 sizerAll
->Add(icon
, wxSizerFlags().Centre());
733 // create the text sizer with a minimal size so that we are sure it won't be too small
734 wxString message
= EllipsizeString(messages
.Last());
735 wxSizer
*szText
= CreateTextSizer(message
);
736 szText
->SetMinSize(wxMin(300, wxGetDisplaySize().x
/ 3), -1);
738 sizerAll
->Add(szText
, wxSizerFlags(1).Centre().Border(wxLEFT
| wxRIGHT
));
740 wxButton
*btnOk
= new wxButton(this, wxID_OK
);
741 sizerAll
->Add(btnOk
, wxSizerFlags().Centre());
743 sizerTop
->Add(sizerAll
, wxSizerFlags().Expand().Border());
746 // add the details pane
747 #ifndef __SMARTPHONE__
750 wxCollapsiblePane
* const
751 collpane
= new wxCollapsiblePane(this, wxID_ANY
, ms_details
);
752 sizerTop
->Add(collpane
, wxSizerFlags(1).Expand().Border());
754 wxWindow
*win
= collpane
->GetPane();
756 wxPanel
* win
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
759 wxSizer
* const paneSz
= new wxBoxSizer(wxVERTICAL
);
761 CreateDetailsControls(win
);
763 paneSz
->Add(m_listctrl
, wxSizerFlags(1).Expand().Border(wxTOP
));
765 #if wxUSE_CLIPBOARD || CAN_SAVE_FILES
766 wxBoxSizer
* const btnSizer
= new wxBoxSizer(wxHORIZONTAL
);
768 wxSizerFlags flagsBtn
;
769 flagsBtn
.Border(wxLEFT
);
772 btnSizer
->Add(new wxButton(win
, wxID_COPY
), flagsBtn
);
773 #endif // wxUSE_CLIPBOARD
776 btnSizer
->Add(new wxButton(win
, wxID_SAVE
), flagsBtn
);
777 #endif // CAN_SAVE_FILES
779 paneSz
->Add(btnSizer
, wxSizerFlags().Right().Border(wxTOP
|wxBOTTOM
));
780 #endif // wxUSE_CLIPBOARD || CAN_SAVE_FILES
782 win
->SetSizer(paneSz
);
783 paneSz
->SetSizeHints(win
);
784 #else // __SMARTPHONE__
785 SetLeftMenu(wxID_OK
);
786 SetRightMenu(wxID_MORE
, ms_details
+ EXPAND_SUFFIX
);
787 #endif // __SMARTPHONE__/!__SMARTPHONE__
789 SetSizerAndFit(sizerTop
);
795 // Move up the screen so that when we expand the dialog,
796 // there's enough space.
797 Move(wxPoint(GetPosition().x
, GetPosition().y
/ 2));
801 void wxLogDialog::CreateDetailsControls(wxWindow
*parent
)
803 wxString fmt
= wxLog::GetTimestamp();
804 bool hasTimeStamp
= !fmt
.IsEmpty();
806 // create the list ctrl now
807 m_listctrl
= new wxListCtrl(parent
, wxID_ANY
,
808 wxDefaultPosition
, wxDefaultSize
,
814 // This makes a big aesthetic difference on WinCE but I
815 // don't want to risk problems on other platforms
819 // no need to translate these strings as they're not shown to the
820 // user anyhow (we use wxLC_NO_HEADER style)
821 m_listctrl
->InsertColumn(0, wxT("Message"));
824 m_listctrl
->InsertColumn(1, wxT("Time"));
826 // prepare the imagelist
827 static const int ICON_SIZE
= 16;
828 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
830 // order should be the same as in the switch below!
831 static const char* const icons
[] =
838 bool loadedIcons
= true;
840 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
842 wxBitmap bmp
= wxArtProvider::GetBitmap(icons
[icon
], wxART_MESSAGE_BOX
,
843 wxSize(ICON_SIZE
, ICON_SIZE
));
845 // This may very well fail if there are insufficient colours available.
846 // Degrade gracefully.
857 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
860 size_t count
= m_messages
.GetCount();
861 for ( size_t n
= 0; n
< count
; n
++ )
867 switch ( m_severity
[n
] )
881 else // failed to load images
886 wxString msg
= m_messages
[n
];
887 msg
.Replace(wxT("\n"), wxT(" "));
888 msg
= EllipsizeString(msg
);
890 m_listctrl
->InsertItem(n
, msg
, image
);
893 m_listctrl
->SetItem(n
, 1, TimeStamp(fmt
, (time_t)m_times
[n
]));
896 // let the columns size themselves
897 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
899 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
901 // calculate an approximately nice height for the listctrl
902 int height
= GetCharHeight()*(count
+ 4);
904 // but check that the dialog won't fall fown from the screen
906 // we use GetMinHeight() to get the height of the dialog part without the
907 // details and we consider that the "Save" button below and the separator
908 // line (and the margins around it) take about as much, hence double it
909 int heightMax
= wxGetDisplaySize().y
- GetPosition().y
- 2*GetMinHeight();
911 // we should leave a margin
915 m_listctrl
->SetSize(wxDefaultCoord
, wxMin(height
, heightMax
));
918 void wxLogDialog::OnListItemActivated(wxListEvent
& event
)
920 // show the activated item in a message box
921 // This allow the user to correctly display the logs which are longer
922 // than the listctrl and thus gets truncated or those which contains
926 // wxString str = m_listctrl->GetItemText(event.GetIndex());
927 // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW.
928 wxString str
= m_messages
[event
.GetIndex()];
930 // wxMessageBox will nicely handle the '\n' in the string (if any)
931 // and supports long strings
932 wxMessageBox(str
, wxT("Log message"), wxOK
, this);
935 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
940 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
942 wxString
wxLogDialog::GetLogMessages() const
944 wxString fmt
= wxLog::GetTimestamp();
947 // use the default format
951 const size_t count
= m_messages
.GetCount();
954 text
.reserve(count
*m_messages
[0].length());
955 for ( size_t n
= 0; n
< count
; n
++ )
957 text
<< TimeStamp(fmt
, (time_t)m_times
[n
])
960 << wxTextFile::GetEOL();
966 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
970 void wxLogDialog::OnCopy(wxCommandEvent
& WXUNUSED(event
))
972 wxClipboardLocker clip
;
974 !wxTheClipboard
->AddData(new wxTextDataObject(GetLogMessages())) )
976 wxLogError(_("Failed to copy dialog contents to the clipboard."));
980 #endif // wxUSE_CLIPBOARD
984 void wxLogDialog::OnSave(wxCommandEvent
& WXUNUSED(event
))
987 int rc
= OpenLogFile(file
, NULL
, this);
994 if ( !rc
|| !file
.Write(GetLogMessages()) || !file
.Close() )
996 wxLogError(_("Can't save log contents to file."));
1000 #endif // CAN_SAVE_FILES
1002 wxLogDialog::~wxLogDialog()
1006 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
1010 #endif // wxUSE_LOG_DIALOG
1014 // pass an uninitialized file object, the function will ask the user for the
1015 // filename and try to open it, returns true on success (file was opened),
1016 // false if file couldn't be opened/created and -1 if the file selection
1017 // dialog was cancelled
1018 static int OpenLogFile(wxFile
& file
, wxString
*pFilename
, wxWindow
*parent
)
1020 // get the file name
1021 // -----------------
1022 wxString filename
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent
);
1030 bool bOk
= true; // suppress warning about it being possible uninitialized
1031 if ( wxFile::Exists(filename
) ) {
1032 bool bAppend
= false;
1034 strMsg
.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
1036 switch ( wxMessageBox(strMsg
, _("Question"),
1037 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
) ) {
1050 wxFAIL_MSG(_("invalid message box return value"));
1054 bOk
= file
.Open(filename
, wxFile::write_append
);
1057 bOk
= file
.Create(filename
, true /* overwrite */);
1061 bOk
= file
.Create(filename
);
1065 *pFilename
= filename
;
1070 #endif // CAN_SAVE_FILES
1072 #endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
1074 #if wxUSE_LOG && wxUSE_TEXTCTRL
1076 // ----------------------------------------------------------------------------
1077 // wxLogTextCtrl implementation
1078 // ----------------------------------------------------------------------------
1080 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
1082 m_pTextCtrl
= pTextCtrl
;
1085 void wxLogTextCtrl::DoLogText(const wxString
& msg
)
1087 m_pTextCtrl
->AppendText(msg
+ wxS('\n'));
1090 #endif // wxUSE_LOG && wxUSE_TEXTCTRL