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