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