From: Vadim Zeitlin Date: Wed, 9 Mar 2011 09:28:41 +0000 (+0000) Subject: Fix search for item by text in wxMSW wxListCtrl. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/ba7bc4e4e6080988d8e034bcae20de0fe936d187 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 --- diff --git a/src/msw/listctrl.cpp b/src/msw/listctrl.cpp index aa9ccb521b..c8635708c8 100644 --- a/src/msw/listctrl.cpp +++ b/src/msw/listctrl.cpp @@ -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 ) - { - // 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; + } } SetItemState(*result,