1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/appbase.cpp
3 // Purpose: implements wxAppConsoleBase class
4 // Author: Vadim Zeitlin
6 // Created: 19.06.2003 (extracted from common/appcmn.cpp)
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/msw/wrapwin.h" // includes windows.h for MessageBox()
36 #include "wx/wxcrtvararg.h"
39 #include "wx/apptrait.h"
40 #include "wx/cmdline.h"
41 #include "wx/confbase.h"
42 #include "wx/evtloop.h"
43 #include "wx/filename.h"
44 #include "wx/msgout.h"
45 #include "wx/scopedptr.h"
46 #include "wx/sysopt.h"
47 #include "wx/tokenzr.h"
48 #include "wx/thread.h"
50 #if wxUSE_EXCEPTIONS && wxUSE_STL
55 #if !defined(__WINDOWS__) || defined(__WXMICROWIN__)
56 #include <signal.h> // for SIGTRAP used by wxTrap()
62 #include "wx/fontmap.h"
63 #endif // wxUSE_FONTMAP
67 #include "wx/stackwalk.h"
69 #include "wx/msw/debughlp.h"
71 #endif // wxUSE_STACKWALKER
73 #include "wx/recguard.h"
74 #endif // wxDEBUG_LEVEL
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"
83 // ----------------------------------------------------------------------------
84 // private functions prototypes
85 // ----------------------------------------------------------------------------
88 // really just show the assert dialog
89 static bool DoShowAssertDialog(const wxString
& msg
);
91 // prepare for showing the assert dialog, use the given traits or
92 // DoShowAssertDialog() as last fallback to really show it
94 void ShowAssertDialog(const wxString
& file
,
99 wxAppTraits
*traits
= NULL
);
100 #endif // wxDEBUG_LEVEL
103 // turn on the trace masks specified in the env variable WXTRACE
104 static void LINKAGEMODE
SetTraceMasks();
105 #endif // __WXDEBUG__
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 wxAppConsole
*wxAppConsoleBase::ms_appInstance
= NULL
;
113 wxAppInitializerFunction
wxAppConsoleBase::ms_appInitFn
= NULL
;
115 wxSocketManager
*wxAppTraitsBase::ms_manager
= NULL
;
117 WXDLLIMPEXP_DATA_BASE(wxList
) wxPendingDelete
;
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 // this defines wxEventLoopPtr
124 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopBase
)
126 // ============================================================================
127 // wxAppConsoleBase implementation
128 // ============================================================================
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 wxAppConsoleBase::wxAppConsoleBase()
138 m_bDoPendingEventProcessing
= true;
140 ms_appInstance
= static_cast<wxAppConsole
*>(this);
145 // In unicode mode the SetTraceMasks call can cause an apptraits to be
146 // created, but since we are still in the constructor the wrong kind will
147 // be created for GUI apps. Destroy it so it can be created again later.
152 wxEvtHandler::AddFilter(this);
155 wxAppConsoleBase::~wxAppConsoleBase()
157 wxEvtHandler::RemoveFilter(this);
159 // we're being destroyed and using this object from now on may not work or
160 // even crash so don't leave dangling pointers to it
161 ms_appInstance
= NULL
;
166 // ----------------------------------------------------------------------------
167 // initialization/cleanup
168 // ----------------------------------------------------------------------------
170 bool wxAppConsoleBase::Initialize(int& WXUNUSED(argc
), wxChar
**WXUNUSED(argv
))
173 GetTraits()->SetLocale();
179 wxString
wxAppConsoleBase::GetAppName() const
181 wxString name
= m_appName
;
186 // the application name is, by default, the name of its executable file
187 wxFileName::SplitPath(argv
[0], NULL
, &name
, NULL
);
193 wxString
wxAppConsoleBase::GetAppDisplayName() const
195 // use the explicitly provided display name, if any
196 if ( !m_appDisplayName
.empty() )
197 return m_appDisplayName
;
199 // if the application name was explicitly set, use it as is as capitalizing
200 // it won't always produce good results
201 if ( !m_appName
.empty() )
204 // if neither is set, use the capitalized version of the program file as
205 // it's the most reasonable default
206 return GetAppName().Capitalize();
209 wxEventLoopBase
*wxAppConsoleBase::CreateMainLoop()
211 return GetTraits()->CreateEventLoop();
214 void wxAppConsoleBase::CleanUp()
216 wxDELETE(m_mainLoop
);
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
223 bool wxAppConsoleBase::OnInit()
225 #if wxUSE_CMDLINE_PARSER
226 wxCmdLineParser
parser(argc
, argv
);
228 OnInitCmdLine(parser
);
231 switch ( parser
.Parse(false /* don't show usage */) )
234 cont
= OnCmdLineHelp(parser
);
238 cont
= OnCmdLineParsed(parser
);
242 cont
= OnCmdLineError(parser
);
248 #endif // wxUSE_CMDLINE_PARSER
253 int wxAppConsoleBase::OnRun()
258 int wxAppConsoleBase::OnExit()
261 // delete the config object if any (don't use Get() here, but Set()
262 // because Get() could create a new config object)
263 delete wxConfigBase::Set(NULL
);
264 #endif // wxUSE_CONFIG
269 void wxAppConsoleBase::Exit()
271 if (m_mainLoop
!= NULL
)
277 // ----------------------------------------------------------------------------
279 // ----------------------------------------------------------------------------
281 wxAppTraits
*wxAppConsoleBase::CreateTraits()
283 return new wxConsoleAppTraits
;
286 wxAppTraits
*wxAppConsoleBase::GetTraits()
288 // FIXME-MT: protect this with a CS?
291 m_traits
= CreateTraits();
293 wxASSERT_MSG( m_traits
, wxT("wxApp::CreateTraits() failed?") );
300 wxAppTraits
*wxAppConsoleBase::GetTraitsIfExists()
302 wxAppConsole
* const app
= GetInstance();
303 return app
? app
->GetTraits() : NULL
;
306 // ----------------------------------------------------------------------------
307 // wxEventLoop redirection
308 // ----------------------------------------------------------------------------
310 int wxAppConsoleBase::MainLoop()
312 wxEventLoopBaseTiedPtr
mainLoop(&m_mainLoop
, CreateMainLoop());
314 return m_mainLoop
? m_mainLoop
->Run() : -1;
317 void wxAppConsoleBase::ExitMainLoop()
319 // we should exit from the main event loop, not just any currently active
320 // (e.g. modal dialog) event loop
321 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
327 bool wxAppConsoleBase::Pending()
329 // use the currently active message loop here, not m_mainLoop, because if
330 // we're showing a modal dialog (with its own event loop) currently the
331 // main event loop is not running anyhow
332 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
334 return loop
&& loop
->Pending();
337 bool wxAppConsoleBase::Dispatch()
339 // see comment in Pending()
340 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
342 return loop
&& loop
->Dispatch();
345 bool wxAppConsoleBase::Yield(bool onlyIfNeeded
)
347 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
349 return loop
->Yield(onlyIfNeeded
);
351 wxScopedPtr
<wxEventLoopBase
> tmpLoop(CreateMainLoop());
352 return tmpLoop
->Yield(onlyIfNeeded
);
355 void wxAppConsoleBase::WakeUpIdle()
357 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
363 bool wxAppConsoleBase::ProcessIdle()
365 // synthesize an idle event and check if more of them are needed
367 event
.SetEventObject(this);
371 // flush the logged messages if any (do this after processing the events
372 // which could have logged new messages)
373 wxLog::FlushActive();
376 // Garbage collect all objects previously scheduled for destruction.
377 DeletePendingObjects();
379 return event
.MoreRequested();
382 bool wxAppConsoleBase::UsesEventLoop() const
384 // in console applications we don't know whether we're going to have an
385 // event loop so assume we won't -- unless we already have one running
386 return wxEventLoopBase::GetActive() != NULL
;
389 // ----------------------------------------------------------------------------
391 // ----------------------------------------------------------------------------
394 bool wxAppConsoleBase::IsMainLoopRunning()
396 const wxAppConsole
* const app
= GetInstance();
398 return app
&& app
->m_mainLoop
!= NULL
;
401 int wxAppConsoleBase::FilterEvent(wxEvent
& WXUNUSED(event
))
403 // process the events normally by default
407 void wxAppConsoleBase::DelayPendingEventHandler(wxEvtHandler
* toDelay
)
409 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
411 // move the handler from the list of handlers with processable pending events
412 // to the list of handlers with pending events which needs to be processed later
413 m_handlersWithPendingEvents
.Remove(toDelay
);
415 if (m_handlersWithPendingDelayedEvents
.Index(toDelay
) == wxNOT_FOUND
)
416 m_handlersWithPendingDelayedEvents
.Add(toDelay
);
418 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
421 void wxAppConsoleBase::RemovePendingEventHandler(wxEvtHandler
* toRemove
)
423 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
425 if (m_handlersWithPendingEvents
.Index(toRemove
) != wxNOT_FOUND
)
427 m_handlersWithPendingEvents
.Remove(toRemove
);
429 // check that the handler was present only once in the list
430 wxASSERT_MSG( m_handlersWithPendingEvents
.Index(toRemove
) == wxNOT_FOUND
,
431 "Handler occurs twice in the m_handlersWithPendingEvents list!" );
433 //else: it wasn't in this list at all, it's ok
435 if (m_handlersWithPendingDelayedEvents
.Index(toRemove
) != wxNOT_FOUND
)
437 m_handlersWithPendingDelayedEvents
.Remove(toRemove
);
439 // check that the handler was present only once in the list
440 wxASSERT_MSG( m_handlersWithPendingDelayedEvents
.Index(toRemove
) == wxNOT_FOUND
,
441 "Handler occurs twice in m_handlersWithPendingDelayedEvents list!" );
443 //else: it wasn't in this list at all, it's ok
445 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
448 void wxAppConsoleBase::AppendPendingEventHandler(wxEvtHandler
* toAppend
)
450 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
452 if ( m_handlersWithPendingEvents
.Index(toAppend
) == wxNOT_FOUND
)
453 m_handlersWithPendingEvents
.Add(toAppend
);
455 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
458 bool wxAppConsoleBase::HasPendingEvents() const
460 wxENTER_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
462 bool has
= !m_handlersWithPendingEvents
.IsEmpty();
464 wxLEAVE_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
469 void wxAppConsoleBase::SuspendProcessingOfPendingEvents()
471 m_bDoPendingEventProcessing
= false;
474 void wxAppConsoleBase::ResumeProcessingOfPendingEvents()
476 m_bDoPendingEventProcessing
= true;
479 void wxAppConsoleBase::ProcessPendingEvents()
481 if ( m_bDoPendingEventProcessing
)
483 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
485 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
486 "this helper list should be empty" );
488 // iterate until the list becomes empty: the handlers remove themselves
489 // from it when they don't have any more pending events
490 while (!m_handlersWithPendingEvents
.IsEmpty())
492 // In ProcessPendingEvents(), new handlers might be added
493 // and we can safely leave the critical section here.
494 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
496 // NOTE: we always call ProcessPendingEvents() on the first event handler
497 // with pending events because handlers auto-remove themselves
498 // from this list (see RemovePendingEventHandler) if they have no
499 // more pending events.
500 m_handlersWithPendingEvents
[0]->ProcessPendingEvents();
502 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
505 // now the wxHandlersWithPendingEvents is surely empty; however some event
506 // handlers may have moved themselves into wxHandlersWithPendingDelayedEvents
507 // because of a selective wxYield call in progress.
508 // Now we need to move them back to wxHandlersWithPendingEvents so the next
509 // call to this function has the chance of processing them:
510 if (!m_handlersWithPendingDelayedEvents
.IsEmpty())
512 WX_APPEND_ARRAY(m_handlersWithPendingEvents
, m_handlersWithPendingDelayedEvents
);
513 m_handlersWithPendingDelayedEvents
.Clear();
516 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
520 void wxAppConsoleBase::DeletePendingEvents()
522 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
524 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
525 "this helper list should be empty" );
527 for (unsigned int i
=0; i
<m_handlersWithPendingEvents
.GetCount(); i
++)
528 m_handlersWithPendingEvents
[i
]->DeletePendingEvents();
530 m_handlersWithPendingEvents
.Clear();
532 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
535 // ----------------------------------------------------------------------------
536 // delayed objects destruction
537 // ----------------------------------------------------------------------------
539 bool wxAppConsoleBase::IsScheduledForDestruction(wxObject
*object
) const
541 return wxPendingDelete
.Member(object
);
544 void wxAppConsoleBase::ScheduleForDestruction(wxObject
*object
)
546 if ( !UsesEventLoop() )
548 // we won't be able to delete it later so do it right now
552 //else: we either already have or will soon start an event loop
554 if ( !wxPendingDelete
.Member(object
) )
555 wxPendingDelete
.Append(object
);
558 void wxAppConsoleBase::DeletePendingObjects()
560 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
563 wxObject
*obj
= node
->GetData();
565 // remove it from the list first so that if we get back here somehow
566 // during the object deletion (e.g. wxYield called from its dtor) we
567 // wouldn't try to delete it the second time
568 if ( wxPendingDelete
.Member(obj
) )
569 wxPendingDelete
.Erase(node
);
573 // Deleting one object may have deleted other pending
574 // objects, so start from beginning of list again.
575 node
= wxPendingDelete
.GetFirst();
579 // ----------------------------------------------------------------------------
580 // exception handling
581 // ----------------------------------------------------------------------------
586 wxAppConsoleBase::HandleEvent(wxEvtHandler
*handler
,
587 wxEventFunction func
,
588 wxEvent
& event
) const
590 // by default, simply call the handler
591 (handler
->*func
)(event
);
594 void wxAppConsoleBase::CallEventHandler(wxEvtHandler
*handler
,
595 wxEventFunctor
& functor
,
596 wxEvent
& event
) const
598 // If the functor holds a method then, for backward compatibility, call
600 wxEventFunction eventFunction
= functor
.GetEvtMethod();
603 HandleEvent(handler
, eventFunction
, event
);
605 functor(handler
, event
);
608 void wxAppConsoleBase::OnUnhandledException()
611 // we're called from an exception handler so we can re-throw the exception
612 // to recover its type
619 catch ( std::exception
& e
)
621 what
.Printf("std::exception of type \"%s\", what() = \"%s\"",
622 typeid(e
).name(), e
.what());
627 what
= "unknown exception";
630 wxMessageOutputBest().Printf(
631 "*** Caught unhandled %s; terminating\n", what
633 #endif // __WXDEBUG__
636 // ----------------------------------------------------------------------------
637 // exceptions support
638 // ----------------------------------------------------------------------------
640 bool wxAppConsoleBase::OnExceptionInMainLoop()
644 // some compilers are too stupid to know that we never return after throw
645 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
650 #endif // wxUSE_EXCEPTIONS
652 // ----------------------------------------------------------------------------
654 // ----------------------------------------------------------------------------
656 #if wxUSE_CMDLINE_PARSER
658 #define OPTION_VERBOSE "verbose"
660 void wxAppConsoleBase::OnInitCmdLine(wxCmdLineParser
& parser
)
662 // the standard command line options
663 static const wxCmdLineEntryDesc cmdLineDesc
[] =
669 gettext_noop("show this help message"),
671 wxCMD_LINE_OPTION_HELP
679 gettext_noop("generate verbose log messages"),
689 parser
.SetDesc(cmdLineDesc
);
692 bool wxAppConsoleBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
695 if ( parser
.Found(OPTION_VERBOSE
) )
697 wxLog::SetVerbose(true);
706 bool wxAppConsoleBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
713 bool wxAppConsoleBase::OnCmdLineError(wxCmdLineParser
& parser
)
720 #endif // wxUSE_CMDLINE_PARSER
722 // ----------------------------------------------------------------------------
724 // ----------------------------------------------------------------------------
727 bool wxAppConsoleBase::CheckBuildOptions(const char *optionsSignature
,
728 const char *componentName
)
730 #if 0 // can't use wxLogTrace, not up and running yet
731 printf("checking build options object '%s' (ptr %p) in '%s'\n",
732 optionsSignature
, optionsSignature
, componentName
);
735 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
737 wxString lib
= wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
738 wxString prog
= wxString::FromAscii(optionsSignature
);
739 wxString progName
= wxString::FromAscii(componentName
);
742 msg
.Printf(wxT("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
743 lib
.c_str(), progName
.c_str(), prog
.c_str());
745 wxLogFatalError(msg
.c_str());
747 // normally wxLogFatalError doesn't return
754 void wxAppConsoleBase::OnAssertFailure(const wxChar
*file
,
761 ShowAssertDialog(file
, line
, func
, cond
, msg
, GetTraits());
763 // this function is still present even in debug level 0 build for ABI
764 // compatibility reasons but is never called there and so can simply do
771 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
774 void wxAppConsoleBase::OnAssert(const wxChar
*file
,
779 OnAssertFailure(file
, line
, NULL
, cond
, msg
);
782 // ============================================================================
783 // other classes implementations
784 // ============================================================================
786 // ----------------------------------------------------------------------------
787 // wxConsoleAppTraitsBase
788 // ----------------------------------------------------------------------------
792 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
794 return new wxLogStderr
;
799 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
801 return new wxMessageOutputStderr
;
806 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
808 return (wxFontMapper
*)new wxFontMapperBase
;
811 #endif // wxUSE_FONTMAP
813 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
815 // console applications don't use renderers
819 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
821 return wxAppTraitsBase::ShowAssertDialog(msg
);
824 bool wxConsoleAppTraitsBase::HasStderr()
826 // console applications always have stderr, even under Mac/Windows
830 // ----------------------------------------------------------------------------
832 // ----------------------------------------------------------------------------
835 void wxAppTraitsBase::SetLocale()
837 wxSetlocale(LC_ALL
, "");
838 wxUpdateLocaleIsUtf8();
843 void wxMutexGuiEnterImpl();
844 void wxMutexGuiLeaveImpl();
846 void wxAppTraitsBase::MutexGuiEnter()
848 wxMutexGuiEnterImpl();
851 void wxAppTraitsBase::MutexGuiLeave()
853 wxMutexGuiLeaveImpl();
856 void WXDLLIMPEXP_BASE
wxMutexGuiEnter()
858 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
860 traits
->MutexGuiEnter();
863 void WXDLLIMPEXP_BASE
wxMutexGuiLeave()
865 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
867 traits
->MutexGuiLeave();
869 #endif // wxUSE_THREADS
871 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msgOriginal
)
876 #if wxUSE_STACKWALKER
877 const wxString stackTrace
= GetAssertStackTrace();
878 if ( !stackTrace
.empty() )
880 msg
<< wxT("\n\nCall stack:\n") << stackTrace
;
882 wxMessageOutputDebug().Output(msg
);
884 #endif // wxUSE_STACKWALKER
886 return DoShowAssertDialog(msgOriginal
+ msg
);
887 #else // !wxDEBUG_LEVEL
888 wxUnusedVar(msgOriginal
);
891 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
894 #if wxUSE_STACKWALKER
895 wxString
wxAppTraitsBase::GetAssertStackTrace()
899 #if !defined(__WINDOWS__)
900 // on Unix stack frame generation may take some time, depending on the
901 // size of the executable mainly... warn the user that we are working
902 wxFprintf(stderr
, "Collecting stack trace information, please wait...");
904 #endif // !__WINDOWS__
909 class StackDump
: public wxStackWalker
914 const wxString
& GetStackTrace() const { return m_stackTrace
; }
917 virtual void OnStackFrame(const wxStackFrame
& frame
)
919 m_stackTrace
<< wxString::Format
922 wx_truncate_cast(int, frame
.GetLevel())
925 wxString name
= frame
.GetName();
928 m_stackTrace
<< wxString::Format(wxT("%-40s"), name
.c_str());
932 m_stackTrace
<< wxString::Format(wxT("%p"), frame
.GetAddress());
935 if ( frame
.HasSourceLocation() )
937 m_stackTrace
<< wxT('\t')
938 << frame
.GetFileName()
943 m_stackTrace
<< wxT('\n');
947 wxString m_stackTrace
;
950 // don't show more than maxLines or we could get a dialog too tall to be
951 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
952 // characters it is still only 300 pixels...
953 static const int maxLines
= 20;
956 dump
.Walk(2, maxLines
); // don't show OnAssert() call itself
957 stackTrace
= dump
.GetStackTrace();
959 const int count
= stackTrace
.Freq(wxT('\n'));
960 for ( int i
= 0; i
< count
- maxLines
; i
++ )
961 stackTrace
= stackTrace
.BeforeLast(wxT('\n'));
964 #else // !wxDEBUG_LEVEL
965 // this function is still present for ABI-compatibility even in debug level
966 // 0 build but is not used there and so can simply do nothing
968 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
970 #endif // wxUSE_STACKWALKER
973 // ============================================================================
974 // global functions implementation
975 // ============================================================================
985 // what else can we do?
994 wxTheApp
->WakeUpIdle();
996 //else: do nothing, what can we do?
1000 bool wxAssertIsEqual(int x
, int y
)
1016 // break into the debugger
1019 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
1021 #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
1023 #elif defined(__UNIX__)
1030 // default assert handler
1032 wxDefaultAssertHandler(const wxString
& file
,
1034 const wxString
& func
,
1035 const wxString
& cond
,
1036 const wxString
& msg
)
1038 // If this option is set, we should abort immediately when assert happens.
1039 if ( wxSystemOptions::GetOptionInt("exit-on-assert") )
1043 static int s_bInAssert
= 0;
1045 wxRecursionGuard
guard(s_bInAssert
);
1046 if ( guard
.IsInside() )
1048 // can't use assert here to avoid infinite loops, so just trap
1056 // by default, show the assert dialog box -- we can't customize this
1058 ShowAssertDialog(file
, line
, func
, cond
, msg
);
1062 // let the app process it as it wants
1063 // FIXME-UTF8: use wc_str(), not c_str(), when ANSI build is removed
1064 wxTheApp
->OnAssertFailure(file
.c_str(), line
, func
.c_str(),
1065 cond
.c_str(), msg
.c_str());
1069 wxAssertHandler_t wxTheAssertHandler
= wxDefaultAssertHandler
;
1071 void wxSetDefaultAssertHandler()
1073 wxTheAssertHandler
= wxDefaultAssertHandler
;
1076 void wxOnAssert(const wxString
& file
,
1078 const wxString
& func
,
1079 const wxString
& cond
,
1080 const wxString
& msg
)
1082 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1085 void wxOnAssert(const wxString
& file
,
1087 const wxString
& func
,
1088 const wxString
& cond
)
1090 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
1093 void wxOnAssert(const wxChar
*file
,
1099 // this is the backwards-compatible version (unless we don't use Unicode)
1100 // so it could be called directly from the user code and this might happen
1101 // even when wxTheAssertHandler is NULL
1103 if ( wxTheAssertHandler
)
1104 #endif // wxUSE_UNICODE
1105 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1108 void wxOnAssert(const char *file
,
1112 const wxString
& msg
)
1114 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1117 void wxOnAssert(const char *file
,
1121 const wxCStrData
& msg
)
1123 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1127 void wxOnAssert(const char *file
,
1132 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
1135 void wxOnAssert(const char *file
,
1141 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1144 void wxOnAssert(const char *file
,
1150 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1152 #endif // wxUSE_UNICODE
1154 #endif // wxDEBUG_LEVEL
1156 // ============================================================================
1157 // private functions implementation
1158 // ============================================================================
1162 static void LINKAGEMODE
SetTraceMasks()
1166 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
1168 wxStringTokenizer
tkn(mask
, wxT(",;:"));
1169 while ( tkn
.HasMoreTokens() )
1170 wxLog::AddTraceMask(tkn
.GetNextToken());
1175 #endif // __WXDEBUG__
1180 bool DoShowAssertDialog(const wxString
& msg
)
1182 // under Windows we can show the dialog even in the console mode
1183 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
1184 wxString
msgDlg(msg
);
1186 // this message is intentionally not translated -- it is for developers
1187 // only -- and the less code we use here, less is the danger of recursively
1188 // asserting and dying
1189 msgDlg
+= wxT("\nDo you want to stop the program?\n")
1190 wxT("You can also choose [Cancel] to suppress ")
1191 wxT("further warnings.");
1193 switch ( ::MessageBox(NULL
, msgDlg
.t_str(), wxT("wxWidgets Debug Alert"),
1194 MB_YESNOCANCEL
| MB_ICONSTOP
) )
1204 //case IDNO: nothing to do
1206 #else // !__WINDOWS__
1208 #endif // __WINDOWS__/!__WINDOWS__
1210 // continue with the asserts by default
1214 // show the standard assert dialog
1216 void ShowAssertDialog(const wxString
& file
,
1218 const wxString
& func
,
1219 const wxString
& cond
,
1220 const wxString
& msgUser
,
1221 wxAppTraits
*traits
)
1223 // this variable can be set to true to suppress "assert failure" messages
1224 static bool s_bNoAsserts
= false;
1229 // make life easier for people using VC++ IDE by using this format: like
1230 // this, clicking on the message will take us immediately to the place of
1231 // the failed assert
1232 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), file
, line
, cond
);
1234 // add the function name, if any
1235 if ( !func
.empty() )
1236 msg
<< wxT(" in ") << func
<< wxT("()");
1238 // and the message itself
1239 if ( !msgUser
.empty() )
1241 msg
<< wxT(": ") << msgUser
;
1243 else // no message given
1249 // if we are not in the main thread, output the assert directly and trap
1250 // since dialogs cannot be displayed
1251 if ( !wxThread::IsMain() )
1253 msg
+= wxString::Format(" [in thread %lx]", wxThread::GetCurrentId());
1255 #endif // wxUSE_THREADS
1257 // log the assert in any case
1258 wxMessageOutputDebug().Output(msg
);
1260 if ( !s_bNoAsserts
)
1264 // delegate showing assert dialog (if possible) to that class
1265 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
1267 else // no traits object
1269 // fall back to the function of last resort
1270 s_bNoAsserts
= DoShowAssertDialog(msg
);
1275 #endif // wxDEBUG_LEVEL