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