From 0da6f2e916c5912ab5b2bd2d8aa271cf31a3300a Mon Sep 17 00:00:00 2001 From: Alex Wilson Date: Tue, 17 Jan 2012 15:50:57 +1300 Subject: [PATCH] Rename BALM::GroupItem to BALM::ALMGroup, and move it to its own files. Also make ALMGroup totally self-contained, i.e. BALMLayout no longer does the parsing, and is completely unaware of ALMGroup. A small touch of refactoring as well. --- headers/libs/alm/ALMGroup.h | 71 +++++++++++++ headers/libs/alm/ALMLayout.h | 11 +- headers/libs/alm/Area.h | 32 +----- src/libs/alm/ALMGroup.cpp | 143 ++++++++++++++++++++++++++ src/libs/alm/ALMLayout.cpp | 48 +-------- src/libs/alm/Area.cpp | 88 ---------------- src/libs/alm/Jamfile | 1 + src/tests/libs/alm/OperatorLayout.cpp | 10 +- 8 files changed, 223 insertions(+), 181 deletions(-) create mode 100644 headers/libs/alm/ALMGroup.h create mode 100644 src/libs/alm/ALMGroup.cpp diff --git a/headers/libs/alm/ALMGroup.h b/headers/libs/alm/ALMGroup.h new file mode 100644 index 0000000000..8886141a33 --- /dev/null +++ b/headers/libs/alm/ALMGroup.h @@ -0,0 +1,71 @@ +/* + * Copyright 2012, Haiku, Inc. All rights reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef ALMGROUP_H +#define ALMGROUP_H + + +#include + +#include // for enum orientation +#include + +#include "Tab.h" + + +class BLayoutItem; +class BView; + + +namespace BALM { + +class BALMLayout; + + +class ALMGroup { +public: + ALMGroup(BLayoutItem* item); + ALMGroup(BView* view); + + BLayoutItem* LayoutItem() const; + BView* View() const; + + const std::vector& Groups() const; + enum orientation Orientation() const; + + ALMGroup& operator|(const ALMGroup& right); + ALMGroup& operator/(const ALMGroup& bottom); + + void BuildLayout(BALMLayout* layout, + XTab* left = NULL, YTab* top = NULL, + XTab* right = NULL, YTab* bottom = NULL); + +private: + ALMGroup(); + + void _Init(BLayoutItem* item, BView* view, + enum orientation orien = B_HORIZONTAL); + ALMGroup& _AddItem(const ALMGroup& item, + enum orientation orien); + + void _Build(BALMLayout* layout, + BReference left, BReference top, + BReference right, + BReference bottom) const; + + + BLayoutItem* fLayoutItem; + BView* fView; + + std::vector fGroups; + enum orientation fOrientation; +}; + + +}; + + +using BALM::ALMGroup; + +#endif diff --git a/headers/libs/alm/ALMLayout.h b/headers/libs/alm/ALMLayout.h index 728be32c40..b16bc46dc5 100644 --- a/headers/libs/alm/ALMLayout.h +++ b/headers/libs/alm/ALMLayout.h @@ -22,7 +22,7 @@ namespace BALM { class Column; -class GroupItem; +class ALMGroup; class Row; class RowColumnManager; @@ -86,10 +86,6 @@ public: YTab* BottomOf(const BView* view) const; YTab* BottomOf(const BLayoutItem* item) const; - void BuildLayout(GroupItem& item, XTab* left = NULL, - YTab* top = NULL, XTab* right = NULL, - YTab* bottom = NULL); - virtual BLayoutItem* AddView(BView* child); virtual BLayoutItem* AddView(int32 index, BView* child); virtual Area* AddView(BView* view, XTab* left, YTab* top, @@ -135,11 +131,6 @@ private: BSize _CalculateMaxSize(); BSize _CalculatePreferredSize(); - void _ParseGroupItem(GroupItem& item, - BReference left, BReference top, - BReference right, - BReference bottom); - LinearProgramming::LinearSpec* fSolver; LinearProgramming::LinearSpec fOwnSolver; diff --git a/headers/libs/alm/Area.h b/headers/libs/alm/Area.h index 2932ba3dcc..71e409d449 100644 --- a/headers/libs/alm/Area.h +++ b/headers/libs/alm/Area.h @@ -6,11 +6,9 @@ #define AREA_H -#include - -#include // for enum orientation #include #include +#include #include #include @@ -34,33 +32,6 @@ class XTab; class YTab; -class GroupItem { -public: - GroupItem(BLayoutItem* item); - GroupItem(BView* view); - - BLayoutItem* LayoutItem(); - BView* View(); - - const std::vector& GroupItems(); - enum orientation Orientation(); - - GroupItem& operator|(const GroupItem& right); - GroupItem& operator/(const GroupItem& bottom); -private: - GroupItem(); - - void _Init(BLayoutItem* item, BView* view, - enum orientation orien = B_HORIZONTAL); - GroupItem& _AddItem(const GroupItem& item, - enum orientation orien); - - BLayoutItem* fLayoutItem; - BView* fView; - - std::vector fGroupItems; - enum orientation fOrientation; -}; class RowColumnManager; @@ -179,7 +150,6 @@ public: } // namespace BALM using BALM::Area; -using BALM::GroupItem; #endif // AREA_H diff --git a/src/libs/alm/ALMGroup.cpp b/src/libs/alm/ALMGroup.cpp new file mode 100644 index 0000000000..d0fd65beb1 --- /dev/null +++ b/src/libs/alm/ALMGroup.cpp @@ -0,0 +1,143 @@ +#include "ALMGroup.h" + + +#include +#include + + +ALMGroup::ALMGroup(BLayoutItem* item) +{ + _Init(item, NULL); +} + + +ALMGroup::ALMGroup(BView* view) +{ + _Init(NULL, view); +} + + +BLayoutItem* +ALMGroup::LayoutItem() const +{ + return fLayoutItem; +} + + +BView* +ALMGroup::View() const +{ + return fView; +} + + +const std::vector& +ALMGroup::Groups() const +{ + return fGroups; +} + + +enum orientation +ALMGroup::Orientation() const +{ + return fOrientation; +} + + +ALMGroup& +ALMGroup::operator|(const ALMGroup& right) +{ + return _AddItem(right, B_HORIZONTAL); +} + + +ALMGroup& +ALMGroup::operator/(const ALMGroup& bottom) +{ + return _AddItem(bottom, B_VERTICAL); +} + + +void +ALMGroup::BuildLayout(BALMLayout* layout, XTab* left, YTab* top, XTab* right, + YTab* bottom) +{ + if (left == NULL) + left = layout->Left(); + if (top == NULL) + top = layout->Top(); + if (right == NULL) + right = layout->Right(); + if (bottom == NULL) + bottom = layout->Bottom(); + + _Build(layout, left, top, right, bottom); +} + + +ALMGroup::ALMGroup() +{ + _Init(NULL, NULL); +} + + +void +ALMGroup::_Init(BLayoutItem* item, BView* view, enum orientation orien) +{ + fLayoutItem = item; + fView = view; + fOrientation = orien; +} + + +void +ALMGroup::_Build(BALMLayout* layout, BReference left, + BReference top, BReference right, BReference bottom) const +{ + if (LayoutItem()) + layout->AddItem(LayoutItem(), left, top, right, bottom); + else if (View()) { + layout->AddView(View(), left, top, right, bottom); + } else { + for (unsigned int i = 0; i < Groups().size(); i++) { + const ALMGroup& current = Groups()[i]; + if (Orientation() == B_HORIZONTAL) { + BReference currentRight; + if (i == Groups().size() - 1) + currentRight = right; + else + currentRight = layout->AddXTab(); + current._Build(layout, left, top, currentRight, bottom); + left = currentRight; + } else { + BReference currentBottom; + if (i == Groups().size() - 1) + currentBottom = bottom; + else + currentBottom = layout->AddYTab(); + current._Build(layout, left, top, right, currentBottom); + top = currentBottom; + } + } + } +} + + +ALMGroup& +ALMGroup::_AddItem(const ALMGroup& item, enum orientation orien) +{ + if (fGroups.size() == 0) + fGroups.push_back(*this); + else if (fOrientation != orien) { + ALMGroup clone = *this; + fGroups.clear(); + _Init(NULL, NULL, orien); + fGroups.push_back(clone); + } + + _Init(NULL, NULL, orien); + fGroups.push_back(item); + return *this; +} + diff --git a/src/libs/alm/ALMLayout.cpp b/src/libs/alm/ALMLayout.cpp index 49ff892f93..abde2f2ae2 100644 --- a/src/libs/alm/ALMLayout.cpp +++ b/src/libs/alm/ALMLayout.cpp @@ -11,6 +11,7 @@ #include +#include "ALMGroup.h" #include "RowColumnManager.h" #include "ViewLayoutItem.h" @@ -352,53 +353,6 @@ BALMLayout::BottomOf(const BLayoutItem* item) const } -void -BALMLayout::BuildLayout(GroupItem& item, XTab* left, YTab* top, XTab* right, - YTab* bottom) -{ - if (left == NULL) - left = Left(); - if (top == NULL) - top = Top(); - if (right == NULL) - right = Right(); - if (bottom == NULL) - bottom = Bottom(); - - _ParseGroupItem(item, left, top, right, bottom); -} - - -void -BALMLayout::_ParseGroupItem(GroupItem& item, BReference left, - BReference top, BReference right, BReference bottom) -{ - if (item.LayoutItem()) - AddItem(item.LayoutItem(), left, top, right, bottom); - else if (item.View()) { - AddView(item.View(), left, top, right, bottom); - } - else { - for (unsigned int i = 0; i < item.GroupItems().size(); i++) { - GroupItem& current = const_cast( - item.GroupItems()[i]); - if (item.Orientation() == B_HORIZONTAL) { - BReference r = (i == item.GroupItems().size() - 1) ? right - : AddXTab(); - _ParseGroupItem(current, left, top, r, bottom); - left = r; - } - else { - BReference b = (i == item.GroupItems().size() - 1) - ? bottom : AddYTab(); - _ParseGroupItem(current, left, top, right, b); - top = b; - } - } - } -} - - BLayoutItem* BALMLayout::AddView(BView* child) { diff --git a/src/libs/alm/Area.cpp b/src/libs/alm/Area.cpp index 8b3bf9fa43..d0d4e07a7f 100644 --- a/src/libs/alm/Area.cpp +++ b/src/libs/alm/Area.cpp @@ -17,94 +17,6 @@ using namespace LinearProgramming; -using namespace std; - - -GroupItem::GroupItem(BLayoutItem* item) -{ - _Init(item, NULL); -} - - -GroupItem::GroupItem(BView* view) -{ - _Init(NULL, view); -} - - -BLayoutItem* -GroupItem::LayoutItem() -{ - return fLayoutItem; -} - - -BView* -GroupItem::View() -{ - return fView; -} - - -const std::vector& -GroupItem::GroupItems() -{ - return fGroupItems; -} - - -enum orientation -GroupItem::Orientation() -{ - return fOrientation; -} - - -GroupItem& -GroupItem::operator|(const GroupItem& right) -{ - return _AddItem(right, B_HORIZONTAL); -} - - -GroupItem& -GroupItem::operator/(const GroupItem& bottom) -{ - return _AddItem(bottom, B_VERTICAL); -} - - -GroupItem::GroupItem() -{ - _Init(NULL, NULL); -} - - -void -GroupItem::_Init(BLayoutItem* item, BView* view, enum orientation orien) -{ - fLayoutItem = item; - fView = view; - fOrientation = orien; -} - - -GroupItem& -GroupItem::_AddItem(const GroupItem& item, enum orientation orien) -{ - if (fGroupItems.size() == 0) - fGroupItems.push_back(*this); - else if (fOrientation != orien) { - GroupItem clone = *this; - fGroupItems.clear(); - _Init(NULL, NULL, orien); - fGroupItems.push_back(clone); - } - - _Init(NULL, NULL, orien); - fGroupItems.push_back(item); - return *this; -} BLayoutItem* diff --git a/src/libs/alm/Jamfile b/src/libs/alm/Jamfile index 32179c2651..b10209ad3e 100644 --- a/src/libs/alm/Jamfile +++ b/src/libs/alm/Jamfile @@ -7,6 +7,7 @@ UseHeaders [ FDirName $(SUBDIR) $(DOTDOT) $(DOTDOT) kits interface ] ; SharedLibrary libalm.so : + ALMGroup.cpp ALMLayout.cpp ALMLayoutBuilder.cpp Area.cpp diff --git a/src/tests/libs/alm/OperatorLayout.cpp b/src/tests/libs/alm/OperatorLayout.cpp index a31adff0e8..b556357708 100644 --- a/src/tests/libs/alm/OperatorLayout.cpp +++ b/src/tests/libs/alm/OperatorLayout.cpp @@ -11,6 +11,7 @@ #include #include "ALMLayout.h" +#include "ALMGroup.h" class OperatorWindow : public BWindow { @@ -37,11 +38,10 @@ public: SetLayout(layout); layout->SetInsets(spacing); - GroupItem item = GroupItem(button1) | (GroupItem(button2) - / (GroupItem(button3) | GroupItem(BSpaceLayoutItem::CreateGlue()) - | GroupItem(button4)) - / GroupItem(button5)); - layout->BuildLayout(item); + (ALMGroup(button1) | (ALMGroup(button2) + / (ALMGroup(button3) | ALMGroup(BSpaceLayoutItem::CreateGlue()) + | ALMGroup(button4)) + / ALMGroup(button5))).BuildLayout(layout); // test size limits BSize min = layout->MinSize();