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
- 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 )
// does this item begin with searchstr?
if ( wxStrnicmp(searchstr,
GetItemText(currentPos), len) == 0 )
*result = currentPos;
break;
}
*result = currentPos;
break;
}
- }
- while ( ++currentPos != startPos );
- if ( *result == -1 )
- {
- // not found
- return false;
+ // Go to next item with wrapping if necessary.
+ if ( ++currentPos == maxPos )
+ {
+ // 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;
+ }