]>
Commit | Line | Data |
---|---|---|
dd85fc6b VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: 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 license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | // ============================================================================ | |
14 | // declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
21 | // no #pragma implementation "log.h" because it's already in src/common/log.cpp | |
22 | ||
23 | // For compilers that support precompilation, includes "wx.h". | |
24 | #include "wx/wxprec.h" | |
25 | ||
26 | #ifdef __BORLANDC__ | |
27 | #pragma hdrstop | |
28 | #endif | |
29 | ||
e90c1d2a VZ |
30 | #if !wxUSE_GUI |
31 | #error "This file can't be compiled without GUI!" | |
dd85fc6b VZ |
32 | #endif |
33 | ||
34 | #ifndef WX_PRECOMP | |
35 | #include "wx/app.h" | |
36 | #include "wx/intl.h" | |
37 | #include "wx/log.h" | |
38 | #include "wx/menu.h" | |
39 | #include "wx/frame.h" | |
40 | #include "wx/filedlg.h" | |
8ca2f11c | 41 | #include "wx/msgdlg.h" |
dd85fc6b | 42 | #include "wx/textctrl.h" |
f1df0927 VZ |
43 | #include "wx/sizer.h" |
44 | #include "wx/statbmp.h" | |
dd85fc6b VZ |
45 | #endif // WX_PRECOMP |
46 | ||
47 | #include "wx/file.h" | |
48 | #include "wx/textfile.h" | |
49 | ||
39a7c7e1 VZ |
50 | #ifdef __WXMSW__ |
51 | // for OutputDebugString() | |
52 | #include "wx/msw/private.h" | |
53 | #endif // Windows | |
54 | ||
f1df0927 VZ |
55 | // may be defined to 0 for old behavior (using wxMessageBox) - shouldn't be |
56 | // changed normally (that's why it's here and not in setup.h) | |
57 | #define wxUSE_LOG_DIALOG 1 | |
58 | ||
59 | #if wxUSE_LOG_DIALOG | |
60 | #include "wx/datetime.h" | |
61 | #include "wx/listctrl.h" | |
de8e98f1 | 62 | #include "wx/imaglist.h" |
f1df0927 VZ |
63 | #include "wx/image.h" |
64 | #else // !wxUSE_TEXTFILE | |
65 | #include "wx/msgdlg.h" | |
66 | #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG | |
67 | ||
68 | // ---------------------------------------------------------------------------- | |
69 | // private classes | |
70 | // ---------------------------------------------------------------------------- | |
71 | ||
72 | #if wxUSE_LOG_DIALOG | |
73 | ||
74 | class wxLogDialog : public wxDialog | |
75 | { | |
76 | public: | |
77 | wxLogDialog(wxWindow *parent, | |
78 | const wxArrayString& messages, | |
79 | const wxArrayInt& severity, | |
80 | const wxArrayLong& timess, | |
81 | const wxString& caption, | |
82 | long style); | |
83 | virtual ~wxLogDialog(); | |
84 | ||
85 | // event handlers | |
86 | void OnOk(wxCommandEvent& event); | |
87 | void OnDetails(wxCommandEvent& event); | |
4aff28fc | 88 | void OnListSelect(wxListEvent& event); |
f1df0927 VZ |
89 | |
90 | private: | |
91 | // the data for the listctrl | |
224ff7a5 VZ |
92 | wxArrayString m_messages; |
93 | wxArrayInt m_severity; | |
94 | wxArrayLong m_times; | |
f1df0927 VZ |
95 | |
96 | // the "toggle" button and its state | |
97 | wxButton *m_btnDetails; | |
98 | bool m_showingDetails; | |
99 | ||
100 | // the listctrl (not shown initially) | |
101 | wxListCtrl *m_listctrl; | |
102 | ||
5f170f33 VZ |
103 | // the translated "Details" string |
104 | static wxString ms_details; | |
105 | ||
f1df0927 VZ |
106 | DECLARE_EVENT_TABLE() |
107 | }; | |
108 | ||
109 | BEGIN_EVENT_TABLE(wxLogDialog, wxDialog) | |
110 | EVT_BUTTON(wxID_OK, wxLogDialog::OnOk) | |
111 | EVT_BUTTON(wxID_MORE, wxLogDialog::OnDetails) | |
4aff28fc | 112 | EVT_LIST_ITEM_SELECTED(-1, wxLogDialog::OnListSelect) |
f1df0927 VZ |
113 | END_EVENT_TABLE() |
114 | ||
115 | #endif // wxUSE_LOG_DIALOG | |
116 | ||
dd85fc6b VZ |
117 | // ---------------------------------------------------------------------------- |
118 | // global variables | |
119 | // ---------------------------------------------------------------------------- | |
120 | ||
121 | // we use a global variable to store the frame pointer for wxLogStatus - bad, | |
122 | // but it's he easiest way | |
123 | static wxFrame *gs_pFrame; // FIXME MT-unsafe | |
124 | ||
125 | // ============================================================================ | |
126 | // implementation | |
127 | // ============================================================================ | |
128 | ||
129 | // ---------------------------------------------------------------------------- | |
130 | // global functions | |
131 | // ---------------------------------------------------------------------------- | |
132 | ||
133 | // accepts an additional argument which tells to which frame the output should | |
134 | // be directed | |
135 | void wxLogStatus(wxFrame *pFrame, const wxChar *szFormat, ...) | |
136 | { | |
137 | wxString msg; | |
138 | ||
139 | wxLog *pLog = wxLog::GetActiveTarget(); | |
140 | if ( pLog != NULL ) { | |
141 | va_list argptr; | |
142 | va_start(argptr, szFormat); | |
143 | msg.PrintfV(szFormat, argptr); | |
144 | va_end(argptr); | |
145 | ||
146 | wxASSERT( gs_pFrame == NULL ); // should be reset! | |
147 | gs_pFrame = pFrame; | |
148 | wxLog::OnLog(wxLOG_Status, msg, time(NULL)); | |
149 | gs_pFrame = (wxFrame *) NULL; | |
150 | } | |
151 | } | |
152 | ||
153 | // ---------------------------------------------------------------------------- | |
154 | // wxLogTextCtrl implementation | |
155 | // ---------------------------------------------------------------------------- | |
156 | ||
157 | wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl) | |
158 | { | |
159 | m_pTextCtrl = pTextCtrl; | |
160 | } | |
161 | ||
5e0201ea | 162 | void wxLogTextCtrl::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
dd85fc6b VZ |
163 | { |
164 | wxString msg; | |
165 | TimeStamp(&msg); | |
223d09f6 | 166 | msg << szString << wxT('\n'); |
dd85fc6b VZ |
167 | |
168 | m_pTextCtrl->AppendText(msg); | |
169 | } | |
170 | ||
171 | // ---------------------------------------------------------------------------- | |
172 | // wxLogGui implementation (FIXME MT-unsafe) | |
173 | // ---------------------------------------------------------------------------- | |
174 | ||
175 | wxLogGui::wxLogGui() | |
176 | { | |
5f170f33 VZ |
177 | // we must translate them here in the very beginning or we risk to have |
178 | // reentrancy problems when called from inside wxGetTranslation() leading | |
179 | // to inifnite recursion | |
180 | m_error = _("Error"); | |
181 | m_warning = _("Warning"); | |
182 | m_info = _("Information"); | |
183 | ||
dd85fc6b VZ |
184 | Clear(); |
185 | } | |
186 | ||
187 | void wxLogGui::Clear() | |
188 | { | |
5f170f33 VZ |
189 | m_bErrors = |
190 | m_bWarnings = | |
191 | m_bHasMessages = FALSE; | |
192 | ||
dd85fc6b | 193 | m_aMessages.Empty(); |
f1df0927 | 194 | m_aSeverity.Empty(); |
dd85fc6b VZ |
195 | m_aTimes.Empty(); |
196 | } | |
197 | ||
198 | void wxLogGui::Flush() | |
199 | { | |
200 | if ( !m_bHasMessages ) | |
201 | return; | |
202 | ||
203 | // do it right now to block any new calls to Flush() while we're here | |
204 | m_bHasMessages = FALSE; | |
205 | ||
f1df0927 VZ |
206 | wxString title = wxTheApp->GetAppName(); |
207 | if ( !!title ) | |
208 | { | |
209 | title[0u] = wxToupper(title[0u]); | |
210 | title += _T(' '); | |
211 | } | |
212 | ||
213 | long style; | |
214 | if ( m_bErrors ) { | |
5f170f33 | 215 | title += m_error; |
f1df0927 VZ |
216 | style = wxICON_STOP; |
217 | } | |
218 | else if ( m_bWarnings ) { | |
5f170f33 | 219 | title += m_warning; |
f1df0927 VZ |
220 | style = wxICON_EXCLAMATION; |
221 | } | |
222 | else { | |
5f170f33 | 223 | title += m_info; |
f1df0927 VZ |
224 | style = wxICON_INFORMATION; |
225 | } | |
226 | ||
23f681ec VZ |
227 | // this is the best we can do here |
228 | wxWindow *parent = wxTheApp->GetTopWindow(); | |
224ff7a5 | 229 | |
23f681ec | 230 | size_t nMsgCount = m_aMessages.Count(); |
224ff7a5 | 231 | |
23f681ec VZ |
232 | wxString str; |
233 | if ( nMsgCount == 1 ) | |
234 | { | |
235 | str = m_aMessages[0]; | |
236 | } | |
237 | else // more than one message | |
238 | { | |
239 | #if wxUSE_LOG_DIALOG | |
240 | wxLogDialog dlg(parent, | |
241 | m_aMessages, m_aSeverity, m_aTimes, | |
242 | title, style); | |
f1df0927 | 243 | |
23f681ec VZ |
244 | // clear the message list before showing the dialog because while it's |
245 | // shown some new messages may appear | |
246 | Clear(); | |
247 | ||
248 | (void)dlg.ShowModal(); | |
f1df0927 | 249 | #else // !wxUSE_LOG_DIALOG |
23f681ec VZ |
250 | // concatenate all strings (but not too many to not overfill the msg box) |
251 | size_t nLines = 0; | |
dd85fc6b | 252 | |
23f681ec VZ |
253 | // start from the most recent message |
254 | for ( size_t n = nMsgCount; n > 0; n-- ) { | |
255 | // for Windows strings longer than this value are wrapped (NT 4.0) | |
256 | const size_t nMsgLineWidth = 156; | |
dd85fc6b | 257 | |
23f681ec | 258 | nLines += (m_aMessages[n - 1].Len() + nMsgLineWidth - 1) / nMsgLineWidth; |
dd85fc6b | 259 | |
23f681ec VZ |
260 | if ( nLines > 25 ) // don't put too many lines in message box |
261 | break; | |
dd85fc6b | 262 | |
23f681ec VZ |
263 | str << m_aMessages[n - 1] << wxT("\n"); |
264 | } | |
265 | #endif // wxUSE_LOG_DIALOG/!wxUSE_LOG_DIALOG | |
dd85fc6b VZ |
266 | } |
267 | ||
23f681ec VZ |
268 | // this catches both cases of 1 message with wxUSE_LOG_DIALOG and any |
269 | // situation without it | |
270 | if ( !!str ) | |
271 | { | |
272 | wxMessageBox(str, title, wxOK | style, parent); | |
dd85fc6b | 273 | |
23f681ec VZ |
274 | // no undisplayed messages whatsoever |
275 | Clear(); | |
276 | } | |
dd85fc6b VZ |
277 | } |
278 | ||
23f681ec | 279 | // log all kinds of messages |
dd85fc6b VZ |
280 | void wxLogGui::DoLog(wxLogLevel level, const wxChar *szString, time_t t) |
281 | { | |
282 | switch ( level ) { | |
283 | case wxLOG_Info: | |
284 | if ( GetVerbose() ) | |
285 | case wxLOG_Message: | |
f1df0927 | 286 | { |
dd85fc6b VZ |
287 | if ( !m_bErrors ) { |
288 | m_aMessages.Add(szString); | |
f1df0927 | 289 | m_aSeverity.Add(wxLOG_Message); |
dd85fc6b VZ |
290 | m_aTimes.Add((long)t); |
291 | m_bHasMessages = TRUE; | |
292 | } | |
f1df0927 VZ |
293 | } |
294 | break; | |
dd85fc6b VZ |
295 | |
296 | case wxLOG_Status: | |
297 | #if wxUSE_STATUSBAR | |
f1df0927 VZ |
298 | { |
299 | // find the top window and set it's status text if it has any | |
300 | wxFrame *pFrame = gs_pFrame; | |
301 | if ( pFrame == NULL ) { | |
302 | wxWindow *pWin = wxTheApp->GetTopWindow(); | |
303 | if ( pWin != NULL && pWin->IsKindOf(CLASSINFO(wxFrame)) ) { | |
304 | pFrame = (wxFrame *)pWin; | |
dd85fc6b | 305 | } |
dd85fc6b | 306 | } |
f1df0927 VZ |
307 | |
308 | if ( pFrame && pFrame->GetStatusBar() ) | |
309 | pFrame->SetStatusText(szString); | |
310 | } | |
dd85fc6b | 311 | #endif // wxUSE_STATUSBAR |
f1df0927 | 312 | break; |
dd85fc6b VZ |
313 | |
314 | case wxLOG_Trace: | |
315 | case wxLOG_Debug: | |
f1df0927 VZ |
316 | #ifdef __WXDEBUG__ |
317 | { | |
318 | #ifdef __WXMSW__ | |
319 | // don't prepend debug/trace here: it goes to the | |
320 | // debug window anyhow, but do put a timestamp | |
321 | wxString str; | |
322 | TimeStamp(&str); | |
323 | str << szString << wxT("\n\r"); | |
324 | OutputDebugString(str); | |
325 | #else | |
326 | // send them to stderr | |
327 | wxFprintf(stderr, wxT("%s: %s\n"), | |
328 | level == wxLOG_Trace ? wxT("Trace") | |
329 | : wxT("Debug"), | |
330 | szString); | |
331 | fflush(stderr); | |
332 | #endif | |
333 | } | |
334 | #endif // __WXDEBUG__ | |
dd85fc6b | 335 | |
f1df0927 | 336 | break; |
dd85fc6b VZ |
337 | |
338 | case wxLOG_FatalError: | |
f1df0927 VZ |
339 | // show this one immediately |
340 | wxMessageBox(szString, _("Fatal error"), wxICON_HAND); | |
a2a0d40d | 341 | wxExit(); |
f1df0927 | 342 | break; |
dd85fc6b VZ |
343 | |
344 | case wxLOG_Error: | |
f1df0927 VZ |
345 | if ( !m_bErrors ) { |
346 | #if !wxUSE_LOG_DIALOG | |
dd85fc6b | 347 | // discard earlier informational messages if this is the 1st |
f1df0927 VZ |
348 | // error because they might not make sense any more and showing |
349 | // them in a message box might be confusing | |
350 | m_aMessages.Empty(); | |
351 | m_aSeverity.Empty(); | |
352 | m_aTimes.Empty(); | |
353 | #endif // wxUSE_LOG_DIALOG | |
354 | m_bErrors = TRUE; | |
355 | } | |
356 | // fall through | |
dd85fc6b VZ |
357 | |
358 | case wxLOG_Warning: | |
f1df0927 VZ |
359 | if ( !m_bErrors ) { |
360 | // for the warning we don't discard the info messages | |
361 | m_bWarnings = TRUE; | |
362 | } | |
363 | ||
364 | m_aMessages.Add(szString); | |
06b466c7 | 365 | m_aSeverity.Add((int)level); |
f1df0927 VZ |
366 | m_aTimes.Add((long)t); |
367 | m_bHasMessages = TRUE; | |
368 | break; | |
dd85fc6b VZ |
369 | } |
370 | } | |
371 | ||
372 | // ---------------------------------------------------------------------------- | |
373 | // wxLogWindow and wxLogFrame implementation | |
374 | // ---------------------------------------------------------------------------- | |
375 | ||
376 | // log frame class | |
377 | // --------------- | |
378 | class wxLogFrame : public wxFrame | |
379 | { | |
380 | public: | |
381 | // ctor & dtor | |
382 | wxLogFrame(wxFrame *pParent, wxLogWindow *log, const wxChar *szTitle); | |
383 | virtual ~wxLogFrame(); | |
384 | ||
385 | // menu callbacks | |
386 | void OnClose(wxCommandEvent& event); | |
387 | void OnCloseWindow(wxCloseEvent& event); | |
388 | #if wxUSE_FILE | |
389 | void OnSave (wxCommandEvent& event); | |
390 | #endif // wxUSE_FILE | |
391 | void OnClear(wxCommandEvent& event); | |
392 | ||
393 | void OnIdle(wxIdleEvent&); | |
394 | ||
395 | // accessors | |
396 | wxTextCtrl *TextCtrl() const { return m_pTextCtrl; } | |
397 | ||
398 | private: | |
857eb081 | 399 | // use standard ids for our commands! |
dd85fc6b VZ |
400 | enum |
401 | { | |
857eb081 VZ |
402 | Menu_Close = wxID_CLOSE, |
403 | Menu_Save = wxID_SAVE, | |
404 | Menu_Clear = wxID_CLEAR | |
dd85fc6b VZ |
405 | }; |
406 | ||
407 | // instead of closing just hide the window to be able to Show() it later | |
408 | void DoClose() { Show(FALSE); } | |
409 | ||
410 | wxTextCtrl *m_pTextCtrl; | |
411 | wxLogWindow *m_log; | |
412 | ||
413 | DECLARE_EVENT_TABLE() | |
414 | }; | |
415 | ||
416 | BEGIN_EVENT_TABLE(wxLogFrame, wxFrame) | |
417 | // wxLogWindow menu events | |
418 | EVT_MENU(Menu_Close, wxLogFrame::OnClose) | |
419 | #if wxUSE_FILE | |
420 | EVT_MENU(Menu_Save, wxLogFrame::OnSave) | |
421 | #endif // wxUSE_FILE | |
422 | EVT_MENU(Menu_Clear, wxLogFrame::OnClear) | |
423 | ||
424 | EVT_CLOSE(wxLogFrame::OnCloseWindow) | |
425 | END_EVENT_TABLE() | |
426 | ||
427 | wxLogFrame::wxLogFrame(wxFrame *pParent, wxLogWindow *log, const wxChar *szTitle) | |
428 | : wxFrame(pParent, -1, szTitle) | |
429 | { | |
430 | m_log = log; | |
431 | ||
432 | m_pTextCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition, | |
433 | wxDefaultSize, | |
434 | wxTE_MULTILINE | | |
435 | wxHSCROLL | | |
436 | wxTE_READONLY); | |
437 | ||
438 | // create menu | |
439 | wxMenuBar *pMenuBar = new wxMenuBar; | |
440 | wxMenu *pMenu = new wxMenu; | |
441 | #if wxUSE_FILE | |
442 | pMenu->Append(Menu_Save, _("&Save..."), _("Save log contents to file")); | |
443 | #endif // wxUSE_FILE | |
444 | pMenu->Append(Menu_Clear, _("C&lear"), _("Clear the log contents")); | |
445 | pMenu->AppendSeparator(); | |
446 | pMenu->Append(Menu_Close, _("&Close"), _("Close this window")); | |
447 | pMenuBar->Append(pMenu, _("&Log")); | |
448 | SetMenuBar(pMenuBar); | |
449 | ||
450 | #if wxUSE_STATUSBAR | |
451 | // status bar for menu prompts | |
452 | CreateStatusBar(); | |
453 | #endif // wxUSE_STATUSBAR | |
454 | ||
455 | m_log->OnFrameCreate(this); | |
456 | } | |
457 | ||
458 | void wxLogFrame::OnClose(wxCommandEvent& WXUNUSED(event)) | |
459 | { | |
460 | DoClose(); | |
461 | } | |
462 | ||
463 | void wxLogFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
464 | { | |
465 | DoClose(); | |
466 | } | |
467 | ||
468 | #if wxUSE_FILE | |
469 | void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event)) | |
470 | { | |
471 | // get the file name | |
472 | // ----------------- | |
223d09f6 | 473 | const wxChar *szFileName = wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt")); |
dd85fc6b VZ |
474 | if ( szFileName == NULL ) { |
475 | // cancelled | |
476 | return; | |
477 | } | |
478 | ||
479 | // open file | |
480 | // --------- | |
481 | wxFile file; | |
482 | bool bOk = FALSE; | |
483 | if ( wxFile::Exists(szFileName) ) { | |
484 | bool bAppend = FALSE; | |
485 | wxString strMsg; | |
486 | strMsg.Printf(_("Append log to file '%s' " | |
487 | "(choosing [No] will overwrite it)?"), szFileName); | |
488 | switch ( wxMessageBox(strMsg, _("Question"), wxYES_NO | wxCANCEL) ) { | |
489 | case wxYES: | |
490 | bAppend = TRUE; | |
491 | break; | |
492 | ||
493 | case wxNO: | |
494 | bAppend = FALSE; | |
495 | break; | |
496 | ||
497 | case wxCANCEL: | |
498 | return; | |
499 | ||
500 | default: | |
501 | wxFAIL_MSG(_("invalid message box return value")); | |
502 | } | |
503 | ||
504 | if ( bAppend ) { | |
505 | bOk = file.Open(szFileName, wxFile::write_append); | |
506 | } | |
507 | else { | |
508 | bOk = file.Create(szFileName, TRUE /* overwrite */); | |
509 | } | |
510 | } | |
511 | else { | |
512 | bOk = file.Create(szFileName); | |
513 | } | |
514 | ||
515 | // retrieve text and save it | |
516 | // ------------------------- | |
517 | int nLines = m_pTextCtrl->GetNumberOfLines(); | |
518 | for ( int nLine = 0; bOk && nLine < nLines; nLine++ ) { | |
519 | bOk = file.Write(m_pTextCtrl->GetLineText(nLine) + | |
520 | // we're not going to pull in the whole wxTextFile if all we need is this... | |
521 | #if wxUSE_TEXTFILE | |
522 | wxTextFile::GetEOL() | |
523 | #else // !wxUSE_TEXTFILE | |
524 | '\n' | |
525 | #endif // wxUSE_TEXTFILE | |
526 | ); | |
527 | } | |
528 | ||
529 | if ( bOk ) | |
530 | bOk = file.Close(); | |
531 | ||
532 | if ( !bOk ) { | |
533 | wxLogError(_("Can't save log contents to file.")); | |
534 | } | |
535 | else { | |
536 | wxLogStatus(this, _("Log saved to the file '%s'."), szFileName); | |
537 | } | |
538 | } | |
539 | #endif // wxUSE_FILE | |
540 | ||
541 | void wxLogFrame::OnClear(wxCommandEvent& WXUNUSED(event)) | |
542 | { | |
543 | m_pTextCtrl->Clear(); | |
544 | } | |
545 | ||
546 | wxLogFrame::~wxLogFrame() | |
547 | { | |
548 | m_log->OnFrameDelete(this); | |
549 | } | |
550 | ||
551 | // wxLogWindow | |
552 | // ----------- | |
553 | wxLogWindow::wxLogWindow(wxFrame *pParent, | |
554 | const wxChar *szTitle, | |
555 | bool bShow, | |
556 | bool bDoPass) | |
557 | { | |
558 | m_bPassMessages = bDoPass; | |
559 | ||
560 | m_pLogFrame = new wxLogFrame(pParent, this, szTitle); | |
561 | m_pOldLog = wxLog::SetActiveTarget(this); | |
562 | ||
563 | if ( bShow ) | |
564 | m_pLogFrame->Show(TRUE); | |
565 | } | |
566 | ||
567 | void wxLogWindow::Show(bool bShow) | |
568 | { | |
569 | m_pLogFrame->Show(bShow); | |
570 | } | |
571 | ||
572 | void wxLogWindow::Flush() | |
573 | { | |
574 | if ( m_pOldLog != NULL ) | |
575 | m_pOldLog->Flush(); | |
576 | ||
577 | m_bHasMessages = FALSE; | |
578 | } | |
579 | ||
580 | void wxLogWindow::DoLog(wxLogLevel level, const wxChar *szString, time_t t) | |
581 | { | |
582 | // first let the previous logger show it | |
583 | if ( m_pOldLog != NULL && m_bPassMessages ) { | |
f1df0927 | 584 | // bogus cast just to access protected DoLog |
dd85fc6b VZ |
585 | ((wxLogWindow *)m_pOldLog)->DoLog(level, szString, t); |
586 | } | |
587 | ||
588 | if ( m_pLogFrame ) { | |
589 | switch ( level ) { | |
590 | case wxLOG_Status: | |
591 | // by default, these messages are ignored by wxLog, so process | |
592 | // them ourselves | |
593 | if ( !wxIsEmpty(szString) ) | |
594 | { | |
595 | wxString str; | |
596 | str << _("Status: ") << szString; | |
597 | DoLogString(str, t); | |
598 | } | |
599 | break; | |
600 | ||
601 | // don't put trace messages in the text window for 2 reasons: | |
602 | // 1) there are too many of them | |
603 | // 2) they may provoke other trace messages thus sending a program | |
604 | // into an infinite loop | |
605 | case wxLOG_Trace: | |
606 | break; | |
607 | ||
608 | default: | |
609 | // and this will format it nicely and call our DoLogString() | |
610 | wxLog::DoLog(level, szString, t); | |
611 | } | |
612 | } | |
613 | ||
614 | m_bHasMessages = TRUE; | |
615 | } | |
616 | ||
617 | void wxLogWindow::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) | |
618 | { | |
619 | // put the text into our window | |
620 | wxTextCtrl *pText = m_pLogFrame->TextCtrl(); | |
621 | ||
622 | // remove selection (WriteText is in fact ReplaceSelection) | |
623 | #ifdef __WXMSW__ | |
624 | long nLen = pText->GetLastPosition(); | |
625 | pText->SetSelection(nLen, nLen); | |
626 | #endif // Windows | |
627 | ||
628 | wxString msg; | |
629 | TimeStamp(&msg); | |
223d09f6 | 630 | msg << szString << wxT('\n'); |
dd85fc6b VZ |
631 | |
632 | pText->AppendText(msg); | |
633 | ||
634 | // TODO ensure that the line can be seen | |
635 | } | |
636 | ||
637 | wxFrame *wxLogWindow::GetFrame() const | |
638 | { | |
639 | return m_pLogFrame; | |
640 | } | |
641 | ||
642 | void wxLogWindow::OnFrameCreate(wxFrame * WXUNUSED(frame)) | |
643 | { | |
644 | } | |
645 | ||
646 | void wxLogWindow::OnFrameDelete(wxFrame * WXUNUSED(frame)) | |
647 | { | |
648 | m_pLogFrame = (wxLogFrame *)NULL; | |
649 | } | |
650 | ||
651 | wxLogWindow::~wxLogWindow() | |
652 | { | |
653 | delete m_pOldLog; | |
654 | ||
655 | // may be NULL if log frame already auto destroyed itself | |
656 | delete m_pLogFrame; | |
657 | } | |
658 | ||
f1df0927 VZ |
659 | // ---------------------------------------------------------------------------- |
660 | // wxLogDialog | |
661 | // ---------------------------------------------------------------------------- | |
662 | ||
663 | #if wxUSE_LOG_DIALOG | |
664 | ||
665 | static const size_t MARGIN = 10; | |
666 | ||
5f170f33 VZ |
667 | wxString wxLogDialog::ms_details; |
668 | ||
f1df0927 VZ |
669 | wxLogDialog::wxLogDialog(wxWindow *parent, |
670 | const wxArrayString& messages, | |
671 | const wxArrayInt& severity, | |
672 | const wxArrayLong& times, | |
673 | const wxString& caption, | |
674 | long style) | |
4aff28fc | 675 | : wxDialog(parent, -1, caption ) |
f1df0927 | 676 | { |
669f7a11 | 677 | if ( ms_details.IsEmpty() ) |
5f170f33 VZ |
678 | { |
679 | // ensure that we won't try to call wxGetTranslation() twice | |
680 | ms_details = _T("&Details"); | |
681 | ms_details = wxGetTranslation(ms_details); | |
682 | } | |
683 | ||
4aff28fc VZ |
684 | size_t count = messages.GetCount(); |
685 | m_messages.Alloc(count); | |
686 | m_severity.Alloc(count); | |
687 | m_times.Alloc(count); | |
688 | ||
689 | for ( size_t n = 0; n < count; n++ ) | |
690 | { | |
691 | wxString msg = messages[n]; | |
692 | do | |
693 | { | |
694 | m_messages.Add(msg.BeforeFirst(_T('\n'))); | |
695 | msg = msg.AfterFirst(_T('\n')); | |
696 | ||
697 | m_severity.Add(severity[n]); | |
698 | m_times.Add(times[n]); | |
699 | } | |
700 | while ( !!msg ); | |
701 | } | |
702 | ||
f1df0927 VZ |
703 | m_showingDetails = FALSE; // not initially |
704 | m_listctrl = (wxListCtrl *)NULL; | |
705 | ||
706 | // create the controls which are always shown and layout them: we use | |
707 | // sizers even though our window is not resizeable to calculate the size of | |
708 | // the dialog properly | |
709 | wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL); | |
710 | wxBoxSizer *sizerButtons = new wxBoxSizer(wxVERTICAL); | |
711 | wxBoxSizer *sizerAll = new wxBoxSizer(wxHORIZONTAL); | |
712 | ||
5f170f33 | 713 | wxButton *btnOk = new wxButton(this, wxID_OK, _("OK")); |
f1df0927 | 714 | sizerButtons->Add(btnOk, 0, wxCENTRE|wxBOTTOM, MARGIN/2); |
5f170f33 | 715 | m_btnDetails = new wxButton(this, wxID_MORE, ms_details + _T(" >>")); |
f1df0927 VZ |
716 | sizerButtons->Add(m_btnDetails, 0, wxCENTRE|wxTOP, MARGIN/2 - 1); |
717 | ||
669f7a11 | 718 | #ifndef __WIN16__ |
06b466c7 | 719 | wxIcon icon = wxTheApp->GetStdIcon((int)(style & wxICON_MASK)); |
f1df0927 | 720 | sizerAll->Add(new wxStaticBitmap(this, -1, icon), 0, wxCENTRE); |
669f7a11 JS |
721 | #endif |
722 | ||
f1df0927 VZ |
723 | const wxString& message = messages.Last(); |
724 | sizerAll->Add(CreateTextSizer(message), 0, wxCENTRE|wxLEFT|wxRIGHT, MARGIN); | |
725 | sizerAll->Add(sizerButtons, 0, wxALIGN_RIGHT|wxLEFT, MARGIN); | |
726 | ||
727 | sizerTop->Add(sizerAll, 0, wxCENTRE|wxALL, MARGIN); | |
728 | ||
729 | SetAutoLayout(TRUE); | |
730 | SetSizer(sizerTop); | |
731 | ||
732 | sizerTop->SetSizeHints(this); | |
733 | sizerTop->Fit(this); | |
734 | ||
735 | btnOk->SetFocus(); | |
736 | ||
23f681ec VZ |
737 | // this can't happen any more as we don't use this dialog in this case |
738 | #if 0 | |
4aff28fc | 739 | if ( count == 1 ) |
50af7fa9 VZ |
740 | { |
741 | // no details... it's easier to disable a button than to change the | |
742 | // dialog layout depending on whether we have details or not | |
743 | m_btnDetails->Disable(); | |
744 | } | |
23f681ec | 745 | #endif // 0 |
50af7fa9 | 746 | |
f1df0927 VZ |
747 | Centre(); |
748 | } | |
749 | ||
4aff28fc VZ |
750 | void wxLogDialog::OnListSelect(wxListEvent& event) |
751 | { | |
752 | // we can't just disable the control because this looks ugly under Windows | |
753 | // (wrong bg colour, no scrolling...), but we still want to disable | |
754 | // selecting items - it makes no sense here | |
225fe9d6 | 755 | m_listctrl->SetItemState(event.GetIndex(), 0, wxLIST_STATE_SELECTED); |
4aff28fc VZ |
756 | } |
757 | ||
f1df0927 VZ |
758 | void wxLogDialog::OnOk(wxCommandEvent& WXUNUSED(event)) |
759 | { | |
760 | EndModal(wxID_OK); | |
761 | } | |
762 | ||
763 | void wxLogDialog::OnDetails(wxCommandEvent& WXUNUSED(event)) | |
764 | { | |
765 | wxSizer *sizer = GetSizer(); | |
766 | ||
767 | if ( m_showingDetails ) | |
768 | { | |
5f170f33 | 769 | m_btnDetails->SetLabel(ms_details + _T(">>")); |
f1df0927 VZ |
770 | |
771 | sizer->Remove(m_listctrl); | |
772 | } | |
773 | else // show details now | |
774 | { | |
5f170f33 | 775 | m_btnDetails->SetLabel(wxString(_T("<< ")) + ms_details); |
f1df0927 VZ |
776 | |
777 | if ( !m_listctrl ) | |
778 | { | |
779 | // create it now | |
780 | m_listctrl = new wxListCtrl(this, -1, | |
781 | wxDefaultPosition, wxDefaultSize, | |
224ff7a5 VZ |
782 | wxSUNKEN_BORDER | |
783 | wxLC_REPORT | | |
4aff28fc VZ |
784 | wxLC_NO_HEADER | |
785 | wxLC_SINGLE_SEL); | |
5f170f33 VZ |
786 | // no need to translate these strings as they're not shown to the |
787 | // user anyhow (we use wxLC_NO_HEADER style) | |
788 | m_listctrl->InsertColumn(0, _T("Message")); | |
789 | m_listctrl->InsertColumn(1, _T("Time")); | |
f1df0927 VZ |
790 | |
791 | // prepare the imagelist | |
792 | static const int ICON_SIZE = 16; | |
793 | wxImageList *imageList = new wxImageList(ICON_SIZE, ICON_SIZE); | |
794 | ||
795 | // order should be the same as in the switch below! | |
796 | static const int icons[] = | |
797 | { | |
798 | wxICON_ERROR, | |
799 | wxICON_EXCLAMATION, | |
800 | wxICON_INFORMATION | |
801 | }; | |
802 | ||
669f7a11 | 803 | #ifndef __WIN16__ |
f1df0927 VZ |
804 | for ( size_t icon = 0; icon < WXSIZEOF(icons); icon++ ) |
805 | { | |
806 | wxBitmap bmp = wxTheApp->GetStdIcon(icons[icon]); | |
807 | imageList->Add(wxImage(bmp). | |
808 | Rescale(ICON_SIZE, ICON_SIZE). | |
809 | ConvertToBitmap()); | |
810 | } | |
811 | ||
812 | m_listctrl->SetImageList(imageList, wxIMAGE_LIST_SMALL); | |
669f7a11 | 813 | #endif |
f1df0927 VZ |
814 | |
815 | // and fill it | |
816 | wxString fmt = wxLog::GetTimestamp(); | |
817 | if ( !fmt ) | |
818 | { | |
819 | // default format | |
820 | fmt = _T("%X"); | |
821 | } | |
822 | ||
823 | size_t count = m_messages.GetCount(); | |
824 | for ( size_t n = 0; n < count; n++ ) | |
825 | { | |
669f7a11 JS |
826 | int image = -1; |
827 | #ifndef __WIN16__ | |
f1df0927 VZ |
828 | switch ( m_severity[n] ) |
829 | { | |
830 | case wxLOG_Error: | |
831 | image = 0; | |
832 | break; | |
833 | ||
834 | case wxLOG_Warning: | |
835 | image = 1; | |
836 | break; | |
837 | ||
838 | default: | |
839 | image = 2; | |
840 | } | |
669f7a11 | 841 | #endif |
f1df0927 VZ |
842 | |
843 | m_listctrl->InsertItem(n, m_messages[n], image); | |
844 | m_listctrl->SetItem(n, 1, | |
845 | wxDateTime((time_t)m_times[n]).Format(fmt)); | |
846 | } | |
847 | ||
50af7fa9 | 848 | // let the columns size themselves |
f1df0927 VZ |
849 | m_listctrl->SetColumnWidth(0, wxLIST_AUTOSIZE); |
850 | m_listctrl->SetColumnWidth(1, wxLIST_AUTOSIZE); | |
851 | ||
852 | // get the approx height of the listctrl | |
853 | wxFont font = GetFont(); | |
854 | if ( !font.Ok() ) | |
855 | font = *wxSWISS_FONT; | |
856 | ||
857 | int y; | |
858 | GetTextExtent(_T("H"), (int*)NULL, &y, (int*)NULL, (int*)NULL, &font); | |
859 | int height = wxMin(y*(count + 3), 100); | |
860 | m_listctrl->SetSize(-1, height); | |
861 | } | |
862 | ||
863 | sizer->Add(m_listctrl, 1, wxEXPAND|(wxALL & ~wxTOP), MARGIN); | |
864 | } | |
865 | ||
866 | m_showingDetails = !m_showingDetails; | |
867 | ||
868 | // in any case, our size changed - update | |
ea451729 | 869 | sizer->SetSizeHints(this); |
f1df0927 VZ |
870 | sizer->Fit(this); |
871 | } | |
872 | ||
873 | wxLogDialog::~wxLogDialog() | |
874 | { | |
875 | if ( m_listctrl ) | |
876 | { | |
877 | delete m_listctrl->GetImageList(wxIMAGE_LIST_SMALL); | |
878 | } | |
879 | } | |
880 | ||
881 | #endif // wxUSE_LOG_DIALOG |