TGUI  1.6.1
Loading...
Searching...
No Matches
ScrollablePanel.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
33 #include <chrono>
34#endif
35
37
38TGUI_MODULE_EXPORT namespace tgui
39{
43 class TGUI_API ScrollablePanel : public Panel, public DualScrollbarChildInterface
44 {
45 public:
46
47 using Ptr = std::shared_ptr<ScrollablePanel>;
48 using ConstPtr = std::shared_ptr<const ScrollablePanel>;
49
50 static constexpr const char StaticWidgetType[] = "ScrollablePanel";
51
59 ScrollablePanel(const char* typeName = StaticWidgetType, bool initRenderer = true);
60
65
70
74 ScrollablePanel& operator= (const ScrollablePanel& other);
75
79 ScrollablePanel& operator= (ScrollablePanel&& other) noexcept;
80
91 TGUI_NODISCARD static ScrollablePanel::Ptr create(const Layout2d& size = {"100%", "100%"}, Vector2f contentSize = {0, 0});
92
100 TGUI_NODISCARD static ScrollablePanel::Ptr copy(const ScrollablePanel::ConstPtr& panel);
101
106 TGUI_NODISCARD ScrollablePanelRenderer* getSharedRenderer() override;
107 TGUI_NODISCARD const ScrollablePanelRenderer* getSharedRenderer() const override;
108
114 TGUI_NODISCARD ScrollablePanelRenderer* getRenderer() override;
115
121 void setSize(const Layout2d& size) override;
122 using Widget::setSize;
123
128 TGUI_NODISCARD Vector2f getInnerSize() const override;
129
134 TGUI_NODISCARD Vector2f getAbsolutePosition(Vector2f offset) const override;
135
147 void add(const Widget::Ptr& widget, const String& widgetName = "") override;
148
156 bool remove(const Widget::Ptr& widget) override;
157
161 void removeAllWidgets() override;
162
173
181 TGUI_NODISCARD Vector2f getContentSize() const;
182
188 TGUI_NODISCARD Vector2f getContentOffset() const;
189
194 TGUI_DEPRECATED("Use getVerticalScrollbar()->getWidth() or getHorizontalScrollbar()->getWidth() instead") TGUI_NODISCARD float getScrollbarWidth() const;
195
200 TGUI_DEPRECATED("Use getVerticalScrollbar()->setPolicy(policy) instead") void setVerticalScrollbarPolicy(Scrollbar::Policy policy);
201
206 TGUI_DEPRECATED("Use getVerticalScrollbar()->getPolicy() instead") TGUI_NODISCARD Scrollbar::Policy getVerticalScrollbarPolicy() const;
207
212 TGUI_DEPRECATED("Use getHorizontalScrollbar()->setPolicy(policy) instead") void setHorizontalScrollbarPolicy(Scrollbar::Policy policy);
213
218 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getPolicy() instead") TGUI_NODISCARD Scrollbar::Policy getHorizontalScrollbarPolicy() const;
219
225 TGUI_DEPRECATED("Use getVerticalScrollbar()->setScrollAmount(scrollAmount) instead") void setVerticalScrollAmount(unsigned int scrollAmount);
226
232 TGUI_DEPRECATED("Use getVerticalScrollbar()->getScrollAmount() instead") TGUI_NODISCARD unsigned int getVerticalScrollAmount() const;
233
239 TGUI_DEPRECATED("Use getHorizontalScrollbar()->setScrollAmount(scrollAmount) instead") void setHorizontalScrollAmount(unsigned int scrollAmount);
240
246 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getScrollAmount() instead") TGUI_NODISCARD unsigned int getHorizontalScrollAmount() const;
247
253 TGUI_DEPRECATED("Use getVerticalScrollbar()->setValue(value) instead") void setVerticalScrollbarValue(unsigned int value);
254
260 TGUI_DEPRECATED("Use getVerticalScrollbar()->getValue() instead") TGUI_NODISCARD unsigned int getVerticalScrollbarValue() const;
261
269 TGUI_DEPRECATED("Use getVerticalScrollbar()->getMaxValue() instead") TGUI_NODISCARD unsigned int getVerticalScrollbarMaxValue() const;
270
276 TGUI_DEPRECATED("Use getHorizontalScrollbar()->setValue(value) instead") void setHorizontalScrollbarValue(unsigned int value);
277
283 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getValue() instead") TGUI_NODISCARD unsigned int getHorizontalScrollbarValue() const;
284
292 TGUI_DEPRECATED("Use getHorizontalScrollbar()->getMaxValue() instead") TGUI_NODISCARD unsigned int getHorizontalScrollbarMaxValue() const;
293
299 TGUI_DEPRECATED("Use getVerticalScrollbar()->isShown() instead") TGUI_NODISCARD bool isVerticalScrollbarShown() const;
300
306 TGUI_DEPRECATED("Use getHorizontalScrollbar()->isShown() instead") TGUI_NODISCARD bool isHorizontalScrollbarShown() const;
307
320 TGUI_NODISCARD Widget::Ptr getWidgetAtPos(Vector2f pos, bool recursive) const override;
321
325 bool leftMousePressed(Vector2f pos) override;
326
330 void leftMouseReleased(Vector2f pos) override;
331
335 void mouseMoved(Vector2f pos) override;
336
340 bool scrolled(float delta, Vector2f pos, bool touch) override;
341
345 void mouseNoLongerOnWidget() override;
346
350 void leftMouseButtonNoLongerDown() override;
351
354 // Shows the tool tip when the widget is located below the mouse.
355 // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
356 // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
358 TGUI_NODISCARD Widget::Ptr askToolTip(Vector2f mousePos) override;
359
366 void draw(BackendRenderTarget& target, RenderStates states) const override;
367
369 protected:
370
376 void rendererChanged(const String& property) override;
377
381 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
382
386 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
387
394 void scrollbarPolicyChanged(Orientation orientation) override;
395
402 void scrollbarScrollAmountChanged(Orientation orientation) override;
403
405 // Makes a copy of the widget
407 TGUI_NODISCARD Widget::Ptr clone() const override;
408
410 private:
411
413 // Update the position, size and value of the scrollbars
415 void updateScrollbars();
416
418 // Find out what the most right and bottom positions are that are in use by the child widgets
420 void recalculateMostBottomRightPosition();
421
423 // Connect the position and size events to recalculate the bottom right position when a widget is updated
425 void connectPositionAndSize(const Widget::Ptr& widget);
426
428 // Disconnect the position and size events that were connected to keep track of widget changes
430 void disconnectAllChildWidgets();
431
433 protected:
434
435 Vector2f m_contentSize;
436 Vector2f m_mostBottomRightPosition;
437
438 std::chrono::steady_clock::time_point m_lastSuccessfulScrollTime; // Timestamp of the last mouse wheel scroll event
439 Vector2f m_lastSuccessfulScrollPos; // Mouse position at the last mouse wheel scroll event
440
441 unsigned int m_verticalScrollAmount = 0;
442 unsigned int m_horizontalScrollAmount = 0;
443
444 bool m_verticalScrollbarWasVisibleOnSizeUpdate = false;
445 bool m_horizontalScrollbarWasVisibleOnSizeUpdate = false;
446 bool m_recalculatingSizeDuringUpdateScrollbars = false;
447 bool m_stuckInUpdateScrollbars = false;
448
449 std::unordered_map<Widget::Ptr, unsigned int> m_connectedPositionCallbacks;
450 std::unordered_map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;
451
453 };
454
456}
457
459
460#endif // TGUI_SCROLLABLE_PANEL_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Base class for widgets with both a vertical and horizontal scrollbar.
Definition Scrollbar.hpp:673
Class to store the position or size of a widget.
Definition Layout.hpp:323
Group of widgets that has a background color and optional borders.
Definition Panel.hpp:39
Definition ScrollablePanelRenderer.hpp:35
Group of widgets that has a background color and optional borders.
Definition ScrollablePanel.hpp:44
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.
void setContentSize(Vector2f size)
Channges the size available for child widgets.
ScrollablePanel(const ScrollablePanel &other)
Copy constructor.
void removeAllWidgets() override
Removes all widgets that were added to the container.
std::shared_ptr< const ScrollablePanel > ConstPtr
Shared constant widget pointer.
Definition ScrollablePanel.hpp:48
Vector2f getContentOffset() const
Returns the amount of pixels the child widgets have been shifted to be displayed by the scrollable pa...
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.
static ScrollablePanel::Ptr copy(const ScrollablePanel::ConstPtr &panel)
Makes a copy of another scrollable panel.
Vector2f getAbsolutePosition(Vector2f offset) const override
ScrollablePanel(ScrollablePanel &&other) noexcept
Move constructor.
void setSize(const Layout2d &size) override
Changes the size of the panel.
Vector2f getInnerSize() const override
Returns the space available for widgets inside the container.
std::shared_ptr< ScrollablePanel > Ptr
Shared widget pointer.
Definition ScrollablePanel.hpp:47
static ScrollablePanel::Ptr create(const Layout2d &size={"100%", "100%"}, Vector2f contentSize={0, 0})
Creates a new scrollable panel widget.
Scrollbar widget.
Definition Scrollbar.hpp:44
Wrapper class to store strings.
Definition String.hpp:96
The parent class for every widget.
Definition Widget.hpp:83
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Orientation
Orientation of the object.
Definition Layout.hpp:52
States used for drawing.
Definition RenderStates.hpp:38