TGUI 1.13
Loading...
Searching...
No Matches
Scrollbar.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_SCROLLBAR_HPP
26#define TGUI_SCROLLBAR_HPP
27
28#include <TGUI/CopiedSharedPtr.hpp>
29#include <TGUI/Renderers/ScrollbarRenderer.hpp>
30#include <TGUI/Widget.hpp>
31
32#include <chrono>
33
35
36namespace tgui
37{
41 class TGUI_API Scrollbar : public Widget
42 {
43 public:
44 using Ptr = std::shared_ptr<Scrollbar>;
45 using ConstPtr = std::shared_ptr<const Scrollbar>;
46
47 static constexpr char StaticWidgetType[] = "Scrollbar";
48
50 enum class Policy : std::uint8_t
51 {
52 Automatic,
53 Always,
54 Never
55 };
56
64 explicit Scrollbar(const char* typeName = StaticWidgetType, bool initRenderer = true);
65
71 [[nodiscard]] static Scrollbar::Ptr create(Orientation orientation = Orientation::Vertical);
72
80 [[nodiscard]] static Scrollbar::Ptr copy(const Scrollbar::ConstPtr& scrollbar);
81
86 [[nodiscard]] ScrollbarRenderer* getSharedRenderer() override;
87 [[nodiscard]] const ScrollbarRenderer* getSharedRenderer() const override;
88
94 [[nodiscard]] ScrollbarRenderer* getRenderer() override;
95
104 void setSize(const Layout2d& size) override;
105 using Widget::setSize;
106
115 void setMaximum(unsigned int maximum);
116
124 [[nodiscard]] unsigned int getMaximum() const;
125
133 void setValue(unsigned int value);
134
142 [[nodiscard]] unsigned int getValue() const;
143
156 void setViewportSize(unsigned int viewport);
157
162 [[nodiscard]] unsigned int getViewportSize() const;
163
171 [[nodiscard]] unsigned int getMaxValue() const;
172
178 void setScrollAmount(unsigned int scrollAmount);
179
185 [[nodiscard]] unsigned int getScrollAmount() const;
186
187#ifndef TGUI_REMOVE_DEPRECATED_CODE
195 TGUI_DEPRECATED("Use setPolicy instead") void setAutoHide(bool autoHide);
196
203 TGUI_DEPRECATED("Use getPolicy instead") [[nodiscard]] bool getAutoHide() const;
204#endif
205
211 void setPolicy(Policy policy);
212
218 [[nodiscard]] Scrollbar::Policy getPolicy() const;
219
234 [[nodiscard]] bool isShown() const;
235
242 TGUI_DEPRECATED("Use setOrientation instead") void setVerticalScroll(bool vertical);
243
248 TGUI_DEPRECATED("Use getOrientation instead") [[nodiscard]] bool getVerticalScroll() const;
249
258 void setOrientation(Orientation orientation);
259
265 [[nodiscard]] Orientation getOrientation() const;
266
273 [[nodiscard]] float getDefaultWidth() const;
274
281 [[nodiscard]] bool canGainFocus() const override;
282
288 [[nodiscard]] bool isMouseOnWidget(Vector2f pos) const override;
289
293 bool leftMousePressed(Vector2f pos) override;
294
298 void leftMouseReleased(Vector2f pos) override;
299
303 void mouseMoved(Vector2f pos) override;
304
308 bool scrolled(float delta, Vector2f pos, bool touch) override;
309
313 void leftMouseButtonNoLongerDown() override;
314
321 void draw(BackendRenderTarget& target, RenderStates states) const override;
322
324
325 protected:
327 // Updates the scrollbar after a size change
329 void updateSize();
330
340 [[nodiscard]] Signal& getSignal(String signalName) override;
341
347 void rendererChanged(const String& property) override;
348
352 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
353
357 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
358
360 // Updates the position of the thumb based on the current value of the slider
362 void updateThumbPosition();
363
365 // Makes a copy of the widget
367 [[nodiscard]] Widget::Ptr clone() const override;
368
370
371 private:
373 // Schedules a callback to regularly change the value of the scrollbar as long as the mouse remains pressed on an arrow
375 void callMousePressPeriodically(std::chrono::time_point<std::chrono::steady_clock> clickedTime, bool repeatedCall);
376
378
379 public:
380 SignalUInt onValueChange = {"ValueChanged"};
381
383
384 protected:
385 enum class Part : std::uint8_t
386 {
387 None,
388 Track,
389 Thumb,
390 ArrowUp,
391 ArrowDown
392 };
393
394 // Keep track on which part of the scrollbar the mouse is standing
395 Part m_mouseHoverOverPart = Part::None;
396
397 // When the mouse went down, did it go down on top of the thumb? If so, where?
398 bool m_mouseDownOnThumb = false;
399 Vector2f m_mouseDownOnThumbPos;
400
401 unsigned int m_maximum = 10;
402 unsigned int m_value = 0;
403
404 // Maximum should be above this value before the scrollbar is needed
405 unsigned int m_viewportSize = 1;
406
407 Orientation m_orientation = Orientation::Vertical; // Is the scrollbar drawn horizontally or vertically?
408 Orientation m_imageOrientation = Orientation::Vertical; // Does the loaded image lie horizontally or vertically?
409 bool m_orientationLocked = false; // TGUI_NEXT: Remove property and make locked the default
410
411 // How far should the value change when pressing one of the arrows?
412 unsigned int m_scrollAmount = 1;
413
414 // Defines when the scrollbar should be visible or hidden
415 Scrollbar::Policy m_policy = Scrollbar::Policy::Automatic;
416
417 // Did the mouse went down on one of the arrows?
418 bool m_mouseDownOnIncreaseArrow = false;
419 bool m_mouseDownOnDecreaseArrow = false;
420 std::chrono::time_point<std::chrono::steady_clock> m_lastMousePressTime;
421
422 bool m_sizeSet = false; // Has setSize been called?
423
424 std::chrono::steady_clock::time_point m_lastSuccessfulScrollTime; // Timestamp of the last mouse wheel scroll event
425 Vector2f m_lastSuccessfulScrollPos; // Mouse position at the last mouse wheel scroll event
426
427 FloatRect m_track;
428 FloatRect m_thumb;
429 FloatRect m_arrowUp;
430 FloatRect m_arrowDown;
431
432 Sprite m_spriteTrack;
433 Sprite m_spriteTrackHover;
434 Sprite m_spriteThumb;
435 Sprite m_spriteThumbHover;
436 Sprite m_spriteArrowUp;
437 Sprite m_spriteArrowUpHover;
438 Sprite m_spriteArrowDown;
439 Sprite m_spriteArrowDownHover;
440
441 // Cached renderer properties
442 Color m_thumbColorCached;
443 Color m_thumbColorHoverCached;
444 Color m_trackColorCached;
445 Color m_trackColorHoverCached;
446 Color m_arrowColorCached;
447 Color m_arrowColorHoverCached;
448 Color m_arrowBackgroundColorCached;
449 Color m_arrowBackgroundColorHoverCached;
450
452 };
453
458 class TGUI_API ScrollbarChildWidget : public Scrollbar
459 {
460 public:
465 explicit ScrollbarChildWidget(Orientation orientation = Orientation::Vertical); // TGUI_NEXT: No more default option
466
470 [[nodiscard]] bool isMouseDownOnThumb() const;
471
478 void draw(BackendRenderTarget& target, RenderStates states) const override;
479 };
480
485 class TGUI_API ScrollbarAccessor
486 {
487 public:
496 std::function<void()> valueChangedCallback,
497 std::function<void()> policyChangedCallback,
498 std::function<void()> scrollAmountChangedCallback);
499
505 void setValue(unsigned int value);
506
512 [[nodiscard]] unsigned int getValue() const;
513
519 void setScrollAmount(unsigned int scrollAmount);
520
525 [[nodiscard]] unsigned int getScrollAmount() const;
526
532
537 [[nodiscard]] Scrollbar::Policy getPolicy() const;
538
544 [[nodiscard]] unsigned int getMaximum() const;
545
551 [[nodiscard]] unsigned int getViewportSize() const;
552
558 [[nodiscard]] unsigned int getMaxValue() const;
559
565 [[nodiscard]] bool isShown() const;
566
576 [[nodiscard]] float getWidth() const;
577
578 private:
579 ScrollbarChildWidget* m_scrollbar;
580 std::function<void()> m_valueChangedCallback;
581 std::function<void()> m_policyChangedCallback;
582 std::function<void()> m_scrollAmountChangedCallback;
583 };
584
592 {
593 public:
598
603
608
612 virtual ~ScrollbarChildInterface() = default;
613
618
623
629
634 [[nodiscard]] const ScrollbarAccessor* getScrollbar() const;
635
637
638 protected:
642 virtual void scrollbarValueChanged();
643
648
653
657 void saveScrollbarPolicy(std::unique_ptr<DataIO::Node>& node) const;
658
662 void loadScrollbarPolicy(const std::unique_ptr<DataIO::Node>& node);
663
665
666 protected:
668 ScrollbarAccessor m_scrollbarAccessor;
669 };
670
678 {
679 public:
684
689
694
698 virtual ~DualScrollbarChildInterface() = default;
699
704
709
715
720 [[nodiscard]] const ScrollbarAccessor* getVerticalScrollbar() const;
721
727
732 [[nodiscard]] const ScrollbarAccessor* getHorizontalScrollbar() const;
733
735
736 protected:
743 virtual void scrollbarValueChanged(Orientation orientation);
744
751 virtual void scrollbarPolicyChanged(Orientation orientation);
752
759 virtual void scrollbarScrollAmountChanged(Orientation orientation);
760
764 void saveScrollbarPolicies(std::unique_ptr<DataIO::Node>& node) const;
765
769 void loadScrollbarPolicies(const std::unique_ptr<DataIO::Node>& node);
770
772
773 protected:
774 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
775 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
776 ScrollbarAccessor m_verticalScrollbarAccessor;
777 ScrollbarAccessor m_horizontalScrollbarAccessor;
778 };
779} // namespace tgui
780
781#endif // TGUI_SCROLLBAR_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Definition CopiedSharedPtr.hpp:40
DualScrollbarChildInterface & operator=(const DualScrollbarChildInterface &other)
Overload of copy assignment operator.
virtual ~DualScrollbarChildInterface()=default
Destructor.
virtual void scrollbarPolicyChanged(Orientation orientation)
Called when the policy of one of the scrollbars has been changed calling either getVerticalScrollbar(...
virtual void scrollbarScrollAmountChanged(Orientation orientation)
Called when the scroll amount of one of the scrollbars has been changed calling either getVerticalScr...
DualScrollbarChildInterface(const DualScrollbarChildInterface &other)
Copy constructor.
DualScrollbarChildInterface & operator=(DualScrollbarChildInterface &&other) noexcept
Move assignment.
void loadScrollbarPolicies(const std::unique_ptr< DataIO::Node > &node)
This function should be called inside the load function in order to load the wanted scrollbar policie...
DualScrollbarChildInterface()
Default constructor.
const ScrollbarAccessor * getHorizontalScrollbar() const
Returns an object that provides access to the widget's horizontal scrollbar.
virtual void scrollbarValueChanged(Orientation orientation)
Called when the value of one of the scrollbars has been changed by calling either getVerticalScrollba...
ScrollbarAccessor * getVerticalScrollbar()
Returns an object that provides access to the widget's vertical scrollbar.
ScrollbarAccessor * getHorizontalScrollbar()
Returns an object that provides access to the widget's horizontal scrollbar.
const ScrollbarAccessor * getVerticalScrollbar() const
Returns an object that provides access to the widget's vertical scrollbar.
DualScrollbarChildInterface(DualScrollbarChildInterface &&other) noexcept
Move constructor.
void saveScrollbarPolicies(std::unique_ptr< DataIO::Node > &node) const
This function should be called inside the save function in order to save the configured scrollbar pol...
Class to store the position or size of a widget.
Definition Layout.hpp:320
Class returned by widgets that have a scrollbar to let the user access scrollbar properties.
Definition Scrollbar.hpp:486
void setScrollAmount(unsigned int scrollAmount)
Changes how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
void setPolicy(Scrollbar::Policy policy)
Changes when the scrollbar should be displayed.
unsigned int getMaxValue() const
Returns the current maximum value of the scrollbar.
float getWidth() const
Returns the width of the scrollbar.
unsigned int getMaximum() const
Returns the current maximum of the scrollbar.
unsigned int getViewportSize() const
Returns the current viewport size of the scrollbar.
void setValue(unsigned int value)
Changes the current value of the scrollbar.
Scrollbar::Policy getPolicy() const
Returns when the scrollbar should be displayed.
bool isShown() const
Returns whether the scrollbar is currently visible.
unsigned int getScrollAmount() const
Returns how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
ScrollbarAccessor(ScrollbarChildWidget &scrollbar, std::function< void()> valueChangedCallback, std::function< void()> policyChangedCallback, std::function< void()> scrollAmountChangedCallback)
Constructor.
unsigned int getValue() const
Returns the current value of the scrollbar.
void loadScrollbarPolicy(const std::unique_ptr< DataIO::Node > &node)
This function should be called inside the load function in order to load the wanted scrollbar policy.
ScrollbarChildInterface(ScrollbarChildInterface &&other) noexcept
Move constructor.
ScrollbarChildInterface & operator=(ScrollbarChildInterface &&other) noexcept
Move assignment.
const ScrollbarAccessor * getScrollbar() const
Returns an object that provides access to the widget's scrollbar.
ScrollbarChildInterface(const ScrollbarChildInterface &other)
Copy constructor.
ScrollbarChildInterface()
Default constructor.
virtual void scrollbarPolicyChanged()
Called when the policy of the scrollbar has been changed via getScrollbar()->setPolicy(....
virtual void scrollbarValueChanged()
Called when the value of the scrollbar has been changed via getScrollbar()->setValue(....
ScrollbarAccessor * getScrollbar()
Returns an object that provides access to the widget's scrollbar.
ScrollbarChildInterface & operator=(const ScrollbarChildInterface &other)
Overload of copy assignment operator.
virtual void scrollbarScrollAmountChanged()
Called when the scroll amount of the scrollbar has been changed via getScrollbar()->setScrollAmount(....
virtual ~ScrollbarChildInterface()=default
Destructor.
void saveScrollbarPolicy(std::unique_ptr< DataIO::Node > &node) const
This function should be called inside the save function in order to save the configured scrollbar pol...
Wrapper around scrollbar to be used inside widgets that need a scrollbar.
Definition Scrollbar.hpp:459
ScrollbarChildWidget(Orientation orientation=Orientation::Vertical)
Default constructor.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
bool isMouseDownOnThumb() const
Returns whether the left mouse button has been pressed on top of the thumb of the scrollbar.
Definition ScrollbarRenderer.hpp:35
Scrollbar widget.
Definition Scrollbar.hpp:42
void setPolicy(Policy policy)
Changes when the scrollbar should be displayed.
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition Scrollbar.hpp:44
unsigned int getScrollAmount() const
Returns how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
float getDefaultWidth() const
Returns the default width of the scrollbar.
static Scrollbar::Ptr create(Orientation orientation=Orientation::Vertical)
Creates a new scrollbar widget.
Scrollbar::Policy getPolicy() const
Returns when the scrollbar should be displayed.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
unsigned int getMaximum() const
Returns the maximum value.
void setValue(unsigned int value)
Changes the current value.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
bool getVerticalScroll() const
Returns whether the scrollbar lies horizontally or vertically.
void setOrientation(Orientation orientation)
Changes whether the scrollbar lies horizontally or vertically.
Orientation getOrientation() const
Returns whether the scrollbar lies horizontally or vertically.
unsigned int getMaxValue() const
Returns the maximum value that can be set with the setValue function.
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...
bool canGainFocus() const override
Returns whether the widget can gain focus.
void setMaximum(unsigned int maximum)
Sets a maximum value.
void setSize(const Layout2d &size) override
Changes the size of the scrollbar.
void setViewportSize(unsigned int viewport)
Changes the viewport size.
SignalUInt onValueChange
Value of the scrollbar changed. Optional parameter: new value.
Definition Scrollbar.hpp:380
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.
static Scrollbar::Ptr copy(const Scrollbar::ConstPtr &scrollbar)
Makes a copy of another scrollbar.
unsigned int getViewportSize() const
Returns the viewport size.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
Policy
Defines when the scrollbar shows up.
Definition Scrollbar.hpp:51
unsigned int getValue() const
Returns the current value.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
ScrollbarRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setVerticalScroll(bool vertical)
Changes whether the scrollbar lies horizontally or vertically.
void setScrollAmount(unsigned int scrollAmount)
Changes how much the value changes when scrolling or pressing one of the arrows of the scrollbar.
std::shared_ptr< const Scrollbar > ConstPtr
Shared constant widget pointer.
Definition Scrollbar.hpp:45
void setAutoHide(bool autoHide)
Changes whether the scrollbar should hide automatically or not.
bool isShown() const
Returns whether the scrollbar is currently visible.
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition Scrollbar.hpp:47
ScrollbarRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
bool getAutoHide() const
Returns whether the scrollbar is hiding automatically or not.
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:59
Wrapper class to store strings.
Definition String.hpp:94
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
SignalTyped< unsigned int > SignalUInt
Signal with one "unsigned int" as optional unbound parameter.
Definition Signal.hpp:409
Orientation
Orientation of the object.
Definition Layout.hpp:50
@ Vertical
Vertical orientation.
Definition Layout.hpp:51
States used for drawing.
Definition RenderStates.hpp:38