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