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