]> git.saurik.com Git - wxWidgets.git/blame - src/common/lboxcmn.cpp
Use wxControl as wxComboBox base class for wxRTTI in all ports.
[wxWidgets.git] / src / common / lboxcmn.cpp
CommitLineData
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
42wxListBoxBase::~wxListBoxBase()
43{
44 // this destructor is required for Darwin
45}
46
28953245
SC
47// ----------------------------------------------------------------------------
48// XTI
49// ----------------------------------------------------------------------------
50
51wxDEFINE_FLAGS( wxListBoxStyle )
52wxBEGIN_FLAGS( wxListBoxStyle )
53// new style border flags, we put them first to
54// use them for streaming out
55wxFLAGS_MEMBER(wxBORDER_SIMPLE)
56wxFLAGS_MEMBER(wxBORDER_SUNKEN)
57wxFLAGS_MEMBER(wxBORDER_DOUBLE)
58wxFLAGS_MEMBER(wxBORDER_RAISED)
59wxFLAGS_MEMBER(wxBORDER_STATIC)
60wxFLAGS_MEMBER(wxBORDER_NONE)
61
62// old style border flags
63wxFLAGS_MEMBER(wxSIMPLE_BORDER)
64wxFLAGS_MEMBER(wxSUNKEN_BORDER)
65wxFLAGS_MEMBER(wxDOUBLE_BORDER)
66wxFLAGS_MEMBER(wxRAISED_BORDER)
67wxFLAGS_MEMBER(wxSTATIC_BORDER)
68wxFLAGS_MEMBER(wxBORDER)
69
70// standard window styles
71wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
72wxFLAGS_MEMBER(wxCLIP_CHILDREN)
73wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
74wxFLAGS_MEMBER(wxWANTS_CHARS)
75wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
76wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
77wxFLAGS_MEMBER(wxVSCROLL)
78wxFLAGS_MEMBER(wxHSCROLL)
79
80wxFLAGS_MEMBER(wxLB_SINGLE)
81wxFLAGS_MEMBER(wxLB_MULTIPLE)
82wxFLAGS_MEMBER(wxLB_EXTENDED)
83wxFLAGS_MEMBER(wxLB_HSCROLL)
84wxFLAGS_MEMBER(wxLB_ALWAYS_SB)
85wxFLAGS_MEMBER(wxLB_NEEDED_SB)
86wxFLAGS_MEMBER(wxLB_SORT)
87wxEND_FLAGS( wxListBoxStyle )
88
89wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControlWithItems, "wx/listbox.h")
90
91wxBEGIN_PROPERTIES_TABLE(wxListBox)
92wxEVENT_PROPERTY( Select, wxEVT_COMMAND_LISTBOX_SELECTED, wxCommandEvent )
93wxEVENT_PROPERTY( DoubleClick, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, wxCommandEvent )
94
95wxPROPERTY( Font, wxFont, SetFont, GetFont , wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \
96 wxT("Helpstring"), wxT("group"))
97wxPROPERTY_COLLECTION( Choices, wxArrayString, wxString, AppendString, \
98 GetStrings, 0 /*flags*/, wxT("Helpstring"), wxT("group") )
99wxPROPERTY( Selection, int, SetSelection, GetSelection, wxEMPTY_PARAMETER_VALUE, \
100 0 /*flags*/, wxT("Helpstring"), wxT("group") )
101
102wxPROPERTY_FLAGS( WindowStyle, wxListBoxStyle, long, SetWindowStyleFlag, \
103 GetWindowStyleFlag, wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \
104 wxT("Helpstring"), wxT("group")) // style
105wxEND_PROPERTIES_TABLE()
106
107wxEMPTY_HANDLERS_TABLE(wxListBox)
108
109wxCONSTRUCTOR_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
123bool 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
134void wxListBoxBase::SetSelection(int n)
135{
136 if ( !HasMultipleSelection() )
137 DoChangeSingleSelection(n);
138
139 DoSetSelection(n, true);
140}
141
1e6feb95
VZ
142void 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
167void wxListBoxBase::UpdateOldSelections()
168{
a614ffae
VS
169 // We need to remember the selection even in single-selection case on
170 // Windows, so that we don't send an event when the user clicks on an
171 // already selected item.
172#ifndef __WXMSW__
05d790f8 173 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
a614ffae
VS
174#endif
175 {
05d790f8 176 GetSelections( m_oldSelections );
a614ffae 177 }
05d790f8
RR
178}
179
53f60d4a 180bool wxListBoxBase::SendEvent(wxEventType evtType, int item, bool selected)
05d790f8 181{
53f60d4a
VZ
182 wxCommandEvent event(evtType, GetId());
183 event.SetEventObject(this);
184
185 event.SetInt(item);
186 event.SetString(GetString(item));
187 event.SetExtraLong(selected);
188
189 if ( HasClientObjectData() )
190 event.SetClientObject(GetClientObject(item));
191 else if ( HasClientUntypedData() )
192 event.SetClientData(GetClientData(item));
193
194 return HandleWindowEvent(event);
05d790f8
RR
195}
196
24ee1bef
VZ
197bool wxListBoxBase::DoChangeSingleSelection(int item)
198{
199 // As we don't use m_oldSelections in single selection mode, we store the
200 // last item that we notified the user about in it in this case because we
201 // need to remember it to be able to filter out the dummy selection changes
202 // that we get when the user clicks on an already selected item.
203 if ( !m_oldSelections.empty() && *m_oldSelections.begin() == item )
204 {
205 // Same item as the last time.
206 return false;
207 }
208
209 m_oldSelections.clear();
210 m_oldSelections.push_back(item);
211
212 return true;
213}
214
53f60d4a 215bool wxListBoxBase::CalcAndSendEvent()
05d790f8 216{
05d790f8 217 wxArrayInt selections;
7ead3845 218 GetSelections(selections);
53f60d4a 219 bool selected = true;
7ead3845
VZ
220
221 if ( selections.empty() && m_oldSelections.empty() )
222 {
223 // nothing changed, just leave
53f60d4a 224 return false;
7ead3845
VZ
225 }
226
227 const size_t countSel = selections.size(),
228 countSelOld = m_oldSelections.size();
229 if ( countSel == countSelOld )
230 {
231 bool changed = false;
232 for ( size_t idx = 0; idx < countSel; idx++ )
05d790f8 233 {
7ead3845 234 if (selections[idx] != m_oldSelections[idx])
05d790f8 235 {
7ead3845
VZ
236 changed = true;
237 break;
05d790f8 238 }
05d790f8
RR
239 }
240
7ead3845
VZ
241 // nothing changed, just leave
242 if ( !changed )
53f60d4a 243 return false;
7ead3845
VZ
244 }
245
246 int item = wxNOT_FOUND;
247 if ( selections.empty() )
248 {
53f60d4a 249 selected = false;
7ead3845
VZ
250 item = m_oldSelections[0];
251 }
252 else // we [still] have some selections
253 {
05d790f8
RR
254 // Now test if any new item is selected
255 bool any_new_selected = false;
7ead3845 256 for ( size_t idx = 0; idx < countSel; idx++ )
05d790f8
RR
257 {
258 item = selections[idx];
7ead3845 259 if ( m_oldSelections.Index(item) == wxNOT_FOUND )
05d790f8
RR
260 {
261 any_new_selected = true;
262 break;
263 }
264 }
7ead3845 265
53f60d4a 266 if ( !any_new_selected )
05d790f8 267 {
53f60d4a 268 // No new items selected, now test if any new item is deselected
7ead3845
VZ
269 bool any_new_deselected = false;
270 for ( size_t idx = 0; idx < countSelOld; idx++ )
05d790f8 271 {
7ead3845
VZ
272 item = m_oldSelections[idx];
273 if ( selections.Index(item) == wxNOT_FOUND )
274 {
275 any_new_deselected = true;
276 break;
277 }
278 }
279
280 if ( any_new_deselected )
281 {
282 // indicate that this is a selection
53f60d4a 283 selected = false;
7ead3845
VZ
284 }
285 else
286 {
287 item = wxNOT_FOUND; // this should be impossible
05d790f8
RR
288 }
289 }
7ead3845
VZ
290 }
291
292 wxASSERT_MSG( item != wxNOT_FOUND,
293 "Logic error in wxListBox selection event generation code" );
294
295 m_oldSelections = selections;
296
53f60d4a 297 return SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED, item, selected);
05d790f8
RR
298}
299
2ee3ee1b 300// ----------------------------------------------------------------------------
6c8a980f 301// misc
2ee3ee1b
VZ
302// ----------------------------------------------------------------------------
303
6c8a980f 304void wxListBoxBase::Command(wxCommandEvent& event)
2ee3ee1b 305{
687706f5 306 SetSelection(event.GetInt(), event.GetExtraLong() != 0);
004867db 307 (void)GetEventHandler()->ProcessEvent(event);
2ee3ee1b
VZ
308}
309
1e6feb95
VZ
310// ----------------------------------------------------------------------------
311// SetFirstItem() and such
312// ----------------------------------------------------------------------------
313
2ee3ee1b
VZ
314void wxListBoxBase::SetFirstItem(const wxString& s)
315{
316 int n = FindString(s);
317
f644b28c 318 wxCHECK_RET( n != wxNOT_FOUND, wxT("invalid string in wxListBox::SetFirstItem") );
2ee3ee1b
VZ
319
320 DoSetFirstItem(n);
321}
1e6feb95
VZ
322
323void wxListBoxBase::AppendAndEnsureVisible(const wxString& s)
324{
325 Append(s);
326 EnsureVisible(GetCount() - 1);
327}
328
329void wxListBoxBase::EnsureVisible(int WXUNUSED(n))
330{
331 // the base class version does nothing (the only alternative would be to
332 // call SetFirstItem() but this is probably even more stupid)
333}
334
335#endif // wxUSE_LISTBOX