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 #include "wx/thread.h"
58 #endif // wxUSE_THREADS
61 // for OutputDebugString()
62 #include "wx/msw/private.h"
71 #include "wx/listctrl.h"
72 #include "wx/imaglist.h"
74 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
76 #if defined(__MWERKS__) && wxUSE_UNICODE
80 #include "wx/datetime.h"
82 // the suffix we add to the button to show that the dialog can be expanded
83 #define EXPAND_SUFFIX _T(" >>")
85 #define CAN_SAVE_FILES (wxUSE_FILE && wxUSE_FILEDLG)
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
93 // this function is a wrapper around strftime(3)
94 // allows to exclude the usage of wxDateTime
95 static wxString
TimeStamp(const wxString
& format
, time_t t
)
100 if ( !wxStrftime(buf
, WXSIZEOF(buf
), format
, wxLocaltime_r(&t
, &tm
)) )
102 // buffer is too small?
103 wxFAIL_MSG(_T("strftime() failed"));
105 return wxString(buf
);
106 #else // !wxUSE_DATETIME
107 return wxEmptyString
;
108 #endif // wxUSE_DATETIME/!wxUSE_DATETIME
112 class wxLogDialog
: public wxDialog
115 wxLogDialog(wxWindow
*parent
,
116 const wxArrayString
& messages
,
117 const wxArrayInt
& severity
,
118 const wxArrayLong
& timess
,
119 const wxString
& caption
,
121 virtual ~wxLogDialog();
124 void OnOk(wxCommandEvent
& event
);
126 void OnCopy(wxCommandEvent
& event
);
127 #endif // wxUSE_CLIPBOARD
129 void OnSave(wxCommandEvent
& event
);
130 #endif // CAN_SAVE_FILES
131 void OnListItemActivated(wxListEvent
& event
);
134 // create controls needed for the details display
135 void CreateDetailsControls(wxWindow
*);
137 // if necessary truncates the given string and adds an ellipsis
138 wxString
EllipsizeString(const wxString
&text
)
140 if (ms_maxLength
> 0 &&
141 text
.length() > ms_maxLength
)
144 ret
.Truncate(ms_maxLength
);
152 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
153 // return the contents of the dialog as a multiline string
154 wxString
GetLogMessages() const;
155 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
158 // the data for the listctrl
159 wxArrayString m_messages
;
160 wxArrayInt m_severity
;
163 // the controls which are not shown initially (but only when details
164 // button is pressed)
165 wxListCtrl
*m_listctrl
;
167 // the translated "Details" string
168 static wxString ms_details
;
170 // the maximum length of the log message
171 static size_t ms_maxLength
;
173 DECLARE_EVENT_TABLE()
174 wxDECLARE_NO_COPY_CLASS(wxLogDialog
);
177 BEGIN_EVENT_TABLE(wxLogDialog
, wxDialog
)
178 EVT_BUTTON(wxID_OK
, wxLogDialog::OnOk
)
180 EVT_BUTTON(wxID_COPY
, wxLogDialog::OnCopy
)
181 #endif // wxUSE_CLIPBOARD
183 EVT_BUTTON(wxID_SAVE
, wxLogDialog::OnSave
)
184 #endif // CAN_SAVE_FILES
185 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxLogDialog::OnListItemActivated
)
188 #endif // wxUSE_LOG_DIALOG
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
196 // pass an uninitialized file object, the function will ask the user for the
197 // filename and try to open it, returns true on success (file was opened),
198 // false if file couldn't be opened/created and -1 if the file selection
199 // dialog was cancelled
200 static int OpenLogFile(wxFile
& file
, wxString
*filename
= NULL
, wxWindow
*parent
= NULL
);
202 #endif // CAN_SAVE_FILES
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 // we use a global variable to store the frame pointer for wxLogStatus - bad,
209 // but it's the easiest way
210 static wxFrame
*gs_pFrame
= NULL
; // FIXME MT-unsafe
212 // ============================================================================
214 // ============================================================================
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
220 // accepts an additional argument which tells to which frame the output should
222 void wxVLogStatus(wxFrame
*pFrame
, const wxString
& format
, va_list argptr
)
226 wxLog
*pLog
= wxLog::GetActiveTarget();
227 if ( pLog
!= NULL
) {
228 msg
.PrintfV(format
, argptr
);
230 wxASSERT( gs_pFrame
== NULL
); // should be reset!
233 wxLog::OnLog(wxLOG_Status
, msg
, 0);
235 wxLog::OnLog(wxLOG_Status
, msg
, time(NULL
));
241 #if !wxUSE_UTF8_LOCALE_ONLY
242 void wxDoLogStatusWchar(wxFrame
*pFrame
, const wxChar
*format
, ...)
245 va_start(argptr
, format
);
246 wxVLogStatus(pFrame
, format
, argptr
);
249 #endif // !wxUSE_UTF8_LOCALE_ONLY
251 #if wxUSE_UNICODE_UTF8
252 void wxDoLogStatusUtf8(wxFrame
*pFrame
, const char *format
, ...)
255 va_start(argptr
, format
);
256 wxVLogStatus(pFrame
, format
, argptr
);
259 #endif // wxUSE_UNICODE_UTF8
261 // ----------------------------------------------------------------------------
262 // wxLogGui implementation (FIXME MT-unsafe)
263 // ----------------------------------------------------------------------------
272 void wxLogGui::Clear()
276 m_bHasMessages
= false;
283 int wxLogGui::GetSeverityIcon() const
285 return m_bErrors
? wxICON_STOP
286 : m_bWarnings
? wxICON_EXCLAMATION
287 : wxICON_INFORMATION
;
290 wxString
wxLogGui::GetTitle() const
292 wxString titleFormat
;
293 switch ( GetSeverityIcon() )
296 titleFormat
= _("%s Error");
299 case wxICON_EXCLAMATION
:
300 titleFormat
= _("%s Warning");
304 wxFAIL_MSG( "unexpected icon severity" );
307 case wxICON_INFORMATION
:
308 titleFormat
= _("%s Information");
311 return wxString::Format(titleFormat
, wxTheApp
->GetAppDisplayName());
315 wxLogGui::DoShowSingleLogMessage(const wxString
& message
,
316 const wxString
& title
,
319 wxMessageBox(message
, title
, wxOK
| style
);
323 wxLogGui::DoShowMultipleLogMessages(const wxArrayString
& messages
,
324 const wxArrayInt
& severities
,
325 const wxArrayLong
& times
,
326 const wxString
& title
,
330 wxLogDialog
dlg(NULL
,
331 messages
, severities
, times
,
334 // clear the message list before showing the dialog because while it's
335 // shown some new messages may appear
338 (void)dlg
.ShowModal();
339 #else // !wxUSE_LOG_DIALOG
340 // start from the most recent message
342 const size_t nMsgCount
= messages
.size();
343 message
.reserve(nMsgCount
*100);
344 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
345 message
<< m_aMessages
[n
- 1] << wxT("\n");
348 DoShowSingleLogMessage(message
, title
, style
);
349 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
352 void wxLogGui::Flush()
354 if ( !m_bHasMessages
)
357 // do it right now to block any new calls to Flush() while we're here
358 m_bHasMessages
= false;
360 // note that this must be done before examining m_aMessages as it may log
361 // yet another message
362 const unsigned repeatCount
= LogLastRepeatIfNeeded();
364 const size_t nMsgCount
= m_aMessages
.size();
366 if ( repeatCount
> 0 )
368 m_aMessages
[nMsgCount
- 1] << " (" << m_aMessages
[nMsgCount
- 2] << ")";
371 const wxString title
= GetTitle();
372 const int style
= GetSeverityIcon();
374 // avoid showing other log dialogs until we're done with the dialog we're
375 // showing right now: nested modal dialogs make for really bad UI!
378 if ( nMsgCount
== 1 )
380 // make a copy before calling Clear()
381 const wxString
message(m_aMessages
[0]);
384 DoShowSingleLogMessage(message
, title
, style
);
386 else // more than one message
388 wxArrayString messages
;
389 wxArrayInt severities
;
392 messages
.swap(m_aMessages
);
393 severities
.swap(m_aSeverity
);
394 times
.swap(m_aTimes
);
398 DoShowMultipleLogMessages(messages
, severities
, times
, title
, style
);
401 // allow flushing the logs again
405 // log all kinds of messages
406 void wxLogGui::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
413 m_aMessages
.Add(szString
);
414 m_aSeverity
.Add(wxLOG_Message
);
415 m_aTimes
.Add((long)t
);
416 m_bHasMessages
= true;
423 // find the top window and set it's status text if it has any
424 wxFrame
*pFrame
= gs_pFrame
;
425 if ( pFrame
== NULL
) {
426 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
427 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
428 pFrame
= (wxFrame
*)pWin
;
432 if ( pFrame
&& pFrame
->GetStatusBar() )
433 pFrame
->SetStatusText(szString
);
435 #endif // wxUSE_STATUSBAR
438 case wxLOG_FatalError
:
439 // show this one immediately
440 wxMessageBox(szString
, _("Fatal error"), wxICON_HAND
);
446 #if !wxUSE_LOG_DIALOG
447 // discard earlier informational messages if this is the 1st
448 // error because they might not make sense any more and showing
449 // them in a message box might be confusing
453 #endif // wxUSE_LOG_DIALOG
460 // for the warning we don't discard the info messages
464 m_aMessages
.Add(szString
);
465 m_aSeverity
.Add((int)level
);
466 m_aTimes
.Add((long)t
);
467 m_bHasMessages
= true;
471 // let the base class deal with debug/trace messages as well as any
473 wxLog::DoLog(level
, szString
, t
);
477 #endif // wxUSE_LOGGUI
479 // ----------------------------------------------------------------------------
480 // wxLogWindow and wxLogFrame implementation
481 // ----------------------------------------------------------------------------
487 class wxLogFrame
: public wxFrame
491 wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
);
492 virtual ~wxLogFrame();
495 void OnClose(wxCommandEvent
& event
);
496 void OnCloseWindow(wxCloseEvent
& event
);
498 void OnSave(wxCommandEvent
& event
);
499 #endif // CAN_SAVE_FILES
500 void OnClear(wxCommandEvent
& event
);
502 // this function is safe to call from any thread (notice that it should be
503 // also called from the main thread to ensure that the messages logged from
504 // it appear in correct order with the messages from the other threads)
505 void AddLogMessage(const wxString
& message
);
507 // actually append the messages logged from secondary threads to the text
508 // control during idle time in the main thread
509 virtual void OnInternalIdle();
512 // use standard ids for our commands!
515 Menu_Close
= wxID_CLOSE
,
516 Menu_Save
= wxID_SAVE
,
517 Menu_Clear
= wxID_CLEAR
520 // common part of OnClose() and OnCloseWindow()
523 // do show the message in the text control
524 void DoShowLogMessage(const wxString
& message
)
526 m_pTextCtrl
->AppendText(message
);
529 wxTextCtrl
*m_pTextCtrl
;
532 // queue of messages logged from other threads which need to be displayed
533 wxArrayString m_pendingMessages
;
536 // critical section to protect access to m_pendingMessages
537 wxCriticalSection m_critSection
;
538 #endif // wxUSE_THREADS
541 DECLARE_EVENT_TABLE()
542 wxDECLARE_NO_COPY_CLASS(wxLogFrame
);
545 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
546 // wxLogWindow menu events
547 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
549 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
550 #endif // CAN_SAVE_FILES
551 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
553 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
556 wxLogFrame::wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
)
557 : wxFrame(pParent
, wxID_ANY
, szTitle
)
561 m_pTextCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
565 // needed for Win32 to avoid 65Kb limit but it doesn't work well
566 // when using RichEdit 2.0 which we always do in the Unicode build
569 #endif // !wxUSE_UNICODE
574 wxMenuBar
*pMenuBar
= new wxMenuBar
;
575 wxMenu
*pMenu
= new wxMenu
;
577 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
578 #endif // CAN_SAVE_FILES
579 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
580 pMenu
->AppendSeparator();
581 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
582 pMenuBar
->Append(pMenu
, _("&Log"));
583 SetMenuBar(pMenuBar
);
584 #endif // wxUSE_MENUS
587 // status bar for menu prompts
589 #endif // wxUSE_STATUSBAR
591 m_log
->OnFrameCreate(this);
594 void wxLogFrame::DoClose()
596 if ( m_log
->OnFrameClose(this) )
598 // instead of closing just hide the window to be able to Show() it
604 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
609 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
615 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
619 int rc
= OpenLogFile(file
, &filename
, this);
628 // retrieve text and save it
629 // -------------------------
630 int nLines
= m_pTextCtrl
->GetNumberOfLines();
631 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
632 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
633 wxTextFile::GetEOL());
640 wxLogError(_("Can't save log contents to file."));
643 wxLogStatus((wxFrame
*)this, _("Log saved to the file '%s'."), filename
.c_str());
646 #endif // CAN_SAVE_FILES
648 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
650 m_pTextCtrl
->Clear();
653 void wxLogFrame::OnInternalIdle()
656 wxCRIT_SECT_LOCKER(locker
, m_critSection
);
658 const size_t count
= m_pendingMessages
.size();
659 for ( size_t n
= 0; n
< count
; n
++ )
661 DoShowLogMessage(m_pendingMessages
[n
]);
664 m_pendingMessages
.clear();
665 } // release m_critSection
667 wxFrame::OnInternalIdle();
670 void wxLogFrame::AddLogMessage(const wxString
& message
)
672 wxCRIT_SECT_LOCKER(locker
, m_critSection
);
675 if ( !wxThread::IsMain() || !m_pendingMessages
.empty() )
677 // message needs to be queued for later showing
678 m_pendingMessages
.Add(message
);
682 else // we are the main thread and no messages are queued, so we can
683 // log the message directly
684 #endif // wxUSE_THREADS
686 DoShowLogMessage(message
);
690 wxLogFrame::~wxLogFrame()
692 m_log
->OnFrameDelete(this);
698 wxLogWindow::wxLogWindow(wxWindow
*pParent
,
699 const wxString
& szTitle
,
703 PassMessages(bDoPass
);
705 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
711 void wxLogWindow::Show(bool bShow
)
713 m_pLogFrame
->Show(bShow
);
716 void wxLogWindow::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
718 // first let the previous logger show it
719 wxLogPassThrough::DoLog(level
, szString
, t
);
724 // by default, these messages are ignored by wxLog, so process
726 if ( !szString
.empty() )
729 str
<< _("Status: ") << szString
;
734 // don't put trace messages in the text window for 2 reasons:
735 // 1) there are too many of them
736 // 2) they may provoke other trace messages thus sending a program
737 // into an infinite loop
742 // and this will format it nicely and call our DoLogString()
743 wxLog::DoLog(level
, szString
, t
);
748 void wxLogWindow::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
753 msg
<< szString
<< wxT('\n');
755 m_pLogFrame
->AddLogMessage(msg
);
758 wxFrame
*wxLogWindow::GetFrame() const
763 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
767 bool wxLogWindow::OnFrameClose(wxFrame
* WXUNUSED(frame
))
773 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
778 wxLogWindow::~wxLogWindow()
780 // may be NULL if log frame already auto destroyed itself
784 #endif // wxUSE_LOGWINDOW
786 // ----------------------------------------------------------------------------
788 // ----------------------------------------------------------------------------
792 wxString
wxLogDialog::ms_details
;
793 size_t wxLogDialog::ms_maxLength
= 0;
795 wxLogDialog::wxLogDialog(wxWindow
*parent
,
796 const wxArrayString
& messages
,
797 const wxArrayInt
& severity
,
798 const wxArrayLong
& times
,
799 const wxString
& caption
,
801 : wxDialog(parent
, wxID_ANY
, caption
,
802 wxDefaultPosition
, wxDefaultSize
,
803 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
805 // init the static variables:
807 if ( ms_details
.empty() )
809 // ensure that we won't loop here if wxGetTranslation()
810 // happens to pop up a Log message while translating this :-)
811 ms_details
= wxTRANSLATE("&Details");
812 ms_details
= wxGetTranslation(ms_details
);
813 #ifdef __SMARTPHONE__
814 ms_details
= wxStripMenuCodes(ms_details
);
818 if ( ms_maxLength
== 0 )
820 ms_maxLength
= (2 * wxGetDisplaySize().x
/3) / GetCharWidth();
823 size_t count
= messages
.GetCount();
824 m_messages
.Alloc(count
);
825 m_severity
.Alloc(count
);
826 m_times
.Alloc(count
);
828 for ( size_t n
= 0; n
< count
; n
++ )
830 m_messages
.Add(messages
[n
]);
831 m_severity
.Add(severity
[n
]);
832 m_times
.Add(times
[n
]);
837 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
839 // create the controls which are always shown and layout them: we use
840 // sizers even though our window is not resizeable to calculate the size of
841 // the dialog properly
842 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
843 wxBoxSizer
*sizerAll
= new wxBoxSizer(isPda
? wxVERTICAL
: wxHORIZONTAL
);
847 wxStaticBitmap
*icon
= new wxStaticBitmap
851 wxArtProvider::GetMessageBoxIcon(style
)
853 sizerAll
->Add(icon
, wxSizerFlags().Centre());
856 // create the text sizer with a minimal size so that we are sure it won't be too small
857 wxString message
= EllipsizeString(messages
.Last());
858 wxSizer
*szText
= CreateTextSizer(message
);
859 szText
->SetMinSize(wxMin(300, wxGetDisplaySize().x
/ 3), -1);
861 sizerAll
->Add(szText
, wxSizerFlags(1).Centre().Border(wxLEFT
| wxRIGHT
));
863 wxButton
*btnOk
= new wxButton(this, wxID_OK
);
864 sizerAll
->Add(btnOk
, wxSizerFlags().Centre());
866 sizerTop
->Add(sizerAll
, wxSizerFlags().Expand().Border());
869 // add the details pane
870 #ifndef __SMARTPHONE__
871 wxCollapsiblePane
* const
872 collpane
= new wxCollapsiblePane(this, wxID_ANY
, ms_details
);
873 sizerTop
->Add(collpane
, wxSizerFlags(1).Expand().Border());
875 wxWindow
*win
= collpane
->GetPane();
876 wxSizer
* const paneSz
= new wxBoxSizer(wxVERTICAL
);
878 CreateDetailsControls(win
);
880 paneSz
->Add(m_listctrl
, wxSizerFlags(1).Expand().Border(wxTOP
));
882 #if wxUSE_CLIPBOARD || CAN_SAVE_FILES
883 wxBoxSizer
* const btnSizer
= new wxBoxSizer(wxHORIZONTAL
);
885 wxSizerFlags flagsBtn
;
886 flagsBtn
.Border(wxLEFT
);
889 btnSizer
->Add(new wxButton(win
, wxID_COPY
), flagsBtn
);
890 #endif // wxUSE_CLIPBOARD
893 btnSizer
->Add(new wxButton(win
, wxID_SAVE
), flagsBtn
);
894 #endif // CAN_SAVE_FILES
896 paneSz
->Add(btnSizer
, wxSizerFlags().Right().Border(wxTOP
));
897 #endif // wxUSE_CLIPBOARD || CAN_SAVE_FILES
899 win
->SetSizer(paneSz
);
900 paneSz
->SetSizeHints(win
);
901 #else // __SMARTPHONE__
902 SetLeftMenu(wxID_OK
);
903 SetRightMenu(wxID_MORE
, ms_details
+ EXPAND_SUFFIX
);
904 #endif // __SMARTPHONE__/!__SMARTPHONE__
906 SetSizerAndFit(sizerTop
);
912 // Move up the screen so that when we expand the dialog,
913 // there's enough space.
914 Move(wxPoint(GetPosition().x
, GetPosition().y
/ 2));
918 void wxLogDialog::CreateDetailsControls(wxWindow
*parent
)
920 wxString fmt
= wxLog::GetTimestamp();
921 bool hasTimeStamp
= !fmt
.IsEmpty();
923 // create the list ctrl now
924 m_listctrl
= new wxListCtrl(parent
, wxID_ANY
,
925 wxDefaultPosition
, wxDefaultSize
,
931 // This makes a big aesthetic difference on WinCE but I
932 // don't want to risk problems on other platforms
936 // no need to translate these strings as they're not shown to the
937 // user anyhow (we use wxLC_NO_HEADER style)
938 m_listctrl
->InsertColumn(0, _T("Message"));
941 m_listctrl
->InsertColumn(1, _T("Time"));
943 // prepare the imagelist
944 static const int ICON_SIZE
= 16;
945 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
947 // order should be the same as in the switch below!
948 static const wxChar
* icons
[] =
955 bool loadedIcons
= true;
957 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
959 wxBitmap bmp
= wxArtProvider::GetBitmap(icons
[icon
], wxART_MESSAGE_BOX
,
960 wxSize(ICON_SIZE
, ICON_SIZE
));
962 // This may very well fail if there are insufficient colours available.
963 // Degrade gracefully.
974 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
977 size_t count
= m_messages
.GetCount();
978 for ( size_t n
= 0; n
< count
; n
++ )
984 switch ( m_severity
[n
] )
998 else // failed to load images
1003 wxString msg
= m_messages
[n
];
1004 msg
.Replace(wxT("\n"), wxT(" "));
1005 msg
= EllipsizeString(msg
);
1007 m_listctrl
->InsertItem(n
, msg
, image
);
1010 m_listctrl
->SetItem(n
, 1, TimeStamp(fmt
, (time_t)m_times
[n
]));
1013 // let the columns size themselves
1014 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
1016 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
1018 // calculate an approximately nice height for the listctrl
1019 int height
= GetCharHeight()*(count
+ 4);
1021 // but check that the dialog won't fall fown from the screen
1023 // we use GetMinHeight() to get the height of the dialog part without the
1024 // details and we consider that the "Save" button below and the separator
1025 // line (and the margins around it) take about as much, hence double it
1026 int heightMax
= wxGetDisplaySize().y
- GetPosition().y
- 2*GetMinHeight();
1028 // we should leave a margin
1032 m_listctrl
->SetSize(wxDefaultCoord
, wxMin(height
, heightMax
));
1035 void wxLogDialog::OnListItemActivated(wxListEvent
& event
)
1037 // show the activated item in a message box
1038 // This allow the user to correctly display the logs which are longer
1039 // than the listctrl and thus gets truncated or those which contains
1043 // wxString str = m_listctrl->GetItemText(event.GetIndex());
1044 // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW.
1045 wxString str
= m_messages
[event
.GetIndex()];
1047 // wxMessageBox will nicely handle the '\n' in the string (if any)
1048 // and supports long strings
1049 wxMessageBox(str
, wxT("Log message"), wxOK
, this);
1052 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
1057 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
1059 wxString
wxLogDialog::GetLogMessages() const
1061 wxString fmt
= wxLog::GetTimestamp();
1064 // use the default format
1068 const size_t count
= m_messages
.GetCount();
1071 text
.reserve(count
*m_messages
[0].length());
1072 for ( size_t n
= 0; n
< count
; n
++ )
1074 text
<< TimeStamp(fmt
, (time_t)m_times
[n
])
1077 << wxTextFile::GetEOL();
1083 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
1087 void wxLogDialog::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1089 wxClipboardLocker clip
;
1091 !wxTheClipboard
->AddData(new wxTextDataObject(GetLogMessages())) )
1093 wxLogError(_("Failed to copy dialog contents to the clipboard."));
1097 #endif // wxUSE_CLIPBOARD
1101 void wxLogDialog::OnSave(wxCommandEvent
& WXUNUSED(event
))
1104 int rc
= OpenLogFile(file
, NULL
, this);
1111 if ( !rc
|| !file
.Write(GetLogMessages()) || !file
.Close() )
1112 wxLogError(_("Can't save log contents to file."));
1115 #endif // CAN_SAVE_FILES
1117 wxLogDialog::~wxLogDialog()
1121 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
1125 #endif // wxUSE_LOG_DIALOG
1129 // pass an uninitialized file object, the function will ask the user for the
1130 // filename and try to open it, returns true on success (file was opened),
1131 // false if file couldn't be opened/created and -1 if the file selection
1132 // dialog was cancelled
1133 static int OpenLogFile(wxFile
& file
, wxString
*pFilename
, wxWindow
*parent
)
1135 // get the file name
1136 // -----------------
1137 wxString filename
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent
);
1146 if ( wxFile::Exists(filename
) ) {
1147 bool bAppend
= false;
1149 strMsg
.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
1151 switch ( wxMessageBox(strMsg
, _("Question"),
1152 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
) ) {
1165 wxFAIL_MSG(_("invalid message box return value"));
1169 bOk
= file
.Open(filename
, wxFile::write_append
);
1172 bOk
= file
.Create(filename
, true /* overwrite */);
1176 bOk
= file
.Create(filename
);
1180 *pFilename
= filename
;
1185 #endif // CAN_SAVE_FILES
1187 #endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
1189 #if wxUSE_LOG && wxUSE_TEXTCTRL
1191 // ----------------------------------------------------------------------------
1192 // wxLogTextCtrl implementation
1193 // ----------------------------------------------------------------------------
1195 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
1197 m_pTextCtrl
= pTextCtrl
;
1200 void wxLogTextCtrl::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
1205 msg
<< szString
<< wxT('\n');
1206 m_pTextCtrl
->AppendText(msg
);
1209 #endif // wxUSE_LOG && wxUSE_TEXTCTRL