TGUI 1.13
Loading...
Searching...
No Matches
ScrollablePanel.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 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/Renderers/ScrollablePanelRenderer.hpp>
29#include <TGUI/Widgets/Panel.hpp>
30#include <TGUI/Widgets/Scrollbar.hpp>
31
32#include <chrono>
33
35
36namespace tgui
37{
41 class TGUI_API ScrollablePanel : public Panel, public DualScrollbarChildInterface
42 {
43 public:
44 using Ptr = std::shared_ptr<ScrollablePanel>;
45 using ConstPtr = std::shared_ptr<const ScrollablePanel>;
46
47 static constexpr char StaticWidgetType[] = "ScrollablePanel";
48
56 explicit ScrollablePanel(const char* typeName = StaticWidgetType, bool initRenderer = true);
57
61 ScrollablePanel(const ScrollablePanel& other);
62
66 ScrollablePanel(ScrollablePanel&& other) noexcept;
67
71 ScrollablePanel& operator=(const ScrollablePanel& other);
72
76 ScrollablePanel& operator=(ScrollablePanel&& other) noexcept;
77
88 [[nodiscard]] static ScrollablePanel::Ptr create(const Layout2d& size = {"100%", "100%"}, Vector2f contentSize = {0, 0});
89
97 [[nodiscard]] static ScrollablePanel::Ptr copy(const ScrollablePanel::ConstPtr& panel);
98
104 [[nodiscard]] const ScrollablePanelRenderer* getSharedRenderer() const override;
105
111 [[nodiscard]] ScrollablePanelRenderer* getRenderer() override;
112
118 void setSize(const Layout2d& size) override;
119 using Widget::setSize;
120
125 [[nodiscard]] Vector2f getInnerSize() const override;
126
131 [[nodiscard]] Vector2f getAbsolutePosition(Vector2f offset) const override;
132
144 void add(const Widget::Ptr& widget, const String& widgetName = "") override;
145
153 bool remove(const Widget::Ptr& widget) override;
154
158 void removeAllWidgets() override;
159
169 void setContentSize(Vector2f size);
170
178 [[nodiscard]] Vector2f getContentSize() const;
179
185 [[nodiscard]] Vector2f getContentOffset() const;
186
191 TGUI_DEPRECATED("Use getVerticalScrollbar()->getWidth() or getHorizontalScrollbar()->getWidth() instead")
192 [[nodiscard]] float getScrollbarWidth() const;
193
198 TGUI_DEPRECATED("Use getVerticalScrollbar()->setPolicy(policy) instead")
200
205 TGUI_DEPRECATED("Use getVerticalScrollbar()->getPolicy() instead")
206 [[nodiscard]] Scrollbar::Policy getVerticalScrollbarPolicy() const;
207
212 TGUI_DEPRECATED("Use getHorizontalScrollbar()->setPolicy(policy) instead")
214
219 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getPolicy() instead")
220 [[nodiscard]] Scrollbar::Policy getHorizontalScrollbarPolicy() const;
221
227 TGUI_DEPRECATED("Use getVerticalScrollbar()->setScrollAmount(scrollAmount) instead")
228 void setVerticalScrollAmount(unsigned int scrollAmount);
229
235 TGUI_DEPRECATED("Use getVerticalScrollbar()->getScrollAmount() instead")
236 [[nodiscard]] unsigned int getVerticalScrollAmount() const;
237
243 TGUI_DEPRECATED("Use getHorizontalScrollbar()->setScrollAmount(scrollAmount) instead")
244 void setHorizontalScrollAmount(unsigned int scrollAmount);
245
251 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getScrollAmount() instead")
252 [[nodiscard]] unsigned int getHorizontalScrollAmount() const;
253
259 TGUI_DEPRECATED("Use getVerticalScrollbar()->setValue(value) instead") void setVerticalScrollbarValue(unsigned int value);
260
266 TGUI_DEPRECATED("Use getVerticalScrollbar()->getValue() instead")
267 [[nodiscard]] unsigned int getVerticalScrollbarValue() const;
268
276 TGUI_DEPRECATED("Use getVerticalScrollbar()->getMaxValue() instead")
277 [[nodiscard]] unsigned int getVerticalScrollbarMaxValue() const;
278
284 TGUI_DEPRECATED("Use getHorizontalScrollbar()->setValue(value) instead")
285 void setHorizontalScrollbarValue(unsigned int value);
286
292 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getValue() instead")
293 [[nodiscard]] unsigned int getHorizontalScrollbarValue() const;
294
302 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getMaxValue() instead")
303 [[nodiscard]] unsigned int getHorizontalScrollbarMaxValue() const;
304
310 TGUI_DEPRECATED("Use getVerticalScrollbar()->isShown() instead") [[nodiscard]] bool isVerticalScrollbarShown() const;
311
317 TGUI_DEPRECATED("Use getHorizontalScrollbar()->isShown() instead") [[nodiscard]] bool isHorizontalScrollbarShown() const;
318
331 [[nodiscard]] Widget::Ptr getWidgetAtPos(Vector2f pos, bool recursive) const override;
332
336 bool leftMousePressed(Vector2f pos) override;
337
341 void leftMouseReleased(Vector2f pos) override;
342
346 void rightMousePressed(Vector2f pos) override;
347
351 void rightMouseReleased(Vector2f pos) override;
352
356 void mouseMoved(Vector2f pos) override;
357
361 bool scrolled(float delta, Vector2f pos, bool touch) override;
362
366 void mouseNoLongerOnWidget() override;
367
371 void leftMouseButtonNoLongerDown() override;
372
375 // Shows the tool tip when the widget is located below the mouse.
376 // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
377 // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
379 [[nodiscard]] Widget::Ptr askToolTip(Vector2f mousePos) override;
380
387 void draw(BackendRenderTarget& target, RenderStates states) const override;
388
390
391 protected:
397 void rendererChanged(const String& property) override;
398
402 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
403
407 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
408
415 void scrollbarPolicyChanged(Orientation orientation) override;
416
423 void scrollbarScrollAmountChanged(Orientation orientation) override;
424
426 // Makes a copy of the widget
428 [[nodiscard]] Widget::Ptr clone() const override;
429
431
432 private:
434 // Update the position, size and value of the scrollbars
436 void updateScrollbars();
437
439 // Find out what the most right and bottom positions are that are in use by the child widgets
441 void recalculateMostBottomRightPosition();
442
444 // Connect the position and size events to recalculate the bottom right position when a widget is updated
446 void connectPositionAndSize(const Widget::Ptr& widget);
447
449 // Disconnect the position and size events that were connected to keep track of widget changes
451 void disconnectAllChildWidgets();
452
454
455 protected:
456 Vector2f m_contentSize;
457 Vector2f m_mostBottomRightPosition;
458
459 std::chrono::steady_clock::time_point m_lastSuccessfulScrollTime; // Timestamp of the last mouse wheel scroll event
460 Vector2f m_lastSuccessfulScrollPos; // Mouse position at the last mouse wheel scroll event
461
462 unsigned int m_verticalScrollAmount = 0;
463 unsigned int m_horizontalScrollAmount = 0;
464
465 bool m_verticalScrollbarWasVisibleOnSizeUpdate = false;
466 bool m_horizontalScrollbarWasVisibleOnSizeUpdate = false;
467 bool m_recalculatingSizeDuringUpdateScrollbars = false;
468 bool m_stuckInUpdateScrollbars = false;
469
470 std::unordered_map<Widget::Ptr, unsigned int> m_connectedPositionCallbacks;
471 std::unordered_map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;
472 };
473} // namespace tgui
474
475#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:320
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.
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.
ScrollablePanel & operator=(ScrollablePanel &&other) noexcept
Overload of move assignment operator.
bool isVerticalScrollbarShown() const
Returns whether the vertical scrollbar is currently visible.
ScrollablePanel & operator=(const ScrollablePanel &other)
Overload of assignment operator.
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:45
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.
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition ScrollablePanel.hpp:47
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:44
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:83
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:85
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
Orientation
Orientation of the object.
Definition Layout.hpp:50
States used for drawing.
Definition RenderStates.hpp:38