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