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