]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/generic/logg.cpp
Fix HTML help on Mac.
[wxWidgets.git] / src / generic / logg.cpp
... / ...
CommitLineData
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/button.h"
37 #include "wx/intl.h"
38 #include "wx/log.h"
39 #include "wx/menu.h"
40 #include "wx/frame.h"
41 #include "wx/filedlg.h"
42 #include "wx/msgdlg.h"
43 #include "wx/textctrl.h"
44 #include "wx/sizer.h"
45 #include "wx/statbmp.h"
46 #include "wx/button.h"
47#endif // WX_PRECOMP
48
49#if wxUSE_LOGGUI || wxUSE_LOGWINDOW
50
51#include "wx/file.h"
52#include "wx/textfile.h"
53#include "wx/statline.h"
54
55#ifdef __WXMSW__
56 // for OutputDebugString()
57 #include "wx/msw/private.h"
58#endif // Windows
59
60#if wxUSE_LOG_DIALOG
61 #include "wx/listctrl.h"
62 #include "wx/imaglist.h"
63 #include "wx/image.h"
64#else // !wxUSE_LOG_DIALOG
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// this function is a wrapper around strftime(3)
75// allows to exclude the usage of wxDateTime
76static wxString TimeStamp(const wxChar *format, time_t t)
77{
78 wxChar buf[4096];
79 if ( !wxStrftime(buf, WXSIZEOF(buf), format, localtime(&t)) )
80 {
81 // buffer is too small?
82 wxFAIL_MSG(_T("strftime() failed"));
83 }
84 return wxString(buf);
85}
86
87
88class wxLogDialog : public wxDialog
89{
90public:
91 wxLogDialog(wxWindow *parent,
92 const wxArrayString& messages,
93 const wxArrayInt& severity,
94 const wxArrayLong& timess,
95 const wxString& caption,
96 long style);
97 virtual ~wxLogDialog();
98
99 // event handlers
100 void OnOk(wxCommandEvent& event);
101 void OnDetails(wxCommandEvent& event);
102#if wxUSE_FILE
103 void OnSave(wxCommandEvent& event);
104#endif // wxUSE_FILE
105 void OnListSelect(wxListEvent& event);
106
107private:
108 // create controls needed for the details display
109 void CreateDetailsControls();
110
111 // the data for the listctrl
112 wxArrayString m_messages;
113 wxArrayInt m_severity;
114 wxArrayLong m_times;
115
116 // the "toggle" button and its state
117 wxButton *m_btnDetails;
118 bool m_showingDetails;
119
120 // the controls which are not shown initially (but only when details
121 // button is pressed)
122 wxListCtrl *m_listctrl;
123#if wxUSE_STATLINE
124 wxStaticLine *m_statline;
125#endif // wxUSE_STATLINE
126#if wxUSE_FILE
127 wxButton *m_btnSave;
128#endif // wxUSE_FILE
129
130 // the translated "Details" string
131 static wxString ms_details;
132
133 DECLARE_EVENT_TABLE()
134};
135
136BEGIN_EVENT_TABLE(wxLogDialog, wxDialog)
137 EVT_BUTTON(wxID_CANCEL, wxLogDialog::OnOk)
138 EVT_BUTTON(wxID_MORE, wxLogDialog::OnDetails)
139#if wxUSE_FILE
140 EVT_BUTTON(wxID_SAVE, wxLogDialog::OnSave)
141#endif // wxUSE_FILE
142 EVT_LIST_ITEM_SELECTED(-1, wxLogDialog::OnListSelect)
143END_EVENT_TABLE()
144
145#endif // wxUSE_LOG_DIALOG
146
147// ----------------------------------------------------------------------------
148// private functions
149// ----------------------------------------------------------------------------
150
151#if wxUSE_FILE && wxUSE_FILEDLG
152
153// pass an uninitialized file object, the function will ask the user for the
154// filename and try to open it, returns TRUE on success (file was opened),
155// FALSE if file couldn't be opened/created and -1 if the file selection
156// dialog was cancelled
157static int OpenLogFile(wxFile& file, wxString *filename = NULL);
158
159#endif // wxUSE_FILE
160
161// ----------------------------------------------------------------------------
162// global variables
163// ----------------------------------------------------------------------------
164
165// we use a global variable to store the frame pointer for wxLogStatus - bad,
166// but it's the easiest way
167static wxFrame *gs_pFrame; // FIXME MT-unsafe
168
169// ============================================================================
170// implementation
171// ============================================================================
172
173// ----------------------------------------------------------------------------
174// global functions
175// ----------------------------------------------------------------------------
176
177// accepts an additional argument which tells to which frame the output should
178// be directed
179void wxLogStatus(wxFrame *pFrame, const wxChar *szFormat, ...)
180{
181 wxString msg;
182
183 wxLog *pLog = wxLog::GetActiveTarget();
184 if ( pLog != NULL ) {
185 va_list argptr;
186 va_start(argptr, szFormat);
187 msg.PrintfV(szFormat, argptr);
188 va_end(argptr);
189
190 wxASSERT( gs_pFrame == NULL ); // should be reset!
191 gs_pFrame = pFrame;
192 wxLog::OnLog(wxLOG_Status, msg, time(NULL));
193 gs_pFrame = (wxFrame *) NULL;
194 }
195}
196
197// ----------------------------------------------------------------------------
198// wxLogGui implementation (FIXME MT-unsafe)
199// ----------------------------------------------------------------------------
200
201wxLogGui::wxLogGui()
202{
203 Clear();
204}
205
206void wxLogGui::Clear()
207{
208 m_bErrors =
209 m_bWarnings =
210 m_bHasMessages = FALSE;
211
212 m_aMessages.Empty();
213 m_aSeverity.Empty();
214 m_aTimes.Empty();
215}
216
217void wxLogGui::Flush()
218{
219 if ( !m_bHasMessages )
220 return;
221
222 // do it right now to block any new calls to Flush() while we're here
223 m_bHasMessages = FALSE;
224
225 wxString appName = wxTheApp->GetAppName();
226 if ( !!appName )
227 appName[0u] = wxToupper(appName[0u]);
228
229 long style;
230 wxString titleFormat;
231 if ( m_bErrors ) {
232 titleFormat = _("%s Error");
233 style = wxICON_STOP;
234 }
235 else if ( m_bWarnings ) {
236 titleFormat = _("%s Warning");
237 style = wxICON_EXCLAMATION;
238 }
239 else {
240 titleFormat = _("%s Information");
241 style = wxICON_INFORMATION;
242 }
243
244 wxString title;
245 title.Printf(titleFormat, appName.c_str());
246
247 // this is the best we can do here
248 wxWindow *parent = wxTheApp->GetTopWindow();
249
250 size_t nMsgCount = m_aMessages.Count();
251
252 wxString str;
253 if ( nMsgCount == 1 )
254 {
255 str = m_aMessages[0];
256 }
257 else // more than one message
258 {
259#if wxUSE_LOG_DIALOG
260
261 wxLogDialog dlg(parent,
262 m_aMessages, m_aSeverity, m_aTimes,
263 title, style);
264
265 // clear the message list before showing the dialog because while it's
266 // shown some new messages may appear
267 Clear();
268
269 (void)dlg.ShowModal();
270#else // !wxUSE_LOG_DIALOG
271 // concatenate all strings (but not too many to not overfill the msg box)
272 size_t nLines = 0;
273
274 // start from the most recent message
275 for ( size_t n = nMsgCount; n > 0; n-- ) {
276 // for Windows strings longer than this value are wrapped (NT 4.0)
277 const size_t nMsgLineWidth = 156;
278
279 nLines += (m_aMessages[n - 1].Len() + nMsgLineWidth - 1) / nMsgLineWidth;
280
281 if ( nLines > 25 ) // don't put too many lines in message box
282 break;
283
284 str << m_aMessages[n - 1] << wxT("\n");
285 }
286#endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
287 }
288
289 // this catches both cases of 1 message with wxUSE_LOG_DIALOG and any
290 // situation without it
291 if ( !!str )
292 {
293 wxMessageBox(str, title, wxOK | style, parent);
294
295 // no undisplayed messages whatsoever
296 Clear();
297 }
298}
299
300// log all kinds of messages
301void wxLogGui::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
302{
303 switch ( level ) {
304 case wxLOG_Info:
305 if ( GetVerbose() )
306 case wxLOG_Message:
307 {
308 if ( !m_bErrors ) {
309 m_aMessages.Add(szString);
310 m_aSeverity.Add(wxLOG_Message);
311 m_aTimes.Add((long)t);
312 m_bHasMessages = TRUE;
313 }
314 }
315 break;
316
317 case wxLOG_Status:
318#if wxUSE_STATUSBAR
319 {
320 // find the top window and set it's status text if it has any
321 wxFrame *pFrame = gs_pFrame;
322 if ( pFrame == NULL ) {
323 wxWindow *pWin = wxTheApp->GetTopWindow();
324 if ( pWin != NULL && pWin->IsKindOf(CLASSINFO(wxFrame)) ) {
325 pFrame = (wxFrame *)pWin;
326 }
327 }
328
329 if ( pFrame && pFrame->GetStatusBar() )
330 pFrame->SetStatusText(szString);
331 }
332#endif // wxUSE_STATUSBAR
333 break;
334
335 case wxLOG_Trace:
336 case wxLOG_Debug:
337 #ifdef __WXDEBUG__
338 {
339 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
340 // don't prepend debug/trace here: it goes to the
341 // debug window anyhow, but do put a timestamp
342 wxString str;
343 TimeStamp(&str);
344 str << szString << wxT("\r\n");
345 OutputDebugString(str);
346 #else
347 // send them to stderr
348 wxFprintf(stderr, wxT("%s: %s\n"),
349 level == wxLOG_Trace ? wxT("Trace")
350 : wxT("Debug"),
351 szString);
352 fflush(stderr);
353 #endif
354 }
355 #endif // __WXDEBUG__
356
357 break;
358
359 case wxLOG_FatalError:
360 // show this one immediately
361 wxMessageBox(szString, _("Fatal error"), wxICON_HAND);
362 wxExit();
363 break;
364
365 case wxLOG_Error:
366 if ( !m_bErrors ) {
367#if !wxUSE_LOG_DIALOG
368 // discard earlier informational messages if this is the 1st
369 // error because they might not make sense any more and showing
370 // them in a message box might be confusing
371 m_aMessages.Empty();
372 m_aSeverity.Empty();
373 m_aTimes.Empty();
374#endif // wxUSE_LOG_DIALOG
375 m_bErrors = TRUE;
376 }
377 // fall through
378
379 case wxLOG_Warning:
380 if ( !m_bErrors ) {
381 // for the warning we don't discard the info messages
382 m_bWarnings = TRUE;
383 }
384
385 m_aMessages.Add(szString);
386 m_aSeverity.Add((int)level);
387 m_aTimes.Add((long)t);
388 m_bHasMessages = TRUE;
389 break;
390 }
391}
392
393// ----------------------------------------------------------------------------
394// wxLogWindow and wxLogFrame implementation
395// ----------------------------------------------------------------------------
396
397// log frame class
398// ---------------
399class wxLogFrame : public wxFrame
400{
401public:
402 // ctor & dtor
403 wxLogFrame(wxFrame *pParent, wxLogWindow *log, const wxChar *szTitle);
404 virtual ~wxLogFrame();
405
406 // menu callbacks
407 void OnClose(wxCommandEvent& event);
408 void OnCloseWindow(wxCloseEvent& event);
409#if wxUSE_FILE
410 void OnSave (wxCommandEvent& event);
411#endif // wxUSE_FILE
412 void OnClear(wxCommandEvent& event);
413
414 void OnIdle(wxIdleEvent&);
415
416 // accessors
417 wxTextCtrl *TextCtrl() const { return m_pTextCtrl; }
418
419private:
420 // use standard ids for our commands!
421 enum
422 {
423 Menu_Close = wxID_CLOSE,
424 Menu_Save = wxID_SAVE,
425 Menu_Clear = wxID_CLEAR
426 };
427
428 // common part of OnClose() and OnCloseWindow()
429 void DoClose();
430
431 wxTextCtrl *m_pTextCtrl;
432 wxLogWindow *m_log;
433
434 DECLARE_EVENT_TABLE()
435};
436
437BEGIN_EVENT_TABLE(wxLogFrame, wxFrame)
438 // wxLogWindow menu events
439 EVT_MENU(Menu_Close, wxLogFrame::OnClose)
440#if wxUSE_FILE
441 EVT_MENU(Menu_Save, wxLogFrame::OnSave)
442#endif // wxUSE_FILE
443 EVT_MENU(Menu_Clear, wxLogFrame::OnClear)
444
445 EVT_CLOSE(wxLogFrame::OnCloseWindow)
446END_EVENT_TABLE()
447
448wxLogFrame::wxLogFrame(wxFrame *pParent, wxLogWindow *log, const wxChar *szTitle)
449 : wxFrame(pParent, -1, szTitle)
450{
451 m_log = log;
452
453 m_pTextCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition,
454 wxDefaultSize,
455 wxTE_MULTILINE |
456 wxHSCROLL |
457 wxTE_READONLY);
458
459#if wxUSE_MENUS
460 // create menu
461 wxMenuBar *pMenuBar = new wxMenuBar;
462 wxMenu *pMenu = new wxMenu;
463#if wxUSE_FILE
464 pMenu->Append(Menu_Save, _("&Save..."), _("Save log contents to file"));
465#endif // wxUSE_FILE
466 pMenu->Append(Menu_Clear, _("C&lear"), _("Clear the log contents"));
467 pMenu->AppendSeparator();
468 pMenu->Append(Menu_Close, _("&Close"), _("Close this window"));
469 pMenuBar->Append(pMenu, _("&Log"));
470 SetMenuBar(pMenuBar);
471#endif // wxUSE_MENUS
472
473#if wxUSE_STATUSBAR
474 // status bar for menu prompts
475 CreateStatusBar();
476#endif // wxUSE_STATUSBAR
477
478 m_log->OnFrameCreate(this);
479}
480
481void wxLogFrame::DoClose()
482{
483 if ( m_log->OnFrameClose(this) )
484 {
485 // instead of closing just hide the window to be able to Show() it
486 // later
487 Show(FALSE);
488 }
489}
490
491void wxLogFrame::OnClose(wxCommandEvent& WXUNUSED(event))
492{
493 DoClose();
494}
495
496void wxLogFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
497{
498 DoClose();
499}
500
501#if wxUSE_FILE
502void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event))
503{
504#if wxUSE_FILEDLG
505 wxString filename;
506 wxFile file;
507 int rc = OpenLogFile(file, &filename);
508 if ( rc == -1 )
509 {
510 // cancelled
511 return;
512 }
513
514 bool bOk = rc != 0;
515
516 // retrieve text and save it
517 // -------------------------
518 int nLines = m_pTextCtrl->GetNumberOfLines();
519 for ( int nLine = 0; bOk && nLine < nLines; nLine++ ) {
520 bOk = file.Write(m_pTextCtrl->GetLineText(nLine) +
521 wxTextFile::GetEOL());
522 }
523
524 if ( bOk )
525 bOk = file.Close();
526
527 if ( !bOk ) {
528 wxLogError(_("Can't save log contents to file."));
529 }
530 else {
531 wxLogStatus(this, _("Log saved to the file '%s'."), filename.c_str());
532 }
533#endif
534}
535#endif // wxUSE_FILE
536
537void wxLogFrame::OnClear(wxCommandEvent& WXUNUSED(event))
538{
539 m_pTextCtrl->Clear();
540}
541
542wxLogFrame::~wxLogFrame()
543{
544 m_log->OnFrameDelete(this);
545}
546
547// wxLogWindow
548// -----------
549
550wxLogWindow::wxLogWindow(wxFrame *pParent,
551 const wxChar *szTitle,
552 bool bShow,
553 bool bDoPass)
554{
555 PassMessages(bDoPass);
556
557 m_pLogFrame = new wxLogFrame(pParent, this, szTitle);
558
559 if ( bShow )
560 m_pLogFrame->Show(TRUE);
561}
562
563void wxLogWindow::Show(bool bShow)
564{
565 m_pLogFrame->Show(bShow);
566}
567
568void wxLogWindow::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
569{
570 // first let the previous logger show it
571 wxLogPassThrough::DoLog(level, szString, t);
572
573 if ( m_pLogFrame ) {
574 switch ( level ) {
575 case wxLOG_Status:
576 // by default, these messages are ignored by wxLog, so process
577 // them ourselves
578 if ( !wxIsEmpty(szString) )
579 {
580 wxString str;
581 str << _("Status: ") << szString;
582 DoLogString(str, t);
583 }
584 break;
585
586 // don't put trace messages in the text window for 2 reasons:
587 // 1) there are too many of them
588 // 2) they may provoke other trace messages thus sending a program
589 // into an infinite loop
590 case wxLOG_Trace:
591 break;
592
593 default:
594 // and this will format it nicely and call our DoLogString()
595 wxLog::DoLog(level, szString, t);
596 }
597 }
598
599 m_bHasMessages = TRUE;
600}
601
602void wxLogWindow::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
603{
604 // put the text into our window
605 wxTextCtrl *pText = m_pLogFrame->TextCtrl();
606
607 // remove selection (WriteText is in fact ReplaceSelection)
608#ifdef __WXMSW__
609 long nLen = pText->GetLastPosition();
610 pText->SetSelection(nLen, nLen);
611#endif // Windows
612
613 wxString msg;
614 TimeStamp(&msg);
615 msg << szString << wxT('\n');
616
617 pText->AppendText(msg);
618
619 // TODO ensure that the line can be seen
620}
621
622wxFrame *wxLogWindow::GetFrame() const
623{
624 return m_pLogFrame;
625}
626
627void wxLogWindow::OnFrameCreate(wxFrame * WXUNUSED(frame))
628{
629}
630
631bool wxLogWindow::OnFrameClose(wxFrame * WXUNUSED(frame))
632{
633 // allow to close
634 return TRUE;
635}
636
637void wxLogWindow::OnFrameDelete(wxFrame * WXUNUSED(frame))
638{
639 m_pLogFrame = (wxLogFrame *)NULL;
640}
641
642wxLogWindow::~wxLogWindow()
643{
644 // may be NULL if log frame already auto destroyed itself
645 delete m_pLogFrame;
646}
647
648// ----------------------------------------------------------------------------
649// wxLogDialog
650// ----------------------------------------------------------------------------
651
652#if wxUSE_LOG_DIALOG
653
654static const size_t MARGIN = 10;
655
656wxString wxLogDialog::ms_details;
657
658wxLogDialog::wxLogDialog(wxWindow *parent,
659 const wxArrayString& messages,
660 const wxArrayInt& severity,
661 const wxArrayLong& times,
662 const wxString& caption,
663 long style)
664 : wxDialog(parent, -1, caption)
665{
666 if ( ms_details.IsEmpty() )
667 {
668 // ensure that we won't loop here if wxGetTranslation()
669 // happens to pop up a Log message while translating this :-)
670 ms_details = wxTRANSLATE("&Details");
671 ms_details = wxGetTranslation(ms_details);
672 }
673
674 size_t count = messages.GetCount();
675 m_messages.Alloc(count);
676 m_severity.Alloc(count);
677 m_times.Alloc(count);
678
679 for ( size_t n = 0; n < count; n++ )
680 {
681 wxString msg = messages[n];
682 do
683 {
684 m_messages.Add(msg.BeforeFirst(_T('\n')));
685 msg = msg.AfterFirst(_T('\n'));
686
687 m_severity.Add(severity[n]);
688 m_times.Add(times[n]);
689 }
690 while ( !!msg );
691 }
692
693 m_showingDetails = FALSE; // not initially
694 m_listctrl = (wxListCtrl *)NULL;
695
696#if wxUSE_STATLINE
697 m_statline = (wxStaticLine *)NULL;
698#endif // wxUSE_STATLINE
699
700#if wxUSE_FILE
701 m_btnSave = (wxButton *)NULL;
702#endif // wxUSE_FILE
703
704 // create the controls which are always shown and layout them: we use
705 // sizers even though our window is not resizeable to calculate the size of
706 // the dialog properly
707 wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
708 wxBoxSizer *sizerButtons = new wxBoxSizer(wxVERTICAL);
709 wxBoxSizer *sizerAll = new wxBoxSizer(wxHORIZONTAL);
710
711 // this "Ok" button has wxID_CANCEL id - not very logical, but this allows
712 // to close the log dialog with <Esc> which wouldn't work otherwise (as it
713 // translates into click on cancel button)
714 wxButton *btnOk = new wxButton(this, wxID_CANCEL, _("OK"));
715 sizerButtons->Add(btnOk, 0, wxCENTRE|wxBOTTOM, MARGIN/2);
716 m_btnDetails = new wxButton(this, wxID_MORE, ms_details + _T(" >>"));
717 sizerButtons->Add(m_btnDetails, 0, wxCENTRE|wxTOP, MARGIN/2 - 1);
718
719#ifndef __WIN16__
720 wxIcon icon = wxTheApp->GetStdIcon((int)(style & wxICON_MASK));
721 sizerAll->Add(new wxStaticBitmap(this, -1, icon), 0, wxCENTRE);
722#endif // !Win16
723
724 const wxString& message = messages.Last();
725 sizerAll->Add(CreateTextSizer(message), 0, wxCENTRE|wxLEFT|wxRIGHT, MARGIN);
726 sizerAll->Add(sizerButtons, 0, wxALIGN_RIGHT|wxLEFT, MARGIN);
727
728 sizerTop->Add(sizerAll, 0, wxCENTRE|wxALL, MARGIN);
729
730 SetAutoLayout(TRUE);
731 SetSizer(sizerTop);
732
733 sizerTop->SetSizeHints(this);
734 sizerTop->Fit(this);
735
736 btnOk->SetFocus();
737
738 // this can't happen any more as we don't use this dialog in this case
739#if 0
740 if ( count == 1 )
741 {
742 // no details... it's easier to disable a button than to change the
743 // dialog layout depending on whether we have details or not
744 m_btnDetails->Disable();
745 }
746#endif // 0
747
748 Centre();
749}
750
751void wxLogDialog::CreateDetailsControls()
752{
753 // create the save button and separator line if possible
754#if wxUSE_FILE
755 m_btnSave = new wxButton(this, wxID_SAVE, _("&Save..."));
756#endif // wxUSE_FILE
757
758#if wxUSE_STATLINE
759 m_statline = new wxStaticLine(this, -1);
760#endif // wxUSE_STATLINE
761
762 // create the list ctrl now
763 m_listctrl = new wxListCtrl(this, -1,
764 wxDefaultPosition, wxDefaultSize,
765 wxSUNKEN_BORDER |
766 wxLC_REPORT |
767 wxLC_NO_HEADER |
768 wxLC_SINGLE_SEL);
769
770 // no need to translate these strings as they're not shown to the
771 // user anyhow (we use wxLC_NO_HEADER style)
772 m_listctrl->InsertColumn(0, _T("Message"));
773 m_listctrl->InsertColumn(1, _T("Time"));
774
775 // prepare the imagelist
776 static const int ICON_SIZE = 16;
777 wxImageList *imageList = new wxImageList(ICON_SIZE, ICON_SIZE);
778
779 // order should be the same as in the switch below!
780 static const int icons[] =
781 {
782 wxICON_ERROR,
783 wxICON_EXCLAMATION,
784 wxICON_INFORMATION
785 };
786
787 bool loadedIcons = TRUE;
788
789#ifndef __WIN16__
790 for ( size_t icon = 0; icon < WXSIZEOF(icons); icon++ )
791 {
792 wxBitmap bmp = wxTheApp->GetStdIcon(icons[icon]);
793
794 // This may very well fail if there are insufficient
795 // colours available. Degrade gracefully.
796
797 if (!bmp.Ok())
798 loadedIcons = FALSE;
799 else
800 imageList->Add(wxImage(bmp).
801 Rescale(ICON_SIZE, ICON_SIZE).
802 ConvertToBitmap());
803 }
804
805 m_listctrl->SetImageList(imageList, wxIMAGE_LIST_SMALL);
806#endif // !Win16
807
808 // and fill it
809 wxString fmt = wxLog::GetTimestamp();
810 if ( !fmt )
811 {
812 // default format
813 fmt = _T("%c");
814 }
815
816 size_t count = m_messages.GetCount();
817 for ( size_t n = 0; n < count; n++ )
818 {
819 int image = -1;
820#ifndef __WIN16__
821 switch ( m_severity[n] )
822 {
823 case wxLOG_Error:
824 image = 0;
825 break;
826
827 case wxLOG_Warning:
828 image = 1;
829 break;
830
831 default:
832 image = 2;
833 }
834#endif // !Win16
835
836 if (!loadedIcons)
837 image = -1;
838
839 if (image > -1)
840 m_listctrl->InsertItem(n, m_messages[n], image);
841 else
842 m_listctrl->InsertItem(n, m_messages[n]);
843
844 m_listctrl->SetItem(n, 1,
845 TimeStamp(fmt, (time_t)m_times[n]));
846 }
847
848 // let the columns size themselves
849 m_listctrl->SetColumnWidth(0, wxLIST_AUTOSIZE);
850 m_listctrl->SetColumnWidth(1, wxLIST_AUTOSIZE);
851
852 // get the approx height of the listctrl
853 wxFont font = GetFont();
854 if ( !font.Ok() )
855 font = *wxSWISS_FONT;
856
857 int y;
858 GetTextExtent(_T("H"), (int*)NULL, &y, (int*)NULL, (int*)NULL, &font);
859 int height = wxMax(y*(count + 3), 100);
860 m_listctrl->SetSize(-1, height);
861}
862
863void wxLogDialog::OnListSelect(wxListEvent& event)
864{
865 // we can't just disable the control because this looks ugly under Windows
866 // (wrong bg colour, no scrolling...), but we still want to disable
867 // selecting items - it makes no sense here
868 m_listctrl->SetItemState(event.GetIndex(), 0, wxLIST_STATE_SELECTED);
869}
870
871void wxLogDialog::OnOk(wxCommandEvent& WXUNUSED(event))
872{
873 EndModal(wxID_OK);
874}
875
876#if wxUSE_FILE
877
878void wxLogDialog::OnSave(wxCommandEvent& WXUNUSED(event))
879{
880#if wxUSE_FILEDLG
881 wxFile file;
882 int rc = OpenLogFile(file);
883 if ( rc == -1 )
884 {
885 // cancelled
886 return;
887 }
888
889 bool ok = rc != 0;
890
891 wxString fmt = wxLog::GetTimestamp();
892 if ( !fmt )
893 {
894 // default format
895 fmt = _T("%c");
896 }
897
898 size_t count = m_messages.GetCount();
899 for ( size_t n = 0; ok && (n < count); n++ )
900 {
901 wxString line;
902 line << TimeStamp(fmt, (time_t)m_times[n])
903 << _T(": ")
904 << m_messages[n]
905 << wxTextFile::GetEOL();
906
907 ok = file.Write(line);
908 }
909
910 if ( ok )
911 ok = file.Close();
912
913 if ( !ok )
914 wxLogError(_("Can't save log contents to file."));
915#endif
916}
917
918#endif // wxUSE_FILE
919
920void wxLogDialog::OnDetails(wxCommandEvent& WXUNUSED(event))
921{
922 wxSizer *sizer = GetSizer();
923
924 if ( m_showingDetails )
925 {
926 m_btnDetails->SetLabel(ms_details + _T(">>"));
927
928 sizer->Remove(m_listctrl);
929
930#if wxUSE_STATLINE
931 sizer->Remove(m_statline);
932#endif // wxUSE_STATLINE
933
934#if wxUSE_FILE
935 sizer->Remove(m_btnSave);
936#endif // wxUSE_FILE
937 }
938 else // show details now
939 {
940 m_btnDetails->SetLabel(wxString(_T("<< ")) + ms_details);
941
942 if ( !m_listctrl )
943 {
944 CreateDetailsControls();
945 }
946
947#if wxUSE_STATLINE
948 sizer->Add(m_statline, 0, wxEXPAND | (wxALL & ~wxTOP), MARGIN);
949#endif // wxUSE_STATLINE
950
951 sizer->Add(m_listctrl, 1, wxEXPAND | (wxALL & ~wxTOP), MARGIN);
952
953#if wxUSE_FILE
954 sizer->Add(m_btnSave, 0, wxALIGN_RIGHT | (wxALL & ~wxTOP), MARGIN);
955#endif // wxUSE_FILE
956 }
957
958 m_showingDetails = !m_showingDetails;
959
960 // in any case, our size changed - update
961 sizer->SetSizeHints(this);
962 sizer->Fit(this);
963
964#ifdef __WXGTK__
965 // VS: this is neccessary in order to force frame redraw under
966 // WindowMaker or fvwm2 (and probably other broken WMs).
967 // Otherwise, detailed list wouldn't be displayed.
968 Show(TRUE);
969#endif // wxGTK
970}
971
972wxLogDialog::~wxLogDialog()
973{
974 if ( m_listctrl )
975 {
976 delete m_listctrl->GetImageList(wxIMAGE_LIST_SMALL);
977 }
978}
979
980#endif // wxUSE_LOG_DIALOG
981
982#if wxUSE_FILE && wxUSE_FILEDLG
983
984// pass an uninitialized file object, the function will ask the user for the
985// filename and try to open it, returns TRUE on success (file was opened),
986// FALSE if file couldn't be opened/created and -1 if the file selection
987// dialog was cancelled
988static int OpenLogFile(wxFile& file, wxString *pFilename)
989{
990 // get the file name
991 // -----------------
992 wxString filename = wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"));
993 if ( !filename ) {
994 // cancelled
995 return -1;
996 }
997
998 // open file
999 // ---------
1000 bool bOk = FALSE;
1001 if ( wxFile::Exists(filename) ) {
1002 bool bAppend = FALSE;
1003 wxString strMsg;
1004 strMsg.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
1005 filename.c_str());
1006 switch ( wxMessageBox(strMsg, _("Question"),
1007 wxICON_QUESTION | wxYES_NO | wxCANCEL) ) {
1008 case wxYES:
1009 bAppend = TRUE;
1010 break;
1011
1012 case wxNO:
1013 bAppend = FALSE;
1014 break;
1015
1016 case wxCANCEL:
1017 return -1;
1018
1019 default:
1020 wxFAIL_MSG(_("invalid message box return value"));
1021 }
1022
1023 if ( bAppend ) {
1024 bOk = file.Open(filename, wxFile::write_append);
1025 }
1026 else {
1027 bOk = file.Create(filename, TRUE /* overwrite */);
1028 }
1029 }
1030 else {
1031 bOk = file.Create(filename);
1032 }
1033
1034 if ( pFilename )
1035 *pFilename = filename;
1036
1037 return bOk;
1038}
1039
1040#endif // wxUSE_FILE
1041
1042#endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
1043
1044#if wxUSE_TEXTCTRL
1045
1046// ----------------------------------------------------------------------------
1047// wxLogTextCtrl implementation
1048// ----------------------------------------------------------------------------
1049
1050wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl)
1051{
1052 m_pTextCtrl = pTextCtrl;
1053}
1054
1055void wxLogTextCtrl::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
1056{
1057 wxString msg;
1058 TimeStamp(&msg);
1059
1060#ifdef __WXMAC__
1061 // VZ: this is a bug in wxMac, it *must* accept '\n' as new line, the
1062 // translation must be done in wxTextCtrl, not here! (FIXME)
1063 msg << szString << wxT('\r');
1064#else
1065 msg << szString << wxT('\n');
1066#endif
1067
1068 m_pTextCtrl->AppendText(msg);
1069}
1070
1071#endif // wxUSE_TEXTCTRL
1072