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