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()
328 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
334 bool wxAppConsoleBase::ProcessIdle()
336 // synthesize an idle event and check if more of them are needed
338 event
.SetEventObject(this);
341 return event
.MoreRequested();
344 // ----------------------------------------------------------------------------
346 // ----------------------------------------------------------------------------
349 bool wxAppConsoleBase::IsMainLoopRunning()
351 const wxAppConsole
* const app
= GetInstance();
353 return app
&& app
->m_mainLoop
!= NULL
;
356 int wxAppConsoleBase::FilterEvent(wxEvent
& WXUNUSED(event
))
358 // process the events normally by default
362 void wxAppConsoleBase::DelayPendingEventHandler(wxEvtHandler
* toDelay
)
364 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
366 // move the handler from the list of handlers with processable pending events
367 // to the list of handlers with pending events which needs to be processed later
368 m_handlersWithPendingEvents
.Remove(toDelay
);
370 if (m_handlersWithPendingDelayedEvents
.Index(toDelay
) == wxNOT_FOUND
)
371 m_handlersWithPendingDelayedEvents
.Add(toDelay
);
373 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
376 void wxAppConsoleBase::RemovePendingEventHandler(wxEvtHandler
* toRemove
)
378 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
380 if (m_handlersWithPendingEvents
.Index(toRemove
) != wxNOT_FOUND
)
382 m_handlersWithPendingEvents
.Remove(toRemove
);
384 // check that the handler was present only once in the list
385 wxASSERT_MSG( m_handlersWithPendingEvents
.Index(toRemove
) == wxNOT_FOUND
,
386 "Handler occurs twice in the m_handlersWithPendingEvents list!" );
388 //else: it wasn't in this list at all, it's ok
390 if (m_handlersWithPendingDelayedEvents
.Index(toRemove
) != wxNOT_FOUND
)
392 m_handlersWithPendingDelayedEvents
.Remove(toRemove
);
394 // check that the handler was present only once in the list
395 wxASSERT_MSG( m_handlersWithPendingDelayedEvents
.Index(toRemove
) == wxNOT_FOUND
,
396 "Handler occurs twice in m_handlersWithPendingDelayedEvents list!" );
398 //else: it wasn't in this list at all, it's ok
400 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
403 void wxAppConsoleBase::AppendPendingEventHandler(wxEvtHandler
* toAppend
)
405 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
407 if ( m_handlersWithPendingEvents
.Index(toAppend
) == wxNOT_FOUND
)
408 m_handlersWithPendingEvents
.Add(toAppend
);
410 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
413 bool wxAppConsoleBase::HasPendingEvents() const
415 wxENTER_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
417 bool has
= !m_handlersWithPendingEvents
.IsEmpty();
419 wxLEAVE_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
424 void wxAppConsoleBase::SuspendProcessingOfPendingEvents()
426 m_bDoPendingEventProcessing
= false;
429 void wxAppConsoleBase::ResumeProcessingOfPendingEvents()
431 m_bDoPendingEventProcessing
= true;
434 void wxAppConsoleBase::ProcessPendingEvents()
436 if (!m_bDoPendingEventProcessing
)
439 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
441 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
442 "this helper list should be empty" );
444 // iterate until the list becomes empty: the handlers remove themselves
445 // from it when they don't have any more pending events
446 while (!m_handlersWithPendingEvents
.IsEmpty())
448 // In ProcessPendingEvents(), new handlers might be added
449 // and we can safely leave the critical section here.
450 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
452 // NOTE: we always call ProcessPendingEvents() on the first event handler
453 // with pending events because handlers auto-remove themselves
454 // from this list (see RemovePendingEventHandler) if they have no
455 // more pending events.
456 m_handlersWithPendingEvents
[0]->ProcessPendingEvents();
458 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
461 // now the wxHandlersWithPendingEvents is surely empty; however some event
462 // handlers may have moved themselves into wxHandlersWithPendingDelayedEvents
463 // because of a selective wxYield call in progress.
464 // Now we need to move them back to wxHandlersWithPendingEvents so the next
465 // call to this function has the chance of processing them:
466 if (!m_handlersWithPendingDelayedEvents
.IsEmpty())
468 WX_APPEND_ARRAY(m_handlersWithPendingEvents
, m_handlersWithPendingDelayedEvents
);
469 m_handlersWithPendingDelayedEvents
.Clear();
472 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
475 void wxAppConsoleBase::DeletePendingEvents()
477 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
479 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
480 "this helper list should be empty" );
482 for (unsigned int i
=0; i
<m_handlersWithPendingEvents
.GetCount(); i
++)
483 m_handlersWithPendingEvents
[i
]->DeletePendingEvents();
485 m_handlersWithPendingEvents
.Clear();
487 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
490 // ----------------------------------------------------------------------------
491 // exception handling
492 // ----------------------------------------------------------------------------
497 wxAppConsoleBase::HandleEvent(wxEvtHandler
*handler
,
498 wxEventFunction func
,
499 wxEvent
& event
) const
501 // by default, simply call the handler
502 (handler
->*func
)(event
);
505 void wxAppConsoleBase::CallEventHandler(wxEvtHandler
*handler
,
506 wxEventFunctor
& functor
,
507 wxEvent
& event
) const
509 // If the functor holds a method then, for backward compatibility, call
511 wxEventFunction eventFunction
= functor
.GetEvtMethod();
514 HandleEvent(handler
, eventFunction
, event
);
516 functor(handler
, event
);
519 void wxAppConsoleBase::OnUnhandledException()
522 // we're called from an exception handler so we can re-throw the exception
523 // to recover its type
530 catch ( std::exception
& e
)
532 what
.Printf("std::exception of type \"%s\", what() = \"%s\"",
533 typeid(e
).name(), e
.what());
538 what
= "unknown exception";
541 wxMessageOutputBest().Printf(
542 "*** Caught unhandled %s; terminating\n", what
544 #endif // __WXDEBUG__
547 // ----------------------------------------------------------------------------
548 // exceptions support
549 // ----------------------------------------------------------------------------
551 bool wxAppConsoleBase::OnExceptionInMainLoop()
555 // some compilers are too stupid to know that we never return after throw
556 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
561 #endif // wxUSE_EXCEPTIONS
563 // ----------------------------------------------------------------------------
565 // ----------------------------------------------------------------------------
567 #if wxUSE_CMDLINE_PARSER
569 #define OPTION_VERBOSE "verbose"
571 void wxAppConsoleBase::OnInitCmdLine(wxCmdLineParser
& parser
)
573 // the standard command line options
574 static const wxCmdLineEntryDesc cmdLineDesc
[] =
580 gettext_noop("show this help message"),
582 wxCMD_LINE_OPTION_HELP
590 gettext_noop("generate verbose log messages"),
600 parser
.SetDesc(cmdLineDesc
);
603 bool wxAppConsoleBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
606 if ( parser
.Found(OPTION_VERBOSE
) )
608 wxLog::SetVerbose(true);
617 bool wxAppConsoleBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
624 bool wxAppConsoleBase::OnCmdLineError(wxCmdLineParser
& parser
)
631 #endif // wxUSE_CMDLINE_PARSER
633 // ----------------------------------------------------------------------------
635 // ----------------------------------------------------------------------------
638 bool wxAppConsoleBase::CheckBuildOptions(const char *optionsSignature
,
639 const char *componentName
)
641 #if 0 // can't use wxLogTrace, not up and running yet
642 printf("checking build options object '%s' (ptr %p) in '%s'\n",
643 optionsSignature
, optionsSignature
, componentName
);
646 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
648 wxString lib
= wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
649 wxString prog
= wxString::FromAscii(optionsSignature
);
650 wxString progName
= wxString::FromAscii(componentName
);
653 msg
.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
654 lib
.c_str(), progName
.c_str(), prog
.c_str());
656 wxLogFatalError(msg
.c_str());
658 // normally wxLogFatalError doesn't return
665 void wxAppConsoleBase::OnAssertFailure(const wxChar
*file
,
672 ShowAssertDialog(file
, line
, func
, cond
, msg
, GetTraits());
674 // this function is still present even in debug level 0 build for ABI
675 // compatibility reasons but is never called there and so can simply do
682 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
685 void wxAppConsoleBase::OnAssert(const wxChar
*file
,
690 OnAssertFailure(file
, line
, NULL
, cond
, msg
);
693 // ============================================================================
694 // other classes implementations
695 // ============================================================================
697 // ----------------------------------------------------------------------------
698 // wxConsoleAppTraitsBase
699 // ----------------------------------------------------------------------------
703 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
705 return new wxLogStderr
;
710 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
712 return new wxMessageOutputStderr
;
717 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
719 return (wxFontMapper
*)new wxFontMapperBase
;
722 #endif // wxUSE_FONTMAP
724 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
726 // console applications don't use renderers
730 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
732 return wxAppTraitsBase::ShowAssertDialog(msg
);
735 bool wxConsoleAppTraitsBase::HasStderr()
737 // console applications always have stderr, even under Mac/Windows
741 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
746 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
751 // ----------------------------------------------------------------------------
753 // ----------------------------------------------------------------------------
756 void wxAppTraitsBase::SetLocale()
758 wxSetlocale(LC_ALL
, "");
759 wxUpdateLocaleIsUtf8();
764 void wxMutexGuiEnterImpl();
765 void wxMutexGuiLeaveImpl();
767 void wxAppTraitsBase::MutexGuiEnter()
769 wxMutexGuiEnterImpl();
772 void wxAppTraitsBase::MutexGuiLeave()
774 wxMutexGuiLeaveImpl();
777 void WXDLLIMPEXP_BASE
wxMutexGuiEnter()
779 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
781 traits
->MutexGuiEnter();
784 void WXDLLIMPEXP_BASE
wxMutexGuiLeave()
786 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
788 traits
->MutexGuiLeave();
790 #endif // wxUSE_THREADS
792 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msgOriginal
)
795 wxString msg
= msgOriginal
;
797 #if wxUSE_STACKWALKER
798 #if !defined(__WXMSW__)
799 // on Unix stack frame generation may take some time, depending on the
800 // size of the executable mainly... warn the user that we are working
801 wxFprintf(stderr
, wxT("[Debug] Generating a stack trace... please wait"));
805 const wxString stackTrace
= GetAssertStackTrace();
806 if ( !stackTrace
.empty() )
807 msg
<< _T("\n\nCall stack:\n") << stackTrace
;
808 #endif // wxUSE_STACKWALKER
810 return DoShowAssertDialog(msg
);
811 #else // !wxDEBUG_LEVEL
812 wxUnusedVar(msgOriginal
);
815 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
818 #if wxUSE_STACKWALKER
819 wxString
wxAppTraitsBase::GetAssertStackTrace()
824 class StackDump
: public wxStackWalker
829 const wxString
& GetStackTrace() const { return m_stackTrace
; }
832 virtual void OnStackFrame(const wxStackFrame
& frame
)
834 m_stackTrace
<< wxString::Format
837 wx_truncate_cast(int, frame
.GetLevel())
840 wxString name
= frame
.GetName();
843 m_stackTrace
<< wxString::Format(_T("%-40s"), name
.c_str());
847 m_stackTrace
<< wxString::Format(_T("%p"), frame
.GetAddress());
850 if ( frame
.HasSourceLocation() )
852 m_stackTrace
<< _T('\t')
853 << frame
.GetFileName()
858 m_stackTrace
<< _T('\n');
862 wxString m_stackTrace
;
865 // don't show more than maxLines or we could get a dialog too tall to be
866 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
867 // characters it is still only 300 pixels...
868 static const int maxLines
= 20;
871 dump
.Walk(2, maxLines
); // don't show OnAssert() call itself
872 stackTrace
= dump
.GetStackTrace();
874 const int count
= stackTrace
.Freq(wxT('\n'));
875 for ( int i
= 0; i
< count
- maxLines
; i
++ )
876 stackTrace
= stackTrace
.BeforeLast(wxT('\n'));
879 #else // !wxDEBUG_LEVEL
880 // this function is still present for ABI-compatibility even in debug level
881 // 0 build but is not used there and so can simply do nothing
883 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
885 #endif // wxUSE_STACKWALKER
888 // ============================================================================
889 // global functions implementation
890 // ============================================================================
900 // what else can we do?
909 wxTheApp
->WakeUpIdle();
911 //else: do nothing, what can we do?
915 bool wxAssertIsEqual(int x
, int y
)
922 // break into the debugger
925 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
927 #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
929 #elif defined(__UNIX__)
936 // default assert handler
938 wxDefaultAssertHandler(const wxString
& file
,
940 const wxString
& func
,
941 const wxString
& cond
,
945 static int s_bInAssert
= 0;
947 wxRecursionGuard
guard(s_bInAssert
);
948 if ( guard
.IsInside() )
950 // can't use assert here to avoid infinite loops, so just trap
958 // by default, show the assert dialog box -- we can't customize this
960 ShowAssertDialog(file
, line
, func
, cond
, msg
);
964 // let the app process it as it wants
965 // FIXME-UTF8: use wc_str(), not c_str(), when ANSI build is removed
966 wxTheApp
->OnAssertFailure(file
.c_str(), line
, func
.c_str(),
967 cond
.c_str(), msg
.c_str());
971 wxAssertHandler_t wxTheAssertHandler
= wxDefaultAssertHandler
;
973 void wxOnAssert(const wxString
& file
,
975 const wxString
& func
,
976 const wxString
& cond
,
979 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
982 void wxOnAssert(const wxString
& file
,
984 const wxString
& func
,
985 const wxString
& cond
)
987 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
990 void wxOnAssert(const wxChar
*file
,
996 // this is the backwards-compatible version (unless we don't use Unicode)
997 // so it could be called directly from the user code and this might happen
998 // even when wxTheAssertHandler is NULL
1000 if ( wxTheAssertHandler
)
1001 #endif // wxUSE_UNICODE
1002 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1005 void wxOnAssert(const char *file
,
1009 const wxString
& msg
)
1011 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1014 void wxOnAssert(const char *file
,
1018 const wxCStrData
& msg
)
1020 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1024 void wxOnAssert(const char *file
,
1029 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
1032 void wxOnAssert(const char *file
,
1038 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1041 void wxOnAssert(const char *file
,
1047 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1049 #endif // wxUSE_UNICODE
1051 #endif // wxDEBUG_LEVEL
1053 // ============================================================================
1054 // private functions implementation
1055 // ============================================================================
1059 static void LINKAGEMODE
SetTraceMasks()
1063 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
1065 wxStringTokenizer
tkn(mask
, wxT(",;:"));
1066 while ( tkn
.HasMoreTokens() )
1067 wxLog::AddTraceMask(tkn
.GetNextToken());
1072 #endif // __WXDEBUG__
1077 bool DoShowAssertDialog(const wxString
& msg
)
1079 // under MSW we can show the dialog even in the console mode
1080 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1081 wxString
msgDlg(msg
);
1083 // this message is intentionally not translated -- it is for developers
1084 // only -- and the less code we use here, less is the danger of recursively
1085 // asserting and dying
1086 msgDlg
+= wxT("\nDo you want to stop the program?\n")
1087 wxT("You can also choose [Cancel] to suppress ")
1088 wxT("further warnings.");
1090 switch ( ::MessageBox(NULL
, msgDlg
.wx_str(), _T("wxWidgets Debug Alert"),
1091 MB_YESNOCANCEL
| MB_ICONSTOP
) )
1101 //case IDNO: nothing to do
1104 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
1107 // TODO: ask the user to enter "Y" or "N" on the console?
1109 #endif // __WXMSW__/!__WXMSW__
1111 // continue with the asserts
1115 // show the standard assert dialog
1117 void ShowAssertDialog(const wxString
& file
,
1119 const wxString
& func
,
1120 const wxString
& cond
,
1121 const wxString
& msgUser
,
1122 wxAppTraits
*traits
)
1124 // this variable can be set to true to suppress "assert failure" messages
1125 static bool s_bNoAsserts
= false;
1130 // make life easier for people using VC++ IDE by using this format: like
1131 // this, clicking on the message will take us immediately to the place of
1132 // the failed assert
1133 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), file
, line
, cond
);
1135 // add the function name, if any
1136 if ( !func
.empty() )
1137 msg
<< _T(" in ") << func
<< _T("()");
1139 // and the message itself
1140 if ( !msgUser
.empty() )
1142 msg
<< _T(": ") << msgUser
;
1144 else // no message given
1150 // if we are not in the main thread, output the assert directly and trap
1151 // since dialogs cannot be displayed
1152 if ( !wxThread::IsMain() )
1154 msg
+= wxT(" [in child thread]");
1156 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1158 OutputDebugString(msg
.wx_str());
1161 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
1164 // He-e-e-e-elp!! we're asserting in a child thread
1168 #endif // wxUSE_THREADS
1170 if ( !s_bNoAsserts
)
1172 // send it to the normal log destination
1173 wxLogDebug(_T("%s"), msg
.c_str());
1177 // delegate showing assert dialog (if possible) to that class
1178 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
1180 else // no traits object
1182 // fall back to the function of last resort
1183 s_bNoAsserts
= DoShowAssertDialog(msg
);
1188 #endif // wxDEBUG_LEVEL