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
56 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
57 #include <signal.h> // for SIGTRAP used by wxTrap()
61 #endif // ! __WXPALMOS5__
64 #include "wx/fontmap.h"
65 #endif // wxUSE_FONTMAP
69 #include "wx/stackwalk.h"
71 #include "wx/msw/debughlp.h"
73 #endif // wxUSE_STACKWALKER
75 #include "wx/recguard.h"
76 #endif // wxDEBUG_LEVEL
78 // wxABI_VERSION can be defined when compiling applications but it should be
79 // left undefined when compiling the library itself, it is then set to its
80 // default value in version.h
81 #if wxABI_VERSION != wxMAJOR_VERSION * 10000 + wxMINOR_VERSION * 100 + 99
82 #error "wxABI_VERSION should not be defined when compiling the library"
85 // ----------------------------------------------------------------------------
86 // private functions prototypes
87 // ----------------------------------------------------------------------------
90 // really just show the assert dialog
91 static bool DoShowAssertDialog(const wxString
& msg
);
93 // prepare for showing the assert dialog, use the given traits or
94 // DoShowAssertDialog() as last fallback to really show it
96 void ShowAssertDialog(const wxString
& file
,
101 wxAppTraits
*traits
= NULL
);
102 #endif // wxDEBUG_LEVEL
105 // turn on the trace masks specified in the env variable WXTRACE
106 static void LINKAGEMODE
SetTraceMasks();
107 #endif // __WXDEBUG__
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 wxAppConsole
*wxAppConsoleBase::ms_appInstance
= NULL
;
115 wxAppInitializerFunction
wxAppConsoleBase::ms_appInitFn
= NULL
;
117 wxSocketManager
*wxAppTraitsBase::ms_manager
= NULL
;
119 WXDLLIMPEXP_DATA_BASE(wxList
) wxPendingDelete
;
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 // this defines wxEventLoopPtr
126 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopBase
)
128 // ============================================================================
129 // wxAppConsoleBase implementation
130 // ============================================================================
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 wxAppConsoleBase::wxAppConsoleBase()
140 m_bDoPendingEventProcessing
= true;
142 ms_appInstance
= static_cast<wxAppConsole
*>(this);
147 // In unicode mode the SetTraceMasks call can cause an apptraits to be
148 // created, but since we are still in the constructor the wrong kind will
149 // be created for GUI apps. Destroy it so it can be created again later.
155 wxAppConsoleBase::~wxAppConsoleBase()
157 // we're being destroyed and using this object from now on may not work or
158 // even crash so don't leave dangling pointers to it
159 ms_appInstance
= NULL
;
164 // ----------------------------------------------------------------------------
165 // initialization/cleanup
166 // ----------------------------------------------------------------------------
168 bool wxAppConsoleBase::Initialize(int& WXUNUSED(argc
), wxChar
**WXUNUSED(argv
))
171 GetTraits()->SetLocale();
177 wxString
wxAppConsoleBase::GetAppName() const
179 wxString name
= m_appName
;
185 // the application name is, by default, the name of its executable file
186 wxFileName::SplitPath(argv
[0], NULL
, &name
, NULL
);
189 #endif // !__WXPALMOS__
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(__WXMSW__)
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...");
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
)
1007 // break into the debugger
1010 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1012 #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
1014 #elif defined(__UNIX__)
1021 // default assert handler
1023 wxDefaultAssertHandler(const wxString
& file
,
1025 const wxString
& func
,
1026 const wxString
& cond
,
1027 const wxString
& msg
)
1029 // If this option is set, we should abort immediately when assert happens.
1030 if ( wxSystemOptions::GetOptionInt("exit-on-assert") )
1034 static int s_bInAssert
= 0;
1036 wxRecursionGuard
guard(s_bInAssert
);
1037 if ( guard
.IsInside() )
1039 // can't use assert here to avoid infinite loops, so just trap
1047 // by default, show the assert dialog box -- we can't customize this
1049 ShowAssertDialog(file
, line
, func
, cond
, msg
);
1053 // let the app process it as it wants
1054 // FIXME-UTF8: use wc_str(), not c_str(), when ANSI build is removed
1055 wxTheApp
->OnAssertFailure(file
.c_str(), line
, func
.c_str(),
1056 cond
.c_str(), msg
.c_str());
1060 wxAssertHandler_t wxTheAssertHandler
= wxDefaultAssertHandler
;
1062 void wxSetDefaultAssertHandler()
1064 wxTheAssertHandler
= wxDefaultAssertHandler
;
1067 void wxOnAssert(const wxString
& file
,
1069 const wxString
& func
,
1070 const wxString
& cond
,
1071 const wxString
& msg
)
1073 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1076 void wxOnAssert(const wxString
& file
,
1078 const wxString
& func
,
1079 const wxString
& cond
)
1081 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
1084 void wxOnAssert(const wxChar
*file
,
1090 // this is the backwards-compatible version (unless we don't use Unicode)
1091 // so it could be called directly from the user code and this might happen
1092 // even when wxTheAssertHandler is NULL
1094 if ( wxTheAssertHandler
)
1095 #endif // wxUSE_UNICODE
1096 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1099 void wxOnAssert(const char *file
,
1103 const wxString
& msg
)
1105 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1108 void wxOnAssert(const char *file
,
1112 const wxCStrData
& msg
)
1114 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1118 void wxOnAssert(const char *file
,
1123 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
1126 void wxOnAssert(const char *file
,
1132 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1135 void wxOnAssert(const char *file
,
1141 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1143 #endif // wxUSE_UNICODE
1145 #endif // wxDEBUG_LEVEL
1147 // ============================================================================
1148 // private functions implementation
1149 // ============================================================================
1153 static void LINKAGEMODE
SetTraceMasks()
1157 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
1159 wxStringTokenizer
tkn(mask
, wxT(",;:"));
1160 while ( tkn
.HasMoreTokens() )
1161 wxLog::AddTraceMask(tkn
.GetNextToken());
1166 #endif // __WXDEBUG__
1171 bool DoShowAssertDialog(const wxString
& msg
)
1173 // under MSW we can show the dialog even in the console mode
1174 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1175 wxString
msgDlg(msg
);
1177 // this message is intentionally not translated -- it is for developers
1178 // only -- and the less code we use here, less is the danger of recursively
1179 // asserting and dying
1180 msgDlg
+= wxT("\nDo you want to stop the program?\n")
1181 wxT("You can also choose [Cancel] to suppress ")
1182 wxT("further warnings.");
1184 switch ( ::MessageBox(NULL
, msgDlg
.wx_str(), wxT("wxWidgets Debug Alert"),
1185 MB_YESNOCANCEL
| MB_ICONSTOP
) )
1195 //case IDNO: nothing to do
1199 #endif // __WXMSW__/!__WXMSW__
1201 // continue with the asserts by default
1205 // show the standard assert dialog
1207 void ShowAssertDialog(const wxString
& file
,
1209 const wxString
& func
,
1210 const wxString
& cond
,
1211 const wxString
& msgUser
,
1212 wxAppTraits
*traits
)
1214 // this variable can be set to true to suppress "assert failure" messages
1215 static bool s_bNoAsserts
= false;
1220 // make life easier for people using VC++ IDE by using this format: like
1221 // this, clicking on the message will take us immediately to the place of
1222 // the failed assert
1223 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), file
, line
, cond
);
1225 // add the function name, if any
1226 if ( !func
.empty() )
1227 msg
<< wxT(" in ") << func
<< wxT("()");
1229 // and the message itself
1230 if ( !msgUser
.empty() )
1232 msg
<< wxT(": ") << msgUser
;
1234 else // no message given
1240 // if we are not in the main thread, output the assert directly and trap
1241 // since dialogs cannot be displayed
1242 if ( !wxThread::IsMain() )
1244 msg
+= wxString::Format(" [in thread %lx]", wxThread::GetCurrentId());
1246 #endif // wxUSE_THREADS
1248 // log the assert in any case
1249 wxMessageOutputDebug().Output(msg
);
1251 if ( !s_bNoAsserts
)
1255 // delegate showing assert dialog (if possible) to that class
1256 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
1258 else // no traits object
1260 // fall back to the function of last resort
1261 s_bNoAsserts
= DoShowAssertDialog(msg
);
1266 #endif // wxDEBUG_LEVEL