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