-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathWBoxLayout.C
More file actions
362 lines (307 loc) · 9.94 KB
/
WBoxLayout.C
File metadata and controls
362 lines (307 loc) · 9.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include <cassert>
#include "Wt/WApplication.h"
#include "Wt/WBoxLayout.h"
#include "Wt/WEnvironment.h"
#include "Wt/WLogger.h"
#include "Wt/WWebWidget.h"
#include "Wt/WWidgetItem.h"
#include "StdGridLayoutImpl2.h"
#include "FlexLayoutImpl.h"
namespace {
class Spacer : public Wt::WWebWidget
{
public:
Spacer() { setInline(false); }
protected:
virtual Wt::DomElementType domElementType() const override
{
return Wt::DomElementType::DIV;
}
};
}
namespace Wt {
LOGGER("WBoxLayout");
WBoxLayout::WBoxLayout(LayoutDirection dir)
: direction_(dir)
{ }
void WBoxLayout::addItem(std::unique_ptr<WLayoutItem> item)
{
insertItem(count(), std::move(item), 0, None);
}
std::unique_ptr<WLayoutItem> WBoxLayout::removeItem(WLayoutItem *item)
{
std::unique_ptr<WLayoutItem> result;
int index = indexOf(item);
if (index != -1) {
switch (direction_) {
case LayoutDirection::RightToLeft:
if ((impl() && implementation() != LayoutImplementation::Flex) || !implementationIsFlexLayout())
index = grid_.columns_.size() - 1 - index;
case LayoutDirection::LeftToRight: {
result = std::move(grid_.items_[0][index].item_);
grid_.columns_.erase(grid_.columns_.begin() + index);
grid_.items_[0].erase(grid_.items_[0].begin() + index);
break;
}
case LayoutDirection::BottomToTop:
if ((impl() && implementation() != LayoutImplementation::Flex) || !implementationIsFlexLayout())
index = grid_.rows_.size() - 1 - index;
case LayoutDirection::TopToBottom: {
result = std::move(grid_.items_[index][0].item_);
grid_.rows_.erase(grid_.rows_.begin() + index);
grid_.items_.erase(grid_.items_.begin() + index);
}
}
itemRemoved(item);
}
return result;
}
WLayoutItem *WBoxLayout::itemAt(int index) const
{
switch (direction_) {
case LayoutDirection::RightToLeft:
if ((impl() && implementation() != LayoutImplementation::Flex) || !implementationIsFlexLayout())
index = grid_.columns_.size() - 1 - index;
case LayoutDirection::LeftToRight:
return grid_.items_[0][index].item_.get();
case LayoutDirection::BottomToTop:
if ((impl() && implementation() != LayoutImplementation::Flex) || !implementationIsFlexLayout())
index = grid_.rows_.size() - 1 - index;
case LayoutDirection::TopToBottom:
return grid_.items_[index][0].item_.get();
}
assert(false);
return nullptr;
}
int WBoxLayout::count() const
{
return grid_.rows_.size() * grid_.columns_.size();
}
void WBoxLayout::setDirection(LayoutDirection direction)
{
if (direction_ != direction) {
direction_ = direction;
}
}
void WBoxLayout::setSpacing(int size)
{
grid_.horizontalSpacing_ = size;
grid_.verticalSpacing_ = size;
}
void WBoxLayout::addWidget(std::unique_ptr<WWidget> widget, int stretch,
WFlags<AlignmentFlag> alignment)
{
insertWidget(count(), std::move(widget), stretch, alignment);
}
void WBoxLayout::addLayout(std::unique_ptr<WLayout> layout, int stretch,
WFlags<AlignmentFlag> alignment)
{
insertLayout(count(), std::move(layout), stretch, alignment);
}
void WBoxLayout::addSpacing(const WLength& size)
{
insertSpacing(count(), size);
}
void WBoxLayout::addStretch(int stretch)
{
insertStretch(count(), stretch);
}
void WBoxLayout::insertWidget(int index, std::unique_ptr<WWidget> widget,
int stretch,
WFlags<AlignmentFlag> alignment)
{
if (widget->layoutSizeAware() && stretch == 0)
stretch = -1;
insertItem(index,
std::unique_ptr<WLayoutItem>(new WWidgetItem(std::move(widget))),
stretch, alignment);
}
void WBoxLayout::insertLayout(int index, std::unique_ptr<WLayout> layout,
int stretch,
WFlags<AlignmentFlag> alignment)
{
insertItem(index, std::move(layout), stretch, alignment);
}
void WBoxLayout::insertSpacing(int index, const WLength& size)
{
std::unique_ptr<WWidget> spacer(createSpacer(size));
insertItem(index,
std::unique_ptr<WWidgetItem>(new WWidgetItem(std::move(spacer))),
0, None);
}
void WBoxLayout::insertStretch(int index, int stretch)
{
std::unique_ptr<WWidget> spacer = createSpacer(WLength(0));
insertItem(index,
std::unique_ptr<WWidgetItem>(new WWidgetItem(std::move(spacer))),
stretch, None);
}
bool WBoxLayout::setStretchFactor(WWidget *widget, int stretch)
{
for (int i = 0; i < count(); ++i) {
WLayoutItem *item = itemAt(i);
if (item && item->widget() == widget) {
setStretchFactor(i, stretch);
return true;
}
}
return false;
}
bool WBoxLayout::setStretchFactor(WLayout *layout, int stretch)
{
for (int i = 0; i < count(); ++i) {
WLayoutItem *item = itemAt(i);
if (item && item->layout() == layout) {
setStretchFactor(i, stretch);
return true;
}
}
return false;
}
void WBoxLayout::setStretchFactor(int i, int stretch)
{
switch (direction_) {
case LayoutDirection::RightToLeft:
if ((impl() && implementation() != LayoutImplementation::Flex) || !implementationIsFlexLayout())
i = grid_.columns_.size() - 1 - i;
case LayoutDirection::LeftToRight:
grid_.columns_[i].stretch_ = stretch;
break;
case LayoutDirection::BottomToTop:
if ((impl() && implementation() != LayoutImplementation::Flex) || !implementationIsFlexLayout())
i = grid_.rows_.size() - 1 - i;
case LayoutDirection::TopToBottom:
grid_.rows_[i].stretch_ = stretch;
}
}
void WBoxLayout::insertItem(int index, std::unique_ptr<WLayoutItem> item,
int stretch, WFlags<AlignmentFlag> alignment)
{
WLayoutItem *it = item.get();
switch (direction_) {
case LayoutDirection::RightToLeft:
if ((impl() && implementation() != LayoutImplementation::Flex) || !implementationIsFlexLayout())
index = grid_.columns_.size() - index;
case LayoutDirection::LeftToRight:
grid_.columns_.insert(grid_.columns_.begin() + index,
Impl::Grid::Section(stretch));
if (grid_.items_.empty()) {
grid_.items_.push_back(std::vector<Impl::Grid::Item>());
grid_.rows_.push_back(Impl::Grid::Section());
grid_.rows_[0].stretch_ = -1; // make height managed
}
grid_.items_[0].insert(grid_.items_[0].begin() + index,
Impl::Grid::Item(std::move(item), alignment));
break;
case LayoutDirection::BottomToTop:
if ((impl() && implementation() != LayoutImplementation::Flex) || !implementationIsFlexLayout())
index = grid_.rows_.size() - index;
case LayoutDirection::TopToBottom:
if (grid_.columns_.empty()) {
grid_.columns_.push_back(Impl::Grid::Section());
grid_.columns_[0].stretch_ = -1; // make width managed
}
grid_.rows_.insert(grid_.rows_.begin() + index,
Impl::Grid::Section(stretch));
grid_.items_.insert(grid_.items_.begin() + index,
std::vector<Impl::Grid::Item>());
grid_.items_[index].push_back(Impl::Grid::Item(std::move(item), alignment));
break;
}
itemAdded(it);
}
std::unique_ptr<WWidget> WBoxLayout::createSpacer(const WLength& size)
{
std::unique_ptr<Spacer> spacer(new Spacer());
if (size.toPixels() > 0) {
if (direction_ == LayoutDirection::LeftToRight ||
direction_ == LayoutDirection::RightToLeft)
spacer->setMinimumSize(size, WLength::Auto);
else
spacer->setMinimumSize(WLength::Auto, size);
}
return std::move(spacer);
}
void WBoxLayout::setResizable(int index, bool enabled,
const WLength& initialSize)
{
if (preferredImplementation() == LayoutImplementation::Flex) {
LOG_WARN("Resize handles are not supported for flex layout implementation, "
"using JavaScript implementation instead");
setPreferredImplementation(LayoutImplementation::JavaScript);
}
switch (direction_) {
case LayoutDirection::RightToLeft:
if ((impl() && implementation() != LayoutImplementation::Flex) || !implementationIsFlexLayout())
index = grid_.columns_.size() - 1 - index;
case LayoutDirection::LeftToRight:
grid_.columns_[index].resizable_ = enabled;
grid_.columns_[index].initialSize_ = initialSize;
break;
case LayoutDirection::BottomToTop:
if ((impl() && implementation() != LayoutImplementation::Flex) || !implementationIsFlexLayout())
index = grid_.rows_.size() - 1 - index;
case LayoutDirection::TopToBottom:
grid_.rows_[index].resizable_ = enabled;
grid_.rows_[index].initialSize_ = initialSize;
}
update(nullptr);
}
bool WBoxLayout::isResizable(int index) const
{
switch (direction_) {
case LayoutDirection::RightToLeft:
if ((impl() && implementation() != LayoutImplementation::Flex) || !implementationIsFlexLayout())
index = grid_.columns_.size() - 1 - index;
case LayoutDirection::LeftToRight:
return grid_.columns_[index].resizable_;
case LayoutDirection::BottomToTop:
if ((impl() && implementation() != LayoutImplementation::Flex) || !implementationIsFlexLayout())
index = grid_.rows_.size() - 1 - index;
case LayoutDirection::TopToBottom:
return grid_.rows_[index].resizable_;
}
return false;
}
void WBoxLayout::iterateWidgets(const HandleWidgetMethod& method) const
{
for (unsigned r = 0; r < grid_.rows_.size(); ++r) {
for (unsigned c = 0; c < grid_.columns_.size(); ++c) {
WLayoutItem *item = grid_.items_[r][c].item_.get();
if (item)
item->iterateWidgets(method);
}
}
}
void WBoxLayout::setParentWidget(WWidget *parent)
{
if (parent)
setImplementation();
WLayout::setParentWidget(parent);
}
bool WBoxLayout::implementationIsFlexLayout() const
{
const WEnvironment &env = WApplication::instance()->environment();
return preferredImplementation() == LayoutImplementation::Flex &&
!env.agentIsIElt(10);
}
void WBoxLayout::updateImplementation()
{
if (!parentWidget())
return;
setImplementation();
}
void WBoxLayout::setImplementation()
{
bool isFlexLayout = implementationIsFlexLayout();
if (isFlexLayout)
setImpl(cpp14::make_unique<FlexLayoutImpl>(this, grid_));
else
setImpl(cpp14::make_unique<StdGridLayoutImpl2>(this, grid_));
}
}