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__
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "clntdata.h"
20 #include "wx/string.h"
21 #include "wx/hashmap.h"
23 #if wxABI_VERSION >= 20602
25 typedef int (*wxShadowObjectMethod
)(void*, void*);
26 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(
28 wxShadowObjectMethods
,
29 class WXDLLIMPEXP_BASE
31 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(
34 class WXDLLIMPEXP_BASE
37 class WXDLLIMPEXP_BASE wxShadowObject
42 void AddMethod( const wxString
&name
, wxShadowObjectMethod method
)
44 wxShadowObjectMethods::iterator it
= m_methods
.find( name
);
45 if (it
== m_methods
.end())
46 m_methods
[ name
] = method
;
51 bool InvokeMethod( const wxString
&name
, void* window
, void* param
, int* returnValue
)
53 wxShadowObjectMethods::iterator it
= m_methods
.find( name
);
54 if (it
== m_methods
.end())
56 wxShadowObjectMethod method
= it
->second
;
57 int ret
= (*method
)(window
, param
);
63 void AddField( const wxString
&name
, void* initialValue
= NULL
)
65 wxShadowObjectFields::iterator it
= m_fields
.find( name
);
66 if (it
== m_fields
.end())
67 m_fields
[ name
] = initialValue
;
69 it
->second
= initialValue
;
72 void SetField( const wxString
&name
, void* value
)
74 wxShadowObjectFields::iterator it
= m_fields
.find( name
);
75 if (it
== m_fields
.end())
80 void* GetField( const wxString
&name
, void *defaultValue
= NULL
)
82 wxShadowObjectFields::iterator it
= m_fields
.find( name
);
83 if (it
== m_fields
.end())
89 wxShadowObjectMethods m_methods
;
90 wxShadowObjectFields m_fields
;
93 #endif // wxABI_VERSION
95 // ----------------------------------------------------------------------------
97 // what kind of client data do we have?
100 wxClientData_None
, // we don't know yet because we don't have it at all
101 wxClientData_Object
, // our client data is typed and we own it
102 wxClientData_Void
// client data is untyped and we don't own it
105 class WXDLLIMPEXP_BASE wxClientData
109 virtual ~wxClientData() { }
112 class WXDLLIMPEXP_BASE wxStringClientData
: public wxClientData
115 wxStringClientData() : m_data() { }
116 wxStringClientData( const wxString
&data
) : m_data(data
) { }
117 void SetData( const wxString
&data
) { m_data
= data
; }
118 const wxString
& GetData() const { return m_data
; }
124 // This class is a mixin that provides storage and management of "client
125 // data." The client data stored can either be a pointer to a wxClientData
126 // object in which case it is managed by the container (i.e. it will delete
127 // the data when it's destroyed) or an untyped pointer which won't be deleted
128 // by the container - but not both of them
130 // NOTE: This functionality is currently duplicated in wxEvtHandler in order
131 // to avoid having more than one vtable in that class hierarchy.
133 class WXDLLIMPEXP_BASE wxClientDataContainer
136 wxClientDataContainer();
137 virtual ~wxClientDataContainer();
139 void SetClientObject( wxClientData
*data
) { DoSetClientObject(data
); }
140 wxClientData
*GetClientObject() const { return DoGetClientObject(); }
142 void SetClientData( void *data
) { DoSetClientData(data
); }
143 void *GetClientData() const { return DoGetClientData(); }
146 // The user data: either an object which will be deleted by the container
147 // when it's deleted or some raw pointer which we do nothing with. Only
148 // one type of data can be used with the given window, i.e. you cannot set
149 // the void data and then associate the container with wxClientData or vice
153 wxClientData
*m_clientObject
;
157 // client data accessors
158 virtual void DoSetClientObject( wxClientData
*data
);
159 virtual wxClientData
*DoGetClientObject() const;
161 virtual void DoSetClientData( void *data
);
162 virtual void *DoGetClientData() const;
164 // what kind of data do we have?
165 wxClientDataType m_clientDataType
;
169 // not Motif-specific, but currently used only under Motif,
170 // compiled to make wxMotif and wxGTK base libraries compatible
171 #if defined(__WXMOTIF__) || wxABI_VERSION >= 20602
173 #include "wx/vector.h"
175 struct WXDLLIMPEXP_BASE wxClientDataDictionaryPair
177 wxClientDataDictionaryPair( size_t idx
) : index( idx
), data( 0 ) { }
184 wxClientDataDictionaryPair
,
185 wxClientDataDictionaryPairVector
,
189 // this class is used internally to maintain the association between items
190 // of (some subclasses of) wxControlWithItems and their client data
191 // NOTE: this class does not keep track of whether it contains
192 // wxClientData or void*. The client must ensure that
193 // it does not contain a mix of the two, and that
194 // DestroyData is called if it contains wxClientData
195 class WXDLLIMPEXP_BASE wxClientDataDictionary
198 wxClientDataDictionary() {}
200 // deletes all the data
203 for( size_t i
= 0, end
= m_vec
.size(); i
!= end
; ++i
)
204 delete m_vec
[i
].data
;
208 // if data for the given index is not present, add it,
209 // if it is present, delete the old data and replace it with
211 void Set( size_t index
, wxClientData
* data
, bool doDelete
)
213 size_t ptr
= Find( index
);
217 if( ptr
== m_vec
.size() ) return;
219 delete m_vec
[ptr
].data
;
224 if( ptr
== m_vec
.size() )
226 m_vec
.push_back( wxClientDataDictionaryPair( index
) );
227 ptr
= m_vec
.size() - 1;
231 delete m_vec
[ptr
].data
;
232 m_vec
[ptr
].data
= data
;
236 // get the data associated with the given index,
237 // return 0 if not found
238 wxClientData
* Get( size_t index
) const
240 size_t it
= Find( index
);
241 if( it
== m_vec
.size() ) return 0;
242 return (wxClientData
*)m_vec
[it
].data
; // const cast
245 // delete the data associated with the given index
246 // it also decreases by one the indices of all the elements
247 // with an index greater than the given index
248 void Delete( size_t index
, bool doDelete
)
250 size_t todel
= m_vec
.size();
252 for( size_t i
= 0, end
= m_vec
.size(); i
!= end
; ++i
)
254 if( m_vec
[i
].index
== index
)
256 else if( m_vec
[i
].index
> index
)
260 if( todel
!= m_vec
.size() )
263 delete m_vec
[todel
].data
;
264 m_vec
.erase( todel
);
268 // returns MyVec.size() if not found
269 size_t Find( size_t index
) const
271 for( size_t i
= 0, end
= m_vec
.size(); i
!= end
; ++i
)
273 if( m_vec
[i
].index
== index
)
280 wxClientDataDictionaryPairVector m_vec
;
283 #endif // __WXMOTIF__
285 // ----------------------------------------------------------------------------