]> git.saurik.com Git - wxWidgets.git/blame - src/common/appbase.cpp
fixed incorrect use of wxVector<> in wxXRC
[wxWidgets.git] / src / common / appbase.cpp
CommitLineData
e2478fde 1///////////////////////////////////////////////////////////////////////////////
8ecff181 2// Name: src/common/appbase.cpp
e0954e72 3// Purpose: implements wxAppConsoleBase class
e2478fde
VZ
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"
0cb7e05c 36 #include "wx/wxcrtvararg.h"
e2478fde
VZ
37#endif //WX_PRECOMP
38
39#include "wx/apptrait.h"
40#include "wx/cmdline.h"
41#include "wx/confbase.h"
b46b1d59 42#include "wx/evtloop.h"
ce39c39e 43#include "wx/filename.h"
e2478fde 44#include "wx/msgout.h"
b46b1d59 45#include "wx/ptr_scpd.h"
e2478fde 46#include "wx/tokenzr.h"
204abcd4 47#include "wx/thread.h"
e2478fde 48
1663c655
VZ
49#if wxUSE_EXCEPTIONS && wxUSE_STL
50 #include <exception>
51 #include <typeinfo>
52#endif
53
e2478fde
VZ
54#if !defined(__WXMSW__) || defined(__WXMICROWIN__)
55 #include <signal.h> // for SIGTRAP used by wxTrap()
56#endif //Win/Unix
57
0e5ab030
VZ
58#include <locale.h>
59
1c193821
JS
60#if wxUSE_FONTMAP
61 #include "wx/fontmap.h"
62#endif // wxUSE_FONTMAP
63
f0756afe
DE
64#if defined(__DARWIN__) && defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
65 // For MacTypes.h for Debugger function
66 #include <CoreFoundation/CFBase.h>
67#endif
68
e2478fde 69#if defined(__WXMAC__)
22f69dd4 70 #ifdef __DARWIN__
40ba2f3b 71 #include <CoreServices/CoreServices.h>
22f69dd4
VZ
72 #else
73 #include "wx/mac/private.h" // includes mac headers
74 #endif
75#endif // __WXMAC__
e2478fde 76
6c8f8d92 77#ifdef __WXDEBUG__
7b1c3469 78 #if wxUSE_STACKWALKER
6c8f8d92 79 #include "wx/stackwalk.h"
4a92d4bc
VZ
80 #ifdef __WXMSW__
81 #include "wx/msw/debughlp.h"
82 #endif
6c8f8d92
VZ
83 #endif // wxUSE_STACKWALKER
84#endif // __WXDEBUG__
85
121aea46
MW
86// wxABI_VERSION can be defined when compiling applications but it should be
87// left undefined when compiling the library itself, it is then set to its
88// default value in version.h
89#if wxABI_VERSION != wxMAJOR_VERSION * 10000 + wxMINOR_VERSION * 100 + 99
90#error "wxABI_VERSION should not be defined when compiling the library"
91#endif
92
e2478fde
VZ
93// ----------------------------------------------------------------------------
94// private functions prototypes
95// ----------------------------------------------------------------------------
96
97#ifdef __WXDEBUG__
98 // really just show the assert dialog
99 static bool DoShowAssertDialog(const wxString& msg);
100
101 // prepare for showing the assert dialog, use the given traits or
102 // DoShowAssertDialog() as last fallback to really show it
103 static
0accd1cf 104 void ShowAssertDialog(const wxString& szFile,
e2478fde 105 int nLine,
0accd1cf
VS
106 const wxString& szFunc,
107 const wxString& szCond,
108 const wxString& szMsg,
e2478fde
VZ
109 wxAppTraits *traits = NULL);
110
111 // turn on the trace masks specified in the env variable WXTRACE
112 static void LINKAGEMODE SetTraceMasks();
113#endif // __WXDEBUG__
114
115// ----------------------------------------------------------------------------
116// global vars
117// ----------------------------------------------------------------------------
118
e0954e72 119wxAppConsole *wxAppConsoleBase::ms_appInstance = NULL;
e2478fde 120
e0954e72 121wxAppInitializerFunction wxAppConsoleBase::ms_appInitFn = NULL;
e2478fde 122
b46b1d59
VZ
123// ----------------------------------------------------------------------------
124// wxEventLoopPtr
125// ----------------------------------------------------------------------------
126
127// this defines wxEventLoopPtr
2ddff00c 128wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopBase)
b46b1d59 129
e2478fde 130// ============================================================================
e0954e72 131// wxAppConsoleBase implementation
e2478fde
VZ
132// ============================================================================
133
134// ----------------------------------------------------------------------------
135// ctor/dtor
136// ----------------------------------------------------------------------------
137
e0954e72 138wxAppConsoleBase::wxAppConsoleBase()
e2478fde
VZ
139{
140 m_traits = NULL;
b46b1d59 141 m_mainLoop = NULL;
e2478fde 142
e0954e72 143 ms_appInstance = wx_static_cast(wxAppConsole *, this);
e2478fde
VZ
144
145#ifdef __WXDEBUG__
146 SetTraceMasks();
bc334f39
RD
147#if wxUSE_UNICODE
148 // In unicode mode the SetTraceMasks call can cause an apptraits to be
149 // created, but since we are still in the constructor the wrong kind will
150 // be created for GUI apps. Destroy it so it can be created again later.
151 delete m_traits;
152 m_traits = NULL;
153#endif
e2478fde
VZ
154#endif
155}
156
e0954e72 157wxAppConsoleBase::~wxAppConsoleBase()
e2478fde
VZ
158{
159 delete m_traits;
160}
161
94826170
VZ
162// ----------------------------------------------------------------------------
163// initilization/cleanup
164// ----------------------------------------------------------------------------
165
e0954e72 166bool wxAppConsoleBase::Initialize(int& argcOrig, wxChar **argvOrig)
94826170 167{
d774f916
VZ
168#if wxUSE_INTL
169 GetTraits()->SetLocale();
170#endif // wxUSE_INTL
171
94826170 172 // remember the command line arguments
8d79ccef
VZ
173 argc = argcOrig;
174 argv = argvOrig;
94826170 175
b46b1d59
VZ
176#if wxUSE_THREADS
177 wxPendingEventsLocker = new wxCriticalSection;
178#endif
179
4055ed82 180#ifndef __WXPALMOS__
d154ab3d 181 if ( m_appName.empty() && argv )
94826170
VZ
182 {
183 // the application name is, by default, the name of its executable file
94826170 184 wxFileName::SplitPath(argv[0], NULL, &m_appName, NULL);
94826170 185 }
d774f916 186#endif // !__WXPALMOS__
94826170
VZ
187
188 return true;
189}
190
e0954e72 191wxEventLoopBase *wxAppConsoleBase::CreateMainLoop()
b46b1d59
VZ
192{
193 return GetTraits()->CreateEventLoop();
194}
195
e0954e72 196void wxAppConsoleBase::CleanUp()
94826170 197{
aea33a3e
VZ
198 if ( m_mainLoop )
199 {
200 delete m_mainLoop;
201 m_mainLoop = NULL;
202 }
82a4f054 203
b46b1d59
VZ
204 delete wxPendingEvents;
205 wxPendingEvents = NULL;
206
207#if wxUSE_THREADS
208 delete wxPendingEventsLocker;
209 wxPendingEventsLocker = NULL;
210#endif // wxUSE_THREADS
94826170
VZ
211}
212
e2478fde
VZ
213// ----------------------------------------------------------------------------
214// OnXXX() callbacks
215// ----------------------------------------------------------------------------
216
e0954e72 217bool wxAppConsoleBase::OnInit()
e2478fde
VZ
218{
219#if wxUSE_CMDLINE_PARSER
220 wxCmdLineParser parser(argc, argv);
221
222 OnInitCmdLine(parser);
223
224 bool cont;
4629016d 225 switch ( parser.Parse(false /* don't show usage */) )
e2478fde
VZ
226 {
227 case -1:
228 cont = OnCmdLineHelp(parser);
229 break;
230
231 case 0:
232 cont = OnCmdLineParsed(parser);
233 break;
234
235 default:
236 cont = OnCmdLineError(parser);
237 break;
238 }
239
240 if ( !cont )
4629016d 241 return false;
e2478fde
VZ
242#endif // wxUSE_CMDLINE_PARSER
243
4629016d 244 return true;
e2478fde
VZ
245}
246
e0954e72 247int wxAppConsoleBase::OnRun()
b46b1d59
VZ
248{
249 return MainLoop();
8ac09e52 250}
b46b1d59 251
e0954e72 252int wxAppConsoleBase::OnExit()
e2478fde
VZ
253{
254#if wxUSE_CONFIG
255 // delete the config object if any (don't use Get() here, but Set()
256 // because Get() could create a new config object)
257 delete wxConfigBase::Set((wxConfigBase *) NULL);
258#endif // wxUSE_CONFIG
259
e2478fde
VZ
260 return 0;
261}
262
e0954e72 263void wxAppConsoleBase::Exit()
e2478fde 264{
b46b1d59
VZ
265 if (m_mainLoop != NULL)
266 ExitMainLoop();
267 else
268 exit(-1);
e2478fde
VZ
269}
270
271// ----------------------------------------------------------------------------
272// traits stuff
273// ----------------------------------------------------------------------------
274
e0954e72 275wxAppTraits *wxAppConsoleBase::CreateTraits()
e2478fde 276{
7843d11b 277 return new wxConsoleAppTraits;
e2478fde
VZ
278}
279
e0954e72 280wxAppTraits *wxAppConsoleBase::GetTraits()
e2478fde
VZ
281{
282 // FIXME-MT: protect this with a CS?
283 if ( !m_traits )
284 {
285 m_traits = CreateTraits();
286
287 wxASSERT_MSG( m_traits, _T("wxApp::CreateTraits() failed?") );
288 }
289
290 return m_traits;
291}
292
e2478fde
VZ
293// ----------------------------------------------------------------------------
294// event processing
295// ----------------------------------------------------------------------------
296
e0954e72 297int wxAppConsoleBase::MainLoop()
e2478fde 298{
2ddff00c 299 wxEventLoopBaseTiedPtr mainLoop(&m_mainLoop, CreateMainLoop());
b46b1d59
VZ
300
301 return m_mainLoop ? m_mainLoop->Run() : -1;
302}
303
e0954e72 304void wxAppConsoleBase::ExitMainLoop()
b46b1d59
VZ
305{
306 // we should exit from the main event loop, not just any currently active
307 // (e.g. modal dialog) event loop
308 if ( m_mainLoop && m_mainLoop->IsRunning() )
309 {
310 m_mainLoop->Exit(0);
311 }
312}
313
e0954e72 314bool wxAppConsoleBase::Pending()
b46b1d59
VZ
315{
316 // use the currently active message loop here, not m_mainLoop, because if
317 // we're showing a modal dialog (with its own event loop) currently the
318 // main event loop is not running anyhow
2ddff00c 319 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
b46b1d59
VZ
320
321 return loop && loop->Pending();
322}
323
e0954e72 324bool wxAppConsoleBase::Dispatch()
b46b1d59
VZ
325{
326 // see comment in Pending()
2ddff00c 327 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
b46b1d59
VZ
328
329 return loop && loop->Dispatch();
330}
8ecff181 331
e0954e72 332bool wxAppConsoleBase::HasPendingEvents() const
b46b1d59 333{
e2478fde
VZ
334 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
335
a1312bc8
PC
336 bool has = wxPendingEvents && !wxPendingEvents->IsEmpty();
337
b46b1d59 338 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
a1312bc8
PC
339
340 return has;
8ac09e52 341}
b46b1d59 342
e0954e72
VZ
343/* static */
344bool wxAppConsoleBase::IsMainLoopRunning()
345{
346 const wxAppConsole * const app = GetInstance();
347
348 return app && app->m_mainLoop != NULL;
349}
350
351void wxAppConsoleBase::ProcessPendingEvents()
b46b1d59
VZ
352{
353#if wxUSE_THREADS
354 if ( !wxPendingEventsLocker )
355 return;
356#endif
357
c0bfefb6
VZ
358 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
359
a1312bc8 360 if (wxPendingEvents)
e2478fde 361 {
a1312bc8
PC
362 // iterate until the list becomes empty
363 wxList::compatibility_iterator node = wxPendingEvents->GetFirst();
364 while (node)
365 {
366 wxEvtHandler *handler = (wxEvtHandler *)node->GetData();
367 wxPendingEvents->Erase(node);
e2478fde 368
a1312bc8
PC
369 // In ProcessPendingEvents(), new handlers might be add
370 // and we can safely leave the critical section here.
371 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
8ecff181 372
a1312bc8 373 handler->ProcessPendingEvents();
de4de64f 374
a1312bc8 375 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
e2478fde 376
a1312bc8
PC
377 node = wxPendingEvents->GetFirst();
378 }
e2478fde
VZ
379 }
380
381 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
382}
383
e0954e72 384void wxAppConsoleBase::WakeUpIdle()
b46b1d59
VZ
385{
386 if ( m_mainLoop )
387 m_mainLoop->WakeUp();
388}
389
e0954e72 390bool wxAppConsoleBase::ProcessIdle()
b46b1d59
VZ
391{
392 wxIdleEvent event;
393
394 event.SetEventObject(this);
395 ProcessEvent(event);
396 return event.MoreRequested();
397}
398
e0954e72 399int wxAppConsoleBase::FilterEvent(wxEvent& WXUNUSED(event))
e2478fde
VZ
400{
401 // process the events normally by default
402 return -1;
403}
404
78361a0e
VZ
405// ----------------------------------------------------------------------------
406// exception handling
407// ----------------------------------------------------------------------------
408
6f054ac5
VZ
409#if wxUSE_EXCEPTIONS
410
411void
e0954e72
VZ
412wxAppConsoleBase::HandleEvent(wxEvtHandler *handler,
413 wxEventFunction func,
414 wxEvent& event) const
6f054ac5 415{
96d38c7e
VS
416 // by default, simply call the handler
417 (handler->*func)(event);
6f054ac5
VZ
418}
419
1663c655
VZ
420void wxAppConsoleBase::OnUnhandledException()
421{
422#ifdef __WXDEBUG__
423 // we're called from an exception handler so we can re-throw the exception
424 // to recover its type
425 wxString what;
426 try
427 {
428 throw;
429 }
430#if wxUSE_STL
431 catch ( std::exception& e )
432 {
433 what.Printf("std::exception of type \"%s\", what() = \"%s\"",
434 typeid(e).name(), e.what());
435 }
436#endif // wxUSE_STL
437 catch ( ... )
438 {
439 what = "unknown exception";
440 }
441
442 wxMessageOutputBest().Printf(
443 "*** Caught unhandled %s; terminating\n", what
444 );
445#endif // __WXDEBUG__
446}
447
b46b1d59
VZ
448// ----------------------------------------------------------------------------
449// exceptions support
450// ----------------------------------------------------------------------------
451
e0954e72 452bool wxAppConsoleBase::OnExceptionInMainLoop()
b46b1d59
VZ
453{
454 throw;
455
456 // some compilers are too stupid to know that we never return after throw
457#if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
458 return false;
459#endif
460}
461
6f054ac5
VZ
462#endif // wxUSE_EXCEPTIONS
463
e2478fde
VZ
464// ----------------------------------------------------------------------------
465// cmd line parsing
466// ----------------------------------------------------------------------------
467
468#if wxUSE_CMDLINE_PARSER
469
e6d4038a 470#define OPTION_VERBOSE "verbose"
e2478fde 471
e0954e72 472void wxAppConsoleBase::OnInitCmdLine(wxCmdLineParser& parser)
e2478fde
VZ
473{
474 // the standard command line options
475 static const wxCmdLineEntryDesc cmdLineDesc[] =
476 {
477 {
478 wxCMD_LINE_SWITCH,
e6d4038a
VZ
479 "h",
480 "help",
e2478fde
VZ
481 gettext_noop("show this help message"),
482 wxCMD_LINE_VAL_NONE,
483 wxCMD_LINE_OPTION_HELP
484 },
485
486#if wxUSE_LOG
487 {
488 wxCMD_LINE_SWITCH,
0d5ab92f 489 NULL,
e2478fde
VZ
490 OPTION_VERBOSE,
491 gettext_noop("generate verbose log messages"),
492 wxCMD_LINE_VAL_NONE,
493 0x0
494 },
495#endif // wxUSE_LOG
496
e2478fde 497 // terminator
0d5ab92f 498 wxCMD_LINE_DESC_END
e2478fde
VZ
499 };
500
501 parser.SetDesc(cmdLineDesc);
502}
503
e0954e72 504bool wxAppConsoleBase::OnCmdLineParsed(wxCmdLineParser& parser)
e2478fde
VZ
505{
506#if wxUSE_LOG
507 if ( parser.Found(OPTION_VERBOSE) )
508 {
fa0d3447 509 wxLog::SetVerbose(true);
e2478fde 510 }
fa0d3447
WS
511#else
512 wxUnusedVar(parser);
e2478fde
VZ
513#endif // wxUSE_LOG
514
fa0d3447 515 return true;
e2478fde
VZ
516}
517
e0954e72 518bool wxAppConsoleBase::OnCmdLineHelp(wxCmdLineParser& parser)
e2478fde
VZ
519{
520 parser.Usage();
521
4629016d 522 return false;
e2478fde
VZ
523}
524
e0954e72 525bool wxAppConsoleBase::OnCmdLineError(wxCmdLineParser& parser)
e2478fde
VZ
526{
527 parser.Usage();
528
4629016d 529 return false;
e2478fde
VZ
530}
531
532#endif // wxUSE_CMDLINE_PARSER
533
534// ----------------------------------------------------------------------------
535// debugging support
536// ----------------------------------------------------------------------------
537
538/* static */
e0954e72
VZ
539bool wxAppConsoleBase::CheckBuildOptions(const char *optionsSignature,
540 const char *componentName)
e2478fde 541{
2a7c7605
VS
542#if 0 // can't use wxLogTrace, not up and running yet
543 printf("checking build options object '%s' (ptr %p) in '%s'\n",
544 optionsSignature, optionsSignature, componentName);
e2478fde
VZ
545#endif
546
2a7c7605 547 if ( strcmp(optionsSignature, WX_BUILD_OPTIONS_SIGNATURE) != 0 )
e2478fde 548 {
2a7c7605
VS
549 wxString lib = wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE);
550 wxString prog = wxString::FromAscii(optionsSignature);
551 wxString progName = wxString::FromAscii(componentName);
e2478fde 552 wxString msg;
2dbc444a 553
b880adec 554 msg.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
2a7c7605 555 lib.c_str(), progName.c_str(), prog.c_str());
2dbc444a 556
095d49f2 557 wxLogFatalError(msg.c_str());
e2478fde
VZ
558
559 // normally wxLogFatalError doesn't return
4629016d 560 return false;
e2478fde
VZ
561 }
562#undef wxCMP
563
4629016d 564 return true;
e2478fde
VZ
565}
566
567#ifdef __WXDEBUG__
568
e0954e72
VZ
569void wxAppConsoleBase::OnAssertFailure(const wxChar *file,
570 int line,
571 const wxChar *func,
572 const wxChar *cond,
573 const wxChar *msg)
dfa0b52f
VZ
574{
575 ShowAssertDialog(file, line, func, cond, msg, GetTraits());
576}
577
e0954e72
VZ
578void wxAppConsoleBase::OnAssert(const wxChar *file,
579 int line,
580 const wxChar *cond,
581 const wxChar *msg)
e2478fde 582{
acc476c5 583 OnAssertFailure(file, line, NULL, cond, msg);
e2478fde
VZ
584}
585
586#endif // __WXDEBUG__
587
588// ============================================================================
589// other classes implementations
590// ============================================================================
591
592// ----------------------------------------------------------------------------
593// wxConsoleAppTraitsBase
594// ----------------------------------------------------------------------------
595
596#if wxUSE_LOG
597
598wxLog *wxConsoleAppTraitsBase::CreateLogTarget()
599{
600 return new wxLogStderr;
601}
602
603#endif // wxUSE_LOG
604
605wxMessageOutput *wxConsoleAppTraitsBase::CreateMessageOutput()
606{
607 return new wxMessageOutputStderr;
608}
609
610#if wxUSE_FONTMAP
611
612wxFontMapper *wxConsoleAppTraitsBase::CreateFontMapper()
613{
614 return (wxFontMapper *)new wxFontMapperBase;
615}
616
617#endif // wxUSE_FONTMAP
618
f0244295
VZ
619wxRendererNative *wxConsoleAppTraitsBase::CreateRenderer()
620{
621 // console applications don't use renderers
622 return NULL;
623}
624
8df6de97 625#ifdef __WXDEBUG__
e2478fde
VZ
626bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString& msg)
627{
628 return wxAppTraitsBase::ShowAssertDialog(msg);
629}
8df6de97 630#endif
e2478fde
VZ
631
632bool wxConsoleAppTraitsBase::HasStderr()
633{
634 // console applications always have stderr, even under Mac/Windows
635 return true;
636}
637
638void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject *object)
639{
640 delete object;
641}
642
643void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject * WXUNUSED(object))
644{
645 // nothing to do
646}
2dbc444a 647
e2478fde
VZ
648// ----------------------------------------------------------------------------
649// wxAppTraits
650// ----------------------------------------------------------------------------
651
6c4f5ea5 652#if wxUSE_INTL
d774f916
VZ
653void wxAppTraitsBase::SetLocale()
654{
d6f2a891 655 wxSetlocale(LC_ALL, "");
cb352236 656 wxUpdateLocaleIsUtf8();
d774f916 657}
6c4f5ea5 658#endif
d774f916 659
d254213e
PC
660#if wxUSE_THREADS
661void wxMutexGuiEnterImpl();
662void wxMutexGuiLeaveImpl();
663
664void wxAppTraitsBase::MutexGuiEnter()
665{
666 wxMutexGuiEnterImpl();
667}
668
669void wxAppTraitsBase::MutexGuiLeave()
670{
671 wxMutexGuiLeaveImpl();
672}
673
674void WXDLLIMPEXP_BASE wxMutexGuiEnter()
675{
676 wxAppConsoleBase::GetInstance()->GetTraits()->MutexGuiEnter();
677}
678
679void WXDLLIMPEXP_BASE wxMutexGuiLeave()
680{
681 wxAppConsoleBase::GetInstance()->GetTraits()->MutexGuiLeave();
682}
683#endif // wxUSE_THREADS
684
e2478fde
VZ
685#ifdef __WXDEBUG__
686
db9febdf 687bool wxAppTraitsBase::ShowAssertDialog(const wxString& msgOriginal)
e2478fde 688{
db9febdf
RR
689 wxString msg = msgOriginal;
690
691#if wxUSE_STACKWALKER
692#if !defined(__WXMSW__)
693 // on Unix stack frame generation may take some time, depending on the
694 // size of the executable mainly... warn the user that we are working
695 wxFprintf(stderr, wxT("[Debug] Generating a stack trace... please wait"));
696 fflush(stderr);
697#endif
698
699 const wxString stackTrace = GetAssertStackTrace();
700 if ( !stackTrace.empty() )
701 msg << _T("\n\nCall stack:\n") << stackTrace;
702#endif // wxUSE_STACKWALKER
703
e2478fde
VZ
704 return DoShowAssertDialog(msg);
705}
706
db9febdf
RR
707#if wxUSE_STACKWALKER
708wxString wxAppTraitsBase::GetAssertStackTrace()
709{
710 wxString stackTrace;
711
712 class StackDump : public wxStackWalker
713 {
714 public:
715 StackDump() { }
716
717 const wxString& GetStackTrace() const { return m_stackTrace; }
718
719 protected:
720 virtual void OnStackFrame(const wxStackFrame& frame)
721 {
722 m_stackTrace << wxString::Format
723 (
724 _T("[%02d] "),
725 wx_truncate_cast(int, frame.GetLevel())
726 );
727
728 wxString name = frame.GetName();
729 if ( !name.empty() )
730 {
731 m_stackTrace << wxString::Format(_T("%-40s"), name.c_str());
732 }
733 else
734 {
735 m_stackTrace << wxString::Format(_T("%p"), frame.GetAddress());
736 }
737
738 if ( frame.HasSourceLocation() )
739 {
740 m_stackTrace << _T('\t')
741 << frame.GetFileName()
742 << _T(':')
743 << frame.GetLine();
744 }
745
746 m_stackTrace << _T('\n');
747 }
748
749 private:
750 wxString m_stackTrace;
751 };
752
753 // don't show more than maxLines or we could get a dialog too tall to be
754 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
755 // characters it is still only 300 pixels...
756 static const int maxLines = 20;
757
758 StackDump dump;
759 dump.Walk(2, maxLines); // don't show OnAssert() call itself
760 stackTrace = dump.GetStackTrace();
761
762 const int count = stackTrace.Freq(wxT('\n'));
763 for ( int i = 0; i < count - maxLines; i++ )
764 stackTrace = stackTrace.BeforeLast(wxT('\n'));
765
766 return stackTrace;
767}
768#endif // wxUSE_STACKWALKER
769
770
e2478fde
VZ
771#endif // __WXDEBUG__
772
e2478fde
VZ
773// ============================================================================
774// global functions implementation
775// ============================================================================
776
777void wxExit()
778{
779 if ( wxTheApp )
780 {
781 wxTheApp->Exit();
782 }
783 else
784 {
785 // what else can we do?
786 exit(-1);
787 }
788}
789
790void wxWakeUpIdle()
791{
792 if ( wxTheApp )
793 {
794 wxTheApp->WakeUpIdle();
795 }
796 //else: do nothing, what can we do?
797}
798
799#ifdef __WXDEBUG__
800
801// wxASSERT() helper
802bool wxAssertIsEqual(int x, int y)
803{
804 return x == y;
805}
806
807// break into the debugger
808void wxTrap()
809{
810#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
811 DebugBreak();
f0756afe
DE
812#elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
813 Debugger();
e2478fde
VZ
814#elif defined(__UNIX__)
815 raise(SIGTRAP);
816#else
817 // TODO
818#endif // Win/Unix
819}
820
e2478fde 821// this function is called when an assert fails
0accd1cf
VS
822static void wxDoOnAssert(const wxString& szFile,
823 int nLine,
824 const wxString& szFunc,
825 const wxString& szCond,
826 const wxString& szMsg = wxEmptyString)
e2478fde
VZ
827{
828 // FIXME MT-unsafe
4629016d 829 static bool s_bInAssert = false;
e2478fde
VZ
830
831 if ( s_bInAssert )
832 {
833 // He-e-e-e-elp!! we're trapped in endless loop
834 wxTrap();
835
4629016d 836 s_bInAssert = false;
e2478fde
VZ
837
838 return;
839 }
840
4629016d 841 s_bInAssert = true;
e2478fde
VZ
842
843 if ( !wxTheApp )
844 {
845 // by default, show the assert dialog box -- we can't customize this
846 // behaviour
0accd1cf 847 ShowAssertDialog(szFile, nLine, szFunc, szCond, szMsg);
e2478fde
VZ
848 }
849 else
850 {
851 // let the app process it as it wants
0accd1cf
VS
852 // FIXME-UTF8: use wc_str(), not c_str(), when ANSI build is removed
853 wxTheApp->OnAssertFailure(szFile.c_str(), nLine, szFunc.c_str(),
854 szCond.c_str(), szMsg.c_str());
e2478fde
VZ
855 }
856
4629016d 857 s_bInAssert = false;
e2478fde
VZ
858}
859
0accd1cf
VS
860void wxOnAssert(const wxString& szFile,
861 int nLine,
862 const wxString& szFunc,
863 const wxString& szCond,
864 const wxString& szMsg)
865{
866 wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg);
867}
868
869void wxOnAssert(const wxString& szFile,
870 int nLine,
871 const wxString& szFunc,
872 const wxString& szCond)
873{
874 wxDoOnAssert(szFile, nLine, szFunc, szCond);
875}
876
877void wxOnAssert(const wxChar *szFile,
878 int nLine,
879 const char *szFunc,
880 const wxChar *szCond,
881 const wxChar *szMsg)
882{
883 wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg);
884}
885
fbaf7d45
VS
886void wxOnAssert(const char *szFile,
887 int nLine,
888 const char *szFunc,
889 const char *szCond,
890 const wxString& szMsg)
891{
892 wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg);
893}
894
2b232d20
VZ
895void wxOnAssert(const char *szFile,
896 int nLine,
897 const char *szFunc,
898 const char *szCond,
899 const wxCStrData& msg)
900{
901 wxDoOnAssert(szFile, nLine, szFunc, szCond, msg);
902}
903
0accd1cf
VS
904#if wxUSE_UNICODE
905void wxOnAssert(const char *szFile,
906 int nLine,
907 const char *szFunc,
908 const char *szCond)
909{
910 wxDoOnAssert(szFile, nLine, szFunc, szCond);
911}
912
913void wxOnAssert(const char *szFile,
914 int nLine,
915 const char *szFunc,
916 const char *szCond,
917 const char *szMsg)
918{
919 wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg);
920}
921
922void wxOnAssert(const char *szFile,
923 int nLine,
924 const char *szFunc,
925 const char *szCond,
926 const wxChar *szMsg)
927{
928 wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg);
929}
930#endif // wxUSE_UNICODE
931
e2478fde
VZ
932#endif // __WXDEBUG__
933
934// ============================================================================
935// private functions implementation
936// ============================================================================
937
938#ifdef __WXDEBUG__
939
940static void LINKAGEMODE SetTraceMasks()
941{
942#if wxUSE_LOG
943 wxString mask;
944 if ( wxGetEnv(wxT("WXTRACE"), &mask) )
945 {
946 wxStringTokenizer tkn(mask, wxT(",;:"));
947 while ( tkn.HasMoreTokens() )
948 wxLog::AddTraceMask(tkn.GetNextToken());
949 }
950#endif // wxUSE_LOG
951}
952
6ef1b2a1 953static
e2478fde
VZ
954bool DoShowAssertDialog(const wxString& msg)
955{
956 // under MSW we can show the dialog even in the console mode
957#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
958 wxString msgDlg(msg);
959
960 // this message is intentionally not translated -- it is for
961 // developpers only
962 msgDlg += wxT("\nDo you want to stop the program?\n")
963 wxT("You can also choose [Cancel] to suppress ")
964 wxT("further warnings.");
965
e0a050e3 966 switch ( ::MessageBox(NULL, msgDlg.wx_str(), _T("wxWidgets Debug Alert"),
e2478fde
VZ
967 MB_YESNOCANCEL | MB_ICONSTOP ) )
968 {
969 case IDYES:
970 wxTrap();
971 break;
972
973 case IDCANCEL:
974 // stop the asserts
975 return true;
976
977 //case IDNO: nothing to do
978 }
979#else // !__WXMSW__
980 wxFprintf(stderr, wxT("%s\n"), msg.c_str());
981 fflush(stderr);
982
983 // TODO: ask the user to enter "Y" or "N" on the console?
984 wxTrap();
985#endif // __WXMSW__/!__WXMSW__
986
987 // continue with the asserts
988 return false;
989}
990
4a92d4bc
VZ
991// show the assert modal dialog
992static
0accd1cf 993void ShowAssertDialog(const wxString& szFile,
4a92d4bc 994 int nLine,
0accd1cf
VS
995 const wxString& szFunc,
996 const wxString& szCond,
997 const wxString& szMsg,
4a92d4bc
VZ
998 wxAppTraits *traits)
999{
1000 // this variable can be set to true to suppress "assert failure" messages
1001 static bool s_bNoAsserts = false;
1002
1003 wxString msg;
1004 msg.reserve(2048);
1005
1006 // make life easier for people using VC++ IDE by using this format: like
1007 // this, clicking on the message will take us immediately to the place of
1008 // the failed assert
1009 msg.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile, nLine, szCond);
1010
dfa0b52f 1011 // add the function name, if any
0accd1cf 1012 if ( !szFunc.empty() )
dfa0b52f
VZ
1013 msg << _T(" in ") << szFunc << _T("()");
1014
1015 // and the message itself
0accd1cf 1016 if ( !szMsg.empty() )
2c9b3131 1017 {
4a92d4bc 1018 msg << _T(": ") << szMsg;
2c9b3131 1019 }
4a92d4bc
VZ
1020 else // no message given
1021 {
1022 msg << _T('.');
1023 }
1024
e2478fde
VZ
1025#if wxUSE_THREADS
1026 // if we are not in the main thread, output the assert directly and trap
1027 // since dialogs cannot be displayed
1028 if ( !wxThread::IsMain() )
1029 {
1030 msg += wxT(" [in child thread]");
1031
1032#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1033 msg << wxT("\r\n");
e0a050e3 1034 OutputDebugString(msg.wx_str());
e2478fde
VZ
1035#else
1036 // send to stderr
1037 wxFprintf(stderr, wxT("%s\n"), msg.c_str());
1038 fflush(stderr);
1039#endif
1040 // He-e-e-e-elp!! we're asserting in a child thread
1041 wxTrap();
1042 }
6c8f8d92 1043 else
e2478fde
VZ
1044#endif // wxUSE_THREADS
1045
1046 if ( !s_bNoAsserts )
1047 {
1048 // send it to the normal log destination
56fae7b8 1049 wxLogDebug(_T("%s"), msg.c_str());
e2478fde
VZ
1050
1051 if ( traits )
1052 {
1053 // delegate showing assert dialog (if possible) to that class
1054 s_bNoAsserts = traits->ShowAssertDialog(msg);
1055 }
1056 else // no traits object
1057 {
1058 // fall back to the function of last resort
1059 s_bNoAsserts = DoShowAssertDialog(msg);
1060 }
1061 }
1062}
1063
1064#endif // __WXDEBUG__