]> git.saurik.com Git - wxWidgets.git/blame - src/generic/logg.cpp
Committing in .
[wxWidgets.git] / src / generic / logg.cpp
CommitLineData
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"
41 #include "wx/msgdlg.h"
42 #include "wx/textctrl.h"
43#endif // WX_PRECOMP
44
45#include "wx/file.h"
46#include "wx/textfile.h"
47
39a7c7e1
VZ
48#ifdef __WXMSW__
49 // for OutputDebugString()
50 #include "wx/msw/private.h"
51#endif // Windows
52
dd85fc6b
VZ
53// ----------------------------------------------------------------------------
54// global variables
55// ----------------------------------------------------------------------------
56
57// we use a global variable to store the frame pointer for wxLogStatus - bad,
58// but it's he easiest way
59static wxFrame *gs_pFrame; // FIXME MT-unsafe
60
61// ============================================================================
62// implementation
63// ============================================================================
64
65// ----------------------------------------------------------------------------
66// global functions
67// ----------------------------------------------------------------------------
68
69// accepts an additional argument which tells to which frame the output should
70// be directed
71void wxLogStatus(wxFrame *pFrame, const wxChar *szFormat, ...)
72{
73 wxString msg;
74
75 wxLog *pLog = wxLog::GetActiveTarget();
76 if ( pLog != NULL ) {
77 va_list argptr;
78 va_start(argptr, szFormat);
79 msg.PrintfV(szFormat, argptr);
80 va_end(argptr);
81
82 wxASSERT( gs_pFrame == NULL ); // should be reset!
83 gs_pFrame = pFrame;
84 wxLog::OnLog(wxLOG_Status, msg, time(NULL));
85 gs_pFrame = (wxFrame *) NULL;
86 }
87}
88
89// ----------------------------------------------------------------------------
90// wxLogTextCtrl implementation
91// ----------------------------------------------------------------------------
92
93wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl)
94{
95 m_pTextCtrl = pTextCtrl;
96}
97
5e0201ea 98void wxLogTextCtrl::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
dd85fc6b
VZ
99{
100 wxString msg;
101 TimeStamp(&msg);
223d09f6 102 msg << szString << wxT('\n');
dd85fc6b
VZ
103
104 m_pTextCtrl->AppendText(msg);
105}
106
107// ----------------------------------------------------------------------------
108// wxLogGui implementation (FIXME MT-unsafe)
109// ----------------------------------------------------------------------------
110
111wxLogGui::wxLogGui()
112{
113 Clear();
114}
115
116void wxLogGui::Clear()
117{
118 m_bErrors = m_bWarnings = FALSE;
119 m_aMessages.Empty();
120 m_aTimes.Empty();
121}
122
123void wxLogGui::Flush()
124{
125 if ( !m_bHasMessages )
126 return;
127
128 // do it right now to block any new calls to Flush() while we're here
129 m_bHasMessages = FALSE;
130
131 // concatenate all strings (but not too many to not overfill the msg box)
132 wxString str;
133 size_t nLines = 0,
134 nMsgCount = m_aMessages.Count();
135
136 // start from the most recent message
137 for ( size_t n = nMsgCount; n > 0; n-- ) {
138 // for Windows strings longer than this value are wrapped (NT 4.0)
139 const size_t nMsgLineWidth = 156;
140
141 nLines += (m_aMessages[n - 1].Len() + nMsgLineWidth - 1) / nMsgLineWidth;
142
143 if ( nLines > 25 ) // don't put too many lines in message box
144 break;
145
223d09f6 146 str << m_aMessages[n - 1] << wxT("\n");
dd85fc6b
VZ
147 }
148
149 const wxChar *title;
150 long style;
151
152 if ( m_bErrors ) {
153 title = _("Error");
154 style = wxICON_STOP;
155 }
156 else if ( m_bWarnings ) {
157 title = _("Warning");
158 style = wxICON_EXCLAMATION;
159 }
160 else {
161 title = _("Information");
162 style = wxICON_INFORMATION;
163 }
164
165 wxMessageBox(str, title, wxOK | style);
166
167 // no undisplayed messages whatsoever
168 Clear();
169
170 // do it here again
171 m_bHasMessages = FALSE;
172}
173
174// the default behaviour is to discard all informational messages if there
175// are any errors/warnings.
176void wxLogGui::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
177{
178 switch ( level ) {
179 case wxLOG_Info:
180 if ( GetVerbose() )
181 case wxLOG_Message:
182 if ( !m_bErrors ) {
183 m_aMessages.Add(szString);
184 m_aTimes.Add((long)t);
185 m_bHasMessages = TRUE;
186 }
187 break;
188
189 case wxLOG_Status:
190#if wxUSE_STATUSBAR
191 {
192 // find the top window and set it's status text if it has any
193 wxFrame *pFrame = gs_pFrame;
194 if ( pFrame == NULL ) {
195 wxWindow *pWin = wxTheApp->GetTopWindow();
196 if ( pWin != NULL && pWin->IsKindOf(CLASSINFO(wxFrame)) ) {
197 pFrame = (wxFrame *)pWin;
198 }
199 }
200
201 if ( pFrame != NULL )
202 pFrame->SetStatusText(szString);
203 }
204#endif // wxUSE_STATUSBAR
205 break;
206
207 case wxLOG_Trace:
208 case wxLOG_Debug:
209 #ifdef __WXDEBUG__
210 {
211 #ifdef __WXMSW__
212 // don't prepend debug/trace here: it goes to the
213 // debug window anyhow, but do put a timestamp
214 wxString str;
215 TimeStamp(&str);
223d09f6 216 str << szString << wxT("\n\r");
dd85fc6b
VZ
217 OutputDebugString(str);
218 #else
219 // send them to stderr
223d09f6
KB
220 wxFprintf(stderr, wxT("%s: %s\n"),
221 level == wxLOG_Trace ? wxT("Trace")
222 : wxT("Debug"),
dd85fc6b
VZ
223 szString);
224 fflush(stderr);
225 #endif
226 }
227 #endif // __WXDEBUG__
228
229 break;
230
231 case wxLOG_FatalError:
232 // show this one immediately
233 wxMessageBox(szString, _("Fatal error"), wxICON_HAND);
234 break;
235
236 case wxLOG_Error:
237 // discard earlier informational messages if this is the 1st
238 // error because they might not make sense any more
239 if ( !m_bErrors ) {
240 m_aMessages.Empty();
241 m_aTimes.Empty();
242 m_bErrors = TRUE;
243 }
244 // fall through
245
246 case wxLOG_Warning:
247 if ( !m_bErrors ) {
248 // for the warning we don't discard the info messages
249 m_bWarnings = TRUE;
250 }
251
252 m_aMessages.Add(szString);
253 m_aTimes.Add((long)t);
254 m_bHasMessages = TRUE;
255 break;
256 }
257}
258
259// ----------------------------------------------------------------------------
260// wxLogWindow and wxLogFrame implementation
261// ----------------------------------------------------------------------------
262
263// log frame class
264// ---------------
265class wxLogFrame : public wxFrame
266{
267public:
268 // ctor & dtor
269 wxLogFrame(wxFrame *pParent, wxLogWindow *log, const wxChar *szTitle);
270 virtual ~wxLogFrame();
271
272 // menu callbacks
273 void OnClose(wxCommandEvent& event);
274 void OnCloseWindow(wxCloseEvent& event);
275#if wxUSE_FILE
276 void OnSave (wxCommandEvent& event);
277#endif // wxUSE_FILE
278 void OnClear(wxCommandEvent& event);
279
280 void OnIdle(wxIdleEvent&);
281
282 // accessors
283 wxTextCtrl *TextCtrl() const { return m_pTextCtrl; }
284
285private:
286 enum
287 {
288 Menu_Close = 100,
289 Menu_Save,
290 Menu_Clear
291 };
292
293 // instead of closing just hide the window to be able to Show() it later
294 void DoClose() { Show(FALSE); }
295
296 wxTextCtrl *m_pTextCtrl;
297 wxLogWindow *m_log;
298
299 DECLARE_EVENT_TABLE()
300};
301
302BEGIN_EVENT_TABLE(wxLogFrame, wxFrame)
303 // wxLogWindow menu events
304 EVT_MENU(Menu_Close, wxLogFrame::OnClose)
305#if wxUSE_FILE
306 EVT_MENU(Menu_Save, wxLogFrame::OnSave)
307#endif // wxUSE_FILE
308 EVT_MENU(Menu_Clear, wxLogFrame::OnClear)
309
310 EVT_CLOSE(wxLogFrame::OnCloseWindow)
311END_EVENT_TABLE()
312
313wxLogFrame::wxLogFrame(wxFrame *pParent, wxLogWindow *log, const wxChar *szTitle)
314 : wxFrame(pParent, -1, szTitle)
315{
316 m_log = log;
317
318 m_pTextCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition,
319 wxDefaultSize,
320 wxTE_MULTILINE |
321 wxHSCROLL |
322 wxTE_READONLY);
323
324 // create menu
325 wxMenuBar *pMenuBar = new wxMenuBar;
326 wxMenu *pMenu = new wxMenu;
327#if wxUSE_FILE
328 pMenu->Append(Menu_Save, _("&Save..."), _("Save log contents to file"));
329#endif // wxUSE_FILE
330 pMenu->Append(Menu_Clear, _("C&lear"), _("Clear the log contents"));
331 pMenu->AppendSeparator();
332 pMenu->Append(Menu_Close, _("&Close"), _("Close this window"));
333 pMenuBar->Append(pMenu, _("&Log"));
334 SetMenuBar(pMenuBar);
335
336#if wxUSE_STATUSBAR
337 // status bar for menu prompts
338 CreateStatusBar();
339#endif // wxUSE_STATUSBAR
340
341 m_log->OnFrameCreate(this);
342}
343
344void wxLogFrame::OnClose(wxCommandEvent& WXUNUSED(event))
345{
346 DoClose();
347}
348
349void wxLogFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
350{
351 DoClose();
352}
353
354#if wxUSE_FILE
355void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event))
356{
357 // get the file name
358 // -----------------
223d09f6 359 const wxChar *szFileName = wxSaveFileSelector(wxT("log"), wxT("txt"), wxT("log.txt"));
dd85fc6b
VZ
360 if ( szFileName == NULL ) {
361 // cancelled
362 return;
363 }
364
365 // open file
366 // ---------
367 wxFile file;
368 bool bOk = FALSE;
369 if ( wxFile::Exists(szFileName) ) {
370 bool bAppend = FALSE;
371 wxString strMsg;
372 strMsg.Printf(_("Append log to file '%s' "
373 "(choosing [No] will overwrite it)?"), szFileName);
374 switch ( wxMessageBox(strMsg, _("Question"), wxYES_NO | wxCANCEL) ) {
375 case wxYES:
376 bAppend = TRUE;
377 break;
378
379 case wxNO:
380 bAppend = FALSE;
381 break;
382
383 case wxCANCEL:
384 return;
385
386 default:
387 wxFAIL_MSG(_("invalid message box return value"));
388 }
389
390 if ( bAppend ) {
391 bOk = file.Open(szFileName, wxFile::write_append);
392 }
393 else {
394 bOk = file.Create(szFileName, TRUE /* overwrite */);
395 }
396 }
397 else {
398 bOk = file.Create(szFileName);
399 }
400
401 // retrieve text and save it
402 // -------------------------
403 int nLines = m_pTextCtrl->GetNumberOfLines();
404 for ( int nLine = 0; bOk && nLine < nLines; nLine++ ) {
405 bOk = file.Write(m_pTextCtrl->GetLineText(nLine) +
406 // we're not going to pull in the whole wxTextFile if all we need is this...
407#if wxUSE_TEXTFILE
408 wxTextFile::GetEOL()
409#else // !wxUSE_TEXTFILE
410 '\n'
411#endif // wxUSE_TEXTFILE
412 );
413 }
414
415 if ( bOk )
416 bOk = file.Close();
417
418 if ( !bOk ) {
419 wxLogError(_("Can't save log contents to file."));
420 }
421 else {
422 wxLogStatus(this, _("Log saved to the file '%s'."), szFileName);
423 }
424}
425#endif // wxUSE_FILE
426
427void wxLogFrame::OnClear(wxCommandEvent& WXUNUSED(event))
428{
429 m_pTextCtrl->Clear();
430}
431
432wxLogFrame::~wxLogFrame()
433{
434 m_log->OnFrameDelete(this);
435}
436
437// wxLogWindow
438// -----------
439wxLogWindow::wxLogWindow(wxFrame *pParent,
440 const wxChar *szTitle,
441 bool bShow,
442 bool bDoPass)
443{
444 m_bPassMessages = bDoPass;
445
446 m_pLogFrame = new wxLogFrame(pParent, this, szTitle);
447 m_pOldLog = wxLog::SetActiveTarget(this);
448
449 if ( bShow )
450 m_pLogFrame->Show(TRUE);
451}
452
453void wxLogWindow::Show(bool bShow)
454{
455 m_pLogFrame->Show(bShow);
456}
457
458void wxLogWindow::Flush()
459{
460 if ( m_pOldLog != NULL )
461 m_pOldLog->Flush();
462
463 m_bHasMessages = FALSE;
464}
465
466void wxLogWindow::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
467{
468 // first let the previous logger show it
469 if ( m_pOldLog != NULL && m_bPassMessages ) {
470 // FIXME why can't we access protected wxLog method from here (we derive
471 // from wxLog)? gcc gives "DoLog is protected in this context", what
472 // does this mean? Anyhow, the cast is harmless and let's us do what
473 // we want.
474 ((wxLogWindow *)m_pOldLog)->DoLog(level, szString, t);
475 }
476
477 if ( m_pLogFrame ) {
478 switch ( level ) {
479 case wxLOG_Status:
480 // by default, these messages are ignored by wxLog, so process
481 // them ourselves
482 if ( !wxIsEmpty(szString) )
483 {
484 wxString str;
485 str << _("Status: ") << szString;
486 DoLogString(str, t);
487 }
488 break;
489
490 // don't put trace messages in the text window for 2 reasons:
491 // 1) there are too many of them
492 // 2) they may provoke other trace messages thus sending a program
493 // into an infinite loop
494 case wxLOG_Trace:
495 break;
496
497 default:
498 // and this will format it nicely and call our DoLogString()
499 wxLog::DoLog(level, szString, t);
500 }
501 }
502
503 m_bHasMessages = TRUE;
504}
505
506void wxLogWindow::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
507{
508 // put the text into our window
509 wxTextCtrl *pText = m_pLogFrame->TextCtrl();
510
511 // remove selection (WriteText is in fact ReplaceSelection)
512#ifdef __WXMSW__
513 long nLen = pText->GetLastPosition();
514 pText->SetSelection(nLen, nLen);
515#endif // Windows
516
517 wxString msg;
518 TimeStamp(&msg);
223d09f6 519 msg << szString << wxT('\n');
dd85fc6b
VZ
520
521 pText->AppendText(msg);
522
523 // TODO ensure that the line can be seen
524}
525
526wxFrame *wxLogWindow::GetFrame() const
527{
528 return m_pLogFrame;
529}
530
531void wxLogWindow::OnFrameCreate(wxFrame * WXUNUSED(frame))
532{
533}
534
535void wxLogWindow::OnFrameDelete(wxFrame * WXUNUSED(frame))
536{
537 m_pLogFrame = (wxLogFrame *)NULL;
538}
539
540wxLogWindow::~wxLogWindow()
541{
542 delete m_pOldLog;
543
544 // may be NULL if log frame already auto destroyed itself
545 delete m_pLogFrame;
546}
547