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.
154 wxEvtHandler::AddFilter(this);
157 wxAppConsoleBase::~wxAppConsoleBase()
159 wxEvtHandler::RemoveFilter(this);
161 // we're being destroyed and using this object from now on may not work or
162 // even crash so don't leave dangling pointers to it
163 ms_appInstance
= NULL
;
168 // ----------------------------------------------------------------------------
169 // initialization/cleanup
170 // ----------------------------------------------------------------------------
172 bool wxAppConsoleBase::Initialize(int& WXUNUSED(argc
), wxChar
**WXUNUSED(argv
))
175 GetTraits()->SetLocale();
181 wxString
wxAppConsoleBase::GetAppName() const
183 wxString name
= m_appName
;
189 // the application name is, by default, the name of its executable file
190 wxFileName::SplitPath(argv
[0], NULL
, &name
, NULL
);
193 #endif // !__WXPALMOS__
197 wxString
wxAppConsoleBase::GetAppDisplayName() const
199 // use the explicitly provided display name, if any
200 if ( !m_appDisplayName
.empty() )
201 return m_appDisplayName
;
203 // if the application name was explicitly set, use it as is as capitalizing
204 // it won't always produce good results
205 if ( !m_appName
.empty() )
208 // if neither is set, use the capitalized version of the program file as
209 // it's the most reasonable default
210 return GetAppName().Capitalize();
213 wxEventLoopBase
*wxAppConsoleBase::CreateMainLoop()
215 return GetTraits()->CreateEventLoop();
218 void wxAppConsoleBase::CleanUp()
220 wxDELETE(m_mainLoop
);
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
227 bool wxAppConsoleBase::OnInit()
229 #if wxUSE_CMDLINE_PARSER
230 wxCmdLineParser
parser(argc
, argv
);
232 OnInitCmdLine(parser
);
235 switch ( parser
.Parse(false /* don't show usage */) )
238 cont
= OnCmdLineHelp(parser
);
242 cont
= OnCmdLineParsed(parser
);
246 cont
= OnCmdLineError(parser
);
252 #endif // wxUSE_CMDLINE_PARSER
257 int wxAppConsoleBase::OnRun()
262 int wxAppConsoleBase::OnExit()
265 // delete the config object if any (don't use Get() here, but Set()
266 // because Get() could create a new config object)
267 delete wxConfigBase::Set(NULL
);
268 #endif // wxUSE_CONFIG
273 void wxAppConsoleBase::Exit()
275 if (m_mainLoop
!= NULL
)
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 wxAppTraits
*wxAppConsoleBase::CreateTraits()
287 return new wxConsoleAppTraits
;
290 wxAppTraits
*wxAppConsoleBase::GetTraits()
292 // FIXME-MT: protect this with a CS?
295 m_traits
= CreateTraits();
297 wxASSERT_MSG( m_traits
, wxT("wxApp::CreateTraits() failed?") );
304 wxAppTraits
*wxAppConsoleBase::GetTraitsIfExists()
306 wxAppConsole
* const app
= GetInstance();
307 return app
? app
->GetTraits() : NULL
;
310 // ----------------------------------------------------------------------------
311 // wxEventLoop redirection
312 // ----------------------------------------------------------------------------
314 int wxAppConsoleBase::MainLoop()
316 wxEventLoopBaseTiedPtr
mainLoop(&m_mainLoop
, CreateMainLoop());
318 return m_mainLoop
? m_mainLoop
->Run() : -1;
321 void wxAppConsoleBase::ExitMainLoop()
323 // we should exit from the main event loop, not just any currently active
324 // (e.g. modal dialog) event loop
325 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
331 bool wxAppConsoleBase::Pending()
333 // use the currently active message loop here, not m_mainLoop, because if
334 // we're showing a modal dialog (with its own event loop) currently the
335 // main event loop is not running anyhow
336 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
338 return loop
&& loop
->Pending();
341 bool wxAppConsoleBase::Dispatch()
343 // see comment in Pending()
344 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
346 return loop
&& loop
->Dispatch();
349 bool wxAppConsoleBase::Yield(bool onlyIfNeeded
)
351 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
353 return loop
->Yield(onlyIfNeeded
);
355 wxScopedPtr
<wxEventLoopBase
> tmpLoop(CreateMainLoop());
356 return tmpLoop
->Yield(onlyIfNeeded
);
359 void wxAppConsoleBase::WakeUpIdle()
361 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
367 bool wxAppConsoleBase::ProcessIdle()
369 // synthesize an idle event and check if more of them are needed
371 event
.SetEventObject(this);
375 // flush the logged messages if any (do this after processing the events
376 // which could have logged new messages)
377 wxLog::FlushActive();
380 // Garbage collect all objects previously scheduled for destruction.
381 DeletePendingObjects();
383 return event
.MoreRequested();
386 bool wxAppConsoleBase::UsesEventLoop() const
388 // in console applications we don't know whether we're going to have an
389 // event loop so assume we won't -- unless we already have one running
390 return wxEventLoopBase::GetActive() != NULL
;
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
398 bool wxAppConsoleBase::IsMainLoopRunning()
400 const wxAppConsole
* const app
= GetInstance();
402 return app
&& app
->m_mainLoop
!= NULL
;
405 int wxAppConsoleBase::FilterEvent(wxEvent
& WXUNUSED(event
))
407 // process the events normally by default
411 void wxAppConsoleBase::DelayPendingEventHandler(wxEvtHandler
* toDelay
)
413 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
415 // move the handler from the list of handlers with processable pending events
416 // to the list of handlers with pending events which needs to be processed later
417 m_handlersWithPendingEvents
.Remove(toDelay
);
419 if (m_handlersWithPendingDelayedEvents
.Index(toDelay
) == wxNOT_FOUND
)
420 m_handlersWithPendingDelayedEvents
.Add(toDelay
);
422 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
425 void wxAppConsoleBase::RemovePendingEventHandler(wxEvtHandler
* toRemove
)
427 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
429 if (m_handlersWithPendingEvents
.Index(toRemove
) != wxNOT_FOUND
)
431 m_handlersWithPendingEvents
.Remove(toRemove
);
433 // check that the handler was present only once in the list
434 wxASSERT_MSG( m_handlersWithPendingEvents
.Index(toRemove
) == wxNOT_FOUND
,
435 "Handler occurs twice in the m_handlersWithPendingEvents list!" );
437 //else: it wasn't in this list at all, it's ok
439 if (m_handlersWithPendingDelayedEvents
.Index(toRemove
) != wxNOT_FOUND
)
441 m_handlersWithPendingDelayedEvents
.Remove(toRemove
);
443 // check that the handler was present only once in the list
444 wxASSERT_MSG( m_handlersWithPendingDelayedEvents
.Index(toRemove
) == wxNOT_FOUND
,
445 "Handler occurs twice in m_handlersWithPendingDelayedEvents list!" );
447 //else: it wasn't in this list at all, it's ok
449 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
452 void wxAppConsoleBase::AppendPendingEventHandler(wxEvtHandler
* toAppend
)
454 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
456 if ( m_handlersWithPendingEvents
.Index(toAppend
) == wxNOT_FOUND
)
457 m_handlersWithPendingEvents
.Add(toAppend
);
459 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
462 bool wxAppConsoleBase::HasPendingEvents() const
464 wxENTER_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
466 bool has
= !m_handlersWithPendingEvents
.IsEmpty();
468 wxLEAVE_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
473 void wxAppConsoleBase::SuspendProcessingOfPendingEvents()
475 m_bDoPendingEventProcessing
= false;
478 void wxAppConsoleBase::ResumeProcessingOfPendingEvents()
480 m_bDoPendingEventProcessing
= true;
483 void wxAppConsoleBase::ProcessPendingEvents()
485 if ( m_bDoPendingEventProcessing
)
487 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
489 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
490 "this helper list should be empty" );
492 // iterate until the list becomes empty: the handlers remove themselves
493 // from it when they don't have any more pending events
494 while (!m_handlersWithPendingEvents
.IsEmpty())
496 // In ProcessPendingEvents(), new handlers might be added
497 // and we can safely leave the critical section here.
498 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
500 // NOTE: we always call ProcessPendingEvents() on the first event handler
501 // with pending events because handlers auto-remove themselves
502 // from this list (see RemovePendingEventHandler) if they have no
503 // more pending events.
504 m_handlersWithPendingEvents
[0]->ProcessPendingEvents();
506 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
509 // now the wxHandlersWithPendingEvents is surely empty; however some event
510 // handlers may have moved themselves into wxHandlersWithPendingDelayedEvents
511 // because of a selective wxYield call in progress.
512 // Now we need to move them back to wxHandlersWithPendingEvents so the next
513 // call to this function has the chance of processing them:
514 if (!m_handlersWithPendingDelayedEvents
.IsEmpty())
516 WX_APPEND_ARRAY(m_handlersWithPendingEvents
, m_handlersWithPendingDelayedEvents
);
517 m_handlersWithPendingDelayedEvents
.Clear();
520 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
524 void wxAppConsoleBase::DeletePendingEvents()
526 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
528 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
529 "this helper list should be empty" );
531 for (unsigned int i
=0; i
<m_handlersWithPendingEvents
.GetCount(); i
++)
532 m_handlersWithPendingEvents
[i
]->DeletePendingEvents();
534 m_handlersWithPendingEvents
.Clear();
536 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
539 // ----------------------------------------------------------------------------
540 // delayed objects destruction
541 // ----------------------------------------------------------------------------
543 bool wxAppConsoleBase::IsScheduledForDestruction(wxObject
*object
) const
545 return wxPendingDelete
.Member(object
);
548 void wxAppConsoleBase::ScheduleForDestruction(wxObject
*object
)
550 if ( !UsesEventLoop() )
552 // we won't be able to delete it later so do it right now
556 //else: we either already have or will soon start an event loop
558 if ( !wxPendingDelete
.Member(object
) )
559 wxPendingDelete
.Append(object
);
562 void wxAppConsoleBase::DeletePendingObjects()
564 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
567 wxObject
*obj
= node
->GetData();
569 // remove it from the list first so that if we get back here somehow
570 // during the object deletion (e.g. wxYield called from its dtor) we
571 // wouldn't try to delete it the second time
572 if ( wxPendingDelete
.Member(obj
) )
573 wxPendingDelete
.Erase(node
);
577 // Deleting one object may have deleted other pending
578 // objects, so start from beginning of list again.
579 node
= wxPendingDelete
.GetFirst();
583 // ----------------------------------------------------------------------------
584 // exception handling
585 // ----------------------------------------------------------------------------
590 wxAppConsoleBase::HandleEvent(wxEvtHandler
*handler
,
591 wxEventFunction func
,
592 wxEvent
& event
) const
594 // by default, simply call the handler
595 (handler
->*func
)(event
);
598 void wxAppConsoleBase::CallEventHandler(wxEvtHandler
*handler
,
599 wxEventFunctor
& functor
,
600 wxEvent
& event
) const
602 // If the functor holds a method then, for backward compatibility, call
604 wxEventFunction eventFunction
= functor
.GetEvtMethod();
607 HandleEvent(handler
, eventFunction
, event
);
609 functor(handler
, event
);
612 void wxAppConsoleBase::OnUnhandledException()
615 // we're called from an exception handler so we can re-throw the exception
616 // to recover its type
623 catch ( std::exception
& e
)
625 what
.Printf("std::exception of type \"%s\", what() = \"%s\"",
626 typeid(e
).name(), e
.what());
631 what
= "unknown exception";
634 wxMessageOutputBest().Printf(
635 "*** Caught unhandled %s; terminating\n", what
637 #endif // __WXDEBUG__
640 // ----------------------------------------------------------------------------
641 // exceptions support
642 // ----------------------------------------------------------------------------
644 bool wxAppConsoleBase::OnExceptionInMainLoop()
648 // some compilers are too stupid to know that we never return after throw
649 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
654 #endif // wxUSE_EXCEPTIONS
656 // ----------------------------------------------------------------------------
658 // ----------------------------------------------------------------------------
660 #if wxUSE_CMDLINE_PARSER
662 #define OPTION_VERBOSE "verbose"
664 void wxAppConsoleBase::OnInitCmdLine(wxCmdLineParser
& parser
)
666 // the standard command line options
667 static const wxCmdLineEntryDesc cmdLineDesc
[] =
673 gettext_noop("show this help message"),
675 wxCMD_LINE_OPTION_HELP
683 gettext_noop("generate verbose log messages"),
693 parser
.SetDesc(cmdLineDesc
);
696 bool wxAppConsoleBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
699 if ( parser
.Found(OPTION_VERBOSE
) )
701 wxLog::SetVerbose(true);
710 bool wxAppConsoleBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
717 bool wxAppConsoleBase::OnCmdLineError(wxCmdLineParser
& parser
)
724 #endif // wxUSE_CMDLINE_PARSER
726 // ----------------------------------------------------------------------------
728 // ----------------------------------------------------------------------------
731 bool wxAppConsoleBase::CheckBuildOptions(const char *optionsSignature
,
732 const char *componentName
)
734 #if 0 // can't use wxLogTrace, not up and running yet
735 printf("checking build options object '%s' (ptr %p) in '%s'\n",
736 optionsSignature
, optionsSignature
, componentName
);
739 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
741 wxString lib
= wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
742 wxString prog
= wxString::FromAscii(optionsSignature
);
743 wxString progName
= wxString::FromAscii(componentName
);
746 msg
.Printf(wxT("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
747 lib
.c_str(), progName
.c_str(), prog
.c_str());
749 wxLogFatalError(msg
.c_str());
751 // normally wxLogFatalError doesn't return
758 void wxAppConsoleBase::OnAssertFailure(const wxChar
*file
,
765 ShowAssertDialog(file
, line
, func
, cond
, msg
, GetTraits());
767 // this function is still present even in debug level 0 build for ABI
768 // compatibility reasons but is never called there and so can simply do
775 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
778 void wxAppConsoleBase::OnAssert(const wxChar
*file
,
783 OnAssertFailure(file
, line
, NULL
, cond
, msg
);
786 // ============================================================================
787 // other classes implementations
788 // ============================================================================
790 // ----------------------------------------------------------------------------
791 // wxConsoleAppTraitsBase
792 // ----------------------------------------------------------------------------
796 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
798 return new wxLogStderr
;
803 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
805 return new wxMessageOutputStderr
;
810 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
812 return (wxFontMapper
*)new wxFontMapperBase
;
815 #endif // wxUSE_FONTMAP
817 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
819 // console applications don't use renderers
823 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
825 return wxAppTraitsBase::ShowAssertDialog(msg
);
828 bool wxConsoleAppTraitsBase::HasStderr()
830 // console applications always have stderr, even under Mac/Windows
834 // ----------------------------------------------------------------------------
836 // ----------------------------------------------------------------------------
839 void wxAppTraitsBase::SetLocale()
841 wxSetlocale(LC_ALL
, "");
842 wxUpdateLocaleIsUtf8();
847 void wxMutexGuiEnterImpl();
848 void wxMutexGuiLeaveImpl();
850 void wxAppTraitsBase::MutexGuiEnter()
852 wxMutexGuiEnterImpl();
855 void wxAppTraitsBase::MutexGuiLeave()
857 wxMutexGuiLeaveImpl();
860 void WXDLLIMPEXP_BASE
wxMutexGuiEnter()
862 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
864 traits
->MutexGuiEnter();
867 void WXDLLIMPEXP_BASE
wxMutexGuiLeave()
869 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
871 traits
->MutexGuiLeave();
873 #endif // wxUSE_THREADS
875 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msgOriginal
)
880 #if wxUSE_STACKWALKER
881 const wxString stackTrace
= GetAssertStackTrace();
882 if ( !stackTrace
.empty() )
884 msg
<< wxT("\n\nCall stack:\n") << stackTrace
;
886 wxMessageOutputDebug().Output(msg
);
888 #endif // wxUSE_STACKWALKER
890 return DoShowAssertDialog(msgOriginal
+ msg
);
891 #else // !wxDEBUG_LEVEL
892 wxUnusedVar(msgOriginal
);
895 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
898 #if wxUSE_STACKWALKER
899 wxString
wxAppTraitsBase::GetAssertStackTrace()
903 #if !defined(__WXMSW__)
904 // on Unix stack frame generation may take some time, depending on the
905 // size of the executable mainly... warn the user that we are working
906 wxFprintf(stderr
, "Collecting stack trace information, please wait...");
913 class StackDump
: public wxStackWalker
918 const wxString
& GetStackTrace() const { return m_stackTrace
; }
921 virtual void OnStackFrame(const wxStackFrame
& frame
)
923 m_stackTrace
<< wxString::Format
926 wx_truncate_cast(int, frame
.GetLevel())
929 wxString name
= frame
.GetName();
932 m_stackTrace
<< wxString::Format(wxT("%-40s"), name
.c_str());
936 m_stackTrace
<< wxString::Format(wxT("%p"), frame
.GetAddress());
939 if ( frame
.HasSourceLocation() )
941 m_stackTrace
<< wxT('\t')
942 << frame
.GetFileName()
947 m_stackTrace
<< wxT('\n');
951 wxString m_stackTrace
;
954 // don't show more than maxLines or we could get a dialog too tall to be
955 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
956 // characters it is still only 300 pixels...
957 static const int maxLines
= 20;
960 dump
.Walk(2, maxLines
); // don't show OnAssert() call itself
961 stackTrace
= dump
.GetStackTrace();
963 const int count
= stackTrace
.Freq(wxT('\n'));
964 for ( int i
= 0; i
< count
- maxLines
; i
++ )
965 stackTrace
= stackTrace
.BeforeLast(wxT('\n'));
968 #else // !wxDEBUG_LEVEL
969 // this function is still present for ABI-compatibility even in debug level
970 // 0 build but is not used there and so can simply do nothing
972 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
974 #endif // wxUSE_STACKWALKER
977 // ============================================================================
978 // global functions implementation
979 // ============================================================================
989 // what else can we do?
998 wxTheApp
->WakeUpIdle();
1000 //else: do nothing, what can we do?
1003 // wxASSERT() helper
1004 bool wxAssertIsEqual(int x
, int y
)
1011 // break into the debugger
1014 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1016 #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
1018 #elif defined(__UNIX__)
1025 // default assert handler
1027 wxDefaultAssertHandler(const wxString
& file
,
1029 const wxString
& func
,
1030 const wxString
& cond
,
1031 const wxString
& msg
)
1033 // If this option is set, we should abort immediately when assert happens.
1034 if ( wxSystemOptions::GetOptionInt("exit-on-assert") )
1038 static int s_bInAssert
= 0;
1040 wxRecursionGuard
guard(s_bInAssert
);
1041 if ( guard
.IsInside() )
1043 // can't use assert here to avoid infinite loops, so just trap
1051 // by default, show the assert dialog box -- we can't customize this
1053 ShowAssertDialog(file
, line
, func
, cond
, msg
);
1057 // let the app process it as it wants
1058 // FIXME-UTF8: use wc_str(), not c_str(), when ANSI build is removed
1059 wxTheApp
->OnAssertFailure(file
.c_str(), line
, func
.c_str(),
1060 cond
.c_str(), msg
.c_str());
1064 wxAssertHandler_t wxTheAssertHandler
= wxDefaultAssertHandler
;
1066 void wxSetDefaultAssertHandler()
1068 wxTheAssertHandler
= wxDefaultAssertHandler
;
1071 void wxOnAssert(const wxString
& file
,
1073 const wxString
& func
,
1074 const wxString
& cond
,
1075 const wxString
& msg
)
1077 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1080 void wxOnAssert(const wxString
& file
,
1082 const wxString
& func
,
1083 const wxString
& cond
)
1085 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
1088 void wxOnAssert(const wxChar
*file
,
1094 // this is the backwards-compatible version (unless we don't use Unicode)
1095 // so it could be called directly from the user code and this might happen
1096 // even when wxTheAssertHandler is NULL
1098 if ( wxTheAssertHandler
)
1099 #endif // wxUSE_UNICODE
1100 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1103 void wxOnAssert(const char *file
,
1107 const wxString
& msg
)
1109 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1112 void wxOnAssert(const char *file
,
1116 const wxCStrData
& msg
)
1118 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1122 void wxOnAssert(const char *file
,
1127 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
1130 void wxOnAssert(const char *file
,
1136 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1139 void wxOnAssert(const char *file
,
1145 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1147 #endif // wxUSE_UNICODE
1149 #endif // wxDEBUG_LEVEL
1151 // ============================================================================
1152 // private functions implementation
1153 // ============================================================================
1157 static void LINKAGEMODE
SetTraceMasks()
1161 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
1163 wxStringTokenizer
tkn(mask
, wxT(",;:"));
1164 while ( tkn
.HasMoreTokens() )
1165 wxLog::AddTraceMask(tkn
.GetNextToken());
1170 #endif // __WXDEBUG__
1175 bool DoShowAssertDialog(const wxString
& msg
)
1177 // under MSW we can show the dialog even in the console mode
1178 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1179 wxString
msgDlg(msg
);
1181 // this message is intentionally not translated -- it is for developers
1182 // only -- and the less code we use here, less is the danger of recursively
1183 // asserting and dying
1184 msgDlg
+= wxT("\nDo you want to stop the program?\n")
1185 wxT("You can also choose [Cancel] to suppress ")
1186 wxT("further warnings.");
1188 switch ( ::MessageBox(NULL
, msgDlg
.wx_str(), wxT("wxWidgets Debug Alert"),
1189 MB_YESNOCANCEL
| MB_ICONSTOP
) )
1199 //case IDNO: nothing to do
1203 #endif // __WXMSW__/!__WXMSW__
1205 // continue with the asserts by default
1209 // show the standard assert dialog
1211 void ShowAssertDialog(const wxString
& file
,
1213 const wxString
& func
,
1214 const wxString
& cond
,
1215 const wxString
& msgUser
,
1216 wxAppTraits
*traits
)
1218 // this variable can be set to true to suppress "assert failure" messages
1219 static bool s_bNoAsserts
= false;
1224 // make life easier for people using VC++ IDE by using this format: like
1225 // this, clicking on the message will take us immediately to the place of
1226 // the failed assert
1227 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), file
, line
, cond
);
1229 // add the function name, if any
1230 if ( !func
.empty() )
1231 msg
<< wxT(" in ") << func
<< wxT("()");
1233 // and the message itself
1234 if ( !msgUser
.empty() )
1236 msg
<< wxT(": ") << msgUser
;
1238 else // no message given
1244 // if we are not in the main thread, output the assert directly and trap
1245 // since dialogs cannot be displayed
1246 if ( !wxThread::IsMain() )
1248 msg
+= wxString::Format(" [in thread %lx]", wxThread::GetCurrentId());
1250 #endif // wxUSE_THREADS
1252 // log the assert in any case
1253 wxMessageOutputDebug().Output(msg
);
1255 if ( !s_bNoAsserts
)
1259 // delegate showing assert dialog (if possible) to that class
1260 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
1262 else // no traits object
1264 // fall back to the function of last resort
1265 s_bNoAsserts
= DoShowAssertDialog(msg
);
1270 #endif // wxDEBUG_LEVEL