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"
56 #include "wx/thread.h"
57 #endif // wxUSE_THREADS
60 // for OutputDebugString()
61 #include "wx/msw/private.h"
70 #include "wx/listctrl.h"
71 #include "wx/imaglist.h"
73 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
75 #if defined(__MWERKS__) && wxUSE_UNICODE
79 #include "wx/datetime.h"
81 // the suffix we add to the button to show that the dialog can be expanded
82 #define EXPAND_SUFFIX _T(" >>")
84 #define CAN_SAVE_FILES (wxUSE_FILE && wxUSE_FILEDLG)
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
92 // this function is a wrapper around strftime(3)
93 // allows to exclude the usage of wxDateTime
94 static wxString
TimeStamp(const wxString
& format
, time_t t
)
99 if ( !wxStrftime(buf
, WXSIZEOF(buf
), format
, wxLocaltime_r(&t
, &tm
)) )
101 // buffer is too small?
102 wxFAIL_MSG(_T("strftime() failed"));
104 return wxString(buf
);
105 #else // !wxUSE_DATETIME
106 return wxEmptyString
;
107 #endif // wxUSE_DATETIME/!wxUSE_DATETIME
111 class wxLogDialog
: public wxDialog
114 wxLogDialog(wxWindow
*parent
,
115 const wxArrayString
& messages
,
116 const wxArrayInt
& severity
,
117 const wxArrayLong
& timess
,
118 const wxString
& caption
,
120 virtual ~wxLogDialog();
123 void OnOk(wxCommandEvent
& event
);
125 void OnCopy(wxCommandEvent
& event
);
126 #endif // wxUSE_CLIPBOARD
128 void OnSave(wxCommandEvent
& event
);
129 #endif // CAN_SAVE_FILES
130 void OnListSelect(wxListEvent
& event
);
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 DECLARE_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_SELECTED(wxID_ANY
, wxLogDialog::OnListSelect
)
186 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxLogDialog::OnListItemActivated
)
189 #endif // wxUSE_LOG_DIALOG
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
197 // pass an uninitialized file object, the function will ask the user for the
198 // filename and try to open it, returns true on success (file was opened),
199 // false if file couldn't be opened/created and -1 if the file selection
200 // dialog was cancelled
201 static int OpenLogFile(wxFile
& file
, wxString
*filename
= NULL
, wxWindow
*parent
= NULL
);
203 #endif // CAN_SAVE_FILES
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
209 // we use a global variable to store the frame pointer for wxLogStatus - bad,
210 // but it's the easiest way
211 static wxFrame
*gs_pFrame
= NULL
; // FIXME MT-unsafe
213 // ============================================================================
215 // ============================================================================
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
221 // accepts an additional argument which tells to which frame the output should
223 void wxVLogStatus(wxFrame
*pFrame
, const wxString
& format
, va_list argptr
)
227 wxLog
*pLog
= wxLog::GetActiveTarget();
228 if ( pLog
!= NULL
) {
229 msg
.PrintfV(format
, argptr
);
231 wxASSERT( gs_pFrame
== NULL
); // should be reset!
234 wxLog::OnLog(wxLOG_Status
, msg
, 0);
236 wxLog::OnLog(wxLOG_Status
, msg
, time(NULL
));
238 gs_pFrame
= (wxFrame
*) NULL
;
242 #if !wxUSE_UTF8_LOCALE_ONLY
243 void wxDoLogStatusWchar(wxFrame
*pFrame
, const wxChar
*format
, ...)
246 va_start(argptr
, format
);
247 wxVLogStatus(pFrame
, format
, argptr
);
250 #endif // !wxUSE_UTF8_LOCALE_ONLY
252 #if wxUSE_UNICODE_UTF8
253 void wxDoLogStatusUtf8(wxFrame
*pFrame
, const char *format
, ...)
256 va_start(argptr
, format
);
257 wxVLogStatus(pFrame
, format
, argptr
);
260 #endif // wxUSE_UNICODE_UTF8
262 // ----------------------------------------------------------------------------
263 // wxLogGui implementation (FIXME MT-unsafe)
264 // ----------------------------------------------------------------------------
273 void wxLogGui::Clear()
277 m_bHasMessages
= false;
284 void wxLogGui::Flush()
286 if ( !m_bHasMessages
)
289 // do it right now to block any new calls to Flush() while we're here
290 m_bHasMessages
= false;
292 const unsigned repeatCount
= LogLastRepeatIfNeeded();
294 wxString appName
= wxTheApp
->GetAppDisplayName();
297 wxString titleFormat
;
299 titleFormat
= _("%s Error");
302 else if ( m_bWarnings
) {
303 titleFormat
= _("%s Warning");
304 style
= wxICON_EXCLAMATION
;
307 titleFormat
= _("%s Information");
308 style
= wxICON_INFORMATION
;
312 title
.Printf(titleFormat
, appName
.c_str());
314 size_t nMsgCount
= m_aMessages
.GetCount();
316 // avoid showing other log dialogs until we're done with the dialog we're
317 // showing right now: nested modal dialogs make for really bad UI!
321 if ( nMsgCount
== 1 )
323 str
= m_aMessages
[0];
325 else // more than one message
329 if ( repeatCount
> 0 )
331 m_aMessages
[nMsgCount
- 1]
332 << " (" << m_aMessages
[nMsgCount
- 2] << ")";
335 wxLogDialog
dlg(NULL
,
336 m_aMessages
, m_aSeverity
, m_aTimes
,
339 // clear the message list before showing the dialog because while it's
340 // shown some new messages may appear
343 (void)dlg
.ShowModal();
344 #else // !wxUSE_LOG_DIALOG
345 // concatenate all strings (but not too many to not overfill the msg box)
348 // start from the most recent message
349 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
350 // for Windows strings longer than this value are wrapped (NT 4.0)
351 const size_t nMsgLineWidth
= 156;
353 nLines
+= (m_aMessages
[n
- 1].Len() + nMsgLineWidth
- 1) / nMsgLineWidth
;
355 if ( nLines
> 25 ) // don't put too many lines in message box
358 str
<< m_aMessages
[n
- 1] << wxT("\n");
360 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
363 // this catches both cases of 1 message with wxUSE_LOG_DIALOG and any
364 // situation without it
367 // we use a message dialog with 2 buttons to be able to use one of them
368 // for copying the message text to clipboard
370 wxMessageDialog
dlg(NULL
, str
, title
, style
| wxYES_NO
);
371 if ( !dlg
.SetYesNoLabels(wxID_COPY
, wxID_OK
) )
372 #endif // wxUSE_CLIPBOARD
374 // but if custom labels are not supported it makes no sense to keep
375 // two buttons so revert to a single one
376 dlg
.SetMessageDialogStyle(style
| wxOK
);
380 if ( dlg
.ShowModal() == wxID_YES
)
382 // this means the wxID_COPY button was selected
383 wxClipboardLocker clip
;
384 if ( !clip
|| !wxTheClipboard
->AddData(new wxTextDataObject(str
)) )
385 wxLogError(_("Failed to copy dialog contents to the clipboard."));
387 #endif // wxUSE_CLIPBOARD
389 // no undisplayed messages whatsoever
393 // allow flushing the logs again
397 // log all kinds of messages
398 void wxLogGui::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
405 m_aMessages
.Add(szString
);
406 m_aSeverity
.Add(wxLOG_Message
);
407 m_aTimes
.Add((long)t
);
408 m_bHasMessages
= true;
415 // find the top window and set it's status text if it has any
416 wxFrame
*pFrame
= gs_pFrame
;
417 if ( pFrame
== NULL
) {
418 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
419 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
420 pFrame
= (wxFrame
*)pWin
;
424 if ( pFrame
&& pFrame
->GetStatusBar() )
425 pFrame
->SetStatusText(szString
);
427 #endif // wxUSE_STATUSBAR
438 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
439 // don't prepend debug/trace here: it goes to the
440 // debug window anyhow
442 OutputDebugString(str
.wx_str());
444 // send them to stderr
445 wxFprintf(stderr
, wxT("[%s] %s\n"),
446 level
== wxLOG_Trace
? wxT("Trace")
452 #endif // __WXDEBUG__
456 case wxLOG_FatalError
:
457 // show this one immediately
458 wxMessageBox(szString
, _("Fatal error"), wxICON_HAND
);
464 #if !wxUSE_LOG_DIALOG
465 // discard earlier informational messages if this is the 1st
466 // error because they might not make sense any more and showing
467 // them in a message box might be confusing
471 #endif // wxUSE_LOG_DIALOG
478 // for the warning we don't discard the info messages
482 m_aMessages
.Add(szString
);
483 m_aSeverity
.Add((int)level
);
484 m_aTimes
.Add((long)t
);
485 m_bHasMessages
= true;
490 #endif // wxUSE_LOGGUI
492 // ----------------------------------------------------------------------------
493 // wxLogWindow and wxLogFrame implementation
494 // ----------------------------------------------------------------------------
500 class wxLogFrame
: public wxFrame
504 wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
);
505 virtual ~wxLogFrame();
508 void OnClose(wxCommandEvent
& event
);
509 void OnCloseWindow(wxCloseEvent
& event
);
511 void OnSave(wxCommandEvent
& event
);
512 #endif // CAN_SAVE_FILES
513 void OnClear(wxCommandEvent
& event
);
515 // this function is safe to call from any thread (notice that it should be
516 // also called from the main thread to ensure that the messages logged from
517 // it appear in correct order with the messages from the other threads)
518 void AddLogMessage(const wxString
& message
);
520 // actually append the messages logged from secondary threads to the text
521 // control during idle time in the main thread
522 virtual void OnInternalIdle();
525 // use standard ids for our commands!
528 Menu_Close
= wxID_CLOSE
,
529 Menu_Save
= wxID_SAVE
,
530 Menu_Clear
= wxID_CLEAR
533 // common part of OnClose() and OnCloseWindow()
536 // do show the message in the text control
537 void DoShowLogMessage(const wxString
& message
)
539 m_pTextCtrl
->AppendText(message
);
542 wxTextCtrl
*m_pTextCtrl
;
545 // queue of messages logged from other threads which need to be displayed
546 wxArrayString m_pendingMessages
;
549 // critical section to protect access to m_pendingMessages
550 wxCriticalSection m_critSection
;
551 #endif // wxUSE_THREADS
554 DECLARE_EVENT_TABLE()
555 DECLARE_NO_COPY_CLASS(wxLogFrame
)
558 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
559 // wxLogWindow menu events
560 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
562 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
563 #endif // CAN_SAVE_FILES
564 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
566 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
569 wxLogFrame::wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
)
570 : wxFrame(pParent
, wxID_ANY
, szTitle
)
574 m_pTextCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
578 // needed for Win32 to avoid 65Kb limit but it doesn't work well
579 // when using RichEdit 2.0 which we always do in the Unicode build
582 #endif // !wxUSE_UNICODE
587 wxMenuBar
*pMenuBar
= new wxMenuBar
;
588 wxMenu
*pMenu
= new wxMenu
;
590 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
591 #endif // CAN_SAVE_FILES
592 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
593 pMenu
->AppendSeparator();
594 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
595 pMenuBar
->Append(pMenu
, _("&Log"));
596 SetMenuBar(pMenuBar
);
597 #endif // wxUSE_MENUS
600 // status bar for menu prompts
602 #endif // wxUSE_STATUSBAR
604 m_log
->OnFrameCreate(this);
607 void wxLogFrame::DoClose()
609 if ( m_log
->OnFrameClose(this) )
611 // instead of closing just hide the window to be able to Show() it
617 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
622 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
628 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
632 int rc
= OpenLogFile(file
, &filename
, this);
641 // retrieve text and save it
642 // -------------------------
643 int nLines
= m_pTextCtrl
->GetNumberOfLines();
644 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
645 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
646 wxTextFile::GetEOL());
653 wxLogError(_("Can't save log contents to file."));
656 wxLogStatus((wxFrame
*)this, _("Log saved to the file '%s'."), filename
.c_str());
659 #endif // CAN_SAVE_FILES
661 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
663 m_pTextCtrl
->Clear();
666 void wxLogFrame::OnInternalIdle()
669 wxCRIT_SECT_LOCKER(locker
, m_critSection
);
671 const size_t count
= m_pendingMessages
.size();
672 for ( size_t n
= 0; n
< count
; n
++ )
674 DoShowLogMessage(m_pendingMessages
[n
]);
677 m_pendingMessages
.clear();
678 } // release m_critSection
680 wxFrame::OnInternalIdle();
683 void wxLogFrame::AddLogMessage(const wxString
& message
)
685 wxCRIT_SECT_LOCKER(locker
, m_critSection
);
688 if ( !wxThread::IsMain() || !m_pendingMessages
.empty() )
690 // message needs to be queued for later showing
691 m_pendingMessages
.Add(message
);
695 else // we are the main thread and no messages are queued, so we can
696 // log the message directly
697 #endif // wxUSE_THREADS
699 DoShowLogMessage(message
);
703 wxLogFrame::~wxLogFrame()
705 m_log
->OnFrameDelete(this);
711 wxLogWindow::wxLogWindow(wxWindow
*pParent
,
712 const wxString
& szTitle
,
716 PassMessages(bDoPass
);
718 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
724 void wxLogWindow::Show(bool bShow
)
726 m_pLogFrame
->Show(bShow
);
729 void wxLogWindow::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
731 // first let the previous logger show it
732 wxLogPassThrough::DoLog(level
, szString
, t
);
737 // by default, these messages are ignored by wxLog, so process
739 if ( !szString
.empty() )
742 str
<< _("Status: ") << szString
;
747 // don't put trace messages in the text window for 2 reasons:
748 // 1) there are too many of them
749 // 2) they may provoke other trace messages thus sending a program
750 // into an infinite loop
755 // and this will format it nicely and call our DoLogString()
756 wxLog::DoLog(level
, szString
, t
);
761 void wxLogWindow::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
766 msg
<< szString
<< wxT('\n');
768 m_pLogFrame
->AddLogMessage(msg
);
771 wxFrame
*wxLogWindow::GetFrame() const
776 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
780 bool wxLogWindow::OnFrameClose(wxFrame
* WXUNUSED(frame
))
786 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
788 m_pLogFrame
= (wxLogFrame
*)NULL
;
791 wxLogWindow::~wxLogWindow()
793 // may be NULL if log frame already auto destroyed itself
797 #endif // wxUSE_LOGWINDOW
799 // ----------------------------------------------------------------------------
801 // ----------------------------------------------------------------------------
805 wxString
wxLogDialog::ms_details
;
806 size_t wxLogDialog::ms_maxLength
= 0;
808 wxLogDialog::wxLogDialog(wxWindow
*parent
,
809 const wxArrayString
& messages
,
810 const wxArrayInt
& severity
,
811 const wxArrayLong
& times
,
812 const wxString
& caption
,
814 : wxDialog(parent
, wxID_ANY
, caption
,
815 wxDefaultPosition
, wxDefaultSize
,
816 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
818 // init the static variables:
820 if ( ms_details
.empty() )
822 // ensure that we won't loop here if wxGetTranslation()
823 // happens to pop up a Log message while translating this :-)
824 ms_details
= wxTRANSLATE("&Details");
825 ms_details
= wxGetTranslation(ms_details
);
826 #ifdef __SMARTPHONE__
827 ms_details
= wxStripMenuCodes(ms_details
);
831 if ( ms_maxLength
== 0 )
833 ms_maxLength
= (2 * wxGetDisplaySize().x
/3) / GetCharWidth();
836 size_t count
= messages
.GetCount();
837 m_messages
.Alloc(count
);
838 m_severity
.Alloc(count
);
839 m_times
.Alloc(count
);
841 for ( size_t n
= 0; n
< count
; n
++ )
843 m_messages
.Add(messages
[n
]);
844 m_severity
.Add(severity
[n
]);
845 m_times
.Add(times
[n
]);
850 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
852 // create the controls which are always shown and layout them: we use
853 // sizers even though our window is not resizeable to calculate the size of
854 // the dialog properly
855 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
856 wxBoxSizer
*sizerAll
= new wxBoxSizer(isPda
? wxVERTICAL
: wxHORIZONTAL
);
860 wxStaticBitmap
*icon
= new wxStaticBitmap
864 wxArtProvider::GetMessageBoxIcon(style
)
866 sizerAll
->Add(icon
, wxSizerFlags().Centre());
869 // create the text sizer with a minimal size so that we are sure it won't be too small
870 wxString message
= EllipsizeString(messages
.Last());
871 wxSizer
*szText
= CreateTextSizer(message
);
872 szText
->SetMinSize(wxMin(300, wxGetDisplaySize().x
/ 3), -1);
874 sizerAll
->Add(szText
, wxSizerFlags(1).Centre().Border(wxLEFT
| wxRIGHT
));
876 wxButton
*btnOk
= new wxButton(this, wxID_OK
);
877 sizerAll
->Add(btnOk
, wxSizerFlags().Centre());
879 sizerTop
->Add(sizerAll
, wxSizerFlags().Expand().Border());
882 // add the details pane
883 #ifndef __SMARTPHONE__
884 wxCollapsiblePane
* const
885 collpane
= new wxCollapsiblePane(this, wxID_ANY
, ms_details
);
886 sizerTop
->Add(collpane
, wxSizerFlags(1).Expand().Border());
888 wxWindow
*win
= collpane
->GetPane();
889 wxSizer
* const paneSz
= new wxBoxSizer(wxVERTICAL
);
891 CreateDetailsControls(win
);
893 paneSz
->Add(m_listctrl
, wxSizerFlags(1).Expand().Border(wxTOP
));
895 #if wxUSE_CLIPBOARD || CAN_SAVE_FILES
896 wxBoxSizer
* const btnSizer
= new wxBoxSizer(wxHORIZONTAL
);
898 wxSizerFlags flagsBtn
;
899 flagsBtn
.Border(wxLEFT
);
902 btnSizer
->Add(new wxButton(win
, wxID_COPY
), flagsBtn
);
903 #endif // wxUSE_CLIPBOARD
906 btnSizer
->Add(new wxButton(win
, wxID_SAVE
), flagsBtn
);
907 #endif // CAN_SAVE_FILES
909 paneSz
->Add(btnSizer
, wxSizerFlags().Right().Border(wxTOP
));
910 #endif // wxUSE_CLIPBOARD || CAN_SAVE_FILES
912 win
->SetSizer(paneSz
);
913 paneSz
->SetSizeHints(win
);
914 #else // __SMARTPHONE__
915 SetLeftMenu(wxID_OK
);
916 SetRightMenu(wxID_MORE
, ms_details
+ EXPAND_SUFFIX
);
917 #endif // __SMARTPHONE__/!__SMARTPHONE__
919 SetSizerAndFit(sizerTop
);
925 // Move up the screen so that when we expand the dialog,
926 // there's enough space.
927 Move(wxPoint(GetPosition().x
, GetPosition().y
/ 2));
931 void wxLogDialog::CreateDetailsControls(wxWindow
*parent
)
933 // create the list ctrl now
934 m_listctrl
= new wxListCtrl(parent
, wxID_ANY
,
935 wxDefaultPosition
, wxDefaultSize
,
941 // This makes a big aesthetic difference on WinCE but I
942 // don't want to risk problems on other platforms
946 // no need to translate these strings as they're not shown to the
947 // user anyhow (we use wxLC_NO_HEADER style)
948 m_listctrl
->InsertColumn(0, _T("Message"));
949 m_listctrl
->InsertColumn(1, _T("Time"));
951 // prepare the imagelist
952 static const int ICON_SIZE
= 16;
953 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
955 // order should be the same as in the switch below!
956 static const wxChar
* icons
[] =
963 bool loadedIcons
= true;
965 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
967 wxBitmap bmp
= wxArtProvider::GetBitmap(icons
[icon
], wxART_MESSAGE_BOX
,
968 wxSize(ICON_SIZE
, ICON_SIZE
));
970 // This may very well fail if there are insufficient colours available.
971 // Degrade gracefully.
982 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
985 wxString fmt
= wxLog::GetTimestamp();
992 size_t count
= m_messages
.GetCount();
993 for ( size_t n
= 0; n
< count
; n
++ )
999 switch ( m_severity
[n
] )
1013 else // failed to load images
1018 wxString msg
= m_messages
[n
];
1019 msg
.Replace(wxT("\n"), wxT(" "));
1020 msg
= EllipsizeString(msg
);
1022 m_listctrl
->InsertItem(n
, msg
, image
);
1023 m_listctrl
->SetItem(n
, 1, TimeStamp(fmt
, (time_t)m_times
[n
]));
1026 // let the columns size themselves
1027 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
1028 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
1030 // calculate an approximately nice height for the listctrl
1031 int height
= GetCharHeight()*(count
+ 4);
1033 // but check that the dialog won't fall fown from the screen
1035 // we use GetMinHeight() to get the height of the dialog part without the
1036 // details and we consider that the "Save" button below and the separator
1037 // line (and the margins around it) take about as much, hence double it
1038 int heightMax
= wxGetDisplaySize().y
- GetPosition().y
- 2*GetMinHeight();
1040 // we should leave a margin
1044 m_listctrl
->SetSize(wxDefaultCoord
, wxMin(height
, heightMax
));
1047 void wxLogDialog::OnListSelect(wxListEvent
& event
)
1049 // we can't just disable the control because this looks ugly under Windows
1050 // (wrong bg colour, no scrolling...), but we still want to disable
1051 // selecting items - it makes no sense here
1052 m_listctrl
->SetItemState(event
.GetIndex(), 0, wxLIST_STATE_SELECTED
);
1055 void wxLogDialog::OnListItemActivated(wxListEvent
& event
)
1057 // show the activated item in a message box
1058 // This allow the user to correctly display the logs which are longer
1059 // than the listctrl and thus gets truncated or those which contains
1063 // wxString str = m_listctrl->GetItemText(event.GetIndex());
1064 // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW.
1065 wxString str
= m_messages
[event
.GetIndex()];
1067 // wxMessageBox will nicely handle the '\n' in the string (if any)
1068 // and supports long strings
1069 wxMessageBox(str
, wxT("Log message"), wxOK
, this);
1072 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
1077 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
1079 wxString
wxLogDialog::GetLogMessages() const
1081 wxString fmt
= wxLog::GetTimestamp();
1084 // use the default format
1088 const size_t count
= m_messages
.GetCount();
1091 text
.reserve(count
*m_messages
[0].length());
1092 for ( size_t n
= 0; n
< count
; n
++ )
1094 text
<< TimeStamp(fmt
, (time_t)m_times
[n
])
1097 << wxTextFile::GetEOL();
1103 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
1107 void wxLogDialog::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1109 wxClipboardLocker clip
;
1111 !wxTheClipboard
->AddData(new wxTextDataObject(GetLogMessages())) )
1113 wxLogError(_("Failed to copy dialog contents to the clipboard."));
1117 #endif // wxUSE_CLIPBOARD
1121 void wxLogDialog::OnSave(wxCommandEvent
& WXUNUSED(event
))
1124 int rc
= OpenLogFile(file
, NULL
, this);
1131 if ( !rc
|| !file
.Write(GetLogMessages()) || !file
.Close() )
1132 wxLogError(_("Can't save log contents to file."));
1135 #endif // CAN_SAVE_FILES
1137 wxLogDialog::~wxLogDialog()
1141 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
1145 #endif // wxUSE_LOG_DIALOG
1149 // pass an uninitialized file object, the function will ask the user for the
1150 // filename and try to open it, returns true on success (file was opened),
1151 // false if file couldn't be opened/created and -1 if the file selection
1152 // dialog was cancelled
1153 static int OpenLogFile(wxFile
& file
, wxString
*pFilename
, wxWindow
*parent
)
1155 // get the file name
1156 // -----------------
1157 wxString filename
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent
);
1166 if ( wxFile::Exists(filename
) ) {
1167 bool bAppend
= false;
1169 strMsg
.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
1171 switch ( wxMessageBox(strMsg
, _("Question"),
1172 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
) ) {
1185 wxFAIL_MSG(_("invalid message box return value"));
1189 bOk
= file
.Open(filename
, wxFile::write_append
);
1192 bOk
= file
.Create(filename
, true /* overwrite */);
1196 bOk
= file
.Create(filename
);
1200 *pFilename
= filename
;
1205 #endif // CAN_SAVE_FILES
1207 #endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
1209 #if wxUSE_LOG && wxUSE_TEXTCTRL
1211 // ----------------------------------------------------------------------------
1212 // wxLogTextCtrl implementation
1213 // ----------------------------------------------------------------------------
1215 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
1217 m_pTextCtrl
= pTextCtrl
;
1220 void wxLogTextCtrl::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
1225 msg
<< szString
<< wxT('\n');
1226 m_pTextCtrl
->AppendText(msg
);
1229 #endif // wxUSE_LOG && wxUSE_TEXTCTRL