X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/19c4d91638c2546630c4427e39c02f5380a8bb87..44580804c2b478ebd36f59c9b416be560c4ef5bf:/src/common/event.cpp?ds=sidebyside diff --git a/src/common/event.cpp b/src/common/event.cpp index e5d4884bba..d122c8d434 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -45,6 +45,13 @@ #include "wx/thread.h" +#if wxUSE_BASE + #include "wx/ptr_scpd.h" + + wxDECLARE_SCOPED_PTR(wxEvent, wxEventPtr) + wxDEFINE_SCOPED_PTR(wxEvent, wxEventPtr) +#endif // wxUSE_BASE + // ---------------------------------------------------------------------------- // wxWin macros // ---------------------------------------------------------------------------- @@ -406,13 +413,15 @@ wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId) wxString wxCommandEvent::GetString() const { - if(m_eventType != wxEVT_COMMAND_TEXT_UPDATED || !m_eventObject) + if (m_eventType != wxEVT_COMMAND_TEXT_UPDATED || !m_eventObject) + { return m_cmdString; + } else { #if wxUSE_TEXTCTRL wxTextCtrl *txt = wxDynamicCast(m_eventObject, wxTextCtrl); - if(txt) + if ( txt ) return txt->GetValue(); else #endif // wxUSE_TEXTCTRL @@ -1012,7 +1021,6 @@ void wxEventHashTable::GrowEventTypeTable() delete[] oldEventTypeTable; } - // ---------------------------------------------------------------------------- // wxEvtHandler // ---------------------------------------------------------------------------- @@ -1028,11 +1036,6 @@ wxEvtHandler::wxEvtHandler() m_enabled = true; m_dynamicEvents = (wxList *) NULL; m_pendingEvents = (wxList *) NULL; -#if wxUSE_THREADS -# if !defined(__VISAGECPP__) - m_eventsLocker = new wxCriticalSection; -# endif -#endif // no client data (yet) m_clientData = NULL; @@ -1057,6 +1060,20 @@ wxEvtHandler::~wxEvtHandler() { wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)*it; + // Remove ourselves from sink destructor notifications + // (this has usually been been done, in wxTrackable destructor) + wxEvtHandler *eventSink = entry->m_eventSink; + if ( eventSink ) + { + wxEventConnectionRef * const + evtConnRef = FindRefInTrackerList(eventSink); + if ( evtConnRef ) + { + eventSink->RemoveNode(evtConnRef); + delete evtConnRef; + } + } + if (entry->m_callbackUserData) delete entry->m_callbackUserData; delete entry; @@ -1068,15 +1085,13 @@ wxEvtHandler::~wxEvtHandler() m_pendingEvents->DeleteContents(true); delete m_pendingEvents; -# if !defined(__VISAGECPP__) - delete m_eventsLocker; -# endif - // Remove us from wxPendingEvents if necessary. if ( wxPendingEvents ) { - if(wxPendingEventsLocker) +#if wxUSE_THREADS + if (wxPendingEventsLocker) wxENTER_CRIT_SECT(*wxPendingEventsLocker); +#endif if ( wxPendingEvents->DeleteObject(this) ) { @@ -1086,8 +1101,10 @@ wxEvtHandler::~wxEvtHandler() } //else: we weren't in this list at all, it's ok - if(wxPendingEventsLocker) +#if wxUSE_THREADS + if (wxPendingEventsLocker) wxLEAVE_CRIT_SECT(*wxPendingEventsLocker); +#endif } // we only delete object data, not untyped @@ -1108,14 +1125,6 @@ bool wxEvtHandler::ProcessThreadEvent(const wxEvent& event) return true; } -void wxEvtHandler::ClearEventLocker() -{ -#if !defined(__VISAGECPP__) - delete m_eventsLocker; - m_eventsLocker = NULL; -#endif -} - #endif // wxUSE_THREADS void wxEvtHandler::AddPendingEvent(const wxEvent& event) @@ -1129,14 +1138,14 @@ void wxEvtHandler::AddPendingEvent(const wxEvent& event) wxCHECK_RET( eventCopy, _T("events of this type aren't supposed to be posted") ); - wxENTER_CRIT_SECT( Lock() ); + wxENTER_CRIT_SECT( m_pendingEventsLock ); if ( !m_pendingEvents ) m_pendingEvents = new wxList; m_pendingEvents->Append(eventCopy); - wxLEAVE_CRIT_SECT( Lock() ); + wxLEAVE_CRIT_SECT( m_pendingEventsLock ); // 2) Add this event handler to list of event handlers that // have pending events. @@ -1157,7 +1166,7 @@ void wxEvtHandler::AddPendingEvent(const wxEvent& event) void wxEvtHandler::ProcessPendingEvents() { - wxENTER_CRIT_SECT( Lock() ); + wxENTER_CRIT_SECT( m_pendingEventsLock ); // this method is only called by wxApp if this handler does have // pending events @@ -1165,7 +1174,7 @@ void wxEvtHandler::ProcessPendingEvents() "should have pending events if called" ); wxList::compatibility_iterator node = m_pendingEvents->GetFirst(); - wxEvent * const event = wx_static_cast(wxEvent *, node->GetData()); + wxEventPtr event(wx_static_cast(wxEvent *, node->GetData())); // it's important we remove event from list before processing it, else a // nested event loop, for example from a modal dialog, might process the @@ -1177,15 +1186,13 @@ void wxEvtHandler::ProcessPendingEvents() if ( m_pendingEvents->IsEmpty() ) wxPendingEvents->DeleteObject(this); - wxLEAVE_CRIT_SECT( Lock() ); + wxLEAVE_CRIT_SECT( m_pendingEventsLock ); ProcessEvent(*event); // careful: this object could have been deleted by the event handler // executed by the above ProcessEvent() call, so we can't access any fields // of this object any more - - delete event; } /* @@ -1364,6 +1371,16 @@ void wxEvtHandler::Connect( int id, int lastId, // Insert at the front of the list so most recent additions are found first m_dynamicEvents->Insert( (wxObject*) entry ); + + // Make sure we get to know when a sink is destroyed + if ( eventSink ) + { + wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink); + if ( evtConnRef ) + evtConnRef->IncRef( ); + else + evtConnRef = new wxEventConnectionRef(this, eventSink); + } } bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType, @@ -1374,6 +1391,14 @@ bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType, if (!m_dynamicEvents) return false; + // Remove connection from tracker node (wxEventConnectionRef) + if ( eventSink ) + { + wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink); + if ( evtConnRef ) + evtConnRef->DecRef(); + } + wxList::compatibility_iterator node = m_dynamicEvents->GetFirst(); while (node) { @@ -1464,6 +1489,46 @@ void *wxEvtHandler::DoGetClientData() const return m_clientData; } +// A helper to find an wxEventConnectionRef object +wxEventConnectionRef * +wxEvtHandler::FindRefInTrackerList(wxEvtHandler *eventSink) +{ + for ( wxTrackerNode *node = eventSink->GetFirst(); node; node = node->m_nxt ) + { + // we only want wxEventConnectionRef nodes here + wxEventConnectionRef *evtConnRef = node->ToEventConnection(); + if ( evtConnRef && evtConnRef->m_src == this ) + { + wxASSERT( evtConnRef->m_sink==eventSink ); + return evtConnRef; + } + } + + return NULL; +} + +void wxEvtHandler::OnSinkDestroyed( wxEvtHandler *sink ) +{ + wxASSERT(m_dynamicEvents); + + // remove all connections with this sink + wxList::compatibility_iterator node = m_dynamicEvents->GetFirst(), node_nxt; + while (node) + { + wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData(); + node_nxt = node->GetNext(); + + if ( entry->m_eventSink==sink ) + { + if (entry->m_callbackUserData) + delete entry->m_callbackUserData; + m_dynamicEvents->Erase( node ); + delete entry; + } + node = node_nxt; + } +} + #endif // wxUSE_BASE #if wxUSE_GUI