From 5b6fca06392320a9c32d542b519d8f4fe876aef2 Mon Sep 17 00:00:00 2001 From: Clemens Zeidler Date: Wed, 29 Sep 2010 03:30:47 +0000 Subject: [PATCH] - Rework the ALM layout api. The AddItemTo* function are now related to the current Area. On default the current Area is the last added Area. - Add some more const where appreciated. - Fix some style issues and a leak in Row and Column. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@38842 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/libs/alm/ALMLayout.h | 51 ++++++------ headers/libs/alm/Column.h | 21 +++-- headers/libs/alm/Row.h | 19 ++--- src/libs/alm/ALMLayout.cpp | 156 ++++++++++++++++++++--------------- src/libs/alm/Area.cpp | 4 +- src/libs/alm/Column.cpp | 29 ++----- src/libs/alm/Row.cpp | 26 ++---- 7 files changed, 148 insertions(+), 158 deletions(-) diff --git a/headers/libs/alm/ALMLayout.h b/headers/libs/alm/ALMLayout.h index a7235cfc4e..140e67de64 100644 --- a/headers/libs/alm/ALMLayout.h +++ b/headers/libs/alm/ALMLayout.h @@ -46,29 +46,33 @@ public: char* PerformancePath() const; void SetPerformancePath(char* path); - LinearSpec* Solver(); + LinearSpec* Solver() const; void SetInset(float inset); - float Inset(); + float Inset() const; void SetSpacing(float spacing); - float Spacing(); + float Spacing() const; + Area* AreaFor(const BView* control) const; + Area* AreaFor(const BLayoutItem* item) const; + Area* CurrentArea() const; + void SetCurrentArea(const Area* area); + void SetCurrentArea(const BView* control); + void SetCurrentArea(const BLayoutItem* item); + virtual BLayoutItem* AddView(BView* child); virtual BLayoutItem* AddView(int32 index, BView* child); virtual Area* AddView(BView* view, XTab* left, YTab* top, XTab* right, YTab* bottom); virtual Area* AddView(BView* view, Row* row, Column* column); - virtual Area* AddViewToRight(BView* view, Area* leftArea, - XTab* right = NULL, YTab* top = NULL, - YTab* bottom = NULL); - virtual Area* AddViewToLeft(BView* view, Area* rightArea, - XTab* left = NULL, YTab* top = NULL, - YTab* bottom = NULL); - virtual Area* AddViewToTop(BView* view, Area* bottomArea, - YTab* top = NULL, XTab* left = NULL, - XTab* right = NULL); - virtual Area* AddViewToBottom(BView* view, Area* topArea, + virtual Area* AddViewToRight(BView* view, XTab* right = NULL, + YTab* top = NULL, YTab* bottom = NULL); + virtual Area* AddViewToLeft(BView* view, XTab* left = NULL, + YTab* top = NULL, YTab* bottom = NULL); + virtual Area* AddViewToTop(BView* view, YTab* top = NULL, + XTab* left = NULL, XTab* right = NULL); + virtual Area* AddViewToBottom(BView* view, YTab* bottom = NULL, XTab* left = NULL, XTab* right = NULL); @@ -79,19 +83,17 @@ public: virtual Area* AddItem(BLayoutItem* item, Row* row, Column* column); virtual Area* AddItemToRight(BLayoutItem* item, - Area* leftArea, XTab* right = NULL, - YTab* top = NULL, YTab* bottom = NULL); + XTab* right = NULL, YTab* top = NULL, + YTab* bottom = NULL); virtual Area* AddItemToLeft(BLayoutItem* item, - Area* rightArea, XTab* left = NULL, - YTab* top = NULL, YTab* bottom = NULL); + XTab* left = NULL, YTab* top = NULL, + YTab* bottom = NULL); virtual Area* AddItemToTop(BLayoutItem* item, - Area* bottomArea, YTab* top = NULL, - XTab* left = NULL, XTab* right = NULL); + YTab* top = NULL, XTab* left = NULL, + XTab* right = NULL); virtual Area* AddItemToBottom(BLayoutItem* item, - Area* topArea, YTab* bottom = NULL, - XTab* left = NULL, XTab* right = NULL); - - virtual Area* AreaOf(BView* control); + YTab* bottom = NULL, XTab* left = NULL, + XTab* right = NULL); virtual BSize BaseMinSize(); virtual BSize BaseMaxSize(); @@ -110,7 +112,6 @@ private: void _SolveLayout(); - Area* _AreaForItem(BLayoutItem* item) const; void _UpdateAreaConstraints(); BSize _CalculateMinSize(); @@ -131,6 +132,8 @@ private: float fInset; float fSpacing; + + Area* fCurrentArea; }; } // namespace BALM diff --git a/headers/libs/alm/Column.h b/headers/libs/alm/Column.h index c561eb54fd..e5f3290040 100644 --- a/headers/libs/alm/Column.h +++ b/headers/libs/alm/Column.h @@ -6,8 +6,6 @@ #define COLUMN_H -#include - #include "Constraint.h" #include "LinearSpec.h" @@ -22,19 +20,20 @@ class XTab; */ class Column { public: + ~Column(); + XTab* Left() const; XTab* Right() const; Column* Previous() const; - void SetPrevious(Column* value); + void SetPrevious(Column* value); Column* Next() const; - void SetNext(Column* value); - //~ string ToString(); - void InsertBefore(Column* column); - void InsertAfter(Column* column); + void SetNext(Column* value); + + void InsertBefore(Column* column); + void InsertAfter(Column* column); Constraint* HasSameWidthAs(Column* column); - BList* Constraints() const; - void SetConstraints(BList* constraints); - ~Column(); + + ConstraintList* Constraints() const; protected: Column(LinearSpec* ls); @@ -49,7 +48,7 @@ private: Column* fNext; Constraint* fPreviousGlue; Constraint* fNextGlue; - BList* fConstraints; + ConstraintList fConstraints; public: friend class BALMLayout; diff --git a/headers/libs/alm/Row.h b/headers/libs/alm/Row.h index 9195743043..44a004bae8 100644 --- a/headers/libs/alm/Row.h +++ b/headers/libs/alm/Row.h @@ -6,8 +6,6 @@ #define ROW_H -#include - #include "Constraint.h" #include "LinearSpec.h" @@ -22,19 +20,18 @@ class YTab; */ class Row { public: + ~Row(); + YTab* Top() const; YTab* Bottom() const; Row* Previous() const; - void SetPrevious(Row* value); + void SetPrevious(Row* value); Row* Next() const; - void SetNext(Row* value); - //~ string ToString(); - void InsertBefore(Row* row); - void InsertAfter(Row* row); + void SetNext(Row* value); + void InsertBefore(Row* row); + void InsertAfter(Row* row); Constraint* HasSameHeightAs(Row* row); - BList* Constraints() const; - void SetConstraints(BList* constraints); - ~Row(); + ConstraintList* Constraints() const; protected: Row(LinearSpec* ls); @@ -49,7 +46,7 @@ private: Row* fNext; Constraint* fPreviousGlue; Constraint* fNextGlue; - BList* fConstraints; + ConstraintList fConstraints; public: friend class BALMLayout; diff --git a/src/libs/alm/ALMLayout.cpp b/src/libs/alm/ALMLayout.cpp index cb8adecc78..14186ae66e 100644 --- a/src/libs/alm/ALMLayout.cpp +++ b/src/libs/alm/ALMLayout.cpp @@ -33,7 +33,8 @@ const BSize kMaxSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED); BALMLayout::BALMLayout(float spacing) : fInset(0.0f), - fSpacing(spacing) + fSpacing(spacing), + fCurrentArea(NULL) { fLeft = new XTab(&fSolver); fRight = new XTab(&fSolver); @@ -146,6 +147,56 @@ BALMLayout::AddColumn(XTab* left, XTab* right) } +/** + * Finds the area that contains the given control. + * + * @param control the control to look for + * @return the area that contains the control + */ +Area* +BALMLayout::AreaFor(const BView* control) const +{ + return AreaFor(ItemAt(IndexOfView(const_cast(control)))); +} + + +Area* +BALMLayout::AreaFor(const BLayoutItem* item) const +{ + if (!item) + return NULL; + return static_cast(item->LayoutData()); +} + + +Area* +BALMLayout::CurrentArea() const +{ + return fCurrentArea; +} + + +void +BALMLayout::SetCurrentArea(const Area* area) +{ + fCurrentArea = const_cast(area); +} + + +void +BALMLayout::SetCurrentArea(const BView* view) +{ + fCurrentArea = AreaFor(view); +} + + +void +BALMLayout::SetCurrentArea(const BLayoutItem* item) +{ + fCurrentArea = AreaFor(item); +} + + BLayoutItem* BALMLayout::AddView(BView* child) { @@ -206,11 +257,10 @@ BALMLayout::AddView(BView* view, Row* row, Column* column) Area* -BALMLayout::AddViewToRight(BView* view, Area* leftArea, XTab* right, YTab* top, - YTab* bottom) +BALMLayout::AddViewToRight(BView* view, XTab* right, YTab* top, YTab* bottom) { BLayoutItem* item = _CreateLayoutItem(view); - Area* area = AddItemToRight(item, leftArea, right, top, bottom); + Area* area = AddItemToRight(item, right, top, bottom); if (!area) { delete item; return NULL; @@ -220,11 +270,10 @@ BALMLayout::AddViewToRight(BView* view, Area* leftArea, XTab* right, YTab* top, Area* -BALMLayout::AddViewToLeft(BView* view, Area* rightArea, XTab* left, YTab* top, - YTab* bottom) +BALMLayout::AddViewToLeft(BView* view, XTab* left, YTab* top, YTab* bottom) { BLayoutItem* item = _CreateLayoutItem(view); - Area* area = AddItemToLeft(item, rightArea, left, top, bottom); + Area* area = AddItemToLeft(item, left, top, bottom); if (!area) { delete item; return NULL; @@ -234,11 +283,10 @@ BALMLayout::AddViewToLeft(BView* view, Area* rightArea, XTab* left, YTab* top, Area* -BALMLayout::AddViewToTop(BView* view, Area* bottomArea, YTab* top, XTab* left, - XTab* right) +BALMLayout::AddViewToTop(BView* view, YTab* top, XTab* left, XTab* right) { BLayoutItem* item = _CreateLayoutItem(view); - Area* area = AddItemToTop(item, bottomArea, top, left, right); + Area* area = AddItemToTop(item, top, left, right); if (!area) { delete item; return NULL; @@ -248,11 +296,10 @@ BALMLayout::AddViewToTop(BView* view, Area* bottomArea, YTab* top, XTab* left, Area* -BALMLayout::AddViewToBottom(BView* view, Area* topArea, YTab* bottom, - XTab* left, XTab* right) +BALMLayout::AddViewToBottom(BView* view, YTab* bottom, XTab* left, XTab* right) { BLayoutItem* item = _CreateLayoutItem(view); - Area* area = AddItemToBottom(item, topArea, bottom, left, right); + Area* area = AddItemToBottom(item, bottom, left, right); if (!area) { delete item; return NULL; @@ -288,7 +335,7 @@ BALMLayout::AddItem(int32 index, BLayoutItem* item) // for index = 0 we already have set the right tabs if (index != 0) { BLayoutItem* prevItem = ItemAt(index - 1); - Area* area = _AreaForItem(prevItem); + Area* area = AreaFor(prevItem); if (area) { left = area->Right(); top = area->Top(); @@ -305,9 +352,10 @@ BALMLayout::AddItem(BLayoutItem* item, XTab* left, YTab* top, XTab* right, { if (!BAbstractLayout::AddItem(-1, item)) return NULL; - Area* area = _AreaForItem(item); + Area* area = AreaFor(item); if (!area) return NULL; + fCurrentArea = area; area->_Init(&fSolver, left, top, right, bottom); return area; @@ -319,9 +367,10 @@ BALMLayout::AddItem(BLayoutItem* item, Row* row, Column* column) { if (!BAbstractLayout::AddItem(-1, item)) return NULL; - Area* area = _AreaForItem(item); + Area* area = AreaFor(item); if (!area) return NULL; + fCurrentArea = area; area->_Init(&fSolver, row, column); return area; @@ -329,62 +378,61 @@ BALMLayout::AddItem(BLayoutItem* item, Row* row, Column* column) Area* -BALMLayout::AddItemToRight(BLayoutItem* item, Area* leftArea, XTab* right, - YTab* top, YTab* bottom) +BALMLayout::AddItemToRight(BLayoutItem* item, XTab* right, YTab* top, + YTab* bottom) { - XTab* left = leftArea->Right(); + XTab* left = fCurrentArea->Right(); if (!right) right = AddXTab(); if (!top) - top = leftArea->Top(); + top = fCurrentArea->Top(); if (!bottom) - bottom = leftArea->Bottom(); + bottom = fCurrentArea->Bottom(); return AddItem(item, left, top, right, bottom); } Area* -BALMLayout::AddItemToLeft(BLayoutItem* item, Area* rightArea, XTab* left, - YTab* top, YTab* bottom) +BALMLayout::AddItemToLeft(BLayoutItem* item, XTab* left, YTab* top, + YTab* bottom) { if (!left) left = AddXTab(); - XTab* right = rightArea->Left(); + XTab* right = fCurrentArea->Left(); if (!top) - top = rightArea->Top(); + top = fCurrentArea->Top(); if (!bottom) - bottom = rightArea->Bottom(); + bottom = fCurrentArea->Bottom(); return AddItem(item, left, top, right, bottom); } Area* -BALMLayout::AddItemToTop(BLayoutItem* item, Area* bottomArea, YTab* top, - XTab* left, XTab* right) +BALMLayout::AddItemToTop(BLayoutItem* item, YTab* top, XTab* left, XTab* right) { if (!left) - left = bottomArea->Left(); + left = fCurrentArea->Left(); if (!right) - right = bottomArea->Right(); + right = fCurrentArea->Right(); if (!top) top = AddYTab(); - YTab* bottom = bottomArea->Top(); + YTab* bottom = fCurrentArea->Top(); return AddItem(item, left, top, right, bottom); } Area* -BALMLayout::AddItemToBottom(BLayoutItem* item, Area* topArea, YTab* bottom, - XTab* left, XTab* right) +BALMLayout::AddItemToBottom(BLayoutItem* item, YTab* bottom, XTab* left, + XTab* right) { if (!left) - left = topArea->Left(); + left = fCurrentArea->Left(); if (!right) - right = topArea->Right(); - YTab* top = topArea->Bottom(); + right = fCurrentArea->Right(); + YTab* top = fCurrentArea->Bottom(); if (!bottom) bottom = AddYTab(); @@ -392,19 +440,6 @@ BALMLayout::AddItemToBottom(BLayoutItem* item, Area* topArea, YTab* bottom, } -/** - * Finds the area that contains the given control. - * - * @param control the control to look for - * @return the area that contains the control - */ -Area* -BALMLayout::AreaOf(BView* control) -{ - return _AreaForItem(ItemAt(IndexOfView(control))); -} - - /** * Gets the left variable. */ @@ -518,7 +553,7 @@ BALMLayout::ItemAdded(BLayoutItem* item, int32 atIndex) void BALMLayout::ItemRemoved(BLayoutItem* item, int32 fromIndex) { - if (Area* area = _AreaForItem(item)) { + if (Area* area = AreaFor(item)) { item->SetLayoutData(NULL); delete area; } @@ -554,7 +589,7 @@ BALMLayout::DerivedLayoutItems() // set the calculated positions and sizes for every area for (int32 i = 0; i < CountItems(); i++) - _AreaForItem(ItemAt(i))->_DoLayout(); + AreaFor(ItemAt(i))->_DoLayout(); } @@ -583,9 +618,9 @@ BALMLayout::SetPerformancePath(char* path) LinearSpec* -BALMLayout::Solver() +BALMLayout::Solver() const { - return &fSolver; + return const_cast(&fSolver); } @@ -597,7 +632,7 @@ BALMLayout::SetInset(float inset) float -BALMLayout::Inset() +BALMLayout::Inset() const { return fInset; } @@ -611,7 +646,7 @@ BALMLayout::SetSpacing(float spacing) float -BALMLayout::Spacing() +BALMLayout::Spacing() const { return fSpacing; } @@ -740,18 +775,9 @@ BALMLayout::_CalculatePreferredSize() } -Area* -BALMLayout::_AreaForItem(BLayoutItem* item) const -{ - if (!item) - return NULL; - return static_cast(item->LayoutData()); -} - - void BALMLayout::_UpdateAreaConstraints() { for (int i = 0; i < CountItems(); i++) - _AreaForItem(ItemAt(i))->InvalidateSizeConstraints(); + AreaFor(ItemAt(i))->InvalidateSizeConstraints(); } diff --git a/src/libs/alm/Area.cpp b/src/libs/alm/Area.cpp index 23cbe81b50..e0b68aaf2e 100644 --- a/src/libs/alm/Area.cpp +++ b/src/libs/alm/Area.cpp @@ -623,8 +623,8 @@ Area::_UpdateMaxSizeConstraint(BSize max) void Area::_UpdatePreferredConstraint(BSize preferred) { - float width = 64000; - float height = 64000; + float width = 32000; + float height = 32000; if (preferred.width > 0) width = preferred.Width() + LeftInset() + RightInset(); if (preferred.height > 0) diff --git a/src/libs/alm/Column.cpp b/src/libs/alm/Column.cpp index d9485c4e6c..54d366a9a9 100644 --- a/src/libs/alm/Column.cpp +++ b/src/libs/alm/Column.cpp @@ -120,11 +120,6 @@ Column::SetNext(Column* value) } -//~ string Column::ToString() { - //~ return "Column(" + fLeft.ToString() + ", " + fRight.ToString() + ")"; -//~ } - - /** * Inserts the given column directly to the left of this column. * @@ -163,28 +158,15 @@ Column::HasSameWidthAs(Column* column) Constraint* constraint = fLS->AddConstraint( -1.0, fLeft, 1.0, fRight, 1.0, column->fLeft, -1.0, column->fRight, OperatorType(EQ), 0.0); - fConstraints->AddItem(constraint); + fConstraints.AddItem(constraint); return constraint; } -/** - * Gets the constraints. - */ -BList* +ConstraintList* Column::Constraints() const { - return fConstraints; -} - - -/** - * Sets the constraints. - */ -void -Column::SetConstraints(BList* constraints) -{ - fConstraints = constraints; + return const_cast(&fConstraints); } @@ -196,8 +178,8 @@ Column::~Column() { if (fPrevious != NULL) fPrevious->SetNext(fNext); - for (int32 i = 0; i < fConstraints->CountItems(); i++) - delete (Constraint*)fConstraints->ItemAt(i); + for (int32 i = 0; i < fConstraints.CountItems(); i++) + delete fConstraints.ItemAt(i); delete fLeft; delete fRight; } @@ -211,6 +193,5 @@ Column::Column(LinearSpec* ls) fLS = ls; fLeft = new XTab(ls); fRight = new XTab(ls); - fConstraints = new BList(1); } diff --git a/src/libs/alm/Row.cpp b/src/libs/alm/Row.cpp index 107832d450..966e20f02a 100644 --- a/src/libs/alm/Row.cpp +++ b/src/libs/alm/Row.cpp @@ -122,11 +122,6 @@ Row::SetNext(Row* value) } -//~ string Row::ToString() { - //~ return "Row(" + fTop.ToString() + ", " + fBottom.ToString() + ")"; -//~ } - - /** * Inserts the given row directly above this row. * @@ -165,7 +160,7 @@ Row::HasSameHeightAs(Row* row) Constraint* constraint = fLS->AddConstraint( -1.0, fTop, 1.0, fBottom, 1.0, row->fTop, -1.0, row->fBottom, OperatorType(EQ), 0.0); - fConstraints->AddItem(constraint); + fConstraints.AddItem(constraint); return constraint; } @@ -173,20 +168,10 @@ Row::HasSameHeightAs(Row* row) /** * Gets the constraints. */ -BList* +ConstraintList* Row::Constraints() const { - return fConstraints; -} - - -/** - * Sets the constraints. - */ -void -Row::SetConstraints(BList* constraints) -{ - fConstraints = constraints; + return const_cast(&fConstraints); } @@ -198,8 +183,8 @@ Row::~Row() { if (fPrevious != NULL) fPrevious->SetNext(fNext); - for (int32 i = 0; i < fConstraints->CountItems(); i++) - delete (Constraint*)fConstraints->ItemAt(i); + for (int32 i = 0; i < fConstraints.CountItems(); i++) + delete (Constraint*)fConstraints.ItemAt(i); delete fTop; delete fBottom; } @@ -213,6 +198,5 @@ Row::Row(LinearSpec* ls) fLS = ls; fTop = new YTab(ls); fBottom = new YTab(ls); - fConstraints = new BList(1); }