]>
Commit | Line | Data |
---|---|---|
c71d3313 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: generic/selstore.cpp | |
3 | // Purpose: wxSelectionStore implementation | |
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 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #include "wx/selstore.h" | |
21 | ||
22 | // ============================================================================ | |
23 | // wxSelectionStore | |
24 | // ============================================================================ | |
25 | ||
26 | // ---------------------------------------------------------------------------- | |
27 | // tests | |
28 | // ---------------------------------------------------------------------------- | |
29 | ||
30 | bool wxSelectionStore::IsSelected(size_t item) const | |
31 | { | |
32 | bool isSel = m_itemsSel.Index(item) != wxNOT_FOUND; | |
33 | ||
34 | // if the default state is to be selected, being in m_itemsSel means that | |
35 | // the item is not selected, so we have to inverse the logic | |
36 | return m_defaultState ? !isSel : isSel; | |
37 | } | |
38 | ||
39 | // ---------------------------------------------------------------------------- | |
40 | // Select*() | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
43 | bool wxSelectionStore::SelectItem(size_t item, bool select) | |
44 | { | |
45 | // search for the item ourselves as like this we get the index where to | |
46 | // insert it later if needed, so we do only one search in the array instead | |
47 | // of two (adding item to a sorted array requires a search) | |
48 | size_t index = m_itemsSel.IndexForInsert(item); | |
49 | bool isSel = index < m_itemsSel.GetCount() && m_itemsSel[index] == item; | |
50 | ||
51 | if ( select != m_defaultState ) | |
52 | { | |
53 | if ( !isSel ) | |
54 | { | |
55 | m_itemsSel.AddAt(item, index); | |
56 | ||
57 | return TRUE; | |
58 | } | |
59 | } | |
60 | else // reset to default state | |
61 | { | |
62 | if ( isSel ) | |
63 | { | |
64 | m_itemsSel.RemoveAt(index); | |
65 | return TRUE; | |
66 | } | |
67 | } | |
68 | ||
69 | return FALSE; | |
70 | } | |
71 | ||
72 | bool wxSelectionStore::SelectRange(size_t itemFrom, size_t itemTo, | |
73 | bool select, | |
74 | wxArrayInt *itemsChanged) | |
75 | { | |
76 | // 100 is hardcoded but it shouldn't matter much: the important thing is | |
77 | // that we don't refresh everything when really few (e.g. 1 or 2) items | |
78 | // change state | |
79 | static const size_t MANY_ITEMS = 100; | |
80 | ||
81 | wxASSERT_MSG( itemFrom <= itemTo, _T("should be in order") ); | |
82 | ||
83 | // are we going to have more [un]selected items than the other ones? | |
84 | if ( itemTo - itemFrom > m_count/2 ) | |
85 | { | |
86 | if ( select != m_defaultState ) | |
87 | { | |
88 | // the default state now becomes the same as 'select' | |
89 | m_defaultState = select; | |
90 | ||
91 | // so all the old selections (which had state select) shouldn't be | |
92 | // selected any more, but all the other ones should | |
93 | wxSelectedIndices selOld = m_itemsSel; | |
94 | m_itemsSel.Empty(); | |
95 | ||
96 | // TODO: it should be possible to optimize the searches a bit | |
97 | // knowing the possible range | |
98 | ||
99 | size_t item; | |
100 | for ( item = 0; item < itemFrom; item++ ) | |
101 | { | |
102 | if ( selOld.Index(item) == wxNOT_FOUND ) | |
103 | m_itemsSel.Add(item); | |
104 | } | |
105 | ||
106 | for ( item = itemTo + 1; item < m_count; item++ ) | |
107 | { | |
108 | if ( selOld.Index(item) == wxNOT_FOUND ) | |
109 | m_itemsSel.Add(item); | |
110 | } | |
111 | ||
112 | // many items (> half) changed state | |
113 | itemsChanged = NULL; | |
114 | } | |
115 | else // select == m_defaultState | |
116 | { | |
117 | // get the inclusive range of items between itemFrom and itemTo | |
118 | size_t count = m_itemsSel.GetCount(), | |
119 | start = m_itemsSel.IndexForInsert(itemFrom), | |
120 | end = m_itemsSel.IndexForInsert(itemTo); | |
121 | ||
122 | if ( start == count || m_itemsSel[start] < itemFrom ) | |
123 | { | |
124 | start++; | |
125 | } | |
126 | ||
127 | if ( end == count || m_itemsSel[end] > itemTo ) | |
128 | { | |
129 | end--; | |
130 | } | |
131 | ||
132 | if ( start <= end ) | |
133 | { | |
134 | // delete all of them (from end to avoid changing indices) | |
135 | for ( int i = end; i >= (int)start; i-- ) | |
136 | { | |
137 | if ( itemsChanged ) | |
138 | { | |
139 | if ( itemsChanged->GetCount() > MANY_ITEMS ) | |
140 | { | |
141 | // stop counting (see comment below) | |
142 | itemsChanged = NULL; | |
143 | } | |
144 | else | |
145 | { | |
146 | itemsChanged->Add(m_itemsSel[i]); | |
147 | } | |
148 | } | |
149 | ||
150 | m_itemsSel.RemoveAt(i); | |
151 | } | |
152 | } | |
153 | } | |
154 | } | |
155 | else // "few" items change state | |
156 | { | |
157 | if ( itemsChanged ) | |
158 | { | |
159 | itemsChanged->Empty(); | |
160 | } | |
161 | ||
162 | // just add the items to the selection | |
163 | for ( size_t item = itemFrom; item <= itemTo; item++ ) | |
164 | { | |
165 | if ( SelectItem(item, select) && itemsChanged ) | |
166 | { | |
167 | itemsChanged->Add(item); | |
168 | ||
169 | if ( itemsChanged->GetCount() > MANY_ITEMS ) | |
170 | { | |
171 | // stop counting them, we'll just eat gobs of memory | |
172 | // for nothing at all - faster to refresh everything in | |
173 | // this case | |
174 | itemsChanged = NULL; | |
175 | } | |
176 | } | |
177 | } | |
178 | } | |
179 | ||
180 | // we set it to NULL if there are many items changing state | |
181 | return itemsChanged != NULL; | |
182 | } | |
183 | ||
184 | // ---------------------------------------------------------------------------- | |
185 | // callbacks | |
186 | // ---------------------------------------------------------------------------- | |
187 | ||
188 | void wxSelectionStore::OnItemDelete(size_t item) | |
189 | { | |
190 | size_t count = m_itemsSel.GetCount(), | |
191 | i = m_itemsSel.IndexForInsert(item); | |
192 | ||
193 | if ( i < count && m_itemsSel[i] == item ) | |
194 | { | |
195 | // this item itself was in m_itemsSel, remove it from there | |
196 | m_itemsSel.RemoveAt(i); | |
197 | ||
198 | count--; | |
199 | } | |
200 | ||
201 | // and adjust the index of all which follow it | |
202 | while ( i < count ) | |
203 | { | |
204 | // all following elements must be greater than the one we deleted | |
205 | wxASSERT_MSG( m_itemsSel[i] > item, _T("logic error") ); | |
206 | ||
207 | m_itemsSel[i++]--; | |
208 | } | |
209 | } | |
210 |