diff --git a/include/wx/clntdata.h b/include/wx/clntdata.h index 02e90d267c..8f2d44ded5 100644 --- a/include/wx/clntdata.h +++ b/include/wx/clntdata.h @@ -50,7 +50,7 @@ private: - +#if 0 // This class is a mixin that provides storage and management of "client // data." The client data stored can either be a pointer to a wxClientData // object in which case it is managed by the container (i.e. it will delete @@ -73,7 +73,7 @@ protected: // The user data: either an object which will be deleted by the container // when it's deleted or some raw pointer which we do nothing with - only // one type of data can be used with the given window (i.e. you cannot set - // the void data and then associate the window with wxClientData or vice + // the void data and then associate the container with wxClientData or vice // versa) union { @@ -92,7 +92,7 @@ protected: wxClientDataType m_clientDataType; }; - +#endif // ---------------------------------------------------------------------------- #endif diff --git a/include/wx/event.h b/include/wx/event.h index 9427bc81f9..760e675e55 100644 --- a/include/wx/event.h +++ b/include/wx/event.h @@ -1653,7 +1653,7 @@ struct WXDLLEXPORT wxEventTable // wxEvtHandler: the base class for all objects handling wxWindows events // ---------------------------------------------------------------------------- -class WXDLLEXPORT wxEvtHandler : public wxObject, public wxClientDataContainer +class WXDLLEXPORT wxEvtHandler : public wxObject { public: wxEvtHandler(); @@ -1703,6 +1703,15 @@ public: wxObject *userData = (wxObject *) NULL ) { return Disconnect(id, -1, eventType, func, userData); } + + // User data can be associated with each wxEvtHandler + void SetClientObject( wxClientData *data ) { DoSetClientObject(data); } + wxClientData *GetClientObject() const { return DoGetClientObject(); } + + void SetClientData( void *data ) { DoSetClientData(data); } + void *GetClientData() const { return DoGetClientData(); } + + // implementation from now on virtual bool SearchEventTable(wxEventTable& table, wxEvent& event); bool SearchDynamicEventTable( wxEvent& event ); @@ -1763,6 +1772,29 @@ protected: // Is event handler enabled? bool m_enabled; + + // The user data: either an object which will be deleted by the container + // when it's deleted or some raw pointer which we do nothing with - only + // one type of data can be used with the given window (i.e. you cannot set + // the void data and then associate the container with wxClientData or vice + // versa) + union + { + wxClientData *m_clientObject; + void *m_clientData; + }; + + // what kind of data do we have? + wxClientDataType m_clientDataType; + + // client data accessors + virtual void DoSetClientObject( wxClientData *data ); + virtual wxClientData *DoGetClientObject() const; + + virtual void DoSetClientData( void *data ); + virtual void *DoGetClientData() const; + + private: DECLARE_DYNAMIC_CLASS(wxEvtHandler) }; diff --git a/src/common/clntdata.cpp b/src/common/clntdata.cpp index 782f518465..d7a984d672 100644 --- a/src/common/clntdata.cpp +++ b/src/common/clntdata.cpp @@ -24,7 +24,7 @@ // ---------------------------------------------------------------------------- - +#if 0 wxClientDataContainer::wxClientDataContainer() { @@ -81,7 +81,7 @@ void *wxClientDataContainer::DoGetClientData() const return m_clientData; } - +#endif // ---------------------------------------------------------------------------- diff --git a/src/common/event.cpp b/src/common/event.cpp index 02c63cc4fa..f75175f165 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -761,6 +761,9 @@ wxEvtHandler::wxEvtHandler() m_eventsLocker = new wxCriticalSection; # endif #endif + // no client data (yet) + m_clientData = NULL; + m_clientDataType = wxClientData_None; } wxEvtHandler::~wxEvtHandler() @@ -798,6 +801,10 @@ wxEvtHandler::~wxEvtHandler() delete m_eventsLocker; # endif #endif + + // we only delete object data, not untyped + if ( m_clientDataType == wxClientData_Object ) + delete m_clientObject; } #if wxUSE_THREADS @@ -1171,6 +1178,48 @@ bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event ) return FALSE; }; +void wxEvtHandler::DoSetClientObject( wxClientData *data ) +{ + wxASSERT_MSG( m_clientDataType != wxClientData_Void, + wxT("can't have both object and void client data") ); + + if ( m_clientObject ) + delete m_clientObject; + + m_clientObject = data; + m_clientDataType = wxClientData_Object; +} + +wxClientData *wxEvtHandler::DoGetClientObject() const +{ + // it's not an error to call GetClientObject() on a window which doesn't + // have client data at all - NULL will be returned + wxASSERT_MSG( m_clientDataType != wxClientData_Void, + wxT("this window doesn't have object client data") ); + + return m_clientObject; +} + +void wxEvtHandler::DoSetClientData( void *data ) +{ + wxASSERT_MSG( m_clientDataType != wxClientData_Object, + wxT("can't have both object and void client data") ); + + m_clientData = data; + m_clientDataType = wxClientData_Void; +} + +void *wxEvtHandler::DoGetClientData() const +{ + // it's not an error to call GetClientData() on a window which doesn't have + // client data at all - NULL will be returned + wxASSERT_MSG( m_clientDataType != wxClientData_Object, + wxT("this window doesn't have void client data") ); + + return m_clientData; +} + + #if WXWIN_COMPATIBILITY bool wxEvtHandler::OnClose() {