WidgetScroll.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #ifndef RMLUI_CORE_WIDGETSCROLL_H
  29. #define RMLUI_CORE_WIDGETSCROLL_H
  30. #include "../../Include/RmlUi/Core/EventListener.h"
  31. namespace Rml {
  32. class Element;
  33. enum class ScrollBehavior;
  34. /**
  35. A widget for incorporating scrolling functionality into an element.
  36. @author Peter Curry
  37. */
  38. class WidgetScroll final : public EventListener {
  39. public:
  40. enum Orientation { UNKNOWN, VERTICAL, HORIZONTAL };
  41. WidgetScroll(Element* parent);
  42. virtual ~WidgetScroll();
  43. /// Initialises the slider to a given orientation.
  44. bool Initialise(Orientation orientation);
  45. /// Updates the key repeats for the increment / decrement arrows.
  46. void Update();
  47. /// Sets the position of the bar.
  48. /// @param[in] bar_position The new position of the bar (0 representing the start of the track, 1 representing the end).
  49. void SetBarPosition(float bar_position);
  50. /// Returns the current position of the bar.
  51. /// @return The current position of the bar (0 representing the start of the track, 1 representing the end).
  52. float GetBarPosition() const;
  53. /// Returns the slider's orientation.
  54. /// @return The orientation of the slider.
  55. Orientation GetOrientation() const;
  56. /// Sets the dimensions to the size of the slider.
  57. /// @param[in] dimensions The dimensions to size.
  58. void GetDimensions(Vector2f& dimensions) const;
  59. /// Sets the length of the entire track in scrollable units (usually lines or characters). This affects the
  60. /// length of the bar element and the speed of scrolling using the mouse-wheel or arrows.
  61. /// @param[in] track_length The length of the track.
  62. void SetTrackLength(float track_length);
  63. /// Sets the length the bar represents in scrollable units (usually lines or characters), relative to the track
  64. /// length. For example, for a scroll bar, this would represent how big each visible 'page' is compared to the
  65. /// whole document (which would be set as the track length).
  66. /// @param[in] bar_length The length of the slider's bar.
  67. void SetBarLength(float bar_length);
  68. /// Lays out and resizes the internal elements.
  69. /// @param[in] containing_block The padded box containing the slider. This is used to resolve relative properties.
  70. /// @param[in] slider_length The total length, in pixels, of the slider widget.
  71. void FormatElements(Vector2f containing_block, float slider_length);
  72. private:
  73. /// Handles events coming through from the slider's components.
  74. void ProcessEvent(Event& event) override;
  75. /// Lays out and positions the bar element.
  76. void FormatBar();
  77. // Set the offset on 'bar' based on its position.
  78. void PositionBar();
  79. void ScrollLineDown();
  80. void ScrollLineUp();
  81. void ScrollPageDown();
  82. void ScrollPageUp();
  83. // Scrolls the parent element by the given distance.
  84. void Scroll(float distance, ScrollBehavior behavior);
  85. Element* parent;
  86. Orientation orientation;
  87. // The background track element, across which the bar slides.
  88. Element* track;
  89. // The bar element. This is the element that is dragged across the trough.
  90. Element* bar;
  91. // The two (optional) buttons for incrementing and decrementing the slider.
  92. Element* arrows[2];
  93. // A number from 0 to 1, indicating how far along the track the bar is.
  94. float bar_position;
  95. // If the bar is being dragged, this is the pixel offset from the start of the bar to where it was picked up.
  96. float bar_drag_anchor;
  97. // Set to the auto-repeat timer if either of the arrow buttons have been pressed, -1 if they haven't.
  98. float arrow_timers[2];
  99. double last_update_time;
  100. float track_length;
  101. float bar_length;
  102. };
  103. } // namespace Rml
  104. #endif