]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: common/base/appbase.cpp | |
3 | // Purpose: implements wxAppConsole class | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 19.06.2003 (extracted from common/appcmn.cpp) | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org> | |
9 | // License: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // for compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/app.h" | |
29 | #include "wx/intl.h" | |
30 | #include "wx/list.h" | |
31 | #include "wx/log.h" | |
32 | #endif //WX_PRECOMP | |
33 | ||
34 | #include "wx/utils.h" | |
35 | #include "wx/apptrait.h" | |
36 | #include "wx/cmdline.h" | |
37 | #include "wx/confbase.h" | |
38 | #include "wx/filename.h" | |
39 | #include "wx/msgout.h" | |
40 | #include "wx/tokenzr.h" | |
41 | ||
42 | #if !defined(__WXMSW__) || defined(__WXMICROWIN__) | |
43 | #include <signal.h> // for SIGTRAP used by wxTrap() | |
44 | #endif //Win/Unix | |
45 | ||
46 | #if defined(__WXMSW__) | |
47 | #include "wx/msw/wrapwin.h" // includes windows.h for MessageBox() | |
48 | #endif | |
49 | ||
50 | #if wxUSE_FONTMAP | |
51 | #include "wx/fontmap.h" | |
52 | #endif // wxUSE_FONTMAP | |
53 | ||
54 | #if defined(__DARWIN__) && defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS | |
55 | // For MacTypes.h for Debugger function | |
56 | #include <CoreFoundation/CFBase.h> | |
57 | #endif | |
58 | ||
59 | #if defined(__WXMAC__) | |
60 | // VZ: MacTypes.h is enough under Mac OS X (where I could test it) but | |
61 | // I don't know which headers are needed under earlier systems so | |
62 | // include everything when in doubt | |
63 | #ifdef __DARWIN__ | |
64 | #include "MacTypes.h" | |
65 | #else | |
66 | #include "wx/mac/private.h" // includes mac headers | |
67 | #endif | |
68 | #endif // __WXMAC__ | |
69 | ||
70 | #ifdef __WXDEBUG__ | |
71 | #ifdef wxUSE_STACKWALKER | |
72 | #include "wx/stackwalk.h" | |
73 | #endif // wxUSE_STACKWALKER | |
74 | #endif // __WXDEBUG__ | |
75 | ||
76 | // ---------------------------------------------------------------------------- | |
77 | // private functions prototypes | |
78 | // ---------------------------------------------------------------------------- | |
79 | ||
80 | #ifdef __WXDEBUG__ | |
81 | // really just show the assert dialog | |
82 | static bool DoShowAssertDialog(const wxString& msg); | |
83 | ||
84 | // prepare for showing the assert dialog, use the given traits or | |
85 | // DoShowAssertDialog() as last fallback to really show it | |
86 | static | |
87 | void ShowAssertDialog(const wxChar *szFile, | |
88 | int nLine, | |
89 | const wxChar *szCond, | |
90 | const wxChar *szMsg, | |
91 | wxAppTraits *traits = NULL); | |
92 | ||
93 | // turn on the trace masks specified in the env variable WXTRACE | |
94 | static void LINKAGEMODE SetTraceMasks(); | |
95 | #endif // __WXDEBUG__ | |
96 | ||
97 | // ---------------------------------------------------------------------------- | |
98 | // global vars | |
99 | // ---------------------------------------------------------------------------- | |
100 | ||
101 | wxAppConsole *wxAppConsole::ms_appInstance = NULL; | |
102 | ||
103 | wxAppInitializerFunction wxAppConsole::ms_appInitFn = NULL; | |
104 | ||
105 | // ============================================================================ | |
106 | // wxAppConsole implementation | |
107 | // ============================================================================ | |
108 | ||
109 | // ---------------------------------------------------------------------------- | |
110 | // ctor/dtor | |
111 | // ---------------------------------------------------------------------------- | |
112 | ||
113 | wxAppConsole::wxAppConsole() | |
114 | { | |
115 | m_traits = NULL; | |
116 | ||
117 | ms_appInstance = this; | |
118 | ||
119 | #ifdef __WXDEBUG__ | |
120 | SetTraceMasks(); | |
121 | #if wxUSE_UNICODE | |
122 | // In unicode mode the SetTraceMasks call can cause an apptraits to be | |
123 | // created, but since we are still in the constructor the wrong kind will | |
124 | // be created for GUI apps. Destroy it so it can be created again later. | |
125 | delete m_traits; | |
126 | m_traits = NULL; | |
127 | #endif | |
128 | #endif | |
129 | } | |
130 | ||
131 | wxAppConsole::~wxAppConsole() | |
132 | { | |
133 | delete m_traits; | |
134 | } | |
135 | ||
136 | // ---------------------------------------------------------------------------- | |
137 | // initilization/cleanup | |
138 | // ---------------------------------------------------------------------------- | |
139 | ||
140 | bool wxAppConsole::Initialize(int& argc, wxChar **argv) | |
141 | { | |
142 | #if wxUSE_LOG | |
143 | // If some code logged something before wxApp instance was created, | |
144 | // wxLogStderr was set as the target. Undo it here by destroying the | |
145 | // current target. It will be re-created next time logging is needed, but | |
146 | // this time wxAppTraits will be used: | |
147 | delete wxLog::SetActiveTarget(NULL); | |
148 | #endif // wxUSE_LOG | |
149 | ||
150 | // remember the command line arguments | |
151 | this->argc = argc; | |
152 | this->argv = argv; | |
153 | ||
154 | #ifndef __WXPALMOS__ | |
155 | if ( m_appName.empty() && argv ) | |
156 | { | |
157 | // the application name is, by default, the name of its executable file | |
158 | wxFileName::SplitPath(argv[0], NULL, &m_appName, NULL); | |
159 | } | |
160 | #endif | |
161 | ||
162 | return true; | |
163 | } | |
164 | ||
165 | void wxAppConsole::CleanUp() | |
166 | { | |
167 | } | |
168 | ||
169 | // ---------------------------------------------------------------------------- | |
170 | // OnXXX() callbacks | |
171 | // ---------------------------------------------------------------------------- | |
172 | ||
173 | bool wxAppConsole::OnInit() | |
174 | { | |
175 | #if wxUSE_CMDLINE_PARSER | |
176 | wxCmdLineParser parser(argc, argv); | |
177 | ||
178 | OnInitCmdLine(parser); | |
179 | ||
180 | bool cont; | |
181 | switch ( parser.Parse(false /* don't show usage */) ) | |
182 | { | |
183 | case -1: | |
184 | cont = OnCmdLineHelp(parser); | |
185 | break; | |
186 | ||
187 | case 0: | |
188 | cont = OnCmdLineParsed(parser); | |
189 | break; | |
190 | ||
191 | default: | |
192 | cont = OnCmdLineError(parser); | |
193 | break; | |
194 | } | |
195 | ||
196 | if ( !cont ) | |
197 | return false; | |
198 | #endif // wxUSE_CMDLINE_PARSER | |
199 | ||
200 | return true; | |
201 | } | |
202 | ||
203 | int wxAppConsole::OnExit() | |
204 | { | |
205 | #if wxUSE_CONFIG | |
206 | // delete the config object if any (don't use Get() here, but Set() | |
207 | // because Get() could create a new config object) | |
208 | delete wxConfigBase::Set((wxConfigBase *) NULL); | |
209 | #endif // wxUSE_CONFIG | |
210 | ||
211 | // use Set(NULL) and not Get() to avoid creating a message output object on | |
212 | // demand when we just want to delete it | |
213 | delete wxMessageOutput::Set(NULL); | |
214 | ||
215 | return 0; | |
216 | } | |
217 | ||
218 | void wxAppConsole::Exit() | |
219 | { | |
220 | exit(-1); | |
221 | } | |
222 | ||
223 | // ---------------------------------------------------------------------------- | |
224 | // traits stuff | |
225 | // ---------------------------------------------------------------------------- | |
226 | ||
227 | wxAppTraits *wxAppConsole::CreateTraits() | |
228 | { | |
229 | return new wxConsoleAppTraits; | |
230 | } | |
231 | ||
232 | wxAppTraits *wxAppConsole::GetTraits() | |
233 | { | |
234 | // FIXME-MT: protect this with a CS? | |
235 | if ( !m_traits ) | |
236 | { | |
237 | m_traits = CreateTraits(); | |
238 | ||
239 | wxASSERT_MSG( m_traits, _T("wxApp::CreateTraits() failed?") ); | |
240 | } | |
241 | ||
242 | return m_traits; | |
243 | } | |
244 | ||
245 | // we must implement CreateXXX() in wxApp itself for backwards compatibility | |
246 | #if WXWIN_COMPATIBILITY_2_4 | |
247 | ||
248 | #if wxUSE_LOG | |
249 | ||
250 | wxLog *wxAppConsole::CreateLogTarget() | |
251 | { | |
252 | wxAppTraits *traits = GetTraits(); | |
253 | return traits ? traits->CreateLogTarget() : NULL; | |
254 | } | |
255 | ||
256 | #endif // wxUSE_LOG | |
257 | ||
258 | wxMessageOutput *wxAppConsole::CreateMessageOutput() | |
259 | { | |
260 | wxAppTraits *traits = GetTraits(); | |
261 | return traits ? traits->CreateMessageOutput() : NULL; | |
262 | } | |
263 | ||
264 | #endif // WXWIN_COMPATIBILITY_2_4 | |
265 | ||
266 | // ---------------------------------------------------------------------------- | |
267 | // event processing | |
268 | // ---------------------------------------------------------------------------- | |
269 | ||
270 | void wxAppConsole::ProcessPendingEvents() | |
271 | { | |
272 | #if wxUSE_THREADS | |
273 | if ( !wxPendingEventsLocker ) | |
274 | return; | |
275 | #endif | |
276 | ||
277 | // ensure that we're the only thread to modify the pending events list | |
278 | wxENTER_CRIT_SECT( *wxPendingEventsLocker ); | |
279 | ||
280 | if ( !wxPendingEvents ) | |
281 | { | |
282 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); | |
283 | return; | |
284 | } | |
285 | ||
286 | // iterate until the list becomes empty | |
287 | wxList::compatibility_iterator node = wxPendingEvents->GetFirst(); | |
288 | while (node) | |
289 | { | |
290 | wxEvtHandler *handler = (wxEvtHandler *)node->GetData(); | |
291 | wxPendingEvents->Erase(node); | |
292 | ||
293 | // In ProcessPendingEvents(), new handlers might be add | |
294 | // and we can safely leave the critical section here. | |
295 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); | |
296 | ||
297 | handler->ProcessPendingEvents(); | |
298 | ||
299 | wxENTER_CRIT_SECT( *wxPendingEventsLocker ); | |
300 | ||
301 | node = wxPendingEvents->GetFirst(); | |
302 | } | |
303 | ||
304 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); | |
305 | } | |
306 | ||
307 | int wxAppConsole::FilterEvent(wxEvent& WXUNUSED(event)) | |
308 | { | |
309 | // process the events normally by default | |
310 | return -1; | |
311 | } | |
312 | ||
313 | // ---------------------------------------------------------------------------- | |
314 | // exception handling | |
315 | // ---------------------------------------------------------------------------- | |
316 | ||
317 | #if wxUSE_EXCEPTIONS | |
318 | ||
319 | void | |
320 | wxAppConsole::HandleEvent(wxEvtHandler *handler, | |
321 | wxEventFunction func, | |
322 | wxEvent& event) const | |
323 | { | |
324 | // by default, simply call the handler | |
325 | (handler->*func)(event); | |
326 | } | |
327 | ||
328 | bool | |
329 | wxAppConsole::OnExceptionInMainLoop() | |
330 | { | |
331 | throw; | |
332 | ||
333 | // some compilers are too stupid to know that we never return after throw | |
334 | #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200) | |
335 | return false; | |
336 | #endif | |
337 | } | |
338 | ||
339 | #endif // wxUSE_EXCEPTIONS | |
340 | ||
341 | // ---------------------------------------------------------------------------- | |
342 | // cmd line parsing | |
343 | // ---------------------------------------------------------------------------- | |
344 | ||
345 | #if wxUSE_CMDLINE_PARSER | |
346 | ||
347 | #define OPTION_VERBOSE _T("verbose") | |
348 | ||
349 | void wxAppConsole::OnInitCmdLine(wxCmdLineParser& parser) | |
350 | { | |
351 | // the standard command line options | |
352 | static const wxCmdLineEntryDesc cmdLineDesc[] = | |
353 | { | |
354 | { | |
355 | wxCMD_LINE_SWITCH, | |
356 | _T("h"), | |
357 | _T("help"), | |
358 | gettext_noop("show this help message"), | |
359 | wxCMD_LINE_VAL_NONE, | |
360 | wxCMD_LINE_OPTION_HELP | |
361 | }, | |
362 | ||
363 | #if wxUSE_LOG | |
364 | { | |
365 | wxCMD_LINE_SWITCH, | |
366 | wxEmptyString, | |
367 | OPTION_VERBOSE, | |
368 | gettext_noop("generate verbose log messages"), | |
369 | wxCMD_LINE_VAL_NONE, | |
370 | 0x0 | |
371 | }, | |
372 | #endif // wxUSE_LOG | |
373 | ||
374 | // terminator | |
375 | { | |
376 | wxCMD_LINE_NONE, | |
377 | wxEmptyString, | |
378 | wxEmptyString, | |
379 | wxEmptyString, | |
380 | wxCMD_LINE_VAL_NONE, | |
381 | 0x0 | |
382 | } | |
383 | }; | |
384 | ||
385 | parser.SetDesc(cmdLineDesc); | |
386 | } | |
387 | ||
388 | bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser& parser) | |
389 | { | |
390 | #if wxUSE_LOG | |
391 | if ( parser.Found(OPTION_VERBOSE) ) | |
392 | { | |
393 | wxLog::SetVerbose(true); | |
394 | } | |
395 | #else | |
396 | wxUnusedVar(parser); | |
397 | #endif // wxUSE_LOG | |
398 | ||
399 | return true; | |
400 | } | |
401 | ||
402 | bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser& parser) | |
403 | { | |
404 | parser.Usage(); | |
405 | ||
406 | return false; | |
407 | } | |
408 | ||
409 | bool wxAppConsole::OnCmdLineError(wxCmdLineParser& parser) | |
410 | { | |
411 | parser.Usage(); | |
412 | ||
413 | return false; | |
414 | } | |
415 | ||
416 | #endif // wxUSE_CMDLINE_PARSER | |
417 | ||
418 | // ---------------------------------------------------------------------------- | |
419 | // debugging support | |
420 | // ---------------------------------------------------------------------------- | |
421 | ||
422 | /* static */ | |
423 | bool wxAppConsole::CheckBuildOptions(const char *optionsSignature, | |
424 | const char *componentName) | |
425 | { | |
426 | #if 0 // can't use wxLogTrace, not up and running yet | |
427 | printf("checking build options object '%s' (ptr %p) in '%s'\n", | |
428 | optionsSignature, optionsSignature, componentName); | |
429 | #endif | |
430 | ||
431 | if ( strcmp(optionsSignature, WX_BUILD_OPTIONS_SIGNATURE) != 0 ) | |
432 | { | |
433 | wxString lib = wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE); | |
434 | wxString prog = wxString::FromAscii(optionsSignature); | |
435 | wxString progName = wxString::FromAscii(componentName); | |
436 | wxString msg; | |
437 | ||
438 | msg.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."), | |
439 | lib.c_str(), progName.c_str(), prog.c_str()); | |
440 | ||
441 | wxLogFatalError(msg.c_str()); | |
442 | ||
443 | // normally wxLogFatalError doesn't return | |
444 | return false; | |
445 | } | |
446 | #undef wxCMP | |
447 | ||
448 | return true; | |
449 | } | |
450 | ||
451 | #ifdef __WXDEBUG__ | |
452 | ||
453 | void wxAppConsole::OnAssert(const wxChar *file, | |
454 | int line, | |
455 | const wxChar *cond, | |
456 | const wxChar *msg) | |
457 | { | |
458 | ShowAssertDialog(file, line, cond, msg, GetTraits()); | |
459 | } | |
460 | ||
461 | #endif // __WXDEBUG__ | |
462 | ||
463 | #if WXWIN_COMPATIBILITY_2_4 | |
464 | ||
465 | bool wxAppConsole::CheckBuildOptions(const wxBuildOptions& buildOptions) | |
466 | { | |
467 | return CheckBuildOptions(buildOptions.m_signature, "your program"); | |
468 | } | |
469 | ||
470 | #endif | |
471 | ||
472 | // ============================================================================ | |
473 | // other classes implementations | |
474 | // ============================================================================ | |
475 | ||
476 | // ---------------------------------------------------------------------------- | |
477 | // wxConsoleAppTraitsBase | |
478 | // ---------------------------------------------------------------------------- | |
479 | ||
480 | #if wxUSE_LOG | |
481 | ||
482 | wxLog *wxConsoleAppTraitsBase::CreateLogTarget() | |
483 | { | |
484 | return new wxLogStderr; | |
485 | } | |
486 | ||
487 | #endif // wxUSE_LOG | |
488 | ||
489 | wxMessageOutput *wxConsoleAppTraitsBase::CreateMessageOutput() | |
490 | { | |
491 | return new wxMessageOutputStderr; | |
492 | } | |
493 | ||
494 | #if wxUSE_FONTMAP | |
495 | ||
496 | wxFontMapper *wxConsoleAppTraitsBase::CreateFontMapper() | |
497 | { | |
498 | return (wxFontMapper *)new wxFontMapperBase; | |
499 | } | |
500 | ||
501 | #endif // wxUSE_FONTMAP | |
502 | ||
503 | wxRendererNative *wxConsoleAppTraitsBase::CreateRenderer() | |
504 | { | |
505 | // console applications don't use renderers | |
506 | return NULL; | |
507 | } | |
508 | ||
509 | #ifdef __WXDEBUG__ | |
510 | bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString& msg) | |
511 | { | |
512 | return wxAppTraitsBase::ShowAssertDialog(msg); | |
513 | } | |
514 | #endif | |
515 | ||
516 | bool wxConsoleAppTraitsBase::HasStderr() | |
517 | { | |
518 | // console applications always have stderr, even under Mac/Windows | |
519 | return true; | |
520 | } | |
521 | ||
522 | void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject *object) | |
523 | { | |
524 | delete object; | |
525 | } | |
526 | ||
527 | void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject * WXUNUSED(object)) | |
528 | { | |
529 | // nothing to do | |
530 | } | |
531 | ||
532 | #if wxUSE_SOCKETS | |
533 | GSocketGUIFunctionsTable* wxConsoleAppTraitsBase::GetSocketGUIFunctionsTable() | |
534 | { | |
535 | return NULL; | |
536 | } | |
537 | #endif | |
538 | ||
539 | // ---------------------------------------------------------------------------- | |
540 | // wxAppTraits | |
541 | // ---------------------------------------------------------------------------- | |
542 | ||
543 | #ifdef __WXDEBUG__ | |
544 | ||
545 | bool wxAppTraitsBase::ShowAssertDialog(const wxString& msg) | |
546 | { | |
547 | return DoShowAssertDialog(msg); | |
548 | } | |
549 | ||
550 | #endif // __WXDEBUG__ | |
551 | ||
552 | // ============================================================================ | |
553 | // global functions implementation | |
554 | // ============================================================================ | |
555 | ||
556 | void wxExit() | |
557 | { | |
558 | if ( wxTheApp ) | |
559 | { | |
560 | wxTheApp->Exit(); | |
561 | } | |
562 | else | |
563 | { | |
564 | // what else can we do? | |
565 | exit(-1); | |
566 | } | |
567 | } | |
568 | ||
569 | void wxWakeUpIdle() | |
570 | { | |
571 | if ( wxTheApp ) | |
572 | { | |
573 | wxTheApp->WakeUpIdle(); | |
574 | } | |
575 | //else: do nothing, what can we do? | |
576 | } | |
577 | ||
578 | #ifdef __WXDEBUG__ | |
579 | ||
580 | // wxASSERT() helper | |
581 | bool wxAssertIsEqual(int x, int y) | |
582 | { | |
583 | return x == y; | |
584 | } | |
585 | ||
586 | // break into the debugger | |
587 | void wxTrap() | |
588 | { | |
589 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
590 | DebugBreak(); | |
591 | #elif defined(__WXMAC__) && !defined(__DARWIN__) | |
592 | #if __powerc | |
593 | Debugger(); | |
594 | #else | |
595 | SysBreak(); | |
596 | #endif | |
597 | #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS | |
598 | Debugger(); | |
599 | #elif defined(__UNIX__) | |
600 | raise(SIGTRAP); | |
601 | #else | |
602 | // TODO | |
603 | #endif // Win/Unix | |
604 | } | |
605 | ||
606 | void wxAssert(int cond, | |
607 | const wxChar *szFile, | |
608 | int nLine, | |
609 | const wxChar *szCond, | |
610 | const wxChar *szMsg) | |
611 | { | |
612 | if ( !cond ) | |
613 | wxOnAssert(szFile, nLine, szCond, szMsg); | |
614 | } | |
615 | ||
616 | // this function is called when an assert fails | |
617 | void wxOnAssert(const wxChar *szFile, | |
618 | int nLine, | |
619 | const wxChar *szCond, | |
620 | const wxChar *szMsg) | |
621 | { | |
622 | // FIXME MT-unsafe | |
623 | static bool s_bInAssert = false; | |
624 | ||
625 | if ( s_bInAssert ) | |
626 | { | |
627 | // He-e-e-e-elp!! we're trapped in endless loop | |
628 | wxTrap(); | |
629 | ||
630 | s_bInAssert = false; | |
631 | ||
632 | return; | |
633 | } | |
634 | ||
635 | s_bInAssert = true; | |
636 | ||
637 | if ( !wxTheApp ) | |
638 | { | |
639 | // by default, show the assert dialog box -- we can't customize this | |
640 | // behaviour | |
641 | ShowAssertDialog(szFile, nLine, szCond, szMsg); | |
642 | } | |
643 | else | |
644 | { | |
645 | // let the app process it as it wants | |
646 | wxTheApp->OnAssert(szFile, nLine, szCond, szMsg); | |
647 | } | |
648 | ||
649 | s_bInAssert = false; | |
650 | } | |
651 | ||
652 | #endif // __WXDEBUG__ | |
653 | ||
654 | // ============================================================================ | |
655 | // private functions implementation | |
656 | // ============================================================================ | |
657 | ||
658 | #ifdef __WXDEBUG__ | |
659 | ||
660 | static void LINKAGEMODE SetTraceMasks() | |
661 | { | |
662 | #if wxUSE_LOG | |
663 | wxString mask; | |
664 | if ( wxGetEnv(wxT("WXTRACE"), &mask) ) | |
665 | { | |
666 | wxStringTokenizer tkn(mask, wxT(",;:")); | |
667 | while ( tkn.HasMoreTokens() ) | |
668 | wxLog::AddTraceMask(tkn.GetNextToken()); | |
669 | } | |
670 | #endif // wxUSE_LOG | |
671 | } | |
672 | ||
673 | bool DoShowAssertDialog(const wxString& msg) | |
674 | { | |
675 | // under MSW we can show the dialog even in the console mode | |
676 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
677 | wxString msgDlg(msg); | |
678 | ||
679 | // this message is intentionally not translated -- it is for | |
680 | // developpers only | |
681 | msgDlg += wxT("\nDo you want to stop the program?\n") | |
682 | wxT("You can also choose [Cancel] to suppress ") | |
683 | wxT("further warnings."); | |
684 | ||
685 | switch ( ::MessageBox(NULL, msgDlg, _T("wxWidgets Debug Alert"), | |
686 | MB_YESNOCANCEL | MB_ICONSTOP ) ) | |
687 | { | |
688 | case IDYES: | |
689 | wxTrap(); | |
690 | break; | |
691 | ||
692 | case IDCANCEL: | |
693 | // stop the asserts | |
694 | return true; | |
695 | ||
696 | //case IDNO: nothing to do | |
697 | } | |
698 | #else // !__WXMSW__ | |
699 | wxFprintf(stderr, wxT("%s\n"), msg.c_str()); | |
700 | fflush(stderr); | |
701 | ||
702 | // TODO: ask the user to enter "Y" or "N" on the console? | |
703 | wxTrap(); | |
704 | #endif // __WXMSW__/!__WXMSW__ | |
705 | ||
706 | // continue with the asserts | |
707 | return false; | |
708 | } | |
709 | ||
710 | // show the assert modal dialog | |
711 | static | |
712 | void ShowAssertDialog(const wxChar *szFile, | |
713 | int nLine, | |
714 | const wxChar *szCond, | |
715 | const wxChar *szMsg, | |
716 | wxAppTraits *traits) | |
717 | { | |
718 | // this variable can be set to true to suppress "assert failure" messages | |
719 | static bool s_bNoAsserts = false; | |
720 | ||
721 | wxString msg; | |
722 | msg.reserve(2048); | |
723 | ||
724 | // make life easier for people using VC++ IDE by using this format: like | |
725 | // this, clicking on the message will take us immediately to the place of | |
726 | // the failed assert | |
727 | msg.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile, nLine, szCond); | |
728 | ||
729 | if ( szMsg ) | |
730 | { | |
731 | msg << _T(": ") << szMsg; | |
732 | } | |
733 | else // no message given | |
734 | { | |
735 | msg << _T('.'); | |
736 | } | |
737 | ||
738 | #if wxUSE_STACKWALKER | |
739 | class StackDump : public wxStackWalker | |
740 | { | |
741 | public: | |
742 | StackDump() { } | |
743 | ||
744 | const wxString& GetStackTrace() const { return m_stackTrace; } | |
745 | ||
746 | protected: | |
747 | virtual void OnStackFrame(const wxStackFrame& frame) | |
748 | { | |
749 | m_stackTrace << wxString::Format(_T("[%02d] "), frame.GetLevel()); | |
750 | ||
751 | wxString name = frame.GetName(); | |
752 | if ( !name.empty() ) | |
753 | { | |
754 | m_stackTrace << wxString::Format(_T("%-40s"), name.c_str()); | |
755 | } | |
756 | else | |
757 | { | |
758 | m_stackTrace << wxString::Format | |
759 | ( | |
760 | _T("0x%08lx"), | |
761 | (unsigned long)frame.GetAddress() | |
762 | ); | |
763 | } | |
764 | ||
765 | if ( frame.HasSourceLocation() ) | |
766 | { | |
767 | m_stackTrace << _T('\t') | |
768 | << frame.GetFileName() | |
769 | << _T(':') | |
770 | << frame.GetLine(); | |
771 | } | |
772 | ||
773 | m_stackTrace << _T('\n'); | |
774 | } | |
775 | ||
776 | private: | |
777 | wxString m_stackTrace; | |
778 | }; | |
779 | ||
780 | StackDump dump; | |
781 | dump.Walk(5); // don't show OnAssert() call itself | |
782 | wxString stackTrace = dump.GetStackTrace(); | |
783 | ||
784 | // don't show more than maxLines or we could get a dialog too tall to be | |
785 | // shown on screen: 20 should be ok everywhere as even with 15 pixel high | |
786 | // characters it is still only 300 pixels... | |
787 | const int maxLines = 20; | |
788 | int count = stackTrace.Freq(wxT('\n')); | |
789 | if (count > maxLines) | |
790 | { | |
791 | int i; | |
792 | for (i = 0; i < count - maxLines; i++) | |
793 | stackTrace = stackTrace.BeforeLast(wxT('\n')); | |
794 | } | |
795 | if ( !stackTrace.empty() ) | |
796 | { | |
797 | msg << _T("\n\nCall stack:\n") | |
798 | << stackTrace; | |
799 | } | |
800 | #endif // wxUSE_STACKWALKER | |
801 | ||
802 | #if wxUSE_THREADS | |
803 | // if we are not in the main thread, output the assert directly and trap | |
804 | // since dialogs cannot be displayed | |
805 | if ( !wxThread::IsMain() ) | |
806 | { | |
807 | msg += wxT(" [in child thread]"); | |
808 | ||
809 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
810 | msg << wxT("\r\n"); | |
811 | OutputDebugString(msg ); | |
812 | #else | |
813 | // send to stderr | |
814 | wxFprintf(stderr, wxT("%s\n"), msg.c_str()); | |
815 | fflush(stderr); | |
816 | #endif | |
817 | // He-e-e-e-elp!! we're asserting in a child thread | |
818 | wxTrap(); | |
819 | } | |
820 | else | |
821 | #endif // wxUSE_THREADS | |
822 | ||
823 | if ( !s_bNoAsserts ) | |
824 | { | |
825 | // send it to the normal log destination | |
826 | wxLogDebug(_T("%s"), msg.c_str()); | |
827 | ||
828 | if ( traits ) | |
829 | { | |
830 | // delegate showing assert dialog (if possible) to that class | |
831 | s_bNoAsserts = traits->ShowAssertDialog(msg); | |
832 | } | |
833 | else // no traits object | |
834 | { | |
835 | // fall back to the function of last resort | |
836 | s_bNoAsserts = DoShowAssertDialog(msg); | |
837 | } | |
838 | } | |
839 | } | |
840 | ||
841 | #endif // __WXDEBUG__ | |
842 |