ElementScroll.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #include "../../Include/RmlUi/Core/ElementScroll.h"
  29. #include "../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../Include/RmlUi/Core/Context.h"
  31. #include "../../Include/RmlUi/Core/Element.h"
  32. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  33. #include "../../Include/RmlUi/Core/Event.h"
  34. #include "../../Include/RmlUi/Core/Factory.h"
  35. #include "Layout/LayoutDetails.h"
  36. #include "WidgetScroll.h"
  37. namespace Rml {
  38. ElementScroll::ElementScroll(Element* _element)
  39. {
  40. element = _element;
  41. corner = nullptr;
  42. }
  43. ElementScroll::~ElementScroll() {}
  44. void ElementScroll::Update()
  45. {
  46. for (int i = 0; i < 2; i++)
  47. {
  48. if (scrollbars[i].widget != nullptr)
  49. scrollbars[i].widget->Update();
  50. }
  51. }
  52. void ElementScroll::EnableScrollbar(Orientation orientation, float element_width)
  53. {
  54. if (!scrollbars[orientation].enabled)
  55. {
  56. CreateScrollbar(orientation);
  57. scrollbars[orientation].element->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Visible));
  58. scrollbars[orientation].enabled = true;
  59. }
  60. // Determine the size of the scrollbar.
  61. Box box;
  62. LayoutDetails::BuildBox(box, Vector2f(element_width, element_width), scrollbars[orientation].element);
  63. if (orientation == VERTICAL)
  64. scrollbars[orientation].size = box.GetSize(BoxArea::Margin).x;
  65. if (orientation == HORIZONTAL)
  66. {
  67. if (box.GetSize().y < 0)
  68. scrollbars[orientation].size = box.GetCumulativeEdge(BoxArea::Content, BoxEdge::Left) +
  69. box.GetCumulativeEdge(BoxArea::Content, BoxEdge::Right) +
  70. ResolveValue(scrollbars[orientation].element->GetComputedValues().height(), element_width);
  71. else
  72. scrollbars[orientation].size = box.GetSize(BoxArea::Margin).y;
  73. }
  74. }
  75. void ElementScroll::DisableScrollbar(Orientation orientation)
  76. {
  77. if (scrollbars[orientation].enabled)
  78. {
  79. scrollbars[orientation].element->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  80. scrollbars[orientation].enabled = false;
  81. if (corner)
  82. corner->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  83. }
  84. }
  85. void ElementScroll::UpdateScrollbar(Orientation orientation)
  86. {
  87. float bar_position;
  88. float traversable_track;
  89. if (orientation == VERTICAL)
  90. {
  91. bar_position = element->GetScrollTop();
  92. traversable_track = element->GetScrollHeight() - element->GetClientHeight();
  93. }
  94. else
  95. {
  96. bar_position = element->GetScrollLeft();
  97. traversable_track = element->GetScrollWidth() - element->GetClientWidth();
  98. }
  99. if (traversable_track <= 0)
  100. bar_position = 0;
  101. else
  102. bar_position /= traversable_track;
  103. if (scrollbars[orientation].widget != nullptr)
  104. {
  105. bar_position = Math::Clamp(bar_position, 0.0f, 1.0f);
  106. if (scrollbars[orientation].widget->GetBarPosition() != bar_position)
  107. scrollbars[orientation].widget->SetBarPosition(bar_position);
  108. }
  109. }
  110. Element* ElementScroll::GetScrollbar(Orientation orientation)
  111. {
  112. return scrollbars[orientation].element;
  113. }
  114. float ElementScroll::GetScrollbarSize(Orientation orientation)
  115. {
  116. if (!scrollbars[orientation].enabled)
  117. return 0;
  118. return scrollbars[orientation].size;
  119. }
  120. void ElementScroll::FormatScrollbars()
  121. {
  122. const Box& element_box = element->GetBox();
  123. const Vector2f containing_block = element_box.GetSize(BoxArea::Padding);
  124. for (int i = 0; i < 2; i++)
  125. {
  126. if (!scrollbars[i].enabled)
  127. continue;
  128. if (i == VERTICAL)
  129. {
  130. scrollbars[i].widget->SetBarLength(element->GetClientHeight());
  131. scrollbars[i].widget->SetTrackLength(element->GetScrollHeight());
  132. float traversable_track = element->GetScrollHeight() - element->GetClientHeight();
  133. if (traversable_track > 0)
  134. scrollbars[i].widget->SetBarPosition(element->GetScrollTop() / traversable_track);
  135. else
  136. scrollbars[i].widget->SetBarPosition(0);
  137. }
  138. else
  139. {
  140. scrollbars[i].widget->SetBarLength(element->GetClientWidth());
  141. scrollbars[i].widget->SetTrackLength(element->GetScrollWidth());
  142. float traversable_track = element->GetScrollWidth() - element->GetClientWidth();
  143. if (traversable_track > 0)
  144. scrollbars[i].widget->SetBarPosition(element->GetScrollLeft() / traversable_track);
  145. else
  146. scrollbars[i].widget->SetBarPosition(0);
  147. }
  148. float slider_length = containing_block[1 - i];
  149. float user_scrollbar_margin = scrollbars[i].element->GetComputedValues().scrollbar_margin();
  150. float min_scrollbar_margin = GetScrollbarSize(i == VERTICAL ? HORIZONTAL : VERTICAL);
  151. slider_length -= Math::Max(user_scrollbar_margin, min_scrollbar_margin);
  152. scrollbars[i].widget->FormatElements(containing_block, slider_length);
  153. int variable_axis = i == VERTICAL ? 0 : 1;
  154. Vector2f offset = element_box.GetPosition(BoxArea::Padding);
  155. offset[variable_axis] += containing_block[variable_axis] -
  156. (scrollbars[i].element->GetBox().GetSize(BoxArea::Border)[variable_axis] +
  157. scrollbars[i].element->GetBox().GetEdge(BoxArea::Margin, i == VERTICAL ? BoxEdge::Right : BoxEdge::Bottom));
  158. // Add the top or left margin (as appropriate) onto the scrollbar's position.
  159. offset[1 - variable_axis] += scrollbars[i].element->GetBox().GetEdge(BoxArea::Margin, i == VERTICAL ? BoxEdge::Top : BoxEdge::Left);
  160. scrollbars[i].element->SetOffset(offset, element, true);
  161. }
  162. // Format the corner, if it is necessary.
  163. if (scrollbars[0].enabled && scrollbars[1].enabled)
  164. {
  165. CreateCorner();
  166. Box corner_box;
  167. LayoutDetails::BuildBox(corner_box, Vector2f(containing_block.x), corner);
  168. corner_box.SetContent(Vector2f(scrollbars[VERTICAL].size, scrollbars[HORIZONTAL].size));
  169. corner->SetBox(corner_box);
  170. corner->SetOffset(containing_block + element_box.GetPosition(BoxArea::Padding) -
  171. Vector2f(scrollbars[VERTICAL].size, scrollbars[HORIZONTAL].size) - corner_box.GetPosition(BoxArea::Margin),
  172. element, true);
  173. corner->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Visible));
  174. }
  175. }
  176. void ElementScroll::UpdateProperties()
  177. {
  178. for (Element* scroll_element : {scrollbars[VERTICAL].element, scrollbars[HORIZONTAL].element, corner})
  179. {
  180. if (scroll_element)
  181. UpdateScrollElementProperties(scroll_element);
  182. }
  183. }
  184. bool ElementScroll::CreateScrollbar(Orientation orientation)
  185. {
  186. if (scrollbars[orientation].element && scrollbars[orientation].widget)
  187. return true;
  188. ElementPtr scrollbar_element =
  189. Factory::InstanceElement(element, "*", orientation == VERTICAL ? "scrollbarvertical" : "scrollbarhorizontal", XMLAttributes());
  190. scrollbars[orientation].element = scrollbar_element.get();
  191. scrollbars[orientation].element->SetProperty(PropertyId::Clip, Property(1, Unit::NUMBER));
  192. scrollbars[orientation].element->SetProperty(PropertyId::Drag, Property(Style::Drag::Block));
  193. scrollbars[orientation].widget = MakeUnique<WidgetScroll>(scrollbars[orientation].element);
  194. scrollbars[orientation].widget->Initialise(orientation == VERTICAL ? WidgetScroll::VERTICAL : WidgetScroll::HORIZONTAL);
  195. Element* child = element->AppendChild(std::move(scrollbar_element), false);
  196. UpdateScrollElementProperties(child);
  197. return true;
  198. }
  199. bool ElementScroll::CreateCorner()
  200. {
  201. if (corner != nullptr)
  202. return true;
  203. ElementPtr corner_element = Factory::InstanceElement(element, "*", "scrollbarcorner", XMLAttributes());
  204. corner = corner_element.get();
  205. corner->SetProperty(PropertyId::Clip, Property(1, Unit::NUMBER));
  206. corner->SetProperty(PropertyId::Drag, Property(Style::Drag::Block));
  207. Element* child = element->AppendChild(std::move(corner_element), false);
  208. UpdateScrollElementProperties(child);
  209. return true;
  210. }
  211. void ElementScroll::UpdateScrollElementProperties(Element* scroll_element)
  212. {
  213. // The construction of scrollbars can occur during layouting, then we need some properties and computed values straight away.
  214. // In particular their size. Furthermore, updating these properties straight away avoids dirtying the document after layouting,
  215. // which may result in one less additional and unnecessary layouting procedure.
  216. Context* context = element->GetContext();
  217. const float dp_ratio = (context ? context->GetDensityIndependentPixelRatio() : 1.0f);
  218. const Vector2f vp_dimensions = (context ? Vector2f(context->GetDimensions()) : Vector2f(1.0f));
  219. scroll_element->Update(dp_ratio, vp_dimensions);
  220. }
  221. ElementScroll::Scrollbar::Scrollbar() {}
  222. ElementScroll::Scrollbar::~Scrollbar() {}
  223. } // namespace Rml