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 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
90 GetSelections( m_oldSelections
);
93 static void LBSendEvent( wxCommandEvent
&event
, wxListBoxBase
*listbox
, int 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
);
104 void wxListBoxBase::CalcAndSendEvent()
106 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, GetId());
107 event
.SetEventObject( this );
109 wxArrayInt selections
;
110 GetSelections(selections
);
112 if ( selections
.empty() && m_oldSelections
.empty() )
114 // nothing changed, just leave
118 const size_t countSel
= selections
.size(),
119 countSelOld
= m_oldSelections
.size();
120 if ( countSel
== countSelOld
)
122 bool changed
= false;
123 for ( size_t idx
= 0; idx
< countSel
; idx
++ )
125 if (selections
[idx
] != m_oldSelections
[idx
])
132 // nothing changed, just leave
137 int item
= wxNOT_FOUND
;
138 if ( selections
.empty() )
140 // indicate that this is a deselection
141 event
.SetExtraLong(0);
142 item
= m_oldSelections
[0];
144 else // we [still] have some selections
146 // Now test if any new item is selected
147 bool any_new_selected
= false;
148 for ( size_t idx
= 0; idx
< countSel
; idx
++ )
150 item
= selections
[idx
];
151 if ( m_oldSelections
.Index(item
) == wxNOT_FOUND
)
153 any_new_selected
= true;
158 if ( any_new_selected
)
160 // indicate that this is a selection
161 event
.SetExtraLong(1);
163 else // no new items selected
165 // Now test if any new item is deselected
166 bool any_new_deselected
= false;
167 for ( size_t idx
= 0; idx
< countSelOld
; idx
++ )
169 item
= m_oldSelections
[idx
];
170 if ( selections
.Index(item
) == wxNOT_FOUND
)
172 any_new_deselected
= true;
177 if ( any_new_deselected
)
179 // indicate that this is a selection
180 event
.SetExtraLong(0);
184 item
= wxNOT_FOUND
; // this should be impossible
189 wxASSERT_MSG( item
!= wxNOT_FOUND
,
190 "Logic error in wxListBox selection event generation code" );
192 m_oldSelections
= selections
;
194 LBSendEvent(event
, this, item
);
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 void wxListBoxBase::Command(wxCommandEvent
& event
)
203 SetSelection(event
.GetInt(), event
.GetExtraLong() != 0);
204 (void)GetEventHandler()->ProcessEvent(event
);
207 // ----------------------------------------------------------------------------
208 // SetFirstItem() and such
209 // ----------------------------------------------------------------------------
211 void wxListBoxBase::SetFirstItem(const wxString
& s
)
213 int n
= FindString(s
);
215 wxCHECK_RET( n
!= wxNOT_FOUND
, wxT("invalid string in wxListBox::SetFirstItem") );
220 void wxListBoxBase::AppendAndEnsureVisible(const wxString
& s
)
223 EnsureVisible(GetCount() - 1);
226 void wxListBoxBase::EnsureVisible(int WXUNUSED(n
))
228 // the base class version does nothing (the only alternative would be to
229 // call SetFirstItem() but this is probably even more stupid)
232 #endif // wxUSE_LISTBOX