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