]>
Commit | Line | Data |
---|---|---|
2ee3ee1b | 1 | /////////////////////////////////////////////////////////////////////////////// |
aa61d352 | 2 | // Name: src/common/lboxcmn.cpp |
2ee3ee1b VZ |
3 | // Purpose: wxListBox class methods common to all platforms |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 22.10.99 | |
7 | // RCS-ID: $Id$ | |
77ffb593 | 8 | // Copyright: (c) wxWidgets team |
65571936 | 9 | // Licence: wxWindows licence |
2ee3ee1b VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2ee3ee1b VZ |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
1e6feb95 VZ |
27 | #if wxUSE_LISTBOX |
28 | ||
2a673eb1 WS |
29 | #include "wx/listbox.h" |
30 | ||
2ee3ee1b | 31 | #ifndef WX_PRECOMP |
ed39ff57 | 32 | #include "wx/dynarray.h" |
a9711a4d | 33 | #include "wx/arrstr.h" |
2ee3ee1b VZ |
34 | #endif |
35 | ||
22892dea RR |
36 | #include "wx/log.h" |
37 | ||
2ee3ee1b VZ |
38 | // ============================================================================ |
39 | // implementation | |
40 | // ============================================================================ | |
41 | ||
799ea011 GD |
42 | wxListBoxBase::~wxListBoxBase() |
43 | { | |
44 | // this destructor is required for Darwin | |
45 | } | |
46 | ||
2ee3ee1b VZ |
47 | // ---------------------------------------------------------------------------- |
48 | // selection | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
2ee3ee1b VZ |
51 | bool wxListBoxBase::SetStringSelection(const wxString& s, bool select) |
52 | { | |
95668975 VZ |
53 | const int sel = FindString(s); |
54 | if ( sel == wxNOT_FOUND ) | |
55 | return false; | |
2ee3ee1b VZ |
56 | |
57 | SetSelection(sel, select); | |
58 | ||
f644b28c | 59 | return true; |
2ee3ee1b VZ |
60 | } |
61 | ||
24ee1bef VZ |
62 | void wxListBoxBase::SetSelection(int n) |
63 | { | |
64 | if ( !HasMultipleSelection() ) | |
65 | DoChangeSingleSelection(n); | |
66 | ||
67 | DoSetSelection(n, true); | |
68 | } | |
69 | ||
1e6feb95 VZ |
70 | void wxListBoxBase::DeselectAll(int itemToLeaveSelected) |
71 | { | |
72 | if ( HasMultipleSelection() ) | |
73 | { | |
74 | wxArrayInt selections; | |
75 | GetSelections(selections); | |
76 | ||
77 | size_t count = selections.GetCount(); | |
78 | for ( size_t n = 0; n < count; n++ ) | |
79 | { | |
80 | int item = selections[n]; | |
81 | if ( item != itemToLeaveSelected ) | |
82 | Deselect(item); | |
83 | } | |
84 | } | |
85 | else // single selection | |
86 | { | |
87 | int sel = GetSelection(); | |
f644b28c | 88 | if ( sel != wxNOT_FOUND && sel != itemToLeaveSelected ) |
1e6feb95 VZ |
89 | { |
90 | Deselect(sel); | |
91 | } | |
92 | } | |
93 | } | |
94 | ||
05d790f8 RR |
95 | void wxListBoxBase::UpdateOldSelections() |
96 | { | |
a614ffae VS |
97 | // We need to remember the selection even in single-selection case on |
98 | // Windows, so that we don't send an event when the user clicks on an | |
99 | // already selected item. | |
100 | #ifndef __WXMSW__ | |
05d790f8 | 101 | if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED)) |
a614ffae VS |
102 | #endif |
103 | { | |
05d790f8 | 104 | GetSelections( m_oldSelections ); |
a614ffae | 105 | } |
05d790f8 RR |
106 | } |
107 | ||
53f60d4a | 108 | bool wxListBoxBase::SendEvent(wxEventType evtType, int item, bool selected) |
05d790f8 | 109 | { |
53f60d4a VZ |
110 | wxCommandEvent event(evtType, GetId()); |
111 | event.SetEventObject(this); | |
112 | ||
113 | event.SetInt(item); | |
114 | event.SetString(GetString(item)); | |
115 | event.SetExtraLong(selected); | |
116 | ||
117 | if ( HasClientObjectData() ) | |
118 | event.SetClientObject(GetClientObject(item)); | |
119 | else if ( HasClientUntypedData() ) | |
120 | event.SetClientData(GetClientData(item)); | |
121 | ||
122 | return HandleWindowEvent(event); | |
05d790f8 RR |
123 | } |
124 | ||
24ee1bef VZ |
125 | bool wxListBoxBase::DoChangeSingleSelection(int item) |
126 | { | |
127 | // As we don't use m_oldSelections in single selection mode, we store the | |
128 | // last item that we notified the user about in it in this case because we | |
129 | // need to remember it to be able to filter out the dummy selection changes | |
130 | // that we get when the user clicks on an already selected item. | |
131 | if ( !m_oldSelections.empty() && *m_oldSelections.begin() == item ) | |
132 | { | |
133 | // Same item as the last time. | |
134 | return false; | |
135 | } | |
136 | ||
137 | m_oldSelections.clear(); | |
138 | m_oldSelections.push_back(item); | |
139 | ||
140 | return true; | |
141 | } | |
142 | ||
53f60d4a | 143 | bool wxListBoxBase::CalcAndSendEvent() |
05d790f8 | 144 | { |
05d790f8 | 145 | wxArrayInt selections; |
7ead3845 | 146 | GetSelections(selections); |
53f60d4a | 147 | bool selected = true; |
7ead3845 VZ |
148 | |
149 | if ( selections.empty() && m_oldSelections.empty() ) | |
150 | { | |
151 | // nothing changed, just leave | |
53f60d4a | 152 | return false; |
7ead3845 VZ |
153 | } |
154 | ||
155 | const size_t countSel = selections.size(), | |
156 | countSelOld = m_oldSelections.size(); | |
157 | if ( countSel == countSelOld ) | |
158 | { | |
159 | bool changed = false; | |
160 | for ( size_t idx = 0; idx < countSel; idx++ ) | |
05d790f8 | 161 | { |
7ead3845 | 162 | if (selections[idx] != m_oldSelections[idx]) |
05d790f8 | 163 | { |
7ead3845 VZ |
164 | changed = true; |
165 | break; | |
05d790f8 | 166 | } |
05d790f8 RR |
167 | } |
168 | ||
7ead3845 VZ |
169 | // nothing changed, just leave |
170 | if ( !changed ) | |
53f60d4a | 171 | return false; |
7ead3845 VZ |
172 | } |
173 | ||
174 | int item = wxNOT_FOUND; | |
175 | if ( selections.empty() ) | |
176 | { | |
53f60d4a | 177 | selected = false; |
7ead3845 VZ |
178 | item = m_oldSelections[0]; |
179 | } | |
180 | else // we [still] have some selections | |
181 | { | |
05d790f8 RR |
182 | // Now test if any new item is selected |
183 | bool any_new_selected = false; | |
7ead3845 | 184 | for ( size_t idx = 0; idx < countSel; idx++ ) |
05d790f8 RR |
185 | { |
186 | item = selections[idx]; | |
7ead3845 | 187 | if ( m_oldSelections.Index(item) == wxNOT_FOUND ) |
05d790f8 RR |
188 | { |
189 | any_new_selected = true; | |
190 | break; | |
191 | } | |
192 | } | |
7ead3845 | 193 | |
53f60d4a | 194 | if ( !any_new_selected ) |
05d790f8 | 195 | { |
53f60d4a | 196 | // No new items selected, now test if any new item is deselected |
7ead3845 VZ |
197 | bool any_new_deselected = false; |
198 | for ( size_t idx = 0; idx < countSelOld; idx++ ) | |
05d790f8 | 199 | { |
7ead3845 VZ |
200 | item = m_oldSelections[idx]; |
201 | if ( selections.Index(item) == wxNOT_FOUND ) | |
202 | { | |
203 | any_new_deselected = true; | |
204 | break; | |
205 | } | |
206 | } | |
207 | ||
208 | if ( any_new_deselected ) | |
209 | { | |
210 | // indicate that this is a selection | |
53f60d4a | 211 | selected = false; |
7ead3845 VZ |
212 | } |
213 | else | |
214 | { | |
215 | item = wxNOT_FOUND; // this should be impossible | |
05d790f8 RR |
216 | } |
217 | } | |
7ead3845 VZ |
218 | } |
219 | ||
220 | wxASSERT_MSG( item != wxNOT_FOUND, | |
221 | "Logic error in wxListBox selection event generation code" ); | |
222 | ||
223 | m_oldSelections = selections; | |
224 | ||
53f60d4a | 225 | return SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED, item, selected); |
05d790f8 RR |
226 | } |
227 | ||
2ee3ee1b | 228 | // ---------------------------------------------------------------------------- |
6c8a980f | 229 | // misc |
2ee3ee1b VZ |
230 | // ---------------------------------------------------------------------------- |
231 | ||
6c8a980f | 232 | void wxListBoxBase::Command(wxCommandEvent& event) |
2ee3ee1b | 233 | { |
687706f5 | 234 | SetSelection(event.GetInt(), event.GetExtraLong() != 0); |
004867db | 235 | (void)GetEventHandler()->ProcessEvent(event); |
2ee3ee1b VZ |
236 | } |
237 | ||
1e6feb95 VZ |
238 | // ---------------------------------------------------------------------------- |
239 | // SetFirstItem() and such | |
240 | // ---------------------------------------------------------------------------- | |
241 | ||
2ee3ee1b VZ |
242 | void wxListBoxBase::SetFirstItem(const wxString& s) |
243 | { | |
244 | int n = FindString(s); | |
245 | ||
f644b28c | 246 | wxCHECK_RET( n != wxNOT_FOUND, wxT("invalid string in wxListBox::SetFirstItem") ); |
2ee3ee1b VZ |
247 | |
248 | DoSetFirstItem(n); | |
249 | } | |
1e6feb95 VZ |
250 | |
251 | void wxListBoxBase::AppendAndEnsureVisible(const wxString& s) | |
252 | { | |
253 | Append(s); | |
254 | EnsureVisible(GetCount() - 1); | |
255 | } | |
256 | ||
257 | void wxListBoxBase::EnsureVisible(int WXUNUSED(n)) | |
258 | { | |
259 | // the base class version does nothing (the only alternative would be to | |
260 | // call SetFirstItem() but this is probably even more stupid) | |
261 | } | |
262 | ||
263 | #endif // wxUSE_LISTBOX |