]>
Commit | Line | Data |
---|---|---|
e2478fde | 1 | /////////////////////////////////////////////////////////////////////////////// |
8ecff181 | 2 | // Name: src/common/appbase.cpp |
e2478fde VZ |
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 | |
57bd4c60 WS |
28 | #ifdef __WXMSW__ |
29 | #include "wx/msw/wrapwin.h" // includes windows.h for MessageBox() | |
30 | #endif | |
8ecff181 | 31 | #include "wx/list.h" |
8df6de97 VS |
32 | #include "wx/app.h" |
33 | #include "wx/intl.h" | |
86f8d1a8 | 34 | #include "wx/log.h" |
de6185e2 | 35 | #include "wx/utils.h" |
e2478fde VZ |
36 | #endif //WX_PRECOMP |
37 | ||
38 | #include "wx/apptrait.h" | |
39 | #include "wx/cmdline.h" | |
40 | #include "wx/confbase.h" | |
ce39c39e | 41 | #include "wx/filename.h" |
e2478fde VZ |
42 | #include "wx/msgout.h" |
43 | #include "wx/tokenzr.h" | |
44 | ||
45 | #if !defined(__WXMSW__) || defined(__WXMICROWIN__) | |
46 | #include <signal.h> // for SIGTRAP used by wxTrap() | |
47 | #endif //Win/Unix | |
48 | ||
1c193821 JS |
49 | #if wxUSE_FONTMAP |
50 | #include "wx/fontmap.h" | |
51 | #endif // wxUSE_FONTMAP | |
52 | ||
f0756afe DE |
53 | #if defined(__DARWIN__) && defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS |
54 | // For MacTypes.h for Debugger function | |
55 | #include <CoreFoundation/CFBase.h> | |
56 | #endif | |
57 | ||
e2478fde | 58 | #if defined(__WXMAC__) |
22f69dd4 | 59 | #ifdef __DARWIN__ |
40ba2f3b | 60 | #include <CoreServices/CoreServices.h> |
22f69dd4 VZ |
61 | #else |
62 | #include "wx/mac/private.h" // includes mac headers | |
63 | #endif | |
64 | #endif // __WXMAC__ | |
e2478fde | 65 | |
6c8f8d92 | 66 | #ifdef __WXDEBUG__ |
7b1c3469 | 67 | #if wxUSE_STACKWALKER |
6c8f8d92 | 68 | #include "wx/stackwalk.h" |
4a92d4bc VZ |
69 | #ifdef __WXMSW__ |
70 | #include "wx/msw/debughlp.h" | |
71 | #endif | |
6c8f8d92 VZ |
72 | #endif // wxUSE_STACKWALKER |
73 | #endif // __WXDEBUG__ | |
74 | ||
121aea46 MW |
75 | // wxABI_VERSION can be defined when compiling applications but it should be |
76 | // left undefined when compiling the library itself, it is then set to its | |
77 | // default value in version.h | |
78 | #if wxABI_VERSION != wxMAJOR_VERSION * 10000 + wxMINOR_VERSION * 100 + 99 | |
79 | #error "wxABI_VERSION should not be defined when compiling the library" | |
80 | #endif | |
81 | ||
e2478fde VZ |
82 | // ---------------------------------------------------------------------------- |
83 | // private functions prototypes | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
86 | #ifdef __WXDEBUG__ | |
87 | // really just show the assert dialog | |
88 | static bool DoShowAssertDialog(const wxString& msg); | |
89 | ||
90 | // prepare for showing the assert dialog, use the given traits or | |
91 | // DoShowAssertDialog() as last fallback to really show it | |
92 | static | |
93 | void ShowAssertDialog(const wxChar *szFile, | |
94 | int nLine, | |
dfa0b52f | 95 | const wxChar *szFunc, |
e2478fde VZ |
96 | const wxChar *szCond, |
97 | const wxChar *szMsg, | |
98 | wxAppTraits *traits = NULL); | |
99 | ||
100 | // turn on the trace masks specified in the env variable WXTRACE | |
101 | static void LINKAGEMODE SetTraceMasks(); | |
102 | #endif // __WXDEBUG__ | |
103 | ||
104 | // ---------------------------------------------------------------------------- | |
105 | // global vars | |
106 | // ---------------------------------------------------------------------------- | |
107 | ||
7cafd224 | 108 | wxAppConsole *wxAppConsole::ms_appInstance = NULL; |
e2478fde VZ |
109 | |
110 | wxAppInitializerFunction wxAppConsole::ms_appInitFn = NULL; | |
111 | ||
112 | // ============================================================================ | |
113 | // wxAppConsole implementation | |
114 | // ============================================================================ | |
115 | ||
116 | // ---------------------------------------------------------------------------- | |
117 | // ctor/dtor | |
118 | // ---------------------------------------------------------------------------- | |
119 | ||
120 | wxAppConsole::wxAppConsole() | |
121 | { | |
122 | m_traits = NULL; | |
123 | ||
7cafd224 | 124 | ms_appInstance = this; |
e2478fde VZ |
125 | |
126 | #ifdef __WXDEBUG__ | |
127 | SetTraceMasks(); | |
bc334f39 RD |
128 | #if wxUSE_UNICODE |
129 | // In unicode mode the SetTraceMasks call can cause an apptraits to be | |
130 | // created, but since we are still in the constructor the wrong kind will | |
131 | // be created for GUI apps. Destroy it so it can be created again later. | |
132 | delete m_traits; | |
133 | m_traits = NULL; | |
134 | #endif | |
e2478fde VZ |
135 | #endif |
136 | } | |
137 | ||
138 | wxAppConsole::~wxAppConsole() | |
139 | { | |
140 | delete m_traits; | |
141 | } | |
142 | ||
94826170 VZ |
143 | // ---------------------------------------------------------------------------- |
144 | // initilization/cleanup | |
145 | // ---------------------------------------------------------------------------- | |
146 | ||
8d79ccef | 147 | bool wxAppConsole::Initialize(int& argcOrig, wxChar **argvOrig) |
94826170 VZ |
148 | { |
149 | // remember the command line arguments | |
8d79ccef VZ |
150 | argc = argcOrig; |
151 | argv = argvOrig; | |
94826170 | 152 | |
4055ed82 | 153 | #ifndef __WXPALMOS__ |
d546eba9 | 154 | if ( m_appName.empty() && argv ) |
94826170 VZ |
155 | { |
156 | // the application name is, by default, the name of its executable file | |
94826170 | 157 | wxFileName::SplitPath(argv[0], NULL, &m_appName, NULL); |
94826170 | 158 | } |
ffecfa5a | 159 | #endif |
94826170 VZ |
160 | |
161 | return true; | |
162 | } | |
163 | ||
164 | void wxAppConsole::CleanUp() | |
165 | { | |
166 | } | |
167 | ||
e2478fde VZ |
168 | // ---------------------------------------------------------------------------- |
169 | // OnXXX() callbacks | |
170 | // ---------------------------------------------------------------------------- | |
171 | ||
172 | bool wxAppConsole::OnInit() | |
173 | { | |
174 | #if wxUSE_CMDLINE_PARSER | |
175 | wxCmdLineParser parser(argc, argv); | |
176 | ||
177 | OnInitCmdLine(parser); | |
178 | ||
179 | bool cont; | |
4629016d | 180 | switch ( parser.Parse(false /* don't show usage */) ) |
e2478fde VZ |
181 | { |
182 | case -1: | |
183 | cont = OnCmdLineHelp(parser); | |
184 | break; | |
185 | ||
186 | case 0: | |
187 | cont = OnCmdLineParsed(parser); | |
188 | break; | |
189 | ||
190 | default: | |
191 | cont = OnCmdLineError(parser); | |
192 | break; | |
193 | } | |
194 | ||
195 | if ( !cont ) | |
4629016d | 196 | return false; |
e2478fde VZ |
197 | #endif // wxUSE_CMDLINE_PARSER |
198 | ||
4629016d | 199 | return true; |
e2478fde VZ |
200 | } |
201 | ||
202 | int wxAppConsole::OnExit() | |
203 | { | |
204 | #if wxUSE_CONFIG | |
205 | // delete the config object if any (don't use Get() here, but Set() | |
206 | // because Get() could create a new config object) | |
207 | delete wxConfigBase::Set((wxConfigBase *) NULL); | |
208 | #endif // wxUSE_CONFIG | |
209 | ||
e2478fde VZ |
210 | return 0; |
211 | } | |
212 | ||
213 | void wxAppConsole::Exit() | |
214 | { | |
215 | exit(-1); | |
216 | } | |
217 | ||
978af864 VZ |
218 | wxLayoutDirection wxAppConsole::GetLayoutDirection() const |
219 | { | |
220 | #if wxUSE_INTL | |
221 | const wxLocale *const locale = wxGetLocale(); | |
222 | if ( locale ) | |
223 | { | |
224 | const wxLanguageInfo *const | |
225 | info = wxLocale::GetLanguageInfo(locale->GetLanguage()); | |
b137e493 | 226 | |
978af864 VZ |
227 | if ( info ) |
228 | return info->LayoutDirection; | |
229 | } | |
230 | #endif // wxUSE_INTL | |
231 | ||
232 | // we don't know | |
233 | return wxLayout_Default; | |
234 | } | |
235 | ||
e2478fde VZ |
236 | // ---------------------------------------------------------------------------- |
237 | // traits stuff | |
238 | // ---------------------------------------------------------------------------- | |
239 | ||
240 | wxAppTraits *wxAppConsole::CreateTraits() | |
241 | { | |
7843d11b | 242 | return new wxConsoleAppTraits; |
e2478fde VZ |
243 | } |
244 | ||
245 | wxAppTraits *wxAppConsole::GetTraits() | |
246 | { | |
247 | // FIXME-MT: protect this with a CS? | |
248 | if ( !m_traits ) | |
249 | { | |
250 | m_traits = CreateTraits(); | |
251 | ||
252 | wxASSERT_MSG( m_traits, _T("wxApp::CreateTraits() failed?") ); | |
253 | } | |
254 | ||
255 | return m_traits; | |
256 | } | |
257 | ||
258 | // we must implement CreateXXX() in wxApp itself for backwards compatibility | |
259 | #if WXWIN_COMPATIBILITY_2_4 | |
260 | ||
261 | #if wxUSE_LOG | |
262 | ||
263 | wxLog *wxAppConsole::CreateLogTarget() | |
264 | { | |
265 | wxAppTraits *traits = GetTraits(); | |
266 | return traits ? traits->CreateLogTarget() : NULL; | |
267 | } | |
268 | ||
269 | #endif // wxUSE_LOG | |
270 | ||
271 | wxMessageOutput *wxAppConsole::CreateMessageOutput() | |
272 | { | |
273 | wxAppTraits *traits = GetTraits(); | |
274 | return traits ? traits->CreateMessageOutput() : NULL; | |
275 | } | |
276 | ||
277 | #endif // WXWIN_COMPATIBILITY_2_4 | |
278 | ||
279 | // ---------------------------------------------------------------------------- | |
280 | // event processing | |
281 | // ---------------------------------------------------------------------------- | |
282 | ||
283 | void wxAppConsole::ProcessPendingEvents() | |
284 | { | |
de4de64f | 285 | #if wxUSE_THREADS |
f54043c2 RN |
286 | if ( !wxPendingEventsLocker ) |
287 | return; | |
7fdfc5b3 | 288 | #endif |
8ecff181 | 289 | |
e2478fde VZ |
290 | // ensure that we're the only thread to modify the pending events list |
291 | wxENTER_CRIT_SECT( *wxPendingEventsLocker ); | |
292 | ||
293 | if ( !wxPendingEvents ) | |
294 | { | |
295 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); | |
296 | return; | |
297 | } | |
298 | ||
299 | // iterate until the list becomes empty | |
df5168c4 | 300 | wxList::compatibility_iterator node = wxPendingEvents->GetFirst(); |
e2478fde VZ |
301 | while (node) |
302 | { | |
303 | wxEvtHandler *handler = (wxEvtHandler *)node->GetData(); | |
df5168c4 | 304 | wxPendingEvents->Erase(node); |
e2478fde VZ |
305 | |
306 | // In ProcessPendingEvents(), new handlers might be add | |
307 | // and we can safely leave the critical section here. | |
308 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); | |
8ecff181 | 309 | |
e2478fde | 310 | handler->ProcessPendingEvents(); |
de4de64f | 311 | |
e2478fde VZ |
312 | wxENTER_CRIT_SECT( *wxPendingEventsLocker ); |
313 | ||
314 | node = wxPendingEvents->GetFirst(); | |
315 | } | |
316 | ||
317 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); | |
318 | } | |
319 | ||
320 | int wxAppConsole::FilterEvent(wxEvent& WXUNUSED(event)) | |
321 | { | |
322 | // process the events normally by default | |
323 | return -1; | |
324 | } | |
325 | ||
78361a0e VZ |
326 | // ---------------------------------------------------------------------------- |
327 | // exception handling | |
328 | // ---------------------------------------------------------------------------- | |
329 | ||
6f054ac5 VZ |
330 | #if wxUSE_EXCEPTIONS |
331 | ||
332 | void | |
333 | wxAppConsole::HandleEvent(wxEvtHandler *handler, | |
334 | wxEventFunction func, | |
335 | wxEvent& event) const | |
336 | { | |
96d38c7e VS |
337 | // by default, simply call the handler |
338 | (handler->*func)(event); | |
6f054ac5 VZ |
339 | } |
340 | ||
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 | ||
dfa0b52f VZ |
455 | void wxAppConsole::OnAssertFailure(const wxChar *file, |
456 | int line, | |
457 | const wxChar *func, | |
458 | const wxChar *cond, | |
459 | const wxChar *msg) | |
460 | { | |
461 | ShowAssertDialog(file, line, func, cond, msg, GetTraits()); | |
462 | } | |
463 | ||
e2478fde VZ |
464 | void wxAppConsole::OnAssert(const wxChar *file, |
465 | int line, | |
466 | const wxChar *cond, | |
467 | const wxChar *msg) | |
468 | { | |
acc476c5 | 469 | OnAssertFailure(file, line, NULL, cond, msg); |
e2478fde VZ |
470 | } |
471 | ||
472 | #endif // __WXDEBUG__ | |
473 | ||
eecb33b0 WS |
474 | #if WXWIN_COMPATIBILITY_2_4 |
475 | ||
476 | bool wxAppConsole::CheckBuildOptions(const wxBuildOptions& buildOptions) | |
477 | { | |
478 | return CheckBuildOptions(buildOptions.m_signature, "your program"); | |
479 | } | |
480 | ||
481 | #endif | |
482 | ||
e2478fde VZ |
483 | // ============================================================================ |
484 | // other classes implementations | |
485 | // ============================================================================ | |
486 | ||
487 | // ---------------------------------------------------------------------------- | |
488 | // wxConsoleAppTraitsBase | |
489 | // ---------------------------------------------------------------------------- | |
490 | ||
491 | #if wxUSE_LOG | |
492 | ||
493 | wxLog *wxConsoleAppTraitsBase::CreateLogTarget() | |
494 | { | |
495 | return new wxLogStderr; | |
496 | } | |
497 | ||
498 | #endif // wxUSE_LOG | |
499 | ||
500 | wxMessageOutput *wxConsoleAppTraitsBase::CreateMessageOutput() | |
501 | { | |
502 | return new wxMessageOutputStderr; | |
503 | } | |
504 | ||
505 | #if wxUSE_FONTMAP | |
506 | ||
507 | wxFontMapper *wxConsoleAppTraitsBase::CreateFontMapper() | |
508 | { | |
509 | return (wxFontMapper *)new wxFontMapperBase; | |
510 | } | |
511 | ||
512 | #endif // wxUSE_FONTMAP | |
513 | ||
f0244295 VZ |
514 | wxRendererNative *wxConsoleAppTraitsBase::CreateRenderer() |
515 | { | |
516 | // console applications don't use renderers | |
517 | return NULL; | |
518 | } | |
519 | ||
8df6de97 | 520 | #ifdef __WXDEBUG__ |
e2478fde VZ |
521 | bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString& msg) |
522 | { | |
523 | return wxAppTraitsBase::ShowAssertDialog(msg); | |
524 | } | |
8df6de97 | 525 | #endif |
e2478fde VZ |
526 | |
527 | bool wxConsoleAppTraitsBase::HasStderr() | |
528 | { | |
529 | // console applications always have stderr, even under Mac/Windows | |
530 | return true; | |
531 | } | |
532 | ||
533 | void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject *object) | |
534 | { | |
535 | delete object; | |
536 | } | |
537 | ||
538 | void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject * WXUNUSED(object)) | |
539 | { | |
540 | // nothing to do | |
541 | } | |
2dbc444a | 542 | |
38bb138f VS |
543 | #if wxUSE_SOCKETS |
544 | GSocketGUIFunctionsTable* wxConsoleAppTraitsBase::GetSocketGUIFunctionsTable() | |
545 | { | |
546 | return NULL; | |
547 | } | |
548 | #endif | |
e2478fde VZ |
549 | |
550 | // ---------------------------------------------------------------------------- | |
551 | // wxAppTraits | |
552 | // ---------------------------------------------------------------------------- | |
553 | ||
554 | #ifdef __WXDEBUG__ | |
555 | ||
556 | bool wxAppTraitsBase::ShowAssertDialog(const wxString& msg) | |
557 | { | |
558 | return DoShowAssertDialog(msg); | |
559 | } | |
560 | ||
561 | #endif // __WXDEBUG__ | |
562 | ||
e2478fde VZ |
563 | // ============================================================================ |
564 | // global functions implementation | |
565 | // ============================================================================ | |
566 | ||
567 | void wxExit() | |
568 | { | |
569 | if ( wxTheApp ) | |
570 | { | |
571 | wxTheApp->Exit(); | |
572 | } | |
573 | else | |
574 | { | |
575 | // what else can we do? | |
576 | exit(-1); | |
577 | } | |
578 | } | |
579 | ||
580 | void wxWakeUpIdle() | |
581 | { | |
582 | if ( wxTheApp ) | |
583 | { | |
584 | wxTheApp->WakeUpIdle(); | |
585 | } | |
586 | //else: do nothing, what can we do? | |
587 | } | |
588 | ||
589 | #ifdef __WXDEBUG__ | |
590 | ||
591 | // wxASSERT() helper | |
592 | bool wxAssertIsEqual(int x, int y) | |
593 | { | |
594 | return x == y; | |
595 | } | |
596 | ||
597 | // break into the debugger | |
598 | void wxTrap() | |
599 | { | |
600 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
601 | DebugBreak(); | |
602 | #elif defined(__WXMAC__) && !defined(__DARWIN__) | |
603 | #if __powerc | |
604 | Debugger(); | |
605 | #else | |
606 | SysBreak(); | |
607 | #endif | |
f0756afe DE |
608 | #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS |
609 | Debugger(); | |
e2478fde VZ |
610 | #elif defined(__UNIX__) |
611 | raise(SIGTRAP); | |
612 | #else | |
613 | // TODO | |
614 | #endif // Win/Unix | |
615 | } | |
616 | ||
e2478fde VZ |
617 | // this function is called when an assert fails |
618 | void wxOnAssert(const wxChar *szFile, | |
619 | int nLine, | |
acc476c5 | 620 | const char *szFunc, |
e2478fde VZ |
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 | 638 | |
acc476c5 VZ |
639 | // __FUNCTION__ is always in ASCII, convert it to wide char if needed |
640 | const wxString strFunc = wxString::FromAscii(szFunc); | |
641 | ||
e2478fde VZ |
642 | if ( !wxTheApp ) |
643 | { | |
644 | // by default, show the assert dialog box -- we can't customize this | |
645 | // behaviour | |
acc476c5 | 646 | ShowAssertDialog(szFile, nLine, strFunc, szCond, szMsg); |
e2478fde VZ |
647 | } |
648 | else | |
649 | { | |
650 | // let the app process it as it wants | |
acc476c5 | 651 | wxTheApp->OnAssertFailure(szFile, nLine, strFunc, szCond, szMsg); |
e2478fde VZ |
652 | } |
653 | ||
4629016d | 654 | s_bInAssert = false; |
e2478fde VZ |
655 | } |
656 | ||
657 | #endif // __WXDEBUG__ | |
658 | ||
659 | // ============================================================================ | |
660 | // private functions implementation | |
661 | // ============================================================================ | |
662 | ||
663 | #ifdef __WXDEBUG__ | |
664 | ||
665 | static void LINKAGEMODE SetTraceMasks() | |
666 | { | |
667 | #if wxUSE_LOG | |
668 | wxString mask; | |
669 | if ( wxGetEnv(wxT("WXTRACE"), &mask) ) | |
670 | { | |
671 | wxStringTokenizer tkn(mask, wxT(",;:")); | |
672 | while ( tkn.HasMoreTokens() ) | |
673 | wxLog::AddTraceMask(tkn.GetNextToken()); | |
674 | } | |
675 | #endif // wxUSE_LOG | |
676 | } | |
677 | ||
678 | bool DoShowAssertDialog(const wxString& msg) | |
679 | { | |
680 | // under MSW we can show the dialog even in the console mode | |
681 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
682 | wxString msgDlg(msg); | |
683 | ||
684 | // this message is intentionally not translated -- it is for | |
685 | // developpers only | |
686 | msgDlg += wxT("\nDo you want to stop the program?\n") | |
687 | wxT("You can also choose [Cancel] to suppress ") | |
688 | wxT("further warnings."); | |
689 | ||
77ffb593 | 690 | switch ( ::MessageBox(NULL, msgDlg, _T("wxWidgets Debug Alert"), |
e2478fde VZ |
691 | MB_YESNOCANCEL | MB_ICONSTOP ) ) |
692 | { | |
693 | case IDYES: | |
694 | wxTrap(); | |
695 | break; | |
696 | ||
697 | case IDCANCEL: | |
698 | // stop the asserts | |
699 | return true; | |
700 | ||
701 | //case IDNO: nothing to do | |
702 | } | |
703 | #else // !__WXMSW__ | |
704 | wxFprintf(stderr, wxT("%s\n"), msg.c_str()); | |
705 | fflush(stderr); | |
706 | ||
707 | // TODO: ask the user to enter "Y" or "N" on the console? | |
708 | wxTrap(); | |
709 | #endif // __WXMSW__/!__WXMSW__ | |
710 | ||
711 | // continue with the asserts | |
712 | return false; | |
713 | } | |
714 | ||
388186f7 | 715 | #if wxUSE_STACKWALKER |
4a92d4bc | 716 | static wxString GetAssertStackTrace() |
e2478fde | 717 | { |
4a92d4bc | 718 | wxString stackTrace; |
e2478fde | 719 | |
6c8f8d92 VZ |
720 | class StackDump : public wxStackWalker |
721 | { | |
722 | public: | |
723 | StackDump() { } | |
724 | ||
725 | const wxString& GetStackTrace() const { return m_stackTrace; } | |
726 | ||
727 | protected: | |
728 | virtual void OnStackFrame(const wxStackFrame& frame) | |
729 | { | |
17a1ebd1 VZ |
730 | m_stackTrace << wxString::Format |
731 | ( | |
732 | _T("[%02d] "), | |
733 | wx_truncate_cast(int, frame.GetLevel()) | |
734 | ); | |
6c8f8d92 VZ |
735 | |
736 | wxString name = frame.GetName(); | |
737 | if ( !name.empty() ) | |
738 | { | |
739 | m_stackTrace << wxString::Format(_T("%-40s"), name.c_str()); | |
740 | } | |
741 | else | |
742 | { | |
677856c9 | 743 | m_stackTrace << wxString::Format(_T("%p"), frame.GetAddress()); |
6c8f8d92 VZ |
744 | } |
745 | ||
746 | if ( frame.HasSourceLocation() ) | |
747 | { | |
748 | m_stackTrace << _T('\t') | |
749 | << frame.GetFileName() | |
750 | << _T(':') | |
751 | << frame.GetLine(); | |
752 | } | |
753 | ||
754 | m_stackTrace << _T('\n'); | |
755 | } | |
756 | ||
757 | private: | |
758 | wxString m_stackTrace; | |
759 | }; | |
760 | ||
761 | StackDump dump; | |
b0d8bfa6 | 762 | dump.Walk(2); // don't show OnAssert() call itself |
4a92d4bc | 763 | stackTrace = dump.GetStackTrace(); |
2c9b3131 | 764 | |
a625f454 VZ |
765 | // don't show more than maxLines or we could get a dialog too tall to be |
766 | // shown on screen: 20 should be ok everywhere as even with 15 pixel high | |
767 | // characters it is still only 300 pixels... | |
4a92d4bc VZ |
768 | static const int maxLines = 20; |
769 | const int count = stackTrace.Freq(wxT('\n')); | |
770 | for ( int i = 0; i < count - maxLines; i++ ) | |
771 | stackTrace = stackTrace.BeforeLast(wxT('\n')); | |
772 | ||
773 | return stackTrace; | |
774 | } | |
388186f7 | 775 | #endif // wxUSE_STACKWALKER |
4a92d4bc VZ |
776 | |
777 | // show the assert modal dialog | |
778 | static | |
779 | void ShowAssertDialog(const wxChar *szFile, | |
780 | int nLine, | |
dfa0b52f | 781 | const wxChar *szFunc, |
4a92d4bc VZ |
782 | const wxChar *szCond, |
783 | const wxChar *szMsg, | |
784 | wxAppTraits *traits) | |
785 | { | |
786 | // this variable can be set to true to suppress "assert failure" messages | |
787 | static bool s_bNoAsserts = false; | |
788 | ||
789 | wxString msg; | |
790 | msg.reserve(2048); | |
791 | ||
792 | // make life easier for people using VC++ IDE by using this format: like | |
793 | // this, clicking on the message will take us immediately to the place of | |
794 | // the failed assert | |
795 | msg.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile, nLine, szCond); | |
796 | ||
dfa0b52f VZ |
797 | // add the function name, if any |
798 | if ( szFunc && *szFunc ) | |
799 | msg << _T(" in ") << szFunc << _T("()"); | |
800 | ||
801 | // and the message itself | |
4a92d4bc | 802 | if ( szMsg ) |
2c9b3131 | 803 | { |
4a92d4bc | 804 | msg << _T(": ") << szMsg; |
2c9b3131 | 805 | } |
4a92d4bc VZ |
806 | else // no message given |
807 | { | |
808 | msg << _T('.'); | |
809 | } | |
810 | ||
811 | #if wxUSE_STACKWALKER | |
812 | const wxString stackTrace = GetAssertStackTrace(); | |
6c8f8d92 VZ |
813 | if ( !stackTrace.empty() ) |
814 | { | |
4a92d4bc | 815 | msg << _T("\n\nCall stack:\n") << stackTrace; |
6c8f8d92 VZ |
816 | } |
817 | #endif // wxUSE_STACKWALKER | |
818 | ||
e2478fde VZ |
819 | #if wxUSE_THREADS |
820 | // if we are not in the main thread, output the assert directly and trap | |
821 | // since dialogs cannot be displayed | |
822 | if ( !wxThread::IsMain() ) | |
823 | { | |
824 | msg += wxT(" [in child thread]"); | |
825 | ||
826 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
827 | msg << wxT("\r\n"); | |
828 | OutputDebugString(msg ); | |
829 | #else | |
830 | // send to stderr | |
831 | wxFprintf(stderr, wxT("%s\n"), msg.c_str()); | |
832 | fflush(stderr); | |
833 | #endif | |
834 | // He-e-e-e-elp!! we're asserting in a child thread | |
835 | wxTrap(); | |
836 | } | |
6c8f8d92 | 837 | else |
e2478fde VZ |
838 | #endif // wxUSE_THREADS |
839 | ||
840 | if ( !s_bNoAsserts ) | |
841 | { | |
842 | // send it to the normal log destination | |
56fae7b8 | 843 | wxLogDebug(_T("%s"), msg.c_str()); |
e2478fde VZ |
844 | |
845 | if ( traits ) | |
846 | { | |
847 | // delegate showing assert dialog (if possible) to that class | |
848 | s_bNoAsserts = traits->ShowAssertDialog(msg); | |
849 | } | |
850 | else // no traits object | |
851 | { | |
852 | // fall back to the function of last resort | |
853 | s_bNoAsserts = DoShowAssertDialog(msg); | |
854 | } | |
855 | } | |
856 | } | |
857 | ||
858 | #endif // __WXDEBUG__ |