xref: /haiku/src/libs/alm/ALMGroup.cpp (revision 25a7b01d15612846f332751841da3579db313082)
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 
ALMGroup(BLayoutItem * item)12 ALMGroup::ALMGroup(BLayoutItem* item)
13 {
14 	_Init(item, NULL);
15 }
16 
17 
ALMGroup(BView * view)18 ALMGroup::ALMGroup(BView* view)
19 {
20 	_Init(NULL, view);
21 }
22 
23 
24 BLayoutItem*
LayoutItem() const25 ALMGroup::LayoutItem() const
26 {
27 	return fLayoutItem;
28 }
29 
30 
31 BView*
View() const32 ALMGroup::View() const
33 {
34 	return fView;
35 }
36 
37 
38 const std::vector<ALMGroup>&
Groups() const39 ALMGroup::Groups() const
40 {
41 	return fGroups;
42 }
43 
44 
45 enum orientation
Orientation() const46 ALMGroup::Orientation() const
47 {
48 	return fOrientation;
49 }
50 
51 
52 ALMGroup&
operator |(const ALMGroup & right)53 ALMGroup::operator|(const ALMGroup& right)
54 {
55 	return _AddItem(right, B_HORIZONTAL);
56 }
57 
58 
59 ALMGroup&
operator /(const ALMGroup & bottom)60 ALMGroup::operator/(const ALMGroup& bottom)
61 {
62 	return _AddItem(bottom, B_VERTICAL);
63 }
64 
65 
66 void
BuildLayout(BALMLayout * layout,XTab * left,YTab * top,XTab * right,YTab * bottom)67 ALMGroup::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 
ALMGroup()83 ALMGroup::ALMGroup()
84 {
85 	_Init(NULL, NULL);
86 }
87 
88 
89 void
_Init(BLayoutItem * item,BView * view,enum orientation orien)90 ALMGroup::_Init(BLayoutItem* item, BView* view, enum orientation orien)
91 {
92 	fLayoutItem = item;
93 	fView = view;
94 	fOrientation = orien;
95 }
96 
97 
98 void
_Build(BALMLayout * layout,BReference<XTab> left,BReference<YTab> top,BReference<XTab> right,BReference<YTab> bottom) const99 ALMGroup::_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 
131 ALMGroup&
_AddItem(const ALMGroup & item,enum orientation orien)132 ALMGroup::_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