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