]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/logg.cpp
1 /////////////////////////////////////////////////////////////////////////////
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 license
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // no #pragma implementation "log.h" because it's already in src/common/log.cpp
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
31 #error "This file can't be compiled without GUI!"
40 #include "wx/filedlg.h"
41 #include "wx/msgdlg.h"
42 #include "wx/textctrl.h"
44 #include "wx/statbmp.h"
48 #include "wx/textfile.h"
51 // for OutputDebugString()
52 #include "wx/msw/private.h"
55 // may be defined to 0 for old behavior (using wxMessageBox) - shouldn't be
56 // changed normally (that's why it's here and not in setup.h)
57 #define wxUSE_LOG_DIALOG 1
60 #include "wx/datetime.h"
61 #include "wx/listctrl.h"
63 #else // !wxUSE_TEXTFILE
64 #include "wx/msgdlg.h"
65 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
73 class wxLogDialog
: public wxDialog
76 wxLogDialog(wxWindow
*parent
,
77 const wxArrayString
& messages
,
78 const wxArrayInt
& severity
,
79 const wxArrayLong
& timess
,
80 const wxString
& caption
,
82 virtual ~wxLogDialog();
85 void OnOk(wxCommandEvent
& event
);
86 void OnDetails(wxCommandEvent
& event
);
87 void OnListSelect(wxListEvent
& event
);
90 // the data for the listctrl
91 wxArrayString m_messages
;
92 wxArrayInt m_severity
;
95 // the "toggle" button and its state
96 wxButton
*m_btnDetails
;
97 bool m_showingDetails
;
99 // the listctrl (not shown initially)
100 wxListCtrl
*m_listctrl
;
102 // the translated "Details" string
103 static wxString ms_details
;
105 DECLARE_EVENT_TABLE()
108 BEGIN_EVENT_TABLE(wxLogDialog
, wxDialog
)
109 EVT_BUTTON(wxID_OK
, wxLogDialog::OnOk
)
110 EVT_BUTTON(wxID_MORE
, wxLogDialog::OnDetails
)
111 EVT_LIST_ITEM_SELECTED(-1, wxLogDialog::OnListSelect
)
114 #endif // wxUSE_LOG_DIALOG
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 // we use a global variable to store the frame pointer for wxLogStatus - bad,
121 // but it's he easiest way
122 static wxFrame
*gs_pFrame
; // FIXME MT-unsafe
124 // ============================================================================
126 // ============================================================================
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
132 // accepts an additional argument which tells to which frame the output should
134 void wxLogStatus(wxFrame
*pFrame
, const wxChar
*szFormat
, ...)
138 wxLog
*pLog
= wxLog::GetActiveTarget();
139 if ( pLog
!= NULL
) {
141 va_start(argptr
, szFormat
);
142 msg
.PrintfV(szFormat
, argptr
);
145 wxASSERT( gs_pFrame
== NULL
); // should be reset!
147 wxLog::OnLog(wxLOG_Status
, msg
, time(NULL
));
148 gs_pFrame
= (wxFrame
*) NULL
;
152 // ----------------------------------------------------------------------------
153 // wxLogTextCtrl implementation
154 // ----------------------------------------------------------------------------
156 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
158 m_pTextCtrl
= pTextCtrl
;
161 void wxLogTextCtrl::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
165 msg
<< szString
<< wxT('\n');
167 m_pTextCtrl
->AppendText(msg
);
170 // ----------------------------------------------------------------------------
171 // wxLogGui implementation (FIXME MT-unsafe)
172 // ----------------------------------------------------------------------------
176 // we must translate them here in the very beginning or we risk to have
177 // reentrancy problems when called from inside wxGetTranslation() leading
178 // to inifnite recursion
179 m_error
= _("Error");
180 m_warning
= _("Warning");
181 m_info
= _("Information");
186 void wxLogGui::Clear()
190 m_bHasMessages
= FALSE
;
197 void wxLogGui::Flush()
199 if ( !m_bHasMessages
)
202 // do it right now to block any new calls to Flush() while we're here
203 m_bHasMessages
= FALSE
;
205 wxString title
= wxTheApp
->GetAppName();
208 title
[0u] = wxToupper(title
[0u]);
217 else if ( m_bWarnings
) {
219 style
= wxICON_EXCLAMATION
;
223 style
= wxICON_INFORMATION
;
226 // this is the best we can do here
227 wxWindow
*parent
= wxTheApp
->GetTopWindow();
229 size_t nMsgCount
= m_aMessages
.Count();
232 if ( nMsgCount
== 1 )
234 str
= m_aMessages
[0];
236 else // more than one message
239 wxLogDialog
dlg(parent
,
240 m_aMessages
, m_aSeverity
, m_aTimes
,
243 // clear the message list before showing the dialog because while it's
244 // shown some new messages may appear
247 (void)dlg
.ShowModal();
248 #else // !wxUSE_LOG_DIALOG
249 // concatenate all strings (but not too many to not overfill the msg box)
252 // start from the most recent message
253 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
254 // for Windows strings longer than this value are wrapped (NT 4.0)
255 const size_t nMsgLineWidth
= 156;
257 nLines
+= (m_aMessages
[n
- 1].Len() + nMsgLineWidth
- 1) / nMsgLineWidth
;
259 if ( nLines
> 25 ) // don't put too many lines in message box
262 str
<< m_aMessages
[n
- 1] << wxT("\n");
264 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
267 // this catches both cases of 1 message with wxUSE_LOG_DIALOG and any
268 // situation without it
271 wxMessageBox(str
, title
, wxOK
| style
, parent
);
273 // no undisplayed messages whatsoever
278 // log all kinds of messages
279 void wxLogGui::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
287 m_aMessages
.Add(szString
);
288 m_aSeverity
.Add(wxLOG_Message
);
289 m_aTimes
.Add((long)t
);
290 m_bHasMessages
= TRUE
;
298 // find the top window and set it's status text if it has any
299 wxFrame
*pFrame
= gs_pFrame
;
300 if ( pFrame
== NULL
) {
301 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
302 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
303 pFrame
= (wxFrame
*)pWin
;
307 if ( pFrame
&& pFrame
->GetStatusBar() )
308 pFrame
->SetStatusText(szString
);
310 #endif // wxUSE_STATUSBAR
318 // don't prepend debug/trace here: it goes to the
319 // debug window anyhow, but do put a timestamp
322 str
<< szString
<< wxT("\n\r");
323 OutputDebugString(str
);
325 // send them to stderr
326 wxFprintf(stderr
, wxT("%s: %s\n"),
327 level
== wxLOG_Trace
? wxT("Trace")
333 #endif // __WXDEBUG__
337 case wxLOG_FatalError
:
338 // show this one immediately
339 wxMessageBox(szString
, _("Fatal error"), wxICON_HAND
);
345 #if !wxUSE_LOG_DIALOG
346 // discard earlier informational messages if this is the 1st
347 // error because they might not make sense any more and showing
348 // them in a message box might be confusing
352 #endif // wxUSE_LOG_DIALOG
359 // for the warning we don't discard the info messages
363 m_aMessages
.Add(szString
);
364 m_aSeverity
.Add((int)level
);
365 m_aTimes
.Add((long)t
);
366 m_bHasMessages
= TRUE
;
371 // ----------------------------------------------------------------------------
372 // wxLogWindow and wxLogFrame implementation
373 // ----------------------------------------------------------------------------
377 class wxLogFrame
: public wxFrame
381 wxLogFrame(wxFrame
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
);
382 virtual ~wxLogFrame();
385 void OnClose(wxCommandEvent
& event
);
386 void OnCloseWindow(wxCloseEvent
& event
);
388 void OnSave (wxCommandEvent
& event
);
390 void OnClear(wxCommandEvent
& event
);
392 void OnIdle(wxIdleEvent
&);
395 wxTextCtrl
*TextCtrl() const { return m_pTextCtrl
; }
398 // use standard ids for our commands!
401 Menu_Close
= wxID_CLOSE
,
402 Menu_Save
= wxID_SAVE
,
403 Menu_Clear
= wxID_CLEAR
406 // instead of closing just hide the window to be able to Show() it later
407 void DoClose() { Show(FALSE
); }
409 wxTextCtrl
*m_pTextCtrl
;
412 DECLARE_EVENT_TABLE()
415 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
416 // wxLogWindow menu events
417 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
419 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
421 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
423 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
426 wxLogFrame::wxLogFrame(wxFrame
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
)
427 : wxFrame(pParent
, -1, szTitle
)
431 m_pTextCtrl
= new wxTextCtrl(this, -1, wxEmptyString
, wxDefaultPosition
,
438 wxMenuBar
*pMenuBar
= new wxMenuBar
;
439 wxMenu
*pMenu
= new wxMenu
;
441 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
443 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
444 pMenu
->AppendSeparator();
445 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
446 pMenuBar
->Append(pMenu
, _("&Log"));
447 SetMenuBar(pMenuBar
);
450 // status bar for menu prompts
452 #endif // wxUSE_STATUSBAR
454 m_log
->OnFrameCreate(this);
457 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
462 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
468 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
472 const wxChar
*szFileName
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"));
473 if ( szFileName
== NULL
) {
482 if ( wxFile::Exists(szFileName
) ) {
483 bool bAppend
= FALSE
;
485 strMsg
.Printf(_("Append log to file '%s' "
486 "(choosing [No] will overwrite it)?"), szFileName
);
487 switch ( wxMessageBox(strMsg
, _("Question"), wxYES_NO
| wxCANCEL
) ) {
500 wxFAIL_MSG(_("invalid message box return value"));
504 bOk
= file
.Open(szFileName
, wxFile::write_append
);
507 bOk
= file
.Create(szFileName
, TRUE
/* overwrite */);
511 bOk
= file
.Create(szFileName
);
514 // retrieve text and save it
515 // -------------------------
516 int nLines
= m_pTextCtrl
->GetNumberOfLines();
517 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
518 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
519 // we're not going to pull in the whole wxTextFile if all we need is this...
522 #else // !wxUSE_TEXTFILE
524 #endif // wxUSE_TEXTFILE
532 wxLogError(_("Can't save log contents to file."));
535 wxLogStatus(this, _("Log saved to the file '%s'."), szFileName
);
540 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
542 m_pTextCtrl
->Clear();
545 wxLogFrame::~wxLogFrame()
547 m_log
->OnFrameDelete(this);
552 wxLogWindow::wxLogWindow(wxFrame
*pParent
,
553 const wxChar
*szTitle
,
557 m_bPassMessages
= bDoPass
;
559 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
560 m_pOldLog
= wxLog::SetActiveTarget(this);
563 m_pLogFrame
->Show(TRUE
);
566 void wxLogWindow::Show(bool bShow
)
568 m_pLogFrame
->Show(bShow
);
571 void wxLogWindow::Flush()
573 if ( m_pOldLog
!= NULL
)
576 m_bHasMessages
= FALSE
;
579 void wxLogWindow::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
581 // first let the previous logger show it
582 if ( m_pOldLog
!= NULL
&& m_bPassMessages
) {
583 // bogus cast just to access protected DoLog
584 ((wxLogWindow
*)m_pOldLog
)->DoLog(level
, szString
, t
);
590 // by default, these messages are ignored by wxLog, so process
592 if ( !wxIsEmpty(szString
) )
595 str
<< _("Status: ") << szString
;
600 // don't put trace messages in the text window for 2 reasons:
601 // 1) there are too many of them
602 // 2) they may provoke other trace messages thus sending a program
603 // into an infinite loop
608 // and this will format it nicely and call our DoLogString()
609 wxLog::DoLog(level
, szString
, t
);
613 m_bHasMessages
= TRUE
;
616 void wxLogWindow::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
618 // put the text into our window
619 wxTextCtrl
*pText
= m_pLogFrame
->TextCtrl();
621 // remove selection (WriteText is in fact ReplaceSelection)
623 long nLen
= pText
->GetLastPosition();
624 pText
->SetSelection(nLen
, nLen
);
629 msg
<< szString
<< wxT('\n');
631 pText
->AppendText(msg
);
633 // TODO ensure that the line can be seen
636 wxFrame
*wxLogWindow::GetFrame() const
641 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
645 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
647 m_pLogFrame
= (wxLogFrame
*)NULL
;
650 wxLogWindow::~wxLogWindow()
654 // may be NULL if log frame already auto destroyed itself
658 // ----------------------------------------------------------------------------
660 // ----------------------------------------------------------------------------
664 static const size_t MARGIN
= 10;
666 wxString
wxLogDialog::ms_details
;
668 wxLogDialog::wxLogDialog(wxWindow
*parent
,
669 const wxArrayString
& messages
,
670 const wxArrayInt
& severity
,
671 const wxArrayLong
& times
,
672 const wxString
& caption
,
674 : wxDialog(parent
, -1, caption
)
678 // ensure that we won't try to call wxGetTranslation() twice
679 ms_details
= _T("&Details");
680 ms_details
= wxGetTranslation(ms_details
);
683 size_t count
= messages
.GetCount();
684 m_messages
.Alloc(count
);
685 m_severity
.Alloc(count
);
686 m_times
.Alloc(count
);
688 for ( size_t n
= 0; n
< count
; n
++ )
690 wxString msg
= messages
[n
];
693 m_messages
.Add(msg
.BeforeFirst(_T('\n')));
694 msg
= msg
.AfterFirst(_T('\n'));
696 m_severity
.Add(severity
[n
]);
697 m_times
.Add(times
[n
]);
702 m_showingDetails
= FALSE
; // not initially
703 m_listctrl
= (wxListCtrl
*)NULL
;
705 // create the controls which are always shown and layout them: we use
706 // sizers even though our window is not resizeable to calculate the size of
707 // the dialog properly
708 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
709 wxBoxSizer
*sizerButtons
= new wxBoxSizer(wxVERTICAL
);
710 wxBoxSizer
*sizerAll
= new wxBoxSizer(wxHORIZONTAL
);
712 wxButton
*btnOk
= new wxButton(this, wxID_OK
, _("OK"));
713 sizerButtons
->Add(btnOk
, 0, wxCENTRE
|wxBOTTOM
, MARGIN
/2);
714 m_btnDetails
= new wxButton(this, wxID_MORE
, ms_details
+ _T(" >>"));
715 sizerButtons
->Add(m_btnDetails
, 0, wxCENTRE
|wxTOP
, MARGIN
/2 - 1);
717 wxIcon icon
= wxTheApp
->GetStdIcon((int)(style
& wxICON_MASK
));
718 sizerAll
->Add(new wxStaticBitmap(this, -1, icon
), 0, wxCENTRE
);
719 const wxString
& message
= messages
.Last();
720 sizerAll
->Add(CreateTextSizer(message
), 0, wxCENTRE
|wxLEFT
|wxRIGHT
, MARGIN
);
721 sizerAll
->Add(sizerButtons
, 0, wxALIGN_RIGHT
|wxLEFT
, MARGIN
);
723 sizerTop
->Add(sizerAll
, 0, wxCENTRE
|wxALL
, MARGIN
);
728 sizerTop
->SetSizeHints(this);
733 // this can't happen any more as we don't use this dialog in this case
737 // no details... it's easier to disable a button than to change the
738 // dialog layout depending on whether we have details or not
739 m_btnDetails
->Disable();
746 void wxLogDialog::OnListSelect(wxListEvent
& event
)
748 // we can't just disable the control because this looks ugly under Windows
749 // (wrong bg colour, no scrolling...), but we still want to disable
750 // selecting items - it makes no sense here
751 m_listctrl
->SetItemState(event
.GetIndex(), 0, wxLIST_STATE_SELECTED
);
754 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
759 void wxLogDialog::OnDetails(wxCommandEvent
& WXUNUSED(event
))
761 wxSizer
*sizer
= GetSizer();
763 if ( m_showingDetails
)
765 m_btnDetails
->SetLabel(ms_details
+ _T(">>"));
767 sizer
->Remove(m_listctrl
);
769 else // show details now
771 m_btnDetails
->SetLabel(wxString(_T("<< ")) + ms_details
);
776 m_listctrl
= new wxListCtrl(this, -1,
777 wxDefaultPosition
, wxDefaultSize
,
782 // no need to translate these strings as they're not shown to the
783 // user anyhow (we use wxLC_NO_HEADER style)
784 m_listctrl
->InsertColumn(0, _T("Message"));
785 m_listctrl
->InsertColumn(1, _T("Time"));
787 // prepare the imagelist
788 static const int ICON_SIZE
= 16;
789 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
791 // order should be the same as in the switch below!
792 static const int icons
[] =
799 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
801 wxBitmap bmp
= wxTheApp
->GetStdIcon(icons
[icon
]);
802 imageList
->Add(wxImage(bmp
).
803 Rescale(ICON_SIZE
, ICON_SIZE
).
807 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
810 wxString fmt
= wxLog::GetTimestamp();
817 size_t count
= m_messages
.GetCount();
818 for ( size_t n
= 0; n
< count
; n
++ )
821 switch ( m_severity
[n
] )
835 m_listctrl
->InsertItem(n
, m_messages
[n
], image
);
836 m_listctrl
->SetItem(n
, 1,
837 wxDateTime((time_t)m_times
[n
]).Format(fmt
));
840 // let the columns size themselves
841 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
842 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
844 // get the approx height of the listctrl
845 wxFont font
= GetFont();
847 font
= *wxSWISS_FONT
;
850 GetTextExtent(_T("H"), (int*)NULL
, &y
, (int*)NULL
, (int*)NULL
, &font
);
851 int height
= wxMin(y
*(count
+ 3), 100);
852 m_listctrl
->SetSize(-1, height
);
855 sizer
->Add(m_listctrl
, 1, wxEXPAND
|(wxALL
& ~wxTOP
), MARGIN
);
858 m_showingDetails
= !m_showingDetails
;
860 // in any case, our size changed - update
861 sizer
->SetSizeHints(this);
865 wxLogDialog::~wxLogDialog()
869 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
873 #endif // wxUSE_LOG_DIALOG