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