]> git.saurik.com Git - wxWidgets.git/blame - src/common/lboxcmn.cpp
refactor wxLog documentation moving verbose parts to the wxLog overview and grouping...
[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
2ee3ee1b
VZ
47// ----------------------------------------------------------------------------
48// selection
49// ----------------------------------------------------------------------------
50
2ee3ee1b
VZ
51bool 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
62void 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
87void wxListBoxBase::UpdateOldSelections()
88{
89 if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED))
90 GetSelections( m_oldSelections );
91}
92
53f60d4a 93bool wxListBoxBase::SendEvent(wxEventType evtType, int item, bool selected)
05d790f8 94{
53f60d4a
VZ
95 wxCommandEvent event(evtType, GetId());
96 event.SetEventObject(this);
97
98 event.SetInt(item);
99 event.SetString(GetString(item));
100 event.SetExtraLong(selected);
101
102 if ( HasClientObjectData() )
103 event.SetClientObject(GetClientObject(item));
104 else if ( HasClientUntypedData() )
105 event.SetClientData(GetClientData(item));
106
107 return HandleWindowEvent(event);
05d790f8
RR
108}
109
53f60d4a 110bool wxListBoxBase::CalcAndSendEvent()
05d790f8 111{
05d790f8 112 wxArrayInt selections;
7ead3845 113 GetSelections(selections);
53f60d4a 114 bool selected = true;
7ead3845
VZ
115
116 if ( selections.empty() && m_oldSelections.empty() )
117 {
118 // nothing changed, just leave
53f60d4a 119 return false;
7ead3845
VZ
120 }
121
122 const size_t countSel = selections.size(),
123 countSelOld = m_oldSelections.size();
124 if ( countSel == countSelOld )
125 {
126 bool changed = false;
127 for ( size_t idx = 0; idx < countSel; idx++ )
05d790f8 128 {
7ead3845 129 if (selections[idx] != m_oldSelections[idx])
05d790f8 130 {
7ead3845
VZ
131 changed = true;
132 break;
05d790f8 133 }
05d790f8
RR
134 }
135
7ead3845
VZ
136 // nothing changed, just leave
137 if ( !changed )
53f60d4a 138 return false;
7ead3845
VZ
139 }
140
141 int item = wxNOT_FOUND;
142 if ( selections.empty() )
143 {
53f60d4a 144 selected = false;
7ead3845
VZ
145 item = m_oldSelections[0];
146 }
147 else // we [still] have some selections
148 {
05d790f8
RR
149 // Now test if any new item is selected
150 bool any_new_selected = false;
7ead3845 151 for ( size_t idx = 0; idx < countSel; idx++ )
05d790f8
RR
152 {
153 item = selections[idx];
7ead3845 154 if ( m_oldSelections.Index(item) == wxNOT_FOUND )
05d790f8
RR
155 {
156 any_new_selected = true;
157 break;
158 }
159 }
7ead3845 160
53f60d4a 161 if ( !any_new_selected )
05d790f8 162 {
53f60d4a 163 // No new items selected, now test if any new item is deselected
7ead3845
VZ
164 bool any_new_deselected = false;
165 for ( size_t idx = 0; idx < countSelOld; idx++ )
05d790f8 166 {
7ead3845
VZ
167 item = m_oldSelections[idx];
168 if ( selections.Index(item) == wxNOT_FOUND )
169 {
170 any_new_deselected = true;
171 break;
172 }
173 }
174
175 if ( any_new_deselected )
176 {
177 // indicate that this is a selection
53f60d4a 178 selected = false;
7ead3845
VZ
179 }
180 else
181 {
182 item = wxNOT_FOUND; // this should be impossible
05d790f8
RR
183 }
184 }
7ead3845
VZ
185 }
186
187 wxASSERT_MSG( item != wxNOT_FOUND,
188 "Logic error in wxListBox selection event generation code" );
189
190 m_oldSelections = selections;
191
53f60d4a 192 return SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED, item, selected);
05d790f8
RR
193}
194
2ee3ee1b 195// ----------------------------------------------------------------------------
6c8a980f 196// misc
2ee3ee1b
VZ
197// ----------------------------------------------------------------------------
198
6c8a980f 199void wxListBoxBase::Command(wxCommandEvent& event)
2ee3ee1b 200{
687706f5 201 SetSelection(event.GetInt(), event.GetExtraLong() != 0);
004867db 202 (void)GetEventHandler()->ProcessEvent(event);
2ee3ee1b
VZ
203}
204
1e6feb95
VZ
205// ----------------------------------------------------------------------------
206// SetFirstItem() and such
207// ----------------------------------------------------------------------------
208
2ee3ee1b
VZ
209void wxListBoxBase::SetFirstItem(const wxString& s)
210{
211 int n = FindString(s);
212
f644b28c 213 wxCHECK_RET( n != wxNOT_FOUND, wxT("invalid string in wxListBox::SetFirstItem") );
2ee3ee1b
VZ
214
215 DoSetFirstItem(n);
216}
1e6feb95
VZ
217
218void wxListBoxBase::AppendAndEnsureVisible(const wxString& s)
219{
220 Append(s);
221 EnsureVisible(GetCount() - 1);
222}
223
224void wxListBoxBase::EnsureVisible(int WXUNUSED(n))
225{
226 // the base class version does nothing (the only alternative would be to
227 // call SetFirstItem() but this is probably even more stupid)
228}
229
230#endif // wxUSE_LISTBOX