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