1111namespace sfg {
1212
1313const ListBox::IndexType ListBox::NONE = -1 ;
14+ static const sf::String EMPTY = " " ;
1415
1516Signal::SignalID ListBox::OnSelect = 0 ;
1617
@@ -47,7 +48,6 @@ sf::Vector2f ListBox::CalculateRequisition() {
4748 const auto & font_name = Context::Get ().GetEngine ().GetProperty <std::string>( " FontName" , shared_from_this () );
4849 const auto & font = Context::Get ().GetEngine ().GetResourceManager ().GetFont ( font_name );
4950 auto font_size = Context::Get ().GetEngine ().GetProperty <unsigned int >( " FontSize" , shared_from_this () );
50- auto line_height = Context::Get ().GetEngine ().GetFontLineHeight ( *font, font_size );
5151 auto dots_width = Context::Get ().GetEngine ().GetTextStringMetrics (" ..." , *font, font_size).x ;
5252
5353 // Calculate the max width of items
@@ -69,59 +69,6 @@ const std::string& ListBox::GetName() const {
6969 return name;
7070}
7171
72- ListBox::IndexType ListBox::GetItemsCount () const {
73- return m_items.size ();
74- }
75-
76- const sf::String& ListBox::GetItemText ( IndexType index ) const {
77- return m_items[index];
78- }
79-
80- const sf::String& ListBox::GetDisplayedItemText ( IndexType index ) const {
81- if (m_item_text_policy == ItemTextPolicy::RESIZE_LISTBOX) {
82- return GetItemText ( index );
83- } else {
84- return m_displayed_items_texts[index];
85- }
86- }
87-
88- ListBox::IndexType ListBox::GetHighlightedItem () const {
89- return m_highlighted_item;
90- }
91-
92- bool ListBox::IsItemSelected (IndexType index) const {
93- return m_selected_items.find ( index ) != m_selected_items.end ();
94- }
95-
96- ListBox::IndexType ListBox::GetSelectedItemsCount () const {
97- return m_selected_items.size ();
98- }
99-
100- ListBox::IndexType ListBox::GetSelectedItemIndex ( IndexType index ) const {
101- if ( index >= m_selected_items.size () )
102- return NONE;
103-
104- auto it = m_selected_items.cbegin ();
105- std::advance ( it, index );
106- return *it;
107- }
108-
109- const sf::String& ListBox::GetSelectedItemText ( IndexType index ) const {
110- return m_items[ GetSelectedItemIndex ( index ) ];
111- }
112-
113- ListBox::IndexType ListBox::GetFirstDisplayedItemIndex () const {
114- return m_first_displayed_item;
115- }
116-
117- ListBox::IndexType ListBox::GetDisplayedItemsCount () const {
118- return std::min (m_max_displayed_items_count, static_cast <IndexType>( m_items.size () ) - m_first_displayed_item);
119- }
120-
121- ListBox::IndexType ListBox::GetMaxDisplayedItemsCount () const {
122- return m_max_displayed_items_count;
123- }
124-
12572void ListBox::AppendItem ( const sf::String& str ) {
12673 m_items.push_back ( str );
12774
@@ -173,7 +120,19 @@ void ListBox::PrependItem( const sf::String& str ) {
173120 Invalidate ();
174121}
175122
123+ void ListBox::ChangeItem ( IndexType index, const sf::String& str ) {
124+ if ( index >= static_cast <IndexType>( m_items.size () ) || index < 0 ) {
125+ return ;
126+ }
127+
128+ m_items[ static_cast <std::size_t >( index ) ] = str;
129+ }
130+
176131void ListBox::RemoveItem ( IndexType index ) {
132+ if ( index >= static_cast <IndexType>( m_items.size () ) || index < 0 ) {
133+ return ;
134+ }
135+
177136 m_items.erase ( m_items.begin () + index );
178137
179138 // Remove it from the selected indexes.
@@ -208,6 +167,68 @@ void ListBox::Clear() {
208167 Invalidate ();
209168}
210169
170+ ListBox::IndexType ListBox::GetItemsCount () const {
171+ return static_cast <IndexType>( m_items.size () );
172+ }
173+
174+ const sf::String& ListBox::GetItemText ( IndexType index ) const {
175+ if ( index >= static_cast <IndexType>( m_items.size () ) || index < 0 ) {
176+ return EMPTY;
177+ }
178+
179+ return m_items[ static_cast <std::size_t >( index )];
180+ }
181+
182+ const sf::String& ListBox::GetDisplayedItemText ( IndexType index ) const {
183+ if ( index >= static_cast <IndexType>( m_items.size () ) || index < 0 ) {
184+ return EMPTY;
185+ }
186+
187+ if (m_item_text_policy == ItemTextPolicy::RESIZE_LISTBOX) {
188+ return GetItemText ( index );
189+ } else {
190+ return m_displayed_items_texts[ static_cast <std::size_t >( index ) ];
191+ }
192+ }
193+
194+ ListBox::IndexType ListBox::GetHighlightedItem () const {
195+ return m_highlighted_item;
196+ }
197+
198+ bool ListBox::IsItemSelected (IndexType index) const {
199+ return m_selected_items.find ( index ) != m_selected_items.end ();
200+ }
201+
202+ ListBox::IndexType ListBox::GetSelectedItemsCount () const {
203+ return static_cast <IndexType>( m_selected_items.size () );
204+ }
205+
206+ ListBox::IndexType ListBox::GetSelectedItemIndex ( IndexType index ) const {
207+ if ( index >= static_cast <IndexType>( m_selected_items.size () ) || index < 0 ) {
208+ return NONE;
209+ }
210+
211+ auto it = m_selected_items.cbegin ();
212+ std::advance ( it, index );
213+ return *it;
214+ }
215+
216+ const sf::String& ListBox::GetSelectedItemText ( IndexType index ) const {
217+ return m_items[ static_cast <std::size_t >( GetSelectedItemIndex ( index ) ) ];
218+ }
219+
220+ ListBox::IndexType ListBox::GetFirstDisplayedItemIndex () const {
221+ return m_first_displayed_item;
222+ }
223+
224+ ListBox::IndexType ListBox::GetDisplayedItemsCount () const {
225+ return std::min (m_max_displayed_items_count, static_cast <IndexType>( m_items.size () ) - m_first_displayed_item);
226+ }
227+
228+ ListBox::IndexType ListBox::GetMaxDisplayedItemsCount () const {
229+ return m_max_displayed_items_count;
230+ }
231+
211232ListBox::SelectionMode ListBox::GetSelectionMode () const {
212233 return m_selection_mode;
213234}
@@ -264,8 +285,8 @@ void ListBox::HandleMouseMoveEvent( int x, int y ) {
264285 return ;
265286 }
266287
267- float realX = x - GetAllocation ().left ;
268- float realY = y - GetAllocation ().top ;
288+ float realX = static_cast < float >( x ) - GetAllocation ().left ;
289+ float realY = static_cast < float >( y ) - GetAllocation ().top ;
269290
270291 if ( IsMouseInWidget () && ( !IsScrollbarVisible () || ( IsScrollbarVisible () && realX < m_vertical_scrollbar->GetAllocation ().left ) ) ) {
271292 // Highlight the item under the mouse
@@ -285,8 +306,8 @@ void ListBox::HandleMouseButtonEvent( sf::Mouse::Button button, bool press, int
285306 return ;
286307 }
287308
288- float realX = x - GetAllocation ().left ;
289- float realY = y - GetAllocation ().top ;
309+ float realX = static_cast < float >( x ) - GetAllocation ().left ;
310+ float realY = static_cast < float >( y ) - GetAllocation ().top ;
290311
291312 if ( IsMouseInWidget () ) {
292313 if ( button == sf::Mouse::Left && press ) {
@@ -328,7 +349,7 @@ void ListBox::HandleMouseButtonEvent( sf::Mouse::Button button, bool press, int
328349 }
329350}
330351
331- ListBox::IndexType ListBox::GetItemAt ( int y ) const {
352+ ListBox::IndexType ListBox::GetItemAt ( float y ) const {
332353 auto text_padding = Context::Get ().GetEngine ().GetProperty <float >( " Padding" , shared_from_this () );
333354 auto border_width = Context::Get ().GetEngine ().GetProperty <float >( " BorderWidth" , shared_from_this () );
334355 const auto & font_name = Context::Get ().GetEngine ().GetProperty <std::string>( " FontName" , shared_from_this () );
@@ -337,7 +358,7 @@ ListBox::IndexType ListBox::GetItemAt( int y ) const {
337358 auto line_height = Context::Get ().GetEngine ().GetFontLineHeight ( *font, font_size );
338359
339360 IndexType item_index = 0 ;
340- while (y > border_width + item_index * ( line_height + text_padding * 2 ) ) {
361+ while (y > border_width + static_cast < float >( item_index ) * ( line_height + text_padding * 2 ) ) {
341362 ++item_index;
342363 }
343364
@@ -389,10 +410,10 @@ void ListBox::UpdateDisplayedItems() {
389410 auto line_height = Context::Get ().GetEngine ().GetFontLineHeight ( *font, font_size );
390411
391412 // Update the displayed items count.
392- int items_height = 0 ;
413+ float items_height = 0 . f ;
393414 m_max_displayed_items_count = 0 ;
394- while (items_height < GetAllocation ().height - border_width * 2 + text_padding ) {
395- items_height += line_height + text_padding * 2 ;
415+ while (items_height < GetAllocation ().height - border_width * 2 . f + text_padding ) {
416+ items_height += line_height + text_padding * 2 . f ;
396417 ++m_max_displayed_items_count;
397418 }
398419 if (m_max_displayed_items_count > 0 )
@@ -401,19 +422,19 @@ void ListBox::UpdateDisplayedItems() {
401422 // If there aren't enough items from m_first_displayed_item to
402423 // m_first_displayed_item + m_max_displayed_items_count, decrement
403424 // m_first_displayed_item.
404- if (m_first_displayed_item + m_max_displayed_items_count > m_items.size ()) {
405- m_first_displayed_item = std::max (static_cast <IndexType>(m_items.size ()) - m_max_displayed_items_count, static_cast <IndexType>(0 ) );
425+ if (m_first_displayed_item + m_max_displayed_items_count > static_cast <IndexType>( m_items.size () ) ) {
426+ m_first_displayed_item = std::max ( static_cast <IndexType>( m_items.size () ) - m_max_displayed_items_count, static_cast <IndexType>( 0 ) );
406427 }
407428}
408429
409430void ListBox::UpdateScrollbarAdjustment () {
410431 m_vertical_scrollbar->GetAdjustment ()->Configure (
411- m_first_displayed_item,
412- 0 ,
413- m_items.size (),
414- 1 ,
415- m_max_displayed_items_count,
416- m_max_displayed_items_count
432+ static_cast < float >( m_first_displayed_item ) ,
433+ 0 . f ,
434+ static_cast < float >( m_items.size () ),
435+ 1 . f ,
436+ static_cast < float >( m_max_displayed_items_count ) ,
437+ static_cast < float >( m_max_displayed_items_count )
417438 );
418439}
419440
@@ -423,7 +444,7 @@ void ListBox::UpdateScrollbarAllocation() {
423444 GetAllocation ().width - border_width - m_vertical_scrollbar->GetRequisition ().x ,
424445 border_width,
425446 m_vertical_scrollbar->GetRequisition ().x ,
426- GetAllocation ().height - border_width * 2
447+ GetAllocation ().height - border_width * 2 . f
427448 ) );
428449
429450 m_vertical_scrollbar->RequestResize ();
@@ -443,7 +464,6 @@ void ListBox::UpdateDisplayedItemsText() {
443464 const auto & font_name = Context::Get ().GetEngine ().GetProperty <std::string>( " FontName" , shared_from_this () );
444465 const auto & font = Context::Get ().GetEngine ().GetResourceManager ().GetFont ( font_name );
445466 auto font_size = Context::Get ().GetEngine ().GetProperty <unsigned int >( " FontSize" , shared_from_this () );
446- auto line_height = Context::Get ().GetEngine ().GetFontLineHeight ( *font, font_size );
447467 auto dots_width = Context::Get ().GetEngine ().GetTextStringMetrics (" ..." , *font, font_size).x ;
448468
449469 float max_width = GetAllocation ().width - border_width * 2 - text_padding * 2 - ( IsScrollbarVisible () ? m_vertical_scrollbar->GetAllocation ().width : 0 );
@@ -473,7 +493,7 @@ void ListBox::UpdateDisplayedItemsText() {
473493}
474494
475495void ListBox::OnScrollbarChanged () {
476- m_first_displayed_item = m_vertical_scrollbar->GetAdjustment ()->GetValue ();
496+ m_first_displayed_item = static_cast <IndexType>( m_vertical_scrollbar->GetAdjustment ()->GetValue () );
477497 UpdateDisplayedItems ();
478498
479499 Invalidate ();
0 commit comments