]>
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(); |
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 ); | |
8ecff181 | 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 | ||
dfa0b52f VZ |
437 | void wxAppConsole::OnAssertFailure(const wxChar *file, |
438 | int line, | |
439 | const wxChar *func, | |
440 | const wxChar *cond, | |
441 | const wxChar *msg) | |
442 | { | |
443 | ShowAssertDialog(file, line, func, cond, msg, GetTraits()); | |
444 | } | |
445 | ||
e2478fde VZ |
446 | void wxAppConsole::OnAssert(const wxChar *file, |
447 | int line, | |
448 | const wxChar *cond, | |
449 | const wxChar *msg) | |
450 | { | |
acc476c5 | 451 | OnAssertFailure(file, line, NULL, cond, msg); |
e2478fde VZ |
452 | } |
453 | ||
454 | #endif // __WXDEBUG__ | |
455 | ||
eecb33b0 WS |
456 | #if WXWIN_COMPATIBILITY_2_4 |
457 | ||
458 | bool wxAppConsole::CheckBuildOptions(const wxBuildOptions& buildOptions) | |
459 | { | |
460 | return CheckBuildOptions(buildOptions.m_signature, "your program"); | |
461 | } | |
462 | ||
463 | #endif | |
464 | ||
e2478fde VZ |
465 | // ============================================================================ |
466 | // other classes implementations | |
467 | // ============================================================================ | |
468 | ||
469 | // ---------------------------------------------------------------------------- | |
470 | // wxConsoleAppTraitsBase | |
471 | // ---------------------------------------------------------------------------- | |
472 | ||
473 | #if wxUSE_LOG | |
474 | ||
475 | wxLog *wxConsoleAppTraitsBase::CreateLogTarget() | |
476 | { | |
477 | return new wxLogStderr; | |
478 | } | |
479 | ||
480 | #endif // wxUSE_LOG | |
481 | ||
482 | wxMessageOutput *wxConsoleAppTraitsBase::CreateMessageOutput() | |
483 | { | |
484 | return new wxMessageOutputStderr; | |
485 | } | |
486 | ||
487 | #if wxUSE_FONTMAP | |
488 | ||
489 | wxFontMapper *wxConsoleAppTraitsBase::CreateFontMapper() | |
490 | { | |
491 | return (wxFontMapper *)new wxFontMapperBase; | |
492 | } | |
493 | ||
494 | #endif // wxUSE_FONTMAP | |
495 | ||
f0244295 VZ |
496 | wxRendererNative *wxConsoleAppTraitsBase::CreateRenderer() |
497 | { | |
498 | // console applications don't use renderers | |
499 | return NULL; | |
500 | } | |
501 | ||
8df6de97 | 502 | #ifdef __WXDEBUG__ |
e2478fde VZ |
503 | bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString& msg) |
504 | { | |
505 | return wxAppTraitsBase::ShowAssertDialog(msg); | |
506 | } | |
8df6de97 | 507 | #endif |
e2478fde VZ |
508 | |
509 | bool wxConsoleAppTraitsBase::HasStderr() | |
510 | { | |
511 | // console applications always have stderr, even under Mac/Windows | |
512 | return true; | |
513 | } | |
514 | ||
515 | void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject *object) | |
516 | { | |
517 | delete object; | |
518 | } | |
519 | ||
520 | void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject * WXUNUSED(object)) | |
521 | { | |
522 | // nothing to do | |
523 | } | |
2dbc444a | 524 | |
38bb138f VS |
525 | #if wxUSE_SOCKETS |
526 | GSocketGUIFunctionsTable* wxConsoleAppTraitsBase::GetSocketGUIFunctionsTable() | |
527 | { | |
528 | return NULL; | |
529 | } | |
530 | #endif | |
e2478fde VZ |
531 | |
532 | // ---------------------------------------------------------------------------- | |
533 | // wxAppTraits | |
534 | // ---------------------------------------------------------------------------- | |
535 | ||
536 | #ifdef __WXDEBUG__ | |
537 | ||
db9febdf | 538 | bool wxAppTraitsBase::ShowAssertDialog(const wxString& msgOriginal) |
e2478fde | 539 | { |
db9febdf RR |
540 | wxString msg = msgOriginal; |
541 | ||
542 | #if wxUSE_STACKWALKER | |
543 | #if !defined(__WXMSW__) | |
544 | // on Unix stack frame generation may take some time, depending on the | |
545 | // size of the executable mainly... warn the user that we are working | |
546 | wxFprintf(stderr, wxT("[Debug] Generating a stack trace... please wait")); | |
547 | fflush(stderr); | |
548 | #endif | |
549 | ||
550 | const wxString stackTrace = GetAssertStackTrace(); | |
551 | if ( !stackTrace.empty() ) | |
552 | msg << _T("\n\nCall stack:\n") << stackTrace; | |
553 | #endif // wxUSE_STACKWALKER | |
554 | ||
e2478fde VZ |
555 | return DoShowAssertDialog(msg); |
556 | } | |
557 | ||
db9febdf RR |
558 | #if wxUSE_STACKWALKER |
559 | wxString wxAppTraitsBase::GetAssertStackTrace() | |
560 | { | |
561 | wxString stackTrace; | |
562 | ||
563 | class StackDump : public wxStackWalker | |
564 | { | |
565 | public: | |
566 | StackDump() { } | |
567 | ||
568 | const wxString& GetStackTrace() const { return m_stackTrace; } | |
569 | ||
570 | protected: | |
571 | virtual void OnStackFrame(const wxStackFrame& frame) | |
572 | { | |
573 | m_stackTrace << wxString::Format | |
574 | ( | |
575 | _T("[%02d] "), | |
576 | wx_truncate_cast(int, frame.GetLevel()) | |
577 | ); | |
578 | ||
579 | wxString name = frame.GetName(); | |
580 | if ( !name.empty() ) | |
581 | { | |
582 | m_stackTrace << wxString::Format(_T("%-40s"), name.c_str()); | |
583 | } | |
584 | else | |
585 | { | |
586 | m_stackTrace << wxString::Format(_T("%p"), frame.GetAddress()); | |
587 | } | |
588 | ||
589 | if ( frame.HasSourceLocation() ) | |
590 | { | |
591 | m_stackTrace << _T('\t') | |
592 | << frame.GetFileName() | |
593 | << _T(':') | |
594 | << frame.GetLine(); | |
595 | } | |
596 | ||
597 | m_stackTrace << _T('\n'); | |
598 | } | |
599 | ||
600 | private: | |
601 | wxString m_stackTrace; | |
602 | }; | |
603 | ||
604 | // don't show more than maxLines or we could get a dialog too tall to be | |
605 | // shown on screen: 20 should be ok everywhere as even with 15 pixel high | |
606 | // characters it is still only 300 pixels... | |
607 | static const int maxLines = 20; | |
608 | ||
609 | StackDump dump; | |
610 | dump.Walk(2, maxLines); // don't show OnAssert() call itself | |
611 | stackTrace = dump.GetStackTrace(); | |
612 | ||
613 | const int count = stackTrace.Freq(wxT('\n')); | |
614 | for ( int i = 0; i < count - maxLines; i++ ) | |
615 | stackTrace = stackTrace.BeforeLast(wxT('\n')); | |
616 | ||
617 | return stackTrace; | |
618 | } | |
619 | #endif // wxUSE_STACKWALKER | |
620 | ||
621 | ||
e2478fde VZ |
622 | #endif // __WXDEBUG__ |
623 | ||
e2478fde VZ |
624 | // ============================================================================ |
625 | // global functions implementation | |
626 | // ============================================================================ | |
627 | ||
628 | void wxExit() | |
629 | { | |
630 | if ( wxTheApp ) | |
631 | { | |
632 | wxTheApp->Exit(); | |
633 | } | |
634 | else | |
635 | { | |
636 | // what else can we do? | |
637 | exit(-1); | |
638 | } | |
639 | } | |
640 | ||
641 | void wxWakeUpIdle() | |
642 | { | |
643 | if ( wxTheApp ) | |
644 | { | |
645 | wxTheApp->WakeUpIdle(); | |
646 | } | |
647 | //else: do nothing, what can we do? | |
648 | } | |
649 | ||
650 | #ifdef __WXDEBUG__ | |
651 | ||
652 | // wxASSERT() helper | |
653 | bool wxAssertIsEqual(int x, int y) | |
654 | { | |
655 | return x == y; | |
656 | } | |
657 | ||
658 | // break into the debugger | |
659 | void wxTrap() | |
660 | { | |
661 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
662 | DebugBreak(); | |
663 | #elif defined(__WXMAC__) && !defined(__DARWIN__) | |
664 | #if __powerc | |
665 | Debugger(); | |
666 | #else | |
667 | SysBreak(); | |
668 | #endif | |
f0756afe DE |
669 | #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS |
670 | Debugger(); | |
e2478fde VZ |
671 | #elif defined(__UNIX__) |
672 | raise(SIGTRAP); | |
673 | #else | |
674 | // TODO | |
675 | #endif // Win/Unix | |
676 | } | |
677 | ||
e2478fde VZ |
678 | // this function is called when an assert fails |
679 | void wxOnAssert(const wxChar *szFile, | |
680 | int nLine, | |
acc476c5 | 681 | const char *szFunc, |
e2478fde VZ |
682 | const wxChar *szCond, |
683 | const wxChar *szMsg) | |
684 | { | |
685 | // FIXME MT-unsafe | |
4629016d | 686 | static bool s_bInAssert = false; |
e2478fde VZ |
687 | |
688 | if ( s_bInAssert ) | |
689 | { | |
690 | // He-e-e-e-elp!! we're trapped in endless loop | |
691 | wxTrap(); | |
692 | ||
4629016d | 693 | s_bInAssert = false; |
e2478fde VZ |
694 | |
695 | return; | |
696 | } | |
697 | ||
4629016d | 698 | s_bInAssert = true; |
e2478fde | 699 | |
acc476c5 VZ |
700 | // __FUNCTION__ is always in ASCII, convert it to wide char if needed |
701 | const wxString strFunc = wxString::FromAscii(szFunc); | |
702 | ||
e2478fde VZ |
703 | if ( !wxTheApp ) |
704 | { | |
705 | // by default, show the assert dialog box -- we can't customize this | |
706 | // behaviour | |
acc476c5 | 707 | ShowAssertDialog(szFile, nLine, strFunc, szCond, szMsg); |
e2478fde VZ |
708 | } |
709 | else | |
710 | { | |
711 | // let the app process it as it wants | |
acc476c5 | 712 | wxTheApp->OnAssertFailure(szFile, nLine, strFunc, szCond, szMsg); |
e2478fde VZ |
713 | } |
714 | ||
4629016d | 715 | s_bInAssert = false; |
e2478fde VZ |
716 | } |
717 | ||
718 | #endif // __WXDEBUG__ | |
719 | ||
720 | // ============================================================================ | |
721 | // private functions implementation | |
722 | // ============================================================================ | |
723 | ||
724 | #ifdef __WXDEBUG__ | |
725 | ||
726 | static void LINKAGEMODE SetTraceMasks() | |
727 | { | |
728 | #if wxUSE_LOG | |
729 | wxString mask; | |
730 | if ( wxGetEnv(wxT("WXTRACE"), &mask) ) | |
731 | { | |
732 | wxStringTokenizer tkn(mask, wxT(",;:")); | |
733 | while ( tkn.HasMoreTokens() ) | |
734 | wxLog::AddTraceMask(tkn.GetNextToken()); | |
735 | } | |
736 | #endif // wxUSE_LOG | |
737 | } | |
738 | ||
739 | bool DoShowAssertDialog(const wxString& msg) | |
740 | { | |
741 | // under MSW we can show the dialog even in the console mode | |
742 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
743 | wxString msgDlg(msg); | |
744 | ||
745 | // this message is intentionally not translated -- it is for | |
746 | // developpers only | |
747 | msgDlg += wxT("\nDo you want to stop the program?\n") | |
748 | wxT("You can also choose [Cancel] to suppress ") | |
749 | wxT("further warnings."); | |
750 | ||
77ffb593 | 751 | switch ( ::MessageBox(NULL, msgDlg, _T("wxWidgets Debug Alert"), |
e2478fde VZ |
752 | MB_YESNOCANCEL | MB_ICONSTOP ) ) |
753 | { | |
754 | case IDYES: | |
755 | wxTrap(); | |
756 | break; | |
757 | ||
758 | case IDCANCEL: | |
759 | // stop the asserts | |
760 | return true; | |
761 | ||
762 | //case IDNO: nothing to do | |
763 | } | |
764 | #else // !__WXMSW__ | |
765 | wxFprintf(stderr, wxT("%s\n"), msg.c_str()); | |
766 | fflush(stderr); | |
767 | ||
768 | // TODO: ask the user to enter "Y" or "N" on the console? | |
769 | wxTrap(); | |
770 | #endif // __WXMSW__/!__WXMSW__ | |
771 | ||
772 | // continue with the asserts | |
773 | return false; | |
774 | } | |
775 | ||
4a92d4bc VZ |
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 | ||
e2478fde VZ |
810 | #if wxUSE_THREADS |
811 | // if we are not in the main thread, output the assert directly and trap | |
812 | // since dialogs cannot be displayed | |
813 | if ( !wxThread::IsMain() ) | |
814 | { | |
815 | msg += wxT(" [in child thread]"); | |
816 | ||
817 | #if defined(__WXMSW__) && !defined(__WXMICROWIN__) | |
818 | msg << wxT("\r\n"); | |
819 | OutputDebugString(msg ); | |
820 | #else | |
821 | // send to stderr | |
822 | wxFprintf(stderr, wxT("%s\n"), msg.c_str()); | |
823 | fflush(stderr); | |
824 | #endif | |
825 | // He-e-e-e-elp!! we're asserting in a child thread | |
826 | wxTrap(); | |
827 | } | |
6c8f8d92 | 828 | else |
e2478fde VZ |
829 | #endif // wxUSE_THREADS |
830 | ||
831 | if ( !s_bNoAsserts ) | |
832 | { | |
833 | // send it to the normal log destination | |
56fae7b8 | 834 | wxLogDebug(_T("%s"), msg.c_str()); |
e2478fde VZ |
835 | |
836 | if ( traits ) | |
837 | { | |
838 | // delegate showing assert dialog (if possible) to that class | |
839 | s_bNoAsserts = traits->ShowAssertDialog(msg); | |
840 | } | |
841 | else // no traits object | |
842 | { | |
843 | // fall back to the function of last resort | |
844 | s_bNoAsserts = DoShowAssertDialog(msg); | |
845 | } | |
846 | } | |
847 | } | |
848 | ||
849 | #endif // __WXDEBUG__ |