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