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