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