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
This commit is contained in:
Stephan Aßmus
2010-05-14 18:14:19 +00:00
parent 3ddefbc1d4
commit 5288371864
+14 -16
View File
@@ -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;
}