]>
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"
43 #if wxUSE_LOGGUI || wxUSE_LOGWINDOW
46 #include "wx/textfile.h"
47 #include "wx/statline.h"
48 #include "wx/artprov.h"
49 #include "wx/collpane.h"
52 // for OutputDebugString()
53 #include "wx/msw/private.h"
61 #include "wx/listctrl.h"
62 #include "wx/imaglist.h"
64 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
66 #if defined(__MWERKS__) && wxUSE_UNICODE
70 #include "wx/datetime.h"
72 // the suffix we add to the button to show that the dialog can be expanded
73 #define EXPAND_SUFFIX _T(" >>")
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
81 // this function is a wrapper around strftime(3)
82 // allows to exclude the usage of wxDateTime
83 static wxString
TimeStamp(const wxChar
*format
, time_t t
)
88 if ( !wxStrftime(buf
, WXSIZEOF(buf
), format
, wxLocaltime_r(&t
, &tm
)) )
90 // buffer is too small?
91 wxFAIL_MSG(_T("strftime() failed"));
94 #else // !wxUSE_DATETIME
96 #endif // wxUSE_DATETIME/!wxUSE_DATETIME
100 class wxLogDialog
: public wxDialog
103 wxLogDialog(wxWindow
*parent
,
104 const wxArrayString
& messages
,
105 const wxArrayInt
& severity
,
106 const wxArrayLong
& timess
,
107 const wxString
& caption
,
109 virtual ~wxLogDialog();
112 void OnOk(wxCommandEvent
& event
);
114 void OnSave(wxCommandEvent
& event
);
116 void OnListSelect(wxListEvent
& event
);
117 void OnListItemActivated(wxListEvent
& event
);
120 // create controls needed for the details display
121 void CreateDetailsControls(wxWindow
*);
123 // if necessary truncates the given string and adds an ellipsis
124 wxString
EllipsizeString(const wxString
&text
)
126 if (ms_maxLength
> 0 &&
127 text
.length() > ms_maxLength
)
130 ret
.Truncate(ms_maxLength
);
138 // the data for the listctrl
139 wxArrayString m_messages
;
140 wxArrayInt m_severity
;
143 // the controls which are not shown initially (but only when details
144 // button is pressed)
145 wxListCtrl
*m_listctrl
;
146 #ifndef __SMARTPHONE__
148 wxStaticLine
*m_statline
;
149 #endif // wxUSE_STATLINE
153 #endif // __SMARTPHONE__
155 // the translated "Details" string
156 static wxString ms_details
;
158 // the maximum length of the log message
159 static size_t ms_maxLength
;
161 DECLARE_EVENT_TABLE()
162 DECLARE_NO_COPY_CLASS(wxLogDialog
)
165 BEGIN_EVENT_TABLE(wxLogDialog
, wxDialog
)
166 EVT_BUTTON(wxID_OK
, wxLogDialog::OnOk
)
168 EVT_BUTTON(wxID_SAVE
, wxLogDialog::OnSave
)
170 EVT_LIST_ITEM_SELECTED(wxID_ANY
, wxLogDialog::OnListSelect
)
171 EVT_LIST_ITEM_ACTIVATED(wxID_ANY
, wxLogDialog::OnListItemActivated
)
174 #endif // wxUSE_LOG_DIALOG
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 #if wxUSE_FILE && wxUSE_FILEDLG
182 // pass an uninitialized file object, the function will ask the user for the
183 // filename and try to open it, returns true on success (file was opened),
184 // false if file couldn't be opened/created and -1 if the file selection
185 // dialog was cancelled
186 static int OpenLogFile(wxFile
& file
, wxString
*filename
= NULL
, wxWindow
*parent
= NULL
);
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 // we use a global variable to store the frame pointer for wxLogStatus - bad,
195 // but it's the easiest way
196 static wxFrame
*gs_pFrame
= NULL
; // FIXME MT-unsafe
198 // ============================================================================
200 // ============================================================================
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
206 // accepts an additional argument which tells to which frame the output should
208 void wxVLogStatus(wxFrame
*pFrame
, const wxChar
*szFormat
, va_list argptr
)
212 wxLog
*pLog
= wxLog::GetActiveTarget();
213 if ( pLog
!= NULL
) {
214 msg
.PrintfV(szFormat
, argptr
);
216 wxASSERT( gs_pFrame
== NULL
); // should be reset!
219 wxLog::OnLog(wxLOG_Status
, msg
, 0);
221 wxLog::OnLog(wxLOG_Status
, msg
, time(NULL
));
223 gs_pFrame
= (wxFrame
*) NULL
;
227 void wxDoLogStatus(wxFrame
*pFrame
, const wxChar
*szFormat
, ...)
230 va_start(argptr
, szFormat
);
231 wxVLogStatus(pFrame
, szFormat
, argptr
);
235 // ----------------------------------------------------------------------------
236 // wxLogGui implementation (FIXME MT-unsafe)
237 // ----------------------------------------------------------------------------
246 void wxLogGui::Clear()
250 m_bHasMessages
= false;
257 void wxLogGui::Flush()
259 if ( !m_bHasMessages
)
262 // do it right now to block any new calls to Flush() while we're here
263 m_bHasMessages
= false;
265 unsigned repeatCount
= 0;
266 if ( wxLog::GetRepetitionCounting() )
268 repeatCount
= wxLog::DoLogNumberOfRepeats();
271 wxString appName
= wxTheApp
->GetAppName();
272 if ( !appName
.empty() )
273 appName
[0u] = (wxChar
)wxToupper(appName
[0u]);
276 wxString titleFormat
;
278 titleFormat
= _("%s Error");
281 else if ( m_bWarnings
) {
282 titleFormat
= _("%s Warning");
283 style
= wxICON_EXCLAMATION
;
286 titleFormat
= _("%s Information");
287 style
= wxICON_INFORMATION
;
291 title
.Printf(titleFormat
, appName
.c_str());
293 size_t nMsgCount
= m_aMessages
.Count();
295 // avoid showing other log dialogs until we're done with the dialog we're
296 // showing right now: nested modal dialogs make for really bad UI!
300 if ( nMsgCount
== 1 )
302 str
= m_aMessages
[0];
304 else // more than one message
308 if ( repeatCount
> 0 )
309 m_aMessages
[nMsgCount
-1] += wxString::Format(wxT(" (%s)"), m_aMessages
[nMsgCount
-2].c_str());
310 wxLogDialog
dlg(NULL
,
311 m_aMessages
, m_aSeverity
, m_aTimes
,
314 // clear the message list before showing the dialog because while it's
315 // shown some new messages may appear
318 (void)dlg
.ShowModal();
319 #else // !wxUSE_LOG_DIALOG
320 // concatenate all strings (but not too many to not overfill the msg box)
323 // start from the most recent message
324 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
325 // for Windows strings longer than this value are wrapped (NT 4.0)
326 const size_t nMsgLineWidth
= 156;
328 nLines
+= (m_aMessages
[n
- 1].Len() + nMsgLineWidth
- 1) / nMsgLineWidth
;
330 if ( nLines
> 25 ) // don't put too many lines in message box
333 str
<< m_aMessages
[n
- 1] << wxT("\n");
335 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
338 // this catches both cases of 1 message with wxUSE_LOG_DIALOG and any
339 // situation without it
342 wxMessageBox(str
, title
, wxOK
| style
);
344 // no undisplayed messages whatsoever
348 // allow flushing the logs again
352 // log all kinds of messages
353 void wxLogGui::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
360 m_aMessages
.Add(szString
);
361 m_aSeverity
.Add(wxLOG_Message
);
362 m_aTimes
.Add((long)t
);
363 m_bHasMessages
= true;
370 // find the top window and set it's status text if it has any
371 wxFrame
*pFrame
= gs_pFrame
;
372 if ( pFrame
== NULL
) {
373 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
374 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
375 pFrame
= (wxFrame
*)pWin
;
379 if ( pFrame
&& pFrame
->GetStatusBar() )
380 pFrame
->SetStatusText(szString
);
382 #endif // wxUSE_STATUSBAR
393 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
394 // don't prepend debug/trace here: it goes to the
395 // debug window anyhow
397 OutputDebugString(str
);
399 // send them to stderr
400 wxFprintf(stderr
, wxT("[%s] %s\n"),
401 level
== wxLOG_Trace
? wxT("Trace")
407 #endif // __WXDEBUG__
411 case wxLOG_FatalError
:
412 // show this one immediately
413 wxMessageBox(szString
, _("Fatal error"), wxICON_HAND
);
419 #if !wxUSE_LOG_DIALOG
420 // discard earlier informational messages if this is the 1st
421 // error because they might not make sense any more and showing
422 // them in a message box might be confusing
426 #endif // wxUSE_LOG_DIALOG
433 // for the warning we don't discard the info messages
437 m_aMessages
.Add(szString
);
438 m_aSeverity
.Add((int)level
);
439 m_aTimes
.Add((long)t
);
440 m_bHasMessages
= true;
445 #endif // wxUSE_LOGGUI
447 // ----------------------------------------------------------------------------
448 // wxLogWindow and wxLogFrame implementation
449 // ----------------------------------------------------------------------------
455 class wxLogFrame
: public wxFrame
459 wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
);
460 virtual ~wxLogFrame();
463 void OnClose(wxCommandEvent
& event
);
464 void OnCloseWindow(wxCloseEvent
& event
);
466 void OnSave (wxCommandEvent
& event
);
468 void OnClear(wxCommandEvent
& event
);
471 wxTextCtrl
*TextCtrl() const { return m_pTextCtrl
; }
474 // use standard ids for our commands!
477 Menu_Close
= wxID_CLOSE
,
478 Menu_Save
= wxID_SAVE
,
479 Menu_Clear
= wxID_CLEAR
482 // common part of OnClose() and OnCloseWindow()
485 wxTextCtrl
*m_pTextCtrl
;
488 DECLARE_EVENT_TABLE()
489 DECLARE_NO_COPY_CLASS(wxLogFrame
)
492 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
493 // wxLogWindow menu events
494 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
496 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
498 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
500 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
503 wxLogFrame::wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
)
504 : wxFrame(pParent
, wxID_ANY
, szTitle
)
508 m_pTextCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
512 // needed for Win32 to avoid 65Kb limit but it doesn't work well
513 // when using RichEdit 2.0 which we always do in the Unicode build
516 #endif // !wxUSE_UNICODE
521 wxMenuBar
*pMenuBar
= new wxMenuBar
;
522 wxMenu
*pMenu
= new wxMenu
;
524 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
526 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
527 pMenu
->AppendSeparator();
528 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
529 pMenuBar
->Append(pMenu
, _("&Log"));
530 SetMenuBar(pMenuBar
);
531 #endif // wxUSE_MENUS
534 // status bar for menu prompts
536 #endif // wxUSE_STATUSBAR
538 m_log
->OnFrameCreate(this);
541 void wxLogFrame::DoClose()
543 if ( m_log
->OnFrameClose(this) )
545 // instead of closing just hide the window to be able to Show() it
551 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
556 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
562 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
567 int rc
= OpenLogFile(file
, &filename
, this);
576 // retrieve text and save it
577 // -------------------------
578 int nLines
= m_pTextCtrl
->GetNumberOfLines();
579 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
580 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
581 wxTextFile::GetEOL());
588 wxLogError(_("Can't save log contents to file."));
591 wxLogStatus(this, _("Log saved to the file '%s'."), filename
.c_str());
597 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
599 m_pTextCtrl
->Clear();
602 wxLogFrame::~wxLogFrame()
604 m_log
->OnFrameDelete(this);
610 wxLogWindow::wxLogWindow(wxWindow
*pParent
,
611 const wxChar
*szTitle
,
615 PassMessages(bDoPass
);
617 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
623 void wxLogWindow::Show(bool bShow
)
625 m_pLogFrame
->Show(bShow
);
628 void wxLogWindow::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
630 // first let the previous logger show it
631 wxLogPassThrough::DoLog(level
, szString
, t
);
636 // by default, these messages are ignored by wxLog, so process
638 if ( !wxIsEmpty(szString
) )
641 str
<< _("Status: ") << szString
;
646 // don't put trace messages in the text window for 2 reasons:
647 // 1) there are too many of them
648 // 2) they may provoke other trace messages thus sending a program
649 // into an infinite loop
654 // and this will format it nicely and call our DoLogString()
655 wxLog::DoLog(level
, szString
, t
);
660 void wxLogWindow::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
662 // put the text into our window
663 wxTextCtrl
*pText
= m_pLogFrame
->TextCtrl();
665 // remove selection (WriteText is in fact ReplaceSelection)
667 wxTextPos nLen
= pText
->GetLastPosition();
668 pText
->SetSelection(nLen
, nLen
);
673 msg
<< szString
<< wxT('\n');
675 pText
->AppendText(msg
);
677 // TODO ensure that the line can be seen
680 wxFrame
*wxLogWindow::GetFrame() const
685 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
689 bool wxLogWindow::OnFrameClose(wxFrame
* WXUNUSED(frame
))
695 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
697 m_pLogFrame
= (wxLogFrame
*)NULL
;
700 wxLogWindow::~wxLogWindow()
702 // may be NULL if log frame already auto destroyed itself
706 #endif // wxUSE_LOGWINDOW
708 // ----------------------------------------------------------------------------
710 // ----------------------------------------------------------------------------
714 #ifndef __SMARTPHONE__
715 static const size_t MARGIN
= 10;
717 static const size_t MARGIN
= 0;
720 wxString
wxLogDialog::ms_details
;
721 size_t wxLogDialog::ms_maxLength
= 0;
723 wxLogDialog::wxLogDialog(wxWindow
*parent
,
724 const wxArrayString
& messages
,
725 const wxArrayInt
& severity
,
726 const wxArrayLong
& times
,
727 const wxString
& caption
,
729 : wxDialog(parent
, wxID_ANY
, caption
,
730 wxDefaultPosition
, wxDefaultSize
,
731 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
733 // init the static variables:
735 if ( ms_details
.empty() )
737 // ensure that we won't loop here if wxGetTranslation()
738 // happens to pop up a Log message while translating this :-)
739 ms_details
= wxTRANSLATE("&Details");
740 ms_details
= wxGetTranslation(ms_details
);
741 #ifdef __SMARTPHONE__
742 ms_details
= wxStripMenuCodes(ms_details
);
746 if ( ms_maxLength
== 0 )
748 ms_maxLength
= (2 * wxGetDisplaySize().x
/3) / GetCharWidth();
751 size_t count
= messages
.GetCount();
752 m_messages
.Alloc(count
);
753 m_severity
.Alloc(count
);
754 m_times
.Alloc(count
);
756 for ( size_t n
= 0; n
< count
; n
++ )
758 m_messages
.Add(messages
[n
]);
759 m_severity
.Add(severity
[n
]);
760 m_times
.Add(times
[n
]);
763 m_listctrl
= (wxListCtrl
*)NULL
;
765 #ifndef __SMARTPHONE__
768 m_statline
= (wxStaticLine
*)NULL
;
769 #endif // wxUSE_STATLINE
772 m_btnSave
= (wxButton
*)NULL
;
775 #endif // __SMARTPHONE__
777 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
779 // create the controls which are always shown and layout them: we use
780 // sizers even though our window is not resizeable to calculate the size of
781 // the dialog properly
782 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
783 wxBoxSizer
*sizerAll
= new wxBoxSizer(isPda
? wxVERTICAL
: wxHORIZONTAL
);
786 switch ( style
& wxICON_MASK
)
789 bitmap
= wxArtProvider::GetBitmap(wxART_ERROR
, wxART_MESSAGE_BOX
);
791 bitmap
.SetId(wxICON_SMALL_ERROR
);
795 case wxICON_INFORMATION
:
796 bitmap
= wxArtProvider::GetBitmap(wxART_INFORMATION
, wxART_MESSAGE_BOX
);
798 bitmap
.SetId(wxICON_SMALL_INFO
);
803 bitmap
= wxArtProvider::GetBitmap(wxART_WARNING
, wxART_MESSAGE_BOX
);
805 bitmap
.SetId(wxICON_SMALL_WARNING
);
810 wxFAIL_MSG(_T("incorrect log style"));
814 sizerAll
->Add(new wxStaticBitmap(this, wxID_ANY
, bitmap
), 0,
815 wxALIGN_CENTRE_VERTICAL
);
817 // create the text sizer with a minimal size so that we are sure it won't be too small
818 wxString message
= EllipsizeString(messages
.Last());
819 wxSizer
*szText
= CreateTextSizer(message
);
820 szText
->SetMinSize(wxMin(300, wxGetDisplaySize().x
/ 3), -1);
822 sizerAll
->Add(szText
, 1,
823 wxALIGN_CENTRE_VERTICAL
| wxLEFT
| wxRIGHT
, MARGIN
);
825 wxButton
*btnOk
= new wxButton(this, wxID_OK
);
826 sizerAll
->Add(btnOk
, 0, isPda
? wxCENTRE
: wxCENTRE
|wxBOTTOM
, MARGIN
/2);
828 sizerTop
->Add(sizerAll
, 0, wxALL
| wxEXPAND
, MARGIN
);
831 // add the details pane
833 #ifndef __SMARTPHONE__
834 wxCollapsiblePane
*collpane
= new wxCollapsiblePane(this, wxID_ANY
, ms_details
);
835 sizerTop
->Add(collpane
, 1, wxGROW
|wxALL
, MARGIN
);
837 wxWindow
*win
= collpane
->GetPane();
838 wxSizer
*paneSz
= new wxBoxSizer(wxVERTICAL
);
840 CreateDetailsControls(win
);
842 paneSz
->Add(m_listctrl
, 1, wxEXPAND
| wxTOP
, MARGIN
);
844 #if wxUSE_FILE && !defined(__SMARTPHONE__)
845 paneSz
->Add(m_btnSave
, 0, wxALIGN_RIGHT
| wxTOP
, MARGIN
);
848 win
->SetSizer(paneSz
);
849 paneSz
->SetSizeHints(win
);
850 #else // __SMARTPHONE__
851 SetLeftMenu(wxID_OK
);
852 SetRightMenu(wxID_MORE
, ms_details
+ EXPAND_SUFFIX
);
853 #endif // __SMARTPHONE__/!__SMARTPHONE__
855 SetSizerAndFit(sizerTop
);
861 // Move up the screen so that when we expand the dialog,
862 // there's enough space.
863 Move(wxPoint(GetPosition().x
, GetPosition().y
/ 2));
867 void wxLogDialog::CreateDetailsControls(wxWindow
*parent
)
869 #ifndef __SMARTPHONE__
870 // create the save button and separator line if possible
872 m_btnSave
= new wxButton(parent
, wxID_SAVE
);
875 #endif // __SMARTPHONE__
877 // create the list ctrl now
878 m_listctrl
= new wxListCtrl(parent
, wxID_ANY
,
879 wxDefaultPosition
, wxDefaultSize
,
885 // This makes a big aesthetic difference on WinCE but I
886 // don't want to risk problems on other platforms
890 // no need to translate these strings as they're not shown to the
891 // user anyhow (we use wxLC_NO_HEADER style)
892 m_listctrl
->InsertColumn(0, _T("Message"));
893 m_listctrl
->InsertColumn(1, _T("Time"));
895 // prepare the imagelist
896 static const int ICON_SIZE
= 16;
897 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
899 // order should be the same as in the switch below!
900 static const wxChar
* icons
[] =
907 bool loadedIcons
= true;
909 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
911 wxBitmap bmp
= wxArtProvider::GetBitmap(icons
[icon
], wxART_MESSAGE_BOX
,
912 wxSize(ICON_SIZE
, ICON_SIZE
));
914 // This may very well fail if there are insufficient colours available.
915 // Degrade gracefully.
926 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
929 wxString fmt
= wxLog::GetTimestamp();
936 size_t count
= m_messages
.GetCount();
937 for ( size_t n
= 0; n
< count
; n
++ )
943 switch ( m_severity
[n
] )
957 else // failed to load images
962 wxString msg
= m_messages
[n
];
963 msg
.Replace(wxT("\n"), wxT(" "));
964 msg
= EllipsizeString(msg
);
966 m_listctrl
->InsertItem(n
, msg
, image
);
967 m_listctrl
->SetItem(n
, 1, TimeStamp(fmt
, (time_t)m_times
[n
]));
970 // let the columns size themselves
971 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
972 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
974 // calculate an approximately nice height for the listctrl
975 int height
= GetCharHeight()*(count
+ 4);
977 // but check that the dialog won't fall fown from the screen
979 // we use GetMinHeight() to get the height of the dialog part without the
980 // details and we consider that the "Save" button below and the separator
981 // line (and the margins around it) take about as much, hence double it
982 int heightMax
= wxGetDisplaySize().y
- GetPosition().y
- 2*GetMinHeight();
984 // we should leave a margin
988 m_listctrl
->SetSize(wxDefaultCoord
, wxMin(height
, heightMax
));
991 void wxLogDialog::OnListSelect(wxListEvent
& event
)
993 // we can't just disable the control because this looks ugly under Windows
994 // (wrong bg colour, no scrolling...), but we still want to disable
995 // selecting items - it makes no sense here
996 m_listctrl
->SetItemState(event
.GetIndex(), 0, wxLIST_STATE_SELECTED
);
999 void wxLogDialog::OnListItemActivated(wxListEvent
& event
)
1001 // show the activated item in a message box
1002 // This allow the user to correctly display the logs which are longer
1003 // than the listctrl and thus gets truncated or those which contains
1007 // wxString str = m_listctrl->GetItemText(event.GetIndex());
1008 // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW.
1009 wxString str
= m_messages
[event
.GetIndex()];
1011 // wxMessageBox will nicely handle the '\n' in the string (if any)
1012 // and supports long strings
1013 wxMessageBox(str
, wxT("Log message"), wxOK
, this);
1016 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
1023 void wxLogDialog::OnSave(wxCommandEvent
& WXUNUSED(event
))
1027 int rc
= OpenLogFile(file
, NULL
, this);
1036 wxString fmt
= wxLog::GetTimestamp();
1043 size_t count
= m_messages
.GetCount();
1044 for ( size_t n
= 0; ok
&& (n
< count
); n
++ )
1047 line
<< TimeStamp(fmt
, (time_t)m_times
[n
])
1050 << wxTextFile::GetEOL();
1052 ok
= file
.Write(line
);
1059 wxLogError(_("Can't save log contents to file."));
1060 #endif // wxUSE_FILEDLG
1063 #endif // wxUSE_FILE
1065 wxLogDialog::~wxLogDialog()
1069 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
1073 #endif // wxUSE_LOG_DIALOG
1075 #if wxUSE_FILE && wxUSE_FILEDLG
1077 // pass an uninitialized file object, the function will ask the user for the
1078 // filename and try to open it, returns true on success (file was opened),
1079 // false if file couldn't be opened/created and -1 if the file selection
1080 // dialog was cancelled
1081 static int OpenLogFile(wxFile
& file
, wxString
*pFilename
, wxWindow
*parent
)
1083 // get the file name
1084 // -----------------
1085 wxString filename
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent
);
1093 bool bOk
wxDUMMY_INITIALIZE(false);
1094 if ( wxFile::Exists(filename
) ) {
1095 bool bAppend
= false;
1097 strMsg
.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
1099 switch ( wxMessageBox(strMsg
, _("Question"),
1100 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
) ) {
1113 wxFAIL_MSG(_("invalid message box return value"));
1117 bOk
= file
.Open(filename
, wxFile::write_append
);
1120 bOk
= file
.Create(filename
, true /* overwrite */);
1124 bOk
= file
.Create(filename
);
1128 *pFilename
= filename
;
1133 #endif // wxUSE_FILE
1135 #endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
1137 #if wxUSE_LOG && wxUSE_TEXTCTRL
1139 // ----------------------------------------------------------------------------
1140 // wxLogTextCtrl implementation
1141 // ----------------------------------------------------------------------------
1143 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
1145 m_pTextCtrl
= pTextCtrl
;
1148 void wxLogTextCtrl::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
1153 msg
<< szString
<< wxT('\n');
1154 m_pTextCtrl
->AppendText(msg
);
1157 #endif // wxUSE_LOG && wxUSE_TEXTCTRL