1.10
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/canvas.h>
15#include <egt/detail/meta.h>
16#include <egt/frame.h>
17#include <egt/slider.h>
18#include <memory>
19
20namespace egt
21{
22inline namespace v1
23{
24
35class EGT_API ScrolledView : public Frame
36{
37public:
38
42 enum class Policy
43 {
44 never,
45 always,
46 as_needed
47 };
48
53 explicit ScrolledView(Policy horizontal_policy = Policy::as_needed,
54 Policy vertical_policy = Policy::as_needed) noexcept;
55
61 explicit ScrolledView(const Rect& rect,
62 Policy horizontal_policy = Policy::as_needed,
63 Policy vertical_policy = Policy::as_needed) noexcept;
64
71 explicit ScrolledView(Frame& parent, const Rect& rect,
72 Policy horizontal_policy = Policy::as_needed,
73 Policy vertical_policy = Policy::as_needed) noexcept;
74
80 explicit ScrolledView(Frame& parent,
81 Policy horizontal_policy = Policy::as_needed,
82 Policy vertical_policy = Policy::as_needed) noexcept;
83
87 explicit ScrolledView(Serializer::Properties& props) noexcept
88 : ScrolledView(props, false)
89 {
90 }
91
92protected:
93
94 explicit ScrolledView(Serializer::Properties& props, bool is_derived) noexcept;
95
96public:
97
98 void handle(Event& event) override;
99
100 void draw(Painter& painter, const Rect& rect) override;
101
102 void resize(const Size& size) override;
103
104 void layout() override;
105
111 EGT_NODISCARD Point offset() const { return m_offset; }
112
118 void offset(Point offset);
119
125 EGT_NODISCARD const Rect& offset_range() const { return m_offset_range; };
126
132 EGT_NODISCARD Point offset_min() const { return offset_range().bottom_right(); }
133
139 EGT_NODISCARD Point offset_max() const { return offset_range().top_left(); }
140
144 int hoffset() const { return m_offset.x(); }
145
151 void hoffset(int offset)
152 {
153 this->offset(Point(offset, m_offset.y()));
154 }
155
159 int voffset() const { return m_offset.y(); }
160
166 void voffset(int offset)
167 {
168 this->offset(Point(m_offset.x(), offset));
169 }
170
174 EGT_NODISCARD Policy hpolicy() const { return m_horizontal_policy; }
175
179 void hpolicy(Policy policy)
180 {
181 if (detail::change_if_diff<>(m_horizontal_policy, policy))
182 layout();
183 }
184
188 EGT_NODISCARD Policy vpolicy() const { return m_vertical_policy; }
189
193 void vpolicy(Policy policy)
194 {
195 if (detail::change_if_diff<>(m_vertical_policy, policy))
196 layout();
197 }
198
202 EGT_NODISCARD DefaultDim slider_dim() const { return m_slider_dim; }
203
208 {
209 if (detail::change_if_diff<>(m_slider_dim, dim))
210 damage();
211 }
212
216 void serialize(Serializer& serializer) const override;
217
222
223 static std::string policy2str(Policy policy);
224 static Policy str2policy(const std::string& str);
225
226protected:
227
228 bool internal_drag() const override { return true; }
229
230 void damage_from_subordinate(const Rect& rect) override
231 {
232 damage(rect + m_offset);
233 }
234
235 Point point_from_subordinate(const Widget& subordinate) const override;
236
238 EGT_NODISCARD bool hscrollable() const
239 {
240 return m_hscrollable;
241 }
242
244 EGT_NODISCARD bool vscrollable() const
245 {
246 return m_vscrollable;
247 }
248
251 {
252 const auto& r = offset_range();
253
254 m_hscrollable = (m_horizontal_policy == Policy::always) ||
255 (m_horizontal_policy == Policy::as_needed && r.width() > 0);
256 m_vscrollable = (m_vertical_policy == Policy::always) ||
257 (m_vertical_policy == Policy::as_needed && r.height() > 0);
258 }
259
262
265
267 EGT_NODISCARD Rect super_rect() const;
268
271
274
276 bool m_hscrollable{false};
277
279 bool m_vscrollable{false};
280
283
286
289
292
295
297 Policy m_horizontal_policy{Policy::as_needed};
298
300 Policy m_vertical_policy{Policy::as_needed};
301
303 DefaultDim m_slider_dim{8};
304};
305
306}
307}
308
309#endif
A Frame is a Widget that has children widgets.
Definition frame.h:45
Drawing interface for 2D graphics.
Definition painter.h:45
A scrollable view.
Definition view.h:36
static std::string policy2str(Policy policy)
int hoffset() const
Get the horizontal offset.
Definition view.h:144
Slider m_vslider
Vertical slider shown when scrollable.
Definition view.h:291
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:166
void slider_dim(DefaultDim dim)
Set the slider dimension.
Definition view.h:207
void damage_from_subordinate(const Rect &rect) override
Special variation of damage() that is to be called explicitly by subordinate widgets.
Definition view.h:230
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:132
int voffset() const
Get the vertical offset.
Definition view.h:159
Point m_start_offset
Starting offset for the drag.
Definition view.h:294
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:193
EGT_NODISCARD Policy hpolicy() const
Get the horizontal policy.
Definition view.h:174
EGT_NODISCARD const Rect & offset_range() const
Get the offset range currently possible.
Definition view.h:125
EGT_NODISCARD bool hscrollable() const
Horizontal scrollable.
Definition view.h:238
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:288
EGT_NODISCARD bool vscrollable() const
Vertical scrollable.
Definition view.h:244
void resize(const Size &size) override
Resize the widget.
bool internal_drag() const override
Definition view.h:228
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:87
Point point_from_subordinate(const Widget &subordinate) const override
Policy
Scrollbar policy.
Definition view.h:43
EGT_NODISCARD DefaultDim slider_dim() const
Get the slider dimension.
Definition view.h:202
void handle(Event &event) override
Handle an event.
EGT_NODISCARD Policy vpolicy() const
Get the vertical policy.
Definition view.h:188
Point m_offset
Current offset of the view.
Definition view.h:282
EGT_NODISCARD Point offset() const
Get the current offset.
Definition view.h:111
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:139
void hoffset(int offset)
Set the horizontal offset.
Definition view.h:151
void update_scrollable()
Update scrollable settings based on current size.
Definition view.h:250
void hpolicy(Policy policy)
Set the horizontal policy.
Definition view.h:179
Rect m_offset_range
Current offset range.
Definition view.h:285
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:475
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