Refactor BALMLayout::AddItem to reduce repitition.
This commit is contained in:
@@ -151,17 +151,12 @@ private:
|
|||||||
BALMLayout(const BALMLayout&);
|
BALMLayout(const BALMLayout&);
|
||||||
void operator =(const BALMLayout&);
|
void operator =(const BALMLayout&);
|
||||||
|
|
||||||
struct XTabRemover;
|
private:
|
||||||
struct XTabRemoverFunc;
|
template <class T>
|
||||||
|
struct TabAddTransaction;
|
||||||
|
|
||||||
struct YTabRemover;
|
template <class T>
|
||||||
struct YTabRemoverFunc;
|
friend class TabAddTransaction;
|
||||||
|
|
||||||
friend struct XTabRemover;
|
|
||||||
friend struct XTabRemoverFunc;
|
|
||||||
|
|
||||||
friend struct YTabRemover;
|
|
||||||
friend struct YTabRemoverFunc;
|
|
||||||
|
|
||||||
friend class XTab;
|
friend class XTab;
|
||||||
friend class YTab;
|
friend class YTab;
|
||||||
@@ -172,6 +167,10 @@ private:
|
|||||||
|
|
||||||
void _RemoveSelfFromTab(XTab* tab);
|
void _RemoveSelfFromTab(XTab* tab);
|
||||||
void _RemoveSelfFromTab(YTab* tab);
|
void _RemoveSelfFromTab(YTab* tab);
|
||||||
|
bool _HasTabInLayout(XTab* tab);
|
||||||
|
bool _HasTabInLayout(YTab* tab);
|
||||||
|
bool _AddedTab(XTab* tab);
|
||||||
|
bool _AddedTab(YTab* tab);
|
||||||
|
|
||||||
BLayoutItem* _LayoutItemToAdd(BView* view);
|
BLayoutItem* _LayoutItemToAdd(BView* view);
|
||||||
|
|
||||||
|
|||||||
+75
-113
@@ -25,80 +25,75 @@ using namespace LinearProgramming;
|
|||||||
const BSize kUnsetSize(B_SIZE_UNSET, B_SIZE_UNSET);
|
const BSize kUnsetSize(B_SIZE_UNSET, B_SIZE_UNSET);
|
||||||
|
|
||||||
|
|
||||||
struct BALMLayout::XTabRemoverFunc {
|
namespace BALM {
|
||||||
XTabRemoverFunc()
|
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
struct BALMLayout::TabAddTransaction {
|
||||||
|
~TabAddTransaction()
|
||||||
{
|
{
|
||||||
|
if (fTab)
|
||||||
|
fLayout->_RemoveSelfFromTab(fTab);
|
||||||
|
if (fIndex > 0)
|
||||||
|
_TabList()->RemoveItemAt(fIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void operator()(XTab* tab)
|
TabAddTransaction(BALMLayout* layout)
|
||||||
{
|
|
||||||
if (tab) {
|
|
||||||
layout->_RemoveSelfFromTab(tab);
|
|
||||||
if (index > 0)
|
|
||||||
layout->fXTabList.RemoveItemAt(index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BALMLayout* layout;
|
|
||||||
int32 index;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
struct BALMLayout::XTabRemover
|
|
||||||
: public AutoDeleter<XTab, BALMLayout::XTabRemoverFunc> {
|
|
||||||
|
|
||||||
typedef AutoDeleter<XTab, BALMLayout::XTabRemoverFunc> Base;
|
|
||||||
|
|
||||||
XTabRemover(BALMLayout* layout, XTab* tab = NULL)
|
|
||||||
:
|
:
|
||||||
Base(tab)
|
fTab(NULL),
|
||||||
|
fLayout(layout),
|
||||||
|
fIndex(-1)
|
||||||
{
|
{
|
||||||
fDelete.layout = layout;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetIndexTo(int32 index)
|
bool AttempAdd(T* tab)
|
||||||
{
|
{
|
||||||
fDelete.index = index;
|
if (fLayout->_HasTabInLayout(tab))
|
||||||
|
return true;
|
||||||
|
if (!fLayout->_AddedTab(tab))
|
||||||
|
return false;
|
||||||
|
fTab = tab;
|
||||||
|
|
||||||
|
BObjectList<T>* tabList = _TabList();
|
||||||
|
int32 index = tabList->CountItems();
|
||||||
|
if (!tabList->AddItem(tab, index))
|
||||||
|
return false;
|
||||||
|
fIndex = index;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Commit()
|
||||||
|
{
|
||||||
|
fTab = NULL;
|
||||||
|
fIndex = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
BObjectList<T>* _TabList();
|
||||||
|
|
||||||
|
T* fTab;
|
||||||
|
BALMLayout* fLayout;
|
||||||
|
int32 fIndex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct BALMLayout::YTabRemoverFunc {
|
template <>
|
||||||
YTabRemoverFunc()
|
BObjectList<XTab>*
|
||||||
{
|
BALMLayout::TabAddTransaction<XTab>::_TabList()
|
||||||
}
|
{
|
||||||
|
return &fLayout->fXTabList;
|
||||||
void operator()(YTab* tab)
|
}
|
||||||
{
|
|
||||||
if (tab) {
|
|
||||||
layout->_RemoveSelfFromTab(tab);
|
|
||||||
if (index > 0)
|
|
||||||
layout->fYTabList.RemoveItemAt(index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BALMLayout* layout;
|
|
||||||
int32 index;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
struct BALMLayout::YTabRemover
|
template <>
|
||||||
: public AutoDeleter<YTab, BALMLayout::YTabRemoverFunc> {
|
BObjectList<YTab>*
|
||||||
|
BALMLayout::TabAddTransaction<YTab>::_TabList()
|
||||||
typedef AutoDeleter<YTab, BALMLayout::YTabRemoverFunc> Base;
|
{
|
||||||
|
return &fLayout->fYTabList;
|
||||||
|
}
|
||||||
|
|
||||||
YTabRemover(BALMLayout* layout, YTab* tab = NULL)
|
|
||||||
:
|
|
||||||
Base(tab)
|
|
||||||
{
|
|
||||||
fDelete.layout = layout;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetIndexTo(int32 index)
|
}; // end namespace BALM
|
||||||
{
|
|
||||||
fDelete.index = index;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
BALM::BALMLayout::BadLayoutPolicy::~BadLayoutPolicy()
|
BALM::BALMLayout::BadLayoutPolicy::~BadLayoutPolicy()
|
||||||
@@ -578,51 +573,22 @@ BALMLayout::AddItem(BLayoutItem* item, XTab* left, YTab* top, XTab* _right,
|
|||||||
if (bottom.Get() == NULL)
|
if (bottom.Get() == NULL)
|
||||||
bottom = AddYTab();
|
bottom = AddYTab();
|
||||||
|
|
||||||
// TODO: make sure all tabs get into the lists
|
TabAddTransaction<XTab> leftTabAdd(this);
|
||||||
XTabRemover leftRemover(this);
|
if (!leftTabAdd.AttempAdd(left))
|
||||||
if (!left->IsInLayout(this)) {
|
return NULL;
|
||||||
if (!left->AddedToLayout(this))
|
|
||||||
return NULL;
|
|
||||||
leftRemover.SetTo(left);
|
|
||||||
|
|
||||||
if (!fXTabList.AddItem(left, fXTabList.CountItems()))
|
TabAddTransaction<YTab> topTabAdd(this);
|
||||||
return NULL;
|
if (!topTabAdd.AttempAdd(top))
|
||||||
leftRemover.SetIndexTo(fXTabList.CountItems() - 1);
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
YTabRemover topRemover(this);
|
TabAddTransaction<XTab> rightTabAdd(this);
|
||||||
if (!top->IsInLayout(this)) {
|
if (!rightTabAdd.AttempAdd(right))
|
||||||
if (!top->AddedToLayout(this))
|
return NULL;
|
||||||
return NULL;
|
|
||||||
topRemover.SetTo(top);
|
|
||||||
|
|
||||||
if (!fYTabList.AddItem(top, fYTabList.CountItems()))
|
TabAddTransaction<YTab> bottomTabAdd(this);
|
||||||
return NULL;
|
if (!bottomTabAdd.AttempAdd(bottom))
|
||||||
topRemover.SetIndexTo(fYTabList.CountItems() - 1);
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
XTabRemover rightRemover(this);
|
|
||||||
if (_right != NULL && !right->IsInLayout(this)) {
|
|
||||||
if (!right->AddedToLayout(this))
|
|
||||||
return NULL;
|
|
||||||
rightRemover.SetTo(right);
|
|
||||||
|
|
||||||
if (!fXTabList.AddItem(right, fXTabList.CountItems()))
|
|
||||||
return NULL;
|
|
||||||
rightRemover.SetIndexTo(fXTabList.CountItems() - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
YTabRemover bottomRemover(this);
|
|
||||||
if (_bottom != NULL && !bottom->IsInLayout(this)) {
|
|
||||||
if (!bottom->AddedToLayout(this))
|
|
||||||
return NULL;
|
|
||||||
bottomRemover.SetTo(bottom);
|
|
||||||
|
|
||||||
if (!fYTabList.AddItem(bottom, fYTabList.CountItems()))
|
|
||||||
return NULL;
|
|
||||||
bottomRemover.SetIndexTo(fYTabList.CountItems() - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Area is added in ItemAdded
|
// Area is added in ItemAdded
|
||||||
if (!BAbstractLayout::AddItem(-1, item))
|
if (!BAbstractLayout::AddItem(-1, item))
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -635,10 +601,10 @@ BALMLayout::AddItem(BLayoutItem* item, XTab* left, YTab* top, XTab* _right,
|
|||||||
area->_Init(fSolver, left, top, right, bottom, fRowColumnManager);
|
area->_Init(fSolver, left, top, right, bottom, fRowColumnManager);
|
||||||
fRowColumnManager->AddArea(area);
|
fRowColumnManager->AddArea(area);
|
||||||
|
|
||||||
leftRemover.Detach();
|
leftTabAdd.Commit();
|
||||||
rightRemover.Detach();
|
topTabAdd.Commit();
|
||||||
topRemover.Detach();
|
rightTabAdd.Commit();
|
||||||
bottomRemover.Detach();
|
bottomTabAdd.Commit();
|
||||||
return area;
|
return area;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1049,18 +1015,14 @@ BALMLayout::InsetForTab(YTab* tab) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void BALMLayout::_RemoveSelfFromTab(XTab* tab) { tab->LayoutLeaving(this); }
|
||||||
BALMLayout::_RemoveSelfFromTab(XTab* tab)
|
void BALMLayout::_RemoveSelfFromTab(YTab* tab) { tab->LayoutLeaving(this); }
|
||||||
{
|
|
||||||
tab->LayoutLeaving(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
bool BALMLayout::_HasTabInLayout(XTab* tab) { return tab->IsInLayout(this); }
|
||||||
|
bool BALMLayout::_HasTabInLayout(YTab* tab) { return tab->IsInLayout(this); }
|
||||||
|
|
||||||
void
|
bool BALMLayout::_AddedTab(XTab* tab) { return tab->AddedToLayout(this); }
|
||||||
BALMLayout::_RemoveSelfFromTab(YTab* tab)
|
bool BALMLayout::_AddedTab(YTab* tab) { return tab->AddedToLayout(this); }
|
||||||
{
|
|
||||||
tab->LayoutLeaving(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
BLayoutItem*
|
BLayoutItem*
|
||||||
|
|||||||
Reference in New Issue
Block a user