Fix search for item by text in wxMSW wxListCtrl.

LVN_ODFINDITEM handler could enter infinite loop if its selection was 0 and a
key not matching any of the items first letters was pressed.

Rewrite the loop in a simpler form to ensure that it is correct. Also clarify
some comments. Finally, fix the behaviour when no matching item was found (if
it didn't hang in infinite loop, it used to select the first item in the
control).

Closes #13026.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@67155 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2011-03-09 09:28:41 +00:00
parent aab1681669
commit ba7bc4e4e6

View File

@@ -2411,17 +2411,8 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
startPos = 0;
}
int currentPos = startPos;
do
for ( int currentPos = startPos; ; )
{
// wrap to the beginning if necessary
if ( currentPos == maxPos )
{
// somewhat surprisingly, LVFI_WRAP isn't set in
// flags but we still should wrap
currentPos = 0;
}
// does this item begin with searchstr?
if ( wxStrnicmp(searchstr,
GetItemText(currentPos), len) == 0 )
@@ -2429,13 +2420,26 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
*result = currentPos;
break;
}
}
while ( ++currentPos != startPos );
if ( *result == -1 )
// Go to next item with wrapping if necessary.
if ( ++currentPos == maxPos )
{
// not found
return false;
// Surprisingly, LVFI_WRAP seems to be never set in
// the flags so wrap regardless of it.
currentPos = 0;
}
if ( currentPos == startPos )
{
// We examined all items without finding anything.
//
// Notice that we still return true as we did
// perform the search, if we didn't do this the
// message would have been considered unhandled and
// the control seems to always select the first
// item by default in this case.
return true;
}
}
SetItemState(*result,