1.11
view.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_VIEW_H
7#define EGT_VIEW_H
8
14#include <egt/detail/meta.h>
15#include <egt/frame.h>
16#include <egt/slider.h>
17#include <memory>
18
19namespace egt
20{
21inline namespace v1
22{
23
34class EGT_API ScrolledView : public Frame
35{
36public:
37
41 enum class Policy
42 {
43 never,
44 always,
45 as_needed
46 };
47
52 explicit ScrolledView(Policy horizontal_policy = Policy::as_needed,
53 Policy vertical_policy = Policy::as_needed) noexcept;
54
60 explicit ScrolledView(const Rect& rect,
61 Policy horizontal_policy = Policy::as_needed,
62 Policy vertical_policy = Policy::as_needed) noexcept;
63
70 explicit ScrolledView(Frame& parent, const Rect& rect,
71 Policy horizontal_policy = Policy::as_needed,
72 Policy vertical_policy = Policy::as_needed) noexcept;
73
79 explicit ScrolledView(Frame& parent,
80 Policy horizontal_policy = Policy::as_needed,
81 Policy vertical_policy = Policy::as_needed) noexcept;
82
86 explicit ScrolledView(Serializer::Properties& props) noexcept
87 : ScrolledView(props, false)
88 {
89 }
90
91protected:
92
93 explicit ScrolledView(Serializer::Properties& props, bool is_derived) noexcept;
94
95public:
96
97 void handle(Event& event) override;
98
99 void draw(Painter& painter, const Rect& rect) override;
100
101 void resize(const Size& size) override;
102
103 void layout() override;
104
110 EGT_NODISCARD Point offset() const { return m_offset; }
111
117 void offset(Point offset);
118
124 EGT_NODISCARD const Rect& offset_range() const { return m_offset_range; };
125
131 EGT_NODISCARD Point offset_min() const { return offset_range().bottom_right(); }
132
138 EGT_NODISCARD Point offset_max() const { return offset_range().top_left(); }
139
143 int hoffset() const { return m_offset.x(); }
144
150 void hoffset(int offset)
151 {
152 this->offset(Point(offset, m_offset.y()));
153 }
154
158 int voffset() const { return m_offset.y(); }
159
165 void voffset(int offset)
166 {
167 this->offset(Point(m_offset.x(), offset));
168 }
169
173 EGT_NODISCARD Policy hpolicy() const { return m_horizontal_policy; }
174
178 void hpolicy(Policy policy)
179 {
180 if (detail::change_if_diff<>(m_horizontal_policy, policy))
181 layout();
182 }
183
187 EGT_NODISCARD Policy vpolicy() const { return m_vertical_policy; }
188
192 void vpolicy(Policy policy)
193 {
194 if (detail::change_if_diff<>(m_vertical_policy, policy))
195 layout();
196 }
197
201 EGT_NODISCARD DefaultDim slider_dim() const { return m_slider_dim; }
202
207 {
208 if (detail::change_if_diff<>(m_slider_dim, dim))
209 damage();
210 }
211
215 void serialize(Serializer& serializer) const override;
216
221
222 static std::string policy2str(Policy policy);
223 static Policy str2policy(const std::string& str);
224
225protected:
226
227 bool internal_drag() const override { return true; }
228
229 void damage_from_subordinate(const Rect& rect) override
230 {
231 damage(rect + m_offset);
232 }
233
234 Point point_from_subordinate(const Widget& subordinate) const override;
235
237 EGT_NODISCARD bool hscrollable() const
238 {
239 return m_hscrollable;
240 }
241
243 EGT_NODISCARD bool vscrollable() const
244 {
245 return m_vscrollable;
246 }
247
250 {
251 const auto& r = offset_range();
252
253 m_hscrollable = (m_horizontal_policy == Policy::always) ||
254 (m_horizontal_policy == Policy::as_needed && r.width() > 0);
255 m_vscrollable = (m_vertical_policy == Policy::always) ||
256 (m_vertical_policy == Policy::as_needed && r.height() > 0);
257 }
258
261
264
266 EGT_NODISCARD Rect super_rect() const;
267
270
273
275 bool m_hscrollable{false};
276
278 bool m_vscrollable{false};
279
282
285
288
291
294
296 Policy m_horizontal_policy{Policy::as_needed};
297
299 Policy m_vertical_policy{Policy::as_needed};
300
302 DefaultDim m_slider_dim{8};
303};
304
305}
306}
307
308#endif
A Frame is a Widget that has children widgets.
Definition frame.h:45
Drawing interface for 2D graphics.
Definition painter.h:54
A scrollable view.
Definition view.h:35
static std::string policy2str(Policy policy)
int hoffset() const
Get the horizontal offset.
Definition view.h:143
Slider m_vslider
Vertical slider shown when scrollable.
Definition view.h:290
ScrolledView(const Rect &rect, Policy horizontal_policy=Policy::as_needed, Policy vertical_policy=Policy::as_needed) noexcept
void voffset(int offset)
Set the vertical offset.
Definition view.h:165
void slider_dim(DefaultDim dim)
Set the slider dimension.
Definition view.h:206
void damage_from_subordinate(const Rect &rect) override
Special variation of damage() that is to be called explicitly by subordinate widgets.
Definition view.h:229
ScrolledView(Policy horizontal_policy=Policy::as_needed, Policy vertical_policy=Policy::as_needed) noexcept
EGT_NODISCARD Point offset_min() const
Get the minimum offset currently possible.
Definition view.h:131
int voffset() const
Get the vertical offset.
Definition view.h:158
Point m_start_offset
Starting offset for the drag.
Definition view.h:293
void draw(Painter &painter, const Rect &rect) override
Draw the widget.
void init_sliders()
Initialize the sliders properties.
EGT_NODISCARD Rect super_rect() const
Return the super rectangle that includes all of the child widgets.
ScrolledView(Frame &parent, Policy horizontal_policy=Policy::as_needed, Policy vertical_policy=Policy::as_needed) noexcept
void post_deserialize(Serializer::Properties &props) override
Resume deserializing of the widget after its children have been deserialized.
void deserialize(Serializer::Properties &props)
Deserialize ScrolledView properties.
void update_sliders()
Update properties of the sliders.
void offset(Point offset)
Set the position.
void vpolicy(Policy policy)
Set the vertical policy.
Definition view.h:192
EGT_NODISCARD Policy hpolicy() const
Get the horizontal policy.
Definition view.h:173
EGT_NODISCARD const Rect & offset_range() const
Get the offset range currently possible.
Definition view.h:124
EGT_NODISCARD bool hscrollable() const
Horizontal scrollable.
Definition view.h:237
void layout() override
Perform layout of the Widget.
ScrolledView(Frame &parent, const Rect &rect, Policy horizontal_policy=Policy::as_needed, Policy vertical_policy=Policy::as_needed) noexcept
Slider m_hslider
Horizontal slider shown when scrollable.
Definition view.h:287
EGT_NODISCARD bool vscrollable() const
Vertical scrollable.
Definition view.h:243
void resize(const Size &size) override
Resize the widget.
bool internal_drag() const override
Definition view.h:227
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
ScrolledView(Serializer::Properties &props, bool is_derived) noexcept
ScrolledView(Serializer::Properties &props) noexcept
Definition view.h:86
Point point_from_subordinate(const Widget &subordinate) const override
Policy
Scrollbar policy.
Definition view.h:42
EGT_NODISCARD DefaultDim slider_dim() const
Get the slider dimension.
Definition view.h:201
void handle(Event &event) override
Handle an event.
EGT_NODISCARD Policy vpolicy() const
Get the vertical policy.
Definition view.h:187
Point m_offset
Current offset of the view.
Definition view.h:281
EGT_NODISCARD Point offset() const
Get the current offset.
Definition view.h:110
void resize_sliders()
Resize the sliders whenever the size of these changes.
static Policy str2policy(const std::string &str)
EGT_NODISCARD Point offset_max() const
Get the maximum offset currently possible.
Definition view.h:138
void hoffset(int offset)
Set the horizontal offset.
Definition view.h:150
void update_scrollable()
Update scrollable settings based on current size.
Definition view.h:249
void hpolicy(Policy policy)
Set the horizontal policy.
Definition view.h:178
Rect m_offset_range
Current offset range.
Definition view.h:284
Abstract base serializer class.
Definition serialize.h:34
std::list< std::tuple< std::string, std::string, Serializer::Attributes > > Properties
Definition serialize.h:47
This is a slider that can be used to select integer value from a range.
Definition slider.h:591
Base Widget class.
Definition widget.h:53
int DefaultDim
Define the default dimension type used for geometry.
Definition geometry.h:34
EGT framework namespace.
Definition animation.h:24
A single event that has information about the event and state for the event.
Definition event.h:255