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