]>
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"
62 #include "wx/imaglist.h"
64 #else // !wxUSE_TEXTFILE
65 #include "wx/msgdlg.h"
66 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
74 class wxLogDialog
: public wxDialog
77 wxLogDialog(wxWindow
*parent
,
78 const wxArrayString
& messages
,
79 const wxArrayInt
& severity
,
80 const wxArrayLong
& timess
,
81 const wxString
& caption
,
83 virtual ~wxLogDialog();
86 void OnOk(wxCommandEvent
& event
);
87 void OnDetails(wxCommandEvent
& event
);
88 void OnListSelect(wxListEvent
& event
);
91 // the data for the listctrl
92 wxArrayString m_messages
;
93 wxArrayInt m_severity
;
96 // the "toggle" button and its state
97 wxButton
*m_btnDetails
;
98 bool m_showingDetails
;
100 // the listctrl (not shown initially)
101 wxListCtrl
*m_listctrl
;
103 // the translated "Details" string
104 static wxString ms_details
;
106 DECLARE_EVENT_TABLE()
109 BEGIN_EVENT_TABLE(wxLogDialog
, wxDialog
)
110 EVT_BUTTON(wxID_OK
, wxLogDialog::OnOk
)
111 EVT_BUTTON(wxID_MORE
, wxLogDialog::OnDetails
)
112 EVT_LIST_ITEM_SELECTED(-1, wxLogDialog::OnListSelect
)
115 #endif // wxUSE_LOG_DIALOG
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 // we use a global variable to store the frame pointer for wxLogStatus - bad,
122 // but it's he easiest way
123 static wxFrame
*gs_pFrame
; // FIXME MT-unsafe
125 // ============================================================================
127 // ============================================================================
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 // accepts an additional argument which tells to which frame the output should
135 void wxLogStatus(wxFrame
*pFrame
, const wxChar
*szFormat
, ...)
139 wxLog
*pLog
= wxLog::GetActiveTarget();
140 if ( pLog
!= NULL
) {
142 va_start(argptr
, szFormat
);
143 msg
.PrintfV(szFormat
, argptr
);
146 wxASSERT( gs_pFrame
== NULL
); // should be reset!
148 wxLog::OnLog(wxLOG_Status
, msg
, time(NULL
));
149 gs_pFrame
= (wxFrame
*) NULL
;
153 // ----------------------------------------------------------------------------
154 // wxLogTextCtrl implementation
155 // ----------------------------------------------------------------------------
157 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
159 m_pTextCtrl
= pTextCtrl
;
162 void wxLogTextCtrl::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
166 msg
<< szString
<< wxT('\n');
168 m_pTextCtrl
->AppendText(msg
);
171 // ----------------------------------------------------------------------------
172 // wxLogGui implementation (FIXME MT-unsafe)
173 // ----------------------------------------------------------------------------
177 // we must translate them here in the very beginning or we risk to have
178 // reentrancy problems when called from inside wxGetTranslation() leading
179 // to inifnite recursion
180 m_error
= _("Error");
181 m_warning
= _("Warning");
182 m_info
= _("Information");
187 void wxLogGui::Clear()
191 m_bHasMessages
= FALSE
;
198 void wxLogGui::Flush()
200 if ( !m_bHasMessages
)
203 // do it right now to block any new calls to Flush() while we're here
204 m_bHasMessages
= FALSE
;
206 wxString title
= wxTheApp
->GetAppName();
209 title
[0u] = wxToupper(title
[0u]);
218 else if ( m_bWarnings
) {
220 style
= wxICON_EXCLAMATION
;
224 style
= wxICON_INFORMATION
;
227 // this is the best we can do here
228 wxWindow
*parent
= wxTheApp
->GetTopWindow();
230 size_t nMsgCount
= m_aMessages
.Count();
233 if ( nMsgCount
== 1 )
235 str
= m_aMessages
[0];
237 else // more than one message
240 wxLogDialog
dlg(parent
,
241 m_aMessages
, m_aSeverity
, m_aTimes
,
244 // clear the message list before showing the dialog because while it's
245 // shown some new messages may appear
248 (void)dlg
.ShowModal();
249 #else // !wxUSE_LOG_DIALOG
250 // concatenate all strings (but not too many to not overfill the msg box)
253 // start from the most recent message
254 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
255 // for Windows strings longer than this value are wrapped (NT 4.0)
256 const size_t nMsgLineWidth
= 156;
258 nLines
+= (m_aMessages
[n
- 1].Len() + nMsgLineWidth
- 1) / nMsgLineWidth
;
260 if ( nLines
> 25 ) // don't put too many lines in message box
263 str
<< m_aMessages
[n
- 1] << wxT("\n");
265 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
268 // this catches both cases of 1 message with wxUSE_LOG_DIALOG and any
269 // situation without it
272 wxMessageBox(str
, title
, wxOK
| style
, parent
);
274 // no undisplayed messages whatsoever
279 // log all kinds of messages
280 void wxLogGui::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
288 m_aMessages
.Add(szString
);
289 m_aSeverity
.Add(wxLOG_Message
);
290 m_aTimes
.Add((long)t
);
291 m_bHasMessages
= TRUE
;
299 // find the top window and set it's status text if it has any
300 wxFrame
*pFrame
= gs_pFrame
;
301 if ( pFrame
== NULL
) {
302 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
303 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
304 pFrame
= (wxFrame
*)pWin
;
308 if ( pFrame
&& pFrame
->GetStatusBar() )
309 pFrame
->SetStatusText(szString
);
311 #endif // wxUSE_STATUSBAR
319 // don't prepend debug/trace here: it goes to the
320 // debug window anyhow, but do put a timestamp
323 str
<< szString
<< wxT("\n\r");
324 OutputDebugString(str
);
326 // send them to stderr
327 wxFprintf(stderr
, wxT("%s: %s\n"),
328 level
== wxLOG_Trace
? wxT("Trace")
334 #endif // __WXDEBUG__
338 case wxLOG_FatalError
:
339 // show this one immediately
340 wxMessageBox(szString
, _("Fatal error"), wxICON_HAND
);
346 #if !wxUSE_LOG_DIALOG
347 // discard earlier informational messages if this is the 1st
348 // error because they might not make sense any more and showing
349 // them in a message box might be confusing
353 #endif // wxUSE_LOG_DIALOG
360 // for the warning we don't discard the info messages
364 m_aMessages
.Add(szString
);
365 m_aSeverity
.Add((int)level
);
366 m_aTimes
.Add((long)t
);
367 m_bHasMessages
= TRUE
;
372 // ----------------------------------------------------------------------------
373 // wxLogWindow and wxLogFrame implementation
374 // ----------------------------------------------------------------------------
378 class wxLogFrame
: public wxFrame
382 wxLogFrame(wxFrame
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
);
383 virtual ~wxLogFrame();
386 void OnClose(wxCommandEvent
& event
);
387 void OnCloseWindow(wxCloseEvent
& event
);
389 void OnSave (wxCommandEvent
& event
);
391 void OnClear(wxCommandEvent
& event
);
393 void OnIdle(wxIdleEvent
&);
396 wxTextCtrl
*TextCtrl() const { return m_pTextCtrl
; }
399 // use standard ids for our commands!
402 Menu_Close
= wxID_CLOSE
,
403 Menu_Save
= wxID_SAVE
,
404 Menu_Clear
= wxID_CLEAR
407 // instead of closing just hide the window to be able to Show() it later
408 void DoClose() { Show(FALSE
); }
410 wxTextCtrl
*m_pTextCtrl
;
413 DECLARE_EVENT_TABLE()
416 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
417 // wxLogWindow menu events
418 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
420 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
422 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
424 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
427 wxLogFrame::wxLogFrame(wxFrame
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
)
428 : wxFrame(pParent
, -1, szTitle
)
432 m_pTextCtrl
= new wxTextCtrl(this, -1, wxEmptyString
, wxDefaultPosition
,
439 wxMenuBar
*pMenuBar
= new wxMenuBar
;
440 wxMenu
*pMenu
= new wxMenu
;
442 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
444 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
445 pMenu
->AppendSeparator();
446 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
447 pMenuBar
->Append(pMenu
, _("&Log"));
448 SetMenuBar(pMenuBar
);
451 // status bar for menu prompts
453 #endif // wxUSE_STATUSBAR
455 m_log
->OnFrameCreate(this);
458 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
463 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
469 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
473 const wxChar
*szFileName
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"));
474 if ( szFileName
== NULL
) {
483 if ( wxFile::Exists(szFileName
) ) {
484 bool bAppend
= FALSE
;
486 strMsg
.Printf(_("Append log to file '%s' "
487 "(choosing [No] will overwrite it)?"), szFileName
);
488 switch ( wxMessageBox(strMsg
, _("Question"), wxYES_NO
| wxCANCEL
) ) {
501 wxFAIL_MSG(_("invalid message box return value"));
505 bOk
= file
.Open(szFileName
, wxFile::write_append
);
508 bOk
= file
.Create(szFileName
, TRUE
/* overwrite */);
512 bOk
= file
.Create(szFileName
);
515 // retrieve text and save it
516 // -------------------------
517 int nLines
= m_pTextCtrl
->GetNumberOfLines();
518 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
519 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
520 // we're not going to pull in the whole wxTextFile if all we need is this...
523 #else // !wxUSE_TEXTFILE
525 #endif // wxUSE_TEXTFILE
533 wxLogError(_("Can't save log contents to file."));
536 wxLogStatus(this, _("Log saved to the file '%s'."), szFileName
);
541 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
543 m_pTextCtrl
->Clear();
546 wxLogFrame::~wxLogFrame()
548 m_log
->OnFrameDelete(this);
553 wxLogWindow::wxLogWindow(wxFrame
*pParent
,
554 const wxChar
*szTitle
,
558 m_bPassMessages
= bDoPass
;
560 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
561 m_pOldLog
= wxLog::SetActiveTarget(this);
564 m_pLogFrame
->Show(TRUE
);
567 void wxLogWindow::Show(bool bShow
)
569 m_pLogFrame
->Show(bShow
);
572 void wxLogWindow::Flush()
574 if ( m_pOldLog
!= NULL
)
577 m_bHasMessages
= FALSE
;
580 void wxLogWindow::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
582 // first let the previous logger show it
583 if ( m_pOldLog
!= NULL
&& m_bPassMessages
) {
584 // bogus cast just to access protected DoLog
585 ((wxLogWindow
*)m_pOldLog
)->DoLog(level
, szString
, t
);
591 // by default, these messages are ignored by wxLog, so process
593 if ( !wxIsEmpty(szString
) )
596 str
<< _("Status: ") << szString
;
601 // don't put trace messages in the text window for 2 reasons:
602 // 1) there are too many of them
603 // 2) they may provoke other trace messages thus sending a program
604 // into an infinite loop
609 // and this will format it nicely and call our DoLogString()
610 wxLog::DoLog(level
, szString
, t
);
614 m_bHasMessages
= TRUE
;
617 void wxLogWindow::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
619 // put the text into our window
620 wxTextCtrl
*pText
= m_pLogFrame
->TextCtrl();
622 // remove selection (WriteText is in fact ReplaceSelection)
624 long nLen
= pText
->GetLastPosition();
625 pText
->SetSelection(nLen
, nLen
);
630 msg
<< szString
<< wxT('\n');
632 pText
->AppendText(msg
);
634 // TODO ensure that the line can be seen
637 wxFrame
*wxLogWindow::GetFrame() const
642 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
646 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
648 m_pLogFrame
= (wxLogFrame
*)NULL
;
651 wxLogWindow::~wxLogWindow()
655 // may be NULL if log frame already auto destroyed itself
659 // ----------------------------------------------------------------------------
661 // ----------------------------------------------------------------------------
665 static const size_t MARGIN
= 10;
667 wxString
wxLogDialog::ms_details
;
669 wxLogDialog::wxLogDialog(wxWindow
*parent
,
670 const wxArrayString
& messages
,
671 const wxArrayInt
& severity
,
672 const wxArrayLong
& times
,
673 const wxString
& caption
,
675 : wxDialog(parent
, -1, caption
)
677 if ( ms_details
.IsEmpty() )
679 // ensure that we won't try to call wxGetTranslation() twice
680 ms_details
= _T("&Details");
681 ms_details
= wxGetTranslation(ms_details
);
684 size_t count
= messages
.GetCount();
685 m_messages
.Alloc(count
);
686 m_severity
.Alloc(count
);
687 m_times
.Alloc(count
);
689 for ( size_t n
= 0; n
< count
; n
++ )
691 wxString msg
= messages
[n
];
694 m_messages
.Add(msg
.BeforeFirst(_T('\n')));
695 msg
= msg
.AfterFirst(_T('\n'));
697 m_severity
.Add(severity
[n
]);
698 m_times
.Add(times
[n
]);
703 m_showingDetails
= FALSE
; // not initially
704 m_listctrl
= (wxListCtrl
*)NULL
;
706 // create the controls which are always shown and layout them: we use
707 // sizers even though our window is not resizeable to calculate the size of
708 // the dialog properly
709 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
710 wxBoxSizer
*sizerButtons
= new wxBoxSizer(wxVERTICAL
);
711 wxBoxSizer
*sizerAll
= new wxBoxSizer(wxHORIZONTAL
);
713 wxButton
*btnOk
= new wxButton(this, wxID_OK
, _("OK"));
714 sizerButtons
->Add(btnOk
, 0, wxCENTRE
|wxBOTTOM
, MARGIN
/2);
715 m_btnDetails
= new wxButton(this, wxID_MORE
, ms_details
+ _T(" >>"));
716 sizerButtons
->Add(m_btnDetails
, 0, wxCENTRE
|wxTOP
, MARGIN
/2 - 1);
719 wxIcon icon
= wxTheApp
->GetStdIcon((int)(style
& wxICON_MASK
));
720 sizerAll
->Add(new wxStaticBitmap(this, -1, icon
), 0, wxCENTRE
);
723 const wxString
& message
= messages
.Last();
724 sizerAll
->Add(CreateTextSizer(message
), 0, wxCENTRE
|wxLEFT
|wxRIGHT
, MARGIN
);
725 sizerAll
->Add(sizerButtons
, 0, wxALIGN_RIGHT
|wxLEFT
, MARGIN
);
727 sizerTop
->Add(sizerAll
, 0, wxCENTRE
|wxALL
, MARGIN
);
732 sizerTop
->SetSizeHints(this);
737 // this can't happen any more as we don't use this dialog in this case
741 // no details... it's easier to disable a button than to change the
742 // dialog layout depending on whether we have details or not
743 m_btnDetails
->Disable();
750 void wxLogDialog::OnListSelect(wxListEvent
& event
)
752 // we can't just disable the control because this looks ugly under Windows
753 // (wrong bg colour, no scrolling...), but we still want to disable
754 // selecting items - it makes no sense here
755 m_listctrl
->SetItemState(event
.GetIndex(), 0, wxLIST_STATE_SELECTED
);
758 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
763 void wxLogDialog::OnDetails(wxCommandEvent
& WXUNUSED(event
))
765 wxSizer
*sizer
= GetSizer();
767 if ( m_showingDetails
)
769 m_btnDetails
->SetLabel(ms_details
+ _T(">>"));
771 sizer
->Remove(m_listctrl
);
773 else // show details now
775 m_btnDetails
->SetLabel(wxString(_T("<< ")) + ms_details
);
780 m_listctrl
= new wxListCtrl(this, -1,
781 wxDefaultPosition
, wxDefaultSize
,
786 // no need to translate these strings as they're not shown to the
787 // user anyhow (we use wxLC_NO_HEADER style)
788 m_listctrl
->InsertColumn(0, _T("Message"));
789 m_listctrl
->InsertColumn(1, _T("Time"));
791 // prepare the imagelist
792 static const int ICON_SIZE
= 16;
793 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
795 // order should be the same as in the switch below!
796 static const int icons
[] =
804 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
806 wxBitmap bmp
= wxTheApp
->GetStdIcon(icons
[icon
]);
807 imageList
->Add(wxImage(bmp
).
808 Rescale(ICON_SIZE
, ICON_SIZE
).
812 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
816 wxString fmt
= wxLog::GetTimestamp();
823 size_t count
= m_messages
.GetCount();
824 for ( size_t n
= 0; n
< count
; n
++ )
828 switch ( m_severity
[n
] )
843 m_listctrl
->InsertItem(n
, m_messages
[n
], image
);
844 m_listctrl
->SetItem(n
, 1,
845 wxDateTime((time_t)m_times
[n
]).Format(fmt
));
848 // let the columns size themselves
849 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
850 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
852 // get the approx height of the listctrl
853 wxFont font
= GetFont();
855 font
= *wxSWISS_FONT
;
858 GetTextExtent(_T("H"), (int*)NULL
, &y
, (int*)NULL
, (int*)NULL
, &font
);
859 int height
= wxMin(y
*(count
+ 3), 100);
860 m_listctrl
->SetSize(-1, height
);
863 sizer
->Add(m_listctrl
, 1, wxEXPAND
|(wxALL
& ~wxTOP
), MARGIN
);
866 m_showingDetails
= !m_showingDetails
;
868 // in any case, our size changed - update
869 sizer
->SetSizeHints(this);
873 wxLogDialog::~wxLogDialog()
877 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
881 #endif // wxUSE_LOG_DIALOG