]>
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 | ||
1e6feb95 VZ |
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(); | |
f644b28c | 80 | if ( sel != wxNOT_FOUND && sel != itemToLeaveSelected ) |
1e6feb95 VZ |
81 | { |
82 | Deselect(sel); | |
83 | } | |
84 | } | |
85 | } | |
86 | ||
05d790f8 RR |
87 | void wxListBoxBase::UpdateOldSelections() |
88 | { | |
a614ffae VS |
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__ | |
05d790f8 | 93 | if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED)) |
a614ffae VS |
94 | #endif |
95 | { | |
05d790f8 | 96 | GetSelections( m_oldSelections ); |
a614ffae | 97 | } |
05d790f8 RR |
98 | } |
99 | ||
53f60d4a | 100 | bool wxListBoxBase::SendEvent(wxEventType evtType, int item, bool selected) |
05d790f8 | 101 | { |
53f60d4a VZ |
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); | |
05d790f8 RR |
115 | } |
116 | ||
53f60d4a | 117 | bool wxListBoxBase::CalcAndSendEvent() |
05d790f8 | 118 | { |
05d790f8 | 119 | wxArrayInt selections; |
7ead3845 | 120 | GetSelections(selections); |
53f60d4a | 121 | bool selected = true; |
7ead3845 VZ |
122 | |
123 | if ( selections.empty() && m_oldSelections.empty() ) | |
124 | { | |
125 | // nothing changed, just leave | |
53f60d4a | 126 | return false; |
7ead3845 VZ |
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++ ) | |
05d790f8 | 135 | { |
7ead3845 | 136 | if (selections[idx] != m_oldSelections[idx]) |
05d790f8 | 137 | { |
7ead3845 VZ |
138 | changed = true; |
139 | break; | |
05d790f8 | 140 | } |
05d790f8 RR |
141 | } |
142 | ||
7ead3845 VZ |
143 | // nothing changed, just leave |
144 | if ( !changed ) | |
53f60d4a | 145 | return false; |
7ead3845 VZ |
146 | } |
147 | ||
148 | int item = wxNOT_FOUND; | |
149 | if ( selections.empty() ) | |
150 | { | |
53f60d4a | 151 | selected = false; |
7ead3845 VZ |
152 | item = m_oldSelections[0]; |
153 | } | |
154 | else // we [still] have some selections | |
155 | { | |
05d790f8 RR |
156 | // Now test if any new item is selected |
157 | bool any_new_selected = false; | |
7ead3845 | 158 | for ( size_t idx = 0; idx < countSel; idx++ ) |
05d790f8 RR |
159 | { |
160 | item = selections[idx]; | |
7ead3845 | 161 | if ( m_oldSelections.Index(item) == wxNOT_FOUND ) |
05d790f8 RR |
162 | { |
163 | any_new_selected = true; | |
164 | break; | |
165 | } | |
166 | } | |
7ead3845 | 167 | |
53f60d4a | 168 | if ( !any_new_selected ) |
05d790f8 | 169 | { |
53f60d4a | 170 | // No new items selected, now test if any new item is deselected |
7ead3845 VZ |
171 | bool any_new_deselected = false; |
172 | for ( size_t idx = 0; idx < countSelOld; idx++ ) | |
05d790f8 | 173 | { |
7ead3845 VZ |
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 | |
53f60d4a | 185 | selected = false; |
7ead3845 VZ |
186 | } |
187 | else | |
188 | { | |
189 | item = wxNOT_FOUND; // this should be impossible | |
05d790f8 RR |
190 | } |
191 | } | |
7ead3845 VZ |
192 | } |
193 | ||
194 | wxASSERT_MSG( item != wxNOT_FOUND, | |
195 | "Logic error in wxListBox selection event generation code" ); | |
196 | ||
197 | m_oldSelections = selections; | |
198 | ||
53f60d4a | 199 | return SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED, item, selected); |
05d790f8 RR |
200 | } |
201 | ||
2ee3ee1b | 202 | // ---------------------------------------------------------------------------- |
6c8a980f | 203 | // misc |
2ee3ee1b VZ |
204 | // ---------------------------------------------------------------------------- |
205 | ||
6c8a980f | 206 | void wxListBoxBase::Command(wxCommandEvent& event) |
2ee3ee1b | 207 | { |
687706f5 | 208 | SetSelection(event.GetInt(), event.GetExtraLong() != 0); |
004867db | 209 | (void)GetEventHandler()->ProcessEvent(event); |
2ee3ee1b VZ |
210 | } |
211 | ||
1e6feb95 VZ |
212 | // ---------------------------------------------------------------------------- |
213 | // SetFirstItem() and such | |
214 | // ---------------------------------------------------------------------------- | |
215 | ||
2ee3ee1b VZ |
216 | void wxListBoxBase::SetFirstItem(const wxString& s) |
217 | { | |
218 | int n = FindString(s); | |
219 | ||
f644b28c | 220 | wxCHECK_RET( n != wxNOT_FOUND, wxT("invalid string in wxListBox::SetFirstItem") ); |
2ee3ee1b VZ |
221 | |
222 | DoSetFirstItem(n); | |
223 | } | |
1e6feb95 VZ |
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 |