TGUI 1.11
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#include <chrono>
33
35
36namespace tgui
37{
41 class TGUI_API Scrollbar : public Widget
42 {
43 public:
44
45 using Ptr = std::shared_ptr<Scrollbar>;
46 using ConstPtr = std::shared_ptr<const Scrollbar>;
47
48 static constexpr const char StaticWidgetType[] = "Scrollbar";
49
51 enum class Policy
52 {
56 };
57
65 Scrollbar(const char* typeName = StaticWidgetType, bool initRenderer = true);
66
72 TGUI_NODISCARD static Scrollbar::Ptr create(Orientation orientation = Orientation::Vertical);
73
81 TGUI_NODISCARD static Scrollbar::Ptr copy(const Scrollbar::ConstPtr& scrollbar);
82
87 TGUI_NODISCARD ScrollbarRenderer* getSharedRenderer() override;
88 TGUI_NODISCARD const ScrollbarRenderer* getSharedRenderer() const override;
89
95 TGUI_NODISCARD ScrollbarRenderer* getRenderer() override;
96
105 void setSize(const Layout2d& size) override;
106 using Widget::setSize;
107
116 void setMaximum(unsigned int maximum);
117
125 TGUI_NODISCARD unsigned int getMaximum() const;
126
134 void setValue(unsigned int value);
135
143 TGUI_NODISCARD unsigned int getValue() const;
144
157 void setViewportSize(unsigned int viewport);
158
163 TGUI_NODISCARD unsigned int getViewportSize() const;
164
172 TGUI_NODISCARD unsigned int getMaxValue() const;
173
179 void setScrollAmount(unsigned int scrollAmount);
180
186 TGUI_NODISCARD unsigned int getScrollAmount() const;
187
188#ifndef TGUI_REMOVE_DEPRECATED_CODE
196 TGUI_DEPRECATED("Use setPolicy instead") void setAutoHide(bool autoHide);
197
204 TGUI_DEPRECATED("Use getPolicy instead") TGUI_NODISCARD bool getAutoHide() const;
205#endif
206
212 void setPolicy(Policy policy);
213
219 TGUI_NODISCARD Scrollbar::Policy getPolicy() const;
220
235 TGUI_NODISCARD bool isShown() const;
236
243 TGUI_DEPRECATED("Use setOrientation instead") void setVerticalScroll(bool vertical);
244
249 TGUI_DEPRECATED("Use getOrientation instead") TGUI_NODISCARD bool getVerticalScroll() const;
250
259 void setOrientation(Orientation orientation);
260
266 TGUI_NODISCARD Orientation getOrientation() const;
267
274 TGUI_NODISCARD float getDefaultWidth() const;
275
282 TGUI_NODISCARD bool canGainFocus() const override;
283
289 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
290
294 bool leftMousePressed(Vector2f pos) override;
295
299 void leftMouseReleased(Vector2f pos) override;
300
304 void mouseMoved(Vector2f pos) override;
305
309 bool scrolled(float delta, Vector2f pos, bool touch) override;
310
314 void leftMouseButtonNoLongerDown() override;
315
322 void draw(BackendRenderTarget& target, RenderStates states) const override;
323
325 protected:
326
328 // Updates the scrollbar after a size change
330 void updateSize();
331
341 TGUI_NODISCARD Signal& getSignal(String signalName) override;
342
348 void rendererChanged(const String& property) override;
349
353 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
354
358 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
359
361 // Updates the position of the thumb based on the current value of the slider
363 void updateThumbPosition();
364
366 // Makes a copy of the widget
368 TGUI_NODISCARD Widget::Ptr clone() const override;
369
371 private:
372
374 // Schedules a callback to regularly change the value of the scrollbar as long as the mouse remains pressed on an arrow
376 void callMousePressPeriodically(std::chrono::time_point<std::chrono::steady_clock> clickedTime, bool repeatedCall);
377
379 public:
380
381 SignalUInt onValueChange = {"ValueChanged"};
382
384 protected:
385
386 enum class Part
387 {
388 None,
389 Track,
390 Thumb,
391 ArrowUp,
392 ArrowDown
393 };
394
395 // Keep track on which part of the scrollbar the mouse is standing
396 Part m_mouseHoverOverPart = Part::None;
397
398 // When the mouse went down, did it go down on top of the thumb? If so, where?
399 bool m_mouseDownOnThumb = false;
400 Vector2f m_mouseDownOnThumbPos;
401
402 unsigned int m_maximum = 10;
403 unsigned int m_value = 0;
404
405 // Maximum should be above this value before the scrollbar is needed
406 unsigned int m_viewportSize = 1;
407
408 Orientation m_orientation = Orientation::Vertical; // Is the scrollbar drawn horizontally or vertically?
409 Orientation m_imageOrientation = Orientation::Vertical; // Does the loaded image lie horizontally or vertically?
410 bool m_orientationLocked = false; // TGUI_NEXT: Remove property and make locked the default
411
412 // How far should the value change when pressing one of the arrows?
413 unsigned int m_scrollAmount = 1;
414
415 // Defines when the scrollbar should be visible or hidden
416 Scrollbar::Policy m_policy = Scrollbar::Policy::Automatic;
417
418 // Did the mouse went down on one of the arrows?
419 bool m_mouseDownOnIncreaseArrow = false;
420 bool m_mouseDownOnDecreaseArrow = false;
421 std::chrono::time_point<std::chrono::steady_clock> m_lastMousePressTime;
422
423 bool m_sizeSet = false; // Has setSize been called?
424
425 std::chrono::steady_clock::time_point m_lastSuccessfulScrollTime; // Timestamp of the last mouse wheel scroll event
426 Vector2f m_lastSuccessfulScrollPos; // Mouse position at the last mouse wheel scroll event
427
428 FloatRect m_track;
429 FloatRect m_thumb;
430 FloatRect m_arrowUp;
431 FloatRect m_arrowDown;
432
433 Sprite m_spriteTrack;
434 Sprite m_spriteTrackHover;
435 Sprite m_spriteThumb;
436 Sprite m_spriteThumbHover;
437 Sprite m_spriteArrowUp;
438 Sprite m_spriteArrowUpHover;
439 Sprite m_spriteArrowDown;
440 Sprite m_spriteArrowDownHover;
441
442 // Cached renderer properties
443 Color m_thumbColorCached;
444 Color m_thumbColorHoverCached;
445 Color m_trackColorCached;
446 Color m_trackColorHoverCached;
447 Color m_arrowColorCached;
448 Color m_arrowColorHoverCached;
449 Color m_arrowBackgroundColorCached;
450 Color m_arrowBackgroundColorHoverCached;
451
453 };
454
459 class TGUI_API ScrollbarChildWidget : public Scrollbar
460 {
461 public:
462
467 ScrollbarChildWidget(Orientation orientation = Orientation::Vertical); // TGUI_NEXT: No more default option
468
472 TGUI_NODISCARD bool isMouseDownOnThumb() const;
473
480 void draw(BackendRenderTarget& target, RenderStates states) const override;
481 };
482
487 class TGUI_API ScrollbarAccessor
488 {
489 public:
490
499 std::function<void()> valueChangedCallback,
500 std::function<void()> policyChangedCallback,
501 std::function<void()> scrollAmountChangedCallback);
502
508 void setValue(unsigned int value);
509
515 TGUI_NODISCARD unsigned int getValue() const;
516
522 void setScrollAmount(unsigned int scrollAmount);
523
528 TGUI_NODISCARD unsigned int getScrollAmount() const;
529
535
540 TGUI_NODISCARD Scrollbar::Policy getPolicy() const;
541
547 TGUI_NODISCARD unsigned int getMaximum() const;
548
554 TGUI_NODISCARD unsigned int getViewportSize() const;
555
561 TGUI_NODISCARD unsigned int getMaxValue() const;
562
568 TGUI_NODISCARD bool isShown() const;
569
579 TGUI_NODISCARD float getWidth() const;
580
581 private:
582 ScrollbarChildWidget* m_scrollbar;
583 std::function<void()> m_valueChangedCallback;
584 std::function<void()> m_policyChangedCallback;
585 std::function<void()> m_scrollAmountChangedCallback;
586 };
587
595 {
596 public:
597
602
607
612
616 virtual ~ScrollbarChildInterface() = default;
617
622
627
633
638 TGUI_NODISCARD const ScrollbarAccessor* getScrollbar() const;
639
641 protected:
642
646 virtual void scrollbarValueChanged();
647
652
657
661 void saveScrollbarPolicy(std::unique_ptr<DataIO::Node>& node) const;
662
666 void loadScrollbarPolicy(const std::unique_ptr<DataIO::Node>& node);
667
669 protected:
670
672 ScrollbarAccessor m_scrollbarAccessor;
673 };
674
682 {
683 public:
684
689
694
699
703 virtual ~DualScrollbarChildInterface() = default;
704
709
714
720
725 TGUI_NODISCARD const ScrollbarAccessor* getVerticalScrollbar() const;
726
732
737 TGUI_NODISCARD const ScrollbarAccessor* getHorizontalScrollbar() const;
738
740 protected:
741
748 virtual void scrollbarValueChanged(Orientation orientation);
749
756 virtual void scrollbarPolicyChanged(Orientation orientation);
757
764 virtual void scrollbarScrollAmountChanged(Orientation orientation);
765
769 void saveScrollbarPolicies(std::unique_ptr<DataIO::Node>& node) const;
770
774 void loadScrollbarPolicies(const std::unique_ptr<DataIO::Node>& node);
775
777 protected:
778
779 CopiedSharedPtr<ScrollbarChildWidget> m_verticalScrollbar;
780 CopiedSharedPtr<ScrollbarChildWidget> m_horizontalScrollbar;
781 ScrollbarAccessor m_verticalScrollbarAccessor;
782 ScrollbarAccessor m_horizontalScrollbarAccessor;
783 };
784
786
787}
788
790
791#endif // TGUI_SCROLLBAR_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Definition CopiedSharedPtr.hpp:40
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:321
Class returned by widgets that have a scrollbar to let the user access scrollbar properties.
Definition Scrollbar.hpp:488
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:460
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:45
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:52
@ Automatic
Show the scrollbar only when needed (default)
Definition Scrollbar.hpp:53
@ Always
Always show the scrollbar, even when the contents fits.
Definition Scrollbar.hpp:54
@ Never
Never show the scrollbar, even if the contents does not fit.
Definition Scrollbar.hpp:55
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:381
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:46
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:48
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: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:36
SignalTyped< unsigned int > SignalUInt
Signal with one "unsigned int" as optional unbound parameter.
Definition Signal.hpp:422
Orientation
Orientation of the object.
Definition Layout.hpp:50
@ Vertical
Vertical orientation.
Definition Layout.hpp:51
States used for drawing.
Definition RenderStates.hpp:38