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