Make tabs BReferenceable. Maintain a list of tabs.

This commit is contained in:
czeidler 2011-11-30 17:55:36 +13:00
parent b394b1d2bb
commit 57014d1ff7
10 changed files with 181 additions and 82 deletions

View File

@ -35,12 +35,16 @@ public:
BALMLayout* friendLayout = NULL);
virtual ~BALMLayout();
XTab* AddXTab();
YTab* AddYTab();
BReference<XTab> AddXTab();
BReference<YTab> AddYTab();
int32 CountXTabs() const;
int32 CountYTabs() const;
XTab* XTabAt(int32 index) const;
YTab* YTabAt(int32 index) const;
/*! Order the tab list and return a reference to the list. */
const XTabList& OrderedXTabs();
const YTabList& OrderedYTabs();
Row* AddRow(YTab* top, YTab* bottom);
Column* AddColumn(XTab* left, XTab* right);
@ -104,6 +108,7 @@ public:
YTab* bottom = NULL);
virtual Area* AddItem(BLayoutItem* item, Row* row,
Column* column);
virtual Area* AddItemToRight(BLayoutItem* item,
XTab* right = NULL, YTab* top = NULL,
YTab* bottom = NULL);
@ -129,6 +134,9 @@ public:
virtual void DerivedLayoutItems();
private:
friend class XTab;
friend class YTab;
/*! Add a view without initialize the Area. */
BLayoutItem* _CreateLayoutItem(BView* view);
@ -138,16 +146,18 @@ private:
BSize _CalculateMaxSize();
BSize _CalculatePreferredSize();
void _ParseGroupItem(GroupItem& item, XTab* left,
YTab* top, XTab* right, YTab* bottom);
void _ParseGroupItem(GroupItem& item,
BReference<XTab> left, BReference<YTab> top,
BReference<XTab> right,
BReference<YTab> bottom);
LinearSpec* fSolver;
LinearSpec fOwnSolver;
XTab* fLeft;
XTab* fRight;
YTab* fTop;
YTab* fBottom;
BReference<XTab> fLeft;
BReference<XTab> fRight;
BReference<YTab> fTop;
BReference<YTab> fBottom;
BSize fMinSize;
BSize fMaxSize;
BSize fPreferredSize;

View File

@ -103,8 +103,8 @@ public:
void InvalidateSizeConstraints();
BRect Frame();
BRect ItemFrame();
BRect Frame() const;
BRect ItemFrame() const;
private:
Area(BLayoutItem* item);
@ -124,10 +124,10 @@ private:
LinearSpec* fLS;
XTab* fLeft;
XTab* fRight;
YTab* fTop;
YTab* fBottom;
BReference<XTab> fLeft;
BReference<XTab> fRight;
BReference<YTab> fTop;
BReference<YTab> fBottom;
Row* fRow;
Column* fColumn;

View File

@ -33,8 +33,8 @@ private:
Column(LinearSpec* ls, XTab* left, XTab* right);
LinearSpec* fLS;
XTab* fLeft;
XTab* fRight;
BReference<XTab> fLeft;
BReference<XTab> fRight;
//! managed by RowColumnManager
Constraint* fPrefSizeConstraint;

View File

@ -33,8 +33,8 @@ private:
Row(LinearSpec* ls, YTab* top, YTab* bottom);
LinearSpec* fLS;
YTab* fTop;
YTab* fBottom;
BReference<YTab> fTop;
BReference<YTab> fBottom;
//! managed by RowColumnManager
Constraint* fPrefSizeConstraint;

View File

@ -6,6 +6,7 @@
#define X_TAB_H
#include <Referenceable.h>
#include "LinearSpec.h"
#include "Variable.h"
@ -13,34 +14,37 @@
namespace BALM {
class BALMLayout;
/**
* Vertical grid line (x-tab).
*/
class XTab : public Variable {
protected:
XTab(LinearSpec* ls)
:
Variable(ls)
{
}
class XTab : public Variable, public BReferenceable {
public:
virtual ~XTab();
friend class BALMLayout;
protected:
XTab(BALMLayout* layout);
private:
BALMLayout* fALMLayout;
};
class YTab : public Variable {
protected:
YTab(LinearSpec* ls)
:
Variable(ls)
{
}
class YTab : public Variable, public BReferenceable {
public:
virtual ~YTab();
friend class BALMLayout;
protected:
YTab(BALMLayout* layout);
private:
BALMLayout* fALMLayout;
};

View File

@ -68,16 +68,14 @@ BALMLayout::~BALMLayout()
*
* @return the new x-tab
*/
XTab*
BReference<XTab>
BALMLayout::AddXTab()
{
XTab* tab = new(std::nothrow) XTab(fSolver);
BReference<XTab> tab(new(std::nothrow) XTab(this), true);
if (!tab)
return NULL;
if (!fSolver->AddVariable(tab)) {
delete tab;
if (!fSolver->AddVariable(tab))
return NULL;
}
fXTabList.AddItem(tab);
return tab;
@ -89,16 +87,14 @@ BALMLayout::AddXTab()
*
* @return the new y-tab
*/
YTab*
BReference<YTab>
BALMLayout::AddYTab()
{
YTab* tab = new(std::nothrow) YTab(fSolver);
if (!tab)
BReference<YTab> tab(new(std::nothrow) YTab(this), true);
if (tab.Get() == NULL)
return NULL;
if (!fSolver->AddVariable(tab)) {
delete tab;
if (!fSolver->AddVariable(tab))
return NULL;
}
fYTabList.AddItem(tab);
return tab;
@ -133,6 +129,45 @@ BALMLayout::YTabAt(int32 index) const
}
int
CompareXTabFunc(const XTab* tab1, const XTab* tab2)
{
if (tab1->Value() < tab2->Value())
return -1;
else if (tab1->Value() == tab2->Value())
return 0;
return 1;
}
int
CompareYTabFunc(const YTab* tab1, const YTab* tab2)
{
if (tab1->Value() < tab2->Value())
return -1;
else if (tab1->Value() == tab2->Value())
return 0;
return 1;
}
const XTabList&
BALMLayout::OrderedXTabs()
{
fXTabList.SortItems(CompareXTabFunc);
return fXTabList;
}
const YTabList&
BALMLayout::OrderedYTabs()
{
fYTabList.SortItems(CompareYTabFunc);
return fYTabList;
}
/**
* Adds a new row to the specification that is glued to the given y-tabs.
*
@ -141,11 +176,13 @@ BALMLayout::YTabAt(int32 index) const
* @return the new row
*/
Row*
BALMLayout::AddRow(YTab* top, YTab* bottom)
BALMLayout::AddRow(YTab* _top, YTab* _bottom)
{
if (top == NULL)
BReference<YTab> top = _top;
BReference<YTab> bottom = _bottom;
if (_top == NULL)
top = AddYTab();
if (bottom == NULL)
if (_bottom == NULL)
bottom = AddYTab();
return new(std::nothrow) Row(fSolver, top, bottom);
}
@ -159,11 +196,13 @@ BALMLayout::AddRow(YTab* top, YTab* bottom)
* @return the new column
*/
Column*
BALMLayout::AddColumn(XTab* left, XTab* right)
BALMLayout::AddColumn(XTab* _left, XTab* _right)
{
if (left == NULL)
BReference<XTab> left = _left;
BReference<XTab> right = _right;
if (_left == NULL)
left = AddXTab();
if (right == NULL)
if (_right == NULL)
right = AddXTab();
return new(std::nothrow) Column(fSolver, left, right);
}
@ -319,13 +358,13 @@ void
BALMLayout::BuildLayout(GroupItem& item, XTab* left, YTab* top, XTab* right,
YTab* bottom)
{
if (!left)
if (left == NULL)
left = Left();
if (!top)
if (top == NULL)
top = Top();
if (!right)
if (right == NULL)
right = Right();
if (!bottom)
if (bottom == NULL)
bottom = Bottom();
_ParseGroupItem(item, left, top, right, bottom);
@ -333,8 +372,8 @@ BALMLayout::BuildLayout(GroupItem& item, XTab* left, YTab* top, XTab* right,
void
BALMLayout::_ParseGroupItem(GroupItem& item, XTab* left, YTab* top, XTab* right,
YTab* bottom)
BALMLayout::_ParseGroupItem(GroupItem& item, BReference<XTab> left,
BReference<YTab> top, BReference<XTab> right, BReference<YTab> bottom)
{
if (item.LayoutItem())
AddItem(item.LayoutItem(), left, top, right, bottom);
@ -346,14 +385,14 @@ BALMLayout::_ParseGroupItem(GroupItem& item, XTab* left, YTab* top, XTab* right,
GroupItem& current = const_cast<GroupItem&>(
item.GroupItems()[i]);
if (item.Orientation() == B_HORIZONTAL) {
XTab* r = (i == item.GroupItems().size() - 1) ? right
BReference<XTab> r = (i == item.GroupItems().size() - 1) ? right
: AddXTab();
_ParseGroupItem(current, left, top, r, bottom);
left = r;
}
else {
YTab* b = (i == item.GroupItems().size() - 1) ? bottom
: AddYTab();
BReference<YTab> b = (i == item.GroupItems().size() - 1)
? bottom : AddYTab();
_ParseGroupItem(current, left, top, right, b);
top = b;
}
@ -510,12 +549,14 @@ BALMLayout::AddItem(int32 index, BLayoutItem* item)
Area*
BALMLayout::AddItem(BLayoutItem* item, XTab* left, YTab* top, XTab* right,
YTab* bottom)
BALMLayout::AddItem(BLayoutItem* item, XTab* left, YTab* top, XTab* _right,
YTab* _bottom)
{
if (!right)
BReference<XTab> right = _right;
if (right.Get() == NULL)
right = AddXTab();
if (!bottom)
BReference<YTab> bottom = _bottom;
if (bottom.Get() == NULL)
bottom = AddYTab();
// Area is added int ItemAdded
@ -551,14 +592,15 @@ BALMLayout::AddItem(BLayoutItem* item, Row* row, Column* column)
Area*
BALMLayout::AddItemToRight(BLayoutItem* item, XTab* right, YTab* top,
BALMLayout::AddItemToRight(BLayoutItem* item, XTab* _right, YTab* top,
YTab* bottom)
{
if (fCurrentArea == NULL)
return NULL;
XTab* left = fCurrentArea->Right();
if (!right)
BReference<XTab> right = _right;
if (_right == NULL)
right = AddXTab();
if (!top)
top = fCurrentArea->Top();
@ -570,13 +612,14 @@ BALMLayout::AddItemToRight(BLayoutItem* item, XTab* right, YTab* top,
Area*
BALMLayout::AddItemToLeft(BLayoutItem* item, XTab* left, YTab* top,
BALMLayout::AddItemToLeft(BLayoutItem* item, XTab* _left, YTab* top,
YTab* bottom)
{
if (fCurrentArea == NULL)
return NULL;
if (!left)
BReference<XTab> left = _left;
if (_left == NULL)
left = AddXTab();
XTab* right = fCurrentArea->Left();
if (!top)
@ -589,7 +632,7 @@ BALMLayout::AddItemToLeft(BLayoutItem* item, XTab* left, YTab* top,
Area*
BALMLayout::AddItemToTop(BLayoutItem* item, YTab* top, XTab* left, XTab* right)
BALMLayout::AddItemToTop(BLayoutItem* item, YTab* _top, XTab* left, XTab* right)
{
if (fCurrentArea == NULL)
return NULL;
@ -598,7 +641,8 @@ BALMLayout::AddItemToTop(BLayoutItem* item, YTab* top, XTab* left, XTab* right)
left = fCurrentArea->Left();
if (!right)
right = fCurrentArea->Right();
if (!top)
BReference<YTab> top = _top;
if (_top == NULL)
top = AddYTab();
YTab* bottom = fCurrentArea->Top();
@ -607,7 +651,7 @@ BALMLayout::AddItemToTop(BLayoutItem* item, YTab* top, XTab* left, XTab* right)
Area*
BALMLayout::AddItemToBottom(BLayoutItem* item, YTab* bottom, XTab* left,
BALMLayout::AddItemToBottom(BLayoutItem* item, YTab* _bottom, XTab* left,
XTab* right)
{
if (fCurrentArea == NULL)
@ -618,7 +662,8 @@ BALMLayout::AddItemToBottom(BLayoutItem* item, YTab* bottom, XTab* left,
if (!right)
right = fCurrentArea->Right();
YTab* top = fCurrentArea->Bottom();
if (!bottom)
BReference<YTab> bottom = _bottom;
if (_bottom == NULL)
bottom = AddYTab();
return AddItem(item, left, top, right, bottom);

View File

@ -337,7 +337,7 @@ Area::LeftInset() const
return fTopLeftInset.Width();
BALMLayout* layout = static_cast<BALMLayout*>(fLayoutItem->Layout());
if (fLeft == layout->Left())
if (fLeft.Get() == layout->Left())
return layout->Inset();
return layout->Spacing() / 2;
}
@ -353,7 +353,7 @@ Area::TopInset() const
return fTopLeftInset.Height();
BALMLayout* layout = static_cast<BALMLayout*>(fLayoutItem->Layout());
if (fTop == layout->Top())
if (fTop.Get() == layout->Top())
return layout->Inset();
return layout->Spacing() / 2;
}
@ -369,7 +369,7 @@ Area::RightInset() const
return fRightBottomInset.Width();
BALMLayout* layout = static_cast<BALMLayout*>(fLayoutItem->Layout());
if (fRight == layout->Right())
if (fRight.Get() == layout->Right())
return layout->Inset();
return layout->Spacing() / 2;
}
@ -385,7 +385,7 @@ Area::BottomInset() const
return fRightBottomInset.Height();
BALMLayout* layout = static_cast<BALMLayout*>(fLayoutItem->Layout());
if (fBottom == layout->Bottom())
if (fBottom.Get() == layout->Bottom())
return layout->Inset();
return layout->Spacing() / 2;
}
@ -497,7 +497,7 @@ Area::InvalidateSizeConstraints()
BRect
Area::Frame()
Area::Frame() const
{
return BRect(fLeft->Value(), fTop->Value(), fRight->Value(),
fBottom->Value());
@ -505,7 +505,7 @@ Area::Frame()
BRect
Area::ItemFrame()
Area::ItemFrame() const
{
return fLayoutItem->Frame();
}

View File

@ -12,6 +12,7 @@ SharedLibrary libalm.so :
Column.cpp
Row.cpp
RowColumnManager.cpp
Tab.cpp
:
liblpsolve55.so liblinprog.a
be $(TARGET_LIBSTDC++)

View File

@ -108,7 +108,8 @@ RowColumnManager::_FindRowFor(Area* area)
{
for (int32 i = 0; i < fRows.CountItems(); i++) {
Row* row = fRows.ItemAt(i);
if (row->fTop == area->Top() && row->fBottom == area->Bottom())
if (row->fTop.Get() == area->Top()
&& row->fBottom.Get() == area->Bottom())
return row;
}
return NULL;
@ -120,7 +121,8 @@ RowColumnManager::_FindColumnFor(Area* area)
{
for (int32 i = 0; i < fColumns.CountItems(); i++) {
Column* column = fColumns.ItemAt(i);
if (column->fLeft == area->Left() && column->fRight == area->Right())
if (column->fLeft.Get() == area->Left()
&& column->fRight.Get() == area->Right())
return column;
}
return NULL;

37
src/libs/alm/Tab.cpp Normal file
View File

@ -0,0 +1,37 @@
/*
* Copyright 2011, Clemens Zeidler <haiku@clemens-zeidler.de>
* Distributed under the terms of the MIT License.
*/
#include <Tab.h>
#include <ALMLayout.h>
XTab::XTab(BALMLayout* layout)
:
Variable(layout->Solver()),
fALMLayout(layout)
{
}
XTab::~XTab()
{
fALMLayout->fXTabList.RemoveItem(this);
}
YTab::YTab(BALMLayout* layout)
:
Variable(layout->Solver()),
fALMLayout(layout)
{
}
YTab::~YTab()
{
fALMLayout->fYTabList.RemoveItem(this);
}