2 * Copyright 2011, Haiku, Inc. All rights reserved.
3 * Copyright 2011, Clemens Zeidler <haiku@clemens-zeidler.de>
4 * Distributed under the terms of the MIT License.
8 #include "RowColumnManager.h"
11 #include <LayoutItem.h>
14 using namespace LinearProgramming
;
20 RowColumnManager::RowColumnManager(LinearSpec
* spec
)
28 RowColumnManager::~RowColumnManager()
30 for (int32 i
= 0; i
< fRows
.CountItems(); i
++)
31 delete fRows
.ItemAt(i
)->fPrefSizeConstraint
;
33 for (int32 i
= 0; i
< fColumns
.CountItems(); i
++)
34 delete fColumns
.ItemAt(i
)->fPrefSizeConstraint
;
39 RowColumnManager::AddArea(Area
* area
)
41 Row
* row
= _FindRowFor(area
);
43 row
= new Row(fLinearSpec
, area
->Top(), area
->Bottom());
47 row
->fAreas
.AddItem(area
);
49 Column
* column
= _FindColumnFor(area
);
51 column
= new Column(fLinearSpec
, area
->Left(), area
->Right());
52 fColumns
.AddItem(column
);
54 area
->fColumn
= column
;
55 column
->fAreas
.AddItem(area
);
57 _UpdateConstraints(row
);
58 _UpdateConstraints(column
);
63 RowColumnManager::RemoveArea(Area
* area
)
65 Row
* row
= area
->fRow
;
67 row
->fAreas
.RemoveItem(area
);
69 if (row
->fAreas
.CountItems() == 0) {
70 fRows
.RemoveItem(row
);
73 _UpdateConstraints(row
);
76 Column
* column
= area
->fColumn
;
78 column
->fAreas
.RemoveItem(area
);
80 if (column
->fAreas
.CountItems() == 0) {
81 fColumns
.RemoveItem(column
);
84 _UpdateConstraints(column
);
90 RowColumnManager::UpdateConstraints()
92 for (int32 i
= 0; i
< fRows
.CountItems(); i
++)
93 _UpdateConstraints(fRows
.ItemAt(i
));
94 for (int32 i
= 0; i
< fColumns
.CountItems(); i
++)
95 _UpdateConstraints(fColumns
.ItemAt(i
));
100 RowColumnManager::TabsChanged(Area
* area
)
108 RowColumnManager::_FindRowFor(Area
* area
)
110 for (int32 i
= 0; i
< fRows
.CountItems(); i
++) {
111 Row
* row
= fRows
.ItemAt(i
);
112 if (row
->fTop
.Get() == area
->Top()
113 && row
->fBottom
.Get() == area
->Bottom())
121 RowColumnManager::_FindColumnFor(Area
* area
)
123 for (int32 i
= 0; i
< fColumns
.CountItems(); i
++) {
124 Column
* column
= fColumns
.ItemAt(i
);
125 if (column
->fLeft
.Get() == area
->Left()
126 && column
->fRight
.Get() == area
->Right())
134 RowColumnManager::_PreferredHeight(Row
* row
, double& weight
)
139 for (int32 i
= 0; i
< row
->fAreas
.CountItems(); i
++) {
140 BSize prefSize
= row
->fAreas
.ItemAt(i
)->Item()->PreferredSize();
141 if (prefSize
.height
<= 0)
144 pref
+= prefSize
.height
;
145 double negPen
= row
->fAreas
.ItemAt(i
)->ShrinkPenalties().height
;
161 RowColumnManager::_PreferredWidth(Column
* column
, double& weight
)
166 for (int32 i
= 0; i
< column
->fAreas
.CountItems(); i
++) {
167 BSize prefSize
= column
->fAreas
.ItemAt(i
)->Item()->PreferredSize();
168 if (prefSize
.width
<= 0)
171 pref
+= prefSize
.width
;
173 double negPen
= column
->fAreas
.ItemAt(i
)->ShrinkPenalties().height
;
189 RowColumnManager::_UpdateConstraints(Row
* row
)
192 double prefSize
= _PreferredHeight(row
, weight
);
194 if (row
->fPrefSizeConstraint
== NULL
) {
195 row
->fPrefSizeConstraint
= fLinearSpec
->AddConstraint(1,
196 row
->fBottom
, -1, row
->fTop
, kEQ
, prefSize
, weight
, weight
);
197 row
->fPrefSizeConstraint
->SetLabel("Pref Height");
199 row
->fPrefSizeConstraint
->SetRightSide(prefSize
);
200 row
->fPrefSizeConstraint
->SetPenaltyNeg(weight
);
201 row
->fPrefSizeConstraint
->SetPenaltyPos(weight
);
204 delete row
->fPrefSizeConstraint
;
205 row
->fPrefSizeConstraint
= NULL
;
211 RowColumnManager::_UpdateConstraints(Column
* column
)
214 double prefSize
= _PreferredWidth(column
, weight
);
216 if (column
->fPrefSizeConstraint
== NULL
) {
217 column
->fPrefSizeConstraint
= fLinearSpec
->AddConstraint(1,
218 column
->fRight
, -1, column
->fLeft
, kEQ
, prefSize
, weight
,
220 column
->fPrefSizeConstraint
->SetLabel("Pref Width");
222 column
->fPrefSizeConstraint
->SetRightSide(prefSize
);
223 column
->fPrefSizeConstraint
->SetPenaltyNeg(weight
);
224 column
->fPrefSizeConstraint
->SetPenaltyPos(weight
);
227 delete column
->fPrefSizeConstraint
;
228 column
->fPrefSizeConstraint
= NULL
;
233 } // end BALM namespace