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