X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/26eef30437813056c86e5c7516995ff05e65cae3..3b241537efde9ed3b5b6c1fb0756f00517e0bbd9:/src/common/appbase.cpp?ds=inline diff --git a/src/common/appbase.cpp b/src/common/appbase.cpp index 076cabb60f..a3caf25a2b 100644 --- a/src/common/appbase.cpp +++ b/src/common/appbase.cpp @@ -42,7 +42,7 @@ #include "wx/evtloop.h" #include "wx/filename.h" #include "wx/msgout.h" -#include "wx/ptr_scpd.h" +#include "wx/scopedptr.h" #include "wx/tokenzr.h" #include "wx/thread.h" @@ -63,7 +63,7 @@ #include "wx/fontmap.h" #endif // wxUSE_FONTMAP -#ifdef __WXDEBUG__ +#if wxDEBUG_LEVEL #if wxUSE_STACKWALKER #include "wx/stackwalk.h" #ifdef __WXMSW__ @@ -72,7 +72,7 @@ #endif // wxUSE_STACKWALKER #include "wx/recguard.h" -#endif // __WXDEBUG__ +#endif // wxDEBUG_LEVEL // wxABI_VERSION can be defined when compiling applications but it should be // left undefined when compiling the library itself, it is then set to its @@ -85,20 +85,22 @@ // private functions prototypes // ---------------------------------------------------------------------------- -#ifdef __WXDEBUG__ +#if wxDEBUG_LEVEL // really just show the assert dialog static bool DoShowAssertDialog(const wxString& msg); // prepare for showing the assert dialog, use the given traits or // DoShowAssertDialog() as last fallback to really show it static - void ShowAssertDialog(const wxString& szFile, - int nLine, - const wxString& szFunc, - const wxString& szCond, - const wxString& szMsg, + void ShowAssertDialog(const wxString& file, + int line, + const wxString& func, + const wxString& cond, + const wxString& msg, wxAppTraits *traits = NULL); +#endif // wxDEBUG_LEVEL +#ifdef __WXDEBUG__ // turn on the trace masks specified in the env variable WXTRACE static void LINKAGEMODE SetTraceMasks(); #endif // __WXDEBUG__ @@ -111,6 +113,8 @@ wxAppConsole *wxAppConsoleBase::ms_appInstance = NULL; wxAppInitializerFunction wxAppConsoleBase::ms_appInitFn = NULL; +wxSocketManager *wxAppTraitsBase::ms_manager = NULL; + // ---------------------------------------------------------------------------- // wxEventLoopPtr // ---------------------------------------------------------------------------- @@ -130,8 +134,9 @@ wxAppConsoleBase::wxAppConsoleBase() { m_traits = NULL; m_mainLoop = NULL; + m_bDoPendingEventProcessing = true; - ms_appInstance = wx_static_cast(wxAppConsole *, this); + ms_appInstance = static_cast(this); #ifdef __WXDEBUG__ SetTraceMasks(); @@ -151,7 +156,7 @@ wxAppConsoleBase::~wxAppConsoleBase() } // ---------------------------------------------------------------------------- -// initilization/cleanup +// initialization/cleanup // ---------------------------------------------------------------------------- bool wxAppConsoleBase::Initialize(int& WXUNUSED(argc), wxChar **argv) @@ -160,10 +165,6 @@ bool wxAppConsoleBase::Initialize(int& WXUNUSED(argc), wxChar **argv) GetTraits()->SetLocale(); #endif // wxUSE_INTL -#if wxUSE_THREADS - wxPendingEventsLocker = new wxCriticalSection; -#endif - #ifndef __WXPALMOS__ if ( m_appName.empty() && argv && argv[0] ) { @@ -187,14 +188,6 @@ void wxAppConsoleBase::CleanUp() delete m_mainLoop; m_mainLoop = NULL; } - - delete wxPendingEvents; - wxPendingEvents = NULL; - -#if wxUSE_THREADS - delete wxPendingEventsLocker; - wxPendingEventsLocker = NULL; -#endif // wxUSE_THREADS } // ---------------------------------------------------------------------------- @@ -241,7 +234,7 @@ int wxAppConsoleBase::OnExit() #if wxUSE_CONFIG // delete the config object if any (don't use Get() here, but Set() // because Get() could create a new config object) - delete wxConfigBase::Set((wxConfigBase *) NULL); + delete wxConfigBase::Set(NULL); #endif // wxUSE_CONFIG return 0; @@ -285,7 +278,7 @@ wxAppTraits *wxAppConsoleBase::GetTraitsIfExists() } // ---------------------------------------------------------------------------- -// event processing +// wxEventLoop redirection // ---------------------------------------------------------------------------- int wxAppConsoleBase::MainLoop() @@ -323,17 +316,30 @@ bool wxAppConsoleBase::Dispatch() return loop && loop->Dispatch(); } -bool wxAppConsoleBase::HasPendingEvents() const +bool wxAppConsoleBase::Yield(bool onlyIfNeeded) { - wxENTER_CRIT_SECT( *wxPendingEventsLocker ); + wxEventLoopBase * const loop = wxEventLoopBase::GetActive(); - bool has = wxPendingEvents && !wxPendingEvents->IsEmpty(); + return loop && loop->Yield(onlyIfNeeded); +} - wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); +void wxAppConsoleBase::WakeUpIdle() +{ + if ( m_mainLoop ) + m_mainLoop->WakeUp(); +} - return has; +bool wxAppConsoleBase::ProcessIdle() +{ + wxEventLoopBase * const loop = wxEventLoopBase::GetActive(); + + return loop && loop->ProcessIdle(); } +// ---------------------------------------------------------------------------- +// events +// ---------------------------------------------------------------------------- + /* static */ bool wxAppConsoleBase::IsMainLoopRunning() { @@ -342,58 +348,138 @@ bool wxAppConsoleBase::IsMainLoopRunning() return app && app->m_mainLoop != NULL; } -void wxAppConsoleBase::ProcessPendingEvents() +int wxAppConsoleBase::FilterEvent(wxEvent& WXUNUSED(event)) { -#if wxUSE_THREADS - if ( !wxPendingEventsLocker ) - return; -#endif + // process the events normally by default + return -1; +} - wxENTER_CRIT_SECT( *wxPendingEventsLocker ); +void wxAppConsoleBase::DelayPendingEventHandler(wxEvtHandler* toDelay) +{ + wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker); + + // move the handler from the list of handlers with processable pending events + // to the list of handlers with pending events which needs to be processed later + m_handlersWithPendingEvents.Remove(toDelay); + + if (m_handlersWithPendingDelayedEvents.Index(toDelay) == wxNOT_FOUND) + m_handlersWithPendingDelayedEvents.Add(toDelay); + + wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker); +} + +void wxAppConsoleBase::RemovePendingEventHandler(wxEvtHandler* toRemove) +{ + wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker); - if (wxPendingEvents) + if (m_handlersWithPendingEvents.Index(toRemove) != wxNOT_FOUND) { - // iterate until the list becomes empty: the handlers remove themselves - // from it when they don't have any more pending events - wxList::compatibility_iterator node = wxPendingEvents->GetFirst(); - while (node) - { - // In ProcessPendingEvents(), new handlers might be add - // and we can safely leave the critical section here. - wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); + m_handlersWithPendingEvents.Remove(toRemove); - wxEvtHandler *handler = (wxEvtHandler *)node->GetData(); - handler->ProcessPendingEvents(); + // check that the handler was present only once in the list + wxASSERT_MSG( m_handlersWithPendingEvents.Index(toRemove) == wxNOT_FOUND, + "Handler occurs twice in the m_handlersWithPendingEvents list!" ); + } + //else: it wasn't in this list at all, it's ok - wxENTER_CRIT_SECT( *wxPendingEventsLocker ); + if (m_handlersWithPendingDelayedEvents.Index(toRemove) != wxNOT_FOUND) + { + m_handlersWithPendingDelayedEvents.Remove(toRemove); - // restart as the iterators could have been invalidated - node = wxPendingEvents->GetFirst(); - } + // check that the handler was present only once in the list + wxASSERT_MSG( m_handlersWithPendingDelayedEvents.Index(toRemove) == wxNOT_FOUND, + "Handler occurs twice in m_handlersWithPendingDelayedEvents list!" ); } + //else: it wasn't in this list at all, it's ok - wxLEAVE_CRIT_SECT( *wxPendingEventsLocker ); + wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker); } -void wxAppConsoleBase::WakeUpIdle() +void wxAppConsoleBase::AppendPendingEventHandler(wxEvtHandler* toAppend) { - if ( m_mainLoop ) - m_mainLoop->WakeUp(); + wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker); + + if ( m_handlersWithPendingEvents.Index(toAppend) == wxNOT_FOUND ) + m_handlersWithPendingEvents.Add(toAppend); + + wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker); } -bool wxAppConsoleBase::ProcessIdle() +bool wxAppConsoleBase::HasPendingEvents() const +{ + wxENTER_CRIT_SECT(const_cast(this)->m_handlersWithPendingEventsLocker); + + bool has = !m_handlersWithPendingEvents.IsEmpty(); + + wxLEAVE_CRIT_SECT(const_cast(this)->m_handlersWithPendingEventsLocker); + + return has; +} + +void wxAppConsoleBase::SuspendProcessingOfPendingEvents() { - wxIdleEvent event; + m_bDoPendingEventProcessing = false; +} - event.SetEventObject(this); - ProcessEvent(event); - return event.MoreRequested(); +void wxAppConsoleBase::ResumeProcessingOfPendingEvents() +{ + m_bDoPendingEventProcessing = true; } -int wxAppConsoleBase::FilterEvent(wxEvent& WXUNUSED(event)) +void wxAppConsoleBase::ProcessPendingEvents() { - // process the events normally by default - return -1; + if (!m_bDoPendingEventProcessing) + return; + + wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker); + + wxCHECK_RET( m_handlersWithPendingDelayedEvents.IsEmpty(), + "this helper list should be empty" ); + + // iterate until the list becomes empty: the handlers remove themselves + // from it when they don't have any more pending events + while (!m_handlersWithPendingEvents.IsEmpty()) + { + // In ProcessPendingEvents(), new handlers might be added + // and we can safely leave the critical section here. + wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker); + + // NOTE: we always call ProcessPendingEvents() on the first event handler + // with pending events because handlers auto-remove themselves + // from this list (see RemovePendingEventHandler) if they have no + // more pending events. + m_handlersWithPendingEvents[0]->ProcessPendingEvents(); + + wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker); + } + + // now the wxHandlersWithPendingEvents is surely empty; however some event + // handlers may have moved themselves into wxHandlersWithPendingDelayedEvents + // because of a selective wxYield call in progress. + // Now we need to move them back to wxHandlersWithPendingEvents so the next + // call to this function has the chance of processing them: + if (!m_handlersWithPendingDelayedEvents.IsEmpty()) + { + WX_APPEND_ARRAY(m_handlersWithPendingEvents, m_handlersWithPendingDelayedEvents); + m_handlersWithPendingDelayedEvents.Clear(); + } + + wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker); +} + +void wxAppConsoleBase::DeletePendingEvents() +{ + wxENTER_CRIT_SECT(m_handlersWithPendingEventsLocker); + + wxCHECK_RET( m_handlersWithPendingDelayedEvents.IsEmpty(), + "this helper list should be empty" ); + + for (unsigned int i=0; iDeletePendingEvents(); + + m_handlersWithPendingEvents.Clear(); + + wxLEAVE_CRIT_SECT(m_handlersWithPendingEventsLocker); } // ---------------------------------------------------------------------------- @@ -411,6 +497,20 @@ wxAppConsoleBase::HandleEvent(wxEvtHandler *handler, (handler->*func)(event); } +void wxAppConsoleBase::CallEventHandler(wxEvtHandler *handler, + wxEventFunctor& functor, + wxEvent& event) const +{ + // If the functor holds a method then, for backward compatibility, call + // HandleEvent(): + wxEventFunction eventFunction = functor.GetMethod(); + + if ( eventFunction ) + HandleEvent(handler, eventFunction, event); + else + functor(handler, event); +} + void wxAppConsoleBase::OnUnhandledException() { #ifdef __WXDEBUG__ @@ -553,20 +653,28 @@ bool wxAppConsoleBase::CheckBuildOptions(const char *optionsSignature, // normally wxLogFatalError doesn't return return false; } -#undef wxCMP return true; } -#ifdef __WXDEBUG__ - void wxAppConsoleBase::OnAssertFailure(const wxChar *file, int line, const wxChar *func, const wxChar *cond, const wxChar *msg) { +#if wxDEBUG_LEVEL ShowAssertDialog(file, line, func, cond, msg, GetTraits()); +#else + // this function is still present even in debug level 0 build for ABI + // compatibility reasons but is never called there and so can simply do + // nothing in it + wxUnusedVar(file); + wxUnusedVar(line); + wxUnusedVar(func); + wxUnusedVar(cond); + wxUnusedVar(msg); +#endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL } void wxAppConsoleBase::OnAssert(const wxChar *file, @@ -577,8 +685,6 @@ void wxAppConsoleBase::OnAssert(const wxChar *file, OnAssertFailure(file, line, NULL, cond, msg); } -#endif // __WXDEBUG__ - // ============================================================================ // other classes implementations // ============================================================================ @@ -616,12 +722,10 @@ wxRendererNative *wxConsoleAppTraitsBase::CreateRenderer() return NULL; } -#ifdef __WXDEBUG__ bool wxConsoleAppTraitsBase::ShowAssertDialog(const wxString& msg) { return wxAppTraitsBase::ShowAssertDialog(msg); } -#endif bool wxConsoleAppTraitsBase::HasStderr() { @@ -680,10 +784,9 @@ void WXDLLIMPEXP_BASE wxMutexGuiLeave() } #endif // wxUSE_THREADS -#ifdef __WXDEBUG__ - bool wxAppTraitsBase::ShowAssertDialog(const wxString& msgOriginal) { +#if wxDEBUG_LEVEL wxString msg = msgOriginal; #if wxUSE_STACKWALKER @@ -700,11 +803,17 @@ bool wxAppTraitsBase::ShowAssertDialog(const wxString& msgOriginal) #endif // wxUSE_STACKWALKER return DoShowAssertDialog(msg); +#else // !wxDEBUG_LEVEL + wxUnusedVar(msgOriginal); + + return false; +#endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL } #if wxUSE_STACKWALKER wxString wxAppTraitsBase::GetAssertStackTrace() { +#if wxDEBUG_LEVEL wxString stackTrace; class StackDump : public wxStackWalker @@ -762,12 +871,15 @@ wxString wxAppTraitsBase::GetAssertStackTrace() stackTrace = stackTrace.BeforeLast(wxT('\n')); return stackTrace; +#else // !wxDEBUG_LEVEL + // this function is still present for ABI-compatibility even in debug level + // 0 build but is not used there and so can simply do nothing + return wxString(); +#endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL } #endif // wxUSE_STACKWALKER -#endif // __WXDEBUG__ - // ============================================================================ // global functions implementation // ============================================================================ @@ -794,14 +906,14 @@ void wxWakeUpIdle() //else: do nothing, what can we do? } -#ifdef __WXDEBUG__ - // wxASSERT() helper bool wxAssertIsEqual(int x, int y) { return x == y; } +#if wxDEBUG_LEVEL + // break into the debugger void wxTrap() { @@ -816,12 +928,13 @@ void wxTrap() #endif // Win/Unix } -// this function is called when an assert fails -static void wxDoOnAssert(const wxString& szFile, - int nLine, - const wxString& szFunc, - const wxString& szCond, - const wxString& szMsg = wxEmptyString) +// default assert handler +static void +wxDefaultAssertHandler(const wxString& file, + int line, + const wxString& func, + const wxString& cond, + const wxString& msg) { // FIXME MT-unsafe static int s_bInAssert = 0; @@ -839,90 +952,98 @@ static void wxDoOnAssert(const wxString& szFile, { // by default, show the assert dialog box -- we can't customize this // behaviour - ShowAssertDialog(szFile, nLine, szFunc, szCond, szMsg); + ShowAssertDialog(file, line, func, cond, msg); } else { // let the app process it as it wants // FIXME-UTF8: use wc_str(), not c_str(), when ANSI build is removed - wxTheApp->OnAssertFailure(szFile.c_str(), nLine, szFunc.c_str(), - szCond.c_str(), szMsg.c_str()); + wxTheApp->OnAssertFailure(file.c_str(), line, func.c_str(), + cond.c_str(), msg.c_str()); } } -void wxOnAssert(const wxString& szFile, - int nLine, - const wxString& szFunc, - const wxString& szCond, - const wxString& szMsg) +wxAssertHandler_t wxTheAssertHandler = wxDefaultAssertHandler; + +void wxOnAssert(const wxString& file, + int line, + const wxString& func, + const wxString& cond, + const wxString& msg) { - wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg); + wxTheAssertHandler(file, line, func, cond, msg); } -void wxOnAssert(const wxString& szFile, - int nLine, - const wxString& szFunc, - const wxString& szCond) +void wxOnAssert(const wxString& file, + int line, + const wxString& func, + const wxString& cond) { - wxDoOnAssert(szFile, nLine, szFunc, szCond); + wxTheAssertHandler(file, line, func, cond, wxString()); } -void wxOnAssert(const wxChar *szFile, - int nLine, - const char *szFunc, - const wxChar *szCond, - const wxChar *szMsg) +void wxOnAssert(const wxChar *file, + int line, + const char *func, + const wxChar *cond, + const wxChar *msg) { - wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg); + // this is the backwards-compatible version (unless we don't use Unicode) + // so it could be called directly from the user code and this might happen + // even when wxTheAssertHandler is NULL +#if wxUSE_UNICODE + if ( wxTheAssertHandler ) +#endif // wxUSE_UNICODE + wxTheAssertHandler(file, line, func, cond, msg); } -void wxOnAssert(const char *szFile, - int nLine, - const char *szFunc, - const char *szCond, - const wxString& szMsg) +void wxOnAssert(const char *file, + int line, + const char *func, + const char *cond, + const wxString& msg) { - wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg); + wxTheAssertHandler(file, line, func, cond, msg); } -void wxOnAssert(const char *szFile, - int nLine, - const char *szFunc, - const char *szCond, +void wxOnAssert(const char *file, + int line, + const char *func, + const char *cond, const wxCStrData& msg) { - wxDoOnAssert(szFile, nLine, szFunc, szCond, msg); + wxTheAssertHandler(file, line, func, cond, msg); } #if wxUSE_UNICODE -void wxOnAssert(const char *szFile, - int nLine, - const char *szFunc, - const char *szCond) +void wxOnAssert(const char *file, + int line, + const char *func, + const char *cond) { - wxDoOnAssert(szFile, nLine, szFunc, szCond); + wxTheAssertHandler(file, line, func, cond, wxString()); } -void wxOnAssert(const char *szFile, - int nLine, - const char *szFunc, - const char *szCond, - const char *szMsg) +void wxOnAssert(const char *file, + int line, + const char *func, + const char *cond, + const char *msg) { - wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg); + wxTheAssertHandler(file, line, func, cond, msg); } -void wxOnAssert(const char *szFile, - int nLine, - const char *szFunc, - const char *szCond, - const wxChar *szMsg) +void wxOnAssert(const char *file, + int line, + const char *func, + const char *cond, + const wxChar *msg) { - wxDoOnAssert(szFile, nLine, szFunc, szCond, szMsg); + wxTheAssertHandler(file, line, func, cond, msg); } #endif // wxUSE_UNICODE -#endif // __WXDEBUG__ +#endif // wxDEBUG_LEVEL // ============================================================================ // private functions implementation @@ -943,6 +1064,10 @@ static void LINKAGEMODE SetTraceMasks() #endif // wxUSE_LOG } +#endif // __WXDEBUG__ + +#if wxDEBUG_LEVEL + static bool DoShowAssertDialog(const wxString& msg) { @@ -950,8 +1075,9 @@ bool DoShowAssertDialog(const wxString& msg) #if defined(__WXMSW__) && !defined(__WXMICROWIN__) wxString msgDlg(msg); - // this message is intentionally not translated -- it is for - // developpers only + // this message is intentionally not translated -- it is for developers + // only -- and the less code we use here, less is the danger of recursively + // asserting and dying msgDlg += wxT("\nDo you want to stop the program?\n") wxT("You can also choose [Cancel] to suppress ") wxT("further warnings."); @@ -981,13 +1107,13 @@ bool DoShowAssertDialog(const wxString& msg) return false; } -// show the assert modal dialog +// show the standard assert dialog static -void ShowAssertDialog(const wxString& szFile, - int nLine, - const wxString& szFunc, - const wxString& szCond, - const wxString& szMsg, +void ShowAssertDialog(const wxString& file, + int line, + const wxString& func, + const wxString& cond, + const wxString& msgUser, wxAppTraits *traits) { // this variable can be set to true to suppress "assert failure" messages @@ -999,16 +1125,16 @@ void ShowAssertDialog(const wxString& szFile, // make life easier for people using VC++ IDE by using this format: like // this, clicking on the message will take us immediately to the place of // the failed assert - msg.Printf(wxT("%s(%d): assert \"%s\" failed"), szFile, nLine, szCond); + msg.Printf(wxT("%s(%d): assert \"%s\" failed"), file, line, cond); // add the function name, if any - if ( !szFunc.empty() ) - msg << _T(" in ") << szFunc << _T("()"); + if ( !func.empty() ) + msg << _T(" in ") << func << _T("()"); // and the message itself - if ( !szMsg.empty() ) + if ( !msgUser.empty() ) { - msg << _T(": ") << szMsg; + msg << _T(": ") << msgUser; } else // no message given { @@ -1054,4 +1180,4 @@ void ShowAssertDialog(const wxString& szFile, } } -#endif // __WXDEBUG__ +#endif // wxDEBUG_LEVEL