]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/logg.cpp
e158cec0305df043c1f113f9383fe0563fb8a332
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/textctrl.h"
43 #include "wx/statbmp.h"
47 #include "wx/textfile.h"
50 // for OutputDebugString()
51 #include "wx/msw/private.h"
54 // may be defined to 0 for old behavior (using wxMessageBox) - shouldn't be
55 // changed normally (that's why it's here and not in setup.h)
56 #define wxUSE_LOG_DIALOG 1
59 #include "wx/datetime.h"
60 #include "wx/listctrl.h"
62 #else // !wxUSE_TEXTFILE
63 #include "wx/msgdlg.h"
64 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
72 class wxLogDialog
: public wxDialog
75 wxLogDialog(wxWindow
*parent
,
76 const wxArrayString
& messages
,
77 const wxArrayInt
& severity
,
78 const wxArrayLong
& timess
,
79 const wxString
& caption
,
81 virtual ~wxLogDialog();
84 void OnOk(wxCommandEvent
& event
);
85 void OnDetails(wxCommandEvent
& event
);
88 // the data for the listctrl
89 const wxArrayString
& m_messages
;
90 const wxArrayInt
& m_severity
;
91 const wxArrayLong
& m_times
;
93 // the "toggle" button and its state
94 wxButton
*m_btnDetails
;
95 bool m_showingDetails
;
97 // the listctrl (not shown initially)
98 wxListCtrl
*m_listctrl
;
100 DECLARE_EVENT_TABLE()
103 BEGIN_EVENT_TABLE(wxLogDialog
, wxDialog
)
104 EVT_BUTTON(wxID_OK
, wxLogDialog::OnOk
)
105 EVT_BUTTON(wxID_MORE
, wxLogDialog::OnDetails
)
108 #endif // wxUSE_LOG_DIALOG
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 // we use a global variable to store the frame pointer for wxLogStatus - bad,
115 // but it's he easiest way
116 static wxFrame
*gs_pFrame
; // FIXME MT-unsafe
118 // ============================================================================
120 // ============================================================================
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 // accepts an additional argument which tells to which frame the output should
128 void wxLogStatus(wxFrame
*pFrame
, const wxChar
*szFormat
, ...)
132 wxLog
*pLog
= wxLog::GetActiveTarget();
133 if ( pLog
!= NULL
) {
135 va_start(argptr
, szFormat
);
136 msg
.PrintfV(szFormat
, argptr
);
139 wxASSERT( gs_pFrame
== NULL
); // should be reset!
141 wxLog::OnLog(wxLOG_Status
, msg
, time(NULL
));
142 gs_pFrame
= (wxFrame
*) NULL
;
146 // ----------------------------------------------------------------------------
147 // wxLogTextCtrl implementation
148 // ----------------------------------------------------------------------------
150 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
152 m_pTextCtrl
= pTextCtrl
;
155 void wxLogTextCtrl::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
159 msg
<< szString
<< wxT('\n');
161 m_pTextCtrl
->AppendText(msg
);
164 // ----------------------------------------------------------------------------
165 // wxLogGui implementation (FIXME MT-unsafe)
166 // ----------------------------------------------------------------------------
173 void wxLogGui::Clear()
175 m_bErrors
= m_bWarnings
= FALSE
;
181 void wxLogGui::Flush()
183 if ( !m_bHasMessages
)
186 // do it right now to block any new calls to Flush() while we're here
187 m_bHasMessages
= FALSE
;
189 wxString title
= wxTheApp
->GetAppName();
192 title
[0u] = wxToupper(title
[0u]);
201 else if ( m_bWarnings
) {
202 title
+= _("Warning");
203 style
= wxICON_EXCLAMATION
;
206 title
+= _("Information");
207 style
= wxICON_INFORMATION
;
211 wxLogDialog
dlg(wxTheApp
->GetTopWindow(),
212 m_aMessages
, m_aSeverity
, m_aTimes
,
214 (void)dlg
.ShowModal();
216 #else // !wxUSE_LOG_DIALOG
217 // concatenate all strings (but not too many to not overfill the msg box)
220 nMsgCount
= m_aMessages
.Count();
222 // start from the most recent message
223 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
224 // for Windows strings longer than this value are wrapped (NT 4.0)
225 const size_t nMsgLineWidth
= 156;
227 nLines
+= (m_aMessages
[n
- 1].Len() + nMsgLineWidth
- 1) / nMsgLineWidth
;
229 if ( nLines
> 25 ) // don't put too many lines in message box
232 str
<< m_aMessages
[n
- 1] << wxT("\n");
235 wxMessageBox(str
, title
, wxOK
| style
);
236 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
238 // no undisplayed messages whatsoever
242 m_bHasMessages
= FALSE
;
245 // the default behaviour is to discard all informational messages if there
246 // are any errors/warnings.
247 void wxLogGui::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
255 m_aMessages
.Add(szString
);
256 m_aSeverity
.Add(wxLOG_Message
);
257 m_aTimes
.Add((long)t
);
258 m_bHasMessages
= TRUE
;
266 // find the top window and set it's status text if it has any
267 wxFrame
*pFrame
= gs_pFrame
;
268 if ( pFrame
== NULL
) {
269 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
270 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
271 pFrame
= (wxFrame
*)pWin
;
275 if ( pFrame
&& pFrame
->GetStatusBar() )
276 pFrame
->SetStatusText(szString
);
278 #endif // wxUSE_STATUSBAR
286 // don't prepend debug/trace here: it goes to the
287 // debug window anyhow, but do put a timestamp
290 str
<< szString
<< wxT("\n\r");
291 OutputDebugString(str
);
293 // send them to stderr
294 wxFprintf(stderr
, wxT("%s: %s\n"),
295 level
== wxLOG_Trace
? wxT("Trace")
301 #endif // __WXDEBUG__
305 case wxLOG_FatalError
:
306 // show this one immediately
307 wxMessageBox(szString
, _("Fatal error"), wxICON_HAND
);
312 #if !wxUSE_LOG_DIALOG
313 // discard earlier informational messages if this is the 1st
314 // error because they might not make sense any more and showing
315 // them in a message box might be confusing
319 #endif // wxUSE_LOG_DIALOG
326 // for the warning we don't discard the info messages
330 m_aMessages
.Add(szString
);
331 m_aSeverity
.Add(level
);
332 m_aTimes
.Add((long)t
);
333 m_bHasMessages
= TRUE
;
338 // ----------------------------------------------------------------------------
339 // wxLogWindow and wxLogFrame implementation
340 // ----------------------------------------------------------------------------
344 class wxLogFrame
: public wxFrame
348 wxLogFrame(wxFrame
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
);
349 virtual ~wxLogFrame();
352 void OnClose(wxCommandEvent
& event
);
353 void OnCloseWindow(wxCloseEvent
& event
);
355 void OnSave (wxCommandEvent
& event
);
357 void OnClear(wxCommandEvent
& event
);
359 void OnIdle(wxIdleEvent
&);
362 wxTextCtrl
*TextCtrl() const { return m_pTextCtrl
; }
372 // instead of closing just hide the window to be able to Show() it later
373 void DoClose() { Show(FALSE
); }
375 wxTextCtrl
*m_pTextCtrl
;
378 DECLARE_EVENT_TABLE()
381 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
382 // wxLogWindow menu events
383 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
385 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
387 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
389 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
392 wxLogFrame::wxLogFrame(wxFrame
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
)
393 : wxFrame(pParent
, -1, szTitle
)
397 m_pTextCtrl
= new wxTextCtrl(this, -1, wxEmptyString
, wxDefaultPosition
,
404 wxMenuBar
*pMenuBar
= new wxMenuBar
;
405 wxMenu
*pMenu
= new wxMenu
;
407 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
409 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
410 pMenu
->AppendSeparator();
411 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
412 pMenuBar
->Append(pMenu
, _("&Log"));
413 SetMenuBar(pMenuBar
);
416 // status bar for menu prompts
418 #endif // wxUSE_STATUSBAR
420 m_log
->OnFrameCreate(this);
423 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
428 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
434 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
438 const wxChar
*szFileName
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"));
439 if ( szFileName
== NULL
) {
448 if ( wxFile::Exists(szFileName
) ) {
449 bool bAppend
= FALSE
;
451 strMsg
.Printf(_("Append log to file '%s' "
452 "(choosing [No] will overwrite it)?"), szFileName
);
453 switch ( wxMessageBox(strMsg
, _("Question"), wxYES_NO
| wxCANCEL
) ) {
466 wxFAIL_MSG(_("invalid message box return value"));
470 bOk
= file
.Open(szFileName
, wxFile::write_append
);
473 bOk
= file
.Create(szFileName
, TRUE
/* overwrite */);
477 bOk
= file
.Create(szFileName
);
480 // retrieve text and save it
481 // -------------------------
482 int nLines
= m_pTextCtrl
->GetNumberOfLines();
483 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
484 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
485 // we're not going to pull in the whole wxTextFile if all we need is this...
488 #else // !wxUSE_TEXTFILE
490 #endif // wxUSE_TEXTFILE
498 wxLogError(_("Can't save log contents to file."));
501 wxLogStatus(this, _("Log saved to the file '%s'."), szFileName
);
506 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
508 m_pTextCtrl
->Clear();
511 wxLogFrame::~wxLogFrame()
513 m_log
->OnFrameDelete(this);
518 wxLogWindow::wxLogWindow(wxFrame
*pParent
,
519 const wxChar
*szTitle
,
523 m_bPassMessages
= bDoPass
;
525 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
526 m_pOldLog
= wxLog::SetActiveTarget(this);
529 m_pLogFrame
->Show(TRUE
);
532 void wxLogWindow::Show(bool bShow
)
534 m_pLogFrame
->Show(bShow
);
537 void wxLogWindow::Flush()
539 if ( m_pOldLog
!= NULL
)
542 m_bHasMessages
= FALSE
;
545 void wxLogWindow::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
547 // first let the previous logger show it
548 if ( m_pOldLog
!= NULL
&& m_bPassMessages
) {
549 // bogus cast just to access protected DoLog
550 ((wxLogWindow
*)m_pOldLog
)->DoLog(level
, szString
, t
);
556 // by default, these messages are ignored by wxLog, so process
558 if ( !wxIsEmpty(szString
) )
561 str
<< _("Status: ") << szString
;
566 // don't put trace messages in the text window for 2 reasons:
567 // 1) there are too many of them
568 // 2) they may provoke other trace messages thus sending a program
569 // into an infinite loop
574 // and this will format it nicely and call our DoLogString()
575 wxLog::DoLog(level
, szString
, t
);
579 m_bHasMessages
= TRUE
;
582 void wxLogWindow::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
584 // put the text into our window
585 wxTextCtrl
*pText
= m_pLogFrame
->TextCtrl();
587 // remove selection (WriteText is in fact ReplaceSelection)
589 long nLen
= pText
->GetLastPosition();
590 pText
->SetSelection(nLen
, nLen
);
595 msg
<< szString
<< wxT('\n');
597 pText
->AppendText(msg
);
599 // TODO ensure that the line can be seen
602 wxFrame
*wxLogWindow::GetFrame() const
607 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
611 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
613 m_pLogFrame
= (wxLogFrame
*)NULL
;
616 wxLogWindow::~wxLogWindow()
620 // may be NULL if log frame already auto destroyed itself
624 // ----------------------------------------------------------------------------
626 // ----------------------------------------------------------------------------
630 static const size_t MARGIN
= 10;
632 wxLogDialog::wxLogDialog(wxWindow
*parent
,
633 const wxArrayString
& messages
,
634 const wxArrayInt
& severity
,
635 const wxArrayLong
& times
,
636 const wxString
& caption
,
638 : wxDialog(parent
, -1, caption
),
639 m_messages(messages
), m_severity(severity
), m_times(times
)
641 m_showingDetails
= FALSE
; // not initially
642 m_listctrl
= (wxListCtrl
*)NULL
;
644 // create the controls which are always shown and layout them: we use
645 // sizers even though our window is not resizeable to calculate the size of
646 // the dialog properly
647 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
648 wxBoxSizer
*sizerButtons
= new wxBoxSizer(wxVERTICAL
);
649 wxBoxSizer
*sizerAll
= new wxBoxSizer(wxHORIZONTAL
);
651 wxButton
*btnOk
= new wxButton(this, wxID_OK
, _T("Ok"));
652 m_btnDetails
= new wxButton(this, wxID_MORE
, _T("&Details >>"));
653 sizerButtons
->Add(btnOk
, 0, wxCENTRE
|wxBOTTOM
, MARGIN
/2);
654 sizerButtons
->Add(m_btnDetails
, 0, wxCENTRE
|wxTOP
, MARGIN
/2 - 1);
656 wxIcon icon
= wxTheApp
->GetStdIcon(style
& wxICON_MASK
);
657 sizerAll
->Add(new wxStaticBitmap(this, -1, icon
), 0, wxCENTRE
);
658 const wxString
& message
= messages
.Last();
659 sizerAll
->Add(CreateTextSizer(message
), 0, wxCENTRE
|wxLEFT
|wxRIGHT
, MARGIN
);
660 sizerAll
->Add(sizerButtons
, 0, wxALIGN_RIGHT
|wxLEFT
, MARGIN
);
662 sizerTop
->Add(sizerAll
, 0, wxCENTRE
|wxALL
, MARGIN
);
667 sizerTop
->SetSizeHints(this);
675 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
680 void wxLogDialog::OnDetails(wxCommandEvent
& WXUNUSED(event
))
682 wxSizer
*sizer
= GetSizer();
684 if ( m_showingDetails
)
686 m_btnDetails
->SetLabel(_T("&Details >>"));
688 sizer
->Remove(m_listctrl
);
690 else // show details now
692 m_btnDetails
->SetLabel(_T("<< &Details"));
697 m_listctrl
= new wxListCtrl(this, -1,
698 wxDefaultPosition
, wxDefaultSize
,
699 wxLC_REPORT
| wxLC_NO_HEADER
);
700 m_listctrl
->InsertColumn(0, _("Message"));
701 m_listctrl
->InsertColumn(1, _("Time"));
703 // prepare the imagelist
704 static const int ICON_SIZE
= 16;
705 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
707 // order should be the same as in the switch below!
708 static const int icons
[] =
715 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
717 wxBitmap bmp
= wxTheApp
->GetStdIcon(icons
[icon
]);
718 imageList
->Add(wxImage(bmp
).
719 Rescale(ICON_SIZE
, ICON_SIZE
).
723 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
726 wxString fmt
= wxLog::GetTimestamp();
733 size_t count
= m_messages
.GetCount();
734 for ( size_t n
= 0; n
< count
; n
++ )
737 switch ( m_severity
[n
] )
751 m_listctrl
->InsertItem(n
, m_messages
[n
], image
);
752 m_listctrl
->SetItem(n
, 1,
753 wxDateTime((time_t)m_times
[n
]).Format(fmt
));
756 // let the columns size themselves (TODO does this work under GTK?)
757 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
758 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
760 // get the approx height of the listctrl
761 wxFont font
= GetFont();
763 font
= *wxSWISS_FONT
;
766 GetTextExtent(_T("H"), (int*)NULL
, &y
, (int*)NULL
, (int*)NULL
, &font
);
767 int height
= wxMin(y
*(count
+ 3), 100);
768 m_listctrl
->SetSize(-1, height
);
771 sizer
->Add(m_listctrl
, 1, wxEXPAND
|(wxALL
& ~wxTOP
), MARGIN
);
774 m_showingDetails
= !m_showingDetails
;
776 // in any case, our size changed - update
780 wxLogDialog::~wxLogDialog()
784 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
788 #endif // wxUSE_LOG_DIALOG