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