1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/appbase.cpp
3 // Purpose: implements wxAppConsoleBase class
4 // Author: Vadim Zeitlin
6 // Created: 19.06.2003 (extracted from common/appcmn.cpp)
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/msw/wrapwin.h" // includes windows.h for MessageBox()
36 #include "wx/wxcrtvararg.h"
39 #include "wx/apptrait.h"
40 #include "wx/cmdline.h"
41 #include "wx/confbase.h"
42 #include "wx/evtloop.h"
43 #include "wx/filename.h"
44 #include "wx/msgout.h"
45 #include "wx/scopedptr.h"
46 #include "wx/tokenzr.h"
47 #include "wx/thread.h"
49 #if wxUSE_EXCEPTIONS && wxUSE_STL
55 #if !defined(__WXMSW__) || defined(__WXMICROWIN__)
56 #include <signal.h> // for SIGTRAP used by wxTrap()
60 #endif // ! __WXPALMOS5__
63 #include "wx/fontmap.h"
64 #endif // wxUSE_FONTMAP
68 #include "wx/stackwalk.h"
70 #include "wx/msw/debughlp.h"
72 #endif // wxUSE_STACKWALKER
74 #include "wx/recguard.h"
75 #endif // wxDEBUG_LEVEL
77 // wxABI_VERSION can be defined when compiling applications but it should be
78 // left undefined when compiling the library itself, it is then set to its
79 // default value in version.h
80 #if wxABI_VERSION != wxMAJOR_VERSION * 10000 + wxMINOR_VERSION * 100 + 99
81 #error "wxABI_VERSION should not be defined when compiling the library"
84 // ----------------------------------------------------------------------------
85 // private functions prototypes
86 // ----------------------------------------------------------------------------
89 // really just show the assert dialog
90 static bool DoShowAssertDialog(const wxString
& msg
);
92 // prepare for showing the assert dialog, use the given traits or
93 // DoShowAssertDialog() as last fallback to really show it
95 void ShowAssertDialog(const wxString
& file
,
100 wxAppTraits
*traits
= NULL
);
101 #endif // wxDEBUG_LEVEL
104 // turn on the trace masks specified in the env variable WXTRACE
105 static void LINKAGEMODE
SetTraceMasks();
106 #endif // __WXDEBUG__
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 wxAppConsole
*wxAppConsoleBase::ms_appInstance
= NULL
;
114 wxAppInitializerFunction
wxAppConsoleBase::ms_appInitFn
= NULL
;
116 wxSocketManager
*wxAppTraitsBase::ms_manager
= NULL
;
118 WXDLLIMPEXP_DATA_BASE(wxList
) wxPendingDelete
;
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 // this defines wxEventLoopPtr
125 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoopBase
)
127 // ============================================================================
128 // wxAppConsoleBase implementation
129 // ============================================================================
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 wxAppConsoleBase::wxAppConsoleBase()
139 m_bDoPendingEventProcessing
= true;
141 ms_appInstance
= static_cast<wxAppConsole
*>(this);
146 // In unicode mode the SetTraceMasks call can cause an apptraits to be
147 // created, but since we are still in the constructor the wrong kind will
148 // be created for GUI apps. Destroy it so it can be created again later.
155 wxAppConsoleBase::~wxAppConsoleBase()
157 // we're being destroyed and using this object from now on may not work or
158 // even crash so don't leave dangling pointers to it
159 ms_appInstance
= NULL
;
164 // ----------------------------------------------------------------------------
165 // initialization/cleanup
166 // ----------------------------------------------------------------------------
168 bool wxAppConsoleBase::Initialize(int& WXUNUSED(argc
), wxChar
**WXUNUSED(argv
))
171 GetTraits()->SetLocale();
177 wxString
wxAppConsoleBase::GetAppName() const
179 wxString name
= m_appName
;
185 // the application name is, by default, the name of its executable file
186 wxFileName::SplitPath(argv
[0], NULL
, &name
, NULL
);
189 #endif // !__WXPALMOS__
193 wxString
wxAppConsoleBase::GetAppDisplayName() const
195 // use the explicitly provided display name, if any
196 if ( !m_appDisplayName
.empty() )
197 return m_appDisplayName
;
199 // if the application name was explicitly set, use it as is as capitalizing
200 // it won't always produce good results
201 if ( !m_appName
.empty() )
204 // if neither is set, use the capitalized version of the program file as
205 // it's the most reasonable default
206 return GetAppName().Capitalize();
209 wxEventLoopBase
*wxAppConsoleBase::CreateMainLoop()
211 return GetTraits()->CreateEventLoop();
214 void wxAppConsoleBase::CleanUp()
223 // ----------------------------------------------------------------------------
225 // ----------------------------------------------------------------------------
227 bool wxAppConsoleBase::OnInit()
229 #if wxUSE_CMDLINE_PARSER
230 wxCmdLineParser
parser(argc
, argv
);
232 OnInitCmdLine(parser
);
235 switch ( parser
.Parse(false /* don't show usage */) )
238 cont
= OnCmdLineHelp(parser
);
242 cont
= OnCmdLineParsed(parser
);
246 cont
= OnCmdLineError(parser
);
252 #endif // wxUSE_CMDLINE_PARSER
257 int wxAppConsoleBase::OnRun()
262 int wxAppConsoleBase::OnExit()
265 // delete the config object if any (don't use Get() here, but Set()
266 // because Get() could create a new config object)
267 delete wxConfigBase::Set(NULL
);
268 #endif // wxUSE_CONFIG
273 void wxAppConsoleBase::Exit()
275 if (m_mainLoop
!= NULL
)
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 wxAppTraits
*wxAppConsoleBase::CreateTraits()
287 return new wxConsoleAppTraits
;
290 wxAppTraits
*wxAppConsoleBase::GetTraits()
292 // FIXME-MT: protect this with a CS?
295 m_traits
= CreateTraits();
297 wxASSERT_MSG( m_traits
, wxT("wxApp::CreateTraits() failed?") );
304 wxAppTraits
*wxAppConsoleBase::GetTraitsIfExists()
306 wxAppConsole
* const app
= GetInstance();
307 return app
? app
->GetTraits() : NULL
;
310 // ----------------------------------------------------------------------------
311 // wxEventLoop redirection
312 // ----------------------------------------------------------------------------
314 int wxAppConsoleBase::MainLoop()
316 wxEventLoopBaseTiedPtr
mainLoop(&m_mainLoop
, CreateMainLoop());
318 return m_mainLoop
? m_mainLoop
->Run() : -1;
321 void wxAppConsoleBase::ExitMainLoop()
323 // we should exit from the main event loop, not just any currently active
324 // (e.g. modal dialog) event loop
325 if ( m_mainLoop
&& m_mainLoop
->IsRunning() )
331 bool wxAppConsoleBase::Pending()
333 // use the currently active message loop here, not m_mainLoop, because if
334 // we're showing a modal dialog (with its own event loop) currently the
335 // main event loop is not running anyhow
336 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
338 return loop
&& loop
->Pending();
341 bool wxAppConsoleBase::Dispatch()
343 // see comment in Pending()
344 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
346 return loop
&& loop
->Dispatch();
349 bool wxAppConsoleBase::Yield(bool onlyIfNeeded
)
351 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
353 return loop
&& loop
->Yield(onlyIfNeeded
);
356 void wxAppConsoleBase::WakeUpIdle()
358 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
364 bool wxAppConsoleBase::ProcessIdle()
366 // synthesize an idle event and check if more of them are needed
368 event
.SetEventObject(this);
372 // flush the logged messages if any (do this after processing the events
373 // which could have logged new messages)
374 wxLog::FlushActive();
377 return event
.MoreRequested();
380 bool wxAppConsoleBase::UsesEventLoop() const
382 // in console applications we don't know whether we're going to have an
383 // event loop so assume we won't -- unless we already have one running
384 return wxEventLoopBase::GetActive() != NULL
;
387 // ----------------------------------------------------------------------------
389 // ----------------------------------------------------------------------------
392 bool wxAppConsoleBase::IsMainLoopRunning()
394 const wxAppConsole
* const app
= GetInstance();
396 return app
&& app
->m_mainLoop
!= NULL
;
399 int wxAppConsoleBase::FilterEvent(wxEvent
& WXUNUSED(event
))
401 // process the events normally by default
405 void wxAppConsoleBase::DelayPendingEventHandler(wxEvtHandler
* toDelay
)
407 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
409 // move the handler from the list of handlers with processable pending events
410 // to the list of handlers with pending events which needs to be processed later
411 m_handlersWithPendingEvents
.Remove(toDelay
);
413 if (m_handlersWithPendingDelayedEvents
.Index(toDelay
) == wxNOT_FOUND
)
414 m_handlersWithPendingDelayedEvents
.Add(toDelay
);
416 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
419 void wxAppConsoleBase::RemovePendingEventHandler(wxEvtHandler
* toRemove
)
421 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
423 if (m_handlersWithPendingEvents
.Index(toRemove
) != wxNOT_FOUND
)
425 m_handlersWithPendingEvents
.Remove(toRemove
);
427 // check that the handler was present only once in the list
428 wxASSERT_MSG( m_handlersWithPendingEvents
.Index(toRemove
) == wxNOT_FOUND
,
429 "Handler occurs twice in the m_handlersWithPendingEvents list!" );
431 //else: it wasn't in this list at all, it's ok
433 if (m_handlersWithPendingDelayedEvents
.Index(toRemove
) != wxNOT_FOUND
)
435 m_handlersWithPendingDelayedEvents
.Remove(toRemove
);
437 // check that the handler was present only once in the list
438 wxASSERT_MSG( m_handlersWithPendingDelayedEvents
.Index(toRemove
) == wxNOT_FOUND
,
439 "Handler occurs twice in m_handlersWithPendingDelayedEvents list!" );
441 //else: it wasn't in this list at all, it's ok
443 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
446 void wxAppConsoleBase::AppendPendingEventHandler(wxEvtHandler
* toAppend
)
448 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
450 if ( m_handlersWithPendingEvents
.Index(toAppend
) == wxNOT_FOUND
)
451 m_handlersWithPendingEvents
.Add(toAppend
);
453 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
456 bool wxAppConsoleBase::HasPendingEvents() const
458 wxENTER_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
460 bool has
= !m_handlersWithPendingEvents
.IsEmpty();
462 wxLEAVE_CRIT_SECT(const_cast<wxAppConsoleBase
*>(this)->m_handlersWithPendingEventsLocker
);
467 void wxAppConsoleBase::SuspendProcessingOfPendingEvents()
469 m_bDoPendingEventProcessing
= false;
472 void wxAppConsoleBase::ResumeProcessingOfPendingEvents()
474 m_bDoPendingEventProcessing
= true;
477 void wxAppConsoleBase::ProcessPendingEvents()
479 if ( m_bDoPendingEventProcessing
)
481 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
483 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
484 "this helper list should be empty" );
486 // iterate until the list becomes empty: the handlers remove themselves
487 // from it when they don't have any more pending events
488 while (!m_handlersWithPendingEvents
.IsEmpty())
490 // In ProcessPendingEvents(), new handlers might be added
491 // and we can safely leave the critical section here.
492 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
494 // NOTE: we always call ProcessPendingEvents() on the first event handler
495 // with pending events because handlers auto-remove themselves
496 // from this list (see RemovePendingEventHandler) if they have no
497 // more pending events.
498 m_handlersWithPendingEvents
[0]->ProcessPendingEvents();
500 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
503 // now the wxHandlersWithPendingEvents is surely empty; however some event
504 // handlers may have moved themselves into wxHandlersWithPendingDelayedEvents
505 // because of a selective wxYield call in progress.
506 // Now we need to move them back to wxHandlersWithPendingEvents so the next
507 // call to this function has the chance of processing them:
508 if (!m_handlersWithPendingDelayedEvents
.IsEmpty())
510 WX_APPEND_ARRAY(m_handlersWithPendingEvents
, m_handlersWithPendingDelayedEvents
);
511 m_handlersWithPendingDelayedEvents
.Clear();
514 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
517 // Garbage collect all objects previously scheduled for destruction.
518 DeletePendingObjects();
521 void wxAppConsoleBase::DeletePendingEvents()
523 wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker
);
525 wxCHECK_RET( m_handlersWithPendingDelayedEvents
.IsEmpty(),
526 "this helper list should be empty" );
528 for (unsigned int i
=0; i
<m_handlersWithPendingEvents
.GetCount(); i
++)
529 m_handlersWithPendingEvents
[i
]->DeletePendingEvents();
531 m_handlersWithPendingEvents
.Clear();
533 wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker
);
536 // ----------------------------------------------------------------------------
537 // delayed objects destruction
538 // ----------------------------------------------------------------------------
540 bool wxAppConsoleBase::IsScheduledForDestruction(wxObject
*object
) const
542 return wxPendingDelete
.Member(object
);
545 void wxAppConsoleBase::ScheduleForDestruction(wxObject
*object
)
547 if ( !UsesEventLoop() )
549 // we won't be able to delete it later so do it right now
553 //else: we either already have or will soon start an event loop
555 if ( !wxPendingDelete
.Member(object
) )
556 wxPendingDelete
.Append(object
);
559 void wxAppConsoleBase::DeletePendingObjects()
561 wxList::compatibility_iterator node
= wxPendingDelete
.GetFirst();
564 wxObject
*obj
= node
->GetData();
566 // remove it from the list first so that if we get back here somehow
567 // during the object deletion (e.g. wxYield called from its dtor) we
568 // wouldn't try to delete it the second time
569 if ( wxPendingDelete
.Member(obj
) )
570 wxPendingDelete
.Erase(node
);
574 // Deleting one object may have deleted other pending
575 // objects, so start from beginning of list again.
576 node
= wxPendingDelete
.GetFirst();
580 // ----------------------------------------------------------------------------
581 // exception handling
582 // ----------------------------------------------------------------------------
587 wxAppConsoleBase::HandleEvent(wxEvtHandler
*handler
,
588 wxEventFunction func
,
589 wxEvent
& event
) const
591 // by default, simply call the handler
592 (handler
->*func
)(event
);
595 void wxAppConsoleBase::CallEventHandler(wxEvtHandler
*handler
,
596 wxEventFunctor
& functor
,
597 wxEvent
& event
) const
599 // If the functor holds a method then, for backward compatibility, call
601 wxEventFunction eventFunction
= functor
.GetEvtMethod();
604 HandleEvent(handler
, eventFunction
, event
);
606 functor(handler
, event
);
609 void wxAppConsoleBase::OnUnhandledException()
612 // we're called from an exception handler so we can re-throw the exception
613 // to recover its type
620 catch ( std::exception
& e
)
622 what
.Printf("std::exception of type \"%s\", what() = \"%s\"",
623 typeid(e
).name(), e
.what());
628 what
= "unknown exception";
631 wxMessageOutputBest().Printf(
632 "*** Caught unhandled %s; terminating\n", what
634 #endif // __WXDEBUG__
637 // ----------------------------------------------------------------------------
638 // exceptions support
639 // ----------------------------------------------------------------------------
641 bool wxAppConsoleBase::OnExceptionInMainLoop()
645 // some compilers are too stupid to know that we never return after throw
646 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
651 #endif // wxUSE_EXCEPTIONS
653 // ----------------------------------------------------------------------------
655 // ----------------------------------------------------------------------------
657 #if wxUSE_CMDLINE_PARSER
659 #define OPTION_VERBOSE "verbose"
661 void wxAppConsoleBase::OnInitCmdLine(wxCmdLineParser
& parser
)
663 // the standard command line options
664 static const wxCmdLineEntryDesc cmdLineDesc
[] =
670 gettext_noop("show this help message"),
672 wxCMD_LINE_OPTION_HELP
680 gettext_noop("generate verbose log messages"),
690 parser
.SetDesc(cmdLineDesc
);
693 bool wxAppConsoleBase::OnCmdLineParsed(wxCmdLineParser
& parser
)
696 if ( parser
.Found(OPTION_VERBOSE
) )
698 wxLog::SetVerbose(true);
707 bool wxAppConsoleBase::OnCmdLineHelp(wxCmdLineParser
& parser
)
714 bool wxAppConsoleBase::OnCmdLineError(wxCmdLineParser
& parser
)
721 #endif // wxUSE_CMDLINE_PARSER
723 // ----------------------------------------------------------------------------
725 // ----------------------------------------------------------------------------
728 bool wxAppConsoleBase::CheckBuildOptions(const char *optionsSignature
,
729 const char *componentName
)
731 #if 0 // can't use wxLogTrace, not up and running yet
732 printf("checking build options object '%s' (ptr %p) in '%s'\n",
733 optionsSignature
, optionsSignature
, componentName
);
736 if ( strcmp(optionsSignature
, WX_BUILD_OPTIONS_SIGNATURE
) != 0 )
738 wxString lib
= wxString::FromAscii(WX_BUILD_OPTIONS_SIGNATURE
);
739 wxString prog
= wxString::FromAscii(optionsSignature
);
740 wxString progName
= wxString::FromAscii(componentName
);
743 msg
.Printf(wxT("Mismatch between the program and library build versions detected.\nThe library used %s,\nand %s used %s."),
744 lib
.c_str(), progName
.c_str(), prog
.c_str());
746 wxLogFatalError(msg
.c_str());
748 // normally wxLogFatalError doesn't return
755 void wxAppConsoleBase::OnAssertFailure(const wxChar
*file
,
762 ShowAssertDialog(file
, line
, func
, cond
, msg
, GetTraits());
764 // this function is still present even in debug level 0 build for ABI
765 // compatibility reasons but is never called there and so can simply do
772 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
775 void wxAppConsoleBase::OnAssert(const wxChar
*file
,
780 OnAssertFailure(file
, line
, NULL
, cond
, msg
);
783 // ============================================================================
784 // other classes implementations
785 // ============================================================================
787 // ----------------------------------------------------------------------------
788 // wxConsoleAppTraitsBase
789 // ----------------------------------------------------------------------------
793 wxLog
*wxConsoleAppTraitsBase::CreateLogTarget()
795 return new wxLogStderr
;
800 wxMessageOutput
*wxConsoleAppTraitsBase::CreateMessageOutput()
802 return new wxMessageOutputStderr
;
807 wxFontMapper
*wxConsoleAppTraitsBase::CreateFontMapper()
809 return (wxFontMapper
*)new wxFontMapperBase
;
812 #endif // wxUSE_FONTMAP
814 wxRendererNative
*wxConsoleAppTraitsBase::CreateRenderer()
816 // console applications don't use renderers
820 bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString
& msg
)
822 return wxAppTraitsBase::ShowAssertDialog(msg
);
825 bool wxConsoleAppTraitsBase::HasStderr()
827 // console applications always have stderr, even under Mac/Windows
831 // ----------------------------------------------------------------------------
833 // ----------------------------------------------------------------------------
836 void wxAppTraitsBase::SetLocale()
838 wxSetlocale(LC_ALL
, "");
839 wxUpdateLocaleIsUtf8();
844 void wxMutexGuiEnterImpl();
845 void wxMutexGuiLeaveImpl();
847 void wxAppTraitsBase::MutexGuiEnter()
849 wxMutexGuiEnterImpl();
852 void wxAppTraitsBase::MutexGuiLeave()
854 wxMutexGuiLeaveImpl();
857 void WXDLLIMPEXP_BASE
wxMutexGuiEnter()
859 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
861 traits
->MutexGuiEnter();
864 void WXDLLIMPEXP_BASE
wxMutexGuiLeave()
866 wxAppTraits
* const traits
= wxAppConsoleBase::GetTraitsIfExists();
868 traits
->MutexGuiLeave();
870 #endif // wxUSE_THREADS
872 bool wxAppTraitsBase::ShowAssertDialog(const wxString
& msgOriginal
)
877 #if wxUSE_STACKWALKER
878 const wxString stackTrace
= GetAssertStackTrace();
879 if ( !stackTrace
.empty() )
881 msg
<< wxT("\n\nCall stack:\n") << stackTrace
;
883 wxMessageOutputDebug().Output(msg
);
885 #endif // wxUSE_STACKWALKER
887 return DoShowAssertDialog(msgOriginal
+ msg
);
888 #else // !wxDEBUG_LEVEL
889 wxUnusedVar(msgOriginal
);
892 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
895 #if wxUSE_STACKWALKER
896 wxString
wxAppTraitsBase::GetAssertStackTrace()
900 #if !defined(__WXMSW__)
901 // on Unix stack frame generation may take some time, depending on the
902 // size of the executable mainly... warn the user that we are working
903 wxFprintf(stderr
, "Collecting stack trace information, please wait...");
910 class StackDump
: public wxStackWalker
915 const wxString
& GetStackTrace() const { return m_stackTrace
; }
918 virtual void OnStackFrame(const wxStackFrame
& frame
)
920 m_stackTrace
<< wxString::Format
923 wx_truncate_cast(int, frame
.GetLevel())
926 wxString name
= frame
.GetName();
929 m_stackTrace
<< wxString::Format(wxT("%-40s"), name
.c_str());
933 m_stackTrace
<< wxString::Format(wxT("%p"), frame
.GetAddress());
936 if ( frame
.HasSourceLocation() )
938 m_stackTrace
<< wxT('\t')
939 << frame
.GetFileName()
944 m_stackTrace
<< wxT('\n');
948 wxString m_stackTrace
;
951 // don't show more than maxLines or we could get a dialog too tall to be
952 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
953 // characters it is still only 300 pixels...
954 static const int maxLines
= 20;
957 dump
.Walk(2, maxLines
); // don't show OnAssert() call itself
958 stackTrace
= dump
.GetStackTrace();
960 const int count
= stackTrace
.Freq(wxT('\n'));
961 for ( int i
= 0; i
< count
- maxLines
; i
++ )
962 stackTrace
= stackTrace
.BeforeLast(wxT('\n'));
965 #else // !wxDEBUG_LEVEL
966 // this function is still present for ABI-compatibility even in debug level
967 // 0 build but is not used there and so can simply do nothing
969 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
971 #endif // wxUSE_STACKWALKER
974 // ============================================================================
975 // global functions implementation
976 // ============================================================================
986 // what else can we do?
995 wxTheApp
->WakeUpIdle();
997 //else: do nothing, what can we do?
1000 // wxASSERT() helper
1001 bool wxAssertIsEqual(int x
, int y
)
1008 // break into the debugger
1011 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1013 #elif defined(_MSL_USING_MW_C_HEADERS) && _MSL_USING_MW_C_HEADERS
1015 #elif defined(__UNIX__)
1022 // default assert handler
1024 wxDefaultAssertHandler(const wxString
& file
,
1026 const wxString
& func
,
1027 const wxString
& cond
,
1028 const wxString
& msg
)
1031 static int s_bInAssert
= 0;
1033 wxRecursionGuard
guard(s_bInAssert
);
1034 if ( guard
.IsInside() )
1036 // can't use assert here to avoid infinite loops, so just trap
1044 // by default, show the assert dialog box -- we can't customize this
1046 ShowAssertDialog(file
, line
, func
, cond
, msg
);
1050 // let the app process it as it wants
1051 // FIXME-UTF8: use wc_str(), not c_str(), when ANSI build is removed
1052 wxTheApp
->OnAssertFailure(file
.c_str(), line
, func
.c_str(),
1053 cond
.c_str(), msg
.c_str());
1057 wxAssertHandler_t wxTheAssertHandler
= wxDefaultAssertHandler
;
1059 void wxSetDefaultAssertHandler()
1061 wxTheAssertHandler
= wxDefaultAssertHandler
;
1064 void wxOnAssert(const wxString
& file
,
1066 const wxString
& func
,
1067 const wxString
& cond
,
1068 const wxString
& msg
)
1070 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1073 void wxOnAssert(const wxString
& file
,
1075 const wxString
& func
,
1076 const wxString
& cond
)
1078 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
1081 void wxOnAssert(const wxChar
*file
,
1087 // this is the backwards-compatible version (unless we don't use Unicode)
1088 // so it could be called directly from the user code and this might happen
1089 // even when wxTheAssertHandler is NULL
1091 if ( wxTheAssertHandler
)
1092 #endif // wxUSE_UNICODE
1093 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1096 void wxOnAssert(const char *file
,
1100 const wxString
& msg
)
1102 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1105 void wxOnAssert(const char *file
,
1109 const wxCStrData
& msg
)
1111 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1115 void wxOnAssert(const char *file
,
1120 wxTheAssertHandler(file
, line
, func
, cond
, wxString());
1123 void wxOnAssert(const char *file
,
1129 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1132 void wxOnAssert(const char *file
,
1138 wxTheAssertHandler(file
, line
, func
, cond
, msg
);
1140 #endif // wxUSE_UNICODE
1142 #endif // wxDEBUG_LEVEL
1144 // ============================================================================
1145 // private functions implementation
1146 // ============================================================================
1150 static void LINKAGEMODE
SetTraceMasks()
1154 if ( wxGetEnv(wxT("WXTRACE"), &mask
) )
1156 wxStringTokenizer
tkn(mask
, wxT(",;:"));
1157 while ( tkn
.HasMoreTokens() )
1158 wxLog::AddTraceMask(tkn
.GetNextToken());
1163 #endif // __WXDEBUG__
1168 bool DoShowAssertDialog(const wxString
& msg
)
1170 // under MSW we can show the dialog even in the console mode
1171 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1172 wxString
msgDlg(msg
);
1174 // this message is intentionally not translated -- it is for developers
1175 // only -- and the less code we use here, less is the danger of recursively
1176 // asserting and dying
1177 msgDlg
+= wxT("\nDo you want to stop the program?\n")
1178 wxT("You can also choose [Cancel] to suppress ")
1179 wxT("further warnings.");
1181 switch ( ::MessageBox(NULL
, msgDlg
.wx_str(), wxT("wxWidgets Debug Alert"),
1182 MB_YESNOCANCEL
| MB_ICONSTOP
) )
1192 //case IDNO: nothing to do
1196 #endif // __WXMSW__/!__WXMSW__
1198 // continue with the asserts by default
1202 // show the standard assert dialog
1204 void ShowAssertDialog(const wxString
& file
,
1206 const wxString
& func
,
1207 const wxString
& cond
,
1208 const wxString
& msgUser
,
1209 wxAppTraits
*traits
)
1211 // this variable can be set to true to suppress "assert failure" messages
1212 static bool s_bNoAsserts
= false;
1217 // make life easier for people using VC++ IDE by using this format: like
1218 // this, clicking on the message will take us immediately to the place of
1219 // the failed assert
1220 msg
.Printf(wxT("%s(%d): assert \"%s\" failed"), file
, line
, cond
);
1222 // add the function name, if any
1223 if ( !func
.empty() )
1224 msg
<< wxT(" in ") << func
<< wxT("()");
1226 // and the message itself
1227 if ( !msgUser
.empty() )
1229 msg
<< wxT(": ") << msgUser
;
1231 else // no message given
1237 // if we are not in the main thread, output the assert directly and trap
1238 // since dialogs cannot be displayed
1239 if ( !wxThread::IsMain() )
1241 msg
+= wxString::Format(" [in thread %lx]", wxThread::GetCurrentId());
1243 #endif // wxUSE_THREADS
1245 // log the assert in any case
1246 wxMessageOutputDebug().Output(msg
);
1248 if ( !s_bNoAsserts
)
1252 // delegate showing assert dialog (if possible) to that class
1253 s_bNoAsserts
= traits
->ShowAssertDialog(msg
);
1255 else // no traits object
1257 // fall back to the function of last resort
1258 s_bNoAsserts
= DoShowAssertDialog(msg
);
1263 #endif // wxDEBUG_LEVEL