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"
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
& szFile
,
97 const wxString
& szFunc
,
98 const wxString
& szCond
,
99 const wxString
& szMsg
,
100 wxAppTraits
*traits
= NULL
);
102 // turn on the trace masks specified in the env variable WXTRACE
103 static void LINKAGEMODE
SetTraceMasks();
104 #endif // __WXDEBUG__
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 wxAppConsole
*wxAppConsoleBase::ms_appInstance
= NULL
;
112 wxAppInitializerFunction
wxAppConsoleBase::ms_appInitFn
= NULL
;
114 wxSocketManager
*wxAppTraitsBase::ms_manager
= NULL
;
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 // this defines wxEventLoopPtr
121 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopBase
)
123 // ============================================================================
124 // wxAppConsoleBase implementation
125 // ============================================================================
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 wxAppConsoleBase::wxAppConsoleBase()
136 ms_appInstance
= static_cast<wxAppConsole
*>(this);
141 // In unicode mode the SetTraceMasks call can cause an apptraits to be
142 // created, but since we are still in the constructor the wrong kind will
143 // be created for GUI apps. Destroy it so it can be created again later.
150 wxAppConsoleBase::~wxAppConsoleBase()
155 // ----------------------------------------------------------------------------
156 // initialization/cleanup
157 // ----------------------------------------------------------------------------
159 bool wxAppConsoleBase::Initialize(int& WXUNUSED(argc
), wxChar
**argv
)
162 GetTraits()->SetLocale();
166 if ( m_appName
.empty() && argv
&& argv
[0] )
168 // the application name is, by default, the name of its executable file
169 wxFileName::SplitPath(argv
[0], NULL
, &m_appName
, NULL
);
171 #endif // !__WXPALMOS__
176 wxEventLoopBase
*wxAppConsoleBase::CreateMainLoop()
178 return GetTraits()->CreateEventLoop();
181 void wxAppConsoleBase::CleanUp()
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 bool wxAppConsoleBase::OnInit()
196 #if wxUSE_CMDLINE_PARSER
197 wxCmdLineParser
parser(argc
, argv
);
199 OnInitCmdLine(parser
);
202 switch ( parser
.Parse(false /* don't show usage */) )
205 cont
= OnCmdLineHelp(parser
);
209 cont
= OnCmdLineParsed(parser
);
213 cont
= OnCmdLineError(parser
);
219 #endif // wxUSE_CMDLINE_PARSER
224 int wxAppConsoleBase::OnRun()
229 int wxAppConsoleBase::OnExit()
232 // delete the config object if any (don't use Get() here, but Set()
233 // because Get() could create a new config object)
234 delete wxConfigBase::Set(NULL
);
235 #endif // wxUSE_CONFIG
240 void wxAppConsoleBase::Exit()
242 if (m_mainLoop
!= NULL
)
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
252 wxAppTraits
*wxAppConsoleBase::CreateTraits()
254 return new wxConsoleAppTraits
;
257 wxAppTraits
*wxAppConsoleBase::GetTraits()
259 // FIXME-MT: protect this with a CS?
262 m_traits
= CreateTraits();
264 wxASSERT_MSG( m_traits
, _T("wxApp::CreateTraits() failed?") );
271 wxAppTraits
*wxAppConsoleBase::GetTraitsIfExists()
273 wxAppConsole
* const app
= GetInstance();
274 return app
? app
->GetTraits() : NULL
;
277 // ----------------------------------------------------------------------------
278 // wxEventLoop redirection
279 // ----------------------------------------------------------------------------
281 int wxAppConsoleBase::MainLoop()
283 wxEventLoopBaseTiedPtr
mainLoop(&m_mainLoop
, CreateMainLoop());
285 return m_mainLoop
? m_mainLoop
->Run() : -1;
288 void wxAppConsoleBase::ExitMainLoop()
290 // we should exit from the main event loop, not just any currently active
291 // (e.g. modal dialog) event loop
292 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
298 bool wxAppConsoleBase::Pending()
300 // use the currently active message loop here, not m_mainLoop, because if
301 // we're showing a modal dialog (with its own event loop) currently the
302 // main event loop is not running anyhow
303 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
305 return loop
&& loop
->Pending();
308 bool wxAppConsoleBase::Dispatch()
310 // see comment in Pending()
311 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
313 return loop
&& loop
->Dispatch();
316 bool wxAppConsoleBase::Yield(bool onlyIfNeeded
)
318 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
320 return loop
&& loop
->Yield(onlyIfNeeded
);
323 void wxAppConsoleBase::WakeUpIdle()
326 m_mainLoop
->WakeUp();
329 bool wxAppConsoleBase::ProcessIdle()
331 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
333 return loop
&& loop
->ProcessIdle();
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
341 bool wxAppConsoleBase::IsMainLoopRunning()
343 const wxAppConsole
* const app
= GetInstance();
345 return app
&& app
->m_mainLoop
!= NULL
;
348 int wxAppConsoleBase::FilterEvent(wxEvent
& WXUNUSED(event
))
350 // process the events normally by default
354 void wxAppConsoleBase::DelayPendingEventHandler(wxEvtHandler
* toDelay
)
356 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
358 // move the handler from the list of handlers with processable pending events
359 // to the list of handlers with pending events which needs to be processed later
360 m_handlersWithPendingEvents
.Remove(toDelay
);
362 if (m_handlersWithPendingDelayedEvents
.Index(toDelay
) == wxNOT_FOUND
)
363 m_handlersWithPendingDelayedEvents
.Add(toDelay
);
365 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
368 void wxAppConsoleBase::RemovePendingEventHandler(wxEvtHandler
* toRemove
)
370 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
372 if (m_handlersWithPendingEvents
.Index(toRemove
) != wxNOT_FOUND
)
374 m_handlersWithPendingEvents
.Remove(toRemove
);
376 // check that the handler was present only once in the list
377 wxASSERT_MSG( m_handlersWithPendingEvents
.Index(toRemove
) == wxNOT_FOUND
,
378 "Handler occurs twice in the m_handlersWithPendingEvents list!" );
380 //else: it wasn't in this list at all, it's ok
382 if (m_handlersWithPendingDelayedEvents
.Index(toRemove
) != wxNOT_FOUND
)
384 m_handlersWithPendingDelayedEvents
.Remove(toRemove
);
386 // check that the handler was present only once in the list
387 wxASSERT_MSG( m_handlersWithPendingDelayedEvents
.Index(toRemove
) == wxNOT_FOUND
,
388 "Handler occurs twice in m_handlersWithPendingDelayedEvents list!" );
390 //else: it wasn't in this list at all, it's ok
392 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
395 void wxAppConsoleBase::AppendPendingEventHandler(wxEvtHandler
* toAppend
)
397 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
399 if ( m_handlersWithPendingEvents
.Index(toAppend
) == wxNOT_FOUND
)
400 m_handlersWithPendingEvents
.Add(toAppend
);
402 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
405 bool wxAppConsoleBase::HasPendingEvents() const
407 wxENTER_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
409 bool has
= !m_handlersWithPendingEvents
.IsEmpty();
411 wxLEAVE_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
416 void wxAppConsoleBase::SuspendProcessingOfPendingEvents()
418 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
419 // entering the critical section locks blocks calls to ProcessPendingEvents()
422 void wxAppConsoleBase::ResumeProcessingOfPendingEvents()
424 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
427 void wxAppConsoleBase::ProcessPendingEvents()
429 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
431 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
432 "this helper list should be empty" );
434 // iterate until the list becomes empty: the handlers remove themselves
435 // from it when they don't have any more pending events
436 while (!m_handlersWithPendingEvents
.IsEmpty())
438 // In ProcessPendingEvents(), new handlers might be added
439 // and we can safely leave the critical section here.
440 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
442 // NOTE: we always call ProcessPendingEvents() on the first event handler
443 // with pending events because handlers auto-remove themselves
444 // from this list (see RemovePendingEventHandler) if they have no
445 // more pending events.
446 m_handlersWithPendingEvents
[0]->ProcessPendingEvents();
448 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
451 // now the wxHandlersWithPendingEvents is surely empty; however some event
452 // handlers may have moved themselves into wxHandlersWithPendingDelayedEvents
453 // because of a selective wxYield call in progress.
454 // Now we need to move them back to wxHandlersWithPendingEvents so the next
455 // call to this function has the chance of processing them:
456 if (!m_handlersWithPendingDelayedEvents
.IsEmpty())
458 WX_APPEND_ARRAY(m_handlersWithPendingEvents
, m_handlersWithPendingDelayedEvents
);
459 m_handlersWithPendingDelayedEvents
.Clear();
462 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
465 // ----------------------------------------------------------------------------
466 // exception handling
467 // ----------------------------------------------------------------------------
472 wxAppConsoleBase::HandleEvent(wxEvtHandler
*handler
,
473 wxEventFunction func
,
474 wxEvent
& event
) const
476 // by default, simply call the handler
477 (handler
->*func
)(event
);
480 void wxAppConsoleBase::CallEventHandler(wxEvtHandler
*handler
,
481 wxEventFunctor
& functor
,
482 wxEvent
& event
) const
484 // If the functor holds a method then, for backward compatibility, call
486 wxEventFunction eventFunction
= functor
.GetMethod();
489 HandleEvent(handler
, eventFunction
, event
);
491 functor(handler
, event
);
494 void wxAppConsoleBase::OnUnhandledException()
497 // we're called from an exception handler so we can re-throw the exception
498 // to recover its type
505 catch ( std::exception
& e
)
507 what
.Printf("std::exception of type \"%s\", what() = \"%s\"",
508 typeid(e
).name(), e
.what());
513 what
= "unknown exception";
516 wxMessageOutputBest().Printf(
517 "*** Caught unhandled %s; terminating\n", what
519 #endif // __WXDEBUG__
522 // ----------------------------------------------------------------------------
523 // exceptions support
524 // ----------------------------------------------------------------------------
526 bool wxAppConsoleBase::OnExceptionInMainLoop()
530 // some compilers are too stupid to know that we never return after throw
531 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
536 #endif // wxUSE_EXCEPTIONS
538 // ----------------------------------------------------------------------------
540 // ----------------------------------------------------------------------------
542 #if wxUSE_CMDLINE_PARSER
544 #define OPTION_VERBOSE "verbose"
546 void wxAppConsoleBase::OnInitCmdLine(wxCmdLineParser
& parser
)
548 // the standard command line options
549 static const wxCmdLineEntryDesc cmdLineDesc
[] =
555 gettext_noop("show this help message"),
557 wxCMD_LINE_OPTION_HELP
565 gettext_noop("generate verbose log messages"),
575 parser
.SetDesc(cmdLineDesc
);
578 bool wxAppConsoleBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
581 if ( parser
.Found(OPTION_VERBOSE
) )
583 wxLog::SetVerbose(true);
592 bool wxAppConsoleBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
599 bool wxAppConsoleBase::OnCmdLineError(wxCmdLineParser
& parser
)
606 #endif // wxUSE_CMDLINE_PARSER
608 // ----------------------------------------------------------------------------
610 // ----------------------------------------------------------------------------
613 bool wxAppConsoleBase::CheckBuildOptions(const char *optionsSignature
,
614 const char *componentName
)
616 #if 0 // can't use wxLogTrace, not up and running yet
617 printf("checking build options object '%s' (ptr %p) in '%s'\n",
618 optionsSignature
, optionsSignature
, componentName
);
621 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
623 wxString lib
= wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
624 wxString prog
= wxString::FromAscii(optionsSignature
);
625 wxString progName
= wxString::FromAscii(componentName
);
628 msg
.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
629 lib
.c_str(), progName
.c_str(), prog
.c_str());
631 wxLogFatalError(msg
.c_str());
633 // normally wxLogFatalError doesn't return
642 void wxAppConsoleBase::OnAssertFailure(const wxChar
*file
,
648 ShowAssertDialog(file
, line
, func
, cond
, msg
, GetTraits());
651 void wxAppConsoleBase::OnAssert(const wxChar
*file
,
656 OnAssertFailure(file
, line
, NULL
, cond
, msg
);
659 #endif // __WXDEBUG__
661 // ============================================================================
662 // other classes implementations
663 // ============================================================================
665 // ----------------------------------------------------------------------------
666 // wxConsoleAppTraitsBase
667 // ----------------------------------------------------------------------------
671 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
673 return new wxLogStderr
;
678 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
680 return new wxMessageOutputStderr
;
685 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
687 return (wxFontMapper
*)new wxFontMapperBase
;
690 #endif // wxUSE_FONTMAP
692 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
694 // console applications don't use renderers
699 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
701 return wxAppTraitsBase::ShowAssertDialog(msg
);
705 bool wxConsoleAppTraitsBase::HasStderr()
707 // console applications always have stderr, even under Mac/Windows
711 void wxConsoleAppTraitsBase::ScheduleForDestroy(wxObject
*object
)
716 void wxConsoleAppTraitsBase::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
721 // ----------------------------------------------------------------------------
723 // ----------------------------------------------------------------------------
726 void wxAppTraitsBase::SetLocale()
728 wxSetlocale(LC_ALL
, "");
729 wxUpdateLocaleIsUtf8();
734 void wxMutexGuiEnterImpl();
735 void wxMutexGuiLeaveImpl();
737 void wxAppTraitsBase::MutexGuiEnter()
739 wxMutexGuiEnterImpl();
742 void wxAppTraitsBase::MutexGuiLeave()
744 wxMutexGuiLeaveImpl();
747 void WXDLLIMPEXP_BASE
wxMutexGuiEnter()
749 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
751 traits
->MutexGuiEnter();
754 void WXDLLIMPEXP_BASE
wxMutexGuiLeave()
756 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
758 traits
->MutexGuiLeave();
760 #endif // wxUSE_THREADS
764 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msgOriginal
)
766 wxString msg
= msgOriginal
;
768 #if wxUSE_STACKWALKER
769 #if !defined(__WXMSW__)
770 // on Unix stack frame generation may take some time, depending on the
771 // size of the executable mainly... warn the user that we are working
772 wxFprintf(stderr
, wxT("[Debug] Generating a stack trace... please wait"));
776 const wxString stackTrace
= GetAssertStackTrace();
777 if ( !stackTrace
.empty() )
778 msg
<< _T("\n\nCall stack:\n") << stackTrace
;
779 #endif // wxUSE_STACKWALKER
781 return DoShowAssertDialog(msg
);
784 #if wxUSE_STACKWALKER
785 wxString
wxAppTraitsBase::GetAssertStackTrace()
789 class StackDump
: public wxStackWalker
794 const wxString
& GetStackTrace() const { return m_stackTrace
; }
797 virtual void OnStackFrame(const wxStackFrame
& frame
)
799 m_stackTrace
<< wxString::Format
802 wx_truncate_cast(int, frame
.GetLevel())
805 wxString name
= frame
.GetName();
808 m_stackTrace
<< wxString::Format(_T("%-40s"), name
.c_str());
812 m_stackTrace
<< wxString::Format(_T("%p"), frame
.GetAddress());
815 if ( frame
.HasSourceLocation() )
817 m_stackTrace
<< _T('\t')
818 << frame
.GetFileName()
823 m_stackTrace
<< _T('\n');
827 wxString m_stackTrace
;
830 // don't show more than maxLines or we could get a dialog too tall to be
831 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
832 // characters it is still only 300 pixels...
833 static const int maxLines
= 20;
836 dump
.Walk(2, maxLines
); // don't show OnAssert() call itself
837 stackTrace
= dump
.GetStackTrace();
839 const int count
= stackTrace
.Freq(wxT('\n'));
840 for ( int i
= 0; i
< count
- maxLines
; i
++ )
841 stackTrace
= stackTrace
.BeforeLast(wxT('\n'));
845 #endif // wxUSE_STACKWALKER
848 #endif // __WXDEBUG__
850 // ============================================================================
851 // global functions implementation
852 // ============================================================================
862 // what else can we do?
871 wxTheApp
->WakeUpIdle();
873 //else: do nothing, what can we do?
879 bool wxAssertIsEqual(int x
, int y
)
884 // break into the debugger
887 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
889 #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
891 #elif defined(__UNIX__)
898 // this function is called when an assert fails
899 static void wxDoOnAssert(const wxString
& szFile
,
901 const wxString
& szFunc
,
902 const wxString
& szCond
,
903 const wxString
& szMsg
= wxEmptyString
)
906 static int s_bInAssert
= 0;
908 wxRecursionGuard
guard(s_bInAssert
);
909 if ( guard
.IsInside() )
911 // can't use assert here to avoid infinite loops, so just trap
919 // by default, show the assert dialog box -- we can't customize this
921 ShowAssertDialog(szFile
, nLine
, szFunc
, szCond
, szMsg
);
925 // let the app process it as it wants
926 // FIXME-UTF8: use wc_str(), not c_str(), when ANSI build is removed
927 wxTheApp
->OnAssertFailure(szFile
.c_str(), nLine
, szFunc
.c_str(),
928 szCond
.c_str(), szMsg
.c_str());
932 void wxOnAssert(const wxString
& szFile
,
934 const wxString
& szFunc
,
935 const wxString
& szCond
,
936 const wxString
& szMsg
)
938 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
, szMsg
);
941 void wxOnAssert(const wxString
& szFile
,
943 const wxString
& szFunc
,
944 const wxString
& szCond
)
946 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
);
949 void wxOnAssert(const wxChar
*szFile
,
952 const wxChar
*szCond
,
955 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
, szMsg
);
958 void wxOnAssert(const char *szFile
,
962 const wxString
& szMsg
)
964 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
, szMsg
);
967 void wxOnAssert(const char *szFile
,
971 const wxCStrData
& msg
)
973 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
, msg
);
977 void wxOnAssert(const char *szFile
,
982 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
);
985 void wxOnAssert(const char *szFile
,
991 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
, szMsg
);
994 void wxOnAssert(const char *szFile
,
1000 wxDoOnAssert(szFile
, nLine
, szFunc
, szCond
, szMsg
);
1002 #endif // wxUSE_UNICODE
1004 #endif // __WXDEBUG__
1006 // ============================================================================
1007 // private functions implementation
1008 // ============================================================================
1012 static void LINKAGEMODE
SetTraceMasks()
1016 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
1018 wxStringTokenizer
tkn(mask
, wxT(",;:"));
1019 while ( tkn
.HasMoreTokens() )
1020 wxLog::AddTraceMask(tkn
.GetNextToken());
1026 bool DoShowAssertDialog(const wxString
& msg
)
1028 // under MSW we can show the dialog even in the console mode
1029 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1030 wxString
msgDlg(msg
);
1032 // this message is intentionally not translated -- it is for
1034 msgDlg
+= wxT("\nDo you want to stop the program?\n")
1035 wxT("You can also choose [Cancel] to suppress ")
1036 wxT("further warnings.");
1038 switch ( ::MessageBox(NULL
, msgDlg
.wx_str(), _T("wxWidgets Debug Alert"),
1039 MB_YESNOCANCEL
| MB_ICONSTOP
) )
1049 //case IDNO: nothing to do
1052 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
1055 // TODO: ask the user to enter "Y" or "N" on the console?
1057 #endif // __WXMSW__/!__WXMSW__
1059 // continue with the asserts
1063 // show the assert modal dialog
1065 void ShowAssertDialog(const wxString
& szFile
,
1067 const wxString
& szFunc
,
1068 const wxString
& szCond
,
1069 const wxString
& szMsg
,
1070 wxAppTraits
*traits
)
1072 // this variable can be set to true to suppress "assert failure" messages
1073 static bool s_bNoAsserts
= false;
1078 // make life easier for people using VC++ IDE by using this format: like
1079 // this, clicking on the message will take us immediately to the place of
1080 // the failed assert
1081 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile
, nLine
, szCond
);
1083 // add the function name, if any
1084 if ( !szFunc
.empty() )
1085 msg
<< _T(" in ") << szFunc
<< _T("()");
1087 // and the message itself
1088 if ( !szMsg
.empty() )
1090 msg
<< _T(": ") << szMsg
;
1092 else // no message given
1098 // if we are not in the main thread, output the assert directly and trap
1099 // since dialogs cannot be displayed
1100 if ( !wxThread::IsMain() )
1102 msg
+= wxT(" [in child thread]");
1104 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1106 OutputDebugString(msg
.wx_str());
1109 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
1112 // He-e-e-e-elp!! we're asserting in a child thread
1116 #endif // wxUSE_THREADS
1118 if ( !s_bNoAsserts
)
1120 // send it to the normal log destination
1121 wxLogDebug(_T("%s"), msg
.c_str());
1125 // delegate showing assert dialog (if possible) to that class
1126 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
1128 else // no traits object
1130 // fall back to the function of last resort
1131 s_bNoAsserts
= DoShowAssertDialog(msg
);
1136 #endif // __WXDEBUG__