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