1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: A mixin class for holding a wxClientData or void pointer
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_CLNTDATAH__
13 #define _WX_CLNTDATAH__
16 #include "wx/string.h"
17 #include "wx/hashmap.h"
19 typedef int (*wxShadowObjectMethod
)(void*, void*);
20 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(
22 wxShadowObjectMethods
,
23 class WXDLLIMPEXP_BASE
25 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(
28 class WXDLLIMPEXP_BASE
31 class WXDLLIMPEXP_BASE wxShadowObject
36 void AddMethod( const wxString
&name
, wxShadowObjectMethod method
)
38 wxShadowObjectMethods::iterator it
= m_methods
.find( name
);
39 if (it
== m_methods
.end())
40 m_methods
[ name
] = method
;
45 bool InvokeMethod( const wxString
&name
, void* window
, void* param
, int* returnValue
)
47 wxShadowObjectMethods::iterator it
= m_methods
.find( name
);
48 if (it
== m_methods
.end())
50 wxShadowObjectMethod method
= it
->second
;
51 int ret
= (*method
)(window
, param
);
57 void AddField( const wxString
&name
, void* initialValue
= NULL
)
59 wxShadowObjectFields::iterator it
= m_fields
.find( name
);
60 if (it
== m_fields
.end())
61 m_fields
[ name
] = initialValue
;
63 it
->second
= initialValue
;
66 void SetField( const wxString
&name
, void* value
)
68 wxShadowObjectFields::iterator it
= m_fields
.find( name
);
69 if (it
== m_fields
.end())
74 void* GetField( const wxString
&name
, void *defaultValue
= NULL
)
76 wxShadowObjectFields::iterator it
= m_fields
.find( name
);
77 if (it
== m_fields
.end())
83 wxShadowObjectMethods m_methods
;
84 wxShadowObjectFields m_fields
;
88 // ----------------------------------------------------------------------------
90 // what kind of client data do we have?
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
98 class WXDLLIMPEXP_BASE wxClientData
102 virtual ~wxClientData() { }
105 class WXDLLIMPEXP_BASE wxStringClientData
: public wxClientData
108 wxStringClientData() : m_data() { }
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
; }
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
119 // object in which case it is managed by the container (i.e. it will delete
120 // the data when it's destroyed) or an untyped pointer which won't be deleted
121 // by the container - but not both of them
123 // NOTE: This functionality is currently duplicated in wxEvtHandler in order
124 // to avoid having more than one vtable in that class hierarchy.
126 class WXDLLIMPEXP_BASE wxClientDataContainer
129 wxClientDataContainer();
130 virtual ~wxClientDataContainer();
132 void SetClientObject( wxClientData
*data
) { DoSetClientObject(data
); }
133 wxClientData
*GetClientObject() const { return DoGetClientObject(); }
135 void SetClientData( void *data
) { DoSetClientData(data
); }
136 void *GetClientData() const { return DoGetClientData(); }
139 // The user data: either an object which will be deleted by the container
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
142 // the void data and then associate the container with wxClientData or vice
146 wxClientData
*m_clientObject
;
150 // client data accessors
151 virtual void DoSetClientObject( wxClientData
*data
);
152 virtual wxClientData
*DoGetClientObject() const;
154 virtual void DoSetClientData( void *data
);
155 virtual void *DoGetClientData() const;
157 // what kind of data do we have?
158 wxClientDataType m_clientDataType
;
162 #include "wx/vector.h"
164 struct WXDLLIMPEXP_BASE wxClientDataDictionaryPair
166 wxClientDataDictionaryPair( size_t idx
) : index( idx
), data( 0 ) { }
172 // this class is used internally to maintain the association between items
173 // of (some subclasses of) wxControlWithItems and their client data
174 // NOTE: this class does not keep track of whether it contains
175 // wxClientData or void*. The client must ensure that
176 // it does not contain a mix of the two, and that
177 // DestroyData is called if it contains wxClientData
178 class WXDLLIMPEXP_BASE wxClientDataDictionary
181 wxClientDataDictionary() {}
183 // deletes all the data
186 for( size_t i
= 0, end
= m_vec
.size(); i
!= end
; ++i
)
187 delete m_vec
[i
].data
;
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
194 void Set( size_t index
, wxClientData
* data
, bool doDelete
)
196 size_t ptr
= Find( index
);
200 if( ptr
== m_vec
.size() ) return;
202 delete m_vec
[ptr
].data
;
207 if( ptr
== m_vec
.size() )
209 m_vec
.push_back( wxClientDataDictionaryPair( index
) );
210 ptr
= m_vec
.size() - 1;
214 delete m_vec
[ptr
].data
;
215 m_vec
[ptr
].data
= data
;
219 // get the data associated with the given index,
220 // return 0 if not found
221 wxClientData
* Get( size_t index
) const
223 size_t it
= Find( index
);
224 if( it
== m_vec
.size() ) return 0;
225 return (wxClientData
*)m_vec
[it
].data
; // const cast
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
)
233 size_t todel
= m_vec
.size();
235 for( size_t i
= 0, end
= m_vec
.size(); i
!= end
; ++i
)
237 if( m_vec
[i
].index
== index
)
239 else if( m_vec
[i
].index
> index
)
243 if( todel
!= m_vec
.size() )
246 delete m_vec
[todel
].data
;
247 m_vec
.erase( todel
);
251 // returns MyVec.size() if not found
252 size_t Find( size_t index
) const
254 for( size_t i
= 0, end
= m_vec
.size(); i
!= end
; ++i
)
256 if( m_vec
[i
].index
== index
)
263 wxVector
<wxClientDataDictionaryPair
> m_vec
;
266 #endif // _WX_CLNTDATAH__