1.10
frame.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_FRAME_H
7#define EGT_FRAME_H
8
14#include <cassert>
15#include <egt/detail/alignment.h>
16#include <egt/detail/meta.h>
17#include <egt/screen.h>
18#include <egt/widget.h>
19#include <exception>
20#include <memory>
21#include <string>
22#include <vector>
23
24namespace egt
25{
26inline namespace v1
27{
44class EGT_API Frame : public Widget
45{
46public:
47
52 explicit Frame(const Rect& rect = {},
53 const Flags& flags = {}) noexcept;
54
60 Frame(Frame& parent, const Rect& rect,
61 const Flags& flags = {}) noexcept;
62
67 : Frame(props, false)
68 {
69 }
70
71protected:
72
73 Frame(Serializer::Properties& props, bool is_derived) noexcept;
74
75public:
76
77 Frame(const Frame&) = delete;
78 Frame& operator=(const Frame&) = delete;
79 Frame(Frame&&) noexcept = default;
80 Frame& operator=(Frame&&) noexcept = default;
81
82 ~Frame() noexcept override;
83
101 virtual void add(const std::shared_ptr<Widget>& widget);
102
108 template<class T>
109 void add(const std::shared_ptr<T>& widget)
110 {
111 auto p = std::dynamic_pointer_cast<Widget>(widget);
112 add(p);
113 }
114
128 void add(Widget& widget)
129 {
130 // Nasty, but it gets the job done. If a widget is passed in as a
131 // reference, we don't own it, so create a "pointless" shared_ptr that
132 // will not delete it.
133 auto w = std::shared_ptr<Widget>(&widget, [](Widget*) {});
134 add(w);
135 }
136
140 bool is_child(Widget* widget) const;
141
142 EGT_NODISCARD virtual Point to_child(const Point& p) const
143 {
144 return Widget::to_subordinate(p);
145 }
146
147 EGT_NODISCARD Rect to_child(Rect rect) const
148 {
149 return Widget::to_subordinate(rect);
150 }
151
159 virtual void remove(Widget* widget);
160
165
166 using Widget::children;
167
171 EGT_NODISCARD size_t count_children() const { return children().size(); }
172
176 EGT_NODISCARD std::shared_ptr<Widget> child_at(size_t index) const
177 {
178 if (index >= children().size())
179 return nullptr;
180 else
181 return *std::next(children().begin(), index);
182 }
183
187 EGT_NODISCARD bool top_level() const override
188 {
189 return !m_parent;
190 }
191
201 template <class T>
202 std::shared_ptr<T> find_child(const std::string& name)
203 {
204 if (name.empty())
205 return nullptr;
206
207 auto i = std::find_if(children().begin(), children().end(),
208 [&name](const auto & obj)
209 {
210 return obj->name() == name;
211 });
212
213 // just return first one
214 if (i != children().end())
215 return std::dynamic_pointer_cast<T>(*i);
216
217 i = std::find_if(children().begin(), children().end(),
218 [](const auto & obj)
219 {
220 return obj->frame();
221 });
222
223 for (; i != children().end(); ++i)
224 {
225 auto frame = dynamic_cast<Frame*>((*i).get());
226 if (frame)
227 {
228 auto w = frame->find_child<T>(name);
229 if (w)
230 return w;
231 }
232 }
233
234 return nullptr;
235 }
236
237 void walk(const WalkCallback& callback, int level = 0) override;
238
242 void paint_to_file(const std::string& filename = {}) override;
243
248
249 using Widget::zorder;
250 using Widget::zorder_down;
251 using Widget::zorder_up;
252 using Widget::zorder_bottom;
253 using Widget::zorder_top;
254
255 void show() override
256 {
257 if (visible())
258 return;
259
260 Widget::show();
261 layout();
262 }
263
270
284 template<typename T, typename... Args>
285 std::shared_ptr<T> spawn(Args&& ... args)
286 {
287 auto w = std::make_shared<T>(std::forward<Args>(args)...);
288 add(w);
289 return w;
290 }
291
292 using Widget::special_child_draw_callback;
293
294 void serialize(Serializer& serializer) const override;
295
296 void serialize_children(Serializer& serializer) const override;
297
298 void deserialize_children(const Deserializer& deserializer) override;
299
301 void on_screen_resized() override;
302
303private:
304
305 void remove_all_basic();
306};
307
308}
309}
310
311#endif
Definition serialize.h:200
Utility class for managing a set of flags with the ability to observe changes to the flags.
Definition flags.h:40
A Frame is a Widget that has children widgets.
Definition frame.h:45
Frame & operator=(const Frame &)=delete
EGT_NODISCARD bool top_level() const override
Return true if this is a top level frame, with no parent.
Definition frame.h:187
EGT_NODISCARD size_t count_children() const
Get the number of children widgets.
Definition frame.h:171
void serialize_children(Serializer &serializer) const override
Serialize the widget's children to the specified serializer.
virtual EGT_NODISCARD Point to_child(const Point &p) const
Definition frame.h:142
Frame(Serializer::Properties &props) noexcept
Definition frame.h:66
void walk(const WalkCallback &callback, int level=0) override
Walk the Widget tree and call callback with each Widget.
void add(Widget &widget)
Add a child widget.
Definition frame.h:128
virtual void remove(Widget *widget)
Remove a child widget.
std::shared_ptr< T > find_child(const std::string &name)
Find a child Widget in the entire tree by name.
Definition frame.h:202
Frame(Frame &&) noexcept=default
void remove_all()
Remove all child widgets.
std::shared_ptr< T > spawn(Args &&... args)
Create a child widget of the specified type.
Definition frame.h:285
EGT_NODISCARD Rect to_child(Rect rect) const
Definition frame.h:147
void deserialize_children(const Deserializer &deserializer) override
Deserialize the children of this widget.
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
Frame(const Frame &)=delete
Widget * hit_test(const DisplayPoint &point)
Get the widget under the given DisplayPoint.
void show() override
Show the Widget.
Definition frame.h:255
EGT_NODISCARD std::shared_ptr< Widget > child_at(size_t index) const
Get a child widget at a specific index.
Definition frame.h:176
Frame(Frame &parent, const Rect &rect, const Flags &flags={}) noexcept
Frame(Serializer::Properties &props, bool is_derived) noexcept
bool is_child(Widget *widget) const
Returns true if the child exists.
void paint_to_file(const std::string &filename={}) override
Save the entire frame surface to a file.
void on_screen_resized() override
Overridden to be called recursively on all children.
Frame(const Rect &rect={}, const Flags &flags={}) noexcept
void paint_children_to_file()
Paint individual children to file.
Abstract base serializer class.
Definition serialize.h:34
std::list< std::tuple< std::string, std::string, Serializer::Attributes > > Properties
Definition serialize.h:47
Base Widget class.
Definition widget.h:53
std::function< bool(Widget *widget, int level)> WalkCallback
Callback definition used by walk().
Definition widget.h:1161
EGT framework namespace.
Definition animation.h:24