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