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