put GetEscapeId() inside #if wxABI_VERSION > 20601
[wxWidgets.git] / include / wx / clntdata.h
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 typedef int (*wxShadowObjectMethod)(void*, void*);
24 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(
25 wxShadowObjectMethod,
26 wxShadowObjectMethods,
27 class WXDLLIMPEXP_BASE
28 );
29 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(
30 void *,
31 wxShadowObjectFields,
32 class WXDLLIMPEXP_BASE
33 );
34
35 class WXDLLIMPEXP_BASE wxShadowObject
36 {
37 public:
38 wxShadowObject() { }
39
40 void AddMethod( const wxString &name, wxShadowObjectMethod method )
41 {
42 wxShadowObjectMethods::iterator it = m_methods.find( name );
43 if (it == m_methods.end())
44 m_methods[ name ] = method;
45 else
46 it->second = method;
47 }
48
49 bool InvokeMethod( const wxString &name, void* window, void* param, int* returnValue )
50 {
51 wxShadowObjectMethods::iterator it = m_methods.find( name );
52 if (it == m_methods.end())
53 return false;
54 wxShadowObjectMethod method = it->second;
55 int ret = (*method)(window, param);
56 if (returnValue)
57 *returnValue = ret;
58 return true;
59 }
60
61 void AddField( const wxString &name, void* initialValue = NULL )
62 {
63 wxShadowObjectFields::iterator it = m_fields.find( name );
64 if (it == m_fields.end())
65 m_fields[ name ] = initialValue;
66 else
67 it->second = initialValue;
68 }
69
70 void SetField( const wxString &name, void* value )
71 {
72 wxShadowObjectFields::iterator it = m_fields.find( name );
73 if (it == m_fields.end())
74 return;
75 it->second = value;
76 }
77
78 void* GetField( const wxString &name, void *defaultValue = NULL )
79 {
80 wxShadowObjectFields::iterator it = m_fields.find( name );
81 if (it == m_fields.end())
82 return defaultValue;
83 return it->second;
84 }
85
86 private:
87 wxShadowObjectMethods m_methods;
88 wxShadowObjectFields m_fields;
89 };
90
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
101 class WXDLLIMPEXP_BASE wxClientData
102 {
103 public:
104 wxClientData() { }
105 virtual ~wxClientData() { }
106 };
107
108 class WXDLLIMPEXP_BASE wxStringClientData : public wxClientData
109 {
110 public:
111 wxStringClientData() : m_data() { }
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
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
122 // object in which case it is managed by the container (i.e. it will delete
123 // the data when it's destroyed) or an untyped pointer which won't be deleted
124 // by the container - but not both of them
125 //
126 // NOTE: This functionality is currently duplicated in wxEvtHandler in order
127 // to avoid having more than one vtable in that class hierarchy.
128
129 class WXDLLIMPEXP_BASE wxClientDataContainer
130 {
131 public:
132 wxClientDataContainer();
133 virtual ~wxClientDataContainer();
134
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:
142 // The user data: either an object which will be deleted by the container
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
145 // the void data and then associate the container with wxClientData or vice
146 // versa.
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 };
164
165 // not Motif-specific, but currently used only under Motif
166 #ifdef __WXMOTIF__
167
168 #include <wx/vector.h>
169
170 struct WXDLLIMPEXP_BASE wxClientDataDictionaryPair
171 {
172 wxClientDataDictionaryPair( size_t idx ) : index( idx ), data( 0 ) { }
173
174 size_t index;
175 wxClientData* data;
176 };
177
178 WX_DECLARE_VECTOR(wxClientDataDictionaryPair,wxClientDataDictionaryPairVector);
179
180 // this class is used internally to maintain the association between items
181 // of (some subclasses of) wxControlWithItems and their client data
182 // NOTE: this class does not keep track of whether it contains
183 // wxClientData or void*. The client must ensure that
184 // it does not contain a mix of the two, and that
185 // DestroyData is called if it contains wxClientData
186 class WXDLLIMPEXP_BASE wxClientDataDictionary
187 {
188 public:
189 wxClientDataDictionary() {}
190
191 // deletes all the data
192 void DestroyData()
193 {
194 for( size_t i = 0, end = m_vec.size(); i != end; ++i )
195 delete m_vec[i].data;
196 m_vec.clear();
197 }
198
199 // if data for the given index is not present, add it,
200 // if it is present, delete the old data and replace it with
201 // the new one
202 void Set( size_t index, wxClientData* data, bool doDelete )
203 {
204 size_t ptr = Find( index );
205
206 if( !data )
207 {
208 if( ptr == m_vec.size() ) return;
209 if( doDelete )
210 delete m_vec[ptr].data;
211 m_vec.erase( ptr );
212 }
213 else
214 {
215 if( ptr == m_vec.size() )
216 {
217 m_vec.push_back( wxClientDataDictionaryPair( index ) );
218 ptr = m_vec.size() - 1;
219 }
220
221 if( doDelete )
222 delete m_vec[ptr].data;
223 m_vec[ptr].data = data;
224 }
225 }
226
227 // get the data associated with the given index,
228 // return 0 if not found
229 wxClientData* Get( size_t index ) const
230 {
231 size_t it = Find( index );
232 if( it == m_vec.size() ) return 0;
233 return (wxClientData*)m_vec[it].data; // const cast
234 }
235
236 // delete the data associated with the given index
237 // it also decreases by one the indices of all the elements
238 // with an index greater than the given index
239 void Delete( size_t index, bool doDelete )
240 {
241 size_t todel = m_vec.size();
242
243 for( size_t i = 0, end = m_vec.size(); i != end; ++i )
244 {
245 if( m_vec[i].index == index )
246 todel = i;
247 else if( m_vec[i].index > index )
248 --(m_vec[i].index);
249 }
250
251 if( todel != m_vec.size() )
252 {
253 if( doDelete )
254 delete m_vec[todel].data;
255 m_vec.erase( todel );
256 }
257 }
258 private:
259 // returns MyVec.size() if not found
260 size_t Find( size_t index ) const
261 {
262 for( size_t i = 0, end = m_vec.size(); i != end; ++i )
263 {
264 if( m_vec[i].index == index )
265 return i;
266 }
267
268 return m_vec.size();
269 }
270
271 wxClientDataDictionaryPairVector m_vec;
272 };
273
274 #endif // __WXMOTIF__
275
276 // ----------------------------------------------------------------------------
277 #endif
278