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 // License: wxWindows license
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/tokenzr.h"
47 #include "wx/thread.h"
49 #if wxUSE_EXCEPTIONS && wxUSE_STL
55 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
56 #include <signal.h> // for SIGTRAP used by wxTrap()
60 #endif // ! __WXPALMOS5__
63 #include "wx/fontmap.h"
64 #endif // wxUSE_FONTMAP
68 #include "wx/stackwalk.h"
70 #include "wx/msw/debughlp.h"
72 #endif // wxUSE_STACKWALKER
74 #include "wx/recguard.h"
75 #endif // wxDEBUG_LEVEL
77 // wxABI_VERSION can be defined when compiling applications but it should be
78 // left undefined when compiling the library itself, it is then set to its
79 // default value in version.h
80 #if wxABI_VERSION != wxMAJOR_VERSION * 10000 + wxMINOR_VERSION * 100 + 99
81 #error "wxABI_VERSION should not be defined when compiling the library"
84 // ----------------------------------------------------------------------------
85 // private functions prototypes
86 // ----------------------------------------------------------------------------
89 // really just show the assert dialog
90 static bool DoShowAssertDialog(const wxString
& msg
);
92 // prepare for showing the assert dialog, use the given traits or
93 // DoShowAssertDialog() as last fallback to really show it
95 void ShowAssertDialog(const wxString
& file
,
100 wxAppTraits
*traits
= NULL
);
101 #endif // wxDEBUG_LEVEL
104 // turn on the trace masks specified in the env variable WXTRACE
105 static void LINKAGEMODE
SetTraceMasks();
106 #endif // __WXDEBUG__
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 wxAppConsole
*wxAppConsoleBase::ms_appInstance
= NULL
;
114 wxAppInitializerFunction
wxAppConsoleBase::ms_appInitFn
= NULL
;
116 wxSocketManager
*wxAppTraitsBase::ms_manager
= NULL
;
118 WXDLLIMPEXP_DATA_BASE(wxList
) wxPendingDelete
;
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 // this defines wxEventLoopPtr
125 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopBase
)
127 // ============================================================================
128 // wxAppConsoleBase implementation
129 // ============================================================================
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 wxAppConsoleBase::wxAppConsoleBase()
139 m_bDoPendingEventProcessing
= true;
141 ms_appInstance
= static_cast<wxAppConsole
*>(this);
146 // In unicode mode the SetTraceMasks call can cause an apptraits to be
147 // created, but since we are still in the constructor the wrong kind will
148 // be created for GUI apps. Destroy it so it can be created again later.
155 wxAppConsoleBase::~wxAppConsoleBase()
160 // ----------------------------------------------------------------------------
161 // initialization/cleanup
162 // ----------------------------------------------------------------------------
164 bool wxAppConsoleBase::Initialize(int& WXUNUSED(argc
), wxChar
**argv
)
167 GetTraits()->SetLocale();
171 if ( m_appName
.empty() && argv
&& argv
[0] )
173 // the application name is, by default, the name of its executable file
174 wxFileName::SplitPath(argv
[0], NULL
, &m_appName
, NULL
);
176 #endif // !__WXPALMOS__
181 wxEventLoopBase
*wxAppConsoleBase::CreateMainLoop()
183 return GetTraits()->CreateEventLoop();
186 void wxAppConsoleBase::CleanUp()
195 // ----------------------------------------------------------------------------
197 // ----------------------------------------------------------------------------
199 bool wxAppConsoleBase::OnInit()
201 #if wxUSE_CMDLINE_PARSER
202 wxCmdLineParser
parser(argc
, argv
);
204 OnInitCmdLine(parser
);
207 switch ( parser
.Parse(false /* don't show usage */) )
210 cont
= OnCmdLineHelp(parser
);
214 cont
= OnCmdLineParsed(parser
);
218 cont
= OnCmdLineError(parser
);
224 #endif // wxUSE_CMDLINE_PARSER
229 int wxAppConsoleBase::OnRun()
234 int wxAppConsoleBase::OnExit()
237 // delete the config object if any (don't use Get() here, but Set()
238 // because Get() could create a new config object)
239 delete wxConfigBase::Set(NULL
);
240 #endif // wxUSE_CONFIG
245 void wxAppConsoleBase::Exit()
247 if (m_mainLoop
!= NULL
)
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
257 wxAppTraits
*wxAppConsoleBase::CreateTraits()
259 return new wxConsoleAppTraits
;
262 wxAppTraits
*wxAppConsoleBase::GetTraits()
264 // FIXME-MT: protect this with a CS?
267 m_traits
= CreateTraits();
269 wxASSERT_MSG( m_traits
, wxT("wxApp::CreateTraits() failed?") );
276 wxAppTraits
*wxAppConsoleBase::GetTraitsIfExists()
278 wxAppConsole
* const app
= GetInstance();
279 return app
? app
->GetTraits() : NULL
;
282 // ----------------------------------------------------------------------------
283 // wxEventLoop redirection
284 // ----------------------------------------------------------------------------
286 int wxAppConsoleBase::MainLoop()
288 wxEventLoopBaseTiedPtr
mainLoop(&m_mainLoop
, CreateMainLoop());
290 return m_mainLoop
? m_mainLoop
->Run() : -1;
293 void wxAppConsoleBase::ExitMainLoop()
295 // we should exit from the main event loop, not just any currently active
296 // (e.g. modal dialog) event loop
297 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
303 bool wxAppConsoleBase::Pending()
305 // use the currently active message loop here, not m_mainLoop, because if
306 // we're showing a modal dialog (with its own event loop) currently the
307 // main event loop is not running anyhow
308 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
310 return loop
&& loop
->Pending();
313 bool wxAppConsoleBase::Dispatch()
315 // see comment in Pending()
316 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
318 return loop
&& loop
->Dispatch();
321 bool wxAppConsoleBase::Yield(bool onlyIfNeeded
)
323 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
325 return loop
&& loop
->Yield(onlyIfNeeded
);
328 void wxAppConsoleBase::WakeUpIdle()
330 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
336 bool wxAppConsoleBase::ProcessIdle()
338 // synthesize an idle event and check if more of them are needed
340 event
.SetEventObject(this);
344 // flush the logged messages if any (do this after processing the events
345 // which could have logged new messages)
346 wxLog::FlushActive();
349 return event
.MoreRequested();
352 bool wxAppConsoleBase::UsesEventLoop() const
354 // in console applications we don't know whether we're going to have an
355 // event loop so assume we won't -- unless we already have one running
356 return wxEventLoopBase::GetActive() != NULL
;
359 // ----------------------------------------------------------------------------
361 // ----------------------------------------------------------------------------
364 bool wxAppConsoleBase::IsMainLoopRunning()
366 const wxAppConsole
* const app
= GetInstance();
368 return app
&& app
->m_mainLoop
!= NULL
;
371 int wxAppConsoleBase::FilterEvent(wxEvent
& WXUNUSED(event
))
373 // process the events normally by default
377 void wxAppConsoleBase::DelayPendingEventHandler(wxEvtHandler
* toDelay
)
379 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
381 // move the handler from the list of handlers with processable pending events
382 // to the list of handlers with pending events which needs to be processed later
383 m_handlersWithPendingEvents
.Remove(toDelay
);
385 if (m_handlersWithPendingDelayedEvents
.Index(toDelay
) == wxNOT_FOUND
)
386 m_handlersWithPendingDelayedEvents
.Add(toDelay
);
388 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
391 void wxAppConsoleBase::RemovePendingEventHandler(wxEvtHandler
* toRemove
)
393 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
395 if (m_handlersWithPendingEvents
.Index(toRemove
) != wxNOT_FOUND
)
397 m_handlersWithPendingEvents
.Remove(toRemove
);
399 // check that the handler was present only once in the list
400 wxASSERT_MSG( m_handlersWithPendingEvents
.Index(toRemove
) == wxNOT_FOUND
,
401 "Handler occurs twice in the m_handlersWithPendingEvents list!" );
403 //else: it wasn't in this list at all, it's ok
405 if (m_handlersWithPendingDelayedEvents
.Index(toRemove
) != wxNOT_FOUND
)
407 m_handlersWithPendingDelayedEvents
.Remove(toRemove
);
409 // check that the handler was present only once in the list
410 wxASSERT_MSG( m_handlersWithPendingDelayedEvents
.Index(toRemove
) == wxNOT_FOUND
,
411 "Handler occurs twice in m_handlersWithPendingDelayedEvents list!" );
413 //else: it wasn't in this list at all, it's ok
415 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
418 void wxAppConsoleBase::AppendPendingEventHandler(wxEvtHandler
* toAppend
)
420 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
422 if ( m_handlersWithPendingEvents
.Index(toAppend
) == wxNOT_FOUND
)
423 m_handlersWithPendingEvents
.Add(toAppend
);
425 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
428 bool wxAppConsoleBase::HasPendingEvents() const
430 wxENTER_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
432 bool has
= !m_handlersWithPendingEvents
.IsEmpty();
434 wxLEAVE_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
439 void wxAppConsoleBase::SuspendProcessingOfPendingEvents()
441 m_bDoPendingEventProcessing
= false;
444 void wxAppConsoleBase::ResumeProcessingOfPendingEvents()
446 m_bDoPendingEventProcessing
= true;
449 void wxAppConsoleBase::ProcessPendingEvents()
451 if ( m_bDoPendingEventProcessing
)
453 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
455 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
456 "this helper list should be empty" );
458 // iterate until the list becomes empty: the handlers remove themselves
459 // from it when they don't have any more pending events
460 while (!m_handlersWithPendingEvents
.IsEmpty())
462 // In ProcessPendingEvents(), new handlers might be added
463 // and we can safely leave the critical section here.
464 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
466 // NOTE: we always call ProcessPendingEvents() on the first event handler
467 // with pending events because handlers auto-remove themselves
468 // from this list (see RemovePendingEventHandler) if they have no
469 // more pending events.
470 m_handlersWithPendingEvents
[0]->ProcessPendingEvents();
472 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
475 // now the wxHandlersWithPendingEvents is surely empty; however some event
476 // handlers may have moved themselves into wxHandlersWithPendingDelayedEvents
477 // because of a selective wxYield call in progress.
478 // Now we need to move them back to wxHandlersWithPendingEvents so the next
479 // call to this function has the chance of processing them:
480 if (!m_handlersWithPendingDelayedEvents
.IsEmpty())
482 WX_APPEND_ARRAY(m_handlersWithPendingEvents
, m_handlersWithPendingDelayedEvents
);
483 m_handlersWithPendingDelayedEvents
.Clear();
486 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
489 // Garbage collect all objects previously scheduled for destruction.
490 DeletePendingObjects();
493 void wxAppConsoleBase::DeletePendingEvents()
495 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
497 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
498 "this helper list should be empty" );
500 for (unsigned int i
=0; i
<m_handlersWithPendingEvents
.GetCount(); i
++)
501 m_handlersWithPendingEvents
[i
]->DeletePendingEvents();
503 m_handlersWithPendingEvents
.Clear();
505 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
508 // ----------------------------------------------------------------------------
509 // delayed objects destruction
510 // ----------------------------------------------------------------------------
512 bool wxAppConsoleBase::IsScheduledForDestruction(wxObject
*object
) const
514 return wxPendingDelete
.Member(object
) != NULL
;
517 void wxAppConsoleBase::ScheduleForDestruction(wxObject
*object
)
519 if ( !UsesEventLoop() )
521 // we won't be able to delete it later so do it right now
525 //else: we either already have or will soon start an event loop
527 if ( !wxPendingDelete
.Member(object
) )
528 wxPendingDelete
.Append(object
);
531 void wxAppConsoleBase::DeletePendingObjects()
533 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
536 wxObject
*obj
= node
->GetData();
538 // remove it from the list first so that if we get back here somehow
539 // during the object deletion (e.g. wxYield called from its dtor) we
540 // wouldn't try to delete it the second time
541 if ( wxPendingDelete
.Member(obj
) )
542 wxPendingDelete
.Erase(node
);
546 // Deleting one object may have deleted other pending
547 // objects, so start from beginning of list again.
548 node
= wxPendingDelete
.GetFirst();
552 // ----------------------------------------------------------------------------
553 // exception handling
554 // ----------------------------------------------------------------------------
559 wxAppConsoleBase::HandleEvent(wxEvtHandler
*handler
,
560 wxEventFunction func
,
561 wxEvent
& event
) const
563 // by default, simply call the handler
564 (handler
->*func
)(event
);
567 void wxAppConsoleBase::CallEventHandler(wxEvtHandler
*handler
,
568 wxEventFunctor
& functor
,
569 wxEvent
& event
) const
571 // If the functor holds a method then, for backward compatibility, call
573 wxEventFunction eventFunction
= functor
.GetEvtMethod();
576 HandleEvent(handler
, eventFunction
, event
);
578 functor(handler
, event
);
581 void wxAppConsoleBase::OnUnhandledException()
584 // we're called from an exception handler so we can re-throw the exception
585 // to recover its type
592 catch ( std::exception
& e
)
594 what
.Printf("std::exception of type \"%s\", what() = \"%s\"",
595 typeid(e
).name(), e
.what());
600 what
= "unknown exception";
603 wxMessageOutputBest().Printf(
604 "*** Caught unhandled %s; terminating\n", what
606 #endif // __WXDEBUG__
609 // ----------------------------------------------------------------------------
610 // exceptions support
611 // ----------------------------------------------------------------------------
613 bool wxAppConsoleBase::OnExceptionInMainLoop()
617 // some compilers are too stupid to know that we never return after throw
618 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
623 #endif // wxUSE_EXCEPTIONS
625 // ----------------------------------------------------------------------------
627 // ----------------------------------------------------------------------------
629 #if wxUSE_CMDLINE_PARSER
631 #define OPTION_VERBOSE "verbose"
633 void wxAppConsoleBase::OnInitCmdLine(wxCmdLineParser
& parser
)
635 // the standard command line options
636 static const wxCmdLineEntryDesc cmdLineDesc
[] =
642 gettext_noop("show this help message"),
644 wxCMD_LINE_OPTION_HELP
652 gettext_noop("generate verbose log messages"),
662 parser
.SetDesc(cmdLineDesc
);
665 bool wxAppConsoleBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
668 if ( parser
.Found(OPTION_VERBOSE
) )
670 wxLog::SetVerbose(true);
679 bool wxAppConsoleBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
686 bool wxAppConsoleBase::OnCmdLineError(wxCmdLineParser
& parser
)
693 #endif // wxUSE_CMDLINE_PARSER
695 // ----------------------------------------------------------------------------
697 // ----------------------------------------------------------------------------
700 bool wxAppConsoleBase::CheckBuildOptions(const char *optionsSignature
,
701 const char *componentName
)
703 #if 0 // can't use wxLogTrace, not up and running yet
704 printf("checking build options object '%s' (ptr %p) in '%s'\n",
705 optionsSignature
, optionsSignature
, componentName
);
708 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
710 wxString lib
= wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
711 wxString prog
= wxString::FromAscii(optionsSignature
);
712 wxString progName
= wxString::FromAscii(componentName
);
715 msg
.Printf(wxT("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
716 lib
.c_str(), progName
.c_str(), prog
.c_str());
718 wxLogFatalError(msg
.c_str());
720 // normally wxLogFatalError doesn't return
727 void wxAppConsoleBase::OnAssertFailure(const wxChar
*file
,
734 ShowAssertDialog(file
, line
, func
, cond
, msg
, GetTraits());
736 // this function is still present even in debug level 0 build for ABI
737 // compatibility reasons but is never called there and so can simply do
744 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
747 void wxAppConsoleBase::OnAssert(const wxChar
*file
,
752 OnAssertFailure(file
, line
, NULL
, cond
, msg
);
755 // ============================================================================
756 // other classes implementations
757 // ============================================================================
759 // ----------------------------------------------------------------------------
760 // wxConsoleAppTraitsBase
761 // ----------------------------------------------------------------------------
765 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
767 return new wxLogStderr
;
772 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
774 return new wxMessageOutputStderr
;
779 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
781 return (wxFontMapper
*)new wxFontMapperBase
;
784 #endif // wxUSE_FONTMAP
786 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
788 // console applications don't use renderers
792 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
794 return wxAppTraitsBase::ShowAssertDialog(msg
);
797 bool wxConsoleAppTraitsBase::HasStderr()
799 // console applications always have stderr, even under Mac/Windows
803 // ----------------------------------------------------------------------------
805 // ----------------------------------------------------------------------------
808 void wxAppTraitsBase::SetLocale()
810 wxSetlocale(LC_ALL
, "");
811 wxUpdateLocaleIsUtf8();
816 void wxMutexGuiEnterImpl();
817 void wxMutexGuiLeaveImpl();
819 void wxAppTraitsBase::MutexGuiEnter()
821 wxMutexGuiEnterImpl();
824 void wxAppTraitsBase::MutexGuiLeave()
826 wxMutexGuiLeaveImpl();
829 void WXDLLIMPEXP_BASE
wxMutexGuiEnter()
831 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
833 traits
->MutexGuiEnter();
836 void WXDLLIMPEXP_BASE
wxMutexGuiLeave()
838 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
840 traits
->MutexGuiLeave();
842 #endif // wxUSE_THREADS
844 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msgOriginal
)
847 wxString msg
= msgOriginal
;
849 #if wxUSE_STACKWALKER
850 #if !defined(__WXMSW__)
851 // on Unix stack frame generation may take some time, depending on the
852 // size of the executable mainly... warn the user that we are working
853 wxFprintf(stderr
, wxT("[Debug] Generating a stack trace... please wait"));
857 const wxString stackTrace
= GetAssertStackTrace();
858 if ( !stackTrace
.empty() )
859 msg
<< wxT("\n\nCall stack:\n") << stackTrace
;
860 #endif // wxUSE_STACKWALKER
862 return DoShowAssertDialog(msg
);
863 #else // !wxDEBUG_LEVEL
864 wxUnusedVar(msgOriginal
);
867 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
870 #if wxUSE_STACKWALKER
871 wxString
wxAppTraitsBase::GetAssertStackTrace()
876 class StackDump
: public wxStackWalker
881 const wxString
& GetStackTrace() const { return m_stackTrace
; }
884 virtual void OnStackFrame(const wxStackFrame
& frame
)
886 m_stackTrace
<< wxString::Format
889 wx_truncate_cast(int, frame
.GetLevel())
892 wxString name
= frame
.GetName();
895 m_stackTrace
<< wxString::Format(wxT("%-40s"), name
.c_str());
899 m_stackTrace
<< wxString::Format(wxT("%p"), frame
.GetAddress());
902 if ( frame
.HasSourceLocation() )
904 m_stackTrace
<< wxT('\t')
905 << frame
.GetFileName()
910 m_stackTrace
<< wxT('\n');
914 wxString m_stackTrace
;
917 // don't show more than maxLines or we could get a dialog too tall to be
918 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
919 // characters it is still only 300 pixels...
920 static const int maxLines
= 20;
923 dump
.Walk(2, maxLines
); // don't show OnAssert() call itself
924 stackTrace
= dump
.GetStackTrace();
926 const int count
= stackTrace
.Freq(wxT('\n'));
927 for ( int i
= 0; i
< count
- maxLines
; i
++ )
928 stackTrace
= stackTrace
.BeforeLast(wxT('\n'));
931 #else // !wxDEBUG_LEVEL
932 // this function is still present for ABI-compatibility even in debug level
933 // 0 build but is not used there and so can simply do nothing
935 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
937 #endif // wxUSE_STACKWALKER
940 // ============================================================================
941 // global functions implementation
942 // ============================================================================
952 // what else can we do?
961 wxTheApp
->WakeUpIdle();
963 //else: do nothing, what can we do?
967 bool wxAssertIsEqual(int x
, int y
)
974 // break into the debugger
977 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
979 #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
981 #elif defined(__UNIX__)
988 // default assert handler
990 wxDefaultAssertHandler(const wxString
& file
,
992 const wxString
& func
,
993 const wxString
& cond
,
997 static int s_bInAssert
= 0;
999 wxRecursionGuard
guard(s_bInAssert
);
1000 if ( guard
.IsInside() )
1002 // can't use assert here to avoid infinite loops, so just trap
1010 // by default, show the assert dialog box -- we can't customize this
1012 ShowAssertDialog(file
, line
, func
, cond
, msg
);
1016 // let the app process it as it wants
1017 // FIXME-UTF8: use wc_str(), not c_str(), when ANSI build is removed
1018 wxTheApp
->OnAssertFailure(file
.c_str(), line
, func
.c_str(),
1019 cond
.c_str(), msg
.c_str());
1023 wxAssertHandler_t wxTheAssertHandler
= wxDefaultAssertHandler
;
1025 void wxOnAssert(const wxString
& file
,
1027 const wxString
& func
,
1028 const wxString
& cond
,
1029 const wxString
& msg
)
1031 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1034 void wxOnAssert(const wxString
& file
,
1036 const wxString
& func
,
1037 const wxString
& cond
)
1039 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
1042 void wxOnAssert(const wxChar
*file
,
1048 // this is the backwards-compatible version (unless we don't use Unicode)
1049 // so it could be called directly from the user code and this might happen
1050 // even when wxTheAssertHandler is NULL
1052 if ( wxTheAssertHandler
)
1053 #endif // wxUSE_UNICODE
1054 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1057 void wxOnAssert(const char *file
,
1061 const wxString
& msg
)
1063 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1066 void wxOnAssert(const char *file
,
1070 const wxCStrData
& msg
)
1072 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1076 void wxOnAssert(const char *file
,
1081 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
1084 void wxOnAssert(const char *file
,
1090 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1093 void wxOnAssert(const char *file
,
1099 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1101 #endif // wxUSE_UNICODE
1103 #endif // wxDEBUG_LEVEL
1105 // ============================================================================
1106 // private functions implementation
1107 // ============================================================================
1111 static void LINKAGEMODE
SetTraceMasks()
1115 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
1117 wxStringTokenizer
tkn(mask
, wxT(",;:"));
1118 while ( tkn
.HasMoreTokens() )
1119 wxLog::AddTraceMask(tkn
.GetNextToken());
1124 #endif // __WXDEBUG__
1129 bool DoShowAssertDialog(const wxString
& msg
)
1131 // under MSW we can show the dialog even in the console mode
1132 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1133 wxString
msgDlg(msg
);
1135 // this message is intentionally not translated -- it is for developers
1136 // only -- and the less code we use here, less is the danger of recursively
1137 // asserting and dying
1138 msgDlg
+= wxT("\nDo you want to stop the program?\n")
1139 wxT("You can also choose [Cancel] to suppress ")
1140 wxT("further warnings.");
1142 switch ( ::MessageBox(NULL
, msgDlg
.wx_str(), wxT("wxWidgets Debug Alert"),
1143 MB_YESNOCANCEL
| MB_ICONSTOP
) )
1153 //case IDNO: nothing to do
1156 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
1159 // TODO: ask the user to enter "Y" or "N" on the console?
1161 #endif // __WXMSW__/!__WXMSW__
1163 // continue with the asserts
1167 // show the standard assert dialog
1169 void ShowAssertDialog(const wxString
& file
,
1171 const wxString
& func
,
1172 const wxString
& cond
,
1173 const wxString
& msgUser
,
1174 wxAppTraits
*traits
)
1176 // this variable can be set to true to suppress "assert failure" messages
1177 static bool s_bNoAsserts
= false;
1182 // make life easier for people using VC++ IDE by using this format: like
1183 // this, clicking on the message will take us immediately to the place of
1184 // the failed assert
1185 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), file
, line
, cond
);
1187 // add the function name, if any
1188 if ( !func
.empty() )
1189 msg
<< wxT(" in ") << func
<< wxT("()");
1191 // and the message itself
1192 if ( !msgUser
.empty() )
1194 msg
<< wxT(": ") << msgUser
;
1196 else // no message given
1202 // if we are not in the main thread, output the assert directly and trap
1203 // since dialogs cannot be displayed
1204 if ( !wxThread::IsMain() )
1206 msg
+= wxT(" [in child thread]");
1208 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1210 OutputDebugString(msg
.wx_str());
1213 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
1216 // He-e-e-e-elp!! we're asserting in a child thread
1220 #endif // wxUSE_THREADS
1222 if ( !s_bNoAsserts
)
1224 // send it to the normal log destination
1225 wxLogDebug(wxT("%s"), msg
.c_str());
1229 // delegate showing assert dialog (if possible) to that class
1230 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
1232 else // no traits object
1234 // fall back to the function of last resort
1235 s_bNoAsserts
= DoShowAssertDialog(msg
);
1240 #endif // wxDEBUG_LEVEL