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 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 // this defines wxEventLoopPtr
123 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopBase
)
125 // ============================================================================
126 // wxAppConsoleBase implementation
127 // ============================================================================
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 wxAppConsoleBase::wxAppConsoleBase()
137 m_bDoPendingEventProcessing
= true;
139 ms_appInstance
= static_cast<wxAppConsole
*>(this);
144 // In unicode mode the SetTraceMasks call can cause an apptraits to be
145 // created, but since we are still in the constructor the wrong kind will
146 // be created for GUI apps. Destroy it so it can be created again later.
153 wxAppConsoleBase::~wxAppConsoleBase()
158 // ----------------------------------------------------------------------------
159 // initialization/cleanup
160 // ----------------------------------------------------------------------------
162 bool wxAppConsoleBase::Initialize(int& WXUNUSED(argc
), wxChar
**argv
)
165 GetTraits()->SetLocale();
169 if ( m_appName
.empty() && argv
&& argv
[0] )
171 // the application name is, by default, the name of its executable file
172 wxFileName::SplitPath(argv
[0], NULL
, &m_appName
, NULL
);
174 #endif // !__WXPALMOS__
179 wxEventLoopBase
*wxAppConsoleBase::CreateMainLoop()
181 return GetTraits()->CreateEventLoop();
184 void wxAppConsoleBase::CleanUp()
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
197 bool wxAppConsoleBase::OnInit()
199 #if wxUSE_CMDLINE_PARSER
200 wxCmdLineParser
parser(argc
, argv
);
202 OnInitCmdLine(parser
);
205 switch ( parser
.Parse(false /* don't show usage */) )
208 cont
= OnCmdLineHelp(parser
);
212 cont
= OnCmdLineParsed(parser
);
216 cont
= OnCmdLineError(parser
);
222 #endif // wxUSE_CMDLINE_PARSER
227 int wxAppConsoleBase::OnRun()
232 int wxAppConsoleBase::OnExit()
235 // delete the config object if any (don't use Get() here, but Set()
236 // because Get() could create a new config object)
237 delete wxConfigBase::Set(NULL
);
238 #endif // wxUSE_CONFIG
243 void wxAppConsoleBase::Exit()
245 if (m_mainLoop
!= NULL
)
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 wxAppTraits
*wxAppConsoleBase::CreateTraits()
257 return new wxConsoleAppTraits
;
260 wxAppTraits
*wxAppConsoleBase::GetTraits()
262 // FIXME-MT: protect this with a CS?
265 m_traits
= CreateTraits();
267 wxASSERT_MSG( m_traits
, _T("wxApp::CreateTraits() failed?") );
274 wxAppTraits
*wxAppConsoleBase::GetTraitsIfExists()
276 wxAppConsole
* const app
= GetInstance();
277 return app
? app
->GetTraits() : NULL
;
280 // ----------------------------------------------------------------------------
281 // wxEventLoop redirection
282 // ----------------------------------------------------------------------------
284 int wxAppConsoleBase::MainLoop()
286 wxEventLoopBaseTiedPtr
mainLoop(&m_mainLoop
, CreateMainLoop());
288 return m_mainLoop
? m_mainLoop
->Run() : -1;
291 void wxAppConsoleBase::ExitMainLoop()
293 // we should exit from the main event loop, not just any currently active
294 // (e.g. modal dialog) event loop
295 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
301 bool wxAppConsoleBase::Pending()
303 // use the currently active message loop here, not m_mainLoop, because if
304 // we're showing a modal dialog (with its own event loop) currently the
305 // main event loop is not running anyhow
306 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
308 return loop
&& loop
->Pending();
311 bool wxAppConsoleBase::Dispatch()
313 // see comment in Pending()
314 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
316 return loop
&& loop
->Dispatch();
319 bool wxAppConsoleBase::Yield(bool onlyIfNeeded
)
321 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
323 return loop
&& loop
->Yield(onlyIfNeeded
);
326 void wxAppConsoleBase::WakeUpIdle()
329 m_mainLoop
->WakeUp();
332 bool wxAppConsoleBase::ProcessIdle()
334 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
336 return loop
&& loop
->ProcessIdle();
339 // ----------------------------------------------------------------------------
341 // ----------------------------------------------------------------------------
344 bool wxAppConsoleBase::IsMainLoopRunning()
346 const wxAppConsole
* const app
= GetInstance();
348 return app
&& app
->m_mainLoop
!= NULL
;
351 int wxAppConsoleBase::FilterEvent(wxEvent
& WXUNUSED(event
))
353 // process the events normally by default
357 void wxAppConsoleBase::DelayPendingEventHandler(wxEvtHandler
* toDelay
)
359 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
361 // move the handler from the list of handlers with processable pending events
362 // to the list of handlers with pending events which needs to be processed later
363 m_handlersWithPendingEvents
.Remove(toDelay
);
365 if (m_handlersWithPendingDelayedEvents
.Index(toDelay
) == wxNOT_FOUND
)
366 m_handlersWithPendingDelayedEvents
.Add(toDelay
);
368 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
371 void wxAppConsoleBase::RemovePendingEventHandler(wxEvtHandler
* toRemove
)
373 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
375 if (m_handlersWithPendingEvents
.Index(toRemove
) != wxNOT_FOUND
)
377 m_handlersWithPendingEvents
.Remove(toRemove
);
379 // check that the handler was present only once in the list
380 wxASSERT_MSG( m_handlersWithPendingEvents
.Index(toRemove
) == wxNOT_FOUND
,
381 "Handler occurs twice in the m_handlersWithPendingEvents list!" );
383 //else: it wasn't in this list at all, it's ok
385 if (m_handlersWithPendingDelayedEvents
.Index(toRemove
) != wxNOT_FOUND
)
387 m_handlersWithPendingDelayedEvents
.Remove(toRemove
);
389 // check that the handler was present only once in the list
390 wxASSERT_MSG( m_handlersWithPendingDelayedEvents
.Index(toRemove
) == wxNOT_FOUND
,
391 "Handler occurs twice in m_handlersWithPendingDelayedEvents list!" );
393 //else: it wasn't in this list at all, it's ok
395 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
398 void wxAppConsoleBase::AppendPendingEventHandler(wxEvtHandler
* toAppend
)
400 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
402 if ( m_handlersWithPendingEvents
.Index(toAppend
) == wxNOT_FOUND
)
403 m_handlersWithPendingEvents
.Add(toAppend
);
405 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
408 bool wxAppConsoleBase::HasPendingEvents() const
410 wxENTER_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
412 bool has
= !m_handlersWithPendingEvents
.IsEmpty();
414 wxLEAVE_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
419 void wxAppConsoleBase::SuspendProcessingOfPendingEvents()
421 m_bDoPendingEventProcessing
= false;
424 void wxAppConsoleBase::ResumeProcessingOfPendingEvents()
426 m_bDoPendingEventProcessing
= true;
429 void wxAppConsoleBase::ProcessPendingEvents()
431 if (!m_bDoPendingEventProcessing
)
434 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
436 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
437 "this helper list should be empty" );
439 // iterate until the list becomes empty: the handlers remove themselves
440 // from it when they don't have any more pending events
441 while (!m_handlersWithPendingEvents
.IsEmpty())
443 // In ProcessPendingEvents(), new handlers might be added
444 // and we can safely leave the critical section here.
445 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
447 // NOTE: we always call ProcessPendingEvents() on the first event handler
448 // with pending events because handlers auto-remove themselves
449 // from this list (see RemovePendingEventHandler) if they have no
450 // more pending events.
451 m_handlersWithPendingEvents
[0]->ProcessPendingEvents();
453 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
456 // now the wxHandlersWithPendingEvents is surely empty; however some event
457 // handlers may have moved themselves into wxHandlersWithPendingDelayedEvents
458 // because of a selective wxYield call in progress.
459 // Now we need to move them back to wxHandlersWithPendingEvents so the next
460 // call to this function has the chance of processing them:
461 if (!m_handlersWithPendingDelayedEvents
.IsEmpty())
463 WX_APPEND_ARRAY(m_handlersWithPendingEvents
, m_handlersWithPendingDelayedEvents
);
464 m_handlersWithPendingDelayedEvents
.Clear();
467 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
470 void wxAppConsoleBase::DeletePendingEvents()
472 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
474 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
475 "this helper list should be empty" );
477 for (unsigned int i
=0; i
<m_handlersWithPendingEvents
.GetCount(); i
++)
478 m_handlersWithPendingEvents
[i
]->DeletePendingEvents();
480 m_handlersWithPendingEvents
.Clear();
482 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
485 // ----------------------------------------------------------------------------
486 // exception handling
487 // ----------------------------------------------------------------------------
492 wxAppConsoleBase::HandleEvent(wxEvtHandler
*handler
,
493 wxEventFunction func
,
494 wxEvent
& event
) const
496 // by default, simply call the handler
497 (handler
->*func
)(event
);
500 void wxAppConsoleBase::CallEventHandler(wxEvtHandler
*handler
,
501 wxEventFunctor
& functor
,
502 wxEvent
& event
) const
504 // If the functor holds a method then, for backward compatibility, call
506 wxEventFunction eventFunction
= functor
.GetEvtMethod();
509 HandleEvent(handler
, eventFunction
, event
);
511 functor(handler
, event
);
514 void wxAppConsoleBase::OnUnhandledException()
517 // we're called from an exception handler so we can re-throw the exception
518 // to recover its type
525 catch ( std::exception
& e
)
527 what
.Printf("std::exception of type \"%s\", what() = \"%s\"",
528 typeid(e
).name(), e
.what());
533 what
= "unknown exception";
536 wxMessageOutputBest().Printf(
537 "*** Caught unhandled %s; terminating\n", what
539 #endif // __WXDEBUG__
542 // ----------------------------------------------------------------------------
543 // exceptions support
544 // ----------------------------------------------------------------------------
546 bool wxAppConsoleBase::OnExceptionInMainLoop()
550 // some compilers are too stupid to know that we never return after throw
551 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
556 #endif // wxUSE_EXCEPTIONS
558 // ----------------------------------------------------------------------------
560 // ----------------------------------------------------------------------------
562 #if wxUSE_CMDLINE_PARSER
564 #define OPTION_VERBOSE "verbose"
566 void wxAppConsoleBase::OnInitCmdLine(wxCmdLineParser
& parser
)
568 // the standard command line options
569 static const wxCmdLineEntryDesc cmdLineDesc
[] =
575 gettext_noop("show this help message"),
577 wxCMD_LINE_OPTION_HELP
585 gettext_noop("generate verbose log messages"),
595 parser
.SetDesc(cmdLineDesc
);
598 bool wxAppConsoleBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
601 if ( parser
.Found(OPTION_VERBOSE
) )
603 wxLog::SetVerbose(true);
612 bool wxAppConsoleBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
619 bool wxAppConsoleBase::OnCmdLineError(wxCmdLineParser
& parser
)
626 #endif // wxUSE_CMDLINE_PARSER
628 // ----------------------------------------------------------------------------
630 // ----------------------------------------------------------------------------
633 bool wxAppConsoleBase::CheckBuildOptions(const char *optionsSignature
,
634 const char *componentName
)
636 #if 0 // can't use wxLogTrace, not up and running yet
637 printf("checking build options object '%s' (ptr %p) in '%s'\n",
638 optionsSignature
, optionsSignature
, componentName
);
641 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
643 wxString lib
= wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
644 wxString prog
= wxString::FromAscii(optionsSignature
);
645 wxString progName
= wxString::FromAscii(componentName
);
648 msg
.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
649 lib
.c_str(), progName
.c_str(), prog
.c_str());
651 wxLogFatalError(msg
.c_str());
653 // normally wxLogFatalError doesn't return
660 void wxAppConsoleBase::OnAssertFailure(const wxChar
*file
,
667 ShowAssertDialog(file
, line
, func
, cond
, msg
, GetTraits());
669 // this function is still present even in debug level 0 build for ABI
670 // compatibility reasons but is never called there and so can simply do
677 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
680 void wxAppConsoleBase::OnAssert(const wxChar
*file
,
685 OnAssertFailure(file
, line
, NULL
, cond
, msg
);
688 // ============================================================================
689 // other classes implementations
690 // ============================================================================
692 // ----------------------------------------------------------------------------
693 // wxConsoleAppTraitsBase
694 // ----------------------------------------------------------------------------
698 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
700 return new wxLogStderr
;
705 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
707 return new wxMessageOutputStderr
;
712 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
714 return (wxFontMapper
*)new wxFontMapperBase
;
717 #endif // wxUSE_FONTMAP
719 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
721 // console applications don't use renderers
725 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
727 return wxAppTraitsBase::ShowAssertDialog(msg
);
730 bool wxConsoleAppTraitsBase::HasStderr()
732 // console applications always have stderr, even under Mac/Windows
736 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
741 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
746 // ----------------------------------------------------------------------------
748 // ----------------------------------------------------------------------------
751 void wxAppTraitsBase::SetLocale()
753 wxSetlocale(LC_ALL
, "");
754 wxUpdateLocaleIsUtf8();
759 void wxMutexGuiEnterImpl();
760 void wxMutexGuiLeaveImpl();
762 void wxAppTraitsBase::MutexGuiEnter()
764 wxMutexGuiEnterImpl();
767 void wxAppTraitsBase::MutexGuiLeave()
769 wxMutexGuiLeaveImpl();
772 void WXDLLIMPEXP_BASE
wxMutexGuiEnter()
774 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
776 traits
->MutexGuiEnter();
779 void WXDLLIMPEXP_BASE
wxMutexGuiLeave()
781 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
783 traits
->MutexGuiLeave();
785 #endif // wxUSE_THREADS
787 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msgOriginal
)
790 wxString msg
= msgOriginal
;
792 #if wxUSE_STACKWALKER
793 #if !defined(__WXMSW__)
794 // on Unix stack frame generation may take some time, depending on the
795 // size of the executable mainly... warn the user that we are working
796 wxFprintf(stderr
, wxT("[Debug] Generating a stack trace... please wait"));
800 const wxString stackTrace
= GetAssertStackTrace();
801 if ( !stackTrace
.empty() )
802 msg
<< _T("\n\nCall stack:\n") << stackTrace
;
803 #endif // wxUSE_STACKWALKER
805 return DoShowAssertDialog(msg
);
806 #else // !wxDEBUG_LEVEL
807 wxUnusedVar(msgOriginal
);
810 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
813 #if wxUSE_STACKWALKER
814 wxString
wxAppTraitsBase::GetAssertStackTrace()
819 class StackDump
: public wxStackWalker
824 const wxString
& GetStackTrace() const { return m_stackTrace
; }
827 virtual void OnStackFrame(const wxStackFrame
& frame
)
829 m_stackTrace
<< wxString::Format
832 wx_truncate_cast(int, frame
.GetLevel())
835 wxString name
= frame
.GetName();
838 m_stackTrace
<< wxString::Format(_T("%-40s"), name
.c_str());
842 m_stackTrace
<< wxString::Format(_T("%p"), frame
.GetAddress());
845 if ( frame
.HasSourceLocation() )
847 m_stackTrace
<< _T('\t')
848 << frame
.GetFileName()
853 m_stackTrace
<< _T('\n');
857 wxString m_stackTrace
;
860 // don't show more than maxLines or we could get a dialog too tall to be
861 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
862 // characters it is still only 300 pixels...
863 static const int maxLines
= 20;
866 dump
.Walk(2, maxLines
); // don't show OnAssert() call itself
867 stackTrace
= dump
.GetStackTrace();
869 const int count
= stackTrace
.Freq(wxT('\n'));
870 for ( int i
= 0; i
< count
- maxLines
; i
++ )
871 stackTrace
= stackTrace
.BeforeLast(wxT('\n'));
874 #else // !wxDEBUG_LEVEL
875 // this function is still present for ABI-compatibility even in debug level
876 // 0 build but is not used there and so can simply do nothing
878 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
880 #endif // wxUSE_STACKWALKER
883 // ============================================================================
884 // global functions implementation
885 // ============================================================================
895 // what else can we do?
904 wxTheApp
->WakeUpIdle();
906 //else: do nothing, what can we do?
910 bool wxAssertIsEqual(int x
, int y
)
917 // break into the debugger
920 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
922 #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
924 #elif defined(__UNIX__)
931 // default assert handler
933 wxDefaultAssertHandler(const wxString
& file
,
935 const wxString
& func
,
936 const wxString
& cond
,
940 static int s_bInAssert
= 0;
942 wxRecursionGuard
guard(s_bInAssert
);
943 if ( guard
.IsInside() )
945 // can't use assert here to avoid infinite loops, so just trap
953 // by default, show the assert dialog box -- we can't customize this
955 ShowAssertDialog(file
, line
, func
, cond
, msg
);
959 // let the app process it as it wants
960 // FIXME-UTF8: use wc_str(), not c_str(), when ANSI build is removed
961 wxTheApp
->OnAssertFailure(file
.c_str(), line
, func
.c_str(),
962 cond
.c_str(), msg
.c_str());
966 wxAssertHandler_t wxTheAssertHandler
= wxDefaultAssertHandler
;
968 void wxOnAssert(const wxString
& file
,
970 const wxString
& func
,
971 const wxString
& cond
,
974 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
977 void wxOnAssert(const wxString
& file
,
979 const wxString
& func
,
980 const wxString
& cond
)
982 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
985 void wxOnAssert(const wxChar
*file
,
991 // this is the backwards-compatible version (unless we don't use Unicode)
992 // so it could be called directly from the user code and this might happen
993 // even when wxTheAssertHandler is NULL
995 if ( wxTheAssertHandler
)
996 #endif // wxUSE_UNICODE
997 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1000 void wxOnAssert(const char *file
,
1004 const wxString
& msg
)
1006 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1009 void wxOnAssert(const char *file
,
1013 const wxCStrData
& msg
)
1015 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1019 void wxOnAssert(const char *file
,
1024 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
1027 void wxOnAssert(const char *file
,
1033 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1036 void wxOnAssert(const char *file
,
1042 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1044 #endif // wxUSE_UNICODE
1046 #endif // wxDEBUG_LEVEL
1048 // ============================================================================
1049 // private functions implementation
1050 // ============================================================================
1054 static void LINKAGEMODE
SetTraceMasks()
1058 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
1060 wxStringTokenizer
tkn(mask
, wxT(",;:"));
1061 while ( tkn
.HasMoreTokens() )
1062 wxLog::AddTraceMask(tkn
.GetNextToken());
1067 #endif // __WXDEBUG__
1072 bool DoShowAssertDialog(const wxString
& msg
)
1074 // under MSW we can show the dialog even in the console mode
1075 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1076 wxString
msgDlg(msg
);
1078 // this message is intentionally not translated -- it is for developers
1079 // only -- and the less code we use here, less is the danger of recursively
1080 // asserting and dying
1081 msgDlg
+= wxT("\nDo you want to stop the program?\n")
1082 wxT("You can also choose [Cancel] to suppress ")
1083 wxT("further warnings.");
1085 switch ( ::MessageBox(NULL
, msgDlg
.wx_str(), _T("wxWidgets Debug Alert"),
1086 MB_YESNOCANCEL
| MB_ICONSTOP
) )
1096 //case IDNO: nothing to do
1099 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
1102 // TODO: ask the user to enter "Y" or "N" on the console?
1104 #endif // __WXMSW__/!__WXMSW__
1106 // continue with the asserts
1110 // show the standard assert dialog
1112 void ShowAssertDialog(const wxString
& file
,
1114 const wxString
& func
,
1115 const wxString
& cond
,
1116 const wxString
& msgUser
,
1117 wxAppTraits
*traits
)
1119 // this variable can be set to true to suppress "assert failure" messages
1120 static bool s_bNoAsserts
= false;
1125 // make life easier for people using VC++ IDE by using this format: like
1126 // this, clicking on the message will take us immediately to the place of
1127 // the failed assert
1128 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), file
, line
, cond
);
1130 // add the function name, if any
1131 if ( !func
.empty() )
1132 msg
<< _T(" in ") << func
<< _T("()");
1134 // and the message itself
1135 if ( !msgUser
.empty() )
1137 msg
<< _T(": ") << msgUser
;
1139 else // no message given
1145 // if we are not in the main thread, output the assert directly and trap
1146 // since dialogs cannot be displayed
1147 if ( !wxThread::IsMain() )
1149 msg
+= wxT(" [in child thread]");
1151 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1153 OutputDebugString(msg
.wx_str());
1156 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
1159 // He-e-e-e-elp!! we're asserting in a child thread
1163 #endif // wxUSE_THREADS
1165 if ( !s_bNoAsserts
)
1167 // send it to the normal log destination
1168 wxLogDebug(_T("%s"), msg
.c_str());
1172 // delegate showing assert dialog (if possible) to that class
1173 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
1175 else // no traits object
1177 // fall back to the function of last resort
1178 s_bNoAsserts
= DoShowAssertDialog(msg
);
1183 #endif // wxDEBUG_LEVEL