TGUI  1.0-alpha
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) noexcept;
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
119
125 void setSize(const Layout2d& size) override;
126 using Widget::setSize;
127
128
133 Vector2f getInnerSize() const override;
134
135
141
142
149 void add(const Widget::Ptr& widget, const String& widgetName = "") override;
150
151
159 bool remove(const Widget::Ptr& widget) override;
160
161
165 void removeAllWidgets() override;
166
167
178
179
188
189
196
197
202 float getScrollbarWidth() const;
203
204
210
211
217
218
224
225
231
232
238 void setVerticalScrollAmount(unsigned int scrollAmount);
239
240
246 unsigned int getVerticalScrollAmount() const;
247
248
254 void setHorizontalScrollAmount(unsigned int scrollAmount);
255
256
262 unsigned int getHorizontalScrollAmount() const;
263
264
270 void setVerticalScrollbarValue(unsigned int value);
271
272
278 unsigned int getVerticalScrollbarValue() const;
279
280
286 void setHorizontalScrollbarValue(unsigned int value);
287
288
294 unsigned int getHorizontalScrollbarValue() const;
295
296
305
306
310 void leftMousePressed(Vector2f pos) override;
311
315 void leftMouseReleased(Vector2f pos) override;
316
320 void mouseMoved(Vector2f pos) override;
321
325 bool mouseWheelScrolled(float delta, Vector2f pos) override;
326
330 void mouseNoLongerOnWidget() override;
331
335 void leftMouseButtonNoLongerDown() override;
336
337
340 // Shows the tool tip when the widget is located below the mouse.
341 // Returns its tool tip or the tool tip from a child widget if the mouse is on top of the widget.
342 // A nullptr is returned when the mouse is not on top of the widget or when the tool tip is empty.
344 Widget::Ptr askToolTip(Vector2f mousePos) override;
345
346
353 void draw(BackendRenderTarget& target, RenderStates states) const override;
354
355
357 protected:
358
365 void rendererChanged(const String& property) override;
366
367
371 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
372
373
377 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
378
379
381 // Makes a copy of the widget
383 Widget::Ptr clone() const override;
384
385
387 private:
388
390 // Update the position, size and value of the scrollbars
392 void updateScrollbars();
393
394
396 // Find out what the most right and bottom positions are that are in use by the child widgets
398 void recalculateMostBottomRightPosition();
399
400
402 // Connect the position and size events to recalculate the bottom right position when a widget is updated
404 void connectPositionAndSize(Widget::Ptr widget);
405
406
408 // Disconnect the position and size events that were connected to keep track of widget changes
410 void disconnectAllChildWidgets();
411
412
414 private:
415
416 Vector2f m_contentSize;
417 Vector2f m_mostBottomRightPosition;
418 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
419 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
420
421 Scrollbar::Policy m_verticalScrollbarPolicy = Scrollbar::Policy::Automatic;
422 Scrollbar::Policy m_horizontalScrollbarPolicy = Scrollbar::Policy::Automatic;
423
424 unsigned int m_verticalScrollAmount = 0;
425 unsigned int m_horizontalScrollAmount = 0;
426
427 std::unordered_map<Widget::Ptr, unsigned int> m_connectedPositionCallbacks;
428 std::unordered_map<Widget::Ptr, unsigned int> m_connectedSizeCallbacks;
429
431 };
432
434}
435
437
438#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.
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.
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.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
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.
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