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
));
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 int wxLogGui::GetSeverityIcon() const
286 return m_bErrors
? wxICON_STOP
287 : m_bWarnings
? wxICON_EXCLAMATION
288 : wxICON_INFORMATION
;
291 wxString
wxLogGui::GetTitle() const
293 wxString titleFormat
;
294 switch ( GetSeverityIcon() )
297 titleFormat
= _("%s Error");
300 case wxICON_EXCLAMATION
:
301 titleFormat
= _("%s Warning");
305 wxFAIL_MSG( "unexpected icon severity" );
308 case wxICON_INFORMATION
:
309 titleFormat
= _("%s Information");
312 return wxString::Format(titleFormat
, wxTheApp
->GetAppDisplayName());
316 wxLogGui::DoShowSingleLogMessage(const wxString
& message
,
317 const wxString
& title
,
320 wxMessageBox(message
, title
, wxOK
| style
);
324 wxLogGui::DoShowMultipleLogMessages(const wxArrayString
& messages
,
325 const wxArrayInt
& severities
,
326 const wxArrayLong
& times
,
327 const wxString
& title
,
331 wxLogDialog
dlg(NULL
,
332 messages
, severities
, times
,
335 // clear the message list before showing the dialog because while it's
336 // shown some new messages may appear
339 (void)dlg
.ShowModal();
340 #else // !wxUSE_LOG_DIALOG
341 // start from the most recent message
343 const size_t nMsgCount
= messages
.size();
344 message
.reserve(nMsgCount
*100);
345 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
346 message
<< m_aMessages
[n
- 1] << wxT("\n");
349 DoShowSingleLogMessage(message
, title
, style
);
350 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
353 void wxLogGui::Flush()
355 if ( !m_bHasMessages
)
358 // do it right now to block any new calls to Flush() while we're here
359 m_bHasMessages
= false;
361 // note that this must be done before examining m_aMessages as it may log
362 // yet another message
363 const unsigned repeatCount
= LogLastRepeatIfNeeded();
365 const size_t nMsgCount
= m_aMessages
.size();
367 if ( repeatCount
> 0 )
369 m_aMessages
[nMsgCount
- 1] << " (" << m_aMessages
[nMsgCount
- 2] << ")";
372 const wxString title
= GetTitle();
373 const int style
= GetSeverityIcon();
375 // avoid showing other log dialogs until we're done with the dialog we're
376 // showing right now: nested modal dialogs make for really bad UI!
379 if ( nMsgCount
== 1 )
381 // make a copy before calling Clear()
382 const wxString
message(m_aMessages
[0]);
385 DoShowSingleLogMessage(message
, title
, style
);
387 else // more than one message
389 wxArrayString messages
;
390 wxArrayInt severities
;
393 messages
.swap(m_aMessages
);
394 severities
.swap(m_aSeverity
);
395 times
.swap(m_aTimes
);
399 DoShowMultipleLogMessages(messages
, severities
, times
, title
, style
);
402 // allow flushing the logs again
406 // log all kinds of messages
407 void wxLogGui::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
414 m_aMessages
.Add(szString
);
415 m_aSeverity
.Add(wxLOG_Message
);
416 m_aTimes
.Add((long)t
);
417 m_bHasMessages
= true;
424 // find the top window and set it's status text if it has any
425 wxFrame
*pFrame
= gs_pFrame
;
426 if ( pFrame
== NULL
) {
427 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
428 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
429 pFrame
= (wxFrame
*)pWin
;
433 if ( pFrame
&& pFrame
->GetStatusBar() )
434 pFrame
->SetStatusText(szString
);
436 #endif // wxUSE_STATUSBAR
447 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
448 // don't prepend debug/trace here: it goes to the
449 // debug window anyhow
451 OutputDebugString(str
.wx_str());
453 // send them to stderr
454 wxFprintf(stderr
, wxT("[%s] %s\n"),
455 level
== wxLOG_Trace
? wxT("Trace")
461 #endif // __WXDEBUG__
465 case wxLOG_FatalError
:
466 // show this one immediately
467 wxMessageBox(szString
, _("Fatal error"), wxICON_HAND
);
473 #if !wxUSE_LOG_DIALOG
474 // discard earlier informational messages if this is the 1st
475 // error because they might not make sense any more and showing
476 // them in a message box might be confusing
480 #endif // wxUSE_LOG_DIALOG
487 // for the warning we don't discard the info messages
491 m_aMessages
.Add(szString
);
492 m_aSeverity
.Add((int)level
);
493 m_aTimes
.Add((long)t
);
494 m_bHasMessages
= true;
499 #endif // wxUSE_LOGGUI
501 // ----------------------------------------------------------------------------
502 // wxLogWindow and wxLogFrame implementation
503 // ----------------------------------------------------------------------------
509 class wxLogFrame
: public wxFrame
513 wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
);
514 virtual ~wxLogFrame();
517 void OnClose(wxCommandEvent
& event
);
518 void OnCloseWindow(wxCloseEvent
& event
);
520 void OnSave(wxCommandEvent
& event
);
521 #endif // CAN_SAVE_FILES
522 void OnClear(wxCommandEvent
& event
);
524 // this function is safe to call from any thread (notice that it should be
525 // also called from the main thread to ensure that the messages logged from
526 // it appear in correct order with the messages from the other threads)
527 void AddLogMessage(const wxString
& message
);
529 // actually append the messages logged from secondary threads to the text
530 // control during idle time in the main thread
531 virtual void OnInternalIdle();
534 // use standard ids for our commands!
537 Menu_Close
= wxID_CLOSE
,
538 Menu_Save
= wxID_SAVE
,
539 Menu_Clear
= wxID_CLEAR
542 // common part of OnClose() and OnCloseWindow()
545 // do show the message in the text control
546 void DoShowLogMessage(const wxString
& message
)
548 m_pTextCtrl
->AppendText(message
);
551 wxTextCtrl
*m_pTextCtrl
;
554 // queue of messages logged from other threads which need to be displayed
555 wxArrayString m_pendingMessages
;
558 // critical section to protect access to m_pendingMessages
559 wxCriticalSection m_critSection
;
560 #endif // wxUSE_THREADS
563 DECLARE_EVENT_TABLE()
564 DECLARE_NO_COPY_CLASS(wxLogFrame
)
567 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
568 // wxLogWindow menu events
569 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
571 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
572 #endif // CAN_SAVE_FILES
573 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
575 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
578 wxLogFrame::wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
)
579 : wxFrame(pParent
, wxID_ANY
, szTitle
)
583 m_pTextCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
587 // needed for Win32 to avoid 65Kb limit but it doesn't work well
588 // when using RichEdit 2.0 which we always do in the Unicode build
591 #endif // !wxUSE_UNICODE
596 wxMenuBar
*pMenuBar
= new wxMenuBar
;
597 wxMenu
*pMenu
= new wxMenu
;
599 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
600 #endif // CAN_SAVE_FILES
601 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
602 pMenu
->AppendSeparator();
603 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
604 pMenuBar
->Append(pMenu
, _("&Log"));
605 SetMenuBar(pMenuBar
);
606 #endif // wxUSE_MENUS
609 // status bar for menu prompts
611 #endif // wxUSE_STATUSBAR
613 m_log
->OnFrameCreate(this);
616 void wxLogFrame::DoClose()
618 if ( m_log
->OnFrameClose(this) )
620 // instead of closing just hide the window to be able to Show() it
626 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
631 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
637 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
641 int rc
= OpenLogFile(file
, &filename
, this);
650 // retrieve text and save it
651 // -------------------------
652 int nLines
= m_pTextCtrl
->GetNumberOfLines();
653 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
654 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
655 wxTextFile::GetEOL());
662 wxLogError(_("Can't save log contents to file."));
665 wxLogStatus((wxFrame
*)this, _("Log saved to the file '%s'."), filename
.c_str());
668 #endif // CAN_SAVE_FILES
670 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
672 m_pTextCtrl
->Clear();
675 void wxLogFrame::OnInternalIdle()
678 wxCRIT_SECT_LOCKER(locker
, m_critSection
);
680 const size_t count
= m_pendingMessages
.size();
681 for ( size_t n
= 0; n
< count
; n
++ )
683 DoShowLogMessage(m_pendingMessages
[n
]);
686 m_pendingMessages
.clear();
687 } // release m_critSection
689 wxFrame::OnInternalIdle();
692 void wxLogFrame::AddLogMessage(const wxString
& message
)
694 wxCRIT_SECT_LOCKER(locker
, m_critSection
);
697 if ( !wxThread::IsMain() || !m_pendingMessages
.empty() )
699 // message needs to be queued for later showing
700 m_pendingMessages
.Add(message
);
704 else // we are the main thread and no messages are queued, so we can
705 // log the message directly
706 #endif // wxUSE_THREADS
708 DoShowLogMessage(message
);
712 wxLogFrame::~wxLogFrame()
714 m_log
->OnFrameDelete(this);
720 wxLogWindow::wxLogWindow(wxWindow
*pParent
,
721 const wxString
& szTitle
,
725 PassMessages(bDoPass
);
727 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
733 void wxLogWindow::Show(bool bShow
)
735 m_pLogFrame
->Show(bShow
);
738 void wxLogWindow::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
740 // first let the previous logger show it
741 wxLogPassThrough::DoLog(level
, szString
, t
);
746 // by default, these messages are ignored by wxLog, so process
748 if ( !szString
.empty() )
751 str
<< _("Status: ") << szString
;
756 // don't put trace messages in the text window for 2 reasons:
757 // 1) there are too many of them
758 // 2) they may provoke other trace messages thus sending a program
759 // into an infinite loop
764 // and this will format it nicely and call our DoLogString()
765 wxLog::DoLog(level
, szString
, t
);
770 void wxLogWindow::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
775 msg
<< szString
<< wxT('\n');
777 m_pLogFrame
->AddLogMessage(msg
);
780 wxFrame
*wxLogWindow::GetFrame() const
785 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
789 bool wxLogWindow::OnFrameClose(wxFrame
* WXUNUSED(frame
))
795 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
800 wxLogWindow::~wxLogWindow()
802 // may be NULL if log frame already auto destroyed itself
806 #endif // wxUSE_LOGWINDOW
808 // ----------------------------------------------------------------------------
810 // ----------------------------------------------------------------------------
814 wxString
wxLogDialog::ms_details
;
815 size_t wxLogDialog::ms_maxLength
= 0;
817 wxLogDialog::wxLogDialog(wxWindow
*parent
,
818 const wxArrayString
& messages
,
819 const wxArrayInt
& severity
,
820 const wxArrayLong
& times
,
821 const wxString
& caption
,
823 : wxDialog(parent
, wxID_ANY
, caption
,
824 wxDefaultPosition
, wxDefaultSize
,
825 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
827 // init the static variables:
829 if ( ms_details
.empty() )
831 // ensure that we won't loop here if wxGetTranslation()
832 // happens to pop up a Log message while translating this :-)
833 ms_details
= wxTRANSLATE("&Details");
834 ms_details
= wxGetTranslation(ms_details
);
835 #ifdef __SMARTPHONE__
836 ms_details
= wxStripMenuCodes(ms_details
);
840 if ( ms_maxLength
== 0 )
842 ms_maxLength
= (2 * wxGetDisplaySize().x
/3) / GetCharWidth();
845 size_t count
= messages
.GetCount();
846 m_messages
.Alloc(count
);
847 m_severity
.Alloc(count
);
848 m_times
.Alloc(count
);
850 for ( size_t n
= 0; n
< count
; n
++ )
852 m_messages
.Add(messages
[n
]);
853 m_severity
.Add(severity
[n
]);
854 m_times
.Add(times
[n
]);
859 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
861 // create the controls which are always shown and layout them: we use
862 // sizers even though our window is not resizeable to calculate the size of
863 // the dialog properly
864 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
865 wxBoxSizer
*sizerAll
= new wxBoxSizer(isPda
? wxVERTICAL
: wxHORIZONTAL
);
869 wxStaticBitmap
*icon
= new wxStaticBitmap
873 wxArtProvider::GetMessageBoxIcon(style
)
875 sizerAll
->Add(icon
, wxSizerFlags().Centre());
878 // create the text sizer with a minimal size so that we are sure it won't be too small
879 wxString message
= EllipsizeString(messages
.Last());
880 wxSizer
*szText
= CreateTextSizer(message
);
881 szText
->SetMinSize(wxMin(300, wxGetDisplaySize().x
/ 3), -1);
883 sizerAll
->Add(szText
, wxSizerFlags(1).Centre().Border(wxLEFT
| wxRIGHT
));
885 wxButton
*btnOk
= new wxButton(this, wxID_OK
);
886 sizerAll
->Add(btnOk
, wxSizerFlags().Centre());
888 sizerTop
->Add(sizerAll
, wxSizerFlags().Expand().Border());
891 // add the details pane
892 #ifndef __SMARTPHONE__
893 wxCollapsiblePane
* const
894 collpane
= new wxCollapsiblePane(this, wxID_ANY
, ms_details
);
895 sizerTop
->Add(collpane
, wxSizerFlags(1).Expand().Border());
897 wxWindow
*win
= collpane
->GetPane();
898 wxSizer
* const paneSz
= new wxBoxSizer(wxVERTICAL
);
900 CreateDetailsControls(win
);
902 paneSz
->Add(m_listctrl
, wxSizerFlags(1).Expand().Border(wxTOP
));
904 #if wxUSE_CLIPBOARD || CAN_SAVE_FILES
905 wxBoxSizer
* const btnSizer
= new wxBoxSizer(wxHORIZONTAL
);
907 wxSizerFlags flagsBtn
;
908 flagsBtn
.Border(wxLEFT
);
911 btnSizer
->Add(new wxButton(win
, wxID_COPY
), flagsBtn
);
912 #endif // wxUSE_CLIPBOARD
915 btnSizer
->Add(new wxButton(win
, wxID_SAVE
), flagsBtn
);
916 #endif // CAN_SAVE_FILES
918 paneSz
->Add(btnSizer
, wxSizerFlags().Right().Border(wxTOP
));
919 #endif // wxUSE_CLIPBOARD || CAN_SAVE_FILES
921 win
->SetSizer(paneSz
);
922 paneSz
->SetSizeHints(win
);
923 #else // __SMARTPHONE__
924 SetLeftMenu(wxID_OK
);
925 SetRightMenu(wxID_MORE
, ms_details
+ EXPAND_SUFFIX
);
926 #endif // __SMARTPHONE__/!__SMARTPHONE__
928 SetSizerAndFit(sizerTop
);
934 // Move up the screen so that when we expand the dialog,
935 // there's enough space.
936 Move(wxPoint(GetPosition().x
, GetPosition().y
/ 2));
940 void wxLogDialog::CreateDetailsControls(wxWindow
*parent
)
942 // create the list ctrl now
943 m_listctrl
= new wxListCtrl(parent
, wxID_ANY
,
944 wxDefaultPosition
, wxDefaultSize
,
950 // This makes a big aesthetic difference on WinCE but I
951 // don't want to risk problems on other platforms
955 // no need to translate these strings as they're not shown to the
956 // user anyhow (we use wxLC_NO_HEADER style)
957 m_listctrl
->InsertColumn(0, _T("Message"));
958 m_listctrl
->InsertColumn(1, _T("Time"));
960 // prepare the imagelist
961 static const int ICON_SIZE
= 16;
962 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
964 // order should be the same as in the switch below!
965 static const wxChar
* icons
[] =
972 bool loadedIcons
= true;
974 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
976 wxBitmap bmp
= wxArtProvider::GetBitmap(icons
[icon
], wxART_MESSAGE_BOX
,
977 wxSize(ICON_SIZE
, ICON_SIZE
));
979 // This may very well fail if there are insufficient colours available.
980 // Degrade gracefully.
991 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
994 wxString fmt
= wxLog::GetTimestamp();
1001 size_t count
= m_messages
.GetCount();
1002 for ( size_t n
= 0; n
< count
; n
++ )
1008 switch ( m_severity
[n
] )
1022 else // failed to load images
1027 wxString msg
= m_messages
[n
];
1028 msg
.Replace(wxT("\n"), wxT(" "));
1029 msg
= EllipsizeString(msg
);
1031 m_listctrl
->InsertItem(n
, msg
, image
);
1032 m_listctrl
->SetItem(n
, 1, TimeStamp(fmt
, (time_t)m_times
[n
]));
1035 // let the columns size themselves
1036 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
1037 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
1039 // calculate an approximately nice height for the listctrl
1040 int height
= GetCharHeight()*(count
+ 4);
1042 // but check that the dialog won't fall fown from the screen
1044 // we use GetMinHeight() to get the height of the dialog part without the
1045 // details and we consider that the "Save" button below and the separator
1046 // line (and the margins around it) take about as much, hence double it
1047 int heightMax
= wxGetDisplaySize().y
- GetPosition().y
- 2*GetMinHeight();
1049 // we should leave a margin
1053 m_listctrl
->SetSize(wxDefaultCoord
, wxMin(height
, heightMax
));
1056 void wxLogDialog::OnListSelect(wxListEvent
& event
)
1058 // we can't just disable the control because this looks ugly under Windows
1059 // (wrong bg colour, no scrolling...), but we still want to disable
1060 // selecting items - it makes no sense here
1061 m_listctrl
->SetItemState(event
.GetIndex(), 0, wxLIST_STATE_SELECTED
);
1064 void wxLogDialog::OnListItemActivated(wxListEvent
& event
)
1066 // show the activated item in a message box
1067 // This allow the user to correctly display the logs which are longer
1068 // than the listctrl and thus gets truncated or those which contains
1072 // wxString str = m_listctrl->GetItemText(event.GetIndex());
1073 // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW.
1074 wxString str
= m_messages
[event
.GetIndex()];
1076 // wxMessageBox will nicely handle the '\n' in the string (if any)
1077 // and supports long strings
1078 wxMessageBox(str
, wxT("Log message"), wxOK
, this);
1081 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
1086 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
1088 wxString
wxLogDialog::GetLogMessages() const
1090 wxString fmt
= wxLog::GetTimestamp();
1093 // use the default format
1097 const size_t count
= m_messages
.GetCount();
1100 text
.reserve(count
*m_messages
[0].length());
1101 for ( size_t n
= 0; n
< count
; n
++ )
1103 text
<< TimeStamp(fmt
, (time_t)m_times
[n
])
1106 << wxTextFile::GetEOL();
1112 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
1116 void wxLogDialog::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1118 wxClipboardLocker clip
;
1120 !wxTheClipboard
->AddData(new wxTextDataObject(GetLogMessages())) )
1122 wxLogError(_("Failed to copy dialog contents to the clipboard."));
1126 #endif // wxUSE_CLIPBOARD
1130 void wxLogDialog::OnSave(wxCommandEvent
& WXUNUSED(event
))
1133 int rc
= OpenLogFile(file
, NULL
, this);
1140 if ( !rc
|| !file
.Write(GetLogMessages()) || !file
.Close() )
1141 wxLogError(_("Can't save log contents to file."));
1144 #endif // CAN_SAVE_FILES
1146 wxLogDialog::~wxLogDialog()
1150 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
1154 #endif // wxUSE_LOG_DIALOG
1158 // pass an uninitialized file object, the function will ask the user for the
1159 // filename and try to open it, returns true on success (file was opened),
1160 // false if file couldn't be opened/created and -1 if the file selection
1161 // dialog was cancelled
1162 static int OpenLogFile(wxFile
& file
, wxString
*pFilename
, wxWindow
*parent
)
1164 // get the file name
1165 // -----------------
1166 wxString filename
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent
);
1175 if ( wxFile::Exists(filename
) ) {
1176 bool bAppend
= false;
1178 strMsg
.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
1180 switch ( wxMessageBox(strMsg
, _("Question"),
1181 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
) ) {
1194 wxFAIL_MSG(_("invalid message box return value"));
1198 bOk
= file
.Open(filename
, wxFile::write_append
);
1201 bOk
= file
.Create(filename
, true /* overwrite */);
1205 bOk
= file
.Create(filename
);
1209 *pFilename
= filename
;
1214 #endif // CAN_SAVE_FILES
1216 #endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
1218 #if wxUSE_LOG && wxUSE_TEXTCTRL
1220 // ----------------------------------------------------------------------------
1221 // wxLogTextCtrl implementation
1222 // ----------------------------------------------------------------------------
1224 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
1226 m_pTextCtrl
= pTextCtrl
;
1229 void wxLogTextCtrl::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
1234 msg
<< szString
<< wxT('\n');
1235 m_pTextCtrl
->AppendText(msg
);
1238 #endif // wxUSE_LOG && wxUSE_TEXTCTRL