1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/lboxcmn.cpp
3 // Purpose: wxListBox class methods common to all platforms
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/listbox.h"
32 #include "wx/dynarray.h"
33 #include "wx/arrstr.h"
38 // ============================================================================
40 // ============================================================================
42 wxListBoxBase::~wxListBoxBase()
44 // this destructor is required for Darwin
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 bool wxListBoxBase::SetStringSelection(const wxString
& s
, bool select
)
53 const int sel
= FindString(s
);
54 if ( sel
== wxNOT_FOUND
)
57 SetSelection(sel
, select
);
62 void wxListBoxBase::DeselectAll(int itemToLeaveSelected
)
64 if ( HasMultipleSelection() )
66 wxArrayInt selections
;
67 GetSelections(selections
);
69 size_t count
= selections
.GetCount();
70 for ( size_t n
= 0; n
< count
; n
++ )
72 int item
= selections
[n
];
73 if ( item
!= itemToLeaveSelected
)
77 else // single selection
79 int sel
= GetSelection();
80 if ( sel
!= wxNOT_FOUND
&& sel
!= itemToLeaveSelected
)
87 void wxListBoxBase::UpdateOldSelections()
89 // We need to remember the selection even in single-selection case on
90 // Windows, so that we don't send an event when the user clicks on an
91 // already selected item.
93 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
96 GetSelections( m_oldSelections
);
100 bool wxListBoxBase::SendEvent(wxEventType evtType
, int item
, bool selected
)
102 wxCommandEvent
event(evtType
, GetId());
103 event
.SetEventObject(this);
106 event
.SetString(GetString(item
));
107 event
.SetExtraLong(selected
);
109 if ( HasClientObjectData() )
110 event
.SetClientObject(GetClientObject(item
));
111 else if ( HasClientUntypedData() )
112 event
.SetClientData(GetClientData(item
));
114 return HandleWindowEvent(event
);
117 bool wxListBoxBase::CalcAndSendEvent()
119 wxArrayInt selections
;
120 GetSelections(selections
);
121 bool selected
= true;
123 if ( selections
.empty() && m_oldSelections
.empty() )
125 // nothing changed, just leave
129 const size_t countSel
= selections
.size(),
130 countSelOld
= m_oldSelections
.size();
131 if ( countSel
== countSelOld
)
133 bool changed
= false;
134 for ( size_t idx
= 0; idx
< countSel
; idx
++ )
136 if (selections
[idx
] != m_oldSelections
[idx
])
143 // nothing changed, just leave
148 int item
= wxNOT_FOUND
;
149 if ( selections
.empty() )
152 item
= m_oldSelections
[0];
154 else // we [still] have some selections
156 // Now test if any new item is selected
157 bool any_new_selected
= false;
158 for ( size_t idx
= 0; idx
< countSel
; idx
++ )
160 item
= selections
[idx
];
161 if ( m_oldSelections
.Index(item
) == wxNOT_FOUND
)
163 any_new_selected
= true;
168 if ( !any_new_selected
)
170 // No new items selected, now test if any new item is deselected
171 bool any_new_deselected
= false;
172 for ( size_t idx
= 0; idx
< countSelOld
; idx
++ )
174 item
= m_oldSelections
[idx
];
175 if ( selections
.Index(item
) == wxNOT_FOUND
)
177 any_new_deselected
= true;
182 if ( any_new_deselected
)
184 // indicate that this is a selection
189 item
= wxNOT_FOUND
; // this should be impossible
194 wxASSERT_MSG( item
!= wxNOT_FOUND
,
195 "Logic error in wxListBox selection event generation code" );
197 m_oldSelections
= selections
;
199 return SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED
, item
, selected
);
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
206 void wxListBoxBase::Command(wxCommandEvent
& event
)
208 SetSelection(event
.GetInt(), event
.GetExtraLong() != 0);
209 (void)GetEventHandler()->ProcessEvent(event
);
212 // ----------------------------------------------------------------------------
213 // SetFirstItem() and such
214 // ----------------------------------------------------------------------------
216 void wxListBoxBase::SetFirstItem(const wxString
& s
)
218 int n
= FindString(s
);
220 wxCHECK_RET( n
!= wxNOT_FOUND
, wxT("invalid string in wxListBox::SetFirstItem") );
225 void wxListBoxBase::AppendAndEnsureVisible(const wxString
& s
)
228 EnsureVisible(GetCount() - 1);
231 void wxListBoxBase::EnsureVisible(int WXUNUSED(n
))
233 // the base class version does nothing (the only alternative would be to
234 // call SetFirstItem() but this is probably even more stupid)
237 #endif // wxUSE_LISTBOX