TGUI 1.11
Loading...
Searching...
No Matches
ScrollablePanel.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_SCROLLABLE_PANEL_HPP
26#define TGUI_SCROLLABLE_PANEL_HPP
27
28#include <TGUI/Widgets/Panel.hpp>
29#include <TGUI/Widgets/Scrollbar.hpp>
30#include <TGUI/Renderers/ScrollablePanelRenderer.hpp>
31
32#include <chrono>
33
35
36namespace tgui
37{
41 class TGUI_API ScrollablePanel : public Panel, public DualScrollbarChildInterface
42 {
43 public:
44
45 using Ptr = std::shared_ptr<ScrollablePanel>;
46 using ConstPtr = std::shared_ptr<const ScrollablePanel>;
47
48 static constexpr const char StaticWidgetType[] = "ScrollablePanel";
49
57 ScrollablePanel(const char* typeName = StaticWidgetType, bool initRenderer = true);
58
62 ScrollablePanel(const ScrollablePanel& other);
63
67 ScrollablePanel(ScrollablePanel&& other) noexcept;
68
72 ScrollablePanel& operator= (const ScrollablePanel& other);
73
77 ScrollablePanel& operator= (ScrollablePanel&& other) noexcept;
78
89 TGUI_NODISCARD static ScrollablePanel::Ptr create(const Layout2d& size = {"100%", "100%"}, Vector2f contentSize = {0, 0});
90
98 TGUI_NODISCARD static ScrollablePanel::Ptr copy(const ScrollablePanel::ConstPtr& panel);
99
104 TGUI_NODISCARD ScrollablePanelRenderer* getSharedRenderer() override;
105 TGUI_NODISCARD const ScrollablePanelRenderer* getSharedRenderer() const override;
106
112 TGUI_NODISCARD ScrollablePanelRenderer* getRenderer() override;
113
119 void setSize(const Layout2d& size) override;
120 using Widget::setSize;
121
126 TGUI_NODISCARD Vector2f getInnerSize() const override;
127
132 TGUI_NODISCARD Vector2f getAbsolutePosition(Vector2f offset) const override;
133
145 void add(const Widget::Ptr& widget, const String& widgetName = "") override;
146
154 bool remove(const Widget::Ptr& widget) override;
155
159 void removeAllWidgets() override;
160
170 void setContentSize(Vector2f size);
171
179 TGUI_NODISCARD Vector2f getContentSize() const;
180
186 TGUI_NODISCARD Vector2f getContentOffset() const;
187
192 TGUI_DEPRECATED("Use getVerticalScrollbar()->getWidth() or getHorizontalScrollbar()->getWidth() instead") TGUI_NODISCARD float getScrollbarWidth() const;
193
198 TGUI_DEPRECATED("Use getVerticalScrollbar()->setPolicy(policy) instead") void setVerticalScrollbarPolicy(Scrollbar::Policy policy);
199
204 TGUI_DEPRECATED("Use getVerticalScrollbar()->getPolicy() instead") TGUI_NODISCARD Scrollbar::Policy getVerticalScrollbarPolicy() const;
205
210 TGUI_DEPRECATED("Use getHorizontalScrollbar()->setPolicy(policy) instead") void setHorizontalScrollbarPolicy(Scrollbar::Policy policy);
211
216 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getPolicy() instead") TGUI_NODISCARD Scrollbar::Policy getHorizontalScrollbarPolicy() const;
217
223 TGUI_DEPRECATED("Use getVerticalScrollbar()->setScrollAmount(scrollAmount) instead") void setVerticalScrollAmount(unsigned int scrollAmount);
224
230 TGUI_DEPRECATED("Use getVerticalScrollbar()->getScrollAmount() instead") TGUI_NODISCARD unsigned int getVerticalScrollAmount() const;
231
237 TGUI_DEPRECATED("Use getHorizontalScrollbar()->setScrollAmount(scrollAmount) instead") void setHorizontalScrollAmount(unsigned int scrollAmount);
238
244 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getScrollAmount() instead") TGUI_NODISCARD unsigned int getHorizontalScrollAmount() const;
245
251 TGUI_DEPRECATED("Use getVerticalScrollbar()->setValue(value) instead") void setVerticalScrollbarValue(unsigned int value);
252
258 TGUI_DEPRECATED("Use getVerticalScrollbar()->getValue() instead") TGUI_NODISCARD unsigned int getVerticalScrollbarValue() const;
259
267 TGUI_DEPRECATED("Use getVerticalScrollbar()->getMaxValue() instead") TGUI_NODISCARD unsigned int getVerticalScrollbarMaxValue() const;
268
274 TGUI_DEPRECATED("Use getHorizontalScrollbar()->setValue(value) instead") void setHorizontalScrollbarValue(unsigned int value);
275
281 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getValue() instead") TGUI_NODISCARD unsigned int getHorizontalScrollbarValue() const;
282
290 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getMaxValue() instead") TGUI_NODISCARD unsigned int getHorizontalScrollbarMaxValue() const;
291
297 TGUI_DEPRECATED("Use getVerticalScrollbar()->isShown() instead") TGUI_NODISCARD bool isVerticalScrollbarShown() const;
298
304 TGUI_DEPRECATED("Use getHorizontalScrollbar()->isShown() instead") TGUI_NODISCARD bool isHorizontalScrollbarShown() const;
305
318 TGUI_NODISCARD Widget::Ptr getWidgetAtPos(Vector2f pos, bool recursive) const override;
319
323 bool leftMousePressed(Vector2f pos) override;
324
328 void leftMouseReleased(Vector2f pos) override;
329
333 void rightMousePressed(Vector2f pos) override;
334
338 void rightMouseReleased(Vector2f pos) override;
339
343 void mouseMoved(Vector2f pos) override;
344
348 bool scrolled(float delta, Vector2f pos, bool touch) override;
349
353 void mouseNoLongerOnWidget() override;
354
358 void leftMouseButtonNoLongerDown() override;
359
362 // Shows the tool tip when the widget is located below the mouse.
363 // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
364 // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
366 TGUI_NODISCARD Widget::Ptr askToolTip(Vector2f mousePos) override;
367
374 void draw(BackendRenderTarget& target, RenderStates states) const override;
375
377 protected:
378
384 void rendererChanged(const String& property) override;
385
389 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
390
394 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
395
402 void scrollbarPolicyChanged(Orientation orientation) override;
403
410 void scrollbarScrollAmountChanged(Orientation orientation) override;
411
413 // Makes a copy of the widget
415 TGUI_NODISCARD Widget::Ptr clone() const override;
416
418 private:
419
421 // Update the position, size and value of the scrollbars
423 void updateScrollbars();
424
426 // Find out what the most right and bottom positions are that are in use by the child widgets
428 void recalculateMostBottomRightPosition();
429
431 // Connect the position and size events to recalculate the bottom right position when a widget is updated
433 void connectPositionAndSize(const Widget::Ptr& widget);
434
436 // Disconnect the position and size events that were connected to keep track of widget changes
438 void disconnectAllChildWidgets();
439
441 protected:
442
443 Vector2f m_contentSize;
444 Vector2f m_mostBottomRightPosition;
445
446 std::chrono::steady_clock::time_point m_lastSuccessfulScrollTime; // Timestamp of the last mouse wheel scroll event
447 Vector2f m_lastSuccessfulScrollPos; // Mouse position at the last mouse wheel scroll event
448
449 unsigned int m_verticalScrollAmount = 0;
450 unsigned int m_horizontalScrollAmount = 0;
451
452 bool m_verticalScrollbarWasVisibleOnSizeUpdate = false;
453 bool m_horizontalScrollbarWasVisibleOnSizeUpdate = false;
454 bool m_recalculatingSizeDuringUpdateScrollbars = false;
455 bool m_stuckInUpdateScrollbars = false;
456
457 std::unordered_map<Widget::Ptr, unsigned int> m_connectedPositionCallbacks;
458 std::unordered_map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;
459
461 };
462
464}
465
467
468#endif // TGUI_SCROLLABLE_PANEL_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
DualScrollbarChildInterface()
Default constructor.
Class to store the position or size of a widget.
Definition Layout.hpp:321
Definition ScrollablePanelRenderer.hpp:35
ScrollablePanelRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Vector2f getContentSize() const
Returns the size available for child widgets.
Widget::Ptr getWidgetAtPos(Vector2f pos, bool recursive) const override
Returns the widget that is located at the given position.
bool scrolled(float delta, Vector2f pos, bool touch) override
Called by the parent on scroll event (either from mouse wheel of from two finger scrolling on a touch...
void setContentSize(Vector2f size)
Channges the size available for child widgets.
static constexpr const char StaticWidgetType[]
Type name of the widget.
Definition ScrollablePanel.hpp:48
void scrollbarPolicyChanged(Orientation orientation) override
Called when the policy of one of the scrollbars has been changed calling either getVerticalScrollbar(...
bool isHorizontalScrollbarShown() const
Returns whether the horizontal scrollbar is currently visible.
unsigned int getVerticalScrollbarMaxValue() const
Returns the maximum thumb position of the vertical scrollbar.
Scrollbar::Policy getHorizontalScrollbarPolicy() const
Returns when the horizontal scrollbar should be displayed.
ScrollablePanel(const ScrollablePanel &other)
Copy constructor.
void setVerticalScrollAmount(unsigned int scrollAmount)
Changes how much the value changes when scrolling or pressing one of the arrows of the vertical scrol...
void removeAllWidgets() override
Removes all widgets that were added to the container.
void setHorizontalScrollAmount(unsigned int scrollAmount)
Changes how much the value changes when scrolling or pressing one of the arrows of the horizontal scr...
float getScrollbarWidth() const
Returns the width of the scrollbars.
bool isVerticalScrollbarShown() const
Returns whether the vertical scrollbar is currently visible.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
std::shared_ptr< const ScrollablePanel > ConstPtr
Shared constant widget pointer.
Definition ScrollablePanel.hpp:46
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
unsigned int getVerticalScrollAmount() const
Returns how much the value changes when scrolling or pressing one of the arrows of the vertical scrol...
void scrollbarScrollAmountChanged(Orientation orientation) override
Called when the scroll amount of one of the scrollbars has been changed calling either getVerticalScr...
Vector2f getContentOffset() const
Returns the amount of pixels the child widgets have been shifted to be displayed by the scrollable pa...
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
bool remove(const Widget::Ptr &widget) override
Removes a single widget that was added to the container.
ScrollablePanelRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void add(const Widget::Ptr &widget, const String &widgetName="") override
Adds a widget at the end of the layout.
unsigned int getHorizontalScrollbarMaxValue() const
Returns the maximum thumb position of the horizontal scrollbar.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
static ScrollablePanel::Ptr copy(const ScrollablePanel::ConstPtr &panel)
Makes a copy of another scrollable panel.
Vector2f getAbsolutePosition(Vector2f offset) const override
void setVerticalScrollbarValue(unsigned int value)
Changes the thumb position of the vertical scrollbar.
void setHorizontalScrollbarPolicy(Scrollbar::Policy policy)
Changes when the horizontal scrollbar should be displayed.
ScrollablePanel(ScrollablePanel &&other) noexcept
Move constructor.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
void setSize(const Layout2d &size) override
Changes the size of the panel.
unsigned int getHorizontalScrollAmount() const
Returns how much the value changes when scrolling or pressing one of the arrows of the horizontal scr...
void setHorizontalScrollbarValue(unsigned int value)
Changes the thumb position of the horizontal scrollbar.
Scrollbar::Policy getVerticalScrollbarPolicy() const
Returns when the vertical scrollbar should be displayed.
Vector2f getInnerSize() const override
Returns the space available for widgets inside the container.
std::shared_ptr< ScrollablePanel > Ptr
Shared widget pointer.
Definition ScrollablePanel.hpp:45
unsigned int getVerticalScrollbarValue() const
Returns the thumb position of the vertical scrollbar.
unsigned int getHorizontalScrollbarValue() const
Returns the thumb position of the horizontal scrollbar.
void setVerticalScrollbarPolicy(Scrollbar::Policy policy)
Changes when the vertical scrollbar should be displayed.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
static ScrollablePanel::Ptr create(const Layout2d &size={"100%", "100%"}, Vector2f contentSize={0, 0})
Creates a new scrollable panel widget.
Scrollbar widget.
Definition Scrollbar.hpp:42
Wrapper class to store strings.
Definition String.hpp:94
The parent class for every widget.
Definition Widget.hpp:84
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
Orientation
Orientation of the object.
Definition Layout.hpp:50
States used for drawing.
Definition RenderStates.hpp:38