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