]> git.saurik.com Git - wxWidgets.git/blob - src/common/lboxcmn.cpp
Send generic wxTreeCtrl wxEVT_COMMAND_TREE_KEY_DOWN events from OnKeyDown rather...
[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::DeselectAll(int itemToLeaveSelected)
63 {
64 if ( HasMultipleSelection() )
65 {
66 wxArrayInt selections;
67 GetSelections(selections);
68
69 size_t count = selections.GetCount();
70 for ( size_t n = 0; n < count; n++ )
71 {
72 int item = selections[n];
73 if ( item != itemToLeaveSelected )
74 Deselect(item);
75 }
76 }
77 else // single selection
78 {
79 int sel = GetSelection();
80 if ( sel != wxNOT_FOUND && sel != itemToLeaveSelected )
81 {
82 Deselect(sel);
83 }
84 }
85 }
86
87 void wxListBoxBase::UpdateOldSelections()
88 {
89 // We need to remember the selection even in single-selection case on
90 // Windows, so that we don't send an event when the user clicks on an
91 // already selected item.
92 #ifndef __WXMSW__
93 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
94 #endif
95 {
96 GetSelections( m_oldSelections );
97 }
98 }
99
100 bool wxListBoxBase::SendEvent(wxEventType evtType, int item, bool selected)
101 {
102 wxCommandEvent event(evtType, GetId());
103 event.SetEventObject(this);
104
105 event.SetInt(item);
106 event.SetString(GetString(item));
107 event.SetExtraLong(selected);
108
109 if ( HasClientObjectData() )
110 event.SetClientObject(GetClientObject(item));
111 else if ( HasClientUntypedData() )
112 event.SetClientData(GetClientData(item));
113
114 return HandleWindowEvent(event);
115 }
116
117 bool wxListBoxBase::CalcAndSendEvent()
118 {
119 wxArrayInt selections;
120 GetSelections(selections);
121 bool selected = true;
122
123 if ( selections.empty() && m_oldSelections.empty() )
124 {
125 // nothing changed, just leave
126 return false;
127 }
128
129 const size_t countSel = selections.size(),
130 countSelOld = m_oldSelections.size();
131 if ( countSel == countSelOld )
132 {
133 bool changed = false;
134 for ( size_t idx = 0; idx < countSel; idx++ )
135 {
136 if (selections[idx] != m_oldSelections[idx])
137 {
138 changed = true;
139 break;
140 }
141 }
142
143 // nothing changed, just leave
144 if ( !changed )
145 return false;
146 }
147
148 int item = wxNOT_FOUND;
149 if ( selections.empty() )
150 {
151 selected = false;
152 item = m_oldSelections[0];
153 }
154 else // we [still] have some selections
155 {
156 // Now test if any new item is selected
157 bool any_new_selected = false;
158 for ( size_t idx = 0; idx < countSel; idx++ )
159 {
160 item = selections[idx];
161 if ( m_oldSelections.Index(item) == wxNOT_FOUND )
162 {
163 any_new_selected = true;
164 break;
165 }
166 }
167
168 if ( !any_new_selected )
169 {
170 // No new items selected, now test if any new item is deselected
171 bool any_new_deselected = false;
172 for ( size_t idx = 0; idx < countSelOld; idx++ )
173 {
174 item = m_oldSelections[idx];
175 if ( selections.Index(item) == wxNOT_FOUND )
176 {
177 any_new_deselected = true;
178 break;
179 }
180 }
181
182 if ( any_new_deselected )
183 {
184 // indicate that this is a selection
185 selected = false;
186 }
187 else
188 {
189 item = wxNOT_FOUND; // this should be impossible
190 }
191 }
192 }
193
194 wxASSERT_MSG( item != wxNOT_FOUND,
195 "Logic error in wxListBox selection event generation code" );
196
197 m_oldSelections = selections;
198
199 return SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED, item, selected);
200 }
201
202 // ----------------------------------------------------------------------------
203 // misc
204 // ----------------------------------------------------------------------------
205
206 void wxListBoxBase::Command(wxCommandEvent& event)
207 {
208 SetSelection(event.GetInt(), event.GetExtraLong() != 0);
209 (void)GetEventHandler()->ProcessEvent(event);
210 }
211
212 // ----------------------------------------------------------------------------
213 // SetFirstItem() and such
214 // ----------------------------------------------------------------------------
215
216 void wxListBoxBase::SetFirstItem(const wxString& s)
217 {
218 int n = FindString(s);
219
220 wxCHECK_RET( n != wxNOT_FOUND, wxT("invalid string in wxListBox::SetFirstItem") );
221
222 DoSetFirstItem(n);
223 }
224
225 void wxListBoxBase::AppendAndEnsureVisible(const wxString& s)
226 {
227 Append(s);
228 EnsureVisible(GetCount() - 1);
229 }
230
231 void wxListBoxBase::EnsureVisible(int WXUNUSED(n))
232 {
233 // the base class version does nothing (the only alternative would be to
234 // call SetFirstItem() but this is probably even more stupid)
235 }
236
237 #endif // wxUSE_LISTBOX