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