]>
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 | |
77ffb593 | 7 | // Copyright: (c) wxWidgets team |
65571936 | 8 | // Licence: wxWindows licence |
2ee3ee1b VZ |
9 | /////////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
2ee3ee1b VZ |
19 | // For compilers that support precompilation, includes "wx.h". |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
1e6feb95 VZ |
26 | #if wxUSE_LISTBOX |
27 | ||
2a673eb1 WS |
28 | #include "wx/listbox.h" |
29 | ||
2ee3ee1b | 30 | #ifndef WX_PRECOMP |
ed39ff57 | 31 | #include "wx/dynarray.h" |
a9711a4d | 32 | #include "wx/arrstr.h" |
f313deaa | 33 | #include "wx/log.h" |
2ee3ee1b VZ |
34 | #endif |
35 | ||
f313deaa | 36 | extern WXDLLEXPORT_DATA(const char) wxListBoxNameStr[] = "listBox"; |
22892dea | 37 | |
2ee3ee1b VZ |
38 | // ============================================================================ |
39 | // implementation | |
40 | // ============================================================================ | |
41 | ||
799ea011 GD |
42 | wxListBoxBase::~wxListBoxBase() |
43 | { | |
44 | // this destructor is required for Darwin | |
45 | } | |
46 | ||
28953245 SC |
47 | // ---------------------------------------------------------------------------- |
48 | // XTI | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | wxDEFINE_FLAGS( wxListBoxStyle ) | |
52 | wxBEGIN_FLAGS( wxListBoxStyle ) | |
53 | // new style border flags, we put them first to | |
54 | // use them for streaming out | |
55 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) | |
56 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
57 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
58 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
59 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
60 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
61 | ||
62 | // old style border flags | |
63 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) | |
64 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
65 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
66 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
67 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
68 | wxFLAGS_MEMBER(wxBORDER) | |
69 | ||
70 | // standard window styles | |
71 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) | |
72 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
73 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
74 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
75 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) | |
76 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) | |
77 | wxFLAGS_MEMBER(wxVSCROLL) | |
78 | wxFLAGS_MEMBER(wxHSCROLL) | |
79 | ||
80 | wxFLAGS_MEMBER(wxLB_SINGLE) | |
81 | wxFLAGS_MEMBER(wxLB_MULTIPLE) | |
82 | wxFLAGS_MEMBER(wxLB_EXTENDED) | |
83 | wxFLAGS_MEMBER(wxLB_HSCROLL) | |
84 | wxFLAGS_MEMBER(wxLB_ALWAYS_SB) | |
85 | wxFLAGS_MEMBER(wxLB_NEEDED_SB) | |
86 | wxFLAGS_MEMBER(wxLB_SORT) | |
87 | wxEND_FLAGS( wxListBoxStyle ) | |
88 | ||
37788684 | 89 | wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControl, "wx/listbox.h") |
28953245 SC |
90 | |
91 | wxBEGIN_PROPERTIES_TABLE(wxListBox) | |
ce7fe42e VZ |
92 | wxEVENT_PROPERTY( Select, wxEVT_LISTBOX, wxCommandEvent ) |
93 | wxEVENT_PROPERTY( DoubleClick, wxEVT_LISTBOX_DCLICK, wxCommandEvent ) | |
28953245 SC |
94 | |
95 | wxPROPERTY( Font, wxFont, SetFont, GetFont , wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \ | |
96 | wxT("Helpstring"), wxT("group")) | |
97 | wxPROPERTY_COLLECTION( Choices, wxArrayString, wxString, AppendString, \ | |
98 | GetStrings, 0 /*flags*/, wxT("Helpstring"), wxT("group") ) | |
99 | wxPROPERTY( Selection, int, SetSelection, GetSelection, wxEMPTY_PARAMETER_VALUE, \ | |
100 | 0 /*flags*/, wxT("Helpstring"), wxT("group") ) | |
101 | ||
102 | wxPROPERTY_FLAGS( WindowStyle, wxListBoxStyle, long, SetWindowStyleFlag, \ | |
103 | GetWindowStyleFlag, wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \ | |
104 | wxT("Helpstring"), wxT("group")) // style | |
105 | wxEND_PROPERTIES_TABLE() | |
106 | ||
107 | wxEMPTY_HANDLERS_TABLE(wxListBox) | |
108 | ||
109 | wxCONSTRUCTOR_4( wxListBox, wxWindow*, Parent, wxWindowID, Id, \ | |
110 | wxPoint, Position, wxSize, Size ) | |
111 | ||
112 | /* | |
113 | TODO PROPERTIES | |
114 | selection | |
115 | content | |
116 | item | |
117 | */ | |
118 | ||
2ee3ee1b VZ |
119 | // ---------------------------------------------------------------------------- |
120 | // selection | |
121 | // ---------------------------------------------------------------------------- | |
122 | ||
2ee3ee1b VZ |
123 | bool wxListBoxBase::SetStringSelection(const wxString& s, bool select) |
124 | { | |
95668975 VZ |
125 | const int sel = FindString(s); |
126 | if ( sel == wxNOT_FOUND ) | |
127 | return false; | |
2ee3ee1b VZ |
128 | |
129 | SetSelection(sel, select); | |
130 | ||
f644b28c | 131 | return true; |
2ee3ee1b VZ |
132 | } |
133 | ||
24ee1bef VZ |
134 | void wxListBoxBase::SetSelection(int n) |
135 | { | |
136 | if ( !HasMultipleSelection() ) | |
137 | DoChangeSingleSelection(n); | |
138 | ||
139 | DoSetSelection(n, true); | |
140 | } | |
141 | ||
1e6feb95 VZ |
142 | void wxListBoxBase::DeselectAll(int itemToLeaveSelected) |
143 | { | |
144 | if ( HasMultipleSelection() ) | |
145 | { | |
146 | wxArrayInt selections; | |
147 | GetSelections(selections); | |
148 | ||
149 | size_t count = selections.GetCount(); | |
150 | for ( size_t n = 0; n < count; n++ ) | |
151 | { | |
152 | int item = selections[n]; | |
153 | if ( item != itemToLeaveSelected ) | |
154 | Deselect(item); | |
155 | } | |
156 | } | |
157 | else // single selection | |
158 | { | |
159 | int sel = GetSelection(); | |
f644b28c | 160 | if ( sel != wxNOT_FOUND && sel != itemToLeaveSelected ) |
1e6feb95 VZ |
161 | { |
162 | Deselect(sel); | |
163 | } | |
164 | } | |
165 | } | |
166 | ||
05d790f8 RR |
167 | void wxListBoxBase::UpdateOldSelections() |
168 | { | |
0c9dd3b6 VZ |
169 | // When the control becomes empty, any previously remembered selections are |
170 | // invalid anyhow, so just forget them. | |
171 | if ( IsEmpty() ) | |
172 | { | |
173 | m_oldSelections.clear(); | |
174 | return; | |
175 | } | |
176 | ||
a614ffae VS |
177 | // We need to remember the selection even in single-selection case on |
178 | // Windows, so that we don't send an event when the user clicks on an | |
179 | // already selected item. | |
180 | #ifndef __WXMSW__ | |
05d790f8 | 181 | if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED)) |
a614ffae VS |
182 | #endif |
183 | { | |
05d790f8 | 184 | GetSelections( m_oldSelections ); |
a614ffae | 185 | } |
05d790f8 RR |
186 | } |
187 | ||
53f60d4a | 188 | bool wxListBoxBase::SendEvent(wxEventType evtType, int item, bool selected) |
05d790f8 | 189 | { |
53f60d4a VZ |
190 | wxCommandEvent event(evtType, GetId()); |
191 | event.SetEventObject(this); | |
192 | ||
193 | event.SetInt(item); | |
194 | event.SetString(GetString(item)); | |
195 | event.SetExtraLong(selected); | |
196 | ||
197 | if ( HasClientObjectData() ) | |
198 | event.SetClientObject(GetClientObject(item)); | |
199 | else if ( HasClientUntypedData() ) | |
200 | event.SetClientData(GetClientData(item)); | |
201 | ||
202 | return HandleWindowEvent(event); | |
05d790f8 RR |
203 | } |
204 | ||
24ee1bef VZ |
205 | bool wxListBoxBase::DoChangeSingleSelection(int item) |
206 | { | |
207 | // As we don't use m_oldSelections in single selection mode, we store the | |
208 | // last item that we notified the user about in it in this case because we | |
209 | // need to remember it to be able to filter out the dummy selection changes | |
210 | // that we get when the user clicks on an already selected item. | |
211 | if ( !m_oldSelections.empty() && *m_oldSelections.begin() == item ) | |
212 | { | |
213 | // Same item as the last time. | |
214 | return false; | |
215 | } | |
216 | ||
217 | m_oldSelections.clear(); | |
218 | m_oldSelections.push_back(item); | |
219 | ||
220 | return true; | |
221 | } | |
222 | ||
53f60d4a | 223 | bool wxListBoxBase::CalcAndSendEvent() |
05d790f8 | 224 | { |
05d790f8 | 225 | wxArrayInt selections; |
7ead3845 | 226 | GetSelections(selections); |
53f60d4a | 227 | bool selected = true; |
7ead3845 VZ |
228 | |
229 | if ( selections.empty() && m_oldSelections.empty() ) | |
230 | { | |
231 | // nothing changed, just leave | |
53f60d4a | 232 | return false; |
7ead3845 VZ |
233 | } |
234 | ||
235 | const size_t countSel = selections.size(), | |
236 | countSelOld = m_oldSelections.size(); | |
237 | if ( countSel == countSelOld ) | |
238 | { | |
239 | bool changed = false; | |
240 | for ( size_t idx = 0; idx < countSel; idx++ ) | |
05d790f8 | 241 | { |
7ead3845 | 242 | if (selections[idx] != m_oldSelections[idx]) |
05d790f8 | 243 | { |
7ead3845 VZ |
244 | changed = true; |
245 | break; | |
05d790f8 | 246 | } |
05d790f8 RR |
247 | } |
248 | ||
7ead3845 VZ |
249 | // nothing changed, just leave |
250 | if ( !changed ) | |
53f60d4a | 251 | return false; |
7ead3845 VZ |
252 | } |
253 | ||
254 | int item = wxNOT_FOUND; | |
255 | if ( selections.empty() ) | |
256 | { | |
53f60d4a | 257 | selected = false; |
7ead3845 VZ |
258 | item = m_oldSelections[0]; |
259 | } | |
260 | else // we [still] have some selections | |
261 | { | |
05d790f8 RR |
262 | // Now test if any new item is selected |
263 | bool any_new_selected = false; | |
7ead3845 | 264 | for ( size_t idx = 0; idx < countSel; idx++ ) |
05d790f8 RR |
265 | { |
266 | item = selections[idx]; | |
7ead3845 | 267 | if ( m_oldSelections.Index(item) == wxNOT_FOUND ) |
05d790f8 RR |
268 | { |
269 | any_new_selected = true; | |
270 | break; | |
271 | } | |
272 | } | |
7ead3845 | 273 | |
53f60d4a | 274 | if ( !any_new_selected ) |
05d790f8 | 275 | { |
53f60d4a | 276 | // No new items selected, now test if any new item is deselected |
7ead3845 VZ |
277 | bool any_new_deselected = false; |
278 | for ( size_t idx = 0; idx < countSelOld; idx++ ) | |
05d790f8 | 279 | { |
7ead3845 VZ |
280 | item = m_oldSelections[idx]; |
281 | if ( selections.Index(item) == wxNOT_FOUND ) | |
282 | { | |
283 | any_new_deselected = true; | |
284 | break; | |
285 | } | |
286 | } | |
287 | ||
288 | if ( any_new_deselected ) | |
289 | { | |
290 | // indicate that this is a selection | |
53f60d4a | 291 | selected = false; |
7ead3845 VZ |
292 | } |
293 | else | |
294 | { | |
295 | item = wxNOT_FOUND; // this should be impossible | |
05d790f8 RR |
296 | } |
297 | } | |
7ead3845 VZ |
298 | } |
299 | ||
300 | wxASSERT_MSG( item != wxNOT_FOUND, | |
301 | "Logic error in wxListBox selection event generation code" ); | |
302 | ||
303 | m_oldSelections = selections; | |
304 | ||
ce7fe42e | 305 | return SendEvent(wxEVT_LISTBOX, item, selected); |
05d790f8 RR |
306 | } |
307 | ||
2ee3ee1b | 308 | // ---------------------------------------------------------------------------- |
6c8a980f | 309 | // misc |
2ee3ee1b VZ |
310 | // ---------------------------------------------------------------------------- |
311 | ||
6c8a980f | 312 | void wxListBoxBase::Command(wxCommandEvent& event) |
2ee3ee1b | 313 | { |
687706f5 | 314 | SetSelection(event.GetInt(), event.GetExtraLong() != 0); |
004867db | 315 | (void)GetEventHandler()->ProcessEvent(event); |
2ee3ee1b VZ |
316 | } |
317 | ||
1e6feb95 VZ |
318 | // ---------------------------------------------------------------------------- |
319 | // SetFirstItem() and such | |
320 | // ---------------------------------------------------------------------------- | |
321 | ||
2ee3ee1b VZ |
322 | void wxListBoxBase::SetFirstItem(const wxString& s) |
323 | { | |
324 | int n = FindString(s); | |
325 | ||
f644b28c | 326 | wxCHECK_RET( n != wxNOT_FOUND, wxT("invalid string in wxListBox::SetFirstItem") ); |
2ee3ee1b VZ |
327 | |
328 | DoSetFirstItem(n); | |
329 | } | |
1e6feb95 VZ |
330 | |
331 | void wxListBoxBase::AppendAndEnsureVisible(const wxString& s) | |
332 | { | |
333 | Append(s); | |
334 | EnsureVisible(GetCount() - 1); | |
335 | } | |
336 | ||
337 | void wxListBoxBase::EnsureVisible(int WXUNUSED(n)) | |
338 | { | |
339 | // the base class version does nothing (the only alternative would be to | |
340 | // call SetFirstItem() but this is probably even more stupid) | |
341 | } | |
342 | ||
343 | #endif // wxUSE_LISTBOX |