]>
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 | ||
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 |
8ecff181 | 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(); |
5fa150e2 RR |
283 | |
284 | while (node && | |
285 | ((wxEvtHandler *)node->GetData())->IsEventHandlingInProgress() && | |
286 | ((wxEvtHandler *)node->GetData())->IsReentranceAllowed() == false) | |
287 | { | |
288 | // skip over event | |
289 | node = node->GetNext(); | |
290 | } | |
291 | ||
e2478fde VZ |
292 | while (node) |
293 | { | |
294 | wxEvtHandler *handler = (wxEvtHandler *)node->GetData(); | |
df5168c4 | 295 | wxPendingEvents->Erase(node); |
e2478fde VZ |
296 | |
297 | // In ProcessPendingEvents(), new handlers might be add | |
298 | // and we can safely leave the critical section here. | |
299 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); | |
8ecff181 | 300 | |
e2478fde | 301 | handler->ProcessPendingEvents(); |
de4de64f | 302 | |
e2478fde VZ |
303 | wxENTER_CRIT_SECT( *wxPendingEventsLocker ); |
304 | ||
305 | node = wxPendingEvents->GetFirst(); | |
5fa150e2 RR |
306 | |
307 | while (node && | |
308 | ((wxEvtHandler *)node->GetData())->IsEventHandlingInProgress() && | |
309 | ((wxEvtHandler *)node->GetData())->IsReentranceAllowed() == false) | |
310 | { | |
311 | // skip over event | |
312 | node = node->GetNext(); | |
313 | } | |
e2478fde VZ |
314 | } |
315 | ||
316 | wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); | |
317 | } | |
318 | ||
319 | int wxAppConsole::FilterEvent(wxEvent& WXUNUSED(event)) | |
320 | { | |
321 | // process the events normally by default | |
322 | return -1; | |
323 | } | |
324 | ||
78361a0e VZ |
325 | // ---------------------------------------------------------------------------- |
326 | // exception handling | |
327 | // ---------------------------------------------------------------------------- | |
328 | ||
6f054ac5 VZ |
329 | #if wxUSE_EXCEPTIONS |
330 | ||
331 | void | |
332 | wxAppConsole::HandleEvent(wxEvtHandler *handler, | |
333 | wxEventFunction func, | |
334 | wxEvent& event) const | |
335 | { | |
96d38c7e VS |
336 | // by default, simply call the handler |
337 | (handler->*func)(event); | |
6f054ac5 VZ |
338 | } |
339 | ||
340 | #endif // wxUSE_EXCEPTIONS | |
341 | ||
e2478fde VZ |
342 | // ---------------------------------------------------------------------------- |
343 | // cmd line parsing | |
344 | // ---------------------------------------------------------------------------- | |
345 | ||
346 | #if wxUSE_CMDLINE_PARSER | |
347 | ||
348 | #define OPTION_VERBOSE _T("verbose") | |
e2478fde VZ |
349 | |
350 | void wxAppConsole::OnInitCmdLine(wxCmdLineParser& parser) | |
351 | { | |
352 | // the standard command line options | |
353 | static const wxCmdLineEntryDesc cmdLineDesc[] = | |
354 | { | |
355 | { | |
356 | wxCMD_LINE_SWITCH, | |
357 | _T("h"), | |
358 | _T("help"), | |
359 | gettext_noop("show this help message"), | |
360 | wxCMD_LINE_VAL_NONE, | |
361 | wxCMD_LINE_OPTION_HELP | |
362 | }, | |
363 | ||
364 | #if wxUSE_LOG | |
365 | { | |
366 | wxCMD_LINE_SWITCH, | |
b494c48b | 367 | wxEmptyString, |
e2478fde VZ |
368 | OPTION_VERBOSE, |
369 | gettext_noop("generate verbose log messages"), | |
370 | wxCMD_LINE_VAL_NONE, | |
371 | 0x0 | |
372 | }, | |
373 | #endif // wxUSE_LOG | |
374 | ||
e2478fde VZ |
375 | // terminator |
376 | { | |
377 | wxCMD_LINE_NONE, | |
b494c48b WS |
378 | wxEmptyString, |
379 | wxEmptyString, | |
380 | wxEmptyString, | |
e2478fde VZ |
381 | wxCMD_LINE_VAL_NONE, |
382 | 0x0 | |
383 | } | |
384 | }; | |
385 | ||
386 | parser.SetDesc(cmdLineDesc); | |
387 | } | |
388 | ||
389 | bool wxAppConsole::OnCmdLineParsed(wxCmdLineParser& parser) | |
390 | { | |
391 | #if wxUSE_LOG | |
392 | if ( parser.Found(OPTION_VERBOSE) ) | |
393 | { | |
fa0d3447 | 394 | wxLog::SetVerbose(true); |
e2478fde | 395 | } |
fa0d3447 WS |
396 | #else |
397 | wxUnusedVar(parser); | |
e2478fde VZ |
398 | #endif // wxUSE_LOG |
399 | ||
fa0d3447 | 400 | return true; |
e2478fde VZ |
401 | } |
402 | ||
403 | bool wxAppConsole::OnCmdLineHelp(wxCmdLineParser& parser) | |
404 | { | |
405 | parser.Usage(); | |
406 | ||
4629016d | 407 | return false; |
e2478fde VZ |
408 | } |
409 | ||
410 | bool wxAppConsole::OnCmdLineError(wxCmdLineParser& parser) | |
411 | { | |
412 | parser.Usage(); | |
413 | ||
4629016d | 414 | return false; |
e2478fde VZ |
415 | } |
416 | ||
417 | #endif // wxUSE_CMDLINE_PARSER | |
418 | ||
419 | // ---------------------------------------------------------------------------- | |
420 | // debugging support | |
421 | // ---------------------------------------------------------------------------- | |
422 | ||
423 | /* static */ | |
2a7c7605 VS |
424 | bool wxAppConsole::CheckBuildOptions(const char *optionsSignature, |
425 | const char *componentName) | |
e2478fde | 426 | { |
2a7c7605 VS |
427 | #if 0 // can't use wxLogTrace, not up and running yet |
428 | printf("checking build options object '%s' (ptr %p) in '%s'\n", | |
429 | optionsSignature, optionsSignature, componentName); | |
e2478fde VZ |
430 | #endif |
431 | ||
2a7c7605 | 432 | if ( strcmp(optionsSignature, WX_BUILD_OPTIONS_SIGNATURE) != 0 ) |
e2478fde | 433 | { |
2a7c7605 VS |
434 | wxString lib = wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE); |
435 | wxString prog = wxString::FromAscii(optionsSignature); | |
436 | wxString progName = wxString::FromAscii(componentName); | |
e2478fde | 437 | wxString msg; |
2dbc444a | 438 | |
b880adec | 439 | msg.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."), |
2a7c7605 | 440 | lib.c_str(), progName.c_str(), prog.c_str()); |
2dbc444a | 441 | |
095d49f2 | 442 | wxLogFatalError(msg.c_str()); |
e2478fde VZ |
443 | |
444 | // normally wxLogFatalError doesn't return | |
4629016d | 445 | return false; |
e2478fde VZ |
446 | } |
447 | #undef wxCMP | |
448 | ||
4629016d | 449 | return true; |
e2478fde VZ |
450 | } |
451 | ||
452 | #ifdef __WXDEBUG__ | |
453 | ||
dfa0b52f VZ |
454 | void wxAppConsole::OnAssertFailure(const wxChar *file, |
455 | int line, | |
456 | const wxChar *func, | |
457 | const wxChar *cond, | |
458 | const wxChar *msg) | |
459 | { | |
460 | ShowAssertDialog(file, line, func, cond, msg, GetTraits()); | |
461 | } | |
462 | ||
e2478fde VZ |
463 | void wxAppConsole::OnAssert(const wxChar *file, |
464 | int line, | |
465 | const wxChar *cond, | |
466 | const wxChar *msg) | |
467 | { | |
acc476c5 | 468 | OnAssertFailure(file, line, NULL, cond, msg); |
e2478fde VZ |
469 | } |
470 | ||
471 | #endif // __WXDEBUG__ | |
472 | ||
eecb33b0 WS |
473 | #if WXWIN_COMPATIBILITY_2_4 |
474 | ||
475 | bool wxAppConsole::CheckBuildOptions(const wxBuildOptions& buildOptions) | |
476 | { | |
477 | return CheckBuildOptions(buildOptions.m_signature, "your program"); | |
478 | } | |
479 | ||
480 | #endif | |
481 | ||
e2478fde VZ |
482 | // ============================================================================ |
483 | // other classes implementations | |
484 | // ============================================================================ | |
485 | ||
486 | // ---------------------------------------------------------------------------- | |
487 | // wxConsoleAppTraitsBase | |
488 | // ---------------------------------------------------------------------------- | |
489 | ||
490 | #if wxUSE_LOG | |
491 | ||
492 | wxLog *wxConsoleAppTraitsBase::CreateLogTarget() | |
493 | { | |
494 | return new wxLogStderr; | |
495 | } | |
496 | ||
497 | #endif // wxUSE_LOG | |
498 | ||
499 | wxMessageOutput *wxConsoleAppTraitsBase::CreateMessageOutput() | |
500 | { | |
501 | return new wxMessageOutputStderr; | |
502 | } | |
503 | ||
504 | #if wxUSE_FONTMAP | |
505 | ||
506 | wxFontMapper *wxConsoleAppTraitsBase::CreateFontMapper() | |
507 | { | |
508 | return (wxFontMapper *)new wxFontMapperBase; | |
509 | } | |
510 | ||
511 | #endif // wxUSE_FONTMAP | |
512 | ||
f0244295 VZ |
513 | wxRendererNative *wxConsoleAppTraitsBase::CreateRenderer() |
514 | { | |
515 | // console applications don't use renderers | |
516 | return NULL; | |
517 | } | |
518 | ||
8df6de97 | 519 | #ifdef __WXDEBUG__ |
e2478fde VZ |
520 | bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString& msg) |
521 | { | |
522 | return wxAppTraitsBase::ShowAssertDialog(msg); | |
523 | } | |
8df6de97 | 524 | #endif |
e2478fde VZ |
525 | |
526 | bool wxConsoleAppTraitsBase::HasStderr() | |
527 | { | |
528 | // console applications always have stderr, even under Mac/Windows | |
529 | return true; | |
530 | } | |
531 | ||
532 | void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject *object) | |
533 | { | |
534 | delete object; | |
535 | } | |
536 | ||
537 | void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject * WXUNUSED(object)) | |
538 | { | |
539 | // nothing to do | |
540 | } | |
2dbc444a | 541 | |
38bb138f VS |
542 | #if wxUSE_SOCKETS |
543 | GSocketGUIFunctionsTable* wxConsoleAppTraitsBase::GetSocketGUIFunctionsTable() | |
544 | { | |
545 | return NULL; | |
546 | } | |
547 | #endif | |
e2478fde VZ |
548 | |
549 | // ---------------------------------------------------------------------------- | |
550 | // wxAppTraits | |
551 | // ---------------------------------------------------------------------------- | |
552 | ||
553 | #ifdef __WXDEBUG__ | |
554 | ||
555 | bool wxAppTraitsBase::ShowAssertDialog(const wxString& msg) | |
556 | { | |
557 | return DoShowAssertDialog(msg); | |
558 | } | |
559 | ||
560 | #endif // __WXDEBUG__ | |
561 | ||
e2478fde VZ |
562 | // ============================================================================ |
563 | // global functions implementation | |
564 | // ============================================================================ | |
565 | ||
566 | void wxExit() | |
567 | { | |
568 | if ( wxTheApp ) | |
569 | { | |
570 | wxTheApp->Exit(); | |
571 | } | |
572 | else | |
573 | { | |
574 | // what else can we do? | |
575 | exit(-1); | |
576 | } | |
577 | } | |
578 | ||
579 | void wxWakeUpIdle() | |
580 | { | |
581 | if ( wxTheApp ) | |
582 | { | |
583 | wxTheApp->WakeUpIdle(); | |
584 | } | |
585 | //else: do nothing, what can we do? | |
586 | } | |
587 | ||
588 | #ifdef __WXDEBUG__ | |
589 | ||
590 | // wxASSERT() helper | |
591 | bool wxAssertIsEqual(int x, int y) | |
592 | { | |
593 | return x == y; | |
594 | } | |
595 | ||
596 | // break into the debugger | |
597 | void wxTrap() | |
598 | { | |
599 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
600 | DebugBreak(); | |
601 | #elif defined(__WXMAC__) && !defined(__DARWIN__) | |
602 | #if __powerc | |
603 | Debugger(); | |
604 | #else | |
605 | SysBreak(); | |
606 | #endif | |
f0756afe DE |
607 | #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS |
608 | Debugger(); | |
e2478fde VZ |
609 | #elif defined(__UNIX__) |
610 | raise(SIGTRAP); | |
611 | #else | |
612 | // TODO | |
613 | #endif // Win/Unix | |
614 | } | |
615 | ||
e2478fde VZ |
616 | // this function is called when an assert fails |
617 | void wxOnAssert(const wxChar *szFile, | |
618 | int nLine, | |
acc476c5 | 619 | const char *szFunc, |
e2478fde VZ |
620 | const wxChar *szCond, |
621 | const wxChar *szMsg) | |
622 | { | |
623 | // FIXME MT-unsafe | |
4629016d | 624 | static bool s_bInAssert = false; |
e2478fde VZ |
625 | |
626 | if ( s_bInAssert ) | |
627 | { | |
628 | // He-e-e-e-elp!! we're trapped in endless loop | |
629 | wxTrap(); | |
630 | ||
4629016d | 631 | s_bInAssert = false; |
e2478fde VZ |
632 | |
633 | return; | |
634 | } | |
635 | ||
4629016d | 636 | s_bInAssert = true; |
e2478fde | 637 | |
acc476c5 VZ |
638 | // __FUNCTION__ is always in ASCII, convert it to wide char if needed |
639 | const wxString strFunc = wxString::FromAscii(szFunc); | |
640 | ||
e2478fde VZ |
641 | if ( !wxTheApp ) |
642 | { | |
643 | // by default, show the assert dialog box -- we can't customize this | |
644 | // behaviour | |
acc476c5 | 645 | ShowAssertDialog(szFile, nLine, strFunc, szCond, szMsg); |
e2478fde VZ |
646 | } |
647 | else | |
648 | { | |
649 | // let the app process it as it wants | |
acc476c5 | 650 | wxTheApp->OnAssertFailure(szFile, nLine, strFunc, szCond, szMsg); |
e2478fde VZ |
651 | } |
652 | ||
4629016d | 653 | s_bInAssert = false; |
e2478fde VZ |
654 | } |
655 | ||
656 | #endif // __WXDEBUG__ | |
657 | ||
658 | // ============================================================================ | |
659 | // private functions implementation | |
660 | // ============================================================================ | |
661 | ||
662 | #ifdef __WXDEBUG__ | |
663 | ||
664 | static void LINKAGEMODE SetTraceMasks() | |
665 | { | |
666 | #if wxUSE_LOG | |
667 | wxString mask; | |
668 | if ( wxGetEnv(wxT("WXTRACE"), &mask) ) | |
669 | { | |
670 | wxStringTokenizer tkn(mask, wxT(",;:")); | |
671 | while ( tkn.HasMoreTokens() ) | |
672 | wxLog::AddTraceMask(tkn.GetNextToken()); | |
673 | } | |
674 | #endif // wxUSE_LOG | |
675 | } | |
676 | ||
677 | bool DoShowAssertDialog(const wxString& msg) | |
678 | { | |
679 | // under MSW we can show the dialog even in the console mode | |
680 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
681 | wxString msgDlg(msg); | |
682 | ||
683 | // this message is intentionally not translated -- it is for | |
684 | // developpers only | |
685 | msgDlg += wxT("\nDo you want to stop the program?\n") | |
686 | wxT("You can also choose [Cancel] to suppress ") | |
687 | wxT("further warnings."); | |
688 | ||
77ffb593 | 689 | switch ( ::MessageBox(NULL, msgDlg, _T("wxWidgets Debug Alert"), |
e2478fde VZ |
690 | MB_YESNOCANCEL | MB_ICONSTOP ) ) |
691 | { | |
692 | case IDYES: | |
693 | wxTrap(); | |
694 | break; | |
695 | ||
696 | case IDCANCEL: | |
697 | // stop the asserts | |
698 | return true; | |
699 | ||
700 | //case IDNO: nothing to do | |
701 | } | |
702 | #else // !__WXMSW__ | |
703 | wxFprintf(stderr, wxT("%s\n"), msg.c_str()); | |
704 | fflush(stderr); | |
705 | ||
706 | // TODO: ask the user to enter "Y" or "N" on the console? | |
707 | wxTrap(); | |
708 | #endif // __WXMSW__/!__WXMSW__ | |
709 | ||
710 | // continue with the asserts | |
711 | return false; | |
712 | } | |
713 | ||
388186f7 | 714 | #if wxUSE_STACKWALKER |
4a92d4bc | 715 | static wxString GetAssertStackTrace() |
e2478fde | 716 | { |
4a92d4bc | 717 | wxString stackTrace; |
e2478fde | 718 | |
6c8f8d92 VZ |
719 | class StackDump : public wxStackWalker |
720 | { | |
721 | public: | |
722 | StackDump() { } | |
723 | ||
724 | const wxString& GetStackTrace() const { return m_stackTrace; } | |
725 | ||
726 | protected: | |
727 | virtual void OnStackFrame(const wxStackFrame& frame) | |
728 | { | |
17a1ebd1 VZ |
729 | m_stackTrace << wxString::Format |
730 | ( | |
731 | _T("[%02d] "), | |
732 | wx_truncate_cast(int, frame.GetLevel()) | |
733 | ); | |
6c8f8d92 VZ |
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 | { | |
677856c9 | 742 | m_stackTrace << wxString::Format(_T("%p"), frame.GetAddress()); |
6c8f8d92 VZ |
743 | } |
744 | ||
745 | if ( frame.HasSourceLocation() ) | |
746 | { | |
747 | m_stackTrace << _T('\t') | |
748 | << frame.GetFileName() | |
749 | << _T(':') | |
750 | << frame.GetLine(); | |
751 | } | |
752 | ||
753 | m_stackTrace << _T('\n'); | |
754 | } | |
755 | ||
756 | private: | |
757 | wxString m_stackTrace; | |
758 | }; | |
759 | ||
760 | StackDump dump; | |
b0d8bfa6 | 761 | dump.Walk(2); // don't show OnAssert() call itself |
4a92d4bc | 762 | stackTrace = dump.GetStackTrace(); |
2c9b3131 | 763 | |
a625f454 VZ |
764 | // don't show more than maxLines or we could get a dialog too tall to be |
765 | // shown on screen: 20 should be ok everywhere as even with 15 pixel high | |
766 | // characters it is still only 300 pixels... | |
4a92d4bc VZ |
767 | static const int maxLines = 20; |
768 | const int count = stackTrace.Freq(wxT('\n')); | |
769 | for ( int i = 0; i < count - maxLines; i++ ) | |
770 | stackTrace = stackTrace.BeforeLast(wxT('\n')); | |
771 | ||
772 | return stackTrace; | |
773 | } | |
388186f7 | 774 | #endif // wxUSE_STACKWALKER |
4a92d4bc VZ |
775 | |
776 | // show the assert modal dialog | |
777 | static | |
778 | void ShowAssertDialog(const wxChar *szFile, | |
779 | int nLine, | |
dfa0b52f | 780 | const wxChar *szFunc, |
4a92d4bc VZ |
781 | const wxChar *szCond, |
782 | const wxChar *szMsg, | |
783 | wxAppTraits *traits) | |
784 | { | |
785 | // this variable can be set to true to suppress "assert failure" messages | |
786 | static bool s_bNoAsserts = false; | |
787 | ||
788 | wxString msg; | |
789 | msg.reserve(2048); | |
790 | ||
791 | // make life easier for people using VC++ IDE by using this format: like | |
792 | // this, clicking on the message will take us immediately to the place of | |
793 | // the failed assert | |
794 | msg.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile, nLine, szCond); | |
795 | ||
dfa0b52f VZ |
796 | // add the function name, if any |
797 | if ( szFunc && *szFunc ) | |
798 | msg << _T(" in ") << szFunc << _T("()"); | |
799 | ||
800 | // and the message itself | |
4a92d4bc | 801 | if ( szMsg ) |
2c9b3131 | 802 | { |
4a92d4bc | 803 | msg << _T(": ") << szMsg; |
2c9b3131 | 804 | } |
4a92d4bc VZ |
805 | else // no message given |
806 | { | |
807 | msg << _T('.'); | |
808 | } | |
809 | ||
810 | #if wxUSE_STACKWALKER | |
811 | const wxString stackTrace = GetAssertStackTrace(); | |
6c8f8d92 VZ |
812 | if ( !stackTrace.empty() ) |
813 | { | |
4a92d4bc | 814 | msg << _T("\n\nCall stack:\n") << stackTrace; |
6c8f8d92 VZ |
815 | } |
816 | #endif // wxUSE_STACKWALKER | |
817 | ||
e2478fde VZ |
818 | #if wxUSE_THREADS |
819 | // if we are not in the main thread, output the assert directly and trap | |
820 | // since dialogs cannot be displayed | |
821 | if ( !wxThread::IsMain() ) | |
822 | { | |
823 | msg += wxT(" [in child thread]"); | |
824 | ||
825 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
826 | msg << wxT("\r\n"); | |
827 | OutputDebugString(msg ); | |
828 | #else | |
829 | // send to stderr | |
830 | wxFprintf(stderr, wxT("%s\n"), msg.c_str()); | |
831 | fflush(stderr); | |
832 | #endif | |
833 | // He-e-e-e-elp!! we're asserting in a child thread | |
834 | wxTrap(); | |
835 | } | |
6c8f8d92 | 836 | else |
e2478fde VZ |
837 | #endif // wxUSE_THREADS |
838 | ||
839 | if ( !s_bNoAsserts ) | |
840 | { | |
841 | // send it to the normal log destination | |
56fae7b8 | 842 | wxLogDebug(_T("%s"), msg.c_str()); |
e2478fde VZ |
843 | |
844 | if ( traits ) | |
845 | { | |
846 | // delegate showing assert dialog (if possible) to that class | |
847 | s_bNoAsserts = traits->ShowAssertDialog(msg); | |
848 | } | |
849 | else // no traits object | |
850 | { | |
851 | // fall back to the function of last resort | |
852 | s_bNoAsserts = DoShowAssertDialog(msg); | |
853 | } | |
854 | } | |
855 | } | |
856 | ||
857 | #endif // __WXDEBUG__ |