ElementScroll.cpp 9.0 KB

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