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