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