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