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