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