Mana
Loading...
Searching...
No Matches
layout.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2007-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "gui/widgets/layout.h"
23
24#include <cassert>
25
27{
28 return ContainerPlacer(mContainer, &mCell->at(x, y));
29}
30
31LayoutCell &ContainerPlacer::operator()
32 (int x, int y, gcn::Widget *wg, int w, int h)
33{
34 mContainer->add(wg);
35 return mCell->place(wg, x, y, w, h);
36}
37
39{
40 if (mType == ARRAY)
41 delete mArray;
42}
43
45{
46 assert(mType != WIDGET);
47 if (mType == ARRAY)
48 return *mArray;
49 mArray = new LayoutArray;
50 mType = ARRAY;
51 mExtent[0] = 1;
52 mExtent[1] = 1;
53 mPadding = 0;
54 mAlign[0] = FILL;
55 mAlign[1] = FILL;
56 return *mArray;
57}
58
59void LayoutCell::reflow(int nx, int ny, int nw, int nh)
60{
61 assert(mType != NONE);
62 nx += mPadding;
63 ny += mPadding;
64 nw -= 2 * mPadding;
65 nh -= 2 * mPadding;
66 if (mType == ARRAY)
67 mArray->reflow(nx, ny, nw, nh);
68 else
69 mWidget->setDimension(gcn::Rectangle(nx, ny, nw, nh));
70}
71
73{
74 assert(mType == ARRAY);
75
76 for (auto &row : mArray->mCells)
77 {
78 for (auto cell : row)
79 {
80 if (cell && cell->mType == ARRAY)
81 cell->computeSizes();
82 }
83 }
84
85 mSize[0] = mArray->getSize(0);
86 mSize[1] = mArray->getSize(1);
87}
88
90{
91}
92
94{
95 for (auto &row : mCells)
96 {
97 for (auto &cell : row)
98 {
99 delete cell;
100 }
101 }
102}
103
104LayoutCell &LayoutArray::at(int x, int y, int w, int h)
105{
106 resizeGrid(x + w, y + h);
107 LayoutCell *&cell = mCells[y][x];
108 if (!cell)
109 {
110 cell = new LayoutCell;
111 }
112 return *cell;
113}
114
115void LayoutArray::resizeGrid(int w, int h)
116{
117 bool extW = w && w > (int)mSizes[0].size(),
118 extH = h && h > (int)mSizes[1].size();
119 if (!extW && !extH)
120 return;
121
122 if (extH)
123 {
124 mSizes[1].resize(h, Layout::AUTO_DEF);
125 mCells.resize(h);
126 if (!extW) w = mSizes[0].size();
127 }
128
129 if (extW)
130 {
131 mSizes[0].resize(w, Layout::AUTO_DEF);
132 }
133
134 for (auto &row : mCells)
135 {
136 row.resize(w, nullptr);
137 }
138}
139
140void LayoutArray::setColWidth(int n, int w)
141{
142 resizeGrid(n + 1, 0);
143 mSizes[0][n] = w;
144}
145
147{
148 resizeGrid(0, n + 1);
149 mSizes[1][n] = h;
150}
151
152void LayoutArray::matchColWidth(int n1, int n2)
153{
154 resizeGrid(std::max(n1, n2) + 1, 0);
155 std::vector< short > widths = getSizes(0, Layout::AUTO_DEF);
156 int s = std::max(widths[n1], widths[n2]);
157 mSizes[0][n1] = s;
158 mSizes[0][n2] = s;
159}
160
161void LayoutArray::extend(int x, int y, int w, int h)
162{
163 LayoutCell &cell = at(x, y, w, h);
164 cell.mExtent[0] = w;
165 cell.mExtent[1] = h;
166}
167
168LayoutCell &LayoutArray::place(gcn::Widget *widget, int x, int y, int w, int h)
169{
170 LayoutCell &cell = at(x, y, w, h);
171 assert(cell.mType == LayoutCell::NONE);
173 cell.mWidget = widget;
174 cell.mSize[0] = w == 1 ? widget->getWidth() : 0;
175 cell.mSize[1] = h == 1 ? widget->getHeight() : 0;
176 cell.mExtent[0] = w;
177 cell.mExtent[1] = h;
178 cell.mPadding = 0;
179 cell.mAlign[0] = LayoutCell::FILL;
180 cell.mAlign[1] = LayoutCell::FILL;
181 short &cs = mSizes[0][x];
182 short &rs = mSizes[1][y];
183 if (cs == Layout::AUTO_DEF && w == 1)
184 cs = 0;
185 if (rs == Layout::AUTO_DEF && h == 1)
186 rs = 0;
187 return cell;
188}
189
190void LayoutArray::align(int &pos, int &size, int dim,
191 LayoutCell const &cell, const short *sizes) const
192{
193 int size_max = sizes[0];
194 for (int i = 1; i < cell.mExtent[dim]; ++i)
195 size_max += sizes[i] + mSpacing;
196 size = std::min<int>(cell.mSize[dim], size_max);
197
198 switch (cell.mAlign[dim])
199 {
200 case LayoutCell::LEFT:
201 return;
203 pos += size_max - size;
204 return;
206 pos += (size_max - size) / 2;
207 return;
208 case LayoutCell::FILL:
209 size = size_max;
210 return;
211 }
212}
213
214std::vector< short > LayoutArray::getSizes(int dim, int upp) const
215{
216 int gridW = mSizes[0].size();
217 int gridH = mSizes[1].size();
218 std::vector< short > sizes = mSizes[dim];
219
220 // Compute minimum sizes.
221 for (int gridY = 0; gridY < gridH; ++gridY)
222 {
223 for (int gridX = 0; gridX < gridW; ++gridX)
224 {
225 LayoutCell const *cell = mCells[gridY][gridX];
226 if (!cell || cell->mType == LayoutCell::NONE)
227 continue;
228
229 if (cell->mExtent[dim] == 1)
230 {
231 int n = dim == 0 ? gridX : gridY;
232 int s = cell->mSize[dim] + cell->mPadding * 2;
233 if (s > sizes[n]) sizes[n] = s;
234 }
235 }
236 }
237
238 if (upp == Layout::AUTO_DEF) return sizes;
239
240 // Compute the FILL sizes.
241 int nb = sizes.size();
242 int nbFill = 0;
243 for (int i = 0; i < nb; ++i)
244 {
245 if (mSizes[dim][i] <= Layout::AUTO_DEF)
246 {
247 ++nbFill;
248 if (mSizes[dim][i] == Layout::AUTO_SET ||
249 sizes[i] <= Layout::AUTO_DEF)
250 {
251 sizes[i] = 0;
252 }
253 }
254 upp -= sizes[i] + mSpacing;
255 }
256 upp = upp + mSpacing;
257
258 if (nbFill == 0)
259 return sizes;
260
261 for (int i = 0; i < nb; ++i)
262 {
263 if (mSizes[dim][i] > Layout::AUTO_DEF)
264 continue;
265 int s = upp / nbFill;
266 sizes[i] += s;
267 upp -= s;
268 --nbFill;
269 }
270
271 return sizes;
272}
273
274int LayoutArray::getSize(int dim) const
275{
276 std::vector< short > sizes = getSizes(dim, Layout::AUTO_DEF);
277 int size = 0;
278 int nb = sizes.size();
279 for (int i = 0; i < nb; ++i)
280 {
281 if (sizes[i] > Layout::AUTO_DEF) size += sizes[i];
282 size += mSpacing;
283 }
284 return size - mSpacing;
285}
286
287void LayoutArray::reflow(int nx, int ny, int nw, int nh)
288{
289 int gridW = mSizes[0].size();
290 int gridH = mSizes[1].size();
291
292 std::vector< short > widths = getSizes(0, nw);
293 std::vector< short > heights = getSizes(1, nh);
294
295 int y = ny;
296 for (int gridY = 0; gridY < gridH; ++gridY)
297 {
298 int x = nx;
299 for (int gridX = 0; gridX < gridW; ++gridX)
300 {
301 LayoutCell *cell = mCells[gridY][gridX];
302 if (cell && cell->mType != LayoutCell::NONE)
303 {
304 int dx = x, dy = y, dw, dh;
305 align(dx, dw, 0, *cell, &widths[gridX]);
306 align(dy, dh, 1, *cell, &heights[gridY]);
307 cell->reflow(dx, dy, dw, dh);
308 }
309 x += widths[gridX] + mSpacing;
310 }
311 y += heights[gridY] + mSpacing;
312 }
313}
314
315Layout::Layout(): mComputed(false)
316{
317 getArray();
318 setPadding(6);
319}
320
321void Layout::reflow(int &nw, int &nh)
322{
323 if (!mComputed)
324 {
325 computeSizes();
326 mComputed = true;
327 }
328
329 nw = nw == 0 ? mSize[0] + 2 * mPadding : nw;
330 nh = nh == 0 ? mSize[1] + 2 * mPadding : nh;
331 LayoutCell::reflow(0, 0, nw, nh);
332}
This class is a helper for adding widgets to nested tables in a window.
Definition layout.h:34
LayoutCell * mCell
Definition layout.h:60
gcn::Container * mContainer
Definition layout.h:59
ContainerPlacer at(int x, int y)
Returns a placer for the same container but to an inner cell.
Definition layout.cpp:26
This class contains a rectangular array of cells.
Definition layout.h:67
std::vector< std::vector< LayoutCell * > > mCells
Definition layout.h:146
void align(int &pos, int &size, int dim, LayoutCell const &cell, const short *sizes) const
Gets the position and size of a widget along a given axis.
Definition layout.cpp:190
std::vector< short > mSizes[2]
Definition layout.h:145
void setColWidth(int n, int w)
Sets the minimum width of a column.
Definition layout.cpp:140
void reflow(int nX, int nY, int nW, int nH)
Computes and sets the positions of all the widgets.
Definition layout.cpp:287
void matchColWidth(int n1, int n2)
Sets the widths of two columns to the maximum of their widths.
Definition layout.cpp:152
int getSize(int dim) const
Gets the total size along a given axis.
Definition layout.cpp:274
void extend(int x, int y, int w, int h)
Spawns a cell over several columns/rows.
Definition layout.cpp:161
void resizeGrid(int w, int h)
Ensures the private vectors are large enough.
Definition layout.cpp:115
char mSpacing
Definition layout.h:148
LayoutCell & place(gcn::Widget *, int x, int y, int w=1, int h=1)
Places a widget in a given cell.
Definition layout.cpp:168
std::vector< short > getSizes(int dim, int upp) const
Gets the column/row sizes along a given axis.
Definition layout.cpp:214
void setRowHeight(int n, int h)
Sets the minimum height of a row.
Definition layout.cpp:146
friend class LayoutCell
Definition layout.h:68
LayoutCell & at(int x, int y, int w=1, int h=1)
Returns a reference on the cell at given position.
Definition layout.cpp:104
This class describes the formatting of a widget in the cell of a layout table.
Definition layout.h:159
~LayoutCell()
Definition layout.cpp:38
LayoutCell & setPadding(int p)
Sets the padding around the cell content.
Definition layout.h:179
char mAlign[2]
Definition layout.h:262
char mPadding
Definition layout.h:260
short mSize[2]
Definition layout.h:259
char mType
Definition layout.h:264
void reflow(int nx, int ny, int nw, int nh)
Definition layout.cpp:59
friend class LayoutArray
Definition layout.h:161
gcn::Widget * mWidget
Definition layout.h:239
LayoutCell & place(gcn::Widget *wg, int x, int y, int w=1, int h=1)
Definition layout.h:203
LayoutArray * mArray
Definition layout.h:240
char mExtent[2]
Definition layout.h:261
LayoutCell & at(int x, int y)
Definition layout.h:197
LayoutArray & getArray()
Returns the embedded array.
Definition layout.cpp:44
void computeSizes()
Sets the minimum widths and heights of this cell and of all the inner cells.
Definition layout.cpp:72
void reflow(int &nW, int &nH)
Sets the positions of all the widgets.
Definition layout.cpp:321
Layout()
Definition layout.cpp:315
@ AUTO_SET
Uses the share as the new size.
Definition layout.h:304
@ AUTO_DEF
Default value, behaves like AUTO_ADD.
Definition layout.h:303
bool mComputed
Definition layout.h:309