| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: wx/clntdata.h |
| 3 | // Purpose: A mixin class for holding a wxClientData or void pointer |
| 4 | // Author: Robin Dunn |
| 5 | // Modified by: |
| 6 | // Created: 9-Oct-2001 |
| 7 | // Copyright: (c) wxWidgets team |
| 8 | // Licence: wxWindows licence |
| 9 | ///////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | #ifndef _WX_CLNTDATAH__ |
| 12 | #define _WX_CLNTDATAH__ |
| 13 | |
| 14 | #include "wx/defs.h" |
| 15 | #include "wx/string.h" |
| 16 | #include "wx/hashmap.h" |
| 17 | |
| 18 | typedef int (*wxShadowObjectMethod)(void*, void*); |
| 19 | WX_DECLARE_STRING_HASH_MAP_WITH_DECL( |
| 20 | wxShadowObjectMethod, |
| 21 | wxShadowObjectMethods, |
| 22 | class WXDLLIMPEXP_BASE |
| 23 | ); |
| 24 | WX_DECLARE_STRING_HASH_MAP_WITH_DECL( |
| 25 | void *, |
| 26 | wxShadowObjectFields, |
| 27 | class WXDLLIMPEXP_BASE |
| 28 | ); |
| 29 | |
| 30 | class WXDLLIMPEXP_BASE wxShadowObject |
| 31 | { |
| 32 | public: |
| 33 | wxShadowObject() { } |
| 34 | |
| 35 | void AddMethod( const wxString &name, wxShadowObjectMethod method ) |
| 36 | { |
| 37 | wxShadowObjectMethods::iterator it = m_methods.find( name ); |
| 38 | if (it == m_methods.end()) |
| 39 | m_methods[ name ] = method; |
| 40 | else |
| 41 | it->second = method; |
| 42 | } |
| 43 | |
| 44 | bool InvokeMethod( const wxString &name, void* window, void* param, int* returnValue ) |
| 45 | { |
| 46 | wxShadowObjectMethods::iterator it = m_methods.find( name ); |
| 47 | if (it == m_methods.end()) |
| 48 | return false; |
| 49 | wxShadowObjectMethod method = it->second; |
| 50 | int ret = (*method)(window, param); |
| 51 | if (returnValue) |
| 52 | *returnValue = ret; |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | void AddField( const wxString &name, void* initialValue = NULL ) |
| 57 | { |
| 58 | wxShadowObjectFields::iterator it = m_fields.find( name ); |
| 59 | if (it == m_fields.end()) |
| 60 | m_fields[ name ] = initialValue; |
| 61 | else |
| 62 | it->second = initialValue; |
| 63 | } |
| 64 | |
| 65 | void SetField( const wxString &name, void* value ) |
| 66 | { |
| 67 | wxShadowObjectFields::iterator it = m_fields.find( name ); |
| 68 | if (it == m_fields.end()) |
| 69 | return; |
| 70 | it->second = value; |
| 71 | } |
| 72 | |
| 73 | void* GetField( const wxString &name, void *defaultValue = NULL ) |
| 74 | { |
| 75 | wxShadowObjectFields::iterator it = m_fields.find( name ); |
| 76 | if (it == m_fields.end()) |
| 77 | return defaultValue; |
| 78 | return it->second; |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | wxShadowObjectMethods m_methods; |
| 83 | wxShadowObjectFields m_fields; |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | // ---------------------------------------------------------------------------- |
| 88 | |
| 89 | // what kind of client data do we have? |
| 90 | enum wxClientDataType |
| 91 | { |
| 92 | wxClientData_None, // we don't know yet because we don't have it at all |
| 93 | wxClientData_Object, // our client data is typed and we own it |
| 94 | wxClientData_Void // client data is untyped and we don't own it |
| 95 | }; |
| 96 | |
| 97 | class WXDLLIMPEXP_BASE wxClientData |
| 98 | { |
| 99 | public: |
| 100 | wxClientData() { } |
| 101 | virtual ~wxClientData() { } |
| 102 | }; |
| 103 | |
| 104 | class WXDLLIMPEXP_BASE wxStringClientData : public wxClientData |
| 105 | { |
| 106 | public: |
| 107 | wxStringClientData() : m_data() { } |
| 108 | wxStringClientData( const wxString &data ) : m_data(data) { } |
| 109 | void SetData( const wxString &data ) { m_data = data; } |
| 110 | const wxString& GetData() const { return m_data; } |
| 111 | |
| 112 | private: |
| 113 | wxString m_data; |
| 114 | }; |
| 115 | |
| 116 | // This class is a mixin that provides storage and management of "client |
| 117 | // data." The client data stored can either be a pointer to a wxClientData |
| 118 | // object in which case it is managed by the container (i.e. it will delete |
| 119 | // the data when it's destroyed) or an untyped pointer which won't be deleted |
| 120 | // by the container - but not both of them |
| 121 | // |
| 122 | // NOTE: This functionality is currently duplicated in wxEvtHandler in order |
| 123 | // to avoid having more than one vtable in that class hierarchy. |
| 124 | |
| 125 | class WXDLLIMPEXP_BASE wxClientDataContainer |
| 126 | { |
| 127 | public: |
| 128 | wxClientDataContainer(); |
| 129 | virtual ~wxClientDataContainer(); |
| 130 | |
| 131 | void SetClientObject( wxClientData *data ) { DoSetClientObject(data); } |
| 132 | wxClientData *GetClientObject() const { return DoGetClientObject(); } |
| 133 | |
| 134 | void SetClientData( void *data ) { DoSetClientData(data); } |
| 135 | void *GetClientData() const { return DoGetClientData(); } |
| 136 | |
| 137 | protected: |
| 138 | // The user data: either an object which will be deleted by the container |
| 139 | // when it's deleted or some raw pointer which we do nothing with. Only |
| 140 | // one type of data can be used with the given window, i.e. you cannot set |
| 141 | // the void data and then associate the container with wxClientData or vice |
| 142 | // versa. |
| 143 | union |
| 144 | { |
| 145 | wxClientData *m_clientObject; |
| 146 | void *m_clientData; |
| 147 | }; |
| 148 | |
| 149 | // client data accessors |
| 150 | virtual void DoSetClientObject( wxClientData *data ); |
| 151 | virtual wxClientData *DoGetClientObject() const; |
| 152 | |
| 153 | virtual void DoSetClientData( void *data ); |
| 154 | virtual void *DoGetClientData() const; |
| 155 | |
| 156 | // what kind of data do we have? |
| 157 | wxClientDataType m_clientDataType; |
| 158 | |
| 159 | }; |
| 160 | |
| 161 | #endif // _WX_CLNTDATAH__ |
| 162 | |