TGUI 1.10
Loading...
Searching...
No Matches
Scrollbar.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 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/Widget.hpp>
29#include <TGUI/Renderers/ScrollbarRenderer.hpp>
30#include <TGUI/CopiedSharedPtr.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 Scrollbar : public Widget
44 {
45 public:
46
47 using Ptr = std::shared_ptr<Scrollbar>;
48 using ConstPtr = std::shared_ptr<const Scrollbar>;
49
50 static constexpr const char StaticWidgetType[] = "Scrollbar";
51
53 enum class Policy
54 {
58 };
59
67 Scrollbar(const char* typeName = StaticWidgetType, bool initRenderer = true);
68
74 TGUI_NODISCARD static Scrollbar::Ptr create(Orientation orientation = Orientation::Vertical);
75
83 TGUI_NODISCARD static Scrollbar::Ptr copy(const Scrollbar::ConstPtr& scrollbar);
84
89 TGUI_NODISCARD ScrollbarRenderer* getSharedRenderer() override;
90 TGUI_NODISCARD const ScrollbarRenderer* getSharedRenderer() const override;
91
97 TGUI_NODISCARD ScrollbarRenderer* getRenderer() override;
98
107 void setSize(const Layout2d& size) override;
108 using Widget::setSize;
109
118 void setMaximum(unsigned int maximum);
119
127 TGUI_NODISCARD unsigned int getMaximum() const;
128
136 void setValue(unsigned int value);
137
145 TGUI_NODISCARD unsigned int getValue() const;
146
159 void setViewportSize(unsigned int viewport);
160
165 TGUI_NODISCARD unsigned int getViewportSize() const;
166
174 TGUI_NODISCARD unsigned int getMaxValue() const;
175
181 void setScrollAmount(unsigned int scrollAmount);
182
188 TGUI_NODISCARD unsigned int getScrollAmount() const;
189
190#ifndef TGUI_REMOVE_DEPRECATED_CODE
198 TGUI_DEPRECATED("Use setPolicy instead") void setAutoHide(bool autoHide);
199
206 TGUI_DEPRECATED("Use getPolicy instead") TGUI_NODISCARD bool getAutoHide() const;
207#endif
208
214 void setPolicy(Policy policy);
215
221 TGUI_NODISCARD Scrollbar::Policy getPolicy() const;
222
237 TGUI_NODISCARD bool isShown() const;
238
245 TGUI_DEPRECATED("Use setOrientation instead") void setVerticalScroll(bool vertical);
246
251 TGUI_DEPRECATED("Use getOrientation instead") TGUI_NODISCARD bool getVerticalScroll() const;
252
261 void setOrientation(Orientation orientation);
262
268 TGUI_NODISCARD Orientation getOrientation() const;
269
276 TGUI_NODISCARD float getDefaultWidth() const;
277
284 TGUI_NODISCARD bool canGainFocus() const override;
285
291 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
292
296 bool leftMousePressed(Vector2f pos) override;
297
301 void leftMouseReleased(Vector2f pos) override;
302
306 void mouseMoved(Vector2f pos) override;
307
311 bool scrolled(float delta, Vector2f pos, bool touch) override;
312
316 void leftMouseButtonNoLongerDown() override;
317
324 void draw(BackendRenderTarget& target, RenderStates states) const override;
325
327 protected:
328
330 // Updates the scrollbar after a size change
332 void updateSize();
333
343 TGUI_NODISCARD Signal& getSignal(String signalName) override;
344
350 void rendererChanged(const String& property) override;
351
355 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
356
360 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
361
363 // Updates the position of the thumb based on the current value of the slider
365 void updateThumbPosition();
366
368 // Makes a copy of the widget
370 TGUI_NODISCARD Widget::Ptr clone() const override;
371
373 private:
374
376 // Schedules a callback to regularly change the value of the scrollbar as long as the mouse remains pressed on an arrow
378 void callMousePressPeriodically(std::chrono::time_point<std::chrono::steady_clock> clickedTime, bool repeatedCall);
379
381 public:
382
383 SignalUInt onValueChange = {"ValueChanged"};
384
386 protected:
387
388 enum class Part
389 {
390 None,
391 Track,
392 Thumb,
393 ArrowUp,
394 ArrowDown
395 };
396
397 // Keep track on which part of the scrollbar the mouse is standing
398 Part m_mouseHoverOverPart = Part::None;
399
400 // When the mouse went down, did it go down on top of the thumb? If so, where?
401 bool m_mouseDownOnThumb = false;
402 Vector2f m_mouseDownOnThumbPos;
403
404 unsigned int m_maximum = 10;
405 unsigned int m_value = 0;
406
407 // Maximum should be above this value before the scrollbar is needed
408 unsigned int m_viewportSize = 1;
409
410 Orientation m_orientation = Orientation::Vertical; // Is the scrollbar drawn horizontally or vertically?
411 Orientation m_imageOrientation = Orientation::Vertical; // Does the loaded image lie horizontally or vertically?
412 bool m_orientationLocked = false; // TGUI_NEXT: Remove property and make locked the default
413
414 // How far should the value change when pressing one of the arrows?
415 unsigned int m_scrollAmount = 1;
416
417 // Defines when the scrollbar should be visible or hidden
418 Scrollbar::Policy m_policy = Scrollbar::Policy::Automatic;
419
420 // Did the mouse went down on one of the arrows?
421 bool m_mouseDownOnIncreaseArrow = false;
422 bool m_mouseDownOnDecreaseArrow = false;
423 std::chrono::time_point<std::chrono::steady_clock> m_lastMousePressTime;
424
425 bool m_sizeSet = false; // Has setSize been called?
426
427 std::chrono::steady_clock::time_point m_lastSuccessfulScrollTime; // Timestamp of the last mouse wheel scroll event
428 Vector2f m_lastSuccessfulScrollPos; // Mouse position at the last mouse wheel scroll event
429
430 FloatRect m_track;
431 FloatRect m_thumb;
432 FloatRect m_arrowUp;
433 FloatRect m_arrowDown;
434
435 Sprite m_spriteTrack;
436 Sprite m_spriteTrackHover;
437 Sprite m_spriteThumb;
438 Sprite m_spriteThumbHover;
439 Sprite m_spriteArrowUp;
440 Sprite m_spriteArrowUpHover;
441 Sprite m_spriteArrowDown;
442 Sprite m_spriteArrowDownHover;
443
444 // Cached renderer properties
445 Color m_thumbColorCached;
446 Color m_thumbColorHoverCached;
447 Color m_trackColorCached;
448 Color m_trackColorHoverCached;
449 Color m_arrowColorCached;
450 Color m_arrowColorHoverCached;
451 Color m_arrowBackgroundColorCached;
452 Color m_arrowBackgroundColorHoverCached;
453
455 };
456
461 class TGUI_API ScrollbarChildWidget : public Scrollbar
462 {
463 public:
464
469 ScrollbarChildWidget(Orientation orientation = Orientation::Vertical); // TGUI_NEXT: No more default option
470
474 TGUI_NODISCARD bool isMouseDownOnThumb() const;
475
482 void draw(BackendRenderTarget& target, RenderStates states) const override;
483 };
484
489 class TGUI_API ScrollbarAccessor
490 {
491 public:
492
501 std::function<void()> valueChangedCallback,
502 std::function<void()> policyChangedCallback,
503 std::function<void()> scrollAmountChangedCallback);
504
510 void setValue(unsigned int value);
511
517 TGUI_NODISCARD unsigned int getValue() const;
518
524 void setScrollAmount(unsigned int scrollAmount);
525
530 TGUI_NODISCARD unsigned int getScrollAmount() const;
531
537
542 TGUI_NODISCARD Scrollbar::Policy getPolicy() const;
543
549 TGUI_NODISCARD unsigned int getMaximum() const;
550
556 TGUI_NODISCARD unsigned int getViewportSize() const;
557
563 TGUI_NODISCARD unsigned int getMaxValue() const;
564
570 TGUI_NODISCARD bool isShown() const;
571
581 TGUI_NODISCARD float getWidth() const;
582
583 private:
584 ScrollbarChildWidget* m_scrollbar;
585 std::function<void()> m_valueChangedCallback;
586 std::function<void()> m_policyChangedCallback;
587 std::function<void()> m_scrollAmountChangedCallback;
588 };
589
597 {
598 public:
599
604
609
614
618 virtual ~ScrollbarChildInterface() = default;
619
624
629
635
640 TGUI_NODISCARD const ScrollbarAccessor* getScrollbar() const;
641
643 protected:
644
648 virtual void scrollbarValueChanged();
649
654
659
663 void saveScrollbarPolicy(std::unique_ptr<DataIO::Node>& node) const;
664
668 void loadScrollbarPolicy(const std::unique_ptr<DataIO::Node>& node);
669
671 protected:
672
674 ScrollbarAccessor m_scrollbarAccessor;
675 };
676
684 {
685 public:
686
691
696
701
705 virtual ~DualScrollbarChildInterface() = default;
706
711
716
722
727 TGUI_NODISCARD const ScrollbarAccessor* getVerticalScrollbar() const;
728
734
739 TGUI_NODISCARD const ScrollbarAccessor* getHorizontalScrollbar() const;
740
742 protected:
743
750 virtual void scrollbarValueChanged(Orientation orientation);
751
758 virtual void scrollbarPolicyChanged(Orientation orientation);
759
766 virtual void scrollbarScrollAmountChanged(Orientation orientation);
767
771 void saveScrollbarPolicies(std::unique_ptr<DataIO::Node>& node) const;
772
776 void loadScrollbarPolicies(const std::unique_ptr<DataIO::Node>& node);
777
779 protected:
780
781 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
782 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
783 ScrollbarAccessor m_verticalScrollbarAccessor;
784 ScrollbarAccessor m_horizontalScrollbarAccessor;
785 };
786
788
789}
790
792
793#endif // TGUI_SCROLLBAR_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Definition CopiedSharedPtr.hpp:42
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 &)
Copy constructor.
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.
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.
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...
DualScrollbarChildInterface(DualScrollbarChildInterface &&) noexcept
Move constructor.
Class to store the position or size of a widget.
Definition Layout.hpp:323
Class returned by widgets that have a scrollbar to let the user access scrollbar properties.
Definition Scrollbar.hpp:490
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 &&) noexcept
Move 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(const ScrollbarChildInterface &)
Copy constructor.
virtual void scrollbarScrollAmountChanged()
Called when the scroll amount of the scrollbar has been changed via getScrollbar()->setScrollAmount(....
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:462
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:44
void setPolicy(Policy policy)
Changes when the scrollbar should be displayed.
std::shared_ptr< Scrollbar > Ptr
Shared widget pointer.
Definition Scrollbar.hpp:47
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.
Policy
Defines when the scrollbar shows up.
Definition Scrollbar.hpp:54
@ Automatic
Show the scrollbar only when needed (default)
Definition Scrollbar.hpp:55
@ Always
Always show the scrollbar, even when the contents fits.
Definition Scrollbar.hpp:56
@ Never
Never show the scrollbar, even if the contents does not fit.
Definition Scrollbar.hpp:57
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:383
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...
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:48
void setAutoHide(bool autoHide)
Changes whether the scrollbar should hide automatically or not.
static constexpr const char StaticWidgetType[]
Type name of the widget.
Definition Scrollbar.hpp:50
bool isShown() const
Returns whether the scrollbar is currently visible.
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:61
Wrapper class to store strings.
Definition String.hpp:96
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
SignalTyped< unsigned int > SignalUInt
Signal with one "unsigned int" as optional unbound parameter.
Definition Signal.hpp:424
Orientation
Orientation of the object.
Definition Layout.hpp:52
@ Vertical
Vertical orientation.
Definition Layout.hpp:53
States used for drawing.
Definition RenderStates.hpp:38