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