]>
Commit | Line | Data |
---|---|---|
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 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) wxWidgets team | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_CLNTDATAH__ | |
13 | #define _WX_CLNTDATAH__ | |
14 | ||
15 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
16 | #pragma interface "clntdata.h" | |
17 | #endif | |
18 | ||
19 | #include "wx/defs.h" | |
20 | #include "wx/string.h" | |
21 | #include "wx/hashmap.h" | |
22 | ||
23 | #if wxABI_VERSION >= 20602 | |
24 | ||
25 | typedef int (*wxShadowObjectMethod)(void*, void*); | |
26 | WX_DECLARE_STRING_HASH_MAP_WITH_DECL( | |
27 | wxShadowObjectMethod, | |
28 | wxShadowObjectMethods, | |
29 | class WXDLLIMPEXP_BASE | |
30 | ); | |
31 | WX_DECLARE_STRING_HASH_MAP_WITH_DECL( | |
32 | void *, | |
33 | wxShadowObjectFields, | |
34 | class WXDLLIMPEXP_BASE | |
35 | ); | |
36 | ||
37 | class WXDLLIMPEXP_BASE wxShadowObject | |
38 | { | |
39 | public: | |
40 | wxShadowObject() { } | |
41 | ||
42 | void AddMethod( const wxString &name, wxShadowObjectMethod method ) | |
43 | { | |
44 | wxShadowObjectMethods::iterator it = m_methods.find( name ); | |
45 | if (it == m_methods.end()) | |
46 | m_methods[ name ] = method; | |
47 | else | |
48 | it->second = method; | |
49 | } | |
50 | ||
51 | bool InvokeMethod( const wxString &name, void* window, void* param, int* returnValue ) | |
52 | { | |
53 | wxShadowObjectMethods::iterator it = m_methods.find( name ); | |
54 | if (it == m_methods.end()) | |
55 | return false; | |
56 | wxShadowObjectMethod method = it->second; | |
57 | int ret = (*method)(window, param); | |
58 | if (returnValue) | |
59 | *returnValue = ret; | |
60 | return true; | |
61 | } | |
62 | ||
63 | void AddField( const wxString &name, void* initialValue = NULL ) | |
64 | { | |
65 | wxShadowObjectFields::iterator it = m_fields.find( name ); | |
66 | if (it == m_fields.end()) | |
67 | m_fields[ name ] = initialValue; | |
68 | else | |
69 | it->second = initialValue; | |
70 | } | |
71 | ||
72 | void SetField( const wxString &name, void* value ) | |
73 | { | |
74 | wxShadowObjectFields::iterator it = m_fields.find( name ); | |
75 | if (it == m_fields.end()) | |
76 | return; | |
77 | it->second = value; | |
78 | } | |
79 | ||
80 | void* GetField( const wxString &name, void *defaultValue = NULL ) | |
81 | { | |
82 | wxShadowObjectFields::iterator it = m_fields.find( name ); | |
83 | if (it == m_fields.end()) | |
84 | return defaultValue; | |
85 | return it->second; | |
86 | } | |
87 | ||
88 | private: | |
89 | wxShadowObjectMethods m_methods; | |
90 | wxShadowObjectFields m_fields; | |
91 | }; | |
92 | ||
93 | #endif // wxABI_VERSION | |
94 | ||
95 | // ---------------------------------------------------------------------------- | |
96 | ||
97 | // what kind of client data do we have? | |
98 | enum wxClientDataType | |
99 | { | |
100 | wxClientData_None, // we don't know yet because we don't have it at all | |
101 | wxClientData_Object, // our client data is typed and we own it | |
102 | wxClientData_Void // client data is untyped and we don't own it | |
103 | }; | |
104 | ||
105 | class WXDLLIMPEXP_BASE wxClientData | |
106 | { | |
107 | public: | |
108 | wxClientData() { } | |
109 | virtual ~wxClientData() { } | |
110 | }; | |
111 | ||
112 | class WXDLLIMPEXP_BASE wxStringClientData : public wxClientData | |
113 | { | |
114 | public: | |
115 | wxStringClientData() : m_data() { } | |
116 | wxStringClientData( const wxString &data ) : m_data(data) { } | |
117 | void SetData( const wxString &data ) { m_data = data; } | |
118 | const wxString& GetData() const { return m_data; } | |
119 | ||
120 | private: | |
121 | wxString m_data; | |
122 | }; | |
123 | ||
124 | // This class is a mixin that provides storage and management of "client | |
125 | // data." The client data stored can either be a pointer to a wxClientData | |
126 | // object in which case it is managed by the container (i.e. it will delete | |
127 | // the data when it's destroyed) or an untyped pointer which won't be deleted | |
128 | // by the container - but not both of them | |
129 | // | |
130 | // NOTE: This functionality is currently duplicated in wxEvtHandler in order | |
131 | // to avoid having more than one vtable in that class hierarchy. | |
132 | ||
133 | class WXDLLIMPEXP_BASE wxClientDataContainer | |
134 | { | |
135 | public: | |
136 | wxClientDataContainer(); | |
137 | virtual ~wxClientDataContainer(); | |
138 | ||
139 | void SetClientObject( wxClientData *data ) { DoSetClientObject(data); } | |
140 | wxClientData *GetClientObject() const { return DoGetClientObject(); } | |
141 | ||
142 | void SetClientData( void *data ) { DoSetClientData(data); } | |
143 | void *GetClientData() const { return DoGetClientData(); } | |
144 | ||
145 | protected: | |
146 | // The user data: either an object which will be deleted by the container | |
147 | // when it's deleted or some raw pointer which we do nothing with. Only | |
148 | // one type of data can be used with the given window, i.e. you cannot set | |
149 | // the void data and then associate the container with wxClientData or vice | |
150 | // versa. | |
151 | union | |
152 | { | |
153 | wxClientData *m_clientObject; | |
154 | void *m_clientData; | |
155 | }; | |
156 | ||
157 | // client data accessors | |
158 | virtual void DoSetClientObject( wxClientData *data ); | |
159 | virtual wxClientData *DoGetClientObject() const; | |
160 | ||
161 | virtual void DoSetClientData( void *data ); | |
162 | virtual void *DoGetClientData() const; | |
163 | ||
164 | // what kind of data do we have? | |
165 | wxClientDataType m_clientDataType; | |
166 | ||
167 | }; | |
168 | ||
169 | // not Motif-specific, but currently used only under Motif | |
170 | #ifdef __WXMOTIF__ | |
171 | ||
172 | #include <wx/vector.h> | |
173 | ||
174 | struct WXDLLIMPEXP_BASE wxClientDataDictionaryPair | |
175 | { | |
176 | wxClientDataDictionaryPair( size_t idx ) : index( idx ), data( 0 ) { } | |
177 | ||
178 | size_t index; | |
179 | wxClientData* data; | |
180 | }; | |
181 | ||
182 | WX_DECLARE_VECTOR(wxClientDataDictionaryPair,wxClientDataDictionaryPairVector); | |
183 | ||
184 | // this class is used internally to maintain the association between items | |
185 | // of (some subclasses of) wxControlWithItems and their client data | |
186 | // NOTE: this class does not keep track of whether it contains | |
187 | // wxClientData or void*. The client must ensure that | |
188 | // it does not contain a mix of the two, and that | |
189 | // DestroyData is called if it contains wxClientData | |
190 | class WXDLLIMPEXP_BASE wxClientDataDictionary | |
191 | { | |
192 | public: | |
193 | wxClientDataDictionary() {} | |
194 | ||
195 | // deletes all the data | |
196 | void DestroyData() | |
197 | { | |
198 | for( size_t i = 0, end = m_vec.size(); i != end; ++i ) | |
199 | delete m_vec[i].data; | |
200 | m_vec.clear(); | |
201 | } | |
202 | ||
203 | // if data for the given index is not present, add it, | |
204 | // if it is present, delete the old data and replace it with | |
205 | // the new one | |
206 | void Set( size_t index, wxClientData* data, bool doDelete ) | |
207 | { | |
208 | size_t ptr = Find( index ); | |
209 | ||
210 | if( !data ) | |
211 | { | |
212 | if( ptr == m_vec.size() ) return; | |
213 | if( doDelete ) | |
214 | delete m_vec[ptr].data; | |
215 | m_vec.erase( ptr ); | |
216 | } | |
217 | else | |
218 | { | |
219 | if( ptr == m_vec.size() ) | |
220 | { | |
221 | m_vec.push_back( wxClientDataDictionaryPair( index ) ); | |
222 | ptr = m_vec.size() - 1; | |
223 | } | |
224 | ||
225 | if( doDelete ) | |
226 | delete m_vec[ptr].data; | |
227 | m_vec[ptr].data = data; | |
228 | } | |
229 | } | |
230 | ||
231 | // get the data associated with the given index, | |
232 | // return 0 if not found | |
233 | wxClientData* Get( size_t index ) const | |
234 | { | |
235 | size_t it = Find( index ); | |
236 | if( it == m_vec.size() ) return 0; | |
237 | return (wxClientData*)m_vec[it].data; // const cast | |
238 | } | |
239 | ||
240 | // delete the data associated with the given index | |
241 | // it also decreases by one the indices of all the elements | |
242 | // with an index greater than the given index | |
243 | void Delete( size_t index, bool doDelete ) | |
244 | { | |
245 | size_t todel = m_vec.size(); | |
246 | ||
247 | for( size_t i = 0, end = m_vec.size(); i != end; ++i ) | |
248 | { | |
249 | if( m_vec[i].index == index ) | |
250 | todel = i; | |
251 | else if( m_vec[i].index > index ) | |
252 | --(m_vec[i].index); | |
253 | } | |
254 | ||
255 | if( todel != m_vec.size() ) | |
256 | { | |
257 | if( doDelete ) | |
258 | delete m_vec[todel].data; | |
259 | m_vec.erase( todel ); | |
260 | } | |
261 | } | |
262 | private: | |
263 | // returns MyVec.size() if not found | |
264 | size_t Find( size_t index ) const | |
265 | { | |
266 | for( size_t i = 0, end = m_vec.size(); i != end; ++i ) | |
267 | { | |
268 | if( m_vec[i].index == index ) | |
269 | return i; | |
270 | } | |
271 | ||
272 | return m_vec.size(); | |
273 | } | |
274 | ||
275 | wxClientDataDictionaryPairVector m_vec; | |
276 | }; | |
277 | ||
278 | #endif // __WXMOTIF__ | |
279 | ||
280 | // ---------------------------------------------------------------------------- | |
281 | #endif | |
282 |