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