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 // ----------------------------------------------------------------------------
209 // wxLogGui implementation (FIXME MT-unsafe)
210 // ----------------------------------------------------------------------------
219 void wxLogGui::Clear()
223 m_bHasMessages
= false;
230 int wxLogGui::GetSeverityIcon() const
232 return m_bErrors
? wxICON_STOP
233 : m_bWarnings
? wxICON_EXCLAMATION
234 : wxICON_INFORMATION
;
237 wxString
wxLogGui::GetTitle() const
239 wxString titleFormat
;
240 switch ( GetSeverityIcon() )
243 titleFormat
= _("%s Error");
246 case wxICON_EXCLAMATION
:
247 titleFormat
= _("%s Warning");
251 wxFAIL_MSG( "unexpected icon severity" );
254 case wxICON_INFORMATION
:
255 titleFormat
= _("%s Information");
258 return wxString::Format(titleFormat
, wxTheApp
->GetAppDisplayName());
262 wxLogGui::DoShowSingleLogMessage(const wxString
& message
,
263 const wxString
& title
,
266 wxMessageBox(message
, title
, wxOK
| style
);
270 wxLogGui::DoShowMultipleLogMessages(const wxArrayString
& messages
,
271 const wxArrayInt
& severities
,
272 const wxArrayLong
& times
,
273 const wxString
& title
,
277 wxLogDialog
dlg(NULL
,
278 messages
, severities
, times
,
281 // clear the message list before showing the dialog because while it's
282 // shown some new messages may appear
285 (void)dlg
.ShowModal();
286 #else // !wxUSE_LOG_DIALOG
287 // start from the most recent message
289 const size_t nMsgCount
= messages
.size();
290 message
.reserve(nMsgCount
*100);
291 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
292 message
<< m_aMessages
[n
- 1] << wxT("\n");
295 DoShowSingleLogMessage(message
, title
, style
);
296 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
299 void wxLogGui::Flush()
301 if ( !m_bHasMessages
)
304 // do it right now to block any new calls to Flush() while we're here
305 m_bHasMessages
= false;
307 // note that this must be done before examining m_aMessages as it may log
308 // yet another message
309 const unsigned repeatCount
= LogLastRepeatIfNeeded();
311 const size_t nMsgCount
= m_aMessages
.size();
313 if ( repeatCount
> 0 )
315 m_aMessages
[nMsgCount
- 1] << " (" << m_aMessages
[nMsgCount
- 2] << ")";
318 const wxString title
= GetTitle();
319 const int style
= GetSeverityIcon();
321 // avoid showing other log dialogs until we're done with the dialog we're
322 // showing right now: nested modal dialogs make for really bad UI!
325 if ( nMsgCount
== 1 )
327 // make a copy before calling Clear()
328 const wxString
message(m_aMessages
[0]);
331 DoShowSingleLogMessage(message
, title
, style
);
333 else // more than one message
335 wxArrayString messages
;
336 wxArrayInt severities
;
339 messages
.swap(m_aMessages
);
340 severities
.swap(m_aSeverity
);
341 times
.swap(m_aTimes
);
345 DoShowMultipleLogMessages(messages
, severities
, times
, title
, style
);
348 // allow flushing the logs again
352 // log all kinds of messages
353 void wxLogGui::DoLogRecord(wxLogLevel level
,
355 const wxLogRecordInfo
& info
)
363 m_aMessages
.Add(msg
);
364 m_aSeverity
.Add(wxLOG_Message
);
365 m_aTimes
.Add((long)info
.timestamp
);
366 m_bHasMessages
= true;
373 wxFrame
*pFrame
= NULL
;
375 // check if the frame was passed to us explicitly
377 if ( info
.GetNumValue(wxLOG_KEY_FRAME
, &ptr
) )
379 pFrame
= static_cast<wxFrame
*>(wxUIntToPtr(ptr
));
382 // find the top window and set it's status text if it has any
383 if ( pFrame
== NULL
) {
384 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
385 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
386 pFrame
= (wxFrame
*)pWin
;
390 if ( pFrame
&& pFrame
->GetStatusBar() )
391 pFrame
->SetStatusText(msg
);
393 #endif // wxUSE_STATUSBAR
398 #if !wxUSE_LOG_DIALOG
399 // discard earlier informational messages if this is the 1st
400 // error because they might not make sense any more and showing
401 // them in a message box might be confusing
405 #endif // wxUSE_LOG_DIALOG
412 // for the warning we don't discard the info messages
416 m_aMessages
.Add(msg
);
417 m_aSeverity
.Add((int)level
);
418 m_aTimes
.Add((long)info
.timestamp
);
419 m_bHasMessages
= true;
423 // let the base class deal with debug/trace messages as well as any
425 wxLog::DoLogRecord(level
, msg
, info
);
429 #endif // wxUSE_LOGGUI
431 // ----------------------------------------------------------------------------
432 // wxLogWindow and wxLogFrame implementation
433 // ----------------------------------------------------------------------------
439 class wxLogFrame
: public wxFrame
443 wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
);
444 virtual ~wxLogFrame();
447 void OnClose(wxCommandEvent
& event
);
448 void OnCloseWindow(wxCloseEvent
& event
);
450 void OnSave(wxCommandEvent
& event
);
451 #endif // CAN_SAVE_FILES
452 void OnClear(wxCommandEvent
& event
);
454 // this function is safe to call from any thread (notice that it should be
455 // also called from the main thread to ensure that the messages logged from
456 // it appear in correct order with the messages from the other threads)
457 void AddLogMessage(const wxString
& message
);
459 // actually append the messages logged from secondary threads to the text
460 // control during idle time in the main thread
461 virtual void OnInternalIdle();
464 // use standard ids for our commands!
467 Menu_Close
= wxID_CLOSE
,
468 Menu_Save
= wxID_SAVE
,
469 Menu_Clear
= wxID_CLEAR
472 // common part of OnClose() and OnCloseWindow()
475 // do show the message in the text control
476 void DoShowLogMessage(const wxString
& message
)
478 m_pTextCtrl
->AppendText(message
+ wxS('\n'));
481 wxTextCtrl
*m_pTextCtrl
;
484 // queue of messages logged from other threads which need to be displayed
485 wxArrayString m_pendingMessages
;
488 // critical section to protect access to m_pendingMessages
489 wxCriticalSection m_critSection
;
490 #endif // wxUSE_THREADS
493 DECLARE_EVENT_TABLE()
494 wxDECLARE_NO_COPY_CLASS(wxLogFrame
);
497 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
498 // wxLogWindow menu events
499 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
501 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
502 #endif // CAN_SAVE_FILES
503 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
505 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
508 wxLogFrame::wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
)
509 : wxFrame(pParent
, wxID_ANY
, szTitle
)
513 m_pTextCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
517 // needed for Win32 to avoid 65Kb limit but it doesn't work well
518 // when using RichEdit 2.0 which we always do in the Unicode build
521 #endif // !wxUSE_UNICODE
526 wxMenuBar
*pMenuBar
= new wxMenuBar
;
527 wxMenu
*pMenu
= new wxMenu
;
529 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
530 #endif // CAN_SAVE_FILES
531 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
532 pMenu
->AppendSeparator();
533 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
534 pMenuBar
->Append(pMenu
, _("&Log"));
535 SetMenuBar(pMenuBar
);
536 #endif // wxUSE_MENUS
539 // status bar for menu prompts
541 #endif // wxUSE_STATUSBAR
543 m_log
->OnFrameCreate(this);
546 void wxLogFrame::DoClose()
548 if ( m_log
->OnFrameClose(this) )
550 // instead of closing just hide the window to be able to Show() it
556 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
561 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
567 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
571 int rc
= OpenLogFile(file
, &filename
, this);
580 // retrieve text and save it
581 // -------------------------
582 int nLines
= m_pTextCtrl
->GetNumberOfLines();
583 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
584 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
585 wxTextFile::GetEOL());
592 wxLogError(_("Can't save log contents to file."));
595 wxLogStatus((wxFrame
*)this, _("Log saved to the file '%s'."), filename
.c_str());
598 #endif // CAN_SAVE_FILES
600 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
602 m_pTextCtrl
->Clear();
605 void wxLogFrame::OnInternalIdle()
608 wxCRIT_SECT_LOCKER(locker
, m_critSection
);
610 const size_t count
= m_pendingMessages
.size();
611 for ( size_t n
= 0; n
< count
; n
++ )
613 DoShowLogMessage(m_pendingMessages
[n
]);
616 m_pendingMessages
.clear();
617 } // release m_critSection
619 wxFrame::OnInternalIdle();
622 void wxLogFrame::AddLogMessage(const wxString
& message
)
624 wxCRIT_SECT_LOCKER(locker
, m_critSection
);
627 if ( !wxThread::IsMain() || !m_pendingMessages
.empty() )
629 // message needs to be queued for later showing
630 m_pendingMessages
.Add(message
);
634 else // we are the main thread and no messages are queued, so we can
635 // log the message directly
636 #endif // wxUSE_THREADS
638 DoShowLogMessage(message
);
642 wxLogFrame::~wxLogFrame()
644 m_log
->OnFrameDelete(this);
650 wxLogWindow::wxLogWindow(wxWindow
*pParent
,
651 const wxString
& szTitle
,
655 PassMessages(bDoPass
);
657 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
663 void wxLogWindow::Show(bool bShow
)
665 m_pLogFrame
->Show(bShow
);
668 void wxLogWindow::DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
670 // first let the previous logger show it
671 wxLogPassThrough::DoLogTextAtLevel(level
, msg
);
676 // don't put trace messages in the text window for 2 reasons:
677 // 1) there are too many of them
678 // 2) they may provoke other trace messages (e.g. wxMSW code uses
679 // wxLogTrace to log Windows messages and adding text to the control
680 // sends more of them) thus sending a program into an infinite loop
681 if ( level
== wxLOG_Trace
)
684 m_pLogFrame
->AddLogMessage(msg
);
687 wxFrame
*wxLogWindow::GetFrame() const
692 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
696 bool wxLogWindow::OnFrameClose(wxFrame
* WXUNUSED(frame
))
702 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
707 wxLogWindow::~wxLogWindow()
709 // may be NULL if log frame already auto destroyed itself
713 #endif // wxUSE_LOGWINDOW
715 // ----------------------------------------------------------------------------
717 // ----------------------------------------------------------------------------
721 wxString
wxLogDialog::ms_details
;
722 size_t wxLogDialog::ms_maxLength
= 0;
724 wxLogDialog::wxLogDialog(wxWindow
*parent
,
725 const wxArrayString
& messages
,
726 const wxArrayInt
& severity
,
727 const wxArrayLong
& times
,
728 const wxString
& caption
,
730 : wxDialog(parent
, wxID_ANY
, caption
,
731 wxDefaultPosition
, wxDefaultSize
,
732 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
734 // init the static variables:
736 if ( ms_details
.empty() )
738 // ensure that we won't loop here if wxGetTranslation()
739 // happens to pop up a Log message while translating this :-)
740 ms_details
= wxTRANSLATE("&Details");
741 ms_details
= wxGetTranslation(ms_details
);
742 #ifdef __SMARTPHONE__
743 ms_details
= wxStripMenuCodes(ms_details
);
747 if ( ms_maxLength
== 0 )
749 ms_maxLength
= (2 * wxGetDisplaySize().x
/3) / GetCharWidth();
752 size_t count
= messages
.GetCount();
753 m_messages
.Alloc(count
);
754 m_severity
.Alloc(count
);
755 m_times
.Alloc(count
);
757 for ( size_t n
= 0; n
< count
; n
++ )
759 m_messages
.Add(messages
[n
]);
760 m_severity
.Add(severity
[n
]);
761 m_times
.Add(times
[n
]);
766 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
768 // create the controls which are always shown and layout them: we use
769 // sizers even though our window is not resizeable to calculate the size of
770 // the dialog properly
771 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
772 wxBoxSizer
*sizerAll
= new wxBoxSizer(isPda
? wxVERTICAL
: wxHORIZONTAL
);
776 wxStaticBitmap
*icon
= new wxStaticBitmap
780 wxArtProvider::GetMessageBoxIcon(style
)
782 sizerAll
->Add(icon
, wxSizerFlags().Centre());
785 // create the text sizer with a minimal size so that we are sure it won't be too small
786 wxString message
= EllipsizeString(messages
.Last());
787 wxSizer
*szText
= CreateTextSizer(message
);
788 szText
->SetMinSize(wxMin(300, wxGetDisplaySize().x
/ 3), -1);
790 sizerAll
->Add(szText
, wxSizerFlags(1).Centre().Border(wxLEFT
| wxRIGHT
));
792 wxButton
*btnOk
= new wxButton(this, wxID_OK
);
793 sizerAll
->Add(btnOk
, wxSizerFlags().Centre());
795 sizerTop
->Add(sizerAll
, wxSizerFlags().Expand().Border());
798 // add the details pane
799 #ifndef __SMARTPHONE__
800 wxCollapsiblePane
* const
801 collpane
= new wxCollapsiblePane(this, wxID_ANY
, ms_details
);
802 sizerTop
->Add(collpane
, wxSizerFlags(1).Expand().Border());
804 wxWindow
*win
= collpane
->GetPane();
805 wxSizer
* const paneSz
= new wxBoxSizer(wxVERTICAL
);
807 CreateDetailsControls(win
);
809 paneSz
->Add(m_listctrl
, wxSizerFlags(1).Expand().Border(wxTOP
));
811 #if wxUSE_CLIPBOARD || CAN_SAVE_FILES
812 wxBoxSizer
* const btnSizer
= new wxBoxSizer(wxHORIZONTAL
);
814 wxSizerFlags flagsBtn
;
815 flagsBtn
.Border(wxLEFT
);
818 btnSizer
->Add(new wxButton(win
, wxID_COPY
), flagsBtn
);
819 #endif // wxUSE_CLIPBOARD
822 btnSizer
->Add(new wxButton(win
, wxID_SAVE
), flagsBtn
);
823 #endif // CAN_SAVE_FILES
825 paneSz
->Add(btnSizer
, wxSizerFlags().Right().Border(wxTOP
));
826 #endif // wxUSE_CLIPBOARD || CAN_SAVE_FILES
828 win
->SetSizer(paneSz
);
829 paneSz
->SetSizeHints(win
);
830 #else // __SMARTPHONE__
831 SetLeftMenu(wxID_OK
);
832 SetRightMenu(wxID_MORE
, ms_details
+ EXPAND_SUFFIX
);
833 #endif // __SMARTPHONE__/!__SMARTPHONE__
835 SetSizerAndFit(sizerTop
);
841 // Move up the screen so that when we expand the dialog,
842 // there's enough space.
843 Move(wxPoint(GetPosition().x
, GetPosition().y
/ 2));
847 void wxLogDialog::CreateDetailsControls(wxWindow
*parent
)
849 wxString fmt
= wxLog::GetTimestamp();
850 bool hasTimeStamp
= !fmt
.IsEmpty();
852 // create the list ctrl now
853 m_listctrl
= new wxListCtrl(parent
, wxID_ANY
,
854 wxDefaultPosition
, wxDefaultSize
,
860 // This makes a big aesthetic difference on WinCE but I
861 // don't want to risk problems on other platforms
865 // no need to translate these strings as they're not shown to the
866 // user anyhow (we use wxLC_NO_HEADER style)
867 m_listctrl
->InsertColumn(0, _T("Message"));
870 m_listctrl
->InsertColumn(1, _T("Time"));
872 // prepare the imagelist
873 static const int ICON_SIZE
= 16;
874 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
876 // order should be the same as in the switch below!
877 static const wxChar
* icons
[] =
884 bool loadedIcons
= true;
886 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
888 wxBitmap bmp
= wxArtProvider::GetBitmap(icons
[icon
], wxART_MESSAGE_BOX
,
889 wxSize(ICON_SIZE
, ICON_SIZE
));
891 // This may very well fail if there are insufficient colours available.
892 // Degrade gracefully.
903 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
906 size_t count
= m_messages
.GetCount();
907 for ( size_t n
= 0; n
< count
; n
++ )
913 switch ( m_severity
[n
] )
927 else // failed to load images
932 wxString msg
= m_messages
[n
];
933 msg
.Replace(wxT("\n"), wxT(" "));
934 msg
= EllipsizeString(msg
);
936 m_listctrl
->InsertItem(n
, msg
, image
);
939 m_listctrl
->SetItem(n
, 1, TimeStamp(fmt
, (time_t)m_times
[n
]));
942 // let the columns size themselves
943 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
945 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
947 // calculate an approximately nice height for the listctrl
948 int height
= GetCharHeight()*(count
+ 4);
950 // but check that the dialog won't fall fown from the screen
952 // we use GetMinHeight() to get the height of the dialog part without the
953 // details and we consider that the "Save" button below and the separator
954 // line (and the margins around it) take about as much, hence double it
955 int heightMax
= wxGetDisplaySize().y
- GetPosition().y
- 2*GetMinHeight();
957 // we should leave a margin
961 m_listctrl
->SetSize(wxDefaultCoord
, wxMin(height
, heightMax
));
964 void wxLogDialog::OnListItemActivated(wxListEvent
& event
)
966 // show the activated item in a message box
967 // This allow the user to correctly display the logs which are longer
968 // than the listctrl and thus gets truncated or those which contains
972 // wxString str = m_listctrl->GetItemText(event.GetIndex());
973 // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW.
974 wxString str
= m_messages
[event
.GetIndex()];
976 // wxMessageBox will nicely handle the '\n' in the string (if any)
977 // and supports long strings
978 wxMessageBox(str
, wxT("Log message"), wxOK
, this);
981 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
986 #if CAN_SAVE_FILES || wxUSE_CLIPBOARD
988 wxString
wxLogDialog::GetLogMessages() const
990 wxString fmt
= wxLog::GetTimestamp();
993 // use the default format
997 const size_t count
= m_messages
.GetCount();
1000 text
.reserve(count
*m_messages
[0].length());
1001 for ( size_t n
= 0; n
< count
; n
++ )
1003 text
<< TimeStamp(fmt
, (time_t)m_times
[n
])
1006 << wxTextFile::GetEOL();
1012 #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD
1016 void wxLogDialog::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1018 wxClipboardLocker clip
;
1020 !wxTheClipboard
->AddData(new wxTextDataObject(GetLogMessages())) )
1022 wxLogError(_("Failed to copy dialog contents to the clipboard."));
1026 #endif // wxUSE_CLIPBOARD
1030 void wxLogDialog::OnSave(wxCommandEvent
& WXUNUSED(event
))
1033 int rc
= OpenLogFile(file
, NULL
, this);
1040 if ( !rc
|| !file
.Write(GetLogMessages()) || !file
.Close() )
1042 wxLogError(_("Can't save log contents to file."));
1046 #endif // CAN_SAVE_FILES
1048 wxLogDialog::~wxLogDialog()
1052 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
1056 #endif // wxUSE_LOG_DIALOG
1060 // pass an uninitialized file object, the function will ask the user for the
1061 // filename and try to open it, returns true on success (file was opened),
1062 // false if file couldn't be opened/created and -1 if the file selection
1063 // dialog was cancelled
1064 static int OpenLogFile(wxFile
& file
, wxString
*pFilename
, wxWindow
*parent
)
1066 // get the file name
1067 // -----------------
1068 wxString filename
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent
);
1076 bool bOk
= true; // suppress warning about it being possible uninitialized
1077 if ( wxFile::Exists(filename
) ) {
1078 bool bAppend
= false;
1080 strMsg
.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
1082 switch ( wxMessageBox(strMsg
, _("Question"),
1083 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
) ) {
1096 wxFAIL_MSG(_("invalid message box return value"));
1100 bOk
= file
.Open(filename
, wxFile::write_append
);
1103 bOk
= file
.Create(filename
, true /* overwrite */);
1107 bOk
= file
.Create(filename
);
1111 *pFilename
= filename
;
1116 #endif // CAN_SAVE_FILES
1118 #endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
1120 #if wxUSE_LOG && wxUSE_TEXTCTRL
1122 // ----------------------------------------------------------------------------
1123 // wxLogTextCtrl implementation
1124 // ----------------------------------------------------------------------------
1126 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
1128 m_pTextCtrl
= pTextCtrl
;
1131 void wxLogTextCtrl::DoLogText(const wxString
& msg
)
1133 m_pTextCtrl
->AppendText(msg
+ wxS('\n'));
1136 #endif // wxUSE_LOG && wxUSE_TEXTCTRL