]>
Commit | Line | Data |
---|---|---|
c71d3313 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/selstore.h | |
3 | // Purpose: wxSelectionStore stores selected items in a control | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 08.06.03 (extracted from src/generic/listctrl.cpp) | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2000-2003 Vadim Zeitlin <vadim@wxwindows.org> | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_SELSTORE_H_ | |
13 | #define _WX_SELSTORE_H_ | |
14 | ||
15 | #include "wx/dynarray.h" | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // wxSelectedIndices is just a sorted array of indices | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
21 | inline int CMPFUNC_CONV wxSizeTCmpFn(size_t n1, size_t n2) { return n1 - n2; } | |
22 | ||
23 | WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_LONG(size_t, | |
24 | wxSizeTCmpFn, | |
25 | wxSelectedIndices); | |
26 | ||
27 | // ---------------------------------------------------------------------------- | |
28 | // wxSelectionStore is used to store the selected items in the virtual | |
29 | // controls, i.e. it is well suited for storing even when the control contains | |
30 | // a huge (practically infinite) number of items. | |
31 | // | |
32 | // Of course, internally it still has to store the selected items somehow (as | |
33 | // an array currently) but the advantage is that it can handle the selection | |
34 | // of all items (common operation) efficiently and that it could be made even | |
35 | // smarter in the future (e.g. store the selections as an array of ranges + | |
36 | // individual items) without changing its API. | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
39 | class WXDLLEXPORT wxSelectionStore | |
40 | { | |
41 | public: | |
42 | wxSelectionStore() : m_itemsSel(wxSizeTCmpFn) { Init(); } | |
43 | ||
44 | // set the total number of items we handle | |
45 | void SetItemCount(size_t count) { m_count = count; } | |
46 | ||
47 | // special case of SetItemCount(0) | |
48 | void Clear() { m_itemsSel.Clear(); m_count = 0; m_defaultState = FALSE; } | |
49 | ||
50 | // must be called when a new item is inserted/added | |
2eb10e2a | 51 | void OnItemAdd(size_t WXUNUSED(item)) { wxFAIL_MSG( _T("TODO") ); } |
c71d3313 VZ |
52 | |
53 | // must be called when an item is deleted | |
54 | void OnItemDelete(size_t item); | |
55 | ||
56 | // select one item, use SelectRange() insted if possible! | |
57 | // | |
58 | // returns true if the items selection really changed | |
59 | bool SelectItem(size_t item, bool select = TRUE); | |
60 | ||
61 | // select the range of items | |
62 | // | |
63 | // return true and fill the itemsChanged array with the indices of items | |
64 | // which have changed state if "few" of them did, otherwise return false | |
65 | // (meaning that too many items changed state to bother counting them | |
66 | // individually) | |
67 | bool SelectRange(size_t itemFrom, size_t itemTo, | |
68 | bool select = TRUE, | |
69 | wxArrayInt *itemsChanged = NULL); | |
70 | ||
71 | // return true if the given item is selected | |
72 | bool IsSelected(size_t item) const; | |
73 | ||
74 | // return the total number of selected items | |
75 | size_t GetSelectedCount() const | |
76 | { | |
77 | return m_defaultState ? m_count - m_itemsSel.GetCount() | |
78 | : m_itemsSel.GetCount(); | |
79 | } | |
80 | ||
81 | private: | |
82 | // (re)init | |
83 | void Init() { m_defaultState = FALSE; } | |
84 | ||
85 | // the total number of items we handle | |
86 | size_t m_count; | |
87 | ||
88 | // the default state: normally, FALSE (i.e. off) but maybe set to TRUE if | |
89 | // there are more selected items than non selected ones - this allows to | |
90 | // handle selection of all items efficiently | |
91 | bool m_defaultState; | |
92 | ||
93 | // the array of items whose selection state is different from default | |
94 | wxSelectedIndices m_itemsSel; | |
95 | ||
96 | DECLARE_NO_COPY_CLASS(wxSelectionStore) | |
97 | }; | |
98 | ||
99 | ||
100 | #endif // _WX_SELSTORE_H_ | |
101 |