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