]>
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
);
87 void OnListSelect(wxListEvent
& event
);
90 // the data for the listctrl
91 wxArrayString m_messages
;
92 wxArrayInt m_severity
;
95 // the "toggle" button and its state
96 wxButton
*m_btnDetails
;
97 bool m_showingDetails
;
99 // the listctrl (not shown initially)
100 wxListCtrl
*m_listctrl
;
102 DECLARE_EVENT_TABLE()
105 BEGIN_EVENT_TABLE(wxLogDialog
, wxDialog
)
106 EVT_BUTTON(wxID_OK
, wxLogDialog::OnOk
)
107 EVT_BUTTON(wxID_MORE
, wxLogDialog::OnDetails
)
108 EVT_LIST_ITEM_SELECTED(-1, wxLogDialog::OnListSelect
)
111 #endif // wxUSE_LOG_DIALOG
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 // we use a global variable to store the frame pointer for wxLogStatus - bad,
118 // but it's he easiest way
119 static wxFrame
*gs_pFrame
; // FIXME MT-unsafe
121 // ============================================================================
123 // ============================================================================
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 // accepts an additional argument which tells to which frame the output should
131 void wxLogStatus(wxFrame
*pFrame
, const wxChar
*szFormat
, ...)
135 wxLog
*pLog
= wxLog::GetActiveTarget();
136 if ( pLog
!= NULL
) {
138 va_start(argptr
, szFormat
);
139 msg
.PrintfV(szFormat
, argptr
);
142 wxASSERT( gs_pFrame
== NULL
); // should be reset!
144 wxLog::OnLog(wxLOG_Status
, msg
, time(NULL
));
145 gs_pFrame
= (wxFrame
*) NULL
;
149 // ----------------------------------------------------------------------------
150 // wxLogTextCtrl implementation
151 // ----------------------------------------------------------------------------
153 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
155 m_pTextCtrl
= pTextCtrl
;
158 void wxLogTextCtrl::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
162 msg
<< szString
<< wxT('\n');
164 m_pTextCtrl
->AppendText(msg
);
167 // ----------------------------------------------------------------------------
168 // wxLogGui implementation (FIXME MT-unsafe)
169 // ----------------------------------------------------------------------------
176 void wxLogGui::Clear()
178 m_bErrors
= m_bWarnings
= FALSE
;
184 void wxLogGui::Flush()
186 if ( !m_bHasMessages
)
189 // do it right now to block any new calls to Flush() while we're here
190 m_bHasMessages
= FALSE
;
192 wxString title
= wxTheApp
->GetAppName();
195 title
[0u] = wxToupper(title
[0u]);
204 else if ( m_bWarnings
) {
205 title
+= _("Warning");
206 style
= wxICON_EXCLAMATION
;
209 title
+= _("Information");
210 style
= wxICON_INFORMATION
;
213 // this is the best we can do here
214 wxWindow
*parent
= wxTheApp
->GetTopWindow();
216 size_t nMsgCount
= m_aMessages
.Count();
219 if ( nMsgCount
== 1 )
221 str
= m_aMessages
[0];
223 else // more than one message
226 wxLogDialog
dlg(parent
,
227 m_aMessages
, m_aSeverity
, m_aTimes
,
230 // clear the message list before showing the dialog because while it's
231 // shown some new messages may appear
234 (void)dlg
.ShowModal();
235 #else // !wxUSE_LOG_DIALOG
236 // concatenate all strings (but not too many to not overfill the msg box)
239 // start from the most recent message
240 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
241 // for Windows strings longer than this value are wrapped (NT 4.0)
242 const size_t nMsgLineWidth
= 156;
244 nLines
+= (m_aMessages
[n
- 1].Len() + nMsgLineWidth
- 1) / nMsgLineWidth
;
246 if ( nLines
> 25 ) // don't put too many lines in message box
249 str
<< m_aMessages
[n
- 1] << wxT("\n");
251 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
254 // this catches both cases of 1 message with wxUSE_LOG_DIALOG and any
255 // situation without it
258 wxMessageBox(str
, title
, wxOK
| style
, parent
);
260 // no undisplayed messages whatsoever
265 m_bHasMessages
= FALSE
;
268 // log all kinds of messages
269 void wxLogGui::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
277 m_aMessages
.Add(szString
);
278 m_aSeverity
.Add(wxLOG_Message
);
279 m_aTimes
.Add((long)t
);
280 m_bHasMessages
= TRUE
;
288 // find the top window and set it's status text if it has any
289 wxFrame
*pFrame
= gs_pFrame
;
290 if ( pFrame
== NULL
) {
291 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
292 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
293 pFrame
= (wxFrame
*)pWin
;
297 if ( pFrame
&& pFrame
->GetStatusBar() )
298 pFrame
->SetStatusText(szString
);
300 #endif // wxUSE_STATUSBAR
308 // don't prepend debug/trace here: it goes to the
309 // debug window anyhow, but do put a timestamp
312 str
<< szString
<< wxT("\n\r");
313 OutputDebugString(str
);
315 // send them to stderr
316 wxFprintf(stderr
, wxT("%s: %s\n"),
317 level
== wxLOG_Trace
? wxT("Trace")
323 #endif // __WXDEBUG__
327 case wxLOG_FatalError
:
328 // show this one immediately
329 wxMessageBox(szString
, _("Fatal error"), wxICON_HAND
);
335 #if !wxUSE_LOG_DIALOG
336 // discard earlier informational messages if this is the 1st
337 // error because they might not make sense any more and showing
338 // them in a message box might be confusing
342 #endif // wxUSE_LOG_DIALOG
349 // for the warning we don't discard the info messages
353 m_aMessages
.Add(szString
);
354 m_aSeverity
.Add((int)level
);
355 m_aTimes
.Add((long)t
);
356 m_bHasMessages
= TRUE
;
361 // ----------------------------------------------------------------------------
362 // wxLogWindow and wxLogFrame implementation
363 // ----------------------------------------------------------------------------
367 class wxLogFrame
: public wxFrame
371 wxLogFrame(wxFrame
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
);
372 virtual ~wxLogFrame();
375 void OnClose(wxCommandEvent
& event
);
376 void OnCloseWindow(wxCloseEvent
& event
);
378 void OnSave (wxCommandEvent
& event
);
380 void OnClear(wxCommandEvent
& event
);
382 void OnIdle(wxIdleEvent
&);
385 wxTextCtrl
*TextCtrl() const { return m_pTextCtrl
; }
395 // instead of closing just hide the window to be able to Show() it later
396 void DoClose() { Show(FALSE
); }
398 wxTextCtrl
*m_pTextCtrl
;
401 DECLARE_EVENT_TABLE()
404 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
405 // wxLogWindow menu events
406 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
408 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
410 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
412 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
415 wxLogFrame::wxLogFrame(wxFrame
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
)
416 : wxFrame(pParent
, -1, szTitle
)
420 m_pTextCtrl
= new wxTextCtrl(this, -1, wxEmptyString
, wxDefaultPosition
,
427 wxMenuBar
*pMenuBar
= new wxMenuBar
;
428 wxMenu
*pMenu
= new wxMenu
;
430 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
432 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
433 pMenu
->AppendSeparator();
434 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
435 pMenuBar
->Append(pMenu
, _("&Log"));
436 SetMenuBar(pMenuBar
);
439 // status bar for menu prompts
441 #endif // wxUSE_STATUSBAR
443 m_log
->OnFrameCreate(this);
446 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
451 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
457 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
461 const wxChar
*szFileName
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"));
462 if ( szFileName
== NULL
) {
471 if ( wxFile::Exists(szFileName
) ) {
472 bool bAppend
= FALSE
;
474 strMsg
.Printf(_("Append log to file '%s' "
475 "(choosing [No] will overwrite it)?"), szFileName
);
476 switch ( wxMessageBox(strMsg
, _("Question"), wxYES_NO
| wxCANCEL
) ) {
489 wxFAIL_MSG(_("invalid message box return value"));
493 bOk
= file
.Open(szFileName
, wxFile::write_append
);
496 bOk
= file
.Create(szFileName
, TRUE
/* overwrite */);
500 bOk
= file
.Create(szFileName
);
503 // retrieve text and save it
504 // -------------------------
505 int nLines
= m_pTextCtrl
->GetNumberOfLines();
506 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
507 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
508 // we're not going to pull in the whole wxTextFile if all we need is this...
511 #else // !wxUSE_TEXTFILE
513 #endif // wxUSE_TEXTFILE
521 wxLogError(_("Can't save log contents to file."));
524 wxLogStatus(this, _("Log saved to the file '%s'."), szFileName
);
529 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
531 m_pTextCtrl
->Clear();
534 wxLogFrame::~wxLogFrame()
536 m_log
->OnFrameDelete(this);
541 wxLogWindow::wxLogWindow(wxFrame
*pParent
,
542 const wxChar
*szTitle
,
546 m_bPassMessages
= bDoPass
;
548 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
549 m_pOldLog
= wxLog::SetActiveTarget(this);
552 m_pLogFrame
->Show(TRUE
);
555 void wxLogWindow::Show(bool bShow
)
557 m_pLogFrame
->Show(bShow
);
560 void wxLogWindow::Flush()
562 if ( m_pOldLog
!= NULL
)
565 m_bHasMessages
= FALSE
;
568 void wxLogWindow::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
570 // first let the previous logger show it
571 if ( m_pOldLog
!= NULL
&& m_bPassMessages
) {
572 // bogus cast just to access protected DoLog
573 ((wxLogWindow
*)m_pOldLog
)->DoLog(level
, szString
, t
);
579 // by default, these messages are ignored by wxLog, so process
581 if ( !wxIsEmpty(szString
) )
584 str
<< _("Status: ") << szString
;
589 // don't put trace messages in the text window for 2 reasons:
590 // 1) there are too many of them
591 // 2) they may provoke other trace messages thus sending a program
592 // into an infinite loop
597 // and this will format it nicely and call our DoLogString()
598 wxLog::DoLog(level
, szString
, t
);
602 m_bHasMessages
= TRUE
;
605 void wxLogWindow::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
607 // put the text into our window
608 wxTextCtrl
*pText
= m_pLogFrame
->TextCtrl();
610 // remove selection (WriteText is in fact ReplaceSelection)
612 long nLen
= pText
->GetLastPosition();
613 pText
->SetSelection(nLen
, nLen
);
618 msg
<< szString
<< wxT('\n');
620 pText
->AppendText(msg
);
622 // TODO ensure that the line can be seen
625 wxFrame
*wxLogWindow::GetFrame() const
630 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
634 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
636 m_pLogFrame
= (wxLogFrame
*)NULL
;
639 wxLogWindow::~wxLogWindow()
643 // may be NULL if log frame already auto destroyed itself
647 // ----------------------------------------------------------------------------
649 // ----------------------------------------------------------------------------
653 static const size_t MARGIN
= 10;
655 wxLogDialog::wxLogDialog(wxWindow
*parent
,
656 const wxArrayString
& messages
,
657 const wxArrayInt
& severity
,
658 const wxArrayLong
& times
,
659 const wxString
& caption
,
661 : wxDialog(parent
, -1, caption
)
663 size_t count
= messages
.GetCount();
664 m_messages
.Alloc(count
);
665 m_severity
.Alloc(count
);
666 m_times
.Alloc(count
);
668 for ( size_t n
= 0; n
< count
; n
++ )
670 wxString msg
= messages
[n
];
673 m_messages
.Add(msg
.BeforeFirst(_T('\n')));
674 msg
= msg
.AfterFirst(_T('\n'));
676 m_severity
.Add(severity
[n
]);
677 m_times
.Add(times
[n
]);
682 m_showingDetails
= FALSE
; // not initially
683 m_listctrl
= (wxListCtrl
*)NULL
;
685 // create the controls which are always shown and layout them: we use
686 // sizers even though our window is not resizeable to calculate the size of
687 // the dialog properly
688 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
689 wxBoxSizer
*sizerButtons
= new wxBoxSizer(wxVERTICAL
);
690 wxBoxSizer
*sizerAll
= new wxBoxSizer(wxHORIZONTAL
);
692 wxButton
*btnOk
= new wxButton(this, wxID_OK
, _T("OK"));
693 sizerButtons
->Add(btnOk
, 0, wxCENTRE
|wxBOTTOM
, MARGIN
/2);
694 m_btnDetails
= new wxButton(this, wxID_MORE
, _T("&Details >>"));
695 sizerButtons
->Add(m_btnDetails
, 0, wxCENTRE
|wxTOP
, MARGIN
/2 - 1);
697 wxIcon icon
= wxTheApp
->GetStdIcon((int)(style
& wxICON_MASK
));
698 sizerAll
->Add(new wxStaticBitmap(this, -1, icon
), 0, wxCENTRE
);
699 const wxString
& message
= messages
.Last();
700 sizerAll
->Add(CreateTextSizer(message
), 0, wxCENTRE
|wxLEFT
|wxRIGHT
, MARGIN
);
701 sizerAll
->Add(sizerButtons
, 0, wxALIGN_RIGHT
|wxLEFT
, MARGIN
);
703 sizerTop
->Add(sizerAll
, 0, wxCENTRE
|wxALL
, MARGIN
);
708 sizerTop
->SetSizeHints(this);
713 // this can't happen any more as we don't use this dialog in this case
717 // no details... it's easier to disable a button than to change the
718 // dialog layout depending on whether we have details or not
719 m_btnDetails
->Disable();
726 void wxLogDialog::OnListSelect(wxListEvent
& event
)
728 // we can't just disable the control because this looks ugly under Windows
729 // (wrong bg colour, no scrolling...), but we still want to disable
730 // selecting items - it makes no sense here
731 m_listctrl
->SetItemState(event
.GetItem(), 0, wxLIST_STATE_SELECTED
);
734 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
739 void wxLogDialog::OnDetails(wxCommandEvent
& WXUNUSED(event
))
741 wxSizer
*sizer
= GetSizer();
743 if ( m_showingDetails
)
745 m_btnDetails
->SetLabel(_T("&Details >>"));
747 sizer
->Remove(m_listctrl
);
749 else // show details now
751 m_btnDetails
->SetLabel(_T("<< &Details"));
756 m_listctrl
= new wxListCtrl(this, -1,
757 wxDefaultPosition
, wxDefaultSize
,
762 m_listctrl
->InsertColumn(0, _("Message"));
763 m_listctrl
->InsertColumn(1, _("Time"));
765 // prepare the imagelist
766 static const int ICON_SIZE
= 16;
767 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
769 // order should be the same as in the switch below!
770 static const int icons
[] =
777 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
779 wxBitmap bmp
= wxTheApp
->GetStdIcon(icons
[icon
]);
780 imageList
->Add(wxImage(bmp
).
781 Rescale(ICON_SIZE
, ICON_SIZE
).
785 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
788 wxString fmt
= wxLog::GetTimestamp();
795 size_t count
= m_messages
.GetCount();
796 for ( size_t n
= 0; n
< count
; n
++ )
799 switch ( m_severity
[n
] )
813 m_listctrl
->InsertItem(n
, m_messages
[n
], image
);
814 m_listctrl
->SetItem(n
, 1,
815 wxDateTime((time_t)m_times
[n
]).Format(fmt
));
818 // let the columns size themselves
819 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
820 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
822 // get the approx height of the listctrl
823 wxFont font
= GetFont();
825 font
= *wxSWISS_FONT
;
828 GetTextExtent(_T("H"), (int*)NULL
, &y
, (int*)NULL
, (int*)NULL
, &font
);
829 int height
= wxMin(y
*(count
+ 3), 100);
830 m_listctrl
->SetSize(-1, height
);
833 sizer
->Add(m_listctrl
, 1, wxEXPAND
|(wxALL
& ~wxTOP
), MARGIN
);
836 m_showingDetails
= !m_showingDetails
;
838 // in any case, our size changed - update
839 sizer
->SetSizeHints(this);
843 wxLogDialog::~wxLogDialog()
847 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
851 #endif // wxUSE_LOG_DIALOG