Skip to content

Commit 3c39251

Browse files
Add methods to change the selected items of a ListBox
1 parent 4e9dd81 commit 3c39251

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

examples/ListBox.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ int main() {
7171
listbox3->AppendItem( "Sixth item" );
7272
listbox3->AppendItem( "Last one !" );
7373
listbox3->SetSelectionMode( sfg::ListBox::SelectionMode::MULTI_SELECTION );
74+
listbox3->SetSelection( {1, 3, 4, 5} );
7475
box3->PackEnd( listbox3 );
7576

7677
window3->Add( box3 );

include/SFGUI/ListBox.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <SFGUI/Scrollbar.hpp>
55

66
#include <SFML/System/String.hpp>
7+
8+
#include <initializer_list>
79
#include <memory>
810
#include <set>
911
#include <vector>
@@ -58,6 +60,11 @@ class SFGUI_API ListBox : public Container {
5860

5961
IndexType GetHighlightedItem() const;
6062

63+
void SetSelection( IndexType index );
64+
void SetSelection( std::initializer_list<IndexType> indices );
65+
void AppendToSelection( IndexType index );
66+
void RemoveFromSelection( IndexType index );
67+
6168
bool IsItemSelected( IndexType index ) const;
6269
IndexType GetSelectedItemsCount() const;
6370
IndexType GetSelectedItemIndex( IndexType index = 0 ) const;

src/SFGUI/ListBox.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,53 @@ ListBox::IndexType ListBox::GetHighlightedItem() const {
195195
return m_highlighted_item;
196196
}
197197

198+
void ListBox::SetSelection( IndexType index ) {
199+
if( m_selection_mode == SelectionMode::NO_SELECTION ) {
200+
return;
201+
}
202+
203+
m_selected_items.clear();
204+
if( index != NONE ) {
205+
m_selected_items.insert( index );
206+
}
207+
208+
Invalidate();
209+
}
210+
211+
void ListBox::SetSelection( std::initializer_list<IndexType> indices ) {
212+
if( m_selection_mode == SelectionMode::MULTI_SELECTION ) {
213+
m_selected_items = std::set<IndexType>( indices.begin(), indices.end() );
214+
215+
Invalidate();
216+
} else if( m_selection_mode == SelectionMode::SINGLE_SELECTION ) {
217+
if( indices.size() > 0) {
218+
SetSelection( *( indices.begin() ) );
219+
} else {
220+
SetSelection( NONE );
221+
}
222+
223+
Invalidate();
224+
}
225+
}
226+
227+
void ListBox::AppendToSelection( IndexType index ) {
228+
if( m_selection_mode == SelectionMode::NO_SELECTION ) {
229+
return;
230+
}
231+
232+
if( m_selected_items.size() == 0 || m_selection_mode == SelectionMode::MULTI_SELECTION ) {
233+
m_selected_items.insert( index );
234+
}
235+
236+
Invalidate();
237+
}
238+
239+
void ListBox::RemoveFromSelection( IndexType index ) {
240+
m_selected_items.erase( index );
241+
242+
Invalidate();
243+
}
244+
198245
bool ListBox::IsItemSelected(IndexType index) const {
199246
return m_selected_items.find( index ) != m_selected_items.end();
200247
}

0 commit comments

Comments
 (0)