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