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