]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/logg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/logg.cpp
3 // Purpose: wxLog-derived classes which need GUI support (the rest is in
5 // Author: Vadim Zeitlin
7 // Created: 20.09.99 (extracted from src/common/log.cpp)
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
30 #include "wx/button.h"
35 #include "wx/filedlg.h"
36 #include "wx/msgdlg.h"
37 #include "wx/textctrl.h"
39 #include "wx/statbmp.h"
40 #include "wx/settings.h"
41 #include "wx/wxcrtvararg.h"
44 #if wxUSE_LOGGUI || wxUSE_LOGWINDOW
47 #include "wx/textfile.h"
48 #include "wx/statline.h"
49 #include "wx/artprov.h"
50 #include "wx/collpane.h"
53 // for OutputDebugString()
54 #include "wx/msw/private.h"
62 #include "wx/listctrl.h"
63 #include "wx/imaglist.h"
65 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
67 #if defined(__MWERKS__) && wxUSE_UNICODE
71 #include "wx/datetime.h"
73 // the suffix we add to the button to show that the dialog can be expanded
74 #define EXPAND_SUFFIX _T(" >>")
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
82 // this function is a wrapper around strftime(3)
83 // allows to exclude the usage of wxDateTime
84 static wxString
TimeStamp(const wxString
& format
, time_t t
)
89 if ( !wxStrftime(buf
, WXSIZEOF(buf
), format
, wxLocaltime_r(&t
, &tm
)) )
91 // buffer is too small?
92 wxFAIL_MSG(_T("strftime() failed"));
95 #else // !wxUSE_DATETIME
97 #endif // wxUSE_DATETIME/!wxUSE_DATETIME
101 class wxLogDialog
: public wxDialog
104 wxLogDialog(wxWindow
*parent
,
105 const wxArrayString
& messages
,
106 const wxArrayInt
& severity
,
107 const wxArrayLong
& timess
,
108 const wxString
& caption
,
110 virtual ~wxLogDialog();
113 void OnOk(wxCommandEvent
& event
);
115 void OnSave(wxCommandEvent
& event
);
117 void OnListSelect(wxListEvent
& event
);
118 void OnListItemActivated(wxListEvent
& event
);
121 // create controls needed for the details display
122 void CreateDetailsControls(wxWindow
*);
124 // if necessary truncates the given string and adds an ellipsis
125 wxString
EllipsizeString(const wxString
&text
)
127 if (ms_maxLength
> 0 &&
128 text
.length() > ms_maxLength
)
131 ret
.Truncate(ms_maxLength
);
139 // the data for the listctrl
140 wxArrayString m_messages
;
141 wxArrayInt m_severity
;
144 // the controls which are not shown initially (but only when details
145 // button is pressed)
146 wxListCtrl
*m_listctrl
;
147 #ifndef __SMARTPHONE__
149 wxStaticLine
*m_statline
;
150 #endif // wxUSE_STATLINE
154 #endif // __SMARTPHONE__
156 // the translated "Details" string
157 static wxString ms_details
;
159 // the maximum length of the log message
160 static size_t ms_maxLength
;
162 DECLARE_EVENT_TABLE()
163 DECLARE_NO_COPY_CLASS(wxLogDialog
)
166 BEGIN_EVENT_TABLE(wxLogDialog
, wxDialog
)
167 EVT_BUTTON(wxID_OK
, wxLogDialog::OnOk
)
169 EVT_BUTTON(wxID_SAVE
, wxLogDialog::OnSave
)
171 EVT_LIST_ITEM_SELECTED(wxID_ANY
, wxLogDialog::OnListSelect
)
172 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxLogDialog::OnListItemActivated
)
175 #endif // wxUSE_LOG_DIALOG
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 #if wxUSE_FILE && wxUSE_FILEDLG
183 // pass an uninitialized file object, the function will ask the user for the
184 // filename and try to open it, returns true on success (file was opened),
185 // false if file couldn't be opened/created and -1 if the file selection
186 // dialog was cancelled
187 static int OpenLogFile(wxFile
& file
, wxString
*filename
= NULL
, wxWindow
*parent
= NULL
);
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 // we use a global variable to store the frame pointer for wxLogStatus - bad,
196 // but it's the easiest way
197 static wxFrame
*gs_pFrame
= NULL
; // FIXME MT-unsafe
199 // ============================================================================
201 // ============================================================================
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
207 // accepts an additional argument which tells to which frame the output should
209 void wxVLogStatus(wxFrame
*pFrame
, const wxString
& format
, va_list argptr
)
213 wxLog
*pLog
= wxLog::GetActiveTarget();
214 if ( pLog
!= NULL
) {
215 msg
.PrintfV(format
, argptr
);
217 wxASSERT( gs_pFrame
== NULL
); // should be reset!
220 wxLog::OnLog(wxLOG_Status
, msg
, 0);
222 wxLog::OnLog(wxLOG_Status
, msg
, time(NULL
));
224 gs_pFrame
= (wxFrame
*) NULL
;
228 #if !wxUSE_UTF8_LOCALE_ONLY
229 void wxDoLogStatusWchar(wxFrame
*pFrame
, const wxChar
*format
, ...)
232 va_start(argptr
, format
);
233 wxVLogStatus(pFrame
, format
, argptr
);
236 #endif // !wxUSE_UTF8_LOCALE_ONLY
238 #if wxUSE_UNICODE_UTF8
239 void wxDoLogStatusUtf8(wxFrame
*pFrame
, const char *format
, ...)
242 va_start(argptr
, format
);
243 wxVLogStatus(pFrame
, format
, argptr
);
246 #endif // wxUSE_UNICODE_UTF8
248 // ----------------------------------------------------------------------------
249 // wxLogGui implementation (FIXME MT-unsafe)
250 // ----------------------------------------------------------------------------
259 void wxLogGui::Clear()
263 m_bHasMessages
= false;
270 void wxLogGui::Flush()
272 if ( !m_bHasMessages
)
275 // do it right now to block any new calls to Flush() while we're here
276 m_bHasMessages
= false;
278 const unsigned repeatCount
= LogLastRepetitionCountIfNeeded();
280 wxString appName
= wxTheApp
->GetAppDisplayName();
283 wxString titleFormat
;
285 titleFormat
= _("%s Error");
288 else if ( m_bWarnings
) {
289 titleFormat
= _("%s Warning");
290 style
= wxICON_EXCLAMATION
;
293 titleFormat
= _("%s Information");
294 style
= wxICON_INFORMATION
;
298 title
.Printf(titleFormat
, appName
.c_str());
300 size_t nMsgCount
= m_aMessages
.GetCount();
302 // avoid showing other log dialogs until we're done with the dialog we're
303 // showing right now: nested modal dialogs make for really bad UI!
307 if ( nMsgCount
== 1 )
309 str
= m_aMessages
[0];
311 else // more than one message
315 if ( repeatCount
> 0 )
317 m_aMessages
[nMsgCount
- 1]
318 << " (" << m_aMessages
[nMsgCount
- 2] << ")";
321 wxLogDialog
dlg(NULL
,
322 m_aMessages
, m_aSeverity
, m_aTimes
,
325 // clear the message list before showing the dialog because while it's
326 // shown some new messages may appear
329 (void)dlg
.ShowModal();
330 #else // !wxUSE_LOG_DIALOG
331 // concatenate all strings (but not too many to not overfill the msg box)
334 // start from the most recent message
335 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
336 // for Windows strings longer than this value are wrapped (NT 4.0)
337 const size_t nMsgLineWidth
= 156;
339 nLines
+= (m_aMessages
[n
- 1].Len() + nMsgLineWidth
- 1) / nMsgLineWidth
;
341 if ( nLines
> 25 ) // don't put too many lines in message box
344 str
<< m_aMessages
[n
- 1] << wxT("\n");
346 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
349 // this catches both cases of 1 message with wxUSE_LOG_DIALOG and any
350 // situation without it
353 wxMessageBox(str
, title
, wxOK
| style
);
355 // no undisplayed messages whatsoever
359 // allow flushing the logs again
363 // log all kinds of messages
364 void wxLogGui::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
371 m_aMessages
.Add(szString
);
372 m_aSeverity
.Add(wxLOG_Message
);
373 m_aTimes
.Add((long)t
);
374 m_bHasMessages
= true;
381 // find the top window and set it's status text if it has any
382 wxFrame
*pFrame
= gs_pFrame
;
383 if ( pFrame
== NULL
) {
384 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
385 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
386 pFrame
= (wxFrame
*)pWin
;
390 if ( pFrame
&& pFrame
->GetStatusBar() )
391 pFrame
->SetStatusText(szString
);
393 #endif // wxUSE_STATUSBAR
404 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
405 // don't prepend debug/trace here: it goes to the
406 // debug window anyhow
408 OutputDebugString(str
.wx_str());
410 // send them to stderr
411 wxFprintf(stderr
, wxT("[%s] %s\n"),
412 level
== wxLOG_Trace
? wxT("Trace")
418 #endif // __WXDEBUG__
422 case wxLOG_FatalError
:
423 // show this one immediately
424 wxMessageBox(szString
, _("Fatal error"), wxICON_HAND
);
430 #if !wxUSE_LOG_DIALOG
431 // discard earlier informational messages if this is the 1st
432 // error because they might not make sense any more and showing
433 // them in a message box might be confusing
437 #endif // wxUSE_LOG_DIALOG
444 // for the warning we don't discard the info messages
448 m_aMessages
.Add(szString
);
449 m_aSeverity
.Add((int)level
);
450 m_aTimes
.Add((long)t
);
451 m_bHasMessages
= true;
456 #endif // wxUSE_LOGGUI
458 // ----------------------------------------------------------------------------
459 // wxLogWindow and wxLogFrame implementation
460 // ----------------------------------------------------------------------------
466 class wxLogFrame
: public wxFrame
470 wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
);
471 virtual ~wxLogFrame();
474 void OnClose(wxCommandEvent
& event
);
475 void OnCloseWindow(wxCloseEvent
& event
);
477 void OnSave (wxCommandEvent
& event
);
479 void OnClear(wxCommandEvent
& event
);
482 wxTextCtrl
*TextCtrl() const { return m_pTextCtrl
; }
485 // use standard ids for our commands!
488 Menu_Close
= wxID_CLOSE
,
489 Menu_Save
= wxID_SAVE
,
490 Menu_Clear
= wxID_CLEAR
493 // common part of OnClose() and OnCloseWindow()
496 wxTextCtrl
*m_pTextCtrl
;
499 DECLARE_EVENT_TABLE()
500 DECLARE_NO_COPY_CLASS(wxLogFrame
)
503 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
504 // wxLogWindow menu events
505 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
507 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
509 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
511 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
514 wxLogFrame::wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
)
515 : wxFrame(pParent
, wxID_ANY
, szTitle
)
519 m_pTextCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
523 // needed for Win32 to avoid 65Kb limit but it doesn't work well
524 // when using RichEdit 2.0 which we always do in the Unicode build
527 #endif // !wxUSE_UNICODE
532 wxMenuBar
*pMenuBar
= new wxMenuBar
;
533 wxMenu
*pMenu
= new wxMenu
;
535 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
537 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
538 pMenu
->AppendSeparator();
539 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
540 pMenuBar
->Append(pMenu
, _("&Log"));
541 SetMenuBar(pMenuBar
);
542 #endif // wxUSE_MENUS
545 // status bar for menu prompts
547 #endif // wxUSE_STATUSBAR
549 m_log
->OnFrameCreate(this);
552 void wxLogFrame::DoClose()
554 if ( m_log
->OnFrameClose(this) )
556 // instead of closing just hide the window to be able to Show() it
562 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
567 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
573 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
578 int rc
= OpenLogFile(file
, &filename
, this);
587 // retrieve text and save it
588 // -------------------------
589 int nLines
= m_pTextCtrl
->GetNumberOfLines();
590 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
591 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
592 wxTextFile::GetEOL());
599 wxLogError(_("Can't save log contents to file."));
602 wxLogStatus((wxFrame
*)this, _("Log saved to the file '%s'."), filename
.c_str());
608 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
610 m_pTextCtrl
->Clear();
613 wxLogFrame::~wxLogFrame()
615 m_log
->OnFrameDelete(this);
621 wxLogWindow::wxLogWindow(wxWindow
*pParent
,
622 const wxString
& szTitle
,
626 PassMessages(bDoPass
);
628 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
634 void wxLogWindow::Show(bool bShow
)
636 m_pLogFrame
->Show(bShow
);
639 void wxLogWindow::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
641 // first let the previous logger show it
642 wxLogPassThrough::DoLog(level
, szString
, t
);
647 // by default, these messages are ignored by wxLog, so process
649 if ( !szString
.empty() )
652 str
<< _("Status: ") << szString
;
657 // don't put trace messages in the text window for 2 reasons:
658 // 1) there are too many of them
659 // 2) they may provoke other trace messages thus sending a program
660 // into an infinite loop
665 // and this will format it nicely and call our DoLogString()
666 wxLog::DoLog(level
, szString
, t
);
671 void wxLogWindow::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
673 // put the text into our window
674 wxTextCtrl
*pText
= m_pLogFrame
->TextCtrl();
676 // remove selection (WriteText is in fact ReplaceSelection)
678 wxTextPos nLen
= pText
->GetLastPosition();
679 pText
->SetSelection(nLen
, nLen
);
684 msg
<< szString
<< wxT('\n');
686 pText
->AppendText(msg
);
688 // TODO ensure that the line can be seen
691 wxFrame
*wxLogWindow::GetFrame() const
696 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
700 bool wxLogWindow::OnFrameClose(wxFrame
* WXUNUSED(frame
))
706 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
708 m_pLogFrame
= (wxLogFrame
*)NULL
;
711 wxLogWindow::~wxLogWindow()
713 // may be NULL if log frame already auto destroyed itself
717 #endif // wxUSE_LOGWINDOW
719 // ----------------------------------------------------------------------------
721 // ----------------------------------------------------------------------------
725 #ifndef __SMARTPHONE__
726 static const size_t MARGIN
= 10;
728 static const size_t MARGIN
= 0;
731 wxString
wxLogDialog::ms_details
;
732 size_t wxLogDialog::ms_maxLength
= 0;
734 wxLogDialog::wxLogDialog(wxWindow
*parent
,
735 const wxArrayString
& messages
,
736 const wxArrayInt
& severity
,
737 const wxArrayLong
& times
,
738 const wxString
& caption
,
740 : wxDialog(parent
, wxID_ANY
, caption
,
741 wxDefaultPosition
, wxDefaultSize
,
742 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
744 // init the static variables:
746 if ( ms_details
.empty() )
748 // ensure that we won't loop here if wxGetTranslation()
749 // happens to pop up a Log message while translating this :-)
750 ms_details
= wxTRANSLATE("&Details");
751 ms_details
= wxGetTranslation(ms_details
);
752 #ifdef __SMARTPHONE__
753 ms_details
= wxStripMenuCodes(ms_details
);
757 if ( ms_maxLength
== 0 )
759 ms_maxLength
= (2 * wxGetDisplaySize().x
/3) / GetCharWidth();
762 size_t count
= messages
.GetCount();
763 m_messages
.Alloc(count
);
764 m_severity
.Alloc(count
);
765 m_times
.Alloc(count
);
767 for ( size_t n
= 0; n
< count
; n
++ )
769 m_messages
.Add(messages
[n
]);
770 m_severity
.Add(severity
[n
]);
771 m_times
.Add(times
[n
]);
774 m_listctrl
= (wxListCtrl
*)NULL
;
776 #ifndef __SMARTPHONE__
779 m_statline
= (wxStaticLine
*)NULL
;
780 #endif // wxUSE_STATLINE
783 m_btnSave
= (wxButton
*)NULL
;
786 #endif // __SMARTPHONE__
788 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
790 // create the controls which are always shown and layout them: we use
791 // sizers even though our window is not resizeable to calculate the size of
792 // the dialog properly
793 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
794 wxBoxSizer
*sizerAll
= new wxBoxSizer(isPda
? wxVERTICAL
: wxHORIZONTAL
);
797 switch ( style
& wxICON_MASK
)
800 bitmap
= wxArtProvider::GetBitmap(wxART_ERROR
, wxART_MESSAGE_BOX
);
802 bitmap
.SetId(wxICON_SMALL_ERROR
);
806 case wxICON_INFORMATION
:
807 bitmap
= wxArtProvider::GetBitmap(wxART_INFORMATION
, wxART_MESSAGE_BOX
);
809 bitmap
.SetId(wxICON_SMALL_INFO
);
814 bitmap
= wxArtProvider::GetBitmap(wxART_WARNING
, wxART_MESSAGE_BOX
);
816 bitmap
.SetId(wxICON_SMALL_WARNING
);
821 wxFAIL_MSG(_T("incorrect log style"));
825 sizerAll
->Add(new wxStaticBitmap(this, wxID_ANY
, bitmap
), 0,
826 wxALIGN_CENTRE_VERTICAL
);
828 // create the text sizer with a minimal size so that we are sure it won't be too small
829 wxString message
= EllipsizeString(messages
.Last());
830 wxSizer
*szText
= CreateTextSizer(message
);
831 szText
->SetMinSize(wxMin(300, wxGetDisplaySize().x
/ 3), -1);
833 sizerAll
->Add(szText
, 1,
834 wxALIGN_CENTRE_VERTICAL
| wxLEFT
| wxRIGHT
, MARGIN
);
836 wxButton
*btnOk
= new wxButton(this, wxID_OK
);
837 sizerAll
->Add(btnOk
, 0, isPda
? wxCENTRE
: wxCENTRE
|wxBOTTOM
, MARGIN
/2);
839 sizerTop
->Add(sizerAll
, 0, wxALL
| wxEXPAND
, MARGIN
);
842 // add the details pane
844 #ifndef __SMARTPHONE__
845 wxCollapsiblePane
*collpane
= new wxCollapsiblePane(this, wxID_ANY
, ms_details
);
846 sizerTop
->Add(collpane
, 1, wxGROW
|wxALL
, MARGIN
);
848 wxWindow
*win
= collpane
->GetPane();
849 wxSizer
*paneSz
= new wxBoxSizer(wxVERTICAL
);
851 CreateDetailsControls(win
);
853 paneSz
->Add(m_listctrl
, 1, wxEXPAND
| wxTOP
, MARGIN
);
855 #if wxUSE_FILE && !defined(__SMARTPHONE__)
856 paneSz
->Add(m_btnSave
, 0, wxALIGN_RIGHT
| wxTOP
, MARGIN
);
859 win
->SetSizer(paneSz
);
860 paneSz
->SetSizeHints(win
);
861 #else // __SMARTPHONE__
862 SetLeftMenu(wxID_OK
);
863 SetRightMenu(wxID_MORE
, ms_details
+ EXPAND_SUFFIX
);
864 #endif // __SMARTPHONE__/!__SMARTPHONE__
866 SetSizerAndFit(sizerTop
);
872 // Move up the screen so that when we expand the dialog,
873 // there's enough space.
874 Move(wxPoint(GetPosition().x
, GetPosition().y
/ 2));
878 void wxLogDialog::CreateDetailsControls(wxWindow
*parent
)
880 #ifndef __SMARTPHONE__
881 // create the save button and separator line if possible
883 m_btnSave
= new wxButton(parent
, wxID_SAVE
);
886 #endif // __SMARTPHONE__
888 // create the list ctrl now
889 m_listctrl
= new wxListCtrl(parent
, wxID_ANY
,
890 wxDefaultPosition
, wxDefaultSize
,
896 // This makes a big aesthetic difference on WinCE but I
897 // don't want to risk problems on other platforms
901 // no need to translate these strings as they're not shown to the
902 // user anyhow (we use wxLC_NO_HEADER style)
903 m_listctrl
->InsertColumn(0, _T("Message"));
904 m_listctrl
->InsertColumn(1, _T("Time"));
906 // prepare the imagelist
907 static const int ICON_SIZE
= 16;
908 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
910 // order should be the same as in the switch below!
911 static const wxChar
* icons
[] =
918 bool loadedIcons
= true;
920 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
922 wxBitmap bmp
= wxArtProvider::GetBitmap(icons
[icon
], wxART_MESSAGE_BOX
,
923 wxSize(ICON_SIZE
, ICON_SIZE
));
925 // This may very well fail if there are insufficient colours available.
926 // Degrade gracefully.
937 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
940 wxString fmt
= wxLog::GetTimestamp();
947 size_t count
= m_messages
.GetCount();
948 for ( size_t n
= 0; n
< count
; n
++ )
954 switch ( m_severity
[n
] )
968 else // failed to load images
973 wxString msg
= m_messages
[n
];
974 msg
.Replace(wxT("\n"), wxT(" "));
975 msg
= EllipsizeString(msg
);
977 m_listctrl
->InsertItem(n
, msg
, image
);
978 m_listctrl
->SetItem(n
, 1, TimeStamp(fmt
, (time_t)m_times
[n
]));
981 // let the columns size themselves
982 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
983 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
985 // calculate an approximately nice height for the listctrl
986 int height
= GetCharHeight()*(count
+ 4);
988 // but check that the dialog won't fall fown from the screen
990 // we use GetMinHeight() to get the height of the dialog part without the
991 // details and we consider that the "Save" button below and the separator
992 // line (and the margins around it) take about as much, hence double it
993 int heightMax
= wxGetDisplaySize().y
- GetPosition().y
- 2*GetMinHeight();
995 // we should leave a margin
999 m_listctrl
->SetSize(wxDefaultCoord
, wxMin(height
, heightMax
));
1002 void wxLogDialog::OnListSelect(wxListEvent
& event
)
1004 // we can't just disable the control because this looks ugly under Windows
1005 // (wrong bg colour, no scrolling...), but we still want to disable
1006 // selecting items - it makes no sense here
1007 m_listctrl
->SetItemState(event
.GetIndex(), 0, wxLIST_STATE_SELECTED
);
1010 void wxLogDialog::OnListItemActivated(wxListEvent
& event
)
1012 // show the activated item in a message box
1013 // This allow the user to correctly display the logs which are longer
1014 // than the listctrl and thus gets truncated or those which contains
1018 // wxString str = m_listctrl->GetItemText(event.GetIndex());
1019 // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW.
1020 wxString str
= m_messages
[event
.GetIndex()];
1022 // wxMessageBox will nicely handle the '\n' in the string (if any)
1023 // and supports long strings
1024 wxMessageBox(str
, wxT("Log message"), wxOK
, this);
1027 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
1034 void wxLogDialog::OnSave(wxCommandEvent
& WXUNUSED(event
))
1038 int rc
= OpenLogFile(file
, NULL
, this);
1047 wxString fmt
= wxLog::GetTimestamp();
1054 size_t count
= m_messages
.GetCount();
1055 for ( size_t n
= 0; ok
&& (n
< count
); n
++ )
1058 line
<< TimeStamp(fmt
, (time_t)m_times
[n
])
1061 << wxTextFile::GetEOL();
1063 ok
= file
.Write(line
);
1070 wxLogError(_("Can't save log contents to file."));
1071 #endif // wxUSE_FILEDLG
1074 #endif // wxUSE_FILE
1076 wxLogDialog::~wxLogDialog()
1080 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
1084 #endif // wxUSE_LOG_DIALOG
1086 #if wxUSE_FILE && wxUSE_FILEDLG
1088 // pass an uninitialized file object, the function will ask the user for the
1089 // filename and try to open it, returns true on success (file was opened),
1090 // false if file couldn't be opened/created and -1 if the file selection
1091 // dialog was cancelled
1092 static int OpenLogFile(wxFile
& file
, wxString
*pFilename
, wxWindow
*parent
)
1094 // get the file name
1095 // -----------------
1096 wxString filename
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent
);
1104 bool bOk
wxDUMMY_INITIALIZE(false);
1105 if ( wxFile::Exists(filename
) ) {
1106 bool bAppend
= false;
1108 strMsg
.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
1110 switch ( wxMessageBox(strMsg
, _("Question"),
1111 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
) ) {
1124 wxFAIL_MSG(_("invalid message box return value"));
1128 bOk
= file
.Open(filename
, wxFile::write_append
);
1131 bOk
= file
.Create(filename
, true /* overwrite */);
1135 bOk
= file
.Create(filename
);
1139 *pFilename
= filename
;
1144 #endif // wxUSE_FILE
1146 #endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
1148 #if wxUSE_LOG && wxUSE_TEXTCTRL
1150 // ----------------------------------------------------------------------------
1151 // wxLogTextCtrl implementation
1152 // ----------------------------------------------------------------------------
1154 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
1156 m_pTextCtrl
= pTextCtrl
;
1159 void wxLogTextCtrl::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
1164 msg
<< szString
<< wxT('\n');
1165 m_pTextCtrl
->AppendText(msg
);
1168 #endif // wxUSE_LOG && wxUSE_TEXTCTRL