#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
// ----------------------------------------------------------------------------
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
delete[] oldEventTypeTable;
}
-
// ----------------------------------------------------------------------------
// wxEvtHandler
// ----------------------------------------------------------------------------
{
wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)*it;
+#if wxUSE_WEAKREF
+ // 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;
+ }
+ }
+#endif // wxUSE_WEAKREF
+
if (entry->m_callbackUserData)
delete entry->m_callbackUserData;
delete entry;
// Remove us from wxPendingEvents if necessary.
if ( wxPendingEvents )
{
- if(wxPendingEventsLocker)
+#if wxUSE_THREADS
+ if (wxPendingEventsLocker)
wxENTER_CRIT_SECT(*wxPendingEventsLocker);
+#endif
if ( wxPendingEvents->DeleteObject(this) )
{
}
//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
"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
// 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;
}
/*
// Insert at the front of the list so most recent additions are found first
m_dynamicEvents->Insert( (wxObject*) entry );
+
+#if wxUSE_WEAKREF
+ // Make sure we get to know when a sink is destroyed
+ if ( eventSink && eventSink != this )
+ {
+ wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink);
+ if ( evtConnRef )
+ evtConnRef->IncRef( );
+ else
+ evtConnRef = new wxEventConnectionRef(this, eventSink);
+ }
+#endif // wxUSE_WEAKREF
}
bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType,
if (!m_dynamicEvents)
return false;
+#if wxUSE_WEAKREF
+ // Remove connection from tracker node (wxEventConnectionRef)
+ if ( eventSink && eventSink != this )
+ {
+ wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink);
+ if ( evtConnRef )
+ evtConnRef->DecRef();
+ }
+#endif // wxUSE_WEAKREF
+
wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
while (node)
{
return m_clientData;
}
+#if wxUSE_WEAKREF
+// 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_WEAKREF
+
#endif // wxUSE_BASE
#if wxUSE_GUI