From fa01d084960d9ee5acf5786c5f15c6e956026be8 Mon Sep 17 00:00:00 2001 From: Alex Wilson Date: Thu, 8 Sep 2011 18:08:22 -0600 Subject: [PATCH] User BView's layout item tracking functionality to simplify and optimize some methods in BLayout and BView. Delete BLayout::RemoveViewRecursive() as it is no longer needed. Add a few TODO's. --- headers/os/interface/Layout.h | 1 - src/kits/interface/Layout.cpp | 56 +++++++++++-------------------- src/kits/interface/LayoutItem.cpp | 3 ++ src/kits/interface/View.cpp | 10 ++++-- 4 files changed, 31 insertions(+), 39 deletions(-) diff --git a/headers/os/interface/Layout.h b/headers/os/interface/Layout.h index 517368ac7c..913d007ff1 100644 --- a/headers/os/interface/Layout.h +++ b/headers/os/interface/Layout.h @@ -89,7 +89,6 @@ protected: private: friend class BView; - bool RemoveViewRecursive(BView* view); bool InvalidateLayoutsForView(BView* view); bool InvalidationLegal(); void SetOwner(BView* owner); diff --git a/src/kits/interface/Layout.cpp b/src/kits/interface/Layout.cpp index 214b08d980..cfb7ca377f 100644 --- a/src/kits/interface/Layout.cpp +++ b/src/kits/interface/Layout.cpp @@ -11,9 +11,9 @@ #include #include +#include #include #include -#include #include #include @@ -195,10 +195,11 @@ BLayout::RemoveView(BView* child) bool removed = false; // a view can have any number of layout items - we need to remove them all - for (int32 i = fItems.CountItems(); i-- > 0;) { - BLayoutItem* item = ItemAt(i); + BView::Private viewPrivate(child); + for (int32 i = viewPrivate.CountLayoutItems() - 1; i >= 0; i--) { + BLayoutItem* item = viewPrivate.LayoutItemAt(i); - if (item->View() != child) + if (item->Layout() != this) continue; RemoveItem(i); @@ -225,13 +226,15 @@ BLayout::RemoveItem(int32 index) return NULL; BLayoutItem* item = (BLayoutItem*)fItems.RemoveItem(index); - - // if the item refers to a BView, we make sure it is removed from the - // parent view + // If this is the last item in use that refers to a certain BView, + // that BView now needs to be removed. BView* view = item->View(); - if (view && view->fParent == fTarget) + if (view && BView::Private(view).CountLayoutItems() == 0) view->_RemoveSelf(); + // TODO: Is this the right place/order to call these hooks? + // view->Parent() could be NULL, maybe that's not a problem.. + ItemRemoved(item, index); item->SetLayout(NULL); InvalidateLayout(); @@ -578,40 +581,20 @@ BLayout::LayoutContext() const } -bool -BLayout::RemoveViewRecursive(BView* view) -{ - bool removed = RemoveView(view); - for (int32 i = fNestedLayouts.CountItems() - 1; i >= 0; i--) { - BLayout* nested = (BLayout*)fNestedLayouts.ItemAt(i); - removed |= nested->RemoveViewRecursive(view); - } - return removed; -} - - bool BLayout::InvalidateLayoutsForView(BView* view) { - bool found = false; - for (int32 i = fNestedLayouts.CountItems() - 1; i >= 0; i--) { - BLayout* layout = (BLayout*)fNestedLayouts.ItemAt(i); - found |= layout->InvalidateLayoutsForView(view); - } - - if (found) - return found; - - if (!InvalidationLegal()) + BView::Private viewPrivate(view); + int32 count = viewPrivate.CountLayoutItems(); + if (count == 0) return false; - for (int32 i = CountItems() - 1; i >= 0; i--) { - if (ItemAt(i)->View() == view) { - InvalidateLayout(); - return true; - } + for (int32 i = 0; i < count; i++) { + BLayout* layout = viewPrivate.LayoutItemAt(i)->Layout(); + if (layout->InvalidationLegal()) + layout->InvalidateLayout(); } - return found; + return true; } @@ -653,3 +636,4 @@ BLayout::SetTarget(BView* target) InvalidateLayout(); } } + diff --git a/src/kits/interface/LayoutItem.cpp b/src/kits/interface/LayoutItem.cpp index 25ba24243d..43bb42ee39 100644 --- a/src/kits/interface/LayoutItem.cpp +++ b/src/kits/interface/LayoutItem.cpp @@ -168,6 +168,9 @@ BLayoutItem::SetLayout(BLayout* layout) if (layout) DetachedFromLayout(layout); + // TODO: is this the right place to do this? + // at this point, this->Layout() will return not exactly truthful + // values... this could cause problems in DetachedFromLayout(); if (BView* view = View()) { if (layout && !fLayout) { BView::Private(view).DeregisterLayoutItem(this); diff --git a/src/kits/interface/View.cpp b/src/kits/interface/View.cpp index 9ce5062984..317ee81e53 100644 --- a/src/kits/interface/View.cpp +++ b/src/kits/interface/View.cpp @@ -4022,8 +4022,14 @@ BView::PreviousSibling() const bool BView::RemoveSelf() { - if (fParent && fParent->fLayoutData->fLayout) - return fParent->fLayoutData->fLayout->RemoveViewRecursive(this); + if (fParent && fParent->fLayoutData->fLayout) { + int32 itemCount = fLayoutData->fLayoutItems.CountItems(); + for (int32 i = 0; i < itemCount; i++) { + BLayoutItem* item = fLayoutData->fLayoutItems.ItemAt(i); + item->Layout()->RemoveItem(item); + delete item; + } + } return _RemoveSelf(); }