]>
Commit | Line | Data |
---|---|---|
a236aa20 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/generic/ctrlsub.h | |
3 | // Purpose: common functionality of wxItemContainer-derived controls | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2007-07-25 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org> | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef _WX_GENERIC_CTRLSUB_H_ | |
12 | #define _WX_GENERIC_CTRLSUB_H_ | |
13 | ||
14 | #include "wx/dynarray.h" | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // wxControlWithItemsGeneric: generic implementation of item client data | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | class wxControlWithItemsGeneric : public wxControlWithItemsBase | |
21 | { | |
22 | public: | |
23 | wxControlWithItemsGeneric() { } | |
24 | ||
25 | virtual void DoInitItemClientData() | |
26 | { | |
27 | m_itemsClientData.resize(GetCount(), NULL); | |
28 | } | |
29 | ||
30 | virtual void DoSetItemClientData(unsigned int n, void *clientData) | |
31 | { | |
32 | m_itemsClientData[n] = clientData; | |
33 | } | |
34 | ||
35 | virtual void *DoGetItemClientData(unsigned int n) const | |
36 | { | |
37 | return m_itemsClientData[n]; | |
38 | } | |
39 | ||
40 | virtual void DoClear() { m_itemsClientData.clear(); } | |
41 | virtual void DoDeleteOneItem(unsigned int pos) | |
42 | { | |
43 | if ( HasClientData() ) | |
44 | m_itemsClientData.RemoveAt(pos); | |
45 | } | |
46 | ||
47 | protected: | |
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) | |
53 | { | |
54 | if ( HasClientData() ) | |
55 | m_itemsClientData.reserve(m_itemsClientData.size() + numItems); | |
56 | } | |
57 | ||
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, | |
62 | void **clientData, | |
63 | unsigned int n, | |
64 | wxClientDataType type) | |
65 | { | |
66 | if ( InitClientDataIfNeeded(type) ) | |
67 | m_itemsClientData.Insert(clientData[n], pos); | |
68 | } | |
69 | ||
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, | |
75 | void **clientData, | |
76 | wxClientDataType type) | |
77 | { | |
78 | if ( InitClientDataIfNeeded(type) ) | |
79 | { | |
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); | |
84 | ||
85 | for ( unsigned int n = 0; n < numItems; ++n, ++pos ) | |
86 | m_itemsClientData[pos] = clientData[n]; | |
87 | } | |
88 | } | |
89 | ||
90 | ||
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 | |
93 | // control | |
94 | wxArrayPtrVoid m_itemsClientData; | |
95 | ||
96 | private: | |
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) | |
100 | { | |
101 | if ( !HasClientData() ) | |
102 | { | |
103 | if ( type == wxClientData_None ) | |
104 | { | |
105 | // we didn't have the client data before and are not asked to | |
106 | // store it now neither | |
107 | return false; | |
108 | } | |
109 | ||
110 | // this is the first time client data is used with this control | |
111 | DoInitItemClientData(); | |
b4a11fe8 | 112 | SetClientDataType(type); |
a236aa20 VZ |
113 | } |
114 | //else: we already have client data | |
115 | ||
116 | return true; | |
117 | } | |
118 | ||
119 | DECLARE_NO_COPY_CLASS(wxControlWithItemsGeneric) | |
120 | }; | |
121 | ||
122 | #endif // _WX_GENERIC_CTRLSUB_H_ | |
123 |