From 52883718648c95b4f1ac7e0573ed4aa789962ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Fri, 14 May 2010 18:14:19 +0000 Subject: [PATCH] Rewrote BMenu::_NextItem(), since it could busy loop when there was no item. The new code should be a little easier to follow. IMHO, there were also problems with detecting and breaking out of a full cycle, in case there was no start item passed to the method. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36813 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/interface/Menu.cpp | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/kits/interface/Menu.cpp b/src/kits/interface/Menu.cpp index 654ce6cbd4..80f433ebba 100644 --- a/src/kits/interface/Menu.cpp +++ b/src/kits/interface/Menu.cpp @@ -2550,35 +2550,33 @@ BMenu::_SelectNextItem(BMenuItem* item, bool forward) BMenuItem* BMenu::_NextItem(BMenuItem* item, bool forward) const { - // go to next item, and skip over disabled items such as separators - int32 index = fItems.IndexOf(item); const int32 numItems = fItems.CountItems(); - if (index < 0) { - if (forward) - index = -1; - else - index = numItems; - } - int32 startIndex = index; - do { + if (numItems == 0) + return NULL; + + int32 index = fItems.IndexOf(item); + int32 loopCount = numItems; + while (--loopCount) { + // Cycle through menu items in the given direction... if (forward) index++; else index--; - // cycle through menu items + // ... wrap around... if (index < 0) index = numItems - 1; else if (index >= numItems) index = 0; - } while (!ItemAt(index)->IsEnabled() && index != startIndex); - if (index == startIndex) { - // We are back where we started and no item was enabled. - return NULL; + // ... and return the first suitable item found. + BMenuItem* nextItem = ItemAt(index); + if (nextItem->IsEnabled()) + return nextItem; } - return ItemAt(index); + // If no other suitable item was found, return NULL. + return NULL; }