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