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