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();
229 msg
.PrintfV(format
, argptr
);
231 wxASSERT( gs_pFrame
== NULL
); // should be reset!
233 wxLog
::OnLog(wxLOG_Status
, msg
);
238 #if !wxUSE_UTF8_LOCALE_ONLY
239 void wxDoLogStatusWchar(wxFrame
*pFrame
, const wxChar
*format
, ...)
242 va_start(argptr
, format
);
243 wxVLogStatus(pFrame
, format
, argptr
);
246 #endif // !wxUSE_UTF8_LOCALE_ONLY
248 #if wxUSE_UNICODE_UTF8
249 void wxDoLogStatusUtf8(wxFrame
*pFrame
, const char *format
, ...)
252 va_start(argptr
, format
);
253 wxVLogStatus(pFrame
, format
, argptr
);
256 #endif // wxUSE_UNICODE_UTF8
258 // ----------------------------------------------------------------------------
259 // wxLogGui implementation (FIXME MT-unsafe)
260 // ----------------------------------------------------------------------------
269 void wxLogGui
::Clear()
273 m_bHasMessages
= false;
280 int wxLogGui
::GetSeverityIcon() const
282 return m_bErrors ? wxICON_STOP
283 : m_bWarnings ? wxICON_EXCLAMATION
284 : wxICON_INFORMATION
;
287 wxString wxLogGui
::GetTitle() const
289 wxString titleFormat
;
290 switch ( GetSeverityIcon() )
293 titleFormat
= _("%s Error");
296 case wxICON_EXCLAMATION
:
297 titleFormat
= _("%s Warning");
301 wxFAIL_MSG( "unexpected icon severity" );
304 case wxICON_INFORMATION
:
305 titleFormat
= _("%s Information");
308 return wxString
::Format(titleFormat
, wxTheApp
->GetAppDisplayName());
312 wxLogGui
::DoShowSingleLogMessage(const wxString
& message
,
313 const wxString
& title
,
316 wxMessageBox(message
, title
, wxOK
| style
);
320 wxLogGui
::DoShowMultipleLogMessages(const wxArrayString
& messages
,
321 const wxArrayInt
& severities
,
322 const wxArrayLong
& times
,
323 const wxString
& title
,
327 wxLogDialog
dlg(NULL
,
328 messages
, severities
, times
,
331 // clear the message list before showing the dialog because while it's
332 // shown some new messages may appear
335 (void)dlg
.ShowModal();
336 #else // !wxUSE_LOG_DIALOG
337 // start from the most recent message
339 const size_t nMsgCount
= messages
.size();
340 message
.reserve(nMsgCount
*100);
341 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
342 message
<< m_aMessages
[n
- 1] << wxT("\n");
345 DoShowSingleLogMessage(message
, title
, style
);
346 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
349 void wxLogGui
::Flush()
351 if ( !m_bHasMessages
)
354 // do it right now to block any new calls to Flush() while we're here
355 m_bHasMessages
= false;
357 // note that this must be done before examining m_aMessages as it may log
358 // yet another message
359 const unsigned repeatCount
= LogLastRepeatIfNeeded();
361 const size_t nMsgCount
= m_aMessages
.size();
363 if ( repeatCount
> 0 )
365 m_aMessages
[nMsgCount
- 1] << " (" << m_aMessages
[nMsgCount
- 2] << ")";
368 const wxString title
= GetTitle();
369 const int style
= GetSeverityIcon();
371 // avoid showing other log dialogs until we're done with the dialog we're
372 // showing right now: nested modal dialogs make for really bad UI!
375 if ( nMsgCount
== 1 )
377 // make a copy before calling Clear()
378 const wxString
message(m_aMessages
[0]);
381 DoShowSingleLogMessage(message
, title
, style
);
383 else // more than one message
385 wxArrayString messages
;
386 wxArrayInt severities
;
389 messages
.swap(m_aMessages
);
390 severities
.swap(m_aSeverity
);
391 times
.swap(m_aTimes
);
395 DoShowMultipleLogMessages(messages
, severities
, times
, title
, style
);
398 // allow flushing the logs again
402 // log all kinds of messages
403 void wxLogGui
::DoLogRecord(wxLogLevel level
,
405 const wxLogRecordInfo
& info
)
413 m_aMessages
.Add(msg
);
414 m_aSeverity
.Add(wxLOG_Message
);
415 m_aTimes
.Add((long)info
.timestamp
);
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(msg
);
435 #endif // wxUSE_STATUSBAR
440 #if !wxUSE_LOG_DIALOG
441 // discard earlier informational messages if this is the 1st
442 // error because they might not make sense any more and showing
443 // them in a message box might be confusing
447 #endif // wxUSE_LOG_DIALOG
454 // for the warning we don't discard the info messages
458 m_aMessages
.Add(msg
);
459 m_aSeverity
.Add((int)level
);
460 m_aTimes
.Add((long)info
.timestamp
);
461 m_bHasMessages
= true;
465 // let the base class deal with debug/trace messages as well as any
467 wxLog
::DoLogRecord(level
, msg
, info
);
471 #endif // wxUSE_LOGGUI
473 // ----------------------------------------------------------------------------
474 // wxLogWindow and wxLogFrame implementation
475 // ----------------------------------------------------------------------------
481 class wxLogFrame
: public wxFrame
485 wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
);
486 virtual ~wxLogFrame();
489 void OnClose(wxCommandEvent
& event
);
490 void OnCloseWindow(wxCloseEvent
& event
);
492 void OnSave(wxCommandEvent
& event
);
493 #endif // CAN_SAVE_FILES
494 void OnClear(wxCommandEvent
& event
);
496 // this function is safe to call from any thread (notice that it should be
497 // also called from the main thread to ensure that the messages logged from
498 // it appear in correct order with the messages from the other threads)
499 void AddLogMessage(const wxString
& message
);
501 // actually append the messages logged from secondary threads to the text
502 // control during idle time in the main thread
503 virtual void OnInternalIdle();
506 // use standard ids for our commands!
509 Menu_Close
= wxID_CLOSE
,
510 Menu_Save
= wxID_SAVE
,
511 Menu_Clear
= wxID_CLEAR
514 // common part of OnClose() and OnCloseWindow()
517 // do show the message in the text control
518 void DoShowLogMessage(const wxString
& message
)
520 m_pTextCtrl
->AppendText(message
+ wxS('\n'));
523 wxTextCtrl
*m_pTextCtrl
;
526 // queue of messages logged from other threads which need to be displayed
527 wxArrayString m_pendingMessages
;
530 // critical section to protect access to m_pendingMessages
531 wxCriticalSection m_critSection
;
532 #endif // wxUSE_THREADS
535 DECLARE_EVENT_TABLE()
536 wxDECLARE_NO_COPY_CLASS(wxLogFrame
);
539 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
540 // wxLogWindow menu events
541 EVT_MENU(Menu_Close
, wxLogFrame
::OnClose
)
543 EVT_MENU(Menu_Save
, wxLogFrame
::OnSave
)
544 #endif // CAN_SAVE_FILES
545 EVT_MENU(Menu_Clear
, wxLogFrame
::OnClear
)
547 EVT_CLOSE(wxLogFrame
::OnCloseWindow
)
550 wxLogFrame
::wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
)
551 : wxFrame(pParent
, wxID_ANY
, szTitle
)
555 m_pTextCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
559 // needed for Win32 to avoid 65Kb limit but it doesn't work well
560 // when using RichEdit 2.0 which we always do in the Unicode build
563 #endif // !wxUSE_UNICODE
568 wxMenuBar
*pMenuBar
= new wxMenuBar
;
569 wxMenu
*pMenu
= new wxMenu
;
571 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
572 #endif // CAN_SAVE_FILES
573 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
574 pMenu
->AppendSeparator();
575 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
576 pMenuBar
->Append(pMenu
, _("&Log"));
577 SetMenuBar(pMenuBar
);
578 #endif // wxUSE_MENUS
581 // status bar for menu prompts
583 #endif // wxUSE_STATUSBAR
585 m_log
->OnFrameCreate(this);
588 void wxLogFrame
::DoClose()
590 if ( m_log
->OnFrameClose(this) )
592 // instead of closing just hide the window to be able to Show() it
598 void wxLogFrame
::OnClose(wxCommandEvent
& WXUNUSED(event
))
603 void wxLogFrame
::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
609 void wxLogFrame
::OnSave(wxCommandEvent
& WXUNUSED(event
))
613 int rc
= OpenLogFile(file
, &filename
, this);
622 // retrieve text and save it
623 // -------------------------
624 int nLines
= m_pTextCtrl
->GetNumberOfLines();
625 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
626 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
627 wxTextFile
::GetEOL());
634 wxLogError(_("Can't save log contents to file."));
637 wxLogStatus((wxFrame
*)this, _("Log saved to the file '%s'."), filename
.c_str());
640 #endif // CAN_SAVE_FILES
642 void wxLogFrame
::OnClear(wxCommandEvent
& WXUNUSED(event
))
644 m_pTextCtrl
->Clear();
647 void wxLogFrame
::OnInternalIdle()
650 wxCRIT_SECT_LOCKER(locker
, m_critSection
);
652 const size_t count
= m_pendingMessages
.size();
653 for ( size_t n
= 0; n
< count
; n
++ )
655 DoShowLogMessage(m_pendingMessages
[n
]);
658 m_pendingMessages
.clear();
659 } // release m_critSection
661 wxFrame
::OnInternalIdle();
664 void wxLogFrame
::AddLogMessage(const wxString
& message
)
666 wxCRIT_SECT_LOCKER(locker
, m_critSection
);
669 if ( !wxThread
::IsMain() || !m_pendingMessages
.empty() )
671 // message needs to be queued for later showing
672 m_pendingMessages
.Add(message
);
676 else // we are the main thread and no messages are queued, so we can
677 // log the message directly
678 #endif // wxUSE_THREADS
680 DoShowLogMessage(message
);
684 wxLogFrame
::~wxLogFrame()
686 m_log
->OnFrameDelete(this);
692 wxLogWindow
::wxLogWindow(wxWindow
*pParent
,
693 const wxString
& szTitle
,
697 PassMessages(bDoPass
);
699 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
705 void wxLogWindow
::Show(bool bShow
)
707 m_pLogFrame
->Show(bShow
);
710 void wxLogWindow
::DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
712 // first let the previous logger show it
713 wxLogPassThrough
::DoLogTextAtLevel(level
, msg
);
718 // don't put trace messages in the text window for 2 reasons:
719 // 1) there are too many of them
720 // 2) they may provoke other trace messages (e.g. wxMSW code uses
721 // wxLogTrace to log Windows messages and adding text to the control
722 // sends more of them) thus sending a program into an infinite loop
723 if ( level
== wxLOG_Trace
)
726 m_pLogFrame
->AddLogMessage(msg
);
729 wxFrame
*wxLogWindow
::GetFrame() const
734 void wxLogWindow
::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
738 bool wxLogWindow
::OnFrameClose(wxFrame
* WXUNUSED(frame
))
744 void wxLogWindow
::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
749 wxLogWindow
::~wxLogWindow()
751 // may be NULL if log frame already auto destroyed itself
755 #endif // wxUSE_LOGWINDOW
757 // ----------------------------------------------------------------------------
759 // ----------------------------------------------------------------------------
763 wxString wxLogDialog
::ms_details
;
764 size_t wxLogDialog
::ms_maxLength
= 0;
766 wxLogDialog
::wxLogDialog(wxWindow
*parent
,
767 const wxArrayString
& messages
,
768 const wxArrayInt
& severity
,
769 const wxArrayLong
& times
,
770 const wxString
& caption
,
772 : wxDialog(parent
, wxID_ANY
, caption
,
773 wxDefaultPosition
, wxDefaultSize
,
774 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
776 // init the static variables:
778 if ( ms_details
.empty() )
780 // ensure that we won't loop here if wxGetTranslation()
781 // happens to pop up a Log message while translating this :-)
782 ms_details
= wxTRANSLATE("&Details");
783 ms_details
= wxGetTranslation(ms_details
);
784 #ifdef __SMARTPHONE__
785 ms_details
= wxStripMenuCodes(ms_details
);
789 if ( ms_maxLength
== 0 )
791 ms_maxLength
= (2 * wxGetDisplaySize().x
/3) / GetCharWidth();
794 size_t count
= messages
.GetCount();
795 m_messages
.Alloc(count
);
796 m_severity
.Alloc(count
);
797 m_times
.Alloc(count
);
799 for ( size_t n
= 0; n
< count
; n
++ )
801 m_messages
.Add(messages
[n
]);
802 m_severity
.Add(severity
[n
]);
803 m_times
.Add(times
[n
]);
808 bool isPda
= (wxSystemSettings
::GetScreenType() <= wxSYS_SCREEN_PDA
);
810 // create the controls which are always shown and layout them: we use
811 // sizers even though our window is not resizeable to calculate the size of
812 // the dialog properly
813 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
814 wxBoxSizer
*sizerAll
= new wxBoxSizer(isPda ? wxVERTICAL
: wxHORIZONTAL
);
818 wxStaticBitmap
*icon
= new wxStaticBitmap
822 wxArtProvider
::GetMessageBoxIcon(style
)
824 sizerAll
->Add(icon
, wxSizerFlags().Centre());
827 // create the text sizer with a minimal size so that we are sure it won't be too small
828 wxString message
= EllipsizeString(messages
.Last());
829 wxSizer
*szText
= CreateTextSizer(message
);
830 szText
->SetMinSize(wxMin(300, wxGetDisplaySize().x
/ 3), -1);
832 sizerAll
->Add(szText
, wxSizerFlags(1).Centre().Border(wxLEFT
| wxRIGHT
));
834 wxButton
*btnOk
= new wxButton(this, wxID_OK
);
835 sizerAll
->Add(btnOk
, wxSizerFlags().Centre());
837 sizerTop
->Add(sizerAll
, wxSizerFlags().Expand().Border());
840 // add the details pane
841 #ifndef __SMARTPHONE__
842 wxCollapsiblePane
* const
843 collpane
= new wxCollapsiblePane(this, wxID_ANY
, ms_details
);
844 sizerTop
->Add(collpane
, wxSizerFlags(1).Expand().Border());
846 wxWindow
*win
= collpane
->GetPane();
847 wxSizer
* const paneSz
= new wxBoxSizer(wxVERTICAL
);
849 CreateDetailsControls(win
);
851 paneSz
->Add(m_listctrl
, wxSizerFlags(1).Expand().Border(wxTOP
));
853 #if wxUSE_CLIPBOARD || CAN_SAVE_FILES
854 wxBoxSizer
* const btnSizer
= new wxBoxSizer(wxHORIZONTAL
);
856 wxSizerFlags flagsBtn
;
857 flagsBtn
.Border(wxLEFT
);
860 btnSizer
->Add(new wxButton(win
, wxID_COPY
), flagsBtn
);
861 #endif // wxUSE_CLIPBOARD
864 btnSizer
->Add(new wxButton(win
, wxID_SAVE
), flagsBtn
);
865 #endif // CAN_SAVE_FILES
867 paneSz
->Add(btnSizer
, wxSizerFlags().Right().Border(wxTOP
));
868 #endif // wxUSE_CLIPBOARD || CAN_SAVE_FILES
870 win
->SetSizer(paneSz
);
871 paneSz
->SetSizeHints(win
);
872 #else // __SMARTPHONE__
873 SetLeftMenu(wxID_OK
);
874 SetRightMenu(wxID_MORE
, ms_details
+ EXPAND_SUFFIX
);
875 #endif // __SMARTPHONE__/!__SMARTPHONE__
877 SetSizerAndFit(sizerTop
);
883 // Move up the screen so that when we expand the dialog,
884 // there's enough space.
885 Move(wxPoint(GetPosition().x
, GetPosition().y
/ 2));
889 void wxLogDialog
::CreateDetailsControls(wxWindow
*parent
)
891 wxString fmt
= wxLog
::GetTimestamp();
892 bool hasTimeStamp
= !fmt
.IsEmpty();
894 // create the list ctrl now
895 m_listctrl
= new wxListCtrl(parent
, wxID_ANY
,
896 wxDefaultPosition
, wxDefaultSize
,
902 // This makes a big aesthetic difference on WinCE but I
903 // don't want to risk problems on other platforms
907 // no need to translate these strings as they're not shown to the
908 // user anyhow (we use wxLC_NO_HEADER style)
909 m_listctrl
->InsertColumn(0, _T("Message"));
912 m_listctrl
->InsertColumn(1, _T("Time"));
914 // prepare the imagelist
915 static const int ICON_SIZE
= 16;
916 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
918 // order should be the same as in the switch below!
919 static const wxChar
* icons
[] =
926 bool loadedIcons
= true;
928 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
930 wxBitmap bmp
= wxArtProvider
::GetBitmap(icons
[icon
], wxART_MESSAGE_BOX
,
931 wxSize(ICON_SIZE
, ICON_SIZE
));
933 // This may very well fail if there are insufficient colours available.
934 // Degrade gracefully.
945 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
948 size_t count
= m_messages
.GetCount();
949 for ( size_t n
= 0; n
< count
; n
++ )
955 switch ( m_severity
[n
] )
969 else // failed to load images
974 wxString msg
= m_messages
[n
];
975 msg
.Replace(wxT("\n"), wxT(" "));
976 msg
= EllipsizeString(msg
);
978 m_listctrl
->InsertItem(n
, msg
, image
);
981 m_listctrl
->SetItem(n
, 1, TimeStamp(fmt
, (time_t)m_times
[n
]));
984 // let the columns size themselves
985 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
987 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
989 // calculate an approximately nice height for the listctrl
990 int height
= GetCharHeight()*(count
+ 4);
992 // but check that the dialog won't fall fown from the screen
994 // we use GetMinHeight() to get the height of the dialog part without the
995 // details and we consider that the "Save" button below and the separator
996 // line (and the margins around it) take about as much, hence double it
997 int heightMax
= wxGetDisplaySize().y
- GetPosition().y
- 2*GetMinHeight();
999 // we should leave a margin
1003 m_listctrl
->SetSize(wxDefaultCoord
, wxMin(height
, heightMax
));
1006 void wxLogDialog
::OnListItemActivated(wxListEvent
& event
)
1008 // show the activated item in a message box
1009 // This allow the user to correctly display the logs which are longer
1010 // than the listctrl and thus gets truncated or those which contains
1014 // wxString str = m_listctrl->GetItemText(event.GetIndex());
1015 // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW.
1016 wxString str
= m_messages
[event
.GetIndex()];
1018 // wxMessageBox will nicely handle the '\n' in the string (if any)
1019 // and supports long strings
1020 wxMessageBox(str
, wxT("Log message"), wxOK
, this);
1023 void wxLogDialog
::OnOk(wxCommandEvent
& WXUNUSED(event
))
1028 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
1030 wxString wxLogDialog
::GetLogMessages() const
1032 wxString fmt
= wxLog
::GetTimestamp();
1035 // use the default format
1039 const size_t count
= m_messages
.GetCount();
1042 text
.reserve(count
*m_messages
[0].length());
1043 for ( size_t n
= 0; n
< count
; n
++ )
1045 text
<< TimeStamp(fmt
, (time_t)m_times
[n
])
1048 << wxTextFile
::GetEOL();
1054 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
1058 void wxLogDialog
::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1060 wxClipboardLocker clip
;
1062 !wxTheClipboard
->AddData(new wxTextDataObject(GetLogMessages())) )
1064 wxLogError(_("Failed to copy dialog contents to the clipboard."));
1068 #endif // wxUSE_CLIPBOARD
1072 void wxLogDialog
::OnSave(wxCommandEvent
& WXUNUSED(event
))
1075 int rc
= OpenLogFile(file
, NULL
, this);
1082 if ( !rc
|| !file
.Write(GetLogMessages()) || !file
.Close() )
1083 wxLogError(_("Can't save log contents to file."));
1086 #endif // CAN_SAVE_FILES
1088 wxLogDialog
::~wxLogDialog()
1092 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
1096 #endif // wxUSE_LOG_DIALOG
1100 // pass an uninitialized file object, the function will ask the user for the
1101 // filename and try to open it, returns true on success (file was opened),
1102 // false if file couldn't be opened/created and -1 if the file selection
1103 // dialog was cancelled
1104 static int OpenLogFile(wxFile
& file
, wxString
*pFilename
, wxWindow
*parent
)
1106 // get the file name
1107 // -----------------
1108 wxString filename
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent
);
1116 bool bOk
= true; // suppress warning about it being possible uninitialized
1117 if ( wxFile
::Exists(filename
) ) {
1118 bool bAppend
= false;
1120 strMsg
.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
1122 switch ( wxMessageBox(strMsg
, _("Question"),
1123 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
) ) {
1136 wxFAIL_MSG(_("invalid message box return value"));
1140 bOk
= file
.Open(filename
, wxFile
::write_append
);
1143 bOk
= file
.Create(filename
, true /* overwrite */);
1147 bOk
= file
.Create(filename
);
1151 *pFilename
= filename
;
1156 #endif // CAN_SAVE_FILES
1158 #endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
1160 #if wxUSE_LOG && wxUSE_TEXTCTRL
1162 // ----------------------------------------------------------------------------
1163 // wxLogTextCtrl implementation
1164 // ----------------------------------------------------------------------------
1166 wxLogTextCtrl
::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
1168 m_pTextCtrl
= pTextCtrl
;
1171 void wxLogTextCtrl
::DoLogText(const wxString
& msg
)
1173 m_pTextCtrl
->AppendText(msg
+ wxS('\n'));
1176 #endif // wxUSE_LOG && wxUSE_TEXTCTRL