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