]>
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 unsigned repeatCount
= 0;
279 if ( wxLog::GetRepetitionCounting() )
281 repeatCount
= wxLog::DoLogNumberOfRepeats();
284 wxString appName
= wxTheApp
->GetAppName();
285 if ( !appName
.empty() )
286 appName
[0u] = (wxChar
)wxToupper(appName
[0u]);
289 wxString titleFormat
;
291 titleFormat
= _("%s Error");
294 else if ( m_bWarnings
) {
295 titleFormat
= _("%s Warning");
296 style
= wxICON_EXCLAMATION
;
299 titleFormat
= _("%s Information");
300 style
= wxICON_INFORMATION
;
304 title
.Printf(titleFormat
, appName
.c_str());
306 size_t nMsgCount
= m_aMessages
.GetCount();
308 // avoid showing other log dialogs until we're done with the dialog we're
309 // showing right now: nested modal dialogs make for really bad UI!
313 if ( nMsgCount
== 1 )
315 str
= m_aMessages
[0];
317 else // more than one message
321 if ( repeatCount
> 0 )
322 m_aMessages
[nMsgCount
-1] += wxString::Format(wxT(" (%s)"), m_aMessages
[nMsgCount
-2].c_str());
323 wxLogDialog
dlg(NULL
,
324 m_aMessages
, m_aSeverity
, m_aTimes
,
327 // clear the message list before showing the dialog because while it's
328 // shown some new messages may appear
331 (void)dlg
.ShowModal();
332 #else // !wxUSE_LOG_DIALOG
333 // concatenate all strings (but not too many to not overfill the msg box)
336 // start from the most recent message
337 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
338 // for Windows strings longer than this value are wrapped (NT 4.0)
339 const size_t nMsgLineWidth
= 156;
341 nLines
+= (m_aMessages
[n
- 1].Len() + nMsgLineWidth
- 1) / nMsgLineWidth
;
343 if ( nLines
> 25 ) // don't put too many lines in message box
346 str
<< m_aMessages
[n
- 1] << wxT("\n");
348 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
351 // this catches both cases of 1 message with wxUSE_LOG_DIALOG and any
352 // situation without it
355 wxMessageBox(str
, title
, wxOK
| style
);
357 // no undisplayed messages whatsoever
361 // allow flushing the logs again
365 // log all kinds of messages
366 void wxLogGui::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
373 m_aMessages
.Add(szString
);
374 m_aSeverity
.Add(wxLOG_Message
);
375 m_aTimes
.Add((long)t
);
376 m_bHasMessages
= true;
383 // find the top window and set it's status text if it has any
384 wxFrame
*pFrame
= gs_pFrame
;
385 if ( pFrame
== NULL
) {
386 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
387 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
388 pFrame
= (wxFrame
*)pWin
;
392 if ( pFrame
&& pFrame
->GetStatusBar() )
393 pFrame
->SetStatusText(szString
);
395 #endif // wxUSE_STATUSBAR
406 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
407 // don't prepend debug/trace here: it goes to the
408 // debug window anyhow
410 OutputDebugString(str
.wx_str());
412 // send them to stderr
413 wxFprintf(stderr
, wxT("[%s] %s\n"),
414 level
== wxLOG_Trace
? wxT("Trace")
420 #endif // __WXDEBUG__
424 case wxLOG_FatalError
:
425 // show this one immediately
426 wxMessageBox(szString
, _("Fatal error"), wxICON_HAND
);
432 #if !wxUSE_LOG_DIALOG
433 // discard earlier informational messages if this is the 1st
434 // error because they might not make sense any more and showing
435 // them in a message box might be confusing
439 #endif // wxUSE_LOG_DIALOG
446 // for the warning we don't discard the info messages
450 m_aMessages
.Add(szString
);
451 m_aSeverity
.Add((int)level
);
452 m_aTimes
.Add((long)t
);
453 m_bHasMessages
= true;
458 #endif // wxUSE_LOGGUI
460 // ----------------------------------------------------------------------------
461 // wxLogWindow and wxLogFrame implementation
462 // ----------------------------------------------------------------------------
468 class wxLogFrame
: public wxFrame
472 wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
);
473 virtual ~wxLogFrame();
476 void OnClose(wxCommandEvent
& event
);
477 void OnCloseWindow(wxCloseEvent
& event
);
479 void OnSave (wxCommandEvent
& event
);
481 void OnClear(wxCommandEvent
& event
);
484 wxTextCtrl
*TextCtrl() const { return m_pTextCtrl
; }
487 // use standard ids for our commands!
490 Menu_Close
= wxID_CLOSE
,
491 Menu_Save
= wxID_SAVE
,
492 Menu_Clear
= wxID_CLEAR
495 // common part of OnClose() and OnCloseWindow()
498 wxTextCtrl
*m_pTextCtrl
;
501 DECLARE_EVENT_TABLE()
502 DECLARE_NO_COPY_CLASS(wxLogFrame
)
505 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
506 // wxLogWindow menu events
507 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
509 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
511 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
513 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
516 wxLogFrame::wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxString
& szTitle
)
517 : wxFrame(pParent
, wxID_ANY
, szTitle
)
521 m_pTextCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
525 // needed for Win32 to avoid 65Kb limit but it doesn't work well
526 // when using RichEdit 2.0 which we always do in the Unicode build
529 #endif // !wxUSE_UNICODE
534 wxMenuBar
*pMenuBar
= new wxMenuBar
;
535 wxMenu
*pMenu
= new wxMenu
;
537 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
539 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
540 pMenu
->AppendSeparator();
541 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
542 pMenuBar
->Append(pMenu
, _("&Log"));
543 SetMenuBar(pMenuBar
);
544 #endif // wxUSE_MENUS
547 // status bar for menu prompts
549 #endif // wxUSE_STATUSBAR
551 m_log
->OnFrameCreate(this);
554 void wxLogFrame::DoClose()
556 if ( m_log
->OnFrameClose(this) )
558 // instead of closing just hide the window to be able to Show() it
564 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
569 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
575 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
580 int rc
= OpenLogFile(file
, &filename
, this);
589 // retrieve text and save it
590 // -------------------------
591 int nLines
= m_pTextCtrl
->GetNumberOfLines();
592 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
593 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
594 wxTextFile::GetEOL());
601 wxLogError(_("Can't save log contents to file."));
604 wxLogStatus((wxFrame
*)this, _("Log saved to the file '%s'."), filename
.c_str());
610 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
612 m_pTextCtrl
->Clear();
615 wxLogFrame::~wxLogFrame()
617 m_log
->OnFrameDelete(this);
623 wxLogWindow::wxLogWindow(wxWindow
*pParent
,
624 const wxString
& szTitle
,
628 PassMessages(bDoPass
);
630 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
636 void wxLogWindow::Show(bool bShow
)
638 m_pLogFrame
->Show(bShow
);
641 void wxLogWindow::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
643 // first let the previous logger show it
644 wxLogPassThrough::DoLog(level
, szString
, t
);
649 // by default, these messages are ignored by wxLog, so process
651 if ( !szString
.empty() )
654 str
<< _("Status: ") << szString
;
659 // don't put trace messages in the text window for 2 reasons:
660 // 1) there are too many of them
661 // 2) they may provoke other trace messages thus sending a program
662 // into an infinite loop
667 // and this will format it nicely and call our DoLogString()
668 wxLog::DoLog(level
, szString
, t
);
673 void wxLogWindow::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
675 // put the text into our window
676 wxTextCtrl
*pText
= m_pLogFrame
->TextCtrl();
678 // remove selection (WriteText is in fact ReplaceSelection)
680 wxTextPos nLen
= pText
->GetLastPosition();
681 pText
->SetSelection(nLen
, nLen
);
686 msg
<< szString
<< wxT('\n');
688 pText
->AppendText(msg
);
690 // TODO ensure that the line can be seen
693 wxFrame
*wxLogWindow::GetFrame() const
698 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
702 bool wxLogWindow::OnFrameClose(wxFrame
* WXUNUSED(frame
))
708 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
710 m_pLogFrame
= (wxLogFrame
*)NULL
;
713 wxLogWindow::~wxLogWindow()
715 // may be NULL if log frame already auto destroyed itself
719 #endif // wxUSE_LOGWINDOW
721 // ----------------------------------------------------------------------------
723 // ----------------------------------------------------------------------------
727 #ifndef __SMARTPHONE__
728 static const size_t MARGIN
= 10;
730 static const size_t MARGIN
= 0;
733 wxString
wxLogDialog::ms_details
;
734 size_t wxLogDialog::ms_maxLength
= 0;
736 wxLogDialog::wxLogDialog(wxWindow
*parent
,
737 const wxArrayString
& messages
,
738 const wxArrayInt
& severity
,
739 const wxArrayLong
& times
,
740 const wxString
& caption
,
742 : wxDialog(parent
, wxID_ANY
, caption
,
743 wxDefaultPosition
, wxDefaultSize
,
744 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
746 // init the static variables:
748 if ( ms_details
.empty() )
750 // ensure that we won't loop here if wxGetTranslation()
751 // happens to pop up a Log message while translating this :-)
752 ms_details
= wxTRANSLATE("&Details");
753 ms_details
= wxGetTranslation(ms_details
);
754 #ifdef __SMARTPHONE__
755 ms_details
= wxStripMenuCodes(ms_details
);
759 if ( ms_maxLength
== 0 )
761 ms_maxLength
= (2 * wxGetDisplaySize().x
/3) / GetCharWidth();
764 size_t count
= messages
.GetCount();
765 m_messages
.Alloc(count
);
766 m_severity
.Alloc(count
);
767 m_times
.Alloc(count
);
769 for ( size_t n
= 0; n
< count
; n
++ )
771 m_messages
.Add(messages
[n
]);
772 m_severity
.Add(severity
[n
]);
773 m_times
.Add(times
[n
]);
776 m_listctrl
= (wxListCtrl
*)NULL
;
778 #ifndef __SMARTPHONE__
781 m_statline
= (wxStaticLine
*)NULL
;
782 #endif // wxUSE_STATLINE
785 m_btnSave
= (wxButton
*)NULL
;
788 #endif // __SMARTPHONE__
790 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
792 // create the controls which are always shown and layout them: we use
793 // sizers even though our window is not resizeable to calculate the size of
794 // the dialog properly
795 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
796 wxBoxSizer
*sizerAll
= new wxBoxSizer(isPda
? wxVERTICAL
: wxHORIZONTAL
);
799 switch ( style
& wxICON_MASK
)
802 bitmap
= wxArtProvider::GetBitmap(wxART_ERROR
, wxART_MESSAGE_BOX
);
804 bitmap
.SetId(wxICON_SMALL_ERROR
);
808 case wxICON_INFORMATION
:
809 bitmap
= wxArtProvider::GetBitmap(wxART_INFORMATION
, wxART_MESSAGE_BOX
);
811 bitmap
.SetId(wxICON_SMALL_INFO
);
816 bitmap
= wxArtProvider::GetBitmap(wxART_WARNING
, wxART_MESSAGE_BOX
);
818 bitmap
.SetId(wxICON_SMALL_WARNING
);
823 wxFAIL_MSG(_T("incorrect log style"));
827 sizerAll
->Add(new wxStaticBitmap(this, wxID_ANY
, bitmap
), 0,
828 wxALIGN_CENTRE_VERTICAL
);
830 // create the text sizer with a minimal size so that we are sure it won't be too small
831 wxString message
= EllipsizeString(messages
.Last());
832 wxSizer
*szText
= CreateTextSizer(message
);
833 szText
->SetMinSize(wxMin(300, wxGetDisplaySize().x
/ 3), -1);
835 sizerAll
->Add(szText
, 1,
836 wxALIGN_CENTRE_VERTICAL
| wxLEFT
| wxRIGHT
, MARGIN
);
838 wxButton
*btnOk
= new wxButton(this, wxID_OK
);
839 sizerAll
->Add(btnOk
, 0, isPda
? wxCENTRE
: wxCENTRE
|wxBOTTOM
, MARGIN
/2);
841 sizerTop
->Add(sizerAll
, 0, wxALL
| wxEXPAND
, MARGIN
);
844 // add the details pane
846 #ifndef __SMARTPHONE__
847 wxCollapsiblePane
*collpane
= new wxCollapsiblePane(this, wxID_ANY
, ms_details
);
848 sizerTop
->Add(collpane
, 1, wxGROW
|wxALL
, MARGIN
);
850 wxWindow
*win
= collpane
->GetPane();
851 wxSizer
*paneSz
= new wxBoxSizer(wxVERTICAL
);
853 CreateDetailsControls(win
);
855 paneSz
->Add(m_listctrl
, 1, wxEXPAND
| wxTOP
, MARGIN
);
857 #if wxUSE_FILE && !defined(__SMARTPHONE__)
858 paneSz
->Add(m_btnSave
, 0, wxALIGN_RIGHT
| wxTOP
, MARGIN
);
861 win
->SetSizer(paneSz
);
862 paneSz
->SetSizeHints(win
);
863 #else // __SMARTPHONE__
864 SetLeftMenu(wxID_OK
);
865 SetRightMenu(wxID_MORE
, ms_details
+ EXPAND_SUFFIX
);
866 #endif // __SMARTPHONE__/!__SMARTPHONE__
868 SetSizerAndFit(sizerTop
);
874 // Move up the screen so that when we expand the dialog,
875 // there's enough space.
876 Move(wxPoint(GetPosition().x
, GetPosition().y
/ 2));
880 void wxLogDialog::CreateDetailsControls(wxWindow
*parent
)
882 #ifndef __SMARTPHONE__
883 // create the save button and separator line if possible
885 m_btnSave
= new wxButton(parent
, wxID_SAVE
);
888 #endif // __SMARTPHONE__
890 // create the list ctrl now
891 m_listctrl
= new wxListCtrl(parent
, wxID_ANY
,
892 wxDefaultPosition
, wxDefaultSize
,
898 // This makes a big aesthetic difference on WinCE but I
899 // don't want to risk problems on other platforms
903 // no need to translate these strings as they're not shown to the
904 // user anyhow (we use wxLC_NO_HEADER style)
905 m_listctrl
->InsertColumn(0, _T("Message"));
906 m_listctrl
->InsertColumn(1, _T("Time"));
908 // prepare the imagelist
909 static const int ICON_SIZE
= 16;
910 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
912 // order should be the same as in the switch below!
913 static const wxChar
* icons
[] =
920 bool loadedIcons
= true;
922 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
924 wxBitmap bmp
= wxArtProvider::GetBitmap(icons
[icon
], wxART_MESSAGE_BOX
,
925 wxSize(ICON_SIZE
, ICON_SIZE
));
927 // This may very well fail if there are insufficient colours available.
928 // Degrade gracefully.
939 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
942 wxString fmt
= wxLog::GetTimestamp();
949 size_t count
= m_messages
.GetCount();
950 for ( size_t n
= 0; n
< count
; n
++ )
956 switch ( m_severity
[n
] )
970 else // failed to load images
975 wxString msg
= m_messages
[n
];
976 msg
.Replace(wxT("\n"), wxT(" "));
977 msg
= EllipsizeString(msg
);
979 m_listctrl
->InsertItem(n
, msg
, image
);
980 m_listctrl
->SetItem(n
, 1, TimeStamp(fmt
, (time_t)m_times
[n
]));
983 // let the columns size themselves
984 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
985 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
987 // calculate an approximately nice height for the listctrl
988 int height
= GetCharHeight()*(count
+ 4);
990 // but check that the dialog won't fall fown from the screen
992 // we use GetMinHeight() to get the height of the dialog part without the
993 // details and we consider that the "Save" button below and the separator
994 // line (and the margins around it) take about as much, hence double it
995 int heightMax
= wxGetDisplaySize().y
- GetPosition().y
- 2*GetMinHeight();
997 // we should leave a margin
1001 m_listctrl
->SetSize(wxDefaultCoord
, wxMin(height
, heightMax
));
1004 void wxLogDialog::OnListSelect(wxListEvent
& event
)
1006 // we can't just disable the control because this looks ugly under Windows
1007 // (wrong bg colour, no scrolling...), but we still want to disable
1008 // selecting items - it makes no sense here
1009 m_listctrl
->SetItemState(event
.GetIndex(), 0, wxLIST_STATE_SELECTED
);
1012 void wxLogDialog::OnListItemActivated(wxListEvent
& event
)
1014 // show the activated item in a message box
1015 // This allow the user to correctly display the logs which are longer
1016 // than the listctrl and thus gets truncated or those which contains
1020 // wxString str = m_listctrl->GetItemText(event.GetIndex());
1021 // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW.
1022 wxString str
= m_messages
[event
.GetIndex()];
1024 // wxMessageBox will nicely handle the '\n' in the string (if any)
1025 // and supports long strings
1026 wxMessageBox(str
, wxT("Log message"), wxOK
, this);
1029 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
1036 void wxLogDialog::OnSave(wxCommandEvent
& WXUNUSED(event
))
1040 int rc
= OpenLogFile(file
, NULL
, this);
1049 wxString fmt
= wxLog::GetTimestamp();
1056 size_t count
= m_messages
.GetCount();
1057 for ( size_t n
= 0; ok
&& (n
< count
); n
++ )
1060 line
<< TimeStamp(fmt
, (time_t)m_times
[n
])
1063 << wxTextFile::GetEOL();
1065 ok
= file
.Write(line
);
1072 wxLogError(_("Can't save log contents to file."));
1073 #endif // wxUSE_FILEDLG
1076 #endif // wxUSE_FILE
1078 wxLogDialog::~wxLogDialog()
1082 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
1086 #endif // wxUSE_LOG_DIALOG
1088 #if wxUSE_FILE && wxUSE_FILEDLG
1090 // pass an uninitialized file object, the function will ask the user for the
1091 // filename and try to open it, returns true on success (file was opened),
1092 // false if file couldn't be opened/created and -1 if the file selection
1093 // dialog was cancelled
1094 static int OpenLogFile(wxFile
& file
, wxString
*pFilename
, wxWindow
*parent
)
1096 // get the file name
1097 // -----------------
1098 wxString filename
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent
);
1106 bool bOk
wxDUMMY_INITIALIZE(false);
1107 if ( wxFile::Exists(filename
) ) {
1108 bool bAppend
= false;
1110 strMsg
.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
1112 switch ( wxMessageBox(strMsg
, _("Question"),
1113 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
) ) {
1126 wxFAIL_MSG(_("invalid message box return value"));
1130 bOk
= file
.Open(filename
, wxFile::write_append
);
1133 bOk
= file
.Create(filename
, true /* overwrite */);
1137 bOk
= file
.Create(filename
);
1141 *pFilename
= filename
;
1146 #endif // wxUSE_FILE
1148 #endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
1150 #if wxUSE_LOG && wxUSE_TEXTCTRL
1152 // ----------------------------------------------------------------------------
1153 // wxLogTextCtrl implementation
1154 // ----------------------------------------------------------------------------
1156 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
1158 m_pTextCtrl
= pTextCtrl
;
1161 void wxLogTextCtrl::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
1166 msg
<< szString
<< wxT('\n');
1167 m_pTextCtrl
->AppendText(msg
);
1170 #endif // wxUSE_LOG && wxUSE_TEXTCTRL