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