TGUI  0.10-beta
ScrollablePanel.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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
26#ifndef TGUI_SCROLLABLE_PANEL_HPP
27#define TGUI_SCROLLABLE_PANEL_HPP
28
29#include <TGUI/CopiedSharedPtr.hpp>
30#include <TGUI/Widgets/Panel.hpp>
31#include <TGUI/Widgets/Scrollbar.hpp>
32#include <TGUI/Renderers/ScrollablePanelRenderer.hpp>
33
35
36namespace tgui
37{
41 class TGUI_API ScrollablePanel : public Panel
42 {
43 public:
44
45 typedef std::shared_ptr<ScrollablePanel> Ptr;
46 typedef std::shared_ptr<const ScrollablePanel> ConstPtr;
47
55 ScrollablePanel(const char* typeName = "ScrollablePanel", bool initRenderer = true);
56
57
62
63
68
69
73 ScrollablePanel& operator= (const ScrollablePanel& other);
74
75
79 ScrollablePanel& operator= (ScrollablePanel&& other);
80
81
92 static ScrollablePanel::Ptr create(Layout2d size = {"100%", "100%"}, Vector2f contentSize = {0, 0});
93
94
103
104
110 const ScrollablePanelRenderer* getSharedRenderer() const;
111
118 const ScrollablePanelRenderer* getRenderer() const;
119
120
126 void setSize(const Layout2d& size) override;
127 using Widget::setSize;
128
129
134 Vector2f getInnerSize() const override;
135
136
142
143
150 void add(const Widget::Ptr& widget, const String& widgetName = "") override;
151
152
160 bool remove(const Widget::Ptr& widget) override;
161
162
166 void removeAllWidgets() override;
167
168
179
180
189
190
197
198
203 float getScrollbarWidth() const;
204
205
211
212
218
219
225
226
232
233
239 void setVerticalScrollAmount(unsigned int scrollAmount);
240
241
247 unsigned int getVerticalScrollAmount() const;
248
249
255 void setHorizontalScrollAmount(unsigned int scrollAmount);
256
257
263 unsigned int getHorizontalScrollAmount() const;
264
265
271 void setVerticalScrollbarValue(unsigned int value);
272
273
279 unsigned int getVerticalScrollbarValue() const;
280
281
287 void setHorizontalScrollbarValue(unsigned int value);
288
289
295 unsigned int getHorizontalScrollbarValue() const;
296
297
306
307
311 void leftMousePressed(Vector2f pos) override;
312
316 void leftMouseReleased(Vector2f pos) override;
317
321 void mouseMoved(Vector2f pos) override;
322
326 bool mouseWheelScrolled(float delta, Vector2f pos) override;
327
331 void mouseNoLongerOnWidget() override;
332
336 void leftMouseButtonNoLongerDown() override;
337
338
341 // Shows the tool tip when the widget is located below the mouse.
342 // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
343 // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
345 Widget::Ptr askToolTip(Vector2f mousePos) override;
346
347
354 void draw(BackendRenderTarget& target, RenderStates states) const override;
355
356
358 protected:
359
366 void rendererChanged(const String& property) override;
367
368
372 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
373
374
378 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
379
380
382 // Makes a copy of the widget
384 Widget::Ptr clone() const override;
385
386
388 private:
389
391 // Update the position, size and value of the scrollbars
393 void updateScrollbars();
394
395
397 // Find out what the most right and bottom positions are that are in use by the child widgets
399 void recalculateMostBottomRightPosition();
400
401
403 // Connect the position and size events to recalculate the bottom right position when a widget is updated
405 void connectPositionAndSize(Widget::Ptr widget);
406
407
409 // Disconnect the position and size events that were connected to keep track of widget changes
411 void disconnectAllChildWidgets();
412
413
415 private:
416
417 Vector2f m_contentSize;
418 Vector2f m_mostBottomRightPosition;
419 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
420 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
421
422 Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
423 Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Automatic;
424
425 unsigned int m_verticalScrollAmount = 0;
426 unsigned int m_horizontalScrollAmount = 0;
427
428 std::unordered_map<Widget::Ptr, unsigned int> m_connectedPositionCallbacks;
429 std::unordered_map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;
430
432 };
433
435}
436
438
439#endif // TGUI_SCROLLABLE_PANEL_HPP
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Definition: CopiedSharedPtr.hpp:40
Class to store the position or size of a widget.
Definition: Layout.hpp:284
Group of widgets that has a background color and optional borders.
Definition: Panel.hpp:41
Definition: ScrollablePanelRenderer.hpp:37
Group of widgets that has a background color and optional borders.
Definition: ScrollablePanel.hpp:42
std::shared_ptr< ScrollablePanel > Ptr
Shared widget pointer.
Definition: ScrollablePanel.hpp:45
Widget::Ptr getWidgetAtPosition(Vector2f pos) const override
Returns the leaf child widget that is located at the given position.
Vector2f getContentSize() const
Returns the size available for child widgets.
ScrollablePanelRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Vector2f getAbsolutePosition() const override
void setContentSize(Vector2f size)
Channges the size available for child widgets.
ScrollablePanelRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Scrollbar::Policy getHorizontalScrollbarPolicy() const
Returns when the horizontal scrollbar should be displayed.
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.
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.
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...
Vector2f getContentOffset() const
Returns the amount of pixels the child widgets have been shifted to be displayed by the scrollable pa...
std::shared_ptr< const ScrollablePanel > ConstPtr
Shared constant widget pointer.
Definition: ScrollablePanel.hpp:46
bool remove(const Widget::Ptr &widget) override
Removes a single widget that was added to the container.
void add(const Widget::Ptr &widget, const String &widgetName="") override
Adds a widget at the end of the layout.
ScrollablePanel(const ScrollablePanel &copy)
Copy constructor.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
ScrollablePanel(ScrollablePanel &&copy)
Move constructor.
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.
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.
static ScrollablePanel::Ptr create(Layout2d size={"100%", "100%"}, Vector2f contentSize={0, 0})
Creates a new scrollable panel widget.
static ScrollablePanel::Ptr copy(ScrollablePanel::ConstPtr panel)
Makes a copy of another scrollbable panel.
Vector2f getInnerSize() const override
Returns the space available for widgets inside the container.
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.
Policy
Defines when the scrollbar shows up.
Definition: Scrollbar.hpp:50
@ Automatic
Show the scrollbar only when needed (default)
Wrapper class to store strings.
Definition: String.hpp:79
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
States used for drawing.
Definition: RenderStates.hpp:39