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