WidgetScroll.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 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. /**
  34. A widget for incorporating scrolling functionality into an element.
  35. @author Peter Curry
  36. */
  37. class WidgetScroll final : public EventListener
  38. {
  39. public:
  40. enum Orientation
  41. {
  42. UNKNOWN,
  43. VERTICAL,
  44. HORIZONTAL
  45. };
  46. WidgetScroll(Element* parent);
  47. virtual ~WidgetScroll();
  48. /// Initialises the slider to a given orientation.
  49. bool Initialise(Orientation orientation);
  50. /// Updates the key repeats for the increment / decrement arrows.
  51. void Update();
  52. /// Sets the position of the bar.
  53. /// @param[in] bar_position The new position of the bar (0 representing the start of the track, 1 representing the end).
  54. void SetBarPosition(float bar_position);
  55. /// Returns the current position of the bar.
  56. /// @return The current position of the bar (0 representing the start of the track, 1 representing the end).
  57. float GetBarPosition() const;
  58. /// Returns the slider's orientation.
  59. /// @return The orientation of the slider.
  60. Orientation GetOrientation() const;
  61. /// Sets the dimensions to the size of the slider.
  62. /// @param[in] dimensions The dimensions to size.
  63. void GetDimensions(Vector2f& dimensions) const;
  64. /// Sets the length of the entire track in scrollable units (usually lines or characters). This affects the
  65. /// length of the bar element and the speed of scrolling using the mouse-wheel or arrows.
  66. /// @param[in] track_length The length of the track.
  67. /// @param[in] force_resize True to resize the bar immediately, false to wait until the next format.
  68. void SetTrackLength(float track_length, bool force_resize = true);
  69. /// Sets the length the bar represents in scrollable units (usually lines or characters), relative to the track
  70. /// length. For example, for a scroll bar, this would represent how big each visible 'page' is compared to the
  71. /// whole document (which would be set as the track length).
  72. /// @param[in] bar_length The length of the slider's bar.
  73. /// @param[in] force_resize True to resize the bar immediately, false to wait until the next format.
  74. void SetBarLength(float bar_length, bool force_resize = true);
  75. /// Sets the line height of the parent element; this is used for scrolling speeds.
  76. /// @param[in] line_height The line height of the parent element.
  77. void SetLineHeight(float line_height);
  78. /// Lays out and resizes the internal elements.
  79. /// @param[in] containing_block The padded box containing the slider. This is used to resolve relative properties.
  80. /// @param[in] length The total length, in pixels, of the slider widget.
  81. void FormatElements(Vector2f containing_block, float slider_length);
  82. private:
  83. /// Handles events coming through from the slider's components.
  84. void ProcessEvent(Event& event) override;
  85. /// Lays out and resizes the slider's internal elements.
  86. /// @param[in] containing_block The padded box containing the slider. This is used to resolve relative properties.
  87. /// @param[in] resize_element True to resize the parent slider element, false to only resize its components.
  88. /// @param[in] slider_length The total length, in pixels, of the slider widget.
  89. /// @param[in] bar_length The total length of the bar, as a proportion of the track length. If this is -1, the intrinsic length will be used.
  90. void FormatElements(Vector2f containing_block, bool resize_element, float slider_length, float bar_length = -1);
  91. /// Lays out and positions the bar element.
  92. /// @param[in] bar_length The total length of the bar, as a proportion of the track length. If this is -1, the intrinsic length will be used.
  93. void FormatBar(float bar_length = -1);
  94. // Set the offset on 'bar' from its position.
  95. void PositionBar();
  96. /// Called when the slider is incremented by one 'line', either by the down / right key or a mouse-click on the
  97. /// increment arrow.
  98. /// @return The new position of the bar.
  99. float OnLineIncrement();
  100. /// Called when the slider is decremented by one 'line', either by the up / left key or a mouse-click on the
  101. /// decrement arrow.
  102. /// @return The new position of the bar.
  103. float OnLineDecrement();
  104. /// Called when the slider is incremented by one 'page', either by the page-up key or a mouse-click on the
  105. /// track below / right of the bar.
  106. /// @return The new position of the bar.
  107. float OnPageIncrement();
  108. /// Called when the slider is incremented by one 'page', either by the page-down key or a mouse-click on the
  109. /// track above / left of the bar.
  110. /// @return The new position of the bar.
  111. float OnPageDecrement();
  112. // Returns the bar position after scrolling for a number of pixels.
  113. float Scroll(float distance);
  114. Element* parent;
  115. Orientation orientation;
  116. // The background track element, across which the bar slides.
  117. Element* track;
  118. // The bar element. This is the element that is dragged across the trough.
  119. Element* bar;
  120. // The two (optional) buttons for incrementing and decrementing the slider.
  121. Element* arrows[2];
  122. // A number from 0 to 1, indicating how far along the track the bar is.
  123. float bar_position;
  124. // If the bar is being dragged, this is the pixel offset from the start of the bar to where it was picked up.
  125. int bar_drag_anchor;
  126. // Set to the auto-repeat timer if either of the arrow buttons have been pressed, -1 if they haven't.
  127. float arrow_timers[2];
  128. double last_update_time;
  129. float track_length;
  130. float bar_length;
  131. float line_height;
  132. };
  133. } // namespace Rml
  134. #endif