1/*
2 * Copyright 2010, Clemens Zeidler <haiku@clemens-zeidler.de>
3 * Distributed under the terms of the MIT License.
4 */
5#include "ALMGroup.h"
6
7
8#include <ALMLayout.h>
9#include <Tab.h>
10
11
12ALMGroup::ALMGroup(BLayoutItem* item)
13{
14	_Init(item, NULL);
15}
16
17
18ALMGroup::ALMGroup(BView* view)
19{
20	_Init(NULL, view);
21}
22
23
24BLayoutItem*
25ALMGroup::LayoutItem() const
26{
27	return fLayoutItem;
28}
29
30
31BView*
32ALMGroup::View() const
33{
34	return fView;
35}
36
37
38const std::vector<ALMGroup>&
39ALMGroup::Groups() const
40{
41	return fGroups;
42}
43
44
45enum orientation
46ALMGroup::Orientation() const
47{
48	return fOrientation;
49}
50
51
52ALMGroup&
53ALMGroup::operator|(const ALMGroup& right)
54{
55	return _AddItem(right, B_HORIZONTAL);
56}
57
58
59ALMGroup&
60ALMGroup::operator/(const ALMGroup& bottom)
61{
62	return _AddItem(bottom, B_VERTICAL);
63}
64
65
66void
67ALMGroup::BuildLayout(BALMLayout* layout, XTab* left, YTab* top, XTab* right,
68	YTab* bottom)
69{
70	if (left == NULL)
71		left = layout->Left();
72	if (top == NULL)
73		top = layout->Top();
74	if (right == NULL)
75		right = layout->Right();
76	if (bottom == NULL)
77		bottom = layout->Bottom();
78
79	_Build(layout, left, top, right, bottom);
80}
81
82
83ALMGroup::ALMGroup()
84{
85	_Init(NULL, NULL);
86}
87
88
89void
90ALMGroup::_Init(BLayoutItem* item, BView* view, enum orientation orien)
91{
92	fLayoutItem = item;
93	fView = view;
94	fOrientation = orien;
95}
96
97
98void
99ALMGroup::_Build(BALMLayout* layout, BReference<XTab> left,
100	BReference<YTab> top, BReference<XTab> right, BReference<YTab> bottom) const
101{
102	if (LayoutItem())
103		layout->AddItem(LayoutItem(), left, top, right, bottom);
104	else if (View()) {
105		layout->AddView(View(), left, top, right, bottom);
106	} else {
107		for (unsigned int i = 0; i < Groups().size(); i++) {
108			const ALMGroup& current = Groups()[i];
109			if (Orientation() == B_HORIZONTAL) {
110				BReference<XTab> currentRight;
111				if (i == Groups().size() - 1)
112					currentRight = right;
113				else
114					currentRight = layout->AddXTab();
115				current._Build(layout, left, top, currentRight, bottom);
116				left = currentRight;
117			} else {
118				BReference<YTab> currentBottom;
119				if (i == Groups().size() - 1)
120					currentBottom = bottom;
121				else
122					currentBottom = layout->AddYTab();
123				current._Build(layout, left, top, right, currentBottom);
124				top = currentBottom;
125			}
126		}
127	}
128}
129
130
131ALMGroup&
132ALMGroup::_AddItem(const ALMGroup& item, enum orientation orien)
133{
134	if (fGroups.size() == 0)
135		fGroups.push_back(*this);
136	else if (fOrientation != orien) {
137		ALMGroup clone = *this;
138		fGroups.clear();
139		_Init(NULL, NULL, orien);
140		fGroups.push_back(clone);
141	}
142
143	_Init(NULL, NULL, orien);
144	fGroups.push_back(item);
145	return *this;
146}
147
148