1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/ctrlsub.h
3 // Purpose: common functionality of wxItemContainer-derived controls
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_GENERIC_CTRLSUB_H_
12 #define _WX_GENERIC_CTRLSUB_H_
14 #include "wx/dynarray.h"
16 // ----------------------------------------------------------------------------
17 // wxControlWithItemsGeneric: generic implementation of item client data
18 // ----------------------------------------------------------------------------
20 class wxControlWithItemsGeneric
: public wxControlWithItemsBase
23 wxControlWithItemsGeneric() { }
25 virtual void DoInitItemClientData()
27 m_itemsClientData
.resize(GetCount(), NULL
);
30 virtual void DoSetItemClientData(unsigned int n
, void *clientData
)
32 m_itemsClientData
[n
] = clientData
;
35 virtual void *DoGetItemClientData(unsigned int n
) const
37 return m_itemsClientData
[n
];
40 virtual void DoClear() { m_itemsClientData
.clear(); }
41 virtual void DoDeleteOneItem(unsigned int pos
)
43 if ( HasClientData() )
44 m_itemsClientData
.RemoveAt(pos
);
48 // preallocate memory for numItems new items: this should be called from
49 // the derived classes DoInsertItems() to speed up appending big numbers of
50 // items with client data; it is safe to call even if we don't use client
51 // data at all and does nothing in this case
52 void AllocClientData(unsigned int numItems
)
54 if ( HasClientData() )
55 m_itemsClientData
.reserve(m_itemsClientData
.size() + numItems
);
58 // this must be called by derived classes when a new item is added to the
59 // control to add storage for the corresponding client data pointer (before
60 // inserting many items, call AllocClientData())
61 void InsertNewItemClientData(unsigned int pos
,
64 wxClientDataType type
)
66 if ( InitClientDataIfNeeded(type
) )
67 m_itemsClientData
.Insert(clientData
[n
], pos
);
70 // the same as InsertNewItemClientData() but for numItems consecutive items
71 // (this can only be used if the control doesn't support sorting as
72 // otherwise the items positions wouldn't be consecutive any more)
73 void InsertNewItemsClientData(unsigned int pos
,
74 unsigned int numItems
,
76 wxClientDataType type
)
78 if ( InitClientDataIfNeeded(type
) )
80 // it's more efficient to insert everything at once and then update
81 // for big number of items to avoid moving the array contents
82 // around (which would result in O(N^2) algorithm)
83 m_itemsClientData
.Insert(NULL
, pos
, numItems
);
85 for ( unsigned int n
= 0; n
< numItems
; ++n
, ++pos
)
86 m_itemsClientData
[pos
] = clientData
[n
];
91 // vector containing the client data pointers: it is either empty (if
92 // client data is not used) or has the same number of elements as the
94 wxArrayPtrVoid m_itemsClientData
;
97 // initialize client data if needed, return false if we don't have any
98 // client data and true otherwise
99 bool InitClientDataIfNeeded(wxClientDataType type
)
101 if ( !HasClientData() )
103 if ( type
== wxClientData_None
)
105 // we didn't have the client data before and are not asked to
106 // store it now neither
110 // this is the first time client data is used with this control
111 DoInitItemClientData();
112 m_clientDataItemsType
= type
;
114 //else: we already have client data
119 DECLARE_NO_COPY_CLASS(wxControlWithItemsGeneric
)
122 #endif // _WX_GENERIC_CTRLSUB_H_