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 // process pending wx events before sending idle events
337 ProcessPendingEvents();
339 // synthesize an idle event and check if more of them are needed
341 event
.SetEventObject(this);
344 return event
.MoreRequested();
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
352 bool wxAppConsoleBase
::IsMainLoopRunning()
354 const wxAppConsole
* const app
= GetInstance();
356 return app
&& app
->m_mainLoop
!= NULL
;
359 int wxAppConsoleBase
::FilterEvent(wxEvent
& WXUNUSED(event
))
361 // process the events normally by default
365 void wxAppConsoleBase
::DelayPendingEventHandler(wxEvtHandler
* toDelay
)
367 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
369 // move the handler from the list of handlers with processable pending events
370 // to the list of handlers with pending events which needs to be processed later
371 m_handlersWithPendingEvents
.Remove(toDelay
);
373 if (m_handlersWithPendingDelayedEvents
.Index(toDelay
) == wxNOT_FOUND
)
374 m_handlersWithPendingDelayedEvents
.Add(toDelay
);
376 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
379 void wxAppConsoleBase
::RemovePendingEventHandler(wxEvtHandler
* toRemove
)
381 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
383 if (m_handlersWithPendingEvents
.Index(toRemove
) != wxNOT_FOUND
)
385 m_handlersWithPendingEvents
.Remove(toRemove
);
387 // check that the handler was present only once in the list
388 wxASSERT_MSG( m_handlersWithPendingEvents
.Index(toRemove
) == wxNOT_FOUND
,
389 "Handler occurs twice in the m_handlersWithPendingEvents list!" );
391 //else: it wasn't in this list at all, it's ok
393 if (m_handlersWithPendingDelayedEvents
.Index(toRemove
) != wxNOT_FOUND
)
395 m_handlersWithPendingDelayedEvents
.Remove(toRemove
);
397 // check that the handler was present only once in the list
398 wxASSERT_MSG( m_handlersWithPendingDelayedEvents
.Index(toRemove
) == wxNOT_FOUND
,
399 "Handler occurs twice in m_handlersWithPendingDelayedEvents list!" );
401 //else: it wasn't in this list at all, it's ok
403 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
406 void wxAppConsoleBase
::AppendPendingEventHandler(wxEvtHandler
* toAppend
)
408 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
410 if ( m_handlersWithPendingEvents
.Index(toAppend
) == wxNOT_FOUND
)
411 m_handlersWithPendingEvents
.Add(toAppend
);
413 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
416 bool wxAppConsoleBase
::HasPendingEvents() const
418 wxENTER_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
420 bool has
= !m_handlersWithPendingEvents
.IsEmpty();
422 wxLEAVE_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
427 void wxAppConsoleBase
::SuspendProcessingOfPendingEvents()
429 m_bDoPendingEventProcessing
= false;
432 void wxAppConsoleBase
::ResumeProcessingOfPendingEvents()
434 m_bDoPendingEventProcessing
= true;
437 void wxAppConsoleBase
::ProcessPendingEvents()
439 if (!m_bDoPendingEventProcessing
)
442 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
444 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
445 "this helper list should be empty" );
447 // iterate until the list becomes empty: the handlers remove themselves
448 // from it when they don't have any more pending events
449 while (!m_handlersWithPendingEvents
.IsEmpty())
451 // In ProcessPendingEvents(), new handlers might be added
452 // and we can safely leave the critical section here.
453 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
455 // NOTE: we always call ProcessPendingEvents() on the first event handler
456 // with pending events because handlers auto-remove themselves
457 // from this list (see RemovePendingEventHandler) if they have no
458 // more pending events.
459 m_handlersWithPendingEvents
[0]->ProcessPendingEvents();
461 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
464 // now the wxHandlersWithPendingEvents is surely empty; however some event
465 // handlers may have moved themselves into wxHandlersWithPendingDelayedEvents
466 // because of a selective wxYield call in progress.
467 // Now we need to move them back to wxHandlersWithPendingEvents so the next
468 // call to this function has the chance of processing them:
469 if (!m_handlersWithPendingDelayedEvents
.IsEmpty())
471 WX_APPEND_ARRAY(m_handlersWithPendingEvents
, m_handlersWithPendingDelayedEvents
);
472 m_handlersWithPendingDelayedEvents
.Clear();
475 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
478 void wxAppConsoleBase
::DeletePendingEvents()
480 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
482 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
483 "this helper list should be empty" );
485 for (unsigned int i
=0; i
<m_handlersWithPendingEvents
.GetCount(); i
++)
486 m_handlersWithPendingEvents
[i
]->DeletePendingEvents();
488 m_handlersWithPendingEvents
.Clear();
490 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
493 // ----------------------------------------------------------------------------
494 // exception handling
495 // ----------------------------------------------------------------------------
500 wxAppConsoleBase
::HandleEvent(wxEvtHandler
*handler
,
501 wxEventFunction func
,
502 wxEvent
& event
) const
504 // by default, simply call the handler
505 (handler
->*func
)(event
);
508 void wxAppConsoleBase
::CallEventHandler(wxEvtHandler
*handler
,
509 wxEventFunctor
& functor
,
510 wxEvent
& event
) const
512 // If the functor holds a method then, for backward compatibility, call
514 wxEventFunction eventFunction
= functor
.GetEvtMethod();
517 HandleEvent(handler
, eventFunction
, event
);
519 functor(handler
, event
);
522 void wxAppConsoleBase
::OnUnhandledException()
525 // we're called from an exception handler so we can re-throw the exception
526 // to recover its type
533 catch ( std
::exception
& e
)
535 what
.Printf("std::exception of type \"%s\", what() = \"%s\"",
536 typeid(e
).name(), e
.what());
541 what
= "unknown exception";
544 wxMessageOutputBest().Printf(
545 "*** Caught unhandled %s; terminating\n", what
547 #endif // __WXDEBUG__
550 // ----------------------------------------------------------------------------
551 // exceptions support
552 // ----------------------------------------------------------------------------
554 bool wxAppConsoleBase
::OnExceptionInMainLoop()
558 // some compilers are too stupid to know that we never return after throw
559 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
564 #endif // wxUSE_EXCEPTIONS
566 // ----------------------------------------------------------------------------
568 // ----------------------------------------------------------------------------
570 #if wxUSE_CMDLINE_PARSER
572 #define OPTION_VERBOSE "verbose"
574 void wxAppConsoleBase
::OnInitCmdLine(wxCmdLineParser
& parser
)
576 // the standard command line options
577 static const wxCmdLineEntryDesc cmdLineDesc
[] =
583 gettext_noop("show this help message"),
585 wxCMD_LINE_OPTION_HELP
593 gettext_noop("generate verbose log messages"),
603 parser
.SetDesc(cmdLineDesc
);
606 bool wxAppConsoleBase
::OnCmdLineParsed(wxCmdLineParser
& parser
)
609 if ( parser
.Found(OPTION_VERBOSE
) )
611 wxLog
::SetVerbose(true);
620 bool wxAppConsoleBase
::OnCmdLineHelp(wxCmdLineParser
& parser
)
627 bool wxAppConsoleBase
::OnCmdLineError(wxCmdLineParser
& parser
)
634 #endif // wxUSE_CMDLINE_PARSER
636 // ----------------------------------------------------------------------------
638 // ----------------------------------------------------------------------------
641 bool wxAppConsoleBase
::CheckBuildOptions(const char *optionsSignature
,
642 const char *componentName
)
644 #if 0 // can't use wxLogTrace, not up and running yet
645 printf("checking build options object '%s' (ptr %p) in '%s'\n",
646 optionsSignature
, optionsSignature
, componentName
);
649 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
651 wxString lib
= wxString
::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
652 wxString prog
= wxString
::FromAscii(optionsSignature
);
653 wxString progName
= wxString
::FromAscii(componentName
);
656 msg
.Printf(_T("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
657 lib
.c_str(), progName
.c_str(), prog
.c_str());
659 wxLogFatalError(msg
.c_str());
661 // normally wxLogFatalError doesn't return
668 void wxAppConsoleBase
::OnAssertFailure(const wxChar
*file
,
675 ShowAssertDialog(file
, line
, func
, cond
, msg
, GetTraits());
677 // this function is still present even in debug level 0 build for ABI
678 // compatibility reasons but is never called there and so can simply do
685 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
688 void wxAppConsoleBase
::OnAssert(const wxChar
*file
,
693 OnAssertFailure(file
, line
, NULL
, cond
, msg
);
696 // ============================================================================
697 // other classes implementations
698 // ============================================================================
700 // ----------------------------------------------------------------------------
701 // wxConsoleAppTraitsBase
702 // ----------------------------------------------------------------------------
706 wxLog
*wxConsoleAppTraitsBase
::CreateLogTarget()
708 return new wxLogStderr
;
713 wxMessageOutput
*wxConsoleAppTraitsBase
::CreateMessageOutput()
715 return new wxMessageOutputStderr
;
720 wxFontMapper
*wxConsoleAppTraitsBase
::CreateFontMapper()
722 return (wxFontMapper
*)new wxFontMapperBase
;
725 #endif // wxUSE_FONTMAP
727 wxRendererNative
*wxConsoleAppTraitsBase
::CreateRenderer()
729 // console applications don't use renderers
733 bool wxConsoleAppTraitsBase
::ShowAssertDialog(const wxString
& msg
)
735 return wxAppTraitsBase
::ShowAssertDialog(msg
);
738 bool wxConsoleAppTraitsBase
::HasStderr()
740 // console applications always have stderr, even under Mac/Windows
744 void wxConsoleAppTraitsBase
::ScheduleForDestroy(wxObject
*object
)
749 void wxConsoleAppTraitsBase
::RemoveFromPendingDelete(wxObject
* WXUNUSED(object
))
754 // ----------------------------------------------------------------------------
756 // ----------------------------------------------------------------------------
759 void wxAppTraitsBase
::SetLocale()
761 wxSetlocale(LC_ALL
, "");
762 wxUpdateLocaleIsUtf8();
767 void wxMutexGuiEnterImpl();
768 void wxMutexGuiLeaveImpl();
770 void wxAppTraitsBase
::MutexGuiEnter()
772 wxMutexGuiEnterImpl();
775 void wxAppTraitsBase
::MutexGuiLeave()
777 wxMutexGuiLeaveImpl();
780 void WXDLLIMPEXP_BASE
wxMutexGuiEnter()
782 wxAppTraits
* const traits
= wxAppConsoleBase
::GetTraitsIfExists();
784 traits
->MutexGuiEnter();
787 void WXDLLIMPEXP_BASE
wxMutexGuiLeave()
789 wxAppTraits
* const traits
= wxAppConsoleBase
::GetTraitsIfExists();
791 traits
->MutexGuiLeave();
793 #endif // wxUSE_THREADS
795 bool wxAppTraitsBase
::ShowAssertDialog(const wxString
& msgOriginal
)
798 wxString msg
= msgOriginal
;
800 #if wxUSE_STACKWALKER
801 #if !defined(__WXMSW__)
802 // on Unix stack frame generation may take some time, depending on the
803 // size of the executable mainly... warn the user that we are working
804 wxFprintf(stderr
, wxT("[Debug] Generating a stack trace... please wait"));
808 const wxString stackTrace
= GetAssertStackTrace();
809 if ( !stackTrace
.empty() )
810 msg
<< _T("\n\nCall stack:\n") << stackTrace
;
811 #endif // wxUSE_STACKWALKER
813 return DoShowAssertDialog(msg
);
814 #else // !wxDEBUG_LEVEL
815 wxUnusedVar(msgOriginal
);
818 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
821 #if wxUSE_STACKWALKER
822 wxString wxAppTraitsBase
::GetAssertStackTrace()
827 class StackDump
: public wxStackWalker
832 const wxString
& GetStackTrace() const { return m_stackTrace
; }
835 virtual void OnStackFrame(const wxStackFrame
& frame
)
837 m_stackTrace
<< wxString
::Format
840 wx_truncate_cast(int, frame
.GetLevel())
843 wxString name
= frame
.GetName();
846 m_stackTrace
<< wxString
::Format(_T("%-40s"), name
.c_str());
850 m_stackTrace
<< wxString
::Format(_T("%p"), frame
.GetAddress());
853 if ( frame
.HasSourceLocation() )
855 m_stackTrace
<< _T('\t')
856 << frame
.GetFileName()
861 m_stackTrace
<< _T('\n');
865 wxString m_stackTrace
;
868 // don't show more than maxLines or we could get a dialog too tall to be
869 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
870 // characters it is still only 300 pixels...
871 static const int maxLines
= 20;
874 dump
.Walk(2, maxLines
); // don't show OnAssert() call itself
875 stackTrace
= dump
.GetStackTrace();
877 const int count
= stackTrace
.Freq(wxT('\n'));
878 for ( int i
= 0; i
< count
- maxLines
; i
++ )
879 stackTrace
= stackTrace
.BeforeLast(wxT('\n'));
882 #else // !wxDEBUG_LEVEL
883 // this function is still present for ABI-compatibility even in debug level
884 // 0 build but is not used there and so can simply do nothing
886 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
888 #endif // wxUSE_STACKWALKER
891 // ============================================================================
892 // global functions implementation
893 // ============================================================================
903 // what else can we do?
912 wxTheApp
->WakeUpIdle();
914 //else: do nothing, what can we do?
918 bool wxAssertIsEqual(int x
, int y
)
925 // break into the debugger
928 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
930 #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
932 #elif defined(__UNIX__)
939 // default assert handler
941 wxDefaultAssertHandler(const wxString
& file
,
943 const wxString
& func
,
944 const wxString
& cond
,
948 static int s_bInAssert
= 0;
950 wxRecursionGuard
guard(s_bInAssert
);
951 if ( guard
.IsInside() )
953 // can't use assert here to avoid infinite loops, so just trap
961 // by default, show the assert dialog box -- we can't customize this
963 ShowAssertDialog(file
, line
, func
, cond
, msg
);
967 // let the app process it as it wants
968 // FIXME-UTF8: use wc_str(), not c_str(), when ANSI build is removed
969 wxTheApp
->OnAssertFailure(file
.c_str(), line
, func
.c_str(),
970 cond
.c_str(), msg
.c_str());
974 wxAssertHandler_t wxTheAssertHandler
= wxDefaultAssertHandler
;
976 void wxOnAssert(const wxString
& file
,
978 const wxString
& func
,
979 const wxString
& cond
,
982 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
985 void wxOnAssert(const wxString
& file
,
987 const wxString
& func
,
988 const wxString
& cond
)
990 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
993 void wxOnAssert(const wxChar
*file
,
999 // this is the backwards-compatible version (unless we don't use Unicode)
1000 // so it could be called directly from the user code and this might happen
1001 // even when wxTheAssertHandler is NULL
1003 if ( wxTheAssertHandler
)
1004 #endif // wxUSE_UNICODE
1005 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1008 void wxOnAssert(const char *file
,
1012 const wxString
& msg
)
1014 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1017 void wxOnAssert(const char *file
,
1021 const wxCStrData
& msg
)
1023 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1027 void wxOnAssert(const char *file
,
1032 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
1035 void wxOnAssert(const char *file
,
1041 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1044 void wxOnAssert(const char *file
,
1050 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1052 #endif // wxUSE_UNICODE
1054 #endif // wxDEBUG_LEVEL
1056 // ============================================================================
1057 // private functions implementation
1058 // ============================================================================
1062 static void LINKAGEMODE
SetTraceMasks()
1066 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
1068 wxStringTokenizer
tkn(mask
, wxT(",;:"));
1069 while ( tkn
.HasMoreTokens() )
1070 wxLog
::AddTraceMask(tkn
.GetNextToken());
1075 #endif // __WXDEBUG__
1080 bool DoShowAssertDialog(const wxString
& msg
)
1082 // under MSW we can show the dialog even in the console mode
1083 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1084 wxString
msgDlg(msg
);
1086 // this message is intentionally not translated -- it is for developers
1087 // only -- and the less code we use here, less is the danger of recursively
1088 // asserting and dying
1089 msgDlg
+= wxT("\nDo you want to stop the program?\n")
1090 wxT("You can also choose [Cancel] to suppress ")
1091 wxT("further warnings.");
1093 switch ( ::MessageBox(NULL
, msgDlg
.wx_str(), _T("wxWidgets Debug Alert"),
1094 MB_YESNOCANCEL
| MB_ICONSTOP
) )
1104 //case IDNO: nothing to do
1107 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
1110 // TODO: ask the user to enter "Y" or "N" on the console?
1112 #endif // __WXMSW__/!__WXMSW__
1114 // continue with the asserts
1118 // show the standard assert dialog
1120 void ShowAssertDialog(const wxString
& file
,
1122 const wxString
& func
,
1123 const wxString
& cond
,
1124 const wxString
& msgUser
,
1125 wxAppTraits
*traits
)
1127 // this variable can be set to true to suppress "assert failure" messages
1128 static bool s_bNoAsserts
= false;
1133 // make life easier for people using VC++ IDE by using this format: like
1134 // this, clicking on the message will take us immediately to the place of
1135 // the failed assert
1136 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), file
, line
, cond
);
1138 // add the function name, if any
1139 if ( !func
.empty() )
1140 msg
<< _T(" in ") << func
<< _T("()");
1142 // and the message itself
1143 if ( !msgUser
.empty() )
1145 msg
<< _T(": ") << msgUser
;
1147 else // no message given
1153 // if we are not in the main thread, output the assert directly and trap
1154 // since dialogs cannot be displayed
1155 if ( !wxThread
::IsMain() )
1157 msg
+= wxT(" [in child thread]");
1159 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1161 OutputDebugString(msg
.wx_str());
1164 wxFprintf(stderr
, wxT("%s\n"), msg
.c_str());
1167 // He-e-e-e-elp!! we're asserting in a child thread
1171 #endif // wxUSE_THREADS
1173 if ( !s_bNoAsserts
)
1175 // send it to the normal log destination
1176 wxLogDebug(_T("%s"), msg
.c_str());
1180 // delegate showing assert dialog (if possible) to that class
1181 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
1183 else // no traits object
1185 // fall back to the function of last resort
1186 s_bNoAsserts
= DoShowAssertDialog(msg
);
1191 #endif // wxDEBUG_LEVEL