First step to make the ALM layout engine more compatible with the haiku layout API. Many stuff was/is managed by ALM which could and should be done by the standard layout facilities.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@38753 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Clemens Zeidler 2010-09-21 07:04:40 +00:00
parent 88ff3bdb33
commit 248bbad27c
5 changed files with 151 additions and 247 deletions

View File

@ -33,6 +33,8 @@ class Area {
public:
~Area();
BView* View();
XTab* Left() const;
void SetLeft(XTab* left);
XTab* Right() const;
@ -47,13 +49,12 @@ public:
Column* GetColumn() const;
void SetColumn(Column* column);
BView* Content() const;
void SetContent(BView* content);
XTab* ContentLeft() const;
YTab* ContentTop() const;
XTab* ContentRight() const;
YTab* ContentBottom() const;
BSize MinContentSize() const;
void SetMinContentSize(BSize min);
BSize MaxContentSize() const;
void SetMaxContentSize(BSize max);
@ -62,17 +63,17 @@ public:
double ContentAspectRatio() const;
void SetContentAspectRatio(double ratio);
void SetExplicitAlignment(BAlignment alignment);
void SetHorizontalAlignment(alignment horizontal);
void SetVerticalAlignment(
vertical_alignment vertical);
BSize ShrinkPenalties() const;
void SetShrinkPenalties(BSize shrink);
BSize GrowPenalties() const;
void SetGrowPenalties(BSize grow);
BAlignment Alignment() const;
void SetAlignment(BAlignment alignment);
void SetHorizontalAlignment(alignment horizontal);
void SetVerticalAlignment(
vertical_alignment vertical);
int32 LeftInset() const;
void SetLeftInset(int32 left);
int32 TopInset() const;
@ -94,23 +95,20 @@ public:
BList* HasSameSizeAs(Area* area);
protected:
Area(BALMLayout* layout,
LinearSpec* ls, XTab* left, YTab* top,
Area(BALMLayout* layout, BLayoutItem* item);
void Init(LinearSpec* ls, XTab* left, YTab* top,
XTab* right, YTab* bottom, BView* content,
BSize minContentSize);
Area(BALMLayout* layout,
LinearSpec* ls, Row* row, Column* column,
void Init(LinearSpec* ls, Row* row, Column* column,
BView* content, BSize minContentSize);
void DoLayout();
private:
void InitChildArea();
void UpdateHorizontal();
void UpdateVertical();
void Init(BALMLayout* layout,
LinearSpec* ls, XTab* left, YTab* top,
XTab* right, YTab* bottom, BView* content,
BSize minContentSize);
public:
static BSize kMaxSize;
@ -118,12 +116,12 @@ public:
static BSize kUndefinedSize;
protected:
BView* fContent;
BList* fConstraints;
private:
// TODO remove the layout pointer when making Area a LayoutItem
BALMLayout* fALMLayout;
BLayoutItem* fLayoutItem;
LinearSpec* fLS;
XTab* fLeft;

View File

@ -30,6 +30,8 @@ namespace BALM {
class BALMLayout : public BAbstractLayout {
public:
BALMLayout();
virtual ~BALMLayout();
void SolveLayout();
XTab* AddXTab();
@ -39,15 +41,15 @@ public:
Column* AddColumn();
Column* AddColumn(XTab* left, XTab* right);
Area* AddArea(XTab* left, YTab* top, XTab* right, YTab* bottom,
BView* content, BSize minContentSize);
Area* AddArea(Row* row, Column* column, BView* content,
BSize minContentSize);
Area* AddArea(XTab* left, YTab* top, XTab* right, YTab* bottom,
BView* content);
Area* AddArea(Row* row, Column* column, BView* content);
Area* AddArea(XTab* left, YTab* top, XTab* right,
YTab* bottom, BView* content, BSize minContentSize);
Area* AddArea(Row* row, Column* column,
BView* content, BSize minContentSize);
Area* AddArea(XTab* left, YTab* top, XTab* right,
YTab* bottom, BView* content);
Area* AddArea(Row* row, Column* column,
BView* content);
Area* AreaOf(BView* control);
BList* Areas() const;
XTab* Left() const;
XTab* Right() const;
@ -59,23 +61,16 @@ public:
LayoutStyleType LayoutStyle() const;
void SetLayoutStyle(LayoutStyleType style);
BLayoutItem* AddView(BView* child);
BLayoutItem* AddView(int32 index, BView* child);
bool AddItem(BLayoutItem* item);
bool AddItem(int32 index, BLayoutItem* item);
bool RemoveView(BView* child);
bool RemoveItem(BLayoutItem* item);
BLayoutItem* RemoveItem(int32 index);
BSize BaseMinSize();
BSize BaseMaxSize();
BSize BasePreferredSize();
BAlignment BaseAlignment();
bool HasHeightForWidth();
void GetHeightForWidth(float width, float* min,
float* max, float* preferred);
void InvalidateLayout(bool children = false);
virtual void DerivedLayoutItems();
virtual void InvalidateLayout(bool children = false);
virtual bool ItemAdded(BLayoutItem* item, int32 atIndex);
virtual void ItemRemoved(BLayoutItem* item, int32 fromIndex);
virtual void DerivedLayoutItems();
char* PerformancePath() const;
void SetPerformancePath(char* path);
@ -83,6 +78,8 @@ public:
LinearSpec* Solver();
private:
Area* _AreaForItem(BLayoutItem* item) const;
BSize CalculateMinSize();
BSize CalculateMaxSize();
BSize CalculatePreferredSize();
@ -93,7 +90,6 @@ private:
LinearSpec fSolver;
BList* fAreas;
XTab* fLeft;
XTab* fRight;
YTab* fTop;

View File

@ -30,6 +30,13 @@ BSize Area::kMinSize(0, 0);
BSize Area::kUndefinedSize(-1, -1);
BView*
Area::View()
{
return fLayoutItem->View();
}
/**
* Gets the auto preferred content size.
*
@ -234,28 +241,6 @@ Area::SetColumn(Column* column)
}
/**
* Gets the control that is the content of the area.
*/
BView*
Area::Content() const
{
return (fChildArea == NULL) ? fContent : fChildArea->Content();
}
/**
* Sets the control that is the content of the area.
*/
void
Area::SetContent(BView* content)
{
if (fChildArea == NULL) fContent = content;
else fChildArea->fContent = content;
fALMLayout->InvalidateLayout();
}
/**
* Left tab of the area's content. May be different from the left tab of the area.
*/
@ -487,25 +472,16 @@ Area::SetContentAspectRatio(double ratio)
}
/**
* Gets alignment of the content in its area.
*/
BAlignment
Area::Alignment() const
{
return fAlignment;
}
/**
* Sets alignment of the content in its area.
*/
void
Area::SetAlignment(BAlignment alignment)
Area::SetExplicitAlignment(BAlignment alignment)
{
fAlignment = alignment;
UpdateHorizontal();
UpdateVertical();
fALMLayout->InvalidateLayout();
}
@ -625,24 +601,24 @@ Area::SetBottomInset(int32 bottom)
void
Area::SetDefaultBehavior()
{
if (Content() == NULL) {
if (View() == NULL) {
SetPreferredContentSize(BSize(0, 0));
SetShrinkPenalties(BSize(0, 0));
SetGrowPenalties(BSize(0, 0));
return;
}
if (PreferredContentSize() != Content()->PreferredSize()){
SetPreferredContentSize(Content()->PreferredSize());
if (PreferredContentSize() != View()->PreferredSize()){
SetPreferredContentSize(View()->PreferredSize());
fALMLayout->InvalidateLayout();
}
if (dynamic_cast<BButton*>(Content()) != NULL
|| dynamic_cast<BRadioButton*>(Content()) != NULL
|| dynamic_cast<BCheckBox*>(Content()) != NULL
|| dynamic_cast<BStringView*>(Content()) != NULL
|| dynamic_cast<BPictureButton*>(Content()) != NULL
|| dynamic_cast<BStatusBar*>(Content()) != NULL) {
if (dynamic_cast<BButton*>(View()) != NULL
|| dynamic_cast<BRadioButton*>(View()) != NULL
|| dynamic_cast<BCheckBox*>(View()) != NULL
|| dynamic_cast<BStringView*>(View()) != NULL
|| dynamic_cast<BPictureButton*>(View()) != NULL
|| dynamic_cast<BStatusBar*>(View()) != NULL) {
fShrinkPenalties = BSize(4, 4);
fGrowPenalties = BSize(3, 3);
} else {
@ -727,10 +703,10 @@ Area::HasSameSizeAs(Area* area)
*/
Area::~Area()
{
if (fChildArea != NULL) delete fChildArea;
if (fChildArea != NULL)
delete fChildArea;
for (int32 i = 0; i < fConstraints->CountItems(); i++)
delete (Constraint*)fConstraints->ItemAt(i);
fALMLayout->Areas()->RemoveItem(this);
}
@ -738,25 +714,12 @@ Area::~Area()
* Constructor.
* Uses XTabs and YTabs.
*/
Area::Area(BALMLayout* layout, LinearSpec* ls, XTab* left, YTab* top,
XTab* right, YTab* bottom, BView* content, BSize minContentSize)
{
Init(layout, ls, left, top, right, bottom, content, minContentSize);
}
/**
* Constructor.
* Uses Rows and Columns.
*/
Area::Area(BALMLayout* layout, LinearSpec* ls, Row* row, Column* column,
BView* content, BSize minContentSize)
Area::Area(BALMLayout* layout, BLayoutItem* item)
:
fALMLayout(layout),
fLayoutItem(item)
{
Init(layout, ls, column->Left(), row->Top(), column->Right(), row->Bottom(),
content, minContentSize);
fRow = row;
fColumn = column;
}
@ -764,11 +727,9 @@ Area::Area(BALMLayout* layout, LinearSpec* ls, Row* row, Column* column,
* Initialize variables.
*/
void
Area::Init(BALMLayout* layout, LinearSpec* ls, XTab* left, YTab* top,
Area::Init(LinearSpec* ls, XTab* left, YTab* top,
XTab* right, YTab* bottom, BView* content, BSize minContentSize)
{
fALMLayout = layout;
fConstraints = new BList(2);
fMaxContentSize = kMaxSize;
@ -804,7 +765,6 @@ Area::Init(BALMLayout* layout, LinearSpec* ls, XTab* left, YTab* top,
fRight = right;
fTop = top;
fBottom = bottom;
SetContent(content);
fMinContentSize = minContentSize;
// adds the two essential constraints of the area that make sure that the left x-tab is
@ -819,23 +779,34 @@ Area::Init(BALMLayout* layout, LinearSpec* ls, XTab* left, YTab* top,
}
void
Area::Init(LinearSpec* ls, Row* row, Column* column,
BView* content, BSize minContentSize)
{
Init(ls, column->Left(), row->Top(), column->Right(), row->Bottom(),
content, minContentSize);
fRow = row;
fColumn = column;
}
/**
* Perform layout on the area.
*/
void Area::DoLayout()
{
if (Content() == NULL)
if (View() == NULL)
return; // empty areas need no layout
// if there is a childArea, then it is the childArea that actually contains the content
Area* area = (fChildArea != NULL) ? fChildArea : this;
// set content location and size
area->Content()->MoveTo(floor(area->Left()->Value() + 0.5),
area->View()->MoveTo(floor(area->Left()->Value() + 0.5),
floor(area->Top()->Value() + 0.5));
int32 width = (int32)floor(area->Right()->Value() - area->Left()->Value() + 0.5);
int32 height = (int32)floor(area->Bottom()->Value() - area->Top()->Value() + 0.5);
area->Content()->ResizeTo(width, height);
area->View()->ResizeTo(width, height);
}
@ -850,8 +821,10 @@ Area::InitChildArea()
// add a child area with new tabs,
// and add constraints that set its tabs to be equal to the
// coresponding tabs of this area (for a start)
fChildArea = new Area(fALMLayout, fLS, new XTab(fLS), new YTab(fLS),
new XTab(fLS), new YTab(fLS), fContent, BSize(0, 0));
fChildArea = new Area(fALMLayout, fLayoutItem);
fChildArea->Init(fLS, new XTab(fLS), new YTab(fLS), new XTab(fLS),
new YTab(fLS), View(), BSize(0, 0));
fLeftConstraint = fLeft->IsEqual(fChildArea->Left());
fConstraints->AddItem(fLeftConstraint);
fTopConstraint = fTop->IsEqual(fChildArea->Top());

View File

@ -5,6 +5,10 @@
*/
#include "BALMLayout.h"
#include <math.h> // for floor
#include <new>
#include "Area.h"
#include "Column.h"
#include "ResultType.h"
@ -12,8 +16,6 @@
#include "XTab.h"
#include "YTab.h"
#include <math.h> // for floor
/**
* Constructor.
@ -26,7 +28,6 @@ BALMLayout::BALMLayout()
fLayoutStyle = FIT_TO_SIZE;
fActivated = true;
fAreas = new BList(1);
fLeft = new XTab(&fSolver);
fRight = new XTab(&fSolver);
fTop = new YTab(&fSolver);
@ -46,6 +47,12 @@ BALMLayout::BALMLayout()
}
BALMLayout::~BALMLayout()
{
}
/**
* Solves the layout.
*/
@ -54,11 +61,9 @@ BALMLayout::SolveLayout()
{
// if autoPreferredContentSize is set on an area,
// readjust its preferredContentSize and penalties settings
int32 sizeAreas = fAreas->CountItems();
Area* currentArea;
for (int32 i = 0; i < sizeAreas; i++) {
currentArea = (Area*)fAreas->ItemAt(i);
if (currentArea->AutoPreferredContentSize())
for (int32 i = 0; i < CountItems(); i++) {
Area* currentArea = _AreaForItem(ItemAt(i));
if (currentArea && currentArea->AutoPreferredContentSize())
currentArea->SetDefaultBehavior();
}
@ -167,8 +172,10 @@ Column*
BALMLayout::AddColumn(XTab* left, XTab* right)
{
Column* column = new Column(&fSolver);
if (left != NULL) column->Constraints()->AddItem(column->Left()->IsEqual(left));
if (right != NULL) column->Constraints()->AddItem(column->Right()->IsEqual(right));
if (left != NULL)
column->Constraints()->AddItem(column->Left()->IsEqual(left));
if (right != NULL)
column->Constraints()->AddItem(column->Right()->IsEqual(right));
return column;
}
@ -188,12 +195,13 @@ Area*
BALMLayout::AddArea(XTab* left, YTab* top, XTab* right, YTab* bottom,
BView* content, BSize minContentSize)
{
InvalidateLayout();
if (content != NULL)
TargetView()->AddChild(content);
Area* area = new Area(this, &fSolver, left, top, right, bottom, content,
BLayoutItem* item = AddView(content);
Area* area = _AreaForItem(item);
if (!area)
return NULL;
area->Init(&fSolver, left, top, right, bottom, content,
minContentSize);
fAreas->AddItem(area);
return area;
}
@ -211,11 +219,12 @@ Area*
BALMLayout::AddArea(Row* row, Column* column, BView* content,
BSize minContentSize)
{
InvalidateLayout();
if (content != NULL)
TargetView()->AddChild(content);
Area* area = new Area(this, &fSolver, row, column, content, minContentSize);
fAreas->AddItem(area);
BLayoutItem* item = AddView(content);
Area* area = _AreaForItem(item);
if (!area)
return NULL;
area->Init(&fSolver, row, column, content, minContentSize);
return area;
}
@ -234,14 +243,15 @@ Area*
BALMLayout::AddArea(XTab* left, YTab* top, XTab* right, YTab* bottom,
BView* content)
{
InvalidateLayout();
if (content != NULL)
TargetView()->AddChild(content);
Area* area = new Area(this, &fSolver, left, top, right, bottom, content,
BLayoutItem* item = AddView(content);
Area* area = _AreaForItem(item);
if (!area)
return NULL;
area->Init(&fSolver, left, top, right, bottom, content,
BSize(0, 0));
area->SetDefaultBehavior();
area->SetAutoPreferredContentSize(false);
fAreas->AddItem(area);
return area;
}
@ -257,13 +267,14 @@ BALMLayout::AddArea(XTab* left, YTab* top, XTab* right, YTab* bottom,
Area*
BALMLayout::AddArea(Row* row, Column* column, BView* content)
{
InvalidateLayout();
if (content != NULL)
TargetView()->AddChild(content);
Area* area = new Area(this, &fSolver, row, column, content, BSize(0, 0));
BLayoutItem* item = AddView(content);
Area* area = _AreaForItem(item);
if (!area)
return NULL;
area->Init(&fSolver, row, column, content, BSize(0, 0));
area->SetDefaultBehavior();
area->SetAutoPreferredContentSize(false);
fAreas->AddItem(area);
return area;
}
@ -277,25 +288,7 @@ BALMLayout::AddArea(Row* row, Column* column, BView* content)
Area*
BALMLayout::AreaOf(BView* control)
{
Area* area;
for (int32 i = 0; i < fAreas->CountItems(); i++) {
area = (Area*)fAreas->ItemAt(i);
if (area->Content() == control)
return area;
}
return NULL;
}
/**
* Gets the ares.
*
* @return the areas
*/
BList*
BALMLayout::Areas() const
{
return fAreas;
return _AreaForItem(ItemAt(IndexOfView(control)));
}
@ -367,76 +360,6 @@ BALMLayout::SetLayoutStyle(LayoutStyleType style)
}
/**
* Adds view to layout.
*/
BLayoutItem*
BALMLayout::AddView(BView* child)
{
return NULL;
}
/**
* Adds view to layout.
*/
BLayoutItem*
BALMLayout::AddView(int32 index, BView* child)
{
return NULL;
}
/**
* Adds item to layout.
*/
bool
BALMLayout::AddItem(BLayoutItem* item)
{
return false;
}
/**
* Adds item to layout.
*/
bool
BALMLayout::AddItem(int32 index, BLayoutItem* item)
{
return false;
}
/**
* Removes view from layout.
*/
bool
BALMLayout::RemoveView(BView* child)
{
return false;
}
/**
* Removes item from layout.
*/
bool
BALMLayout::RemoveItem(BLayoutItem* item)
{
return false;
}
/**
* Removes item from layout.
*/
BLayoutItem*
BALMLayout::RemoveItem(int32 index)
{
return NULL;
}
/**
* Gets minimum size.
*/
@ -485,23 +408,6 @@ BALMLayout::BaseAlignment()
}
/**
* Gets whether the height of the layout depends on its width.
*/
bool
BALMLayout::HasHeightForWidth()
{
return false;
}
/**
* Gets height constraints for a given width.
*/
void
BALMLayout::GetHeightForWidth(float width, float* min, float* max, float* preferred) {}
/**
* Invalidates the layout.
* Resets minimum/maximum/preferred size.
@ -516,6 +422,24 @@ BALMLayout::InvalidateLayout(bool children)
}
bool
BALMLayout::ItemAdded(BLayoutItem* item, int32 atIndex)
{
item->SetLayoutData(new(std::nothrow) Area(this, item));
return item->LayoutData() != NULL;
}
void
BALMLayout::ItemRemoved(BLayoutItem* item, int32 fromIndex)
{
if (Area* area = _AreaForItem(item)) {
item->SetLayoutData(NULL);
delete area;
}
}
/**
* Calculate and set the layout.
* If no layout specification is given, a specification is reverse engineered automatically.
@ -533,6 +457,9 @@ BALMLayout::DerivedLayoutItems()
if (Owner() == NULL)
return;
//TODO
// _UpdateConstraints();
// reverse engineer a layout specification if none was given
//~ if (this == NULL) RecoverLayout(View());
@ -568,8 +495,8 @@ BALMLayout::DerivedLayoutItems()
}
// set the calculated positions and sizes for every area
for (int32 i = 0; i < Areas()->CountItems(); i++)
((Area*)Areas()->ItemAt(i))->DoLayout();
for (int32 i = 0; i < CountItems(); i++)
_AreaForItem(ItemAt(i))->DoLayout();
fActivated = true;
}
@ -685,3 +612,11 @@ BALMLayout::CalculatePreferredSize()
Bottom()->Value() - Top()->Value());
}
Area*
BALMLayout::_AreaForItem(BLayoutItem* item) const
{
if (!item)
return NULL;
return static_cast<Area*>(item->LayoutData());
}

View File

@ -4,6 +4,8 @@ SetSubDirSupportedPlatformsBeOSCompatible ;
UseLibraryHeaders lp_solve linprog alm ;
UsePrivateHeaders shared ;
UsePrivateHeaders [ FDirName $(SUBDIR) $(DOTDOT) $(DOTDOT) kits interface ] ;
SharedLibrary libalm.so :
Area.cpp