]>
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 |
d3b9f782 | 238 | gs_pFrame = NULL; |
dd85fc6b VZ |
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; | |
f946f8bb | 343 | const size_t nMsgCount = messages.size(); |
14fd5e96 | 344 | message.reserve(nMsgCount*100); |
9ad2fe62 VZ |
345 | for ( size_t n = nMsgCount; n > 0; n-- ) { |
346 | message << m_aMessages[n - 1] << wxT("\n"); | |
347 | } | |
348 | ||
349 | DoShowSingleLogMessage(message, title, style); | |
350 | #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG | |
351 | } | |
352 | ||
dd85fc6b VZ |
353 | void wxLogGui::Flush() |
354 | { | |
355 | if ( !m_bHasMessages ) | |
356 | return; | |
357 | ||
358 | // do it right now to block any new calls to Flush() while we're here | |
dabbc6a5 | 359 | m_bHasMessages = false; |
dd85fc6b | 360 | |
9ad2fe62 VZ |
361 | // note that this must be done before examining m_aMessages as it may log |
362 | // yet another message | |
dbaa16de | 363 | const unsigned repeatCount = LogLastRepeatIfNeeded(); |
f9837791 | 364 | |
9ad2fe62 | 365 | const size_t nMsgCount = m_aMessages.size(); |
f1df0927 | 366 | |
9ad2fe62 VZ |
367 | if ( repeatCount > 0 ) |
368 | { | |
369 | m_aMessages[nMsgCount - 1] << " (" << m_aMessages[nMsgCount - 2] << ")"; | |
f1df0927 | 370 | } |
1e6feb95 | 371 | |
9ad2fe62 VZ |
372 | const wxString title = GetTitle(); |
373 | const int style = GetSeverityIcon(); | |
224ff7a5 | 374 | |
69c916e9 VZ |
375 | // avoid showing other log dialogs until we're done with the dialog we're |
376 | // showing right now: nested modal dialogs make for really bad UI! | |
377 | Suspend(); | |
378 | ||
23f681ec VZ |
379 | if ( nMsgCount == 1 ) |
380 | { | |
9ad2fe62 VZ |
381 | // make a copy before calling Clear() |
382 | const wxString message(m_aMessages[0]); | |
383 | Clear(); | |
384 | ||
385 | DoShowSingleLogMessage(message, title, style); | |
23f681ec VZ |
386 | } |
387 | else // more than one message | |
388 | { | |
9ad2fe62 VZ |
389 | wxArrayString messages; |
390 | wxArrayInt severities; | |
391 | wxArrayLong times; | |
0250efd6 | 392 | |
9ad2fe62 VZ |
393 | messages.swap(m_aMessages); |
394 | severities.swap(m_aSeverity); | |
395 | times.swap(m_aTimes); | |
f1df0927 | 396 | |
23f681ec VZ |
397 | Clear(); |
398 | ||
9ad2fe62 | 399 | DoShowMultipleLogMessages(messages, severities, times, title, style); |
23f681ec | 400 | } |
69c916e9 VZ |
401 | |
402 | // allow flushing the logs again | |
403 | Resume(); | |
dd85fc6b VZ |
404 | } |
405 | ||
23f681ec | 406 | // log all kinds of messages |
5a20d2ce | 407 | void wxLogGui::DoLog(wxLogLevel level, const wxString& szString, time_t t) |
dd85fc6b VZ |
408 | { |
409 | switch ( level ) { | |
410 | case wxLOG_Info: | |
411 | if ( GetVerbose() ) | |
412 | case wxLOG_Message: | |
f1df0927 | 413 | { |
a250392c VZ |
414 | m_aMessages.Add(szString); |
415 | m_aSeverity.Add(wxLOG_Message); | |
416 | m_aTimes.Add((long)t); | |
dabbc6a5 | 417 | m_bHasMessages = true; |
f1df0927 VZ |
418 | } |
419 | break; | |
dd85fc6b VZ |
420 | |
421 | case wxLOG_Status: | |
422 | #if wxUSE_STATUSBAR | |
f1df0927 VZ |
423 | { |
424 | // find the top window and set it's status text if it has any | |
425 | wxFrame *pFrame = gs_pFrame; | |
426 | if ( pFrame == NULL ) { | |
427 | wxWindow *pWin = wxTheApp->GetTopWindow(); | |
428 | if ( pWin != NULL && pWin->IsKindOf(CLASSINFO(wxFrame)) ) { | |
429 | pFrame = (wxFrame *)pWin; | |
dd85fc6b | 430 | } |
dd85fc6b | 431 | } |
f1df0927 VZ |
432 | |
433 | if ( pFrame && pFrame->GetStatusBar() ) | |
434 | pFrame->SetStatusText(szString); | |
435 | } | |
dd85fc6b | 436 | #endif // wxUSE_STATUSBAR |
f1df0927 | 437 | break; |
dd85fc6b VZ |
438 | |
439 | case wxLOG_Trace: | |
440 | case wxLOG_Debug: | |
f1df0927 VZ |
441 | #ifdef __WXDEBUG__ |
442 | { | |
3a57b93d VZ |
443 | wxString str; |
444 | TimeStamp(&str); | |
445 | str += szString; | |
446 | ||
8a46f9b1 | 447 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) |
f1df0927 | 448 | // don't prepend debug/trace here: it goes to the |
3a57b93d VZ |
449 | // debug window anyhow |
450 | str += wxT("\r\n"); | |
e0a050e3 | 451 | OutputDebugString(str.wx_str()); |
f1df0927 VZ |
452 | #else |
453 | // send them to stderr | |
3a57b93d | 454 | wxFprintf(stderr, wxT("[%s] %s\n"), |
f1df0927 VZ |
455 | level == wxLOG_Trace ? wxT("Trace") |
456 | : wxT("Debug"), | |
3a57b93d | 457 | str.c_str()); |
f1df0927 VZ |
458 | fflush(stderr); |
459 | #endif | |
460 | } | |
461 | #endif // __WXDEBUG__ | |
dd85fc6b | 462 | |
f1df0927 | 463 | break; |
dd85fc6b VZ |
464 | |
465 | case wxLOG_FatalError: | |
f1df0927 VZ |
466 | // show this one immediately |
467 | wxMessageBox(szString, _("Fatal error"), wxICON_HAND); | |
a2a0d40d | 468 | wxExit(); |
f1df0927 | 469 | break; |
dd85fc6b VZ |
470 | |
471 | case wxLOG_Error: | |
f1df0927 VZ |
472 | if ( !m_bErrors ) { |
473 | #if !wxUSE_LOG_DIALOG | |
dd85fc6b | 474 | // discard earlier informational messages if this is the 1st |
f1df0927 VZ |
475 | // error because they might not make sense any more and showing |
476 | // them in a message box might be confusing | |
477 | m_aMessages.Empty(); | |
478 | m_aSeverity.Empty(); | |
479 | m_aTimes.Empty(); | |
480 | #endif // wxUSE_LOG_DIALOG | |
dabbc6a5 | 481 | m_bErrors = true; |
f1df0927 VZ |
482 | } |
483 | // fall through | |
dd85fc6b VZ |
484 | |
485 | case wxLOG_Warning: | |
f1df0927 VZ |
486 | if ( !m_bErrors ) { |
487 | // for the warning we don't discard the info messages | |
dabbc6a5 | 488 | m_bWarnings = true; |
f1df0927 VZ |
489 | } |
490 | ||
491 | m_aMessages.Add(szString); | |
06b466c7 | 492 | m_aSeverity.Add((int)level); |
f1df0927 | 493 | m_aTimes.Add((long)t); |
dabbc6a5 | 494 | m_bHasMessages = true; |
f1df0927 | 495 | break; |
dd85fc6b VZ |
496 | } |
497 | } | |
498 | ||
461dae94 VZ |
499 | #endif // wxUSE_LOGGUI |
500 | ||
dd85fc6b VZ |
501 | // ---------------------------------------------------------------------------- |
502 | // wxLogWindow and wxLogFrame implementation | |
503 | // ---------------------------------------------------------------------------- | |
504 | ||
c54a8b64 VZ |
505 | #if wxUSE_LOGWINDOW |
506 | ||
dd85fc6b VZ |
507 | // log frame class |
508 | // --------------- | |
509 | class wxLogFrame : public wxFrame | |
510 | { | |
511 | public: | |
512 | // ctor & dtor | |
46ff9bd5 | 513 | wxLogFrame(wxWindow *pParent, wxLogWindow *log, const wxString& szTitle); |
dd85fc6b VZ |
514 | virtual ~wxLogFrame(); |
515 | ||
516 | // menu callbacks | |
517 | void OnClose(wxCommandEvent& event); | |
518 | void OnCloseWindow(wxCloseEvent& event); | |
b6d2b072 VZ |
519 | #if CAN_SAVE_FILES |
520 | void OnSave(wxCommandEvent& event); | |
521 | #endif // CAN_SAVE_FILES | |
dd85fc6b VZ |
522 | void OnClear(wxCommandEvent& event); |
523 | ||
cd1ff792 VZ |
524 | // this function is safe to call from any thread (notice that it should be |
525 | // also called from the main thread to ensure that the messages logged from | |
526 | // it appear in correct order with the messages from the other threads) | |
527 | void AddLogMessage(const wxString& message); | |
528 | ||
529 | // actually append the messages logged from secondary threads to the text | |
530 | // control during idle time in the main thread | |
531 | virtual void OnInternalIdle(); | |
dd85fc6b VZ |
532 | |
533 | private: | |
857eb081 | 534 | // use standard ids for our commands! |
dd85fc6b VZ |
535 | enum |
536 | { | |
857eb081 VZ |
537 | Menu_Close = wxID_CLOSE, |
538 | Menu_Save = wxID_SAVE, | |
539 | Menu_Clear = wxID_CLEAR | |
dd85fc6b VZ |
540 | }; |
541 | ||
f6bcfd97 BP |
542 | // common part of OnClose() and OnCloseWindow() |
543 | void DoClose(); | |
dd85fc6b | 544 | |
cd1ff792 VZ |
545 | // do show the message in the text control |
546 | void DoShowLogMessage(const wxString& message) | |
547 | { | |
548 | m_pTextCtrl->AppendText(message); | |
549 | } | |
550 | ||
dd85fc6b VZ |
551 | wxTextCtrl *m_pTextCtrl; |
552 | wxLogWindow *m_log; | |
553 | ||
cd1ff792 VZ |
554 | // queue of messages logged from other threads which need to be displayed |
555 | wxArrayString m_pendingMessages; | |
556 | ||
557 | #if wxUSE_THREADS | |
558 | // critical section to protect access to m_pendingMessages | |
559 | wxCriticalSection m_critSection; | |
560 | #endif // wxUSE_THREADS | |
561 | ||
562 | ||
dd85fc6b | 563 | DECLARE_EVENT_TABLE() |
22f3361e | 564 | DECLARE_NO_COPY_CLASS(wxLogFrame) |
dd85fc6b VZ |
565 | }; |
566 | ||
567 | BEGIN_EVENT_TABLE(wxLogFrame, wxFrame) | |
568 | // wxLogWindow menu events | |
569 | EVT_MENU(Menu_Close, wxLogFrame::OnClose) | |
b6d2b072 | 570 | #if CAN_SAVE_FILES |
dd85fc6b | 571 | EVT_MENU(Menu_Save, wxLogFrame::OnSave) |
b6d2b072 | 572 | #endif // CAN_SAVE_FILES |
dd85fc6b VZ |
573 | EVT_MENU(Menu_Clear, wxLogFrame::OnClear) |
574 | ||
575 | EVT_CLOSE(wxLogFrame::OnCloseWindow) | |
576 | END_EVENT_TABLE() | |
577 | ||
46ff9bd5 | 578 | wxLogFrame::wxLogFrame(wxWindow *pParent, wxLogWindow *log, const wxString& szTitle) |
dabbc6a5 | 579 | : wxFrame(pParent, wxID_ANY, szTitle) |
dd85fc6b VZ |
580 | { |
581 | m_log = log; | |
582 | ||
dabbc6a5 | 583 | m_pTextCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, |
dd85fc6b VZ |
584 | wxDefaultSize, |
585 | wxTE_MULTILINE | | |
586 | wxHSCROLL | | |
46d52925 VZ |
587 | // needed for Win32 to avoid 65Kb limit but it doesn't work well |
588 | // when using RichEdit 2.0 which we always do in the Unicode build | |
589 | #if !wxUSE_UNICODE | |
590 | wxTE_RICH | | |
591 | #endif // !wxUSE_UNICODE | |
dd85fc6b VZ |
592 | wxTE_READONLY); |
593 | ||
3a8c693a | 594 | #if wxUSE_MENUS |
dd85fc6b VZ |
595 | // create menu |
596 | wxMenuBar *pMenuBar = new wxMenuBar; | |
597 | wxMenu *pMenu = new wxMenu; | |
b6d2b072 | 598 | #if CAN_SAVE_FILES |
dd85fc6b | 599 | pMenu->Append(Menu_Save, _("&Save..."), _("Save log contents to file")); |
b6d2b072 | 600 | #endif // CAN_SAVE_FILES |
dd85fc6b VZ |
601 | pMenu->Append(Menu_Clear, _("C&lear"), _("Clear the log contents")); |
602 | pMenu->AppendSeparator(); | |
603 | pMenu->Append(Menu_Close, _("&Close"), _("Close this window")); | |
604 | pMenuBar->Append(pMenu, _("&Log")); | |
605 | SetMenuBar(pMenuBar); | |
3a8c693a | 606 | #endif // wxUSE_MENUS |
dd85fc6b VZ |
607 | |
608 | #if wxUSE_STATUSBAR | |
609 | // status bar for menu prompts | |
610 | CreateStatusBar(); | |
611 | #endif // wxUSE_STATUSBAR | |
612 | ||
613 | m_log->OnFrameCreate(this); | |
614 | } | |
615 | ||
f6bcfd97 BP |
616 | void wxLogFrame::DoClose() |
617 | { | |
618 | if ( m_log->OnFrameClose(this) ) | |
619 | { | |
620 | // instead of closing just hide the window to be able to Show() it | |
621 | // later | |
dabbc6a5 | 622 | Show(false); |
f6bcfd97 BP |
623 | } |
624 | } | |
625 | ||
dd85fc6b VZ |
626 | void wxLogFrame::OnClose(wxCommandEvent& WXUNUSED(event)) |
627 | { | |
628 | DoClose(); | |
629 | } | |
630 | ||
631 | void wxLogFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
632 | { | |
633 | DoClose(); | |
634 | } | |
635 | ||
b6d2b072 | 636 | #if CAN_SAVE_FILES |
dd85fc6b VZ |
637 | void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event)) |
638 | { | |
f6bcfd97 BP |
639 | wxString filename; |
640 | wxFile file; | |
73ff84ff | 641 | int rc = OpenLogFile(file, &filename, this); |
f6bcfd97 BP |
642 | if ( rc == -1 ) |
643 | { | |
dd85fc6b VZ |
644 | // cancelled |
645 | return; | |
646 | } | |
647 | ||
f6bcfd97 | 648 | bool bOk = rc != 0; |
dd85fc6b VZ |
649 | |
650 | // retrieve text and save it | |
651 | // ------------------------- | |
652 | int nLines = m_pTextCtrl->GetNumberOfLines(); | |
653 | for ( int nLine = 0; bOk && nLine < nLines; nLine++ ) { | |
654 | bOk = file.Write(m_pTextCtrl->GetLineText(nLine) + | |
f6bcfd97 | 655 | wxTextFile::GetEOL()); |
dd85fc6b VZ |
656 | } |
657 | ||
658 | if ( bOk ) | |
659 | bOk = file.Close(); | |
660 | ||
661 | if ( !bOk ) { | |
662 | wxLogError(_("Can't save log contents to file.")); | |
663 | } | |
664 | else { | |
2523e9b7 | 665 | wxLogStatus((wxFrame*)this, _("Log saved to the file '%s'."), filename.c_str()); |
dd85fc6b VZ |
666 | } |
667 | } | |
b6d2b072 | 668 | #endif // CAN_SAVE_FILES |
dd85fc6b VZ |
669 | |
670 | void wxLogFrame::OnClear(wxCommandEvent& WXUNUSED(event)) | |
671 | { | |
672 | m_pTextCtrl->Clear(); | |
673 | } | |
674 | ||
cd1ff792 VZ |
675 | void wxLogFrame::OnInternalIdle() |
676 | { | |
677 | { | |
678 | wxCRIT_SECT_LOCKER(locker, m_critSection); | |
679 | ||
680 | const size_t count = m_pendingMessages.size(); | |
681 | for ( size_t n = 0; n < count; n++ ) | |
682 | { | |
683 | DoShowLogMessage(m_pendingMessages[n]); | |
684 | } | |
685 | ||
686 | m_pendingMessages.clear(); | |
687 | } // release m_critSection | |
688 | ||
689 | wxFrame::OnInternalIdle(); | |
690 | } | |
691 | ||
692 | void wxLogFrame::AddLogMessage(const wxString& message) | |
693 | { | |
694 | wxCRIT_SECT_LOCKER(locker, m_critSection); | |
695 | ||
696 | #if wxUSE_THREADS | |
697 | if ( !wxThread::IsMain() || !m_pendingMessages.empty() ) | |
698 | { | |
699 | // message needs to be queued for later showing | |
700 | m_pendingMessages.Add(message); | |
701 | ||
702 | wxWakeUpIdle(); | |
703 | } | |
704 | else // we are the main thread and no messages are queued, so we can | |
705 | // log the message directly | |
706 | #endif // wxUSE_THREADS | |
707 | { | |
708 | DoShowLogMessage(message); | |
709 | } | |
710 | } | |
711 | ||
dd85fc6b VZ |
712 | wxLogFrame::~wxLogFrame() |
713 | { | |
714 | m_log->OnFrameDelete(this); | |
715 | } | |
716 | ||
717 | // wxLogWindow | |
718 | // ----------- | |
03147cd0 | 719 | |
55c9a186 | 720 | wxLogWindow::wxLogWindow(wxWindow *pParent, |
46ff9bd5 | 721 | const wxString& szTitle, |
dd85fc6b VZ |
722 | bool bShow, |
723 | bool bDoPass) | |
724 | { | |
03147cd0 | 725 | PassMessages(bDoPass); |
dd85fc6b VZ |
726 | |
727 | m_pLogFrame = new wxLogFrame(pParent, this, szTitle); | |
dd85fc6b VZ |
728 | |
729 | if ( bShow ) | |
dabbc6a5 | 730 | m_pLogFrame->Show(); |
dd85fc6b VZ |
731 | } |
732 | ||
733 | void wxLogWindow::Show(bool bShow) | |
734 | { | |
735 | m_pLogFrame->Show(bShow); | |
736 | } | |
737 | ||
5a20d2ce | 738 | void wxLogWindow::DoLog(wxLogLevel level, const wxString& szString, time_t t) |
dd85fc6b VZ |
739 | { |
740 | // first let the previous logger show it | |
03147cd0 | 741 | wxLogPassThrough::DoLog(level, szString, t); |
dd85fc6b VZ |
742 | |
743 | if ( m_pLogFrame ) { | |
744 | switch ( level ) { | |
745 | case wxLOG_Status: | |
746 | // by default, these messages are ignored by wxLog, so process | |
747 | // them ourselves | |
5a20d2ce | 748 | if ( !szString.empty() ) |
dd85fc6b VZ |
749 | { |
750 | wxString str; | |
751 | str << _("Status: ") << szString; | |
5a20d2ce | 752 | LogString(str, t); |
dd85fc6b VZ |
753 | } |
754 | break; | |
755 | ||
756 | // don't put trace messages in the text window for 2 reasons: | |
757 | // 1) there are too many of them | |
758 | // 2) they may provoke other trace messages thus sending a program | |
759 | // into an infinite loop | |
760 | case wxLOG_Trace: | |
761 | break; | |
762 | ||
763 | default: | |
764 | // and this will format it nicely and call our DoLogString() | |
765 | wxLog::DoLog(level, szString, t); | |
766 | } | |
767 | } | |
dd85fc6b VZ |
768 | } |
769 | ||
5a20d2ce | 770 | void wxLogWindow::DoLogString(const wxString& szString, time_t WXUNUSED(t)) |
dd85fc6b | 771 | { |
dd85fc6b | 772 | wxString msg; |
cd1ff792 | 773 | |
dd85fc6b | 774 | TimeStamp(&msg); |
223d09f6 | 775 | msg << szString << wxT('\n'); |
dd85fc6b | 776 | |
cd1ff792 | 777 | m_pLogFrame->AddLogMessage(msg); |
dd85fc6b VZ |
778 | } |
779 | ||
780 | wxFrame *wxLogWindow::GetFrame() const | |
781 | { | |
782 | return m_pLogFrame; | |
783 | } | |
784 | ||
785 | void wxLogWindow::OnFrameCreate(wxFrame * WXUNUSED(frame)) | |
786 | { | |
787 | } | |
788 | ||
f6bcfd97 BP |
789 | bool wxLogWindow::OnFrameClose(wxFrame * WXUNUSED(frame)) |
790 | { | |
791 | // allow to close | |
dabbc6a5 | 792 | return true; |
f6bcfd97 BP |
793 | } |
794 | ||
dd85fc6b VZ |
795 | void wxLogWindow::OnFrameDelete(wxFrame * WXUNUSED(frame)) |
796 | { | |
d3b9f782 | 797 | m_pLogFrame = NULL; |
dd85fc6b VZ |
798 | } |
799 | ||
800 | wxLogWindow::~wxLogWindow() | |
801 | { | |
dd85fc6b VZ |
802 | // may be NULL if log frame already auto destroyed itself |
803 | delete m_pLogFrame; | |
804 | } | |
805 | ||
c54a8b64 VZ |
806 | #endif // wxUSE_LOGWINDOW |
807 | ||
f1df0927 VZ |
808 | // ---------------------------------------------------------------------------- |
809 | // wxLogDialog | |
810 | // ---------------------------------------------------------------------------- | |
811 | ||
812 | #if wxUSE_LOG_DIALOG | |
813 | ||
5f170f33 | 814 | wxString wxLogDialog::ms_details; |
a14670e2 | 815 | size_t wxLogDialog::ms_maxLength = 0; |
5f170f33 | 816 | |
f1df0927 VZ |
817 | wxLogDialog::wxLogDialog(wxWindow *parent, |
818 | const wxArrayString& messages, | |
819 | const wxArrayInt& severity, | |
820 | const wxArrayLong& times, | |
821 | const wxString& caption, | |
822 | long style) | |
dabbc6a5 | 823 | : wxDialog(parent, wxID_ANY, caption, |
0b165ed6 VZ |
824 | wxDefaultPosition, wxDefaultSize, |
825 | wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) | |
f1df0927 | 826 | { |
a14670e2 VZ |
827 | // init the static variables: |
828 | ||
32b13913 | 829 | if ( ms_details.empty() ) |
5f170f33 | 830 | { |
f6bcfd97 BP |
831 | // ensure that we won't loop here if wxGetTranslation() |
832 | // happens to pop up a Log message while translating this :-) | |
833 | ms_details = wxTRANSLATE("&Details"); | |
5f170f33 | 834 | ms_details = wxGetTranslation(ms_details); |
5e63b67d WS |
835 | #ifdef __SMARTPHONE__ |
836 | ms_details = wxStripMenuCodes(ms_details); | |
837 | #endif | |
5f170f33 VZ |
838 | } |
839 | ||
a14670e2 VZ |
840 | if ( ms_maxLength == 0 ) |
841 | { | |
842 | ms_maxLength = (2 * wxGetDisplaySize().x/3) / GetCharWidth(); | |
843 | } | |
844 | ||
4aff28fc VZ |
845 | size_t count = messages.GetCount(); |
846 | m_messages.Alloc(count); | |
847 | m_severity.Alloc(count); | |
848 | m_times.Alloc(count); | |
849 | ||
850 | for ( size_t n = 0; n < count; n++ ) | |
851 | { | |
a14670e2 | 852 | m_messages.Add(messages[n]); |
786c4e23 VZ |
853 | m_severity.Add(severity[n]); |
854 | m_times.Add(times[n]); | |
4aff28fc VZ |
855 | } |
856 | ||
b6d2b072 | 857 | m_listctrl = NULL; |
5e63b67d | 858 | |
94f53923 | 859 | bool isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA); |
5e63b67d | 860 | |
f1df0927 VZ |
861 | // create the controls which are always shown and layout them: we use |
862 | // sizers even though our window is not resizeable to calculate the size of | |
863 | // the dialog properly | |
864 | wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL); | |
94f53923 | 865 | wxBoxSizer *sizerAll = new wxBoxSizer(isPda ? wxVERTICAL : wxHORIZONTAL); |
f1df0927 | 866 | |
1970409e | 867 | if (!isPda) |
389d906b | 868 | { |
1970409e VZ |
869 | wxStaticBitmap *icon = new wxStaticBitmap |
870 | ( | |
871 | this, | |
872 | wxID_ANY, | |
873 | wxArtProvider::GetMessageBoxIcon(style) | |
874 | ); | |
b6d2b072 | 875 | sizerAll->Add(icon, wxSizerFlags().Centre()); |
389d906b | 876 | } |
5e63b67d | 877 | |
a14670e2 VZ |
878 | // create the text sizer with a minimal size so that we are sure it won't be too small |
879 | wxString message = EllipsizeString(messages.Last()); | |
880 | wxSizer *szText = CreateTextSizer(message); | |
881 | szText->SetMinSize(wxMin(300, wxGetDisplaySize().x / 3), -1); | |
882 | ||
b6d2b072 | 883 | sizerAll->Add(szText, wxSizerFlags(1).Centre().Border(wxLEFT | wxRIGHT)); |
a14670e2 VZ |
884 | |
885 | wxButton *btnOk = new wxButton(this, wxID_OK); | |
b6d2b072 | 886 | sizerAll->Add(btnOk, wxSizerFlags().Centre()); |
f1df0927 | 887 | |
b6d2b072 | 888 | sizerTop->Add(sizerAll, wxSizerFlags().Expand().Border()); |
f1df0927 | 889 | |
f1df0927 | 890 | |
a14670e2 | 891 | // add the details pane |
5e63b67d | 892 | #ifndef __SMARTPHONE__ |
b6d2b072 VZ |
893 | wxCollapsiblePane * const |
894 | collpane = new wxCollapsiblePane(this, wxID_ANY, ms_details); | |
895 | sizerTop->Add(collpane, wxSizerFlags(1).Expand().Border()); | |
a14670e2 VZ |
896 | |
897 | wxWindow *win = collpane->GetPane(); | |
b6d2b072 | 898 | wxSizer * const paneSz = new wxBoxSizer(wxVERTICAL); |
a14670e2 VZ |
899 | |
900 | CreateDetailsControls(win); | |
901 | ||
b6d2b072 VZ |
902 | paneSz->Add(m_listctrl, wxSizerFlags(1).Expand().Border(wxTOP)); |
903 | ||
904 | #if wxUSE_CLIPBOARD || CAN_SAVE_FILES | |
905 | wxBoxSizer * const btnSizer = new wxBoxSizer(wxHORIZONTAL); | |
906 | ||
907 | wxSizerFlags flagsBtn; | |
908 | flagsBtn.Border(wxLEFT); | |
909 | ||
910 | #if wxUSE_CLIPBOARD | |
911 | btnSizer->Add(new wxButton(win, wxID_COPY), flagsBtn); | |
912 | #endif // wxUSE_CLIPBOARD | |
a14670e2 | 913 | |
b6d2b072 VZ |
914 | #if CAN_SAVE_FILES |
915 | btnSizer->Add(new wxButton(win, wxID_SAVE), flagsBtn); | |
916 | #endif // CAN_SAVE_FILES | |
917 | ||
918 | paneSz->Add(btnSizer, wxSizerFlags().Right().Border(wxTOP)); | |
919 | #endif // wxUSE_CLIPBOARD || CAN_SAVE_FILES | |
a14670e2 VZ |
920 | |
921 | win->SetSizer(paneSz); | |
922 | paneSz->SetSizeHints(win); | |
923 | #else // __SMARTPHONE__ | |
924 | SetLeftMenu(wxID_OK); | |
925 | SetRightMenu(wxID_MORE, ms_details + EXPAND_SUFFIX); | |
926 | #endif // __SMARTPHONE__/!__SMARTPHONE__ | |
927 | ||
928 | SetSizerAndFit(sizerTop); | |
f1df0927 VZ |
929 | |
930 | Centre(); | |
5e63b67d | 931 | |
94f53923 JS |
932 | if (isPda) |
933 | { | |
934 | // Move up the screen so that when we expand the dialog, | |
935 | // there's enough space. | |
936 | Move(wxPoint(GetPosition().x, GetPosition().y / 2)); | |
937 | } | |
f1df0927 VZ |
938 | } |
939 | ||
a14670e2 | 940 | void wxLogDialog::CreateDetailsControls(wxWindow *parent) |
f6bcfd97 | 941 | { |
f6bcfd97 | 942 | // create the list ctrl now |
a14670e2 | 943 | m_listctrl = new wxListCtrl(parent, wxID_ANY, |
f6bcfd97 BP |
944 | wxDefaultPosition, wxDefaultSize, |
945 | wxSUNKEN_BORDER | | |
946 | wxLC_REPORT | | |
947 | wxLC_NO_HEADER | | |
948 | wxLC_SINGLE_SEL); | |
94f53923 | 949 | #ifdef __WXWINCE__ |
a14670e2 | 950 | // This makes a big aesthetic difference on WinCE but I |
94f53923 JS |
951 | // don't want to risk problems on other platforms |
952 | m_listctrl->Hide(); | |
5e63b67d | 953 | #endif |
f6bcfd97 BP |
954 | |
955 | // no need to translate these strings as they're not shown to the | |
956 | // user anyhow (we use wxLC_NO_HEADER style) | |
957 | m_listctrl->InsertColumn(0, _T("Message")); | |
958 | m_listctrl->InsertColumn(1, _T("Time")); | |
959 | ||
960 | // prepare the imagelist | |
961 | static const int ICON_SIZE = 16; | |
962 | wxImageList *imageList = new wxImageList(ICON_SIZE, ICON_SIZE); | |
963 | ||
964 | // order should be the same as in the switch below! | |
389d906b | 965 | static const wxChar* icons[] = |
f6bcfd97 | 966 | { |
389d906b VS |
967 | wxART_ERROR, |
968 | wxART_WARNING, | |
969 | wxART_INFORMATION | |
f6bcfd97 BP |
970 | }; |
971 | ||
dabbc6a5 | 972 | bool loadedIcons = true; |
f6bcfd97 | 973 | |
f6bcfd97 BP |
974 | for ( size_t icon = 0; icon < WXSIZEOF(icons); icon++ ) |
975 | { | |
389d906b VS |
976 | wxBitmap bmp = wxArtProvider::GetBitmap(icons[icon], wxART_MESSAGE_BOX, |
977 | wxSize(ICON_SIZE, ICON_SIZE)); | |
f6bcfd97 | 978 | |
9f14a818 VZ |
979 | // This may very well fail if there are insufficient colours available. |
980 | // Degrade gracefully. | |
981 | if ( !bmp.Ok() ) | |
982 | { | |
dabbc6a5 | 983 | loadedIcons = false; |
9f14a818 VZ |
984 | |
985 | break; | |
986 | } | |
987 | ||
389d906b | 988 | imageList->Add(bmp); |
f6bcfd97 BP |
989 | } |
990 | ||
991 | m_listctrl->SetImageList(imageList, wxIMAGE_LIST_SMALL); | |
f6bcfd97 BP |
992 | |
993 | // and fill it | |
994 | wxString fmt = wxLog::GetTimestamp(); | |
995 | if ( !fmt ) | |
996 | { | |
997 | // default format | |
c54a8b64 | 998 | fmt = _T("%c"); |
f6bcfd97 BP |
999 | } |
1000 | ||
1001 | size_t count = m_messages.GetCount(); | |
1002 | for ( size_t n = 0; n < count; n++ ) | |
1003 | { | |
9f14a818 VZ |
1004 | int image; |
1005 | ||
9f14a818 | 1006 | if ( loadedIcons ) |
f6bcfd97 | 1007 | { |
9f14a818 VZ |
1008 | switch ( m_severity[n] ) |
1009 | { | |
1010 | case wxLOG_Error: | |
1011 | image = 0; | |
1012 | break; | |
f6bcfd97 | 1013 | |
9f14a818 VZ |
1014 | case wxLOG_Warning: |
1015 | image = 1; | |
1016 | break; | |
f6bcfd97 | 1017 | |
9f14a818 VZ |
1018 | default: |
1019 | image = 2; | |
1020 | } | |
f6bcfd97 | 1021 | } |
9f14a818 | 1022 | else // failed to load images |
9f14a818 | 1023 | { |
f6bcfd97 | 1024 | image = -1; |
9f14a818 | 1025 | } |
f6bcfd97 | 1026 | |
a14670e2 VZ |
1027 | wxString msg = m_messages[n]; |
1028 | msg.Replace(wxT("\n"), wxT(" ")); | |
1029 | msg = EllipsizeString(msg); | |
1030 | ||
1031 | m_listctrl->InsertItem(n, msg, image); | |
9f14a818 | 1032 | m_listctrl->SetItem(n, 1, TimeStamp(fmt, (time_t)m_times[n])); |
f6bcfd97 BP |
1033 | } |
1034 | ||
1035 | // let the columns size themselves | |
1036 | m_listctrl->SetColumnWidth(0, wxLIST_AUTOSIZE); | |
1037 | m_listctrl->SetColumnWidth(1, wxLIST_AUTOSIZE); | |
1038 | ||
9f14a818 | 1039 | // calculate an approximately nice height for the listctrl |
7b82d782 | 1040 | int height = GetCharHeight()*(count + 4); |
f6bcfd97 | 1041 | |
9f14a818 VZ |
1042 | // but check that the dialog won't fall fown from the screen |
1043 | // | |
1044 | // we use GetMinHeight() to get the height of the dialog part without the | |
1045 | // details and we consider that the "Save" button below and the separator | |
1046 | // line (and the margins around it) take about as much, hence double it | |
1047 | int heightMax = wxGetDisplaySize().y - GetPosition().y - 2*GetMinHeight(); | |
4d509295 | 1048 | |
9f14a818 VZ |
1049 | // we should leave a margin |
1050 | heightMax *= 9; | |
1051 | heightMax /= 10; | |
4d509295 | 1052 | |
422d0ff0 | 1053 | m_listctrl->SetSize(wxDefaultCoord, wxMin(height, heightMax)); |
f6bcfd97 BP |
1054 | } |
1055 | ||
4aff28fc VZ |
1056 | void wxLogDialog::OnListSelect(wxListEvent& event) |
1057 | { | |
1058 | // we can't just disable the control because this looks ugly under Windows | |
1059 | // (wrong bg colour, no scrolling...), but we still want to disable | |
1060 | // selecting items - it makes no sense here | |
225fe9d6 | 1061 | m_listctrl->SetItemState(event.GetIndex(), 0, wxLIST_STATE_SELECTED); |
4aff28fc VZ |
1062 | } |
1063 | ||
a14670e2 VZ |
1064 | void wxLogDialog::OnListItemActivated(wxListEvent& event) |
1065 | { | |
1066 | // show the activated item in a message box | |
1067 | // This allow the user to correctly display the logs which are longer | |
1068 | // than the listctrl and thus gets truncated or those which contains | |
1069 | // newlines. | |
1070 | ||
1071 | // NB: don't do: | |
1072 | // wxString str = m_listctrl->GetItemText(event.GetIndex()); | |
1073 | // as there's a 260 chars limit on the items inside a wxListCtrl in wxMSW. | |
1074 | wxString str = m_messages[event.GetIndex()]; | |
1075 | ||
cd1ff792 | 1076 | // wxMessageBox will nicely handle the '\n' in the string (if any) |
a14670e2 VZ |
1077 | // and supports long strings |
1078 | wxMessageBox(str, wxT("Log message"), wxOK, this); | |
1079 | } | |
1080 | ||
f1df0927 VZ |
1081 | void wxLogDialog::OnOk(wxCommandEvent& WXUNUSED(event)) |
1082 | { | |
1083 | EndModal(wxID_OK); | |
1084 | } | |
1085 | ||
b6d2b072 | 1086 | #if CAN_SAVE_FILES || wxUSE_CLIPBOARD |
f6bcfd97 | 1087 | |
b6d2b072 | 1088 | wxString wxLogDialog::GetLogMessages() const |
f6bcfd97 | 1089 | { |
f6bcfd97 | 1090 | wxString fmt = wxLog::GetTimestamp(); |
b6d2b072 | 1091 | if ( fmt.empty() ) |
f6bcfd97 | 1092 | { |
b6d2b072 VZ |
1093 | // use the default format |
1094 | fmt = "%c"; | |
f6bcfd97 BP |
1095 | } |
1096 | ||
b6d2b072 VZ |
1097 | const size_t count = m_messages.GetCount(); |
1098 | ||
1099 | wxString text; | |
1100 | text.reserve(count*m_messages[0].length()); | |
1101 | for ( size_t n = 0; n < count; n++ ) | |
f6bcfd97 | 1102 | { |
b6d2b072 VZ |
1103 | text << TimeStamp(fmt, (time_t)m_times[n]) |
1104 | << ": " | |
f6bcfd97 BP |
1105 | << m_messages[n] |
1106 | << wxTextFile::GetEOL(); | |
b6d2b072 VZ |
1107 | } |
1108 | ||
1109 | return text; | |
1110 | } | |
1111 | ||
1112 | #endif // CAN_SAVE_FILES || wxUSE_CLIPBOARD | |
f6bcfd97 | 1113 | |
b6d2b072 VZ |
1114 | #if wxUSE_CLIPBOARD |
1115 | ||
1116 | void wxLogDialog::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
1117 | { | |
1118 | wxClipboardLocker clip; | |
1119 | if ( !clip || | |
1120 | !wxTheClipboard->AddData(new wxTextDataObject(GetLogMessages())) ) | |
1121 | { | |
1122 | wxLogError(_("Failed to copy dialog contents to the clipboard.")); | |
f6bcfd97 | 1123 | } |
b6d2b072 | 1124 | } |
f6bcfd97 | 1125 | |
b6d2b072 VZ |
1126 | #endif // wxUSE_CLIPBOARD |
1127 | ||
1128 | #if CAN_SAVE_FILES | |
1129 | ||
1130 | void wxLogDialog::OnSave(wxCommandEvent& WXUNUSED(event)) | |
1131 | { | |
1132 | wxFile file; | |
1133 | int rc = OpenLogFile(file, NULL, this); | |
1134 | if ( rc == -1 ) | |
1135 | { | |
1136 | // cancelled | |
1137 | return; | |
1138 | } | |
f6bcfd97 | 1139 | |
b6d2b072 | 1140 | if ( !rc || !file.Write(GetLogMessages()) || !file.Close() ) |
f6bcfd97 BP |
1141 | wxLogError(_("Can't save log contents to file.")); |
1142 | } | |
1143 | ||
b6d2b072 | 1144 | #endif // CAN_SAVE_FILES |
f6bcfd97 | 1145 | |
f1df0927 VZ |
1146 | wxLogDialog::~wxLogDialog() |
1147 | { | |
1148 | if ( m_listctrl ) | |
1149 | { | |
1150 | delete m_listctrl->GetImageList(wxIMAGE_LIST_SMALL); | |
1151 | } | |
1152 | } | |
1153 | ||
1154 | #endif // wxUSE_LOG_DIALOG | |
f6bcfd97 | 1155 | |
b6d2b072 | 1156 | #if CAN_SAVE_FILES |
f6bcfd97 BP |
1157 | |
1158 | // pass an uninitialized file object, the function will ask the user for the | |
dabbc6a5 DS |
1159 | // filename and try to open it, returns true on success (file was opened), |
1160 | // false if file couldn't be opened/created and -1 if the file selection | |
f6bcfd97 | 1161 | // dialog was cancelled |
73ff84ff | 1162 | static int OpenLogFile(wxFile& file, wxString *pFilename, wxWindow *parent) |
f6bcfd97 BP |
1163 | { |
1164 | // get the file name | |
1165 | // ----------------- | |
73ff84ff | 1166 | wxString filename = wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"), parent); |
f6bcfd97 BP |
1167 | if ( !filename ) { |
1168 | // cancelled | |
1169 | return -1; | |
1170 | } | |
1171 | ||
1172 | // open file | |
1173 | // --------- | |
e822d1bd | 1174 | bool bOk; |
f6bcfd97 | 1175 | if ( wxFile::Exists(filename) ) { |
dabbc6a5 | 1176 | bool bAppend = false; |
f6bcfd97 BP |
1177 | wxString strMsg; |
1178 | strMsg.Printf(_("Append log to file '%s' (choosing [No] will overwrite it)?"), | |
1179 | filename.c_str()); | |
1180 | switch ( wxMessageBox(strMsg, _("Question"), | |
1181 | wxICON_QUESTION | wxYES_NO | wxCANCEL) ) { | |
1182 | case wxYES: | |
dabbc6a5 | 1183 | bAppend = true; |
f6bcfd97 BP |
1184 | break; |
1185 | ||
1186 | case wxNO: | |
dabbc6a5 | 1187 | bAppend = false; |
f6bcfd97 BP |
1188 | break; |
1189 | ||
1190 | case wxCANCEL: | |
1191 | return -1; | |
1192 | ||
1193 | default: | |
1194 | wxFAIL_MSG(_("invalid message box return value")); | |
1195 | } | |
1196 | ||
1197 | if ( bAppend ) { | |
1198 | bOk = file.Open(filename, wxFile::write_append); | |
1199 | } | |
1200 | else { | |
dabbc6a5 | 1201 | bOk = file.Create(filename, true /* overwrite */); |
f6bcfd97 BP |
1202 | } |
1203 | } | |
1204 | else { | |
1205 | bOk = file.Create(filename); | |
1206 | } | |
1207 | ||
1208 | if ( pFilename ) | |
1209 | *pFilename = filename; | |
1210 | ||
1211 | return bOk; | |
1212 | } | |
1213 | ||
b6d2b072 | 1214 | #endif // CAN_SAVE_FILES |
f6bcfd97 | 1215 | |
1e6feb95 VZ |
1216 | #endif // !(wxUSE_LOGGUI || wxUSE_LOGWINDOW) |
1217 | ||
f561a4e0 | 1218 | #if wxUSE_LOG && wxUSE_TEXTCTRL |
1e6feb95 VZ |
1219 | |
1220 | // ---------------------------------------------------------------------------- | |
1221 | // wxLogTextCtrl implementation | |
1222 | // ---------------------------------------------------------------------------- | |
1223 | ||
1224 | wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl) | |
1225 | { | |
1226 | m_pTextCtrl = pTextCtrl; | |
1227 | } | |
1228 | ||
5a20d2ce | 1229 | void wxLogTextCtrl::DoLogString(const wxString& szString, time_t WXUNUSED(t)) |
1e6feb95 VZ |
1230 | { |
1231 | wxString msg; | |
1232 | TimeStamp(&msg); | |
1233 | ||
1e6feb95 | 1234 | msg << szString << wxT('\n'); |
1e6feb95 VZ |
1235 | m_pTextCtrl->AppendText(msg); |
1236 | } | |
1237 | ||
f561a4e0 | 1238 | #endif // wxUSE_LOG && wxUSE_TEXTCTRL |