]>
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 const wxArrayString
& m_messages
;
91 const wxArrayInt
& m_severity
;
92 const wxArrayLong
& m_times
;
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
,
215 (void)dlg
.ShowModal();
217 #else // !wxUSE_LOG_DIALOG
218 // concatenate all strings (but not too many to not overfill the msg box)
221 nMsgCount
= m_aMessages
.Count();
223 // start from the most recent message
224 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
225 // for Windows strings longer than this value are wrapped (NT 4.0)
226 const size_t nMsgLineWidth
= 156;
228 nLines
+= (m_aMessages
[n
- 1].Len() + nMsgLineWidth
- 1) / nMsgLineWidth
;
230 if ( nLines
> 25 ) // don't put too many lines in message box
233 str
<< m_aMessages
[n
- 1] << wxT("\n");
236 wxMessageBox(str
, title
, wxOK
| style
);
237 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
239 // no undisplayed messages whatsoever
243 m_bHasMessages
= FALSE
;
246 // the default behaviour is to discard all informational messages if there
247 // are any errors/warnings.
248 void wxLogGui::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
256 m_aMessages
.Add(szString
);
257 m_aSeverity
.Add(wxLOG_Message
);
258 m_aTimes
.Add((long)t
);
259 m_bHasMessages
= TRUE
;
267 // find the top window and set it's status text if it has any
268 wxFrame
*pFrame
= gs_pFrame
;
269 if ( pFrame
== NULL
) {
270 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
271 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
272 pFrame
= (wxFrame
*)pWin
;
276 if ( pFrame
&& pFrame
->GetStatusBar() )
277 pFrame
->SetStatusText(szString
);
279 #endif // wxUSE_STATUSBAR
287 // don't prepend debug/trace here: it goes to the
288 // debug window anyhow, but do put a timestamp
291 str
<< szString
<< wxT("\n\r");
292 OutputDebugString(str
);
294 // send them to stderr
295 wxFprintf(stderr
, wxT("%s: %s\n"),
296 level
== wxLOG_Trace
? wxT("Trace")
302 #endif // __WXDEBUG__
306 case wxLOG_FatalError
:
307 // show this one immediately
308 wxMessageBox(szString
, _("Fatal error"), wxICON_HAND
);
313 #if !wxUSE_LOG_DIALOG
314 // discard earlier informational messages if this is the 1st
315 // error because they might not make sense any more and showing
316 // them in a message box might be confusing
320 #endif // wxUSE_LOG_DIALOG
327 // for the warning we don't discard the info messages
331 m_aMessages
.Add(szString
);
332 m_aSeverity
.Add(level
);
333 m_aTimes
.Add((long)t
);
334 m_bHasMessages
= TRUE
;
339 // ----------------------------------------------------------------------------
340 // wxLogWindow and wxLogFrame implementation
341 // ----------------------------------------------------------------------------
345 class wxLogFrame
: public wxFrame
349 wxLogFrame(wxFrame
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
);
350 virtual ~wxLogFrame();
353 void OnClose(wxCommandEvent
& event
);
354 void OnCloseWindow(wxCloseEvent
& event
);
356 void OnSave (wxCommandEvent
& event
);
358 void OnClear(wxCommandEvent
& event
);
360 void OnIdle(wxIdleEvent
&);
363 wxTextCtrl
*TextCtrl() const { return m_pTextCtrl
; }
373 // instead of closing just hide the window to be able to Show() it later
374 void DoClose() { Show(FALSE
); }
376 wxTextCtrl
*m_pTextCtrl
;
379 DECLARE_EVENT_TABLE()
382 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
383 // wxLogWindow menu events
384 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
386 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
388 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
390 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
393 wxLogFrame::wxLogFrame(wxFrame
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
)
394 : wxFrame(pParent
, -1, szTitle
)
398 m_pTextCtrl
= new wxTextCtrl(this, -1, wxEmptyString
, wxDefaultPosition
,
405 wxMenuBar
*pMenuBar
= new wxMenuBar
;
406 wxMenu
*pMenu
= new wxMenu
;
408 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
410 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
411 pMenu
->AppendSeparator();
412 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
413 pMenuBar
->Append(pMenu
, _("&Log"));
414 SetMenuBar(pMenuBar
);
417 // status bar for menu prompts
419 #endif // wxUSE_STATUSBAR
421 m_log
->OnFrameCreate(this);
424 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
429 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
435 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
439 const wxChar
*szFileName
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"));
440 if ( szFileName
== NULL
) {
449 if ( wxFile::Exists(szFileName
) ) {
450 bool bAppend
= FALSE
;
452 strMsg
.Printf(_("Append log to file '%s' "
453 "(choosing [No] will overwrite it)?"), szFileName
);
454 switch ( wxMessageBox(strMsg
, _("Question"), wxYES_NO
| wxCANCEL
) ) {
467 wxFAIL_MSG(_("invalid message box return value"));
471 bOk
= file
.Open(szFileName
, wxFile::write_append
);
474 bOk
= file
.Create(szFileName
, TRUE
/* overwrite */);
478 bOk
= file
.Create(szFileName
);
481 // retrieve text and save it
482 // -------------------------
483 int nLines
= m_pTextCtrl
->GetNumberOfLines();
484 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
485 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
486 // we're not going to pull in the whole wxTextFile if all we need is this...
489 #else // !wxUSE_TEXTFILE
491 #endif // wxUSE_TEXTFILE
499 wxLogError(_("Can't save log contents to file."));
502 wxLogStatus(this, _("Log saved to the file '%s'."), szFileName
);
507 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
509 m_pTextCtrl
->Clear();
512 wxLogFrame::~wxLogFrame()
514 m_log
->OnFrameDelete(this);
519 wxLogWindow::wxLogWindow(wxFrame
*pParent
,
520 const wxChar
*szTitle
,
524 m_bPassMessages
= bDoPass
;
526 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
527 m_pOldLog
= wxLog::SetActiveTarget(this);
530 m_pLogFrame
->Show(TRUE
);
533 void wxLogWindow::Show(bool bShow
)
535 m_pLogFrame
->Show(bShow
);
538 void wxLogWindow::Flush()
540 if ( m_pOldLog
!= NULL
)
543 m_bHasMessages
= FALSE
;
546 void wxLogWindow::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
548 // first let the previous logger show it
549 if ( m_pOldLog
!= NULL
&& m_bPassMessages
) {
550 // bogus cast just to access protected DoLog
551 ((wxLogWindow
*)m_pOldLog
)->DoLog(level
, szString
, t
);
557 // by default, these messages are ignored by wxLog, so process
559 if ( !wxIsEmpty(szString
) )
562 str
<< _("Status: ") << szString
;
567 // don't put trace messages in the text window for 2 reasons:
568 // 1) there are too many of them
569 // 2) they may provoke other trace messages thus sending a program
570 // into an infinite loop
575 // and this will format it nicely and call our DoLogString()
576 wxLog::DoLog(level
, szString
, t
);
580 m_bHasMessages
= TRUE
;
583 void wxLogWindow::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
585 // put the text into our window
586 wxTextCtrl
*pText
= m_pLogFrame
->TextCtrl();
588 // remove selection (WriteText is in fact ReplaceSelection)
590 long nLen
= pText
->GetLastPosition();
591 pText
->SetSelection(nLen
, nLen
);
596 msg
<< szString
<< wxT('\n');
598 pText
->AppendText(msg
);
600 // TODO ensure that the line can be seen
603 wxFrame
*wxLogWindow::GetFrame() const
608 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
612 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
614 m_pLogFrame
= (wxLogFrame
*)NULL
;
617 wxLogWindow::~wxLogWindow()
621 // may be NULL if log frame already auto destroyed itself
625 // ----------------------------------------------------------------------------
627 // ----------------------------------------------------------------------------
631 static const size_t MARGIN
= 10;
633 wxLogDialog::wxLogDialog(wxWindow
*parent
,
634 const wxArrayString
& messages
,
635 const wxArrayInt
& severity
,
636 const wxArrayLong
& times
,
637 const wxString
& caption
,
639 : wxDialog(parent
, -1, caption
),
640 m_messages(messages
), m_severity(severity
), m_times(times
)
642 m_showingDetails
= FALSE
; // not initially
643 m_listctrl
= (wxListCtrl
*)NULL
;
645 // create the controls which are always shown and layout them: we use
646 // sizers even though our window is not resizeable to calculate the size of
647 // the dialog properly
648 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
649 wxBoxSizer
*sizerButtons
= new wxBoxSizer(wxVERTICAL
);
650 wxBoxSizer
*sizerAll
= new wxBoxSizer(wxHORIZONTAL
);
652 wxButton
*btnOk
= new wxButton(this, wxID_OK
, _T("Ok"));
653 m_btnDetails
= new wxButton(this, wxID_MORE
, _T("&Details >>"));
654 sizerButtons
->Add(btnOk
, 0, wxCENTRE
|wxBOTTOM
, MARGIN
/2);
655 sizerButtons
->Add(m_btnDetails
, 0, wxCENTRE
|wxTOP
, MARGIN
/2 - 1);
657 wxIcon icon
= wxTheApp
->GetStdIcon(style
& wxICON_MASK
);
658 sizerAll
->Add(new wxStaticBitmap(this, -1, icon
), 0, wxCENTRE
);
659 const wxString
& message
= messages
.Last();
660 sizerAll
->Add(CreateTextSizer(message
), 0, wxCENTRE
|wxLEFT
|wxRIGHT
, MARGIN
);
661 sizerAll
->Add(sizerButtons
, 0, wxALIGN_RIGHT
|wxLEFT
, MARGIN
);
663 sizerTop
->Add(sizerAll
, 0, wxCENTRE
|wxALL
, MARGIN
);
668 sizerTop
->SetSizeHints(this);
676 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
681 void wxLogDialog::OnDetails(wxCommandEvent
& WXUNUSED(event
))
683 wxSizer
*sizer
= GetSizer();
685 if ( m_showingDetails
)
687 m_btnDetails
->SetLabel(_T("&Details >>"));
689 sizer
->Remove(m_listctrl
);
691 else // show details now
693 m_btnDetails
->SetLabel(_T("<< &Details"));
698 m_listctrl
= new wxListCtrl(this, -1,
699 wxDefaultPosition
, wxDefaultSize
,
700 wxLC_REPORT
| wxLC_NO_HEADER
);
701 m_listctrl
->InsertColumn(0, _("Message"));
702 m_listctrl
->InsertColumn(1, _("Time"));
704 // prepare the imagelist
705 static const int ICON_SIZE
= 16;
706 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
708 // order should be the same as in the switch below!
709 static const int icons
[] =
716 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
718 wxBitmap bmp
= wxTheApp
->GetStdIcon(icons
[icon
]);
719 imageList
->Add(wxImage(bmp
).
720 Rescale(ICON_SIZE
, ICON_SIZE
).
724 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
727 wxString fmt
= wxLog::GetTimestamp();
734 size_t count
= m_messages
.GetCount();
735 for ( size_t n
= 0; n
< count
; n
++ )
738 switch ( m_severity
[n
] )
752 m_listctrl
->InsertItem(n
, m_messages
[n
], image
);
753 m_listctrl
->SetItem(n
, 1,
754 wxDateTime((time_t)m_times
[n
]).Format(fmt
));
757 // let the columns size themselves (TODO does this work under GTK?)
758 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
759 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
761 // get the approx height of the listctrl
762 wxFont font
= GetFont();
764 font
= *wxSWISS_FONT
;
767 GetTextExtent(_T("H"), (int*)NULL
, &y
, (int*)NULL
, (int*)NULL
, &font
);
768 int height
= wxMin(y
*(count
+ 3), 100);
769 m_listctrl
->SetSize(-1, height
);
772 sizer
->Add(m_listctrl
, 1, wxEXPAND
|(wxALL
& ~wxTOP
), MARGIN
);
775 m_showingDetails
= !m_showingDetails
;
777 // in any case, our size changed - update
778 sizer
->SetSizeHints(this);
782 wxLogDialog::~wxLogDialog()
786 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
790 #endif // wxUSE_LOG_DIALOG