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