_AddChildToList() was very inefficient in case there was a "before" view

specified. Also, it now drops into the debugger in case "before" doesn't
belong to us already.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@15198 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2005-11-28 15:48:43 +00:00
parent 349837d9c9
commit 4bd3c01eaa
+36 -35
View File
@@ -3872,47 +3872,48 @@ BView::_RemoveChildFromList(BView* child)
bool bool
BView::_AddChildToList(BView* view, BView* before) BView::_AddChildToList(BView* child, BView* before)
{ {
if (!view) if (!child)
return false; return false;
if (child->fParent != NULL) {
BView *current = fFirstChild; debugger("View already belongs to someone else");
BView *last = current;
while (current && current != before) {
last = current;
current = current->fNextSibling;
}
if (!current && before)
return false; return false;
// we're at begining of the list, OR between two elements
if (current) {
if (current == fFirstChild) {
view->fNextSibling = current;
current->fPreviousSibling = view;
fFirstChild = view;
} else {
view->fNextSibling = current;
view->fPreviousSibling = current->fPreviousSibling;
current->fPreviousSibling->fNextSibling = view;
current->fPreviousSibling = view;
} }
} else { if (before != NULL && before->fParent != this) {
// we have reached the end of the list debugger("Invalid before view");
return false;
// if last!=NULL then we add to the end. Otherwise, view is the
// first chiild in the list
if (last) {
last->fNextSibling = view;
view->fPreviousSibling = last;
} else
fFirstChild = view;
} }
view->fParent = this; if (before != NULL) {
// add view before this one
child->fNextSibling = before;
child->fPreviousSibling = before->fPreviousSibling;
if (child->fPreviousSibling != NULL)
child->fPreviousSibling->fNextSibling = child;
before->fPreviousSibling = child;
if (fFirstChild == before)
fFirstChild = child;
} else {
// add view to the end of the list
BView *last = fFirstChild;
while (last != NULL && last->fNextSibling != NULL) {
last = last->fNextSibling;
}
if (last != NULL) {
last->fNextSibling = child;
child->fPreviousSibling = last;
} else {
fFirstChild = child;
child->fPreviousSibling = NULL;
}
child->fNextSibling = NULL;
}
child->fParent = this;
return true; return true;
} }