ElementScroll.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #include "../../Include/RmlUi/Core/ElementScroll.h"
  29. #include "LayoutDetails.h"
  30. #include "WidgetScroll.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. namespace Rml {
  36. ElementScroll::ElementScroll(Element* _element)
  37. {
  38. element = _element;
  39. corner = nullptr;
  40. }
  41. ElementScroll::~ElementScroll()
  42. {
  43. ClearScrollbars();
  44. }
  45. void ElementScroll::ClearScrollbars()
  46. {
  47. for (int i = 0; i < 2; i++)
  48. {
  49. if (scrollbars[i].element != nullptr)
  50. {
  51. scrollbars[i] = Scrollbar();
  52. }
  53. }
  54. if (corner != nullptr)
  55. {
  56. corner->GetParentNode()->RemoveChild(element);
  57. corner = nullptr;
  58. }
  59. }
  60. // Updates the increment / decrement arrows.
  61. void ElementScroll::Update()
  62. {
  63. for (int i = 0; i < 2; i++)
  64. {
  65. if (scrollbars[i].widget != nullptr)
  66. scrollbars[i].widget->Update();
  67. }
  68. }
  69. // Enables and sizes one of the scrollbars.
  70. void ElementScroll::EnableScrollbar(Orientation orientation, float element_width)
  71. {
  72. if (!scrollbars[orientation].enabled)
  73. {
  74. CreateScrollbar(orientation);
  75. scrollbars[orientation].element->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Visible));
  76. scrollbars[orientation].enabled = true;
  77. }
  78. // Determine the size of the scrollbar.
  79. Box box;
  80. LayoutDetails::BuildBox(box, Vector2f(element_width, element_width), scrollbars[orientation].element);
  81. if (orientation == VERTICAL)
  82. scrollbars[orientation].size = box.GetSize(Box::MARGIN).x;
  83. if (orientation == HORIZONTAL)
  84. {
  85. if (box.GetSize().y < 0)
  86. scrollbars[orientation].size = box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) +
  87. box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT) +
  88. ResolveValue(scrollbars[orientation].element->GetComputedValues().height, element_width);
  89. else
  90. scrollbars[orientation].size = box.GetSize(Box::MARGIN).y;
  91. }
  92. }
  93. // Disables and hides one of the scrollbars.
  94. void ElementScroll::DisableScrollbar(Orientation orientation)
  95. {
  96. if (scrollbars[orientation].enabled)
  97. {
  98. scrollbars[orientation].element->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  99. scrollbars[orientation].enabled = false;
  100. }
  101. }
  102. // Updates the position of the scrollbar.
  103. void ElementScroll::UpdateScrollbar(Orientation orientation)
  104. {
  105. float bar_position;
  106. float traversable_track;
  107. if (orientation == VERTICAL)
  108. {
  109. bar_position = element->GetScrollTop();
  110. traversable_track = element->GetScrollHeight() - element->GetClientHeight();
  111. }
  112. else
  113. {
  114. bar_position = element->GetScrollLeft();
  115. traversable_track = element->GetScrollWidth() - element->GetClientWidth();
  116. }
  117. if (traversable_track <= 0)
  118. bar_position = 0;
  119. else
  120. bar_position /= traversable_track;
  121. if (scrollbars[orientation].widget != nullptr)
  122. {
  123. bar_position = Math::Clamp(bar_position, 0.0f, 1.0f);
  124. if (scrollbars[orientation].widget->GetBarPosition() != bar_position)
  125. scrollbars[orientation].widget->SetBarPosition(bar_position);
  126. }
  127. }
  128. // Returns one of the scrollbar elements.
  129. Element* ElementScroll::GetScrollbar(Orientation orientation)
  130. {
  131. return scrollbars[orientation].element;
  132. }
  133. // Returns the size, in pixels, of one of the scrollbars; for a vertical scrollbar, this is width, for a horizontal scrollbar, this is height.
  134. float ElementScroll::GetScrollbarSize(Orientation orientation)
  135. {
  136. if (!scrollbars[orientation].enabled)
  137. return 0;
  138. return scrollbars[orientation].size;
  139. }
  140. // Formats the enabled scrollbars based on the current size of the host element.
  141. void ElementScroll::FormatScrollbars()
  142. {
  143. Vector2f containing_block = element->GetBox().GetSize(Box::PADDING);
  144. for (int i = 0; i < 2; i++)
  145. {
  146. if (!scrollbars[i].enabled)
  147. continue;
  148. if (i == VERTICAL)
  149. {
  150. scrollbars[i].widget->SetBarLength(element->GetClientHeight());
  151. scrollbars[i].widget->SetTrackLength(element->GetScrollHeight());
  152. float traversable_track = element->GetScrollHeight() - element->GetClientHeight();
  153. if (traversable_track > 0)
  154. scrollbars[i].widget->SetBarPosition(element->GetScrollTop() / traversable_track);
  155. else
  156. scrollbars[i].widget->SetBarPosition(0);
  157. }
  158. else
  159. {
  160. scrollbars[i].widget->SetBarLength(element->GetClientWidth());
  161. scrollbars[i].widget->SetTrackLength(element->GetScrollWidth());
  162. float traversable_track = element->GetScrollWidth() - element->GetClientWidth();
  163. if (traversable_track > 0)
  164. scrollbars[i].widget->SetBarPosition(element->GetScrollLeft() / traversable_track);
  165. else
  166. scrollbars[i].widget->SetBarPosition(0);
  167. }
  168. float slider_length = containing_block[1 - i];
  169. float user_scrollbar_margin = scrollbars[i].element->GetComputedValues().scrollbar_margin;
  170. float min_scrollbar_margin = GetScrollbarSize(i == VERTICAL ? HORIZONTAL : VERTICAL);
  171. slider_length -= Math::Max(user_scrollbar_margin, min_scrollbar_margin);
  172. scrollbars[i].widget->FormatElements(containing_block, slider_length);
  173. scrollbars[i].widget->SetLineHeight(element->GetLineHeight());
  174. int variable_axis = i == VERTICAL ? 0 : 1;
  175. Vector2f offset = element->GetBox().GetPosition(Box::PADDING);
  176. offset[variable_axis] += containing_block[variable_axis] - (scrollbars[i].element->GetBox().GetSize(Box::BORDER)[variable_axis] + scrollbars[i].element->GetBox().GetEdge(Box::MARGIN, i == VERTICAL ? Box::RIGHT : Box::BOTTOM));
  177. // Add the top or left margin (as appropriate) onto the scrollbar's position.
  178. offset[1 - variable_axis] += scrollbars[i].element->GetBox().GetEdge(Box::MARGIN, i == VERTICAL ? Box::TOP : Box::LEFT);
  179. scrollbars[i].element->SetOffset(offset, element, true);
  180. }
  181. // Format the corner, if it is necessary.
  182. if (scrollbars[0].enabled &&
  183. scrollbars[1].enabled)
  184. {
  185. CreateCorner();
  186. Box corner_box;
  187. corner_box.SetContent(Vector2f(scrollbars[VERTICAL].size, scrollbars[HORIZONTAL].size));
  188. corner->SetBox(corner_box);
  189. corner->SetOffset(containing_block - Vector2f(scrollbars[VERTICAL].size, scrollbars[HORIZONTAL].size), element, true);
  190. corner->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Visible));
  191. }
  192. else
  193. {
  194. if (corner != nullptr)
  195. corner->SetProperty(PropertyId::Visibility, Property(Style::Visibility::Hidden));
  196. }
  197. }
  198. // Creates one of the scroll component's scrollbar.
  199. bool ElementScroll::CreateScrollbar(Orientation orientation)
  200. {
  201. if (scrollbars[orientation].element &&
  202. scrollbars[orientation].widget)
  203. return true;
  204. ElementPtr scrollbar_element = Factory::InstanceElement(element, "*", orientation == VERTICAL ? "scrollbarvertical" : "scrollbarhorizontal", XMLAttributes());
  205. scrollbars[orientation].element = scrollbar_element.get();
  206. scrollbars[orientation].element->SetProperty(PropertyId::Clip, Property(1, Property::NUMBER));
  207. scrollbars[orientation].widget = new WidgetScroll(scrollbars[orientation].element);
  208. scrollbars[orientation].widget->Initialise(orientation == VERTICAL ? WidgetScroll::VERTICAL : WidgetScroll::HORIZONTAL);
  209. Element* child = element->AppendChild(std::move(scrollbar_element), false);
  210. // The construction of scrollbars can occur during layouting, then we need some properties and computed values straight away.
  211. child->UpdateProperties();
  212. return true;
  213. }
  214. // Creates the scrollbar corner.
  215. bool ElementScroll::CreateCorner()
  216. {
  217. if (corner != nullptr)
  218. return true;
  219. ElementPtr corner_element = Factory::InstanceElement(element, "*", "scrollbarcorner", XMLAttributes());
  220. corner = corner_element.get();
  221. element->AppendChild(std::move(corner_element), false);
  222. return true;
  223. }
  224. ElementScroll::Scrollbar::Scrollbar()
  225. {
  226. element = nullptr;
  227. widget = nullptr;
  228. enabled = false;
  229. size = 0;
  230. }
  231. ElementScroll::Scrollbar::~Scrollbar()
  232. {
  233. if (widget != nullptr)
  234. delete widget;
  235. if (element != nullptr)
  236. {
  237. if (element->GetParentNode() != nullptr)
  238. element->GetParentNode()->RemoveChild(element);
  239. }
  240. }
  241. } // namespace Rml