]>
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
);
89 // the data for the listctrl
90 wxArrayString m_messages
;
91 wxArrayInt m_severity
;
94 // the "toggle" button and its state
95 wxButton
*m_btnDetails
;
96 bool m_showingDetails
;
98 // the listctrl (not shown initially)
99 wxListCtrl
*m_listctrl
;
101 DECLARE_EVENT_TABLE()
104 BEGIN_EVENT_TABLE(wxLogDialog
, wxDialog
)
105 EVT_BUTTON(wxID_OK
, wxLogDialog::OnOk
)
106 EVT_BUTTON(wxID_MORE
, wxLogDialog::OnDetails
)
109 #endif // wxUSE_LOG_DIALOG
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 // we use a global variable to store the frame pointer for wxLogStatus - bad,
116 // but it's he easiest way
117 static wxFrame
*gs_pFrame
; // FIXME MT-unsafe
119 // ============================================================================
121 // ============================================================================
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 // accepts an additional argument which tells to which frame the output should
129 void wxLogStatus(wxFrame
*pFrame
, const wxChar
*szFormat
, ...)
133 wxLog
*pLog
= wxLog::GetActiveTarget();
134 if ( pLog
!= NULL
) {
136 va_start(argptr
, szFormat
);
137 msg
.PrintfV(szFormat
, argptr
);
140 wxASSERT( gs_pFrame
== NULL
); // should be reset!
142 wxLog::OnLog(wxLOG_Status
, msg
, time(NULL
));
143 gs_pFrame
= (wxFrame
*) NULL
;
147 // ----------------------------------------------------------------------------
148 // wxLogTextCtrl implementation
149 // ----------------------------------------------------------------------------
151 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
153 m_pTextCtrl
= pTextCtrl
;
156 void wxLogTextCtrl::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
160 msg
<< szString
<< wxT('\n');
162 m_pTextCtrl
->AppendText(msg
);
165 // ----------------------------------------------------------------------------
166 // wxLogGui implementation (FIXME MT-unsafe)
167 // ----------------------------------------------------------------------------
174 void wxLogGui::Clear()
176 m_bErrors
= m_bWarnings
= FALSE
;
182 void wxLogGui::Flush()
184 if ( !m_bHasMessages
)
187 // do it right now to block any new calls to Flush() while we're here
188 m_bHasMessages
= FALSE
;
190 wxString title
= wxTheApp
->GetAppName();
193 title
[0u] = wxToupper(title
[0u]);
202 else if ( m_bWarnings
) {
203 title
+= _("Warning");
204 style
= wxICON_EXCLAMATION
;
207 title
+= _("Information");
208 style
= wxICON_INFORMATION
;
212 wxLogDialog
dlg(wxTheApp
->GetTopWindow(),
213 m_aMessages
, m_aSeverity
, m_aTimes
,
216 // clear the message list before showing the dialog because while it's
217 // shown some new messages may appear
220 (void)dlg
.ShowModal();
222 #else // !wxUSE_LOG_DIALOG
223 // concatenate all strings (but not too many to not overfill the msg box)
226 nMsgCount
= m_aMessages
.Count();
228 // start from the most recent message
229 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
230 // for Windows strings longer than this value are wrapped (NT 4.0)
231 const size_t nMsgLineWidth
= 156;
233 nLines
+= (m_aMessages
[n
- 1].Len() + nMsgLineWidth
- 1) / nMsgLineWidth
;
235 if ( nLines
> 25 ) // don't put too many lines in message box
238 str
<< m_aMessages
[n
- 1] << wxT("\n");
241 wxMessageBox(str
, title
, wxOK
| style
);
243 // no undisplayed messages whatsoever
245 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
248 m_bHasMessages
= FALSE
;
251 // the default behaviour is to discard all informational messages if there
252 // are any errors/warnings.
253 void wxLogGui::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
261 m_aMessages
.Add(szString
);
262 m_aSeverity
.Add(wxLOG_Message
);
263 m_aTimes
.Add((long)t
);
264 m_bHasMessages
= TRUE
;
272 // find the top window and set it's status text if it has any
273 wxFrame
*pFrame
= gs_pFrame
;
274 if ( pFrame
== NULL
) {
275 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
276 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
277 pFrame
= (wxFrame
*)pWin
;
281 if ( pFrame
&& pFrame
->GetStatusBar() )
282 pFrame
->SetStatusText(szString
);
284 #endif // wxUSE_STATUSBAR
292 // don't prepend debug/trace here: it goes to the
293 // debug window anyhow, but do put a timestamp
296 str
<< szString
<< wxT("\n\r");
297 OutputDebugString(str
);
299 // send them to stderr
300 wxFprintf(stderr
, wxT("%s: %s\n"),
301 level
== wxLOG_Trace
? wxT("Trace")
307 #endif // __WXDEBUG__
311 case wxLOG_FatalError
:
312 // show this one immediately
313 wxMessageBox(szString
, _("Fatal error"), wxICON_HAND
);
319 #if !wxUSE_LOG_DIALOG
320 // discard earlier informational messages if this is the 1st
321 // error because they might not make sense any more and showing
322 // them in a message box might be confusing
326 #endif // wxUSE_LOG_DIALOG
333 // for the warning we don't discard the info messages
337 m_aMessages
.Add(szString
);
338 m_aSeverity
.Add(level
);
339 m_aTimes
.Add((long)t
);
340 m_bHasMessages
= TRUE
;
345 // ----------------------------------------------------------------------------
346 // wxLogWindow and wxLogFrame implementation
347 // ----------------------------------------------------------------------------
351 class wxLogFrame
: public wxFrame
355 wxLogFrame(wxFrame
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
);
356 virtual ~wxLogFrame();
359 void OnClose(wxCommandEvent
& event
);
360 void OnCloseWindow(wxCloseEvent
& event
);
362 void OnSave (wxCommandEvent
& event
);
364 void OnClear(wxCommandEvent
& event
);
366 void OnIdle(wxIdleEvent
&);
369 wxTextCtrl
*TextCtrl() const { return m_pTextCtrl
; }
379 // instead of closing just hide the window to be able to Show() it later
380 void DoClose() { Show(FALSE
); }
382 wxTextCtrl
*m_pTextCtrl
;
385 DECLARE_EVENT_TABLE()
388 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
389 // wxLogWindow menu events
390 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
392 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
394 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
396 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
399 wxLogFrame::wxLogFrame(wxFrame
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
)
400 : wxFrame(pParent
, -1, szTitle
)
404 m_pTextCtrl
= new wxTextCtrl(this, -1, wxEmptyString
, wxDefaultPosition
,
411 wxMenuBar
*pMenuBar
= new wxMenuBar
;
412 wxMenu
*pMenu
= new wxMenu
;
414 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
416 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
417 pMenu
->AppendSeparator();
418 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
419 pMenuBar
->Append(pMenu
, _("&Log"));
420 SetMenuBar(pMenuBar
);
423 // status bar for menu prompts
425 #endif // wxUSE_STATUSBAR
427 m_log
->OnFrameCreate(this);
430 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
435 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
441 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
445 const wxChar
*szFileName
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"));
446 if ( szFileName
== NULL
) {
455 if ( wxFile::Exists(szFileName
) ) {
456 bool bAppend
= FALSE
;
458 strMsg
.Printf(_("Append log to file '%s' "
459 "(choosing [No] will overwrite it)?"), szFileName
);
460 switch ( wxMessageBox(strMsg
, _("Question"), wxYES_NO
| wxCANCEL
) ) {
473 wxFAIL_MSG(_("invalid message box return value"));
477 bOk
= file
.Open(szFileName
, wxFile::write_append
);
480 bOk
= file
.Create(szFileName
, TRUE
/* overwrite */);
484 bOk
= file
.Create(szFileName
);
487 // retrieve text and save it
488 // -------------------------
489 int nLines
= m_pTextCtrl
->GetNumberOfLines();
490 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
491 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
492 // we're not going to pull in the whole wxTextFile if all we need is this...
495 #else // !wxUSE_TEXTFILE
497 #endif // wxUSE_TEXTFILE
505 wxLogError(_("Can't save log contents to file."));
508 wxLogStatus(this, _("Log saved to the file '%s'."), szFileName
);
513 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
515 m_pTextCtrl
->Clear();
518 wxLogFrame::~wxLogFrame()
520 m_log
->OnFrameDelete(this);
525 wxLogWindow::wxLogWindow(wxFrame
*pParent
,
526 const wxChar
*szTitle
,
530 m_bPassMessages
= bDoPass
;
532 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
533 m_pOldLog
= wxLog::SetActiveTarget(this);
536 m_pLogFrame
->Show(TRUE
);
539 void wxLogWindow::Show(bool bShow
)
541 m_pLogFrame
->Show(bShow
);
544 void wxLogWindow::Flush()
546 if ( m_pOldLog
!= NULL
)
549 m_bHasMessages
= FALSE
;
552 void wxLogWindow::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
554 // first let the previous logger show it
555 if ( m_pOldLog
!= NULL
&& m_bPassMessages
) {
556 // bogus cast just to access protected DoLog
557 ((wxLogWindow
*)m_pOldLog
)->DoLog(level
, szString
, t
);
563 // by default, these messages are ignored by wxLog, so process
565 if ( !wxIsEmpty(szString
) )
568 str
<< _("Status: ") << szString
;
573 // don't put trace messages in the text window for 2 reasons:
574 // 1) there are too many of them
575 // 2) they may provoke other trace messages thus sending a program
576 // into an infinite loop
581 // and this will format it nicely and call our DoLogString()
582 wxLog::DoLog(level
, szString
, t
);
586 m_bHasMessages
= TRUE
;
589 void wxLogWindow::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
591 // put the text into our window
592 wxTextCtrl
*pText
= m_pLogFrame
->TextCtrl();
594 // remove selection (WriteText is in fact ReplaceSelection)
596 long nLen
= pText
->GetLastPosition();
597 pText
->SetSelection(nLen
, nLen
);
602 msg
<< szString
<< wxT('\n');
604 pText
->AppendText(msg
);
606 // TODO ensure that the line can be seen
609 wxFrame
*wxLogWindow::GetFrame() const
614 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
618 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
620 m_pLogFrame
= (wxLogFrame
*)NULL
;
623 wxLogWindow::~wxLogWindow()
627 // may be NULL if log frame already auto destroyed itself
631 // ----------------------------------------------------------------------------
633 // ----------------------------------------------------------------------------
637 static const size_t MARGIN
= 10;
639 wxLogDialog::wxLogDialog(wxWindow
*parent
,
640 const wxArrayString
& messages
,
641 const wxArrayInt
& severity
,
642 const wxArrayLong
& times
,
643 const wxString
& caption
,
645 : wxDialog(parent
, -1, caption
),
646 m_messages(messages
), m_severity(severity
), m_times(times
)
648 m_showingDetails
= FALSE
; // not initially
649 m_listctrl
= (wxListCtrl
*)NULL
;
651 // create the controls which are always shown and layout them: we use
652 // sizers even though our window is not resizeable to calculate the size of
653 // the dialog properly
654 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
655 wxBoxSizer
*sizerButtons
= new wxBoxSizer(wxVERTICAL
);
656 wxBoxSizer
*sizerAll
= new wxBoxSizer(wxHORIZONTAL
);
658 wxButton
*btnOk
= new wxButton(this, wxID_OK
, _T("OK"));
659 sizerButtons
->Add(btnOk
, 0, wxCENTRE
|wxBOTTOM
, MARGIN
/2);
660 m_btnDetails
= new wxButton(this, wxID_MORE
, _T("&Details >>"));
661 sizerButtons
->Add(m_btnDetails
, 0, wxCENTRE
|wxTOP
, MARGIN
/2 - 1);
663 wxIcon icon
= wxTheApp
->GetStdIcon(style
& wxICON_MASK
);
664 sizerAll
->Add(new wxStaticBitmap(this, -1, icon
), 0, wxCENTRE
);
665 const wxString
& message
= messages
.Last();
666 sizerAll
->Add(CreateTextSizer(message
), 0, wxCENTRE
|wxLEFT
|wxRIGHT
, MARGIN
);
667 sizerAll
->Add(sizerButtons
, 0, wxALIGN_RIGHT
|wxLEFT
, MARGIN
);
669 sizerTop
->Add(sizerAll
, 0, wxCENTRE
|wxALL
, MARGIN
);
674 sizerTop
->SetSizeHints(this);
679 if ( m_messages
.GetCount() == 1 )
681 // no details... it's easier to disable a button than to change the
682 // dialog layout depending on whether we have details or not
683 m_btnDetails
->Disable();
689 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
694 void wxLogDialog::OnDetails(wxCommandEvent
& WXUNUSED(event
))
696 wxSizer
*sizer
= GetSizer();
698 if ( m_showingDetails
)
700 m_btnDetails
->SetLabel(_T("&Details >>"));
702 sizer
->Remove(m_listctrl
);
704 else // show details now
706 m_btnDetails
->SetLabel(_T("<< &Details"));
711 m_listctrl
= new wxListCtrl(this, -1,
712 wxDefaultPosition
, wxDefaultSize
,
716 m_listctrl
->InsertColumn(0, _("Message"));
717 m_listctrl
->InsertColumn(1, _("Time"));
719 // prepare the imagelist
720 static const int ICON_SIZE
= 16;
721 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
723 // order should be the same as in the switch below!
724 static const int icons
[] =
731 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
733 wxBitmap bmp
= wxTheApp
->GetStdIcon(icons
[icon
]);
734 imageList
->Add(wxImage(bmp
).
735 Rescale(ICON_SIZE
, ICON_SIZE
).
739 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
742 wxString fmt
= wxLog::GetTimestamp();
749 size_t count
= m_messages
.GetCount();
750 for ( size_t n
= 0; n
< count
; n
++ )
753 switch ( m_severity
[n
] )
767 m_listctrl
->InsertItem(n
, m_messages
[n
], image
);
768 m_listctrl
->SetItem(n
, 1,
769 wxDateTime((time_t)m_times
[n
]).Format(fmt
));
772 // let the columns size themselves
773 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
774 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
776 // get the approx height of the listctrl
777 wxFont font
= GetFont();
779 font
= *wxSWISS_FONT
;
782 GetTextExtent(_T("H"), (int*)NULL
, &y
, (int*)NULL
, (int*)NULL
, &font
);
783 int height
= wxMin(y
*(count
+ 3), 100);
784 m_listctrl
->SetSize(-1, height
);
787 sizer
->Add(m_listctrl
, 1, wxEXPAND
|(wxALL
& ~wxTOP
), MARGIN
);
790 m_showingDetails
= !m_showingDetails
;
792 // in any case, our size changed - update
793 sizer
->SetSizeHints(this);
797 wxLogDialog::~wxLogDialog()
801 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
805 #endif // wxUSE_LOG_DIALOG