Merge branch 'choice-getstring'

Make wxChoice::GetString() consistently assert in all ports when given
invalid index.

See https://github.com/wxWidgets/wxWidgets/pull/2169

Closes #18852.
This commit is contained in:
Vadim Zeitlin
2021-01-17 18:18:44 +01:00
5 changed files with 22 additions and 13 deletions

View File

@@ -107,6 +107,8 @@ Changes in behaviour not resulting in compilation errors
- wxGetInstallPrefix() now returns wxString.
- wxChoice::GetString() now consistently asserts when passed an invalid index.
Changes in behaviour which may result in build errors
-----------------------------------------------------

View File

@@ -51,11 +51,14 @@ public:
/**
Returns the label of the item with the given index.
The index must be valid, i.e. less than the value returned by
GetCount(), otherwise an assert is triggered. Notably, this function
can't be called if the control is empty.
@param n
The zero-based index.
@return The label of the item or an empty string if the position was
invalid.
@return The label of the item.
*/
virtual wxString GetString(unsigned int n) const = 0;

View File

@@ -278,13 +278,15 @@ wxString wxChoice::GetString(unsigned int n) const
{
wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid control") );
wxString str;
GtkComboBox* combobox = GTK_COMBO_BOX( m_widget );
GtkTreeModel *model = gtk_combo_box_get_model( combobox );
GtkTreeIter iter;
if (gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
if (!gtk_tree_model_iter_nth_child (model, &iter, NULL, n))
{
wxFAIL_MSG( "invalid index" );
return wxString();
}
GValue value = G_VALUE_INIT;
gtk_tree_model_get_value( model, &iter, m_stringCellIndex, &value );
wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &value ) );
@@ -292,9 +294,6 @@ wxString wxChoice::GetString(unsigned int n) const
return tmp;
}
return str;
}
unsigned int wxChoice::GetCount() const
{
wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid control") );

View File

@@ -362,10 +362,13 @@ void wxChoice::SetString(unsigned int n, const wxString& s)
wxString wxChoice::GetString(unsigned int n) const
{
int len = (int)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
const int len = (int)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
wxString str;
if ( len != CB_ERR && len > 0 )
wxCHECK_MSG( len != CB_ERR, str, wxS("Invalid index") );
if ( len > 0 )
{
if ( ::SendMessage
(

View File

@@ -71,6 +71,7 @@ void ItemContainerTestCase::Count()
wxItemContainer * const container = GetContainer();
CPPUNIT_ASSERT(container->IsEmpty());
WX_ASSERT_FAILS_WITH_ASSERT( container->GetString(0) );
wxArrayString testitems;
testitems.Add("item 0");
@@ -95,6 +96,7 @@ void ItemContainerTestCase::Count()
container->Insert(testitems, 1);
CPPUNIT_ASSERT_EQUAL(5, container->GetCount());
WX_ASSERT_FAILS_WITH_ASSERT( container->GetString(10) );
}
void ItemContainerTestCase::ItemSelection()