1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/ctrlsub.h
3 // Purpose: common functionality of wxItemContainer-derived controls
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_GENERIC_CTRLSUB_H_
11 #define _WX_GENERIC_CTRLSUB_H_
13 #include "wx/dynarray.h"
15 // ----------------------------------------------------------------------------
16 // wxControlWithItemsGeneric: generic implementation of item client data
17 // ----------------------------------------------------------------------------
19 class wxControlWithItemsGeneric
: public wxControlWithItemsBase
22 wxControlWithItemsGeneric() { }
24 virtual void DoInitItemClientData()
26 m_itemsClientData
.resize(GetCount(), NULL
);
29 virtual void DoSetItemClientData(unsigned int n
, void *clientData
)
31 m_itemsClientData
[n
] = clientData
;
34 virtual void *DoGetItemClientData(unsigned int n
) const
36 return m_itemsClientData
[n
];
39 virtual void DoClear() { m_itemsClientData
.clear(); }
40 virtual void DoDeleteOneItem(unsigned int pos
)
42 if ( HasClientData() )
43 m_itemsClientData
.RemoveAt(pos
);
47 // preallocate memory for numItems new items: this should be called from
48 // the derived classes DoInsertItems() to speed up appending big numbers of
49 // items with client data; it is safe to call even if we don't use client
50 // data at all and does nothing in this case
51 void AllocClientData(unsigned int numItems
)
53 if ( HasClientData() )
54 m_itemsClientData
.reserve(m_itemsClientData
.size() + numItems
);
57 // this must be called by derived classes when a new item is added to the
58 // control to add storage for the corresponding client data pointer (before
59 // inserting many items, call AllocClientData())
60 void InsertNewItemClientData(unsigned int pos
,
63 wxClientDataType type
)
65 if ( InitClientDataIfNeeded(type
) )
66 m_itemsClientData
.Insert(clientData
[n
], pos
);
69 // the same as InsertNewItemClientData() but for numItems consecutive items
70 // (this can only be used if the control doesn't support sorting as
71 // otherwise the items positions wouldn't be consecutive any more)
72 void InsertNewItemsClientData(unsigned int pos
,
73 unsigned int numItems
,
75 wxClientDataType type
)
77 if ( InitClientDataIfNeeded(type
) )
79 // it's more efficient to insert everything at once and then update
80 // for big number of items to avoid moving the array contents
81 // around (which would result in O(N^2) algorithm)
82 m_itemsClientData
.Insert(NULL
, pos
, numItems
);
84 for ( unsigned int n
= 0; n
< numItems
; ++n
, ++pos
)
85 m_itemsClientData
[pos
] = clientData
[n
];
90 // vector containing the client data pointers: it is either empty (if
91 // client data is not used) or has the same number of elements as the
93 wxArrayPtrVoid m_itemsClientData
;
96 // initialize client data if needed, return false if we don't have any
97 // client data and true otherwise
98 bool InitClientDataIfNeeded(wxClientDataType type
)
100 if ( !HasClientData() )
102 if ( type
== wxClientData_None
)
104 // we didn't have the client data before and are not asked to
105 // store it now neither
109 // this is the first time client data is used with this control
110 DoInitItemClientData();
111 SetClientDataType(type
);
113 //else: we already have client data
118 wxDECLARE_NO_COPY_CLASS(wxControlWithItemsGeneric
);
121 #endif // _WX_GENERIC_CTRLSUB_H_