Move event generation code for multiple selection mode to common code
[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 // ============================================================================
37 // implementation
38 // ============================================================================
39
40 wxListBoxBase::~wxListBoxBase()
41 {
42 // this destructor is required for Darwin
43 }
44
45 // ----------------------------------------------------------------------------
46 // selection
47 // ----------------------------------------------------------------------------
48
49 bool wxListBoxBase::SetStringSelection(const wxString& s, bool select)
50 {
51 const int sel = FindString(s);
52 if ( sel == wxNOT_FOUND )
53 return false;
54
55 SetSelection(sel, select);
56
57 return true;
58 }
59
60 void wxListBoxBase::DeselectAll(int itemToLeaveSelected)
61 {
62 if ( HasMultipleSelection() )
63 {
64 wxArrayInt selections;
65 GetSelections(selections);
66
67 size_t count = selections.GetCount();
68 for ( size_t n = 0; n < count; n++ )
69 {
70 int item = selections[n];
71 if ( item != itemToLeaveSelected )
72 Deselect(item);
73 }
74 }
75 else // single selection
76 {
77 int sel = GetSelection();
78 if ( sel != wxNOT_FOUND && sel != itemToLeaveSelected )
79 {
80 Deselect(sel);
81 }
82 }
83 }
84
85 void wxListBoxBase::UpdateOldSelections()
86 {
87 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
88 GetSelections( m_oldSelections );
89 }
90
91 static void LBSendEvent( wxCommandEvent &event, wxListBoxBase *listbox, int item )
92 {
93 event.SetInt( item );
94 event.SetString( listbox->GetString( item ) );
95 if ( listbox->HasClientObjectData() )
96 event.SetClientObject( listbox->GetClientObject(item) );
97 else if ( listbox->HasClientUntypedData() )
98 event.SetClientData( listbox->GetClientData(item) );
99 listbox->HandleWindowEvent( event );
100 }
101
102 void wxListBoxBase::CalcAndSendEvent()
103 {
104 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, GetId() );
105 event.SetEventObject( this );
106
107 wxArrayInt selections;
108 GetSelections( selections );
109
110 if ((selections.GetCount() == 0) && (m_oldSelections.GetCount() == 0))
111 {
112 // nothing changed, just leave
113 return;
114 }
115
116 if (selections.GetCount() == m_oldSelections.GetCount())
117 {
118 bool changed = false;
119 size_t idx;
120 for (idx = 0; idx < selections.GetCount(); idx++)
121 {
122 if (selections[idx] != m_oldSelections[idx])
123 {
124 changed = true;
125 break;
126 }
127 }
128
129 // nothing changed, just leave
130 if (!changed)
131 return;
132 }
133
134 if (selections.GetCount() == 0)
135 {
136 // indicate that this is a deselection
137 event.SetExtraLong( 0 );
138 int item = m_oldSelections[0];
139 m_oldSelections = selections;
140 LBSendEvent( event, this, item );
141 return;
142 }
143
144 int item;
145 // Now test if any new item is selected
146 bool any_new_selected = false;
147 size_t idx;
148 for (idx = 0; idx < selections.GetCount(); idx++)
149 {
150 item = selections[idx];
151 if (m_oldSelections.Index(item) == wxNOT_FOUND)
152 {
153 any_new_selected = true;
154 break;
155 }
156 }
157
158 if (any_new_selected)
159 {
160 // indicate that this is a selection
161 event.SetExtraLong( 1 );
162 m_oldSelections = selections;
163 LBSendEvent( event, this, item );
164 return;
165 }
166
167 // Now test if any new item is deselected
168 bool any_new_deselected = false;
169 for (idx = 0; idx < m_oldSelections.GetCount(); idx++)
170 {
171 item = m_oldSelections[idx];
172 if (selections.Index(item) == wxNOT_FOUND)
173 {
174 any_new_deselected = true;
175 break;
176 }
177 }
178
179 if (any_new_deselected)
180 {
181 // indicate that this is a selection
182 event.SetExtraLong( 0 );
183 m_oldSelections = selections;
184 LBSendEvent( event, this, item );
185 return;
186 }
187
188 wxLogError( wxT("Wrong wxListBox selection") );
189 }
190
191 // ----------------------------------------------------------------------------
192 // misc
193 // ----------------------------------------------------------------------------
194
195 void wxListBoxBase::Command(wxCommandEvent& event)
196 {
197 SetSelection(event.GetInt(), event.GetExtraLong() != 0);
198 (void)ProcessEvent(event);
199 }
200
201 // ----------------------------------------------------------------------------
202 // SetFirstItem() and such
203 // ----------------------------------------------------------------------------
204
205 void wxListBoxBase::SetFirstItem(const wxString& s)
206 {
207 int n = FindString(s);
208
209 wxCHECK_RET( n != wxNOT_FOUND, wxT("invalid string in wxListBox::SetFirstItem") );
210
211 DoSetFirstItem(n);
212 }
213
214 void wxListBoxBase::AppendAndEnsureVisible(const wxString& s)
215 {
216 Append(s);
217 EnsureVisible(GetCount() - 1);
218 }
219
220 void wxListBoxBase::EnsureVisible(int WXUNUSED(n))
221 {
222 // the base class version does nothing (the only alternative would be to
223 // call SetFirstItem() but this is probably even more stupid)
224 }
225
226 #endif // wxUSE_LISTBOX