]>
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
);
318 #if !wxUSE_LOG_DIALOG
319 // discard earlier informational messages if this is the 1st
320 // error because they might not make sense any more and showing
321 // them in a message box might be confusing
325 #endif // wxUSE_LOG_DIALOG
332 // for the warning we don't discard the info messages
336 m_aMessages
.Add(szString
);
337 m_aSeverity
.Add(level
);
338 m_aTimes
.Add((long)t
);
339 m_bHasMessages
= TRUE
;
344 // ----------------------------------------------------------------------------
345 // wxLogWindow and wxLogFrame implementation
346 // ----------------------------------------------------------------------------
350 class wxLogFrame
: public wxFrame
354 wxLogFrame(wxFrame
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
);
355 virtual ~wxLogFrame();
358 void OnClose(wxCommandEvent
& event
);
359 void OnCloseWindow(wxCloseEvent
& event
);
361 void OnSave (wxCommandEvent
& event
);
363 void OnClear(wxCommandEvent
& event
);
365 void OnIdle(wxIdleEvent
&);
368 wxTextCtrl
*TextCtrl() const { return m_pTextCtrl
; }
378 // instead of closing just hide the window to be able to Show() it later
379 void DoClose() { Show(FALSE
); }
381 wxTextCtrl
*m_pTextCtrl
;
384 DECLARE_EVENT_TABLE()
387 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
388 // wxLogWindow menu events
389 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
391 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
393 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
395 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
398 wxLogFrame::wxLogFrame(wxFrame
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
)
399 : wxFrame(pParent
, -1, szTitle
)
403 m_pTextCtrl
= new wxTextCtrl(this, -1, wxEmptyString
, wxDefaultPosition
,
410 wxMenuBar
*pMenuBar
= new wxMenuBar
;
411 wxMenu
*pMenu
= new wxMenu
;
413 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
415 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
416 pMenu
->AppendSeparator();
417 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
418 pMenuBar
->Append(pMenu
, _("&Log"));
419 SetMenuBar(pMenuBar
);
422 // status bar for menu prompts
424 #endif // wxUSE_STATUSBAR
426 m_log
->OnFrameCreate(this);
429 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
434 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
440 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
444 const wxChar
*szFileName
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"));
445 if ( szFileName
== NULL
) {
454 if ( wxFile::Exists(szFileName
) ) {
455 bool bAppend
= FALSE
;
457 strMsg
.Printf(_("Append log to file '%s' "
458 "(choosing [No] will overwrite it)?"), szFileName
);
459 switch ( wxMessageBox(strMsg
, _("Question"), wxYES_NO
| wxCANCEL
) ) {
472 wxFAIL_MSG(_("invalid message box return value"));
476 bOk
= file
.Open(szFileName
, wxFile::write_append
);
479 bOk
= file
.Create(szFileName
, TRUE
/* overwrite */);
483 bOk
= file
.Create(szFileName
);
486 // retrieve text and save it
487 // -------------------------
488 int nLines
= m_pTextCtrl
->GetNumberOfLines();
489 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
490 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
491 // we're not going to pull in the whole wxTextFile if all we need is this...
494 #else // !wxUSE_TEXTFILE
496 #endif // wxUSE_TEXTFILE
504 wxLogError(_("Can't save log contents to file."));
507 wxLogStatus(this, _("Log saved to the file '%s'."), szFileName
);
512 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
514 m_pTextCtrl
->Clear();
517 wxLogFrame::~wxLogFrame()
519 m_log
->OnFrameDelete(this);
524 wxLogWindow::wxLogWindow(wxFrame
*pParent
,
525 const wxChar
*szTitle
,
529 m_bPassMessages
= bDoPass
;
531 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
532 m_pOldLog
= wxLog::SetActiveTarget(this);
535 m_pLogFrame
->Show(TRUE
);
538 void wxLogWindow::Show(bool bShow
)
540 m_pLogFrame
->Show(bShow
);
543 void wxLogWindow::Flush()
545 if ( m_pOldLog
!= NULL
)
548 m_bHasMessages
= FALSE
;
551 void wxLogWindow::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
553 // first let the previous logger show it
554 if ( m_pOldLog
!= NULL
&& m_bPassMessages
) {
555 // bogus cast just to access protected DoLog
556 ((wxLogWindow
*)m_pOldLog
)->DoLog(level
, szString
, t
);
562 // by default, these messages are ignored by wxLog, so process
564 if ( !wxIsEmpty(szString
) )
567 str
<< _("Status: ") << szString
;
572 // don't put trace messages in the text window for 2 reasons:
573 // 1) there are too many of them
574 // 2) they may provoke other trace messages thus sending a program
575 // into an infinite loop
580 // and this will format it nicely and call our DoLogString()
581 wxLog::DoLog(level
, szString
, t
);
585 m_bHasMessages
= TRUE
;
588 void wxLogWindow::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
590 // put the text into our window
591 wxTextCtrl
*pText
= m_pLogFrame
->TextCtrl();
593 // remove selection (WriteText is in fact ReplaceSelection)
595 long nLen
= pText
->GetLastPosition();
596 pText
->SetSelection(nLen
, nLen
);
601 msg
<< szString
<< wxT('\n');
603 pText
->AppendText(msg
);
605 // TODO ensure that the line can be seen
608 wxFrame
*wxLogWindow::GetFrame() const
613 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
617 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
619 m_pLogFrame
= (wxLogFrame
*)NULL
;
622 wxLogWindow::~wxLogWindow()
626 // may be NULL if log frame already auto destroyed itself
630 // ----------------------------------------------------------------------------
632 // ----------------------------------------------------------------------------
636 static const size_t MARGIN
= 10;
638 wxLogDialog::wxLogDialog(wxWindow
*parent
,
639 const wxArrayString
& messages
,
640 const wxArrayInt
& severity
,
641 const wxArrayLong
& times
,
642 const wxString
& caption
,
644 : wxDialog(parent
, -1, caption
),
645 m_messages(messages
), m_severity(severity
), m_times(times
)
647 m_showingDetails
= FALSE
; // not initially
648 m_listctrl
= (wxListCtrl
*)NULL
;
650 // create the controls which are always shown and layout them: we use
651 // sizers even though our window is not resizeable to calculate the size of
652 // the dialog properly
653 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
654 wxBoxSizer
*sizerButtons
= new wxBoxSizer(wxVERTICAL
);
655 wxBoxSizer
*sizerAll
= new wxBoxSizer(wxHORIZONTAL
);
657 wxButton
*btnOk
= new wxButton(this, wxID_OK
, _T("Ok"));
658 sizerButtons
->Add(btnOk
, 0, wxCENTRE
|wxBOTTOM
, MARGIN
/2);
659 m_btnDetails
= new wxButton(this, wxID_MORE
, _T("&Details >>"));
660 sizerButtons
->Add(m_btnDetails
, 0, wxCENTRE
|wxTOP
, MARGIN
/2 - 1);
662 wxIcon icon
= wxTheApp
->GetStdIcon(style
& wxICON_MASK
);
663 sizerAll
->Add(new wxStaticBitmap(this, -1, icon
), 0, wxCENTRE
);
664 const wxString
& message
= messages
.Last();
665 sizerAll
->Add(CreateTextSizer(message
), 0, wxCENTRE
|wxLEFT
|wxRIGHT
, MARGIN
);
666 sizerAll
->Add(sizerButtons
, 0, wxALIGN_RIGHT
|wxLEFT
, MARGIN
);
668 sizerTop
->Add(sizerAll
, 0, wxCENTRE
|wxALL
, MARGIN
);
673 sizerTop
->SetSizeHints(this);
678 if ( m_messages
.GetCount() == 1 )
680 // no details... it's easier to disable a button than to change the
681 // dialog layout depending on whether we have details or not
682 m_btnDetails
->Disable();
688 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
693 void wxLogDialog::OnDetails(wxCommandEvent
& WXUNUSED(event
))
695 wxSizer
*sizer
= GetSizer();
697 if ( m_showingDetails
)
699 m_btnDetails
->SetLabel(_T("&Details >>"));
701 sizer
->Remove(m_listctrl
);
703 else // show details now
705 m_btnDetails
->SetLabel(_T("<< &Details"));
710 m_listctrl
= new wxListCtrl(this, -1,
711 wxDefaultPosition
, wxDefaultSize
,
715 m_listctrl
->InsertColumn(0, _("Message"));
716 m_listctrl
->InsertColumn(1, _("Time"));
718 // prepare the imagelist
719 static const int ICON_SIZE
= 16;
720 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
722 // order should be the same as in the switch below!
723 static const int icons
[] =
730 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
732 wxBitmap bmp
= wxTheApp
->GetStdIcon(icons
[icon
]);
733 imageList
->Add(wxImage(bmp
).
734 Rescale(ICON_SIZE
, ICON_SIZE
).
738 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
741 wxString fmt
= wxLog::GetTimestamp();
748 size_t count
= m_messages
.GetCount();
749 for ( size_t n
= 0; n
< count
; n
++ )
752 switch ( m_severity
[n
] )
766 m_listctrl
->InsertItem(n
, m_messages
[n
], image
);
767 m_listctrl
->SetItem(n
, 1,
768 wxDateTime((time_t)m_times
[n
]).Format(fmt
));
771 // let the columns size themselves
772 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
773 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
775 // get the approx height of the listctrl
776 wxFont font
= GetFont();
778 font
= *wxSWISS_FONT
;
781 GetTextExtent(_T("H"), (int*)NULL
, &y
, (int*)NULL
, (int*)NULL
, &font
);
782 int height
= wxMin(y
*(count
+ 3), 100);
783 m_listctrl
->SetSize(-1, height
);
786 sizer
->Add(m_listctrl
, 1, wxEXPAND
|(wxALL
& ~wxTOP
), MARGIN
);
789 m_showingDetails
= !m_showingDetails
;
791 // in any case, our size changed - update
792 sizer
->SetSizeHints(this);
796 wxLogDialog::~wxLogDialog()
800 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
804 #endif // wxUSE_LOG_DIALOG