]>
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 wxChar
*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 void wxDoLogStatus(wxFrame
*pFrame
, const wxString
& format
, ...)
231 va_start(argptr
, format
);
232 wxVLogStatus(pFrame
, format
, argptr
);
236 // ----------------------------------------------------------------------------
237 // wxLogGui implementation (FIXME MT-unsafe)
238 // ----------------------------------------------------------------------------
247 void wxLogGui::Clear()
251 m_bHasMessages
= false;
258 void wxLogGui::Flush()
260 if ( !m_bHasMessages
)
263 // do it right now to block any new calls to Flush() while we're here
264 m_bHasMessages
= false;
266 unsigned repeatCount
= 0;
267 if ( wxLog::GetRepetitionCounting() )
269 repeatCount
= wxLog::DoLogNumberOfRepeats();
272 wxString appName
= wxTheApp
->GetAppName();
273 if ( !appName
.empty() )
274 appName
[0u] = (wxChar
)wxToupper(appName
[0u]);
277 wxString titleFormat
;
279 titleFormat
= _("%s Error");
282 else if ( m_bWarnings
) {
283 titleFormat
= _("%s Warning");
284 style
= wxICON_EXCLAMATION
;
287 titleFormat
= _("%s Information");
288 style
= wxICON_INFORMATION
;
292 title
.Printf(titleFormat
, appName
.c_str());
294 size_t nMsgCount
= m_aMessages
.GetCount();
296 // avoid showing other log dialogs until we're done with the dialog we're
297 // showing right now: nested modal dialogs make for really bad UI!
301 if ( nMsgCount
== 1 )
303 str
= m_aMessages
[0];
305 else // more than one message
309 if ( repeatCount
> 0 )
310 m_aMessages
[nMsgCount
-1] += wxString::Format(wxT(" (%s)"), m_aMessages
[nMsgCount
-2].c_str());
311 wxLogDialog
dlg(NULL
,
312 m_aMessages
, m_aSeverity
, m_aTimes
,
315 // clear the message list before showing the dialog because while it's
316 // shown some new messages may appear
319 (void)dlg
.ShowModal();
320 #else // !wxUSE_LOG_DIALOG
321 // concatenate all strings (but not too many to not overfill the msg box)
324 // start from the most recent message
325 for ( size_t n
= nMsgCount
; n
> 0; n
-- ) {
326 // for Windows strings longer than this value are wrapped (NT 4.0)
327 const size_t nMsgLineWidth
= 156;
329 nLines
+= (m_aMessages
[n
- 1].Len() + nMsgLineWidth
- 1) / nMsgLineWidth
;
331 if ( nLines
> 25 ) // don't put too many lines in message box
334 str
<< m_aMessages
[n
- 1] << wxT("\n");
336 #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG
339 // this catches both cases of 1 message with wxUSE_LOG_DIALOG and any
340 // situation without it
343 wxMessageBox(str
, title
, wxOK
| style
);
345 // no undisplayed messages whatsoever
349 // allow flushing the logs again
353 // log all kinds of messages
354 void wxLogGui::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
361 m_aMessages
.Add(szString
);
362 m_aSeverity
.Add(wxLOG_Message
);
363 m_aTimes
.Add((long)t
);
364 m_bHasMessages
= true;
371 // find the top window and set it's status text if it has any
372 wxFrame
*pFrame
= gs_pFrame
;
373 if ( pFrame
== NULL
) {
374 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
375 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
376 pFrame
= (wxFrame
*)pWin
;
380 if ( pFrame
&& pFrame
->GetStatusBar() )
381 pFrame
->SetStatusText(szString
);
383 #endif // wxUSE_STATUSBAR
394 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
395 // don't prepend debug/trace here: it goes to the
396 // debug window anyhow
398 OutputDebugString(str
);
400 // send them to stderr
401 wxFprintf(stderr
, wxT("[%s] %s\n"),
402 level
== wxLOG_Trace
? wxT("Trace")
408 #endif // __WXDEBUG__
412 case wxLOG_FatalError
:
413 // show this one immediately
414 wxMessageBox(szString
, _("Fatal error"), wxICON_HAND
);
420 #if !wxUSE_LOG_DIALOG
421 // discard earlier informational messages if this is the 1st
422 // error because they might not make sense any more and showing
423 // them in a message box might be confusing
427 #endif // wxUSE_LOG_DIALOG
434 // for the warning we don't discard the info messages
438 m_aMessages
.Add(szString
);
439 m_aSeverity
.Add((int)level
);
440 m_aTimes
.Add((long)t
);
441 m_bHasMessages
= true;
446 #endif // wxUSE_LOGGUI
448 // ----------------------------------------------------------------------------
449 // wxLogWindow and wxLogFrame implementation
450 // ----------------------------------------------------------------------------
456 class wxLogFrame
: public wxFrame
460 wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
);
461 virtual ~wxLogFrame();
464 void OnClose(wxCommandEvent
& event
);
465 void OnCloseWindow(wxCloseEvent
& event
);
467 void OnSave (wxCommandEvent
& event
);
469 void OnClear(wxCommandEvent
& event
);
472 wxTextCtrl
*TextCtrl() const { return m_pTextCtrl
; }
475 // use standard ids for our commands!
478 Menu_Close
= wxID_CLOSE
,
479 Menu_Save
= wxID_SAVE
,
480 Menu_Clear
= wxID_CLEAR
483 // common part of OnClose() and OnCloseWindow()
486 wxTextCtrl
*m_pTextCtrl
;
489 DECLARE_EVENT_TABLE()
490 DECLARE_NO_COPY_CLASS(wxLogFrame
)
493 BEGIN_EVENT_TABLE(wxLogFrame
, wxFrame
)
494 // wxLogWindow menu events
495 EVT_MENU(Menu_Close
, wxLogFrame::OnClose
)
497 EVT_MENU(Menu_Save
, wxLogFrame::OnSave
)
499 EVT_MENU(Menu_Clear
, wxLogFrame::OnClear
)
501 EVT_CLOSE(wxLogFrame::OnCloseWindow
)
504 wxLogFrame::wxLogFrame(wxWindow
*pParent
, wxLogWindow
*log
, const wxChar
*szTitle
)
505 : wxFrame(pParent
, wxID_ANY
, szTitle
)
509 m_pTextCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
513 // needed for Win32 to avoid 65Kb limit but it doesn't work well
514 // when using RichEdit 2.0 which we always do in the Unicode build
517 #endif // !wxUSE_UNICODE
522 wxMenuBar
*pMenuBar
= new wxMenuBar
;
523 wxMenu
*pMenu
= new wxMenu
;
525 pMenu
->Append(Menu_Save
, _("&Save..."), _("Save log contents to file"));
527 pMenu
->Append(Menu_Clear
, _("C&lear"), _("Clear the log contents"));
528 pMenu
->AppendSeparator();
529 pMenu
->Append(Menu_Close
, _("&Close"), _("Close this window"));
530 pMenuBar
->Append(pMenu
, _("&Log"));
531 SetMenuBar(pMenuBar
);
532 #endif // wxUSE_MENUS
535 // status bar for menu prompts
537 #endif // wxUSE_STATUSBAR
539 m_log
->OnFrameCreate(this);
542 void wxLogFrame::DoClose()
544 if ( m_log
->OnFrameClose(this) )
546 // instead of closing just hide the window to be able to Show() it
552 void wxLogFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
557 void wxLogFrame::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
563 void wxLogFrame::OnSave(wxCommandEvent
& WXUNUSED(event
))
568 int rc
= OpenLogFile(file
, &filename
, this);
577 // retrieve text and save it
578 // -------------------------
579 int nLines
= m_pTextCtrl
->GetNumberOfLines();
580 for ( int nLine
= 0; bOk
&& nLine
< nLines
; nLine
++ ) {
581 bOk
= file
.Write(m_pTextCtrl
->GetLineText(nLine
) +
582 wxTextFile::GetEOL());
589 wxLogError(_("Can't save log contents to file."));
592 wxLogStatus((wxFrame
*)this, _("Log saved to the file '%s'."), filename
.c_str());
598 void wxLogFrame::OnClear(wxCommandEvent
& WXUNUSED(event
))
600 m_pTextCtrl
->Clear();
603 wxLogFrame::~wxLogFrame()
605 m_log
->OnFrameDelete(this);
611 wxLogWindow::wxLogWindow(wxWindow
*pParent
,
612 const wxChar
*szTitle
,
616 PassMessages(bDoPass
);
618 m_pLogFrame
= new wxLogFrame(pParent
, this, szTitle
);
624 void wxLogWindow::Show(bool bShow
)
626 m_pLogFrame
->Show(bShow
);
629 void wxLogWindow::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
631 // first let the previous logger show it
632 wxLogPassThrough::DoLog(level
, szString
, t
);
637 // by default, these messages are ignored by wxLog, so process
639 if ( !wxIsEmpty(szString
) )
642 str
<< _("Status: ") << szString
;
647 // don't put trace messages in the text window for 2 reasons:
648 // 1) there are too many of them
649 // 2) they may provoke other trace messages thus sending a program
650 // into an infinite loop
655 // and this will format it nicely and call our DoLogString()
656 wxLog::DoLog(level
, szString
, t
);
661 void wxLogWindow::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
663 // put the text into our window
664 wxTextCtrl
*pText
= m_pLogFrame
->TextCtrl();
666 // remove selection (WriteText is in fact ReplaceSelection)
668 wxTextPos nLen
= pText
->GetLastPosition();
669 pText
->SetSelection(nLen
, nLen
);
674 msg
<< szString
<< wxT('\n');
676 pText
->AppendText(msg
);
678 // TODO ensure that the line can be seen
681 wxFrame
*wxLogWindow::GetFrame() const
686 void wxLogWindow::OnFrameCreate(wxFrame
* WXUNUSED(frame
))
690 bool wxLogWindow::OnFrameClose(wxFrame
* WXUNUSED(frame
))
696 void wxLogWindow::OnFrameDelete(wxFrame
* WXUNUSED(frame
))
698 m_pLogFrame
= (wxLogFrame
*)NULL
;
701 wxLogWindow::~wxLogWindow()
703 // may be NULL if log frame already auto destroyed itself
707 #endif // wxUSE_LOGWINDOW
709 // ----------------------------------------------------------------------------
711 // ----------------------------------------------------------------------------
715 #ifndef __SMARTPHONE__
716 static const size_t MARGIN
= 10;
718 static const size_t MARGIN
= 0;
721 wxString
wxLogDialog::ms_details
;
722 size_t wxLogDialog::ms_maxLength
= 0;
724 wxLogDialog::wxLogDialog(wxWindow
*parent
,
725 const wxArrayString
& messages
,
726 const wxArrayInt
& severity
,
727 const wxArrayLong
& times
,
728 const wxString
& caption
,
730 : wxDialog(parent
, wxID_ANY
, caption
,
731 wxDefaultPosition
, wxDefaultSize
,
732 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
734 // init the static variables:
736 if ( ms_details
.empty() )
738 // ensure that we won't loop here if wxGetTranslation()
739 // happens to pop up a Log message while translating this :-)
740 ms_details
= wxTRANSLATE("&Details");
741 ms_details
= wxGetTranslation(ms_details
);
742 #ifdef __SMARTPHONE__
743 ms_details
= wxStripMenuCodes(ms_details
);
747 if ( ms_maxLength
== 0 )
749 ms_maxLength
= (2 * wxGetDisplaySize().x
/3) / GetCharWidth();
752 size_t count
= messages
.GetCount();
753 m_messages
.Alloc(count
);
754 m_severity
.Alloc(count
);
755 m_times
.Alloc(count
);
757 for ( size_t n
= 0; n
< count
; n
++ )
759 m_messages
.Add(messages
[n
]);
760 m_severity
.Add(severity
[n
]);
761 m_times
.Add(times
[n
]);
764 m_listctrl
= (wxListCtrl
*)NULL
;
766 #ifndef __SMARTPHONE__
769 m_statline
= (wxStaticLine
*)NULL
;
770 #endif // wxUSE_STATLINE
773 m_btnSave
= (wxButton
*)NULL
;
776 #endif // __SMARTPHONE__
778 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
780 // create the controls which are always shown and layout them: we use
781 // sizers even though our window is not resizeable to calculate the size of
782 // the dialog properly
783 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
784 wxBoxSizer
*sizerAll
= new wxBoxSizer(isPda
? wxVERTICAL
: wxHORIZONTAL
);
787 switch ( style
& wxICON_MASK
)
790 bitmap
= wxArtProvider::GetBitmap(wxART_ERROR
, wxART_MESSAGE_BOX
);
792 bitmap
.SetId(wxICON_SMALL_ERROR
);
796 case wxICON_INFORMATION
:
797 bitmap
= wxArtProvider::GetBitmap(wxART_INFORMATION
, wxART_MESSAGE_BOX
);
799 bitmap
.SetId(wxICON_SMALL_INFO
);
804 bitmap
= wxArtProvider::GetBitmap(wxART_WARNING
, wxART_MESSAGE_BOX
);
806 bitmap
.SetId(wxICON_SMALL_WARNING
);
811 wxFAIL_MSG(_T("incorrect log style"));
815 sizerAll
->Add(new wxStaticBitmap(this, wxID_ANY
, bitmap
), 0,
816 wxALIGN_CENTRE_VERTICAL
);
818 // create the text sizer with a minimal size so that we are sure it won't be too small
819 wxString message
= EllipsizeString(messages
.Last());
820 wxSizer
*szText
= CreateTextSizer(message
);
821 szText
->SetMinSize(wxMin(300, wxGetDisplaySize().x
/ 3), -1);
823 sizerAll
->Add(szText
, 1,
824 wxALIGN_CENTRE_VERTICAL
| wxLEFT
| wxRIGHT
, MARGIN
);
826 wxButton
*btnOk
= new wxButton(this, wxID_OK
);
827 sizerAll
->Add(btnOk
, 0, isPda
? wxCENTRE
: wxCENTRE
|wxBOTTOM
, MARGIN
/2);
829 sizerTop
->Add(sizerAll
, 0, wxALL
| wxEXPAND
, MARGIN
);
832 // add the details pane
834 #ifndef __SMARTPHONE__
835 wxCollapsiblePane
*collpane
= new wxCollapsiblePane(this, wxID_ANY
, ms_details
);
836 sizerTop
->Add(collpane
, 1, wxGROW
|wxALL
, MARGIN
);
838 wxWindow
*win
= collpane
->GetPane();
839 wxSizer
*paneSz
= new wxBoxSizer(wxVERTICAL
);
841 CreateDetailsControls(win
);
843 paneSz
->Add(m_listctrl
, 1, wxEXPAND
| wxTOP
, MARGIN
);
845 #if wxUSE_FILE && !defined(__SMARTPHONE__)
846 paneSz
->Add(m_btnSave
, 0, wxALIGN_RIGHT
| wxTOP
, MARGIN
);
849 win
->SetSizer(paneSz
);
850 paneSz
->SetSizeHints(win
);
851 #else // __SMARTPHONE__
852 SetLeftMenu(wxID_OK
);
853 SetRightMenu(wxID_MORE
, ms_details
+ EXPAND_SUFFIX
);
854 #endif // __SMARTPHONE__/!__SMARTPHONE__
856 SetSizerAndFit(sizerTop
);
862 // Move up the screen so that when we expand the dialog,
863 // there's enough space.
864 Move(wxPoint(GetPosition().x
, GetPosition().y
/ 2));
868 void wxLogDialog::CreateDetailsControls(wxWindow
*parent
)
870 #ifndef __SMARTPHONE__
871 // create the save button and separator line if possible
873 m_btnSave
= new wxButton(parent
, wxID_SAVE
);
876 #endif // __SMARTPHONE__
878 // create the list ctrl now
879 m_listctrl
= new wxListCtrl(parent
, wxID_ANY
,
880 wxDefaultPosition
, wxDefaultSize
,
886 // This makes a big aesthetic difference on WinCE but I
887 // don't want to risk problems on other platforms
891 // no need to translate these strings as they're not shown to the
892 // user anyhow (we use wxLC_NO_HEADER style)
893 m_listctrl
->InsertColumn(0, _T("Message"));
894 m_listctrl
->InsertColumn(1, _T("Time"));
896 // prepare the imagelist
897 static const int ICON_SIZE
= 16;
898 wxImageList
*imageList
= new wxImageList(ICON_SIZE
, ICON_SIZE
);
900 // order should be the same as in the switch below!
901 static const wxChar
* icons
[] =
908 bool loadedIcons
= true;
910 for ( size_t icon
= 0; icon
< WXSIZEOF(icons
); icon
++ )
912 wxBitmap bmp
= wxArtProvider::GetBitmap(icons
[icon
], wxART_MESSAGE_BOX
,
913 wxSize(ICON_SIZE
, ICON_SIZE
));
915 // This may very well fail if there are insufficient colours available.
916 // Degrade gracefully.
927 m_listctrl
->SetImageList(imageList
, wxIMAGE_LIST_SMALL
);
930 wxString fmt
= wxLog::GetTimestamp();
937 size_t count
= m_messages
.GetCount();
938 for ( size_t n
= 0; n
< count
; n
++ )
944 switch ( m_severity
[n
] )
958 else // failed to load images
963 wxString msg
= m_messages
[n
];
964 msg
.Replace(wxT("\n"), wxT(" "));
965 msg
= EllipsizeString(msg
);
967 m_listctrl
->InsertItem(n
, msg
, image
);
968 m_listctrl
->SetItem(n
, 1, TimeStamp(fmt
, (time_t)m_times
[n
]));
971 // let the columns size themselves
972 m_listctrl
->SetColumnWidth(0, wxLIST_AUTOSIZE
);
973 m_listctrl
->SetColumnWidth(1, wxLIST_AUTOSIZE
);
975 // calculate an approximately nice height for the listctrl
976 int height
= GetCharHeight()*(count
+ 4);
978 // but check that the dialog won't fall fown from the screen
980 // we use GetMinHeight() to get the height of the dialog part without the
981 // details and we consider that the "Save" button below and the separator
982 // line (and the margins around it) take about as much, hence double it
983 int heightMax
= wxGetDisplaySize().y
- GetPosition().y
- 2*GetMinHeight();
985 // we should leave a margin
989 m_listctrl
->SetSize(wxDefaultCoord
, wxMin(height
, heightMax
));
992 void wxLogDialog::OnListSelect(wxListEvent
& event
)
994 // we can't just disable the control because this looks ugly under Windows
995 // (wrong bg colour, no scrolling...), but we still want to disable
996 // selecting items - it makes no sense here
997 m_listctrl
->SetItemState(event
.GetIndex(), 0, wxLIST_STATE_SELECTED
);
1000 void wxLogDialog::OnListItemActivated(wxListEvent
& event
)
1002 // show the activated item in a message box
1003 // This allow the user to correctly display the logs which are longer
1004 // than the listctrl and thus gets truncated or those which contains
1008 // wxString str = m_listctrl->GetItemText(event.GetIndex());
1009 // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW.
1010 wxString str
= m_messages
[event
.GetIndex()];
1012 // wxMessageBox will nicely handle the '\n' in the string (if any)
1013 // and supports long strings
1014 wxMessageBox(str
, wxT("Log message"), wxOK
, this);
1017 void wxLogDialog::OnOk(wxCommandEvent
& WXUNUSED(event
))
1024 void wxLogDialog::OnSave(wxCommandEvent
& WXUNUSED(event
))
1028 int rc
= OpenLogFile(file
, NULL
, this);
1037 wxString fmt
= wxLog::GetTimestamp();
1044 size_t count
= m_messages
.GetCount();
1045 for ( size_t n
= 0; ok
&& (n
< count
); n
++ )
1048 line
<< TimeStamp(fmt
, (time_t)m_times
[n
])
1051 << wxTextFile::GetEOL();
1053 ok
= file
.Write(line
);
1060 wxLogError(_("Can't save log contents to file."));
1061 #endif // wxUSE_FILEDLG
1064 #endif // wxUSE_FILE
1066 wxLogDialog::~wxLogDialog()
1070 delete m_listctrl
->GetImageList(wxIMAGE_LIST_SMALL
);
1074 #endif // wxUSE_LOG_DIALOG
1076 #if wxUSE_FILE && wxUSE_FILEDLG
1078 // pass an uninitialized file object, the function will ask the user for the
1079 // filename and try to open it, returns true on success (file was opened),
1080 // false if file couldn't be opened/created and -1 if the file selection
1081 // dialog was cancelled
1082 static int OpenLogFile(wxFile
& file
, wxString
*pFilename
, wxWindow
*parent
)
1084 // get the file name
1085 // -----------------
1086 wxString filename
= wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent
);
1094 bool bOk
wxDUMMY_INITIALIZE(false);
1095 if ( wxFile::Exists(filename
) ) {
1096 bool bAppend
= false;
1098 strMsg
.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"),
1100 switch ( wxMessageBox(strMsg
, _("Question"),
1101 wxICON_QUESTION
| wxYES_NO
| wxCANCEL
) ) {
1114 wxFAIL_MSG(_("invalid message box return value"));
1118 bOk
= file
.Open(filename
, wxFile::write_append
);
1121 bOk
= file
.Create(filename
, true /* overwrite */);
1125 bOk
= file
.Create(filename
);
1129 *pFilename
= filename
;
1134 #endif // wxUSE_FILE
1136 #endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW)
1138 #if wxUSE_LOG && wxUSE_TEXTCTRL
1140 // ----------------------------------------------------------------------------
1141 // wxLogTextCtrl implementation
1142 // ----------------------------------------------------------------------------
1144 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl
*pTextCtrl
)
1146 m_pTextCtrl
= pTextCtrl
;
1149 void wxLogTextCtrl::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
1154 msg
<< szString
<< wxT('\n');
1155 m_pTextCtrl
->AppendText(msg
);
1158 #endif // wxUSE_LOG && wxUSE_TEXTCTRL