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"
22 // ----------------------------------------------------------------------------
24 // what kind of client data do we have?
27 wxClientData_None
, // we don't know yet because we don't have it at all
28 wxClientData_Object
, // our client data is typed and we own it
29 wxClientData_Void
// client data is untyped and we don't own it
32 class WXDLLEXPORT wxClientData
36 virtual ~wxClientData() { }
39 class WXDLLEXPORT wxStringClientData
: public wxClientData
42 wxStringClientData() : m_data() { }
43 wxStringClientData( const wxString
&data
) : m_data(data
) { }
44 void SetData( const wxString
&data
) { m_data
= data
; }
45 const wxString
& GetData() const { return m_data
; }
51 // This class is a mixin that provides storage and management of "client
52 // data." The client data stored can either be a pointer to a wxClientData
53 // object in which case it is managed by the container (i.e. it will delete
54 // the data when it's destroyed) or an untyped pointer which won't be deleted
55 // by the container - but not both of them
57 // NOTE: This functionality is currently duplicated in wxEvtHandler in order
58 // to avoid having more than one vtable in that class hierarchy.
60 class WXDLLEXPORT wxClientDataContainer
63 wxClientDataContainer();
64 virtual ~wxClientDataContainer();
66 void SetClientObject( wxClientData
*data
) { DoSetClientObject(data
); }
67 wxClientData
*GetClientObject() const { return DoGetClientObject(); }
69 void SetClientData( void *data
) { DoSetClientData(data
); }
70 void *GetClientData() const { return DoGetClientData(); }
73 // The user data: either an object which will be deleted by the container
74 // when it's deleted or some raw pointer which we do nothing with. Only
75 // one type of data can be used with the given window, i.e. you cannot set
76 // the void data and then associate the container with wxClientData or vice
80 wxClientData
*m_clientObject
;
84 // client data accessors
85 virtual void DoSetClientObject( wxClientData
*data
);
86 virtual wxClientData
*DoGetClientObject() const;
88 virtual void DoSetClientData( void *data
);
89 virtual void *DoGetClientData() const;
91 // what kind of data do we have?
92 wxClientDataType m_clientDataType
;
96 // not Motif-specific, but currently used only under Motif
99 #include <wx/vector.h>
101 struct WXDLLEXPORT wxClientDataDictionaryPair
103 wxClientDataDictionaryPair( size_t idx
) : index( idx
), data( 0 ) { }
109 WX_DECLARE_VECTOR(wxClientDataDictionaryPair
,wxClientDataDictionaryPairVector
);
111 // this class is used internally to maintain the association between items
112 // of (some subclasses of) wxControlWithItems and their client data
113 // NOTE: this class does not keep track of whether it contains
114 // wxClientData or void*. The client must ensure that
115 // it does not contain a mix of the two, and that
116 // DestroyData is called if it contains wxClientData
117 class WXDLLEXPORT wxClientDataDictionary
120 wxClientDataDictionary() {};
122 // deletes all the data
125 for( size_t i
= 0, end
= m_vec
.size(); i
!= end
; ++i
)
126 delete m_vec
[i
].data
;
130 // if data for the given index is not present, add it,
131 // if it is present, delete the old data and replace it with
133 void Set( size_t index
, wxClientData
* data
, bool doDelete
)
135 size_t ptr
= Find( index
);
139 if( ptr
== m_vec
.size() ) return;
141 delete m_vec
[ptr
].data
;
146 if( ptr
== m_vec
.size() )
148 m_vec
.push_back( wxClientDataDictionaryPair( index
) );
149 ptr
= m_vec
.size() - 1;
153 delete m_vec
[ptr
].data
;
154 m_vec
[ptr
].data
= data
;
158 // get the data associated with the given index,
159 // return 0 if not found
160 wxClientData
* Get( size_t index
) const
162 size_t it
= Find( index
);
163 if( it
== m_vec
.size() ) return 0;
164 return (wxClientData
*)m_vec
[it
].data
; // const cast
167 // delete the data associated with the given index
168 // it also decreases by one the indices of all the elements
169 // with an index greater than the given index
170 void Delete( size_t index
, bool doDelete
)
172 size_t todel
= m_vec
.size();
174 for( size_t i
= 0, end
= m_vec
.size(); i
!= end
; ++i
)
176 if( m_vec
[i
].index
== index
)
178 else if( m_vec
[i
].index
> index
)
182 if( todel
!= m_vec
.size() )
185 delete m_vec
[todel
].data
;
186 m_vec
.erase( todel
);
190 // returns MyVec.size() if not found
191 size_t Find( size_t index
) const
193 for( size_t i
= 0, end
= m_vec
.size(); i
!= end
; ++i
)
195 if( m_vec
[i
].index
== index
)
202 wxClientDataDictionaryPairVector m_vec
;
205 #endif // __WXMOTIF__
207 // ----------------------------------------------------------------------------