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:
+14
-16
@@ -2550,35 +2550,33 @@ BMenu::_SelectNextItem(BMenuItem* item, bool forward)
|
|||||||
BMenuItem*
|
BMenuItem*
|
||||||
BMenu::_NextItem(BMenuItem* item, bool forward) const
|
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();
|
const int32 numItems = fItems.CountItems();
|
||||||
if (index < 0) {
|
if (numItems == 0)
|
||||||
if (forward)
|
return NULL;
|
||||||
index = -1;
|
|
||||||
else
|
int32 index = fItems.IndexOf(item);
|
||||||
index = numItems;
|
int32 loopCount = numItems;
|
||||||
}
|
while (--loopCount) {
|
||||||
int32 startIndex = index;
|
// Cycle through menu items in the given direction...
|
||||||
do {
|
|
||||||
if (forward)
|
if (forward)
|
||||||
index++;
|
index++;
|
||||||
else
|
else
|
||||||
index--;
|
index--;
|
||||||
|
|
||||||
// cycle through menu items
|
// ... wrap around...
|
||||||
if (index < 0)
|
if (index < 0)
|
||||||
index = numItems - 1;
|
index = numItems - 1;
|
||||||
else if (index >= numItems)
|
else if (index >= numItems)
|
||||||
index = 0;
|
index = 0;
|
||||||
} while (!ItemAt(index)->IsEnabled() && index != startIndex);
|
|
||||||
|
|
||||||
if (index == startIndex) {
|
// ... and return the first suitable item found.
|
||||||
// We are back where we started and no item was enabled.
|
BMenuItem* nextItem = ItemAt(index);
|
||||||
return NULL;
|
if (nextItem->IsEnabled())
|
||||||
|
return nextItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ItemAt(index);
|
// If no other suitable item was found, return NULL.
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user