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/textfile.h"
49 #include "wx/statline.h"
50 #include "wx/artprov.h"
51 #include "wx/collpane.h"
52 #include "wx/arrstr.h"
55 #include "wx/thread.h"
56 #endif // wxUSE_THREADS
59 // for OutputDebugString()
60 #include "wx/msw/private.h"
69 #include "wx/listctrl.h"
70 #include "wx/imaglist.h"
72 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
74 #if defined(__MWERKS__) && wxUSE_UNICODE
78 #include "wx/datetime.h"
80 // the suffix we add to the button to show that the dialog can be expanded
81 #define EXPAND_SUFFIX _T(" >>")
83 #define CAN_SAVE_FILES (wxUSE_FILE && wxUSE_FILEDLG)
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
91 // this function is a wrapper around strftime(3)
92 // allows to exclude the usage of wxDateTime
93 static wxString
TimeStamp(const wxString
& format
, time_t t
)
98 if ( !wxStrftime(buf
, WXSIZEOF(buf
), format
, wxLocaltime_r(&t
, &tm
)) )
100 // buffer is too small?
101 wxFAIL_MSG(_T("strftime() failed"));
103 return wxString(buf
);
104 #else // !wxUSE_DATETIME
105 return wxEmptyString
;
106 #endif // wxUSE_DATETIME/!wxUSE_DATETIME
110 class wxLogDialog
: public wxDialog
113 wxLogDialog(wxWindow
*parent
,
114 const wxArrayString
& messages
,
115 const wxArrayInt
& severity
,
116 const wxArrayLong
& timess
,
117 const wxString
& caption
,
119 virtual ~wxLogDialog();
122 void OnOk(wxCommandEvent
& event
);
124 void OnCopy(wxCommandEvent
& event
);
125 #endif // wxUSE_CLIPBOARD
127 void OnSave(wxCommandEvent
& event
);
128 #endif // CAN_SAVE_FILES
129 void OnListSelect(wxListEvent
& event
);
130 void OnListItemActivated(wxListEvent
& event
);
133 // create controls needed for the details display
134 void CreateDetailsControls(wxWindow
*);
136 // if necessary truncates the given string and adds an ellipsis
137 wxString
EllipsizeString(const wxString
&text
)
139 if (ms_maxLength
> 0 &&
140 text
.length() > ms_maxLength
)
143 ret
.Truncate(ms_maxLength
);
151 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
152 // return the contents of the dialog as a multiline string
153 wxString
GetLogMessages() const;
154 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
157 // the data for the listctrl
158 wxArrayString m_messages
;
159 wxArrayInt m_severity
;
162 // the controls which are not shown initially (but only when details
163 // button is pressed)
164 wxListCtrl
*m_listctrl
;
166 // the translated "Details" string
167 static wxString ms_details
;
169 // the maximum length of the log message
170 static size_t ms_maxLength
;
172 DECLARE_EVENT_TABLE()
173 DECLARE_NO_COPY_CLASS(wxLogDialog
)
176 BEGIN_EVENT_TABLE(wxLogDialog
, wxDialog
)
177 EVT_BUTTON(wxID_OK
, wxLogDialog::OnOk
)
179 EVT_BUTTON(wxID_COPY
, wxLogDialog::OnCopy
)
180 #endif // wxUSE_CLIPBOARD
182 EVT_BUTTON(wxID_SAVE
, wxLogDialog::OnSave
)
183 #endif // CAN_SAVE_FILES
184 EVT_LIST_ITEM_SELECTED(wxID_ANY
, wxLogDialog::OnListSelect
)
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
));
237 gs_pFrame
= (wxFrame
*) 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 void wxLogGui::Flush()
285 if ( !m_bHasMessages
)
288 // do it right now to block any new calls to Flush() while we're here
289 m_bHasMessages
= false;
291 const unsigned repeatCount
= LogLastRepeatIfNeeded();
293 wxString appName
= wxTheApp
->GetAppDisplayName();
296 wxString titleFormat
;
298 titleFormat
= _("%s Error");
301 else if ( m_bWarnings
) {
302 titleFormat
= _("%s Warning");
303 style
= wxICON_EXCLAMATION
;
306 titleFormat
= _("%s Information");
307 style
= wxICON_INFORMATION
;
311 title
.Printf(titleFormat
, appName
.c_str());
313 size_t nMsgCount
= m_aMessages
.GetCount();
315 // avoid showing other log dialogs until we're done with the dialog we're
316 // showing right now: nested modal dialogs make for really bad UI!
320 if ( nMsgCount
== 1 )
322 str
= m_aMessages
[0];
324 else // more than one message
328 if ( repeatCount
> 0 )
330 m_aMessages
[nMsgCount
- 1]
331 << " (" << m_aMessages
[nMsgCount
- 2] << ")";
334 wxLogDialog
dlg(NULL
,
335 m_aMessages
, m_aSeverity
, m_aTimes
,
338 // clear the message list before showing the dialog because while it's
339 // shown some new messages may appear
342 (void)dlg
.ShowModal();
343 #else // !wxUSE_LOG_DIALOG
344 // concatenate all strings (but not too many to not overfill the msg box)
347 // start from the most recent message
348 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
349 // for Windows strings longer than this value are wrapped (NT 4.0)
350 const size_t nMsgLineWidth
= 156;
352 nLines
+= (m_aMessages
[n
- 1].Len() + nMsgLineWidth
- 1) / nMsgLineWidth
;
354 if ( nLines
> 25 ) // don't put too many lines in message box
357 str
<< m_aMessages
[n
- 1] << wxT("\n");
359 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
362 // this catches both cases of 1 message with wxUSE_LOG_DIALOG and any
363 // situation without it
366 // we use a message dialog with 2 buttons to be able to use one of them
367 // for copying the message text to clipboard
369 wxMessageDialog
dlg(NULL
, str
, title
, style
| wxYES_NO
);
370 if ( !dlg
.SetYesNoLabels(wxID_COPY
, wxID_OK
) )
371 #endif // wxUSE_CLIPBOARD
373 // but if custom labels are not supported it makes no sense to keep
374 // two buttons so revert to a single one
375 dlg
.SetMessageDialogStyle(style
| wxOK
);
379 if ( dlg
.ShowModal() == wxID_YES
)
381 // this means the wxID_COPY button was selected
382 wxClipboardLocker clip
;
383 if ( !clip
|| !wxTheClipboard
->AddData(new wxTextDataObject(str
)) )
384 wxLogError(_("Failed to copy dialog contents to the clipboard."));
386 #endif // wxUSE_CLIPBOARD
388 // no undisplayed messages whatsoever
392 // allow flushing the logs again
396 // log all kinds of messages
397 void wxLogGui::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
404 m_aMessages
.Add(szString
);
405 m_aSeverity
.Add(wxLOG_Message
);
406 m_aTimes
.Add((long)t
);
407 m_bHasMessages
= true;
414 // find the top window and set it's status text if it has any
415 wxFrame
*pFrame
= gs_pFrame
;
416 if ( pFrame
== NULL
) {
417 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
418 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
419 pFrame
= (wxFrame
*)pWin
;
423 if ( pFrame
&& pFrame
->GetStatusBar() )
424 pFrame
->SetStatusText(szString
);
426 #endif // wxUSE_STATUSBAR
437 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
438 // don't prepend debug/trace here: it goes to the
439 // debug window anyhow
441 OutputDebugString(str
.wx_str());
443 // send them to stderr
444 wxFprintf(stderr
, wxT("[%s] %s\n"),
445 level
== wxLOG_Trace
? wxT("Trace")
451 #endif // __WXDEBUG__
455 case wxLOG_FatalError
:
456 // show this one immediately
457 wxMessageBox(szString
, _("Fatal error"), wxICON_HAND
);
463 #if !wxUSE_LOG_DIALOG
464 // discard earlier informational messages if this is the 1st
465 // error because they might not make sense any more and showing
466 // them in a message box might be confusing
470 #endif // wxUSE_LOG_DIALOG
477 // for the warning we don't discard the info messages
481 m_aMessages
.Add(szString
);
482 m_aSeverity
.Add((int)level
);
483 m_aTimes
.Add((long)t
);
484 m_bHasMessages
= true;
489 #endif // wxUSE_LOGGUI
491 // ----------------------------------------------------------------------------
492 // wxLogWindow and wxLogFrame implementation
493 // ----------------------------------------------------------------------------
499 class wxLogFrame
: public wxFrame
503 wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
);
504 virtual ~wxLogFrame();
507 void OnClose(wxCommandEvent
& event
);
508 void OnCloseWindow(wxCloseEvent
& event
);
510 void OnSave(wxCommandEvent
& event
);
511 #endif // CAN_SAVE_FILES
512 void OnClear(wxCommandEvent
& event
);
514 // this function is safe to call from any thread (notice that it should be
515 // also called from the main thread to ensure that the messages logged from
516 // it appear in correct order with the messages from the other threads)
517 void AddLogMessage(const wxString
& message
);
519 // actually append the messages logged from secondary threads to the text
520 // control during idle time in the main thread
521 virtual void OnInternalIdle();
524 // use standard ids for our commands!
527 Menu_Close
= wxID_CLOSE
,
528 Menu_Save
= wxID_SAVE
,
529 Menu_Clear
= wxID_CLEAR
532 // common part of OnClose() and OnCloseWindow()
535 // do show the message in the text control
536 void DoShowLogMessage(const wxString
& message
)
538 m_pTextCtrl
->AppendText(message
);
541 wxTextCtrl
*m_pTextCtrl
;
544 // queue of messages logged from other threads which need to be displayed
545 wxArrayString m_pendingMessages
;
548 // critical section to protect access to m_pendingMessages
549 wxCriticalSection m_critSection
;
550 #endif // wxUSE_THREADS
553 DECLARE_EVENT_TABLE()
554 DECLARE_NO_COPY_CLASS(wxLogFrame
)
557 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
558 // wxLogWindow menu events
559 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
561 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
562 #endif // CAN_SAVE_FILES
563 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
565 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
568 wxLogFrame::wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
)
569 : wxFrame(pParent
, wxID_ANY
, szTitle
)
573 m_pTextCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
577 // needed for Win32 to avoid 65Kb limit but it doesn't work well
578 // when using RichEdit 2.0 which we always do in the Unicode build
581 #endif // !wxUSE_UNICODE
586 wxMenuBar
*pMenuBar
= new wxMenuBar
;
587 wxMenu
*pMenu
= new wxMenu
;
589 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
590 #endif // CAN_SAVE_FILES
591 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
592 pMenu
->AppendSeparator();
593 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
594 pMenuBar
->Append(pMenu
, _("&Log"));
595 SetMenuBar(pMenuBar
);
596 #endif // wxUSE_MENUS
599 // status bar for menu prompts
601 #endif // wxUSE_STATUSBAR
603 m_log
->OnFrameCreate(this);
606 void wxLogFrame::DoClose()
608 if ( m_log
->OnFrameClose(this) )
610 // instead of closing just hide the window to be able to Show() it
616 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
621 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
627 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
631 int rc
= OpenLogFile(file
, &filename
, this);
640 // retrieve text and save it
641 // -------------------------
642 int nLines
= m_pTextCtrl
->GetNumberOfLines();
643 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
644 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
645 wxTextFile::GetEOL());
652 wxLogError(_("Can't save log contents to file."));
655 wxLogStatus((wxFrame
*)this, _("Log saved to the file '%s'."), filename
.c_str());
658 #endif // CAN_SAVE_FILES
660 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
662 m_pTextCtrl
->Clear();
665 void wxLogFrame::OnInternalIdle()
668 wxCRIT_SECT_LOCKER(locker
, m_critSection
);
670 const size_t count
= m_pendingMessages
.size();
671 for ( size_t n
= 0; n
< count
; n
++ )
673 DoShowLogMessage(m_pendingMessages
[n
]);
676 m_pendingMessages
.clear();
677 } // release m_critSection
679 wxFrame::OnInternalIdle();
682 void wxLogFrame::AddLogMessage(const wxString
& message
)
684 wxCRIT_SECT_LOCKER(locker
, m_critSection
);
687 if ( !wxThread::IsMain() || !m_pendingMessages
.empty() )
689 // message needs to be queued for later showing
690 m_pendingMessages
.Add(message
);
694 else // we are the main thread and no messages are queued, so we can
695 // log the message directly
696 #endif // wxUSE_THREADS
698 DoShowLogMessage(message
);
702 wxLogFrame::~wxLogFrame()
704 m_log
->OnFrameDelete(this);
710 wxLogWindow::wxLogWindow(wxWindow
*pParent
,
711 const wxString
& szTitle
,
715 PassMessages(bDoPass
);
717 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
723 void wxLogWindow::Show(bool bShow
)
725 m_pLogFrame
->Show(bShow
);
728 void wxLogWindow::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
730 // first let the previous logger show it
731 wxLogPassThrough::DoLog(level
, szString
, t
);
736 // by default, these messages are ignored by wxLog, so process
738 if ( !szString
.empty() )
741 str
<< _("Status: ") << szString
;
746 // don't put trace messages in the text window for 2 reasons:
747 // 1) there are too many of them
748 // 2) they may provoke other trace messages thus sending a program
749 // into an infinite loop
754 // and this will format it nicely and call our DoLogString()
755 wxLog::DoLog(level
, szString
, t
);
760 void wxLogWindow::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
765 msg
<< szString
<< wxT('\n');
767 m_pLogFrame
->AddLogMessage(msg
);
770 wxFrame
*wxLogWindow::GetFrame() const
775 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
779 bool wxLogWindow::OnFrameClose(wxFrame
* WXUNUSED(frame
))
785 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
787 m_pLogFrame
= (wxLogFrame
*)NULL
;
790 wxLogWindow::~wxLogWindow()
792 // may be NULL if log frame already auto destroyed itself
796 #endif // wxUSE_LOGWINDOW
798 // ----------------------------------------------------------------------------
800 // ----------------------------------------------------------------------------
804 wxString
wxLogDialog::ms_details
;
805 size_t wxLogDialog::ms_maxLength
= 0;
807 wxLogDialog::wxLogDialog(wxWindow
*parent
,
808 const wxArrayString
& messages
,
809 const wxArrayInt
& severity
,
810 const wxArrayLong
& times
,
811 const wxString
& caption
,
813 : wxDialog(parent
, wxID_ANY
, caption
,
814 wxDefaultPosition
, wxDefaultSize
,
815 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
817 // init the static variables:
819 if ( ms_details
.empty() )
821 // ensure that we won't loop here if wxGetTranslation()
822 // happens to pop up a Log message while translating this :-)
823 ms_details
= wxTRANSLATE("&Details");
824 ms_details
= wxGetTranslation(ms_details
);
825 #ifdef __SMARTPHONE__
826 ms_details
= wxStripMenuCodes(ms_details
);
830 if ( ms_maxLength
== 0 )
832 ms_maxLength
= (2 * wxGetDisplaySize().x
/3) / GetCharWidth();
835 size_t count
= messages
.GetCount();
836 m_messages
.Alloc(count
);
837 m_severity
.Alloc(count
);
838 m_times
.Alloc(count
);
840 for ( size_t n
= 0; n
< count
; n
++ )
842 m_messages
.Add(messages
[n
]);
843 m_severity
.Add(severity
[n
]);
844 m_times
.Add(times
[n
]);
849 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
851 // create the controls which are always shown and layout them: we use
852 // sizers even though our window is not resizeable to calculate the size of
853 // the dialog properly
854 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
855 wxBoxSizer
*sizerAll
= new wxBoxSizer(isPda
? wxVERTICAL
: wxHORIZONTAL
);
859 wxStaticBitmap
*icon
= new wxStaticBitmap
863 wxArtProvider::GetMessageBoxIcon(style
)
865 sizerAll
->Add(icon
, wxSizerFlags().Centre());
868 // create the text sizer with a minimal size so that we are sure it won't be too small
869 wxString message
= EllipsizeString(messages
.Last());
870 wxSizer
*szText
= CreateTextSizer(message
);
871 szText
->SetMinSize(wxMin(300, wxGetDisplaySize().x
/ 3), -1);
873 sizerAll
->Add(szText
, wxSizerFlags(1).Centre().Border(wxLEFT
| wxRIGHT
));
875 wxButton
*btnOk
= new wxButton(this, wxID_OK
);
876 sizerAll
->Add(btnOk
, wxSizerFlags().Centre());
878 sizerTop
->Add(sizerAll
, wxSizerFlags().Expand().Border());
881 // add the details pane
882 #ifndef __SMARTPHONE__
883 wxCollapsiblePane
* const
884 collpane
= new wxCollapsiblePane(this, wxID_ANY
, ms_details
);
885 sizerTop
->Add(collpane
, wxSizerFlags(1).Expand().Border());
887 wxWindow
*win
= collpane
->GetPane();
888 wxSizer
* const paneSz
= new wxBoxSizer(wxVERTICAL
);
890 CreateDetailsControls(win
);
892 paneSz
->Add(m_listctrl
, wxSizerFlags(1).Expand().Border(wxTOP
));
894 #if wxUSE_CLIPBOARD || CAN_SAVE_FILES
895 wxBoxSizer
* const btnSizer
= new wxBoxSizer(wxHORIZONTAL
);
897 wxSizerFlags flagsBtn
;
898 flagsBtn
.Border(wxLEFT
);
901 btnSizer
->Add(new wxButton(win
, wxID_COPY
), flagsBtn
);
902 #endif // wxUSE_CLIPBOARD
905 btnSizer
->Add(new wxButton(win
, wxID_SAVE
), flagsBtn
);
906 #endif // CAN_SAVE_FILES
908 paneSz
->Add(btnSizer
, wxSizerFlags().Right().Border(wxTOP
));
909 #endif // wxUSE_CLIPBOARD || CAN_SAVE_FILES
911 win
->SetSizer(paneSz
);
912 paneSz
->SetSizeHints(win
);
913 #else // __SMARTPHONE__
914 SetLeftMenu(wxID_OK
);
915 SetRightMenu(wxID_MORE
, ms_details
+ EXPAND_SUFFIX
);
916 #endif // __SMARTPHONE__/!__SMARTPHONE__
918 SetSizerAndFit(sizerTop
);
924 // Move up the screen so that when we expand the dialog,
925 // there's enough space.
926 Move(wxPoint(GetPosition().x
, GetPosition().y
/ 2));
930 void wxLogDialog::CreateDetailsControls(wxWindow
*parent
)
932 // create the list ctrl now
933 m_listctrl
= new wxListCtrl(parent
, wxID_ANY
,
934 wxDefaultPosition
, wxDefaultSize
,
940 // This makes a big aesthetic difference on WinCE but I
941 // don't want to risk problems on other platforms
945 // no need to translate these strings as they're not shown to the
946 // user anyhow (we use wxLC_NO_HEADER style)
947 m_listctrl
->InsertColumn(0, _T("Message"));
948 m_listctrl
->InsertColumn(1, _T("Time"));
950 // prepare the imagelist
951 static const int ICON_SIZE
= 16;
952 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
954 // order should be the same as in the switch below!
955 static const wxChar
* icons
[] =
962 bool loadedIcons
= true;
964 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
966 wxBitmap bmp
= wxArtProvider::GetBitmap(icons
[icon
], wxART_MESSAGE_BOX
,
967 wxSize(ICON_SIZE
, ICON_SIZE
));
969 // This may very well fail if there are insufficient colours available.
970 // Degrade gracefully.
981 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
984 wxString fmt
= wxLog::GetTimestamp();
991 size_t count
= m_messages
.GetCount();
992 for ( size_t n
= 0; n
< count
; n
++ )
998 switch ( m_severity
[n
] )
1012 else // failed to load images
1017 wxString msg
= m_messages
[n
];
1018 msg
.Replace(wxT("\n"), wxT(" "));
1019 msg
= EllipsizeString(msg
);
1021 m_listctrl
->InsertItem(n
, msg
, image
);
1022 m_listctrl
->SetItem(n
, 1, TimeStamp(fmt
, (time_t)m_times
[n
]));
1025 // let the columns size themselves
1026 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
1027 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
1029 // calculate an approximately nice height for the listctrl
1030 int height
= GetCharHeight()*(count
+ 4);
1032 // but check that the dialog won't fall fown from the screen
1034 // we use GetMinHeight() to get the height of the dialog part without the
1035 // details and we consider that the "Save" button below and the separator
1036 // line (and the margins around it) take about as much, hence double it
1037 int heightMax
= wxGetDisplaySize().y
- GetPosition().y
- 2*GetMinHeight();
1039 // we should leave a margin
1043 m_listctrl
->SetSize(wxDefaultCoord
, wxMin(height
, heightMax
));
1046 void wxLogDialog::OnListSelect(wxListEvent
& event
)
1048 // we can't just disable the control because this looks ugly under Windows
1049 // (wrong bg colour, no scrolling...), but we still want to disable
1050 // selecting items - it makes no sense here
1051 m_listctrl
->SetItemState(event
.GetIndex(), 0, wxLIST_STATE_SELECTED
);
1054 void wxLogDialog::OnListItemActivated(wxListEvent
& event
)
1056 // show the activated item in a message box
1057 // This allow the user to correctly display the logs which are longer
1058 // than the listctrl and thus gets truncated or those which contains
1062 // wxString str = m_listctrl->GetItemText(event.GetIndex());
1063 // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW.
1064 wxString str
= m_messages
[event
.GetIndex()];
1066 // wxMessageBox will nicely handle the '\n' in the string (if any)
1067 // and supports long strings
1068 wxMessageBox(str
, wxT("Log message"), wxOK
, this);
1071 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
1076 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
1078 wxString
wxLogDialog::GetLogMessages() const
1080 wxString fmt
= wxLog::GetTimestamp();
1083 // use the default format
1087 const size_t count
= m_messages
.GetCount();
1090 text
.reserve(count
*m_messages
[0].length());
1091 for ( size_t n
= 0; n
< count
; n
++ )
1093 text
<< TimeStamp(fmt
, (time_t)m_times
[n
])
1096 << wxTextFile::GetEOL();
1102 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
1106 void wxLogDialog::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1108 wxClipboardLocker clip
;
1110 !wxTheClipboard
->AddData(new wxTextDataObject(GetLogMessages())) )
1112 wxLogError(_("Failed to copy dialog contents to the clipboard."));
1116 #endif // wxUSE_CLIPBOARD
1120 void wxLogDialog::OnSave(wxCommandEvent
& WXUNUSED(event
))
1123 int rc
= OpenLogFile(file
, NULL
, this);
1130 if ( !rc
|| !file
.Write(GetLogMessages()) || !file
.Close() )
1131 wxLogError(_("Can't save log contents to file."));
1134 #endif // CAN_SAVE_FILES
1136 wxLogDialog::~wxLogDialog()
1140 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
1144 #endif // wxUSE_LOG_DIALOG
1148 // pass an uninitialized file object, the function will ask the user for the
1149 // filename and try to open it, returns true on success (file was opened),
1150 // false if file couldn't be opened/created and -1 if the file selection
1151 // dialog was cancelled
1152 static int OpenLogFile(wxFile
& file
, wxString
*pFilename
, wxWindow
*parent
)
1154 // get the file name
1155 // -----------------
1156 wxString filename
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent
);
1165 if ( wxFile::Exists(filename
) ) {
1166 bool bAppend
= false;
1168 strMsg
.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
1170 switch ( wxMessageBox(strMsg
, _("Question"),
1171 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
) ) {
1184 wxFAIL_MSG(_("invalid message box return value"));
1188 bOk
= file
.Open(filename
, wxFile::write_append
);
1191 bOk
= file
.Create(filename
, true /* overwrite */);
1195 bOk
= file
.Create(filename
);
1199 *pFilename
= filename
;
1204 #endif // CAN_SAVE_FILES
1206 #endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
1208 #if wxUSE_LOG && wxUSE_TEXTCTRL
1210 // ----------------------------------------------------------------------------
1211 // wxLogTextCtrl implementation
1212 // ----------------------------------------------------------------------------
1214 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
1216 m_pTextCtrl
= pTextCtrl
;
1219 void wxLogTextCtrl::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
1224 msg
<< szString
<< wxT('\n');
1225 m_pTextCtrl
->AppendText(msg
);
1228 #endif // wxUSE_LOG && wxUSE_TEXTCTRL