1.10
sizer.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_SIZER_H
7#define EGT_SIZER_H
8
14#include <egt/detail/alignment.h>
15#include <egt/detail/meta.h>
16#include <egt/frame.h>
17#include <memory>
18#include <utility>
19
20namespace egt
21{
22inline namespace v1
23{
24
48class EGT_API BoxSizer : public Frame
49{
50public:
51
56 explicit BoxSizer(Orientation orient = Orientation::horizontal,
57 Justification justify = Justification::middle)
58 : m_orient(orient),
59 m_justify(justify)
60 {
61 name("BoxSizer" + std::to_string(m_widgetid));
62 }
63
69 explicit BoxSizer(Frame& parent,
70 Orientation orient = Orientation::horizontal,
71 Justification justify = Justification::middle)
72 : BoxSizer(orient, justify)
73 {
74 parent.add(*this);
75 }
76
81 : BoxSizer(props, false)
82 {
83 }
84
85protected:
86
87 explicit BoxSizer(Serializer::Properties& props, bool is_derived);
88
89public:
90
95 void layout() override;
96
100 EGT_NODISCARD Justification justify() const { return m_justify; }
101
105 void justify(Justification justify)
106 {
107 if (detail::change_if_diff<>(m_justify, justify))
108 layout();
109 }
110
114 EGT_NODISCARD Orientation orient() const { return m_orient; }
115
119 void orient(Orientation orient)
120 {
121 if (detail::change_if_diff<>(m_orient, orient))
122 layout();
123 }
124
125 void serialize(Serializer& serializer) const override;
126
127protected:
128
130 EGT_NODISCARD Size super_rect() const
131 {
132 if (orient() == Orientation::flex)
133 {
134 Rect result = size();
135 for (auto& child : children())
136 result = Rect::merge(result, child->box());
137
138 return result.size();
139 }
140
141 DefaultDim width = 0;
142 DefaultDim height = 0;
143
144 if (orient() == Orientation::horizontal)
145 {
146 for (auto& child : children())
147 {
148 if (!child->align().is_set(AlignFlag::expand_horizontal))
149 {
150 width += child->box().width();
151 height = std::max(child->box().height(), height);
152 }
153 }
154 }
155 else
156 {
157 for (auto& child : children())
158 {
159 if (!child->align().is_set(AlignFlag::expand_vertical))
160 {
161 width = std::max(child->box().width(), width);
162 height += child->box().height();
163 }
164 }
165 }
166 width += 2. * moat();
167 height += 2. * moat();
168
169 if (align().is_set(AlignFlag::expand_horizontal))
170 if (width < box().width())
171 width = box().width();
172
173 if (align().is_set(AlignFlag::expand_vertical))
174 if (height < box().height())
175 height = box().height();
176
177 return {width, height};
178 }
179
181 Orientation m_orient{Orientation::horizontal};
183 Justification m_justify{Justification::start};
184
185private:
186
187 void deserialize(Serializer::Properties& props);
188};
189
195class EGT_API HorizontalBoxSizer : public BoxSizer
196{
197public:
198
202 explicit HorizontalBoxSizer(Justification justify = Justification::middle)
204 {
205 name("HorizontalBoxSizer" + std::to_string(m_widgetid));
206 }
207
209 : HorizontalBoxSizer(props, false)
210 {
211 }
212
213protected:
214
215 explicit HorizontalBoxSizer(Serializer::Properties& props, bool is_derived)
216 : BoxSizer(props, true)
217 {
218 if (!is_derived)
219 deserialize_leaf(props);
220 }
221
222public:
223
228 explicit HorizontalBoxSizer(Frame& parent, Justification justify = Justification::middle)
230 {}
231};
232
238class EGT_API VerticalBoxSizer : public BoxSizer
239{
240public:
241
245 explicit VerticalBoxSizer(Justification justify = Justification::middle)
247 {
248 name("VerticalBoxSizer" + std::to_string(m_widgetid));
249 }
250
252 : VerticalBoxSizer(props, false)
253 {
254 }
255
256protected:
257
258 explicit VerticalBoxSizer(Serializer::Properties& props, bool is_derived)
259 : BoxSizer(props, true)
260 {
261 if (!is_derived)
262 deserialize_leaf(props);
263 }
264
265public:
266
271 explicit VerticalBoxSizer(Frame& parent, Justification justify = Justification::middle)
273 {}
274};
275
281class EGT_API FlexBoxSizer : public BoxSizer
282{
283public:
284
288 explicit FlexBoxSizer(Justification justify = Justification::middle)
290 {
291 name("FlexBoxSizer" + std::to_string(m_widgetid));
292 }
293
298 explicit FlexBoxSizer(Frame& parent, Justification justify = Justification::middle)
299 : BoxSizer(parent, Orientation::flex, justify)
300 {}
301
303 : FlexBoxSizer(props, false)
304 {
305 }
306
307protected:
308
309 explicit FlexBoxSizer(Serializer::Properties& props, bool is_derived)
310 : BoxSizer(props, true)
311 {
312 if (!is_derived)
313 deserialize_leaf(props);
314 }
315};
316
317}
318}
319
320#endif
Positions and sizes widgets by Orientation.
Definition sizer.h:49
void orient(Orientation orient)
Set the Orientation.
Definition sizer.h:119
BoxSizer(Serializer::Properties &props, bool is_derived)
EGT_NODISCARD Justification justify() const
Get the justify.
Definition sizer.h:100
EGT_NODISCARD Orientation orient() const
Get the Orientation.
Definition sizer.h:114
void layout() override
BoxSizer(Serializer::Properties &props)
Definition sizer.h:80
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
void justify(Justification justify)
Set the justify.
Definition sizer.h:105
BoxSizer(Orientation orient=Orientation::horizontal, Justification justify=Justification::middle)
Definition sizer.h:56
BoxSizer(Frame &parent, Orientation orient=Orientation::horizontal, Justification justify=Justification::middle)
Definition sizer.h:69
EGT_NODISCARD Size super_rect() const
Calculate the super rectangle of all the children.
Definition sizer.h:130
FlexBoxSizer helper variation of BoxSizer.
Definition sizer.h:282
FlexBoxSizer(Justification justify=Justification::middle)
Definition sizer.h:288
FlexBoxSizer(Frame &parent, Justification justify=Justification::middle)
Definition sizer.h:298
FlexBoxSizer(Serializer::Properties &props, bool is_derived)
Definition sizer.h:309
FlexBoxSizer(Serializer::Properties &props)
Definition sizer.h:302
A Frame is a Widget that has children widgets.
Definition frame.h:45
virtual void add(const std::shared_ptr< Widget > &widget)
Add a child widget.
HorizontalBoxSizer helper variation of BoxSizer.
Definition sizer.h:196
HorizontalBoxSizer(Frame &parent, Justification justify=Justification::middle)
Definition sizer.h:228
HorizontalBoxSizer(Serializer::Properties &props)
Definition sizer.h:208
HorizontalBoxSizer(Justification justify=Justification::middle)
Definition sizer.h:202
HorizontalBoxSizer(Serializer::Properties &props, bool is_derived)
Definition sizer.h:215
EGT_NODISCARD constexpr const SizeType< Dim, DimCompat > & size() const noexcept
Get the SizeType of the rectangle.
Definition geometry.h:738
Abstract base serializer class.
Definition serialize.h:34
std::list< std::tuple< std::string, std::string, Serializer::Attributes > > Properties
Definition serialize.h:47
EGT_NODISCARD constexpr Dim width() const noexcept
Get the width value.
Definition geometry.h:457
VerticalBoxSizer helper variation of BoxSizer.
Definition sizer.h:239
VerticalBoxSizer(Frame &parent, Justification justify=Justification::middle)
Definition sizer.h:271
VerticalBoxSizer(Serializer::Properties &props)
Definition sizer.h:251
VerticalBoxSizer(Serializer::Properties &props, bool is_derived)
Definition sizer.h:258
VerticalBoxSizer(Justification justify=Justification::middle)
Definition sizer.h:245
Justification
Generic justification of children flags.
Definition widgetflags.h:667
T & align(T &widget, const AlignFlags &a)
Helper to set alignment of a widget.
Definition widgetflags.h:624
Orientation
Generic orientation flags.
Definition widgetflags.h:652
int DefaultDim
Define the default dimension type used for geometry.
Definition geometry.h:34
EGT framework namespace.
Definition animation.h:24