-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathWBoxLayout.h
More file actions
408 lines (374 loc) · 14.2 KB
/
WBoxLayout.h
File metadata and controls
408 lines (374 loc) · 14.2 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// This may look like C code, but it's really -*- C++ -*-
/*
* Copyright (C) 2008 Emweb bv, Herent, Belgium.
*
* See the LICENSE file for terms of use.
*/
#ifndef WBOX_LAYOUT_H_
#define WBOX_LAYOUT_H_
#include <Wt/WGridLayout.h>
namespace Wt {
/*! \class WBoxLayout Wt/WBoxLayout.h Wt/WBoxLayout.h
* \brief A layout manager which arranges widgets horizontally or vertically
*
* This layout manager arranges widgets horizontally or vertically
* inside the parent container.
*
* The space is divided so that each widget is given its preferred
* size, and remaining space is divided according to stretch factors
* among widgets. If not all widgets can be given their preferred size
* (there is not enough room), then widgets are given a smaller size
* (down to their minimum size). If necessary, the container (or
* parent layout) of this layout is resized to meet minimum size
* requirements.
*
* The preferred width or height of a widget is based on its natural
* size, where it presents its contents without
* overflowing. WWidget::resize() or (CSS <tt>width</tt>,
* <tt>height</tt> properties) can be used to adjust the preferred
* size of a widget.
*
* The minimum width or height of a widget is based on the minimum
* dimensions of the widget or the nested layout. The default minimum
* height or width for a widget is 0. It can be specified using
* WWidget::setMinimumSize() or using CSS <tt>min-width</tt> or
* <tt>min-height</tt> properties.
*
* You should use \link WContainerWidget::setOverflow()
* WContainerWidget::setOverflow(OverflowAuto) \endlink to automatically
* show scrollbars for widgets inserted in the layout to cope with a size
* set by the layout manager that is smaller than the preferred size.
*
* When the container of a layout manager does not have a defined size
* (by having an explicit size, or by being inside a layout manager),
* or has has only a maximum size set using
* WWidget::setMaximumSize(), then the size of the container will be
* based on the preferred size of the contents, up to this maximum
* size, instead of the default behaviour of constraining the size of
* the children based on the size of the container. Note that because
* of the CSS defaults, a WContainer has by default no height, but
* inherits the width of its parent widget. The width is thus by default
* defined.
*
* A layout manager may provide resize handles between items which
* allow the user to change the automatic layout provided by the
* layout manager (see setResizable()).
*
* Each item is separated using a constant spacing, which defaults to
* 6 pixels, and can be changed using setSpacing(). In addition, when
* this layout is a top-level layout (i.e. is not nested inside
* another layout), a margin is set around the contents. This margin
* defaults to 9 pixels, and can be changed using
* setContentsMargins(). You can add more space between two widgets
* using addSpacing().
*
* For each item a stretch factor may be defined, which controls how
* remaining space is used. Each item is stretched using the stretch
* factor to fill the remaining space.
*
* \if cpp
* Usage example:
* \code
* Wt::WContainerWidget *w = addWidget(std::make_unique<Wt::WContainerWidget>());
* w->resize(Wt::WLength(), 600);
*
* auto layout = std::make_unique<Wt::WBoxLayout>(Wt::LayoutDirection::TopToBottom);
* layout->addWidget(std::make_unique<Wt::WText>("One"));
* layout->addWidget(std::make_unique<Wt::WText>("Two"));
* layout->addWidget(std::make_unique<Wt::WText>("Three"));
* layout->addWidget(std::make_unique<Wt::WText>("Four"));
*
* w->setLayout(std::move(layout));
* \endcode
* \endif
*
* \note When JavaScript support is not available, not all functionality
* of the layout is available. In particular, vertical size management is
* not available.
*
* \note When a layout is used on a first page with progressive
* bootstrap, then the layout will progress only in a limited way to a
* full JavaScript-based layout. You can thus not rely on it to behave
* properly for example when dynamically adding or removing widgets.
*/
class WT_API WBoxLayout : public WLayout
{
public:
/*! \brief Creates a new box layout.
*
* This constructor is rarely used. Instead, use the convenient
* constructors of the specialized WHBoxLayout or WVBoxLayout classes.
*
* Use \p parent = \c 0 to created a layout manager that can be
* nested inside other layout managers.
*/
WBoxLayout(LayoutDirection dir);
virtual ~WBoxLayout() override;
virtual void addItem(std::unique_ptr<WLayoutItem> item) override;
virtual std::unique_ptr<WLayoutItem> removeItem(WLayoutItem *item) override;
virtual WLayoutItem *itemAt(int index) const override;
virtual int count() const override;
/*! \brief Sets the layout direction.
*
* \note Changing the layout direction after something (a widget or nested layout)
* has been added is not supported.
*
* \sa direction()
*/
void setDirection(LayoutDirection direction);
/*! \brief Returns the layout direction.
*
* \sa setDirection()
*/
LayoutDirection direction() const { return direction_; }
/*! \brief Sets spacing between each item.
*
* The default spacing is 6 pixels.
*/
void setSpacing(int size);
/*! \brief Returns the spacing between each item.
*
* \sa setSpacing()
*/
int spacing() const { return grid_.horizontalSpacing_; }
/*! \brief Adds a widget to the layout.
*
* Adds a widget to the layout, with given \p stretch factor. When
* the stretch factor is 0, the widget will not be resized by the
* layout manager (stretched to take excess space).
*
* The \p alignment parameter is a combination of a horizontal
* and/or a vertical AlignmentFlag OR'ed together.
*
* The \p alignment specifies the vertical and horizontal
* alignment of the item. The default value 0 indicates that the
* item is stretched to fill the entire column or row. The alignment can
* be specified as a logical combination of a horizontal alignment
* (Wt::AlignmentFlag::Left, Wt::AlignmentFlag::Center, or Wt::AlignmentFlag::Right) and a
* vertical alignment (Wt::AlignmentFlag::Top, Wt::AlignmentFlag::Middle, or
* Wt::AlignmentFlag::Bottom).
*
* \sa addLayout(), insertWidget()
*/
#ifndef WT_TARGET_JAVA
void addWidget(std::unique_ptr<WWidget> widget, int stretch,
WFlags<AlignmentFlag> alignment);
#else // WT_TARGET_JAVA
void addWidget(std::unique_ptr<WWidget> widget, int stretch = 0,
WFlags<AlignmentFlag> alignment = None);
#endif // WT_TARGET_JAVA
#ifndef WT_TARGET_JAVA
/*! \brief Adds a widget to the layout, returning a raw pointer.
*
* This is implemented as:
*
* \code
* Widget *result = widget.get();
* addWidget(std::unique_ptr<WWidget>(std::move(widget)), stretch, alignment);
* return result;
* \endcode
*/
template <typename Widget>
Widget *addWidget(std::unique_ptr<Widget> widget, int stretch = 0,
WFlags<AlignmentFlag> alignment = None)
{
Widget *result = widget.get();
addWidget(std::unique_ptr<WWidget>(std::move(widget)), stretch, alignment);
return result;
}
#else // WT_TARGET_JAVA
template <typename Widget>
Widget *addWidget(std::unique_ptr<Widget> widget);
template <typename Widget>
Widget *addWidget(std::unique_ptr<Widget> widget, int stretch);
#endif // WT_TARGET_JAVA
/*! \brief Adds a nested layout to the layout.
*
* Adds a nested layout, with given \p stretch factor.
*
* \sa addWidget(WWidget *, int, WFlags<AlignmentFlag>), insertLayout()
*/
#ifndef WT_TARGET_JAVA
void addLayout(std::unique_ptr<WLayout> layout, int stretch,
WFlags<AlignmentFlag> alignment);
#else // WT_TARGET_JAVA
void addLayout(std::unique_ptr<WLayout> layout, int stretch = 0,
WFlags<AlignmentFlag> alignment = None);
#endif // WT_TARGET_JAVA
#ifndef WT_TARGET_JAVA
/*! \briefs Adds a nested layout to the layout, returning a raw pointer.
*
* This is implemented as:
*
* \code
* Layout *result = layout.get();
* addLayout(std::unique_ptr<WLayout>(std::move(layout)), stretch, alignment);
* return result;
* \endcode
*/
template <typename Layout>
Layout *addLayout(std::unique_ptr<Layout> layout, int stretch = 0,
WFlags<AlignmentFlag> alignment = None)
{
Layout *result = layout.get();
addLayout(std::unique_ptr<WLayout>(std::move(layout)), stretch, alignment);
return result;
}
#endif // WT_TARGET_JAVA
/*! \brief Adds extra spacing.
*
* Adds extra spacing to the layout.
*
* \sa addStretch(), insertStretch()
*/
void addSpacing(const WLength& size);
/*! \brief Adds a stretch element.
*
* Adds a stretch element to the layout. This adds an empty space
* that stretches as needed.
*
* \sa addSpacing(), insertStretch()
*/
void addStretch(int stretch = 0);
/*! \brief Inserts a widget in the layout.
*
* Inserts a widget in the layout at position \p index, with given
* \p stretch factor. When the stretch factor is 0, the widget will
* not be resized by the layout manager (stretched to take excess
* space).
*
* The \p alignment specifies the vertical and horizontal alignment
* of the item. The default value None indicates that the item is
* stretched to fill the entire column or row. The alignment can be
* specified as a logical combination of a horizontal alignment
* (Wt::AlignmentFlag::Left, Wt::AlignmentFlag::Center, or
* Wt::AlignmentFlag::Right) and a vertical alignment
* (Wt::AlignmentFlag::Top, Wt::AlignmentFlag::Middle, or
* Wt::AlignmentFlag::AlignBottom).
*
* \sa insertLayout(), addWidget(WWidget *, int, WFlags<AlignmentFlag>)
*/
void insertWidget(int index, std::unique_ptr<WWidget> widget, int stretch = 0,
WFlags<AlignmentFlag> alignment = None);
/*! \brief Inserts a widget in the layout, returning a raw pointer.
*
* This is implemented as:
* \code
* Widget *result = widget.get();
* insertWidget(index, std::unique_ptr<WWidget>(std::move(widget)), stretch, alignment);
* return result;
* \endcode
*/
#ifndef WT_TARGET_JAVA
template <typename Widget>
Widget *insertWidget(int index, std::unique_ptr<Widget> widget, int stretch = 0,
WFlags<AlignmentFlag> alignment = None)
{
Widget *result = widget.get();
insertWidget(index, std::unique_ptr<WWidget>(std::move(widget)), stretch, alignment);
return result;
}
#else // WT_TARGET_JAVA
template <typename Widget>
Widget *insertWidget(int index, std::unique_ptr<Widget> widget, int stretch);
#endif // WT_TARGET_JAVA
/*! \brief Inserts a nested layout in the layout.
*
* Inserts a nested layout in the layout at position\p index,
* with given \p stretch factor.
*
* \sa insertWidget(), addLayout()
*/
void insertLayout(int index, std::unique_ptr<WLayout> layout, int stretch = 0,
WFlags<AlignmentFlag> alignment = None);
/*! \briefs Inserts a nested layout in the layout, returning a raw pointer.
*
* This is implemented as:
* \code
* Layout *result = layout.get();
* addLayout(index, std::unique_ptr<WLayout>(std::move(layout)), stretch, alignment);
* return result;
* \endcode
*/
#ifndef WT_TARGET_JAVA
template <typename Layout>
Layout *insertLayout(int index, std::unique_ptr<Layout> layout, int stretch = 0,
WFlags<AlignmentFlag> alignment = None)
{
Layout *result = layout.get();
insertLayout(index, std::unique_ptr<WLayout>(std::move(layout)), stretch, alignment);
return result;
}
#else // WT_TARGET_JAVA
template <typename Layout>
Layout *insertLayout(int index, std::unique_ptr<Layout> layout, int stretch);
#endif
/*! \brief Inserts extra spacing in the layout.
*
* Inserts extra spacing in the layout at position \p index.
*
* \sa insertStretch(), addSpacing()
*/
void insertSpacing(int index, const WLength& size);
/*! \brief Inserts a stretch element in the layout.
*
* Inserts a stretch element in the layout at position
* \p index. This adds an empty space that stretches as needed.
*
* \sa insertSpacing(), addStretch()
*/
void insertStretch(int index, int stretch = 0);
/*! \brief Sets the stretch factor for a nested layout.
*
* The \p layout must have previously been added to this layout
* using insertLayout() or addLayout().
*
* Returns whether the \p stretch could be set.
*/
bool setStretchFactor(WLayout *layout, int stretch);
/*! \brief Sets the stretch factor for a widget.
*
* The \p widget must have previously been added to this layout
* using insertWidget() or addWidget().
*
* Returns whether the \p stretch could be set.
*/
bool setStretchFactor(WWidget *widget, int stretch);
/*! \brief Sets whether the use may drag a particular border.
*
* This method sets whether the border that separates item
* <i>index</i> from the next item may be resized by the user,
* depending on the value of <i>enabled</i>.
*
* The default value is <i>false</i>.
*
* If an \p initialSize is given (that is not WLength::Auto), then
* this size is used for the size of the item, overriding the size
* it would be given by the layout manager.
*/
void setResizable(int index, bool enabled = true,
const WLength& initialSize = WLength::Auto);
/*! \brief Returns whether the user may drag a particular border.
*
* This method returns whether the border that separates item
* <i>index</i> from the next item may be resized by the user.
*
* \sa setResizable()
*/
bool isResizable(int index) const;
virtual void iterateWidgets(const HandleWidgetMethod& method) const override;
virtual bool implementationIsFlexLayout() const override;
protected:
void insertItem(int index, std::unique_ptr<WLayoutItem> item, int stretch,
WFlags<AlignmentFlag> alignment);
virtual void updateImplementation() override;
private:
LayoutDirection direction_;
Impl::Grid grid_;
void setStretchFactor(int index, int stretch);
std::unique_ptr<WWidget> createSpacer(const WLength& size);
virtual void setParentWidget(WWidget *parent) override;
void setImplementation();
};
}
#endif // WBOX_LAYOUT_H_