]> git.saurik.com Git - wxWidgets.git/commitdiff
Fix search for item by text in wxMSW wxListCtrl.
authorVadim Zeitlin <vadim@wxwidgets.org>
Wed, 9 Mar 2011 09:28:41 +0000 (09:28 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Wed, 9 Mar 2011 09:28:41 +0000 (09:28 +0000)
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

src/msw/listctrl.cpp

index aa9ccb521b87e0fa9a4e26f5112bae1e6708d33f..c8635708c8ff172f687e834cf0506ade3c409c7b 100644 (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 )
-                    {
-                        // 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,