Added missing includes
[wxWidgets.git] / src / generic / logg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: logg.cpp
3 // Purpose: wxLog-derived classes which need GUI support (the rest is in
4 // src/common/log.cpp)
5 // Author: Vadim Zeitlin
6 // Modified by:
7 // Created: 20.09.99 (extracted from src/common/log.cpp)
8 // RCS-ID: $Id$
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
12
13 // ============================================================================
14 // declarations
15 // ============================================================================
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 // no #pragma implementation "log.h" because it's already in src/common/log.cpp
22
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #if !wxUSE_GUI
31 #error "This file can't be compiled without GUI!"
32 #endif
33
34 #ifndef WX_PRECOMP
35 #include "wx/app.h"
36 #include "wx/intl.h"
37 #include "wx/log.h"
38 #include "wx/menu.h"
39 #include "wx/frame.h"
40 #include "wx/filedlg.h"
41 #include "wx/msgdlg.h"
42 #include "wx/textctrl.h"
43 #include "wx/sizer.h"
44 #include "wx/statbmp.h"
45 #endif // WX_PRECOMP
46
47 #include "wx/file.h"
48 #include "wx/textfile.h"
49
50 #ifdef __WXMSW__
51 // for OutputDebugString()
52 #include "wx/msw/private.h"
53 #endif // Windows
54
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
58
59 #if wxUSE_LOG_DIALOG
60 #include "wx/datetime.h"
61 #include "wx/listctrl.h"
62 #include "wx/imaglist.h"
63 #include "wx/image.h"
64 #else // !wxUSE_TEXTFILE
65 #include "wx/msgdlg.h"
66 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
67
68 // ----------------------------------------------------------------------------
69 // private classes
70 // ----------------------------------------------------------------------------
71
72 #if wxUSE_LOG_DIALOG
73
74 class wxLogDialog : public wxDialog
75 {
76 public:
77 wxLogDialog(wxWindow *parent,
78 const wxArrayString& messages,
79 const wxArrayInt& severity,
80 const wxArrayLong& timess,
81 const wxString& caption,
82 long style);
83 virtual ~wxLogDialog();
84
85 // event handlers
86 void OnOk(wxCommandEvent& event);
87 void OnDetails(wxCommandEvent& event);
88 void OnListSelect(wxListEvent& event);
89
90 private:
91 // the data for the listctrl
92 wxArrayString m_messages;
93 wxArrayInt m_severity;
94 wxArrayLong m_times;
95
96 // the "toggle" button and its state
97 wxButton *m_btnDetails;
98 bool m_showingDetails;
99
100 // the listctrl (not shown initially)
101 wxListCtrl *m_listctrl;
102
103 // the translated "Details" string
104 static wxString ms_details;
105
106 DECLARE_EVENT_TABLE()
107 };
108
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)
113 END_EVENT_TABLE()
114
115 #endif // wxUSE_LOG_DIALOG
116
117 // ----------------------------------------------------------------------------
118 // global variables
119 // ----------------------------------------------------------------------------
120
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
124
125 // ============================================================================
126 // implementation
127 // ============================================================================
128
129 // ----------------------------------------------------------------------------
130 // global functions
131 // ----------------------------------------------------------------------------
132
133 // accepts an additional argument which tells to which frame the output should
134 // be directed
135 void wxLogStatus(wxFrame *pFrame, const wxChar *szFormat, ...)
136 {
137 wxString msg;
138
139 wxLog *pLog = wxLog::GetActiveTarget();
140 if ( pLog != NULL ) {
141 va_list argptr;
142 va_start(argptr, szFormat);
143 msg.PrintfV(szFormat, argptr);
144 va_end(argptr);
145
146 wxASSERT( gs_pFrame == NULL ); // should be reset!
147 gs_pFrame = pFrame;
148 wxLog::OnLog(wxLOG_Status, msg, time(NULL));
149 gs_pFrame = (wxFrame *) NULL;
150 }
151 }
152
153 // ----------------------------------------------------------------------------
154 // wxLogTextCtrl implementation
155 // ----------------------------------------------------------------------------
156
157 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl)
158 {
159 m_pTextCtrl = pTextCtrl;
160 }
161
162 void wxLogTextCtrl::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
163 {
164 wxString msg;
165 TimeStamp(&msg);
166 msg << szString << wxT('\n');
167
168 m_pTextCtrl->AppendText(msg);
169 }
170
171 // ----------------------------------------------------------------------------
172 // wxLogGui implementation (FIXME MT-unsafe)
173 // ----------------------------------------------------------------------------
174
175 wxLogGui::wxLogGui()
176 {
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");
183
184 Clear();
185 }
186
187 void wxLogGui::Clear()
188 {
189 m_bErrors =
190 m_bWarnings =
191 m_bHasMessages = FALSE;
192
193 m_aMessages.Empty();
194 m_aSeverity.Empty();
195 m_aTimes.Empty();
196 }
197
198 void wxLogGui::Flush()
199 {
200 if ( !m_bHasMessages )
201 return;
202
203 // do it right now to block any new calls to Flush() while we're here
204 m_bHasMessages = FALSE;
205
206 wxString title = wxTheApp->GetAppName();
207 if ( !!title )
208 {
209 title[0u] = wxToupper(title[0u]);
210 title += _T(' ');
211 }
212
213 long style;
214 if ( m_bErrors ) {
215 title += m_error;
216 style = wxICON_STOP;
217 }
218 else if ( m_bWarnings ) {
219 title += m_warning;
220 style = wxICON_EXCLAMATION;
221 }
222 else {
223 title += m_info;
224 style = wxICON_INFORMATION;
225 }
226
227 // this is the best we can do here
228 wxWindow *parent = wxTheApp->GetTopWindow();
229
230 size_t nMsgCount = m_aMessages.Count();
231
232 wxString str;
233 if ( nMsgCount == 1 )
234 {
235 str = m_aMessages[0];
236 }
237 else // more than one message
238 {
239 #if wxUSE_LOG_DIALOG
240 wxLogDialog dlg(parent,
241 m_aMessages, m_aSeverity, m_aTimes,
242 title, style);
243
244 // clear the message list before showing the dialog because while it's
245 // shown some new messages may appear
246 Clear();
247
248 (void)dlg.ShowModal();
249 #else // !wxUSE_LOG_DIALOG
250 // concatenate all strings (but not too many to not overfill the msg box)
251 size_t nLines = 0;
252
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;
257
258 nLines += (m_aMessages[n - 1].Len() + nMsgLineWidth - 1) / nMsgLineWidth;
259
260 if ( nLines > 25 ) // don't put too many lines in message box
261 break;
262
263 str << m_aMessages[n - 1] << wxT("\n");
264 }
265 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
266 }
267
268 // this catches both cases of 1 message with wxUSE_LOG_DIALOG and any
269 // situation without it
270 if ( !!str )
271 {
272 wxMessageBox(str, title, wxOK | style, parent);
273
274 // no undisplayed messages whatsoever
275 Clear();
276 }
277 }
278
279 // log all kinds of messages
280 void wxLogGui::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
281 {
282 switch ( level ) {
283 case wxLOG_Info:
284 if ( GetVerbose() )
285 case wxLOG_Message:
286 {
287 if ( !m_bErrors ) {
288 m_aMessages.Add(szString);
289 m_aSeverity.Add(wxLOG_Message);
290 m_aTimes.Add((long)t);
291 m_bHasMessages = TRUE;
292 }
293 }
294 break;
295
296 case wxLOG_Status:
297 #if wxUSE_STATUSBAR
298 {
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;
305 }
306 }
307
308 if ( pFrame && pFrame->GetStatusBar() )
309 pFrame->SetStatusText(szString);
310 }
311 #endif // wxUSE_STATUSBAR
312 break;
313
314 case wxLOG_Trace:
315 case wxLOG_Debug:
316 #ifdef __WXDEBUG__
317 {
318 #ifdef __WXMSW__
319 // don't prepend debug/trace here: it goes to the
320 // debug window anyhow, but do put a timestamp
321 wxString str;
322 TimeStamp(&str);
323 str << szString << wxT("\n\r");
324 OutputDebugString(str);
325 #else
326 // send them to stderr
327 wxFprintf(stderr, wxT("%s: %s\n"),
328 level == wxLOG_Trace ? wxT("Trace")
329 : wxT("Debug"),
330 szString);
331 fflush(stderr);
332 #endif
333 }
334 #endif // __WXDEBUG__
335
336 break;
337
338 case wxLOG_FatalError:
339 // show this one immediately
340 wxMessageBox(szString, _("Fatal error"), wxICON_HAND);
341 wxExit();
342 break;
343
344 case wxLOG_Error:
345 if ( !m_bErrors ) {
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
350 m_aMessages.Empty();
351 m_aSeverity.Empty();
352 m_aTimes.Empty();
353 #endif // wxUSE_LOG_DIALOG
354 m_bErrors = TRUE;
355 }
356 // fall through
357
358 case wxLOG_Warning:
359 if ( !m_bErrors ) {
360 // for the warning we don't discard the info messages
361 m_bWarnings = TRUE;
362 }
363
364 m_aMessages.Add(szString);
365 m_aSeverity.Add((int)level);
366 m_aTimes.Add((long)t);
367 m_bHasMessages = TRUE;
368 break;
369 }
370 }
371
372 // ----------------------------------------------------------------------------
373 // wxLogWindow and wxLogFrame implementation
374 // ----------------------------------------------------------------------------
375
376 // log frame class
377 // ---------------
378 class wxLogFrame : public wxFrame
379 {
380 public:
381 // ctor & dtor
382 wxLogFrame(wxFrame *pParent, wxLogWindow *log, const wxChar *szTitle);
383 virtual ~wxLogFrame();
384
385 // menu callbacks
386 void OnClose(wxCommandEvent& event);
387 void OnCloseWindow(wxCloseEvent& event);
388 #if wxUSE_FILE
389 void OnSave (wxCommandEvent& event);
390 #endif // wxUSE_FILE
391 void OnClear(wxCommandEvent& event);
392
393 void OnIdle(wxIdleEvent&);
394
395 // accessors
396 wxTextCtrl *TextCtrl() const { return m_pTextCtrl; }
397
398 private:
399 // use standard ids for our commands!
400 enum
401 {
402 Menu_Close = wxID_CLOSE,
403 Menu_Save = wxID_SAVE,
404 Menu_Clear = wxID_CLEAR
405 };
406
407 // instead of closing just hide the window to be able to Show() it later
408 void DoClose() { Show(FALSE); }
409
410 wxTextCtrl *m_pTextCtrl;
411 wxLogWindow *m_log;
412
413 DECLARE_EVENT_TABLE()
414 };
415
416 BEGIN_EVENT_TABLE(wxLogFrame, wxFrame)
417 // wxLogWindow menu events
418 EVT_MENU(Menu_Close, wxLogFrame::OnClose)
419 #if wxUSE_FILE
420 EVT_MENU(Menu_Save, wxLogFrame::OnSave)
421 #endif // wxUSE_FILE
422 EVT_MENU(Menu_Clear, wxLogFrame::OnClear)
423
424 EVT_CLOSE(wxLogFrame::OnCloseWindow)
425 END_EVENT_TABLE()
426
427 wxLogFrame::wxLogFrame(wxFrame *pParent, wxLogWindow *log, const wxChar *szTitle)
428 : wxFrame(pParent, -1, szTitle)
429 {
430 m_log = log;
431
432 m_pTextCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition,
433 wxDefaultSize,
434 wxTE_MULTILINE |
435 wxHSCROLL |
436 wxTE_READONLY);
437
438 // create menu
439 wxMenuBar *pMenuBar = new wxMenuBar;
440 wxMenu *pMenu = new wxMenu;
441 #if wxUSE_FILE
442 pMenu->Append(Menu_Save, _("&Save..."), _("Save log contents to file"));
443 #endif // wxUSE_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);
449
450 #if wxUSE_STATUSBAR
451 // status bar for menu prompts
452 CreateStatusBar();
453 #endif // wxUSE_STATUSBAR
454
455 m_log->OnFrameCreate(this);
456 }
457
458 void wxLogFrame::OnClose(wxCommandEvent& WXUNUSED(event))
459 {
460 DoClose();
461 }
462
463 void wxLogFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
464 {
465 DoClose();
466 }
467
468 #if wxUSE_FILE
469 void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event))
470 {
471 // get the file name
472 // -----------------
473 const wxChar *szFileName = wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"));
474 if ( szFileName == NULL ) {
475 // cancelled
476 return;
477 }
478
479 // open file
480 // ---------
481 wxFile file;
482 bool bOk = FALSE;
483 if ( wxFile::Exists(szFileName) ) {
484 bool bAppend = FALSE;
485 wxString strMsg;
486 strMsg.Printf(_("Append log to file '%s' "
487 "(choosing [No] will overwrite it)?"), szFileName);
488 switch ( wxMessageBox(strMsg, _("Question"), wxYES_NO | wxCANCEL) ) {
489 case wxYES:
490 bAppend = TRUE;
491 break;
492
493 case wxNO:
494 bAppend = FALSE;
495 break;
496
497 case wxCANCEL:
498 return;
499
500 default:
501 wxFAIL_MSG(_("invalid message box return value"));
502 }
503
504 if ( bAppend ) {
505 bOk = file.Open(szFileName, wxFile::write_append);
506 }
507 else {
508 bOk = file.Create(szFileName, TRUE /* overwrite */);
509 }
510 }
511 else {
512 bOk = file.Create(szFileName);
513 }
514
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...
521 #if wxUSE_TEXTFILE
522 wxTextFile::GetEOL()
523 #else // !wxUSE_TEXTFILE
524 '\n'
525 #endif // wxUSE_TEXTFILE
526 );
527 }
528
529 if ( bOk )
530 bOk = file.Close();
531
532 if ( !bOk ) {
533 wxLogError(_("Can't save log contents to file."));
534 }
535 else {
536 wxLogStatus(this, _("Log saved to the file '%s'."), szFileName);
537 }
538 }
539 #endif // wxUSE_FILE
540
541 void wxLogFrame::OnClear(wxCommandEvent& WXUNUSED(event))
542 {
543 m_pTextCtrl->Clear();
544 }
545
546 wxLogFrame::~wxLogFrame()
547 {
548 m_log->OnFrameDelete(this);
549 }
550
551 // wxLogWindow
552 // -----------
553 wxLogWindow::wxLogWindow(wxFrame *pParent,
554 const wxChar *szTitle,
555 bool bShow,
556 bool bDoPass)
557 {
558 m_bPassMessages = bDoPass;
559
560 m_pLogFrame = new wxLogFrame(pParent, this, szTitle);
561 m_pOldLog = wxLog::SetActiveTarget(this);
562
563 if ( bShow )
564 m_pLogFrame->Show(TRUE);
565 }
566
567 void wxLogWindow::Show(bool bShow)
568 {
569 m_pLogFrame->Show(bShow);
570 }
571
572 void wxLogWindow::Flush()
573 {
574 if ( m_pOldLog != NULL )
575 m_pOldLog->Flush();
576
577 m_bHasMessages = FALSE;
578 }
579
580 void wxLogWindow::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
581 {
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);
586 }
587
588 if ( m_pLogFrame ) {
589 switch ( level ) {
590 case wxLOG_Status:
591 // by default, these messages are ignored by wxLog, so process
592 // them ourselves
593 if ( !wxIsEmpty(szString) )
594 {
595 wxString str;
596 str << _("Status: ") << szString;
597 DoLogString(str, t);
598 }
599 break;
600
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
605 case wxLOG_Trace:
606 break;
607
608 default:
609 // and this will format it nicely and call our DoLogString()
610 wxLog::DoLog(level, szString, t);
611 }
612 }
613
614 m_bHasMessages = TRUE;
615 }
616
617 void wxLogWindow::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
618 {
619 // put the text into our window
620 wxTextCtrl *pText = m_pLogFrame->TextCtrl();
621
622 // remove selection (WriteText is in fact ReplaceSelection)
623 #ifdef __WXMSW__
624 long nLen = pText->GetLastPosition();
625 pText->SetSelection(nLen, nLen);
626 #endif // Windows
627
628 wxString msg;
629 TimeStamp(&msg);
630 msg << szString << wxT('\n');
631
632 pText->AppendText(msg);
633
634 // TODO ensure that the line can be seen
635 }
636
637 wxFrame *wxLogWindow::GetFrame() const
638 {
639 return m_pLogFrame;
640 }
641
642 void wxLogWindow::OnFrameCreate(wxFrame * WXUNUSED(frame))
643 {
644 }
645
646 void wxLogWindow::OnFrameDelete(wxFrame * WXUNUSED(frame))
647 {
648 m_pLogFrame = (wxLogFrame *)NULL;
649 }
650
651 wxLogWindow::~wxLogWindow()
652 {
653 delete m_pOldLog;
654
655 // may be NULL if log frame already auto destroyed itself
656 delete m_pLogFrame;
657 }
658
659 // ----------------------------------------------------------------------------
660 // wxLogDialog
661 // ----------------------------------------------------------------------------
662
663 #if wxUSE_LOG_DIALOG
664
665 static const size_t MARGIN = 10;
666
667 wxString wxLogDialog::ms_details;
668
669 wxLogDialog::wxLogDialog(wxWindow *parent,
670 const wxArrayString& messages,
671 const wxArrayInt& severity,
672 const wxArrayLong& times,
673 const wxString& caption,
674 long style)
675 : wxDialog(parent, -1, caption )
676 {
677 if ( ms_details )
678 {
679 // ensure that we won't try to call wxGetTranslation() twice
680 ms_details = _T("&Details");
681 ms_details = wxGetTranslation(ms_details);
682 }
683
684 size_t count = messages.GetCount();
685 m_messages.Alloc(count);
686 m_severity.Alloc(count);
687 m_times.Alloc(count);
688
689 for ( size_t n = 0; n < count; n++ )
690 {
691 wxString msg = messages[n];
692 do
693 {
694 m_messages.Add(msg.BeforeFirst(_T('\n')));
695 msg = msg.AfterFirst(_T('\n'));
696
697 m_severity.Add(severity[n]);
698 m_times.Add(times[n]);
699 }
700 while ( !!msg );
701 }
702
703 m_showingDetails = FALSE; // not initially
704 m_listctrl = (wxListCtrl *)NULL;
705
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);
712
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);
717
718 wxIcon icon = wxTheApp->GetStdIcon((int)(style & wxICON_MASK));
719 sizerAll->Add(new wxStaticBitmap(this, -1, icon), 0, wxCENTRE);
720 const wxString& message = messages.Last();
721 sizerAll->Add(CreateTextSizer(message), 0, wxCENTRE|wxLEFT|wxRIGHT, MARGIN);
722 sizerAll->Add(sizerButtons, 0, wxALIGN_RIGHT|wxLEFT, MARGIN);
723
724 sizerTop->Add(sizerAll, 0, wxCENTRE|wxALL, MARGIN);
725
726 SetAutoLayout(TRUE);
727 SetSizer(sizerTop);
728
729 sizerTop->SetSizeHints(this);
730 sizerTop->Fit(this);
731
732 btnOk->SetFocus();
733
734 // this can't happen any more as we don't use this dialog in this case
735 #if 0
736 if ( count == 1 )
737 {
738 // no details... it's easier to disable a button than to change the
739 // dialog layout depending on whether we have details or not
740 m_btnDetails->Disable();
741 }
742 #endif // 0
743
744 Centre();
745 }
746
747 void wxLogDialog::OnListSelect(wxListEvent& event)
748 {
749 // we can't just disable the control because this looks ugly under Windows
750 // (wrong bg colour, no scrolling...), but we still want to disable
751 // selecting items - it makes no sense here
752 m_listctrl->SetItemState(event.GetIndex(), 0, wxLIST_STATE_SELECTED);
753 }
754
755 void wxLogDialog::OnOk(wxCommandEvent& WXUNUSED(event))
756 {
757 EndModal(wxID_OK);
758 }
759
760 void wxLogDialog::OnDetails(wxCommandEvent& WXUNUSED(event))
761 {
762 wxSizer *sizer = GetSizer();
763
764 if ( m_showingDetails )
765 {
766 m_btnDetails->SetLabel(ms_details + _T(">>"));
767
768 sizer->Remove(m_listctrl);
769 }
770 else // show details now
771 {
772 m_btnDetails->SetLabel(wxString(_T("<< ")) + ms_details);
773
774 if ( !m_listctrl )
775 {
776 // create it now
777 m_listctrl = new wxListCtrl(this, -1,
778 wxDefaultPosition, wxDefaultSize,
779 wxSUNKEN_BORDER |
780 wxLC_REPORT |
781 wxLC_NO_HEADER |
782 wxLC_SINGLE_SEL);
783 // no need to translate these strings as they're not shown to the
784 // user anyhow (we use wxLC_NO_HEADER style)
785 m_listctrl->InsertColumn(0, _T("Message"));
786 m_listctrl->InsertColumn(1, _T("Time"));
787
788 // prepare the imagelist
789 static const int ICON_SIZE = 16;
790 wxImageList *imageList = new wxImageList(ICON_SIZE, ICON_SIZE);
791
792 // order should be the same as in the switch below!
793 static const int icons[] =
794 {
795 wxICON_ERROR,
796 wxICON_EXCLAMATION,
797 wxICON_INFORMATION
798 };
799
800 for ( size_t icon = 0; icon < WXSIZEOF(icons); icon++ )
801 {
802 wxBitmap bmp = wxTheApp->GetStdIcon(icons[icon]);
803 imageList->Add(wxImage(bmp).
804 Rescale(ICON_SIZE, ICON_SIZE).
805 ConvertToBitmap());
806 }
807
808 m_listctrl->SetImageList(imageList, wxIMAGE_LIST_SMALL);
809
810 // and fill it
811 wxString fmt = wxLog::GetTimestamp();
812 if ( !fmt )
813 {
814 // default format
815 fmt = _T("%X");
816 }
817
818 size_t count = m_messages.GetCount();
819 for ( size_t n = 0; n < count; n++ )
820 {
821 int image;
822 switch ( m_severity[n] )
823 {
824 case wxLOG_Error:
825 image = 0;
826 break;
827
828 case wxLOG_Warning:
829 image = 1;
830 break;
831
832 default:
833 image = 2;
834 }
835
836 m_listctrl->InsertItem(n, m_messages[n], image);
837 m_listctrl->SetItem(n, 1,
838 wxDateTime((time_t)m_times[n]).Format(fmt));
839 }
840
841 // let the columns size themselves
842 m_listctrl->SetColumnWidth(0, wxLIST_AUTOSIZE);
843 m_listctrl->SetColumnWidth(1, wxLIST_AUTOSIZE);
844
845 // get the approx height of the listctrl
846 wxFont font = GetFont();
847 if ( !font.Ok() )
848 font = *wxSWISS_FONT;
849
850 int y;
851 GetTextExtent(_T("H"), (int*)NULL, &y, (int*)NULL, (int*)NULL, &font);
852 int height = wxMin(y*(count + 3), 100);
853 m_listctrl->SetSize(-1, height);
854 }
855
856 sizer->Add(m_listctrl, 1, wxEXPAND|(wxALL & ~wxTOP), MARGIN);
857 }
858
859 m_showingDetails = !m_showingDetails;
860
861 // in any case, our size changed - update
862 sizer->SetSizeHints(this);
863 sizer->Fit(this);
864 }
865
866 wxLogDialog::~wxLogDialog()
867 {
868 if ( m_listctrl )
869 {
870 delete m_listctrl->GetImageList(wxIMAGE_LIST_SMALL);
871 }
872 }
873
874 #endif // wxUSE_LOG_DIALOG