cleaner version of RemoveItems, more error checking

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@14638 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2005-11-02 13:10:33 +00:00
parent 2544aac3eb
commit ecf92c7e4b
+32 -22
View File
@@ -1196,41 +1196,51 @@ BMenu::_AddItem(BMenuItem *item, int32 index)
bool bool
BMenu::RemoveItems(int32 index, int32 count, BMenuItem *_item, bool del) BMenu::RemoveItems(int32 index, int32 count, BMenuItem *item, bool del)
{ {
bool result = false; bool success = false;
bool invalidateLayout = false;
// The plan is simple: If we're given a BMenuItem directly, we use it // The plan is simple: If we're given a BMenuItem directly, we use it
// and ignore index and count. Otherwise, we use them instead. // and ignore index and count. Otherwise, we use them instead.
if (_item != NULL) { if (item != NULL) {
fItems.RemoveItem(_item); if (fItems.RemoveItem(item)) {
_item->SetSuper(NULL); item->SetSuper(NULL);
_item->Uninstall(); item->Uninstall();
if (del) if (del)
delete _item; delete item;
result = true; success = invalidateLayout = true;
}
} else { } else {
BMenuItem *item = NULL;
// We iterate backwards because it's simpler // We iterate backwards because it's simpler
// TODO: We should check if index and count are in bounds. int32 i = min_c(index + count - 1, fItems.CountItems() - 1);
for (int32 i = index + count - 1; i >= index; i--) { // NOTE: the range check for "index" is done after
item = static_cast<BMenuItem *>(fItems.ItemAt(i)); // calculating the last index to be removed, so
// that the range is not "shifted" unintentionally
index = max_c(0, index);
for (; i >= index; i--) {
item = static_cast<BMenuItem*>(fItems.ItemAt(i));
if (item != NULL) { if (item != NULL) {
fItems.RemoveItem(item); if (fItems.RemoveItem(item)) {
item->SetSuper(NULL); item->SetSuper(NULL);
item->Uninstall(); item->Uninstall();
if (del) if (del)
delete item; delete item;
if (!result) success = true;
result = true; invalidateLayout = true;
} else {
// operation not entirely successful
success = false;
break;
}
} }
} }
} }
if (Window() != NULL && fResizeToFit) if (invalidateLayout && Window() != NULL && fResizeToFit)
InvalidateLayout(); InvalidateLayout();
return result; return success;
} }