Don't send SELECTED events for an already selected item in wxGTK wxListBox.
[wxWidgets.git] / src / common / lboxcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/lboxcmn.cpp
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$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_LISTBOX
28
29 #include "wx/listbox.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/dynarray.h"
33 #include "wx/arrstr.h"
34 #endif
35
36 #include "wx/log.h"
37
38 // ============================================================================
39 // implementation
40 // ============================================================================
41
42 wxListBoxBase::~wxListBoxBase()
43 {
44 // this destructor is required for Darwin
45 }
46
47 // ----------------------------------------------------------------------------
48 // selection
49 // ----------------------------------------------------------------------------
50
51 bool wxListBoxBase::SetStringSelection(const wxString& s, bool select)
52 {
53 const int sel = FindString(s);
54 if ( sel == wxNOT_FOUND )
55 return false;
56
57 SetSelection(sel, select);
58
59 return true;
60 }
61
62 void wxListBoxBase::SetSelection(int n)
63 {
64 if ( !HasMultipleSelection() )
65 DoChangeSingleSelection(n);
66
67 DoSetSelection(n, true);
68 }
69
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();
88 if ( sel != wxNOT_FOUND && sel != itemToLeaveSelected )
89 {
90 Deselect(sel);
91 }
92 }
93 }
94
95 void wxListBoxBase::UpdateOldSelections()
96 {
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__
101 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
102 #endif
103 {
104 GetSelections( m_oldSelections );
105 }
106 }
107
108 bool wxListBoxBase::SendEvent(wxEventType evtType, int item, bool selected)
109 {
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);
123 }
124
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
143 bool wxListBoxBase::CalcAndSendEvent()
144 {
145 wxArrayInt selections;
146 GetSelections(selections);
147 bool selected = true;
148
149 if ( selections.empty() && m_oldSelections.empty() )
150 {
151 // nothing changed, just leave
152 return false;
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++ )
161 {
162 if (selections[idx] != m_oldSelections[idx])
163 {
164 changed = true;
165 break;
166 }
167 }
168
169 // nothing changed, just leave
170 if ( !changed )
171 return false;
172 }
173
174 int item = wxNOT_FOUND;
175 if ( selections.empty() )
176 {
177 selected = false;
178 item = m_oldSelections[0];
179 }
180 else // we [still] have some selections
181 {
182 // Now test if any new item is selected
183 bool any_new_selected = false;
184 for ( size_t idx = 0; idx < countSel; idx++ )
185 {
186 item = selections[idx];
187 if ( m_oldSelections.Index(item) == wxNOT_FOUND )
188 {
189 any_new_selected = true;
190 break;
191 }
192 }
193
194 if ( !any_new_selected )
195 {
196 // No new items selected, now test if any new item is deselected
197 bool any_new_deselected = false;
198 for ( size_t idx = 0; idx < countSelOld; idx++ )
199 {
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
211 selected = false;
212 }
213 else
214 {
215 item = wxNOT_FOUND; // this should be impossible
216 }
217 }
218 }
219
220 wxASSERT_MSG( item != wxNOT_FOUND,
221 "Logic error in wxListBox selection event generation code" );
222
223 m_oldSelections = selections;
224
225 return SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED, item, selected);
226 }
227
228 // ----------------------------------------------------------------------------
229 // misc
230 // ----------------------------------------------------------------------------
231
232 void wxListBoxBase::Command(wxCommandEvent& event)
233 {
234 SetSelection(event.GetInt(), event.GetExtraLong() != 0);
235 (void)GetEventHandler()->ProcessEvent(event);
236 }
237
238 // ----------------------------------------------------------------------------
239 // SetFirstItem() and such
240 // ----------------------------------------------------------------------------
241
242 void wxListBoxBase::SetFirstItem(const wxString& s)
243 {
244 int n = FindString(s);
245
246 wxCHECK_RET( n != wxNOT_FOUND, wxT("invalid string in wxListBox::SetFirstItem") );
247
248 DoSetFirstItem(n);
249 }
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