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