ElementScroll.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "../../Include/Rocket/Core/ElementScroll.h"
  29. #include "LayoutEngine.h"
  30. #include "WidgetSliderScroll.h"
  31. #include "../../Include/Rocket/Core/Element.h"
  32. #include "../../Include/Rocket/Core/ElementUtilities.h"
  33. #include "../../Include/Rocket/Core/Event.h"
  34. #include "../../Include/Rocket/Core/Factory.h"
  35. namespace Rocket {
  36. namespace Core {
  37. ElementScroll::ElementScroll(Element* _element)
  38. {
  39. element = _element;
  40. corner = NULL;
  41. }
  42. ElementScroll::~ElementScroll()
  43. {
  44. for (int i = 0; i < 2; i++)
  45. {
  46. if (scrollbars[i].element != NULL)
  47. scrollbars[i].element->RemoveEventListener("scrollchange", this);
  48. }
  49. }
  50. // Updates the increment / decrement arrows.
  51. void ElementScroll::Update()
  52. {
  53. for (int i = 0; i < 2; i++)
  54. {
  55. if (scrollbars[i].widget != NULL)
  56. scrollbars[i].widget->Update();
  57. }
  58. }
  59. // Enables and sizes one of the scrollbars.
  60. void ElementScroll::EnableScrollbar(Orientation orientation, float element_width)
  61. {
  62. if (!scrollbars[orientation].enabled)
  63. {
  64. CreateScrollbar(orientation);
  65. scrollbars[orientation].element->SetProperty(VISIBILITY, "visible");
  66. scrollbars[orientation].enabled = true;
  67. }
  68. // Determine the size of the scrollbar.
  69. Box box;
  70. LayoutEngine::BuildBox(box, Vector2f(element_width, element_width), scrollbars[orientation].element);
  71. if (orientation == VERTICAL)
  72. scrollbars[orientation].size = box.GetSize(Box::MARGIN).x;
  73. if (orientation == HORIZONTAL)
  74. {
  75. if (box.GetSize().y < 0)
  76. scrollbars[orientation].size = box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) +
  77. box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT) +
  78. scrollbars[orientation].element->ResolveProperty(HEIGHT, element_width);
  79. else
  80. scrollbars[orientation].size = box.GetSize(Box::MARGIN).y;
  81. }
  82. }
  83. // Disables and hides one of the scrollbars.
  84. void ElementScroll::DisableScrollbar(Orientation orientation)
  85. {
  86. if (scrollbars[orientation].enabled)
  87. {
  88. scrollbars[orientation].element->SetProperty(VISIBILITY, "hidden");
  89. scrollbars[orientation].enabled = false;
  90. }
  91. }
  92. // Updates the position of the scrollbar.
  93. void ElementScroll::UpdateScrollbar(Orientation orientation)
  94. {
  95. float bar_position;
  96. float traversable_track;
  97. if (orientation == VERTICAL)
  98. {
  99. bar_position = element->GetScrollTop();
  100. traversable_track = element->GetScrollHeight() - element->GetClientHeight();
  101. }
  102. else
  103. {
  104. bar_position = element->GetScrollLeft();
  105. traversable_track = element->GetScrollWidth() - element->GetClientWidth();
  106. }
  107. if (traversable_track <= 0)
  108. bar_position = 0;
  109. else
  110. bar_position /= traversable_track;
  111. if (scrollbars[orientation].widget != NULL)
  112. {
  113. bar_position = Math::Clamp(bar_position, 0.0f, 1.0f);
  114. if (scrollbars[orientation].widget->GetBarPosition() != bar_position)
  115. scrollbars[orientation].widget->SetBarPosition(bar_position);
  116. }
  117. }
  118. // Returns one of the scrollbar elements.
  119. Element* ElementScroll::GetScrollbar(Orientation orientation)
  120. {
  121. return scrollbars[orientation].element;
  122. }
  123. // Returns the size, in pixels, of one of the scrollbars; for a vertical scrollbar, this is width, for a horizontal scrollbar, this is height.
  124. float ElementScroll::GetScrollbarSize(Orientation orientation)
  125. {
  126. if (!scrollbars[orientation].enabled)
  127. return 0;
  128. return scrollbars[orientation].size;
  129. }
  130. // Formats the enabled scrollbars based on the current size of the host element.
  131. void ElementScroll::FormatScrollbars()
  132. {
  133. Vector2f containing_block = element->GetBox().GetSize(Box::PADDING);
  134. for (int i = 0; i < 2; i++)
  135. {
  136. if (!scrollbars[i].enabled)
  137. continue;
  138. if (i == VERTICAL)
  139. {
  140. scrollbars[i].widget->SetBarLength(element->GetClientHeight());
  141. scrollbars[i].widget->SetTrackLength(element->GetScrollHeight());
  142. float traversable_track = element->GetScrollHeight() - element->GetClientHeight();
  143. if (traversable_track > 0)
  144. scrollbars[i].widget->SetBarPosition(element->GetScrollTop() / traversable_track);
  145. else
  146. scrollbars[i].widget->SetBarPosition(0);
  147. }
  148. else
  149. {
  150. scrollbars[i].widget->SetBarLength(element->GetClientWidth());
  151. scrollbars[i].widget->SetTrackLength(element->GetScrollWidth());
  152. float traversable_track = element->GetScrollWidth() - element->GetClientWidth();
  153. if (traversable_track > 0)
  154. scrollbars[i].widget->SetBarPosition(element->GetScrollLeft() / traversable_track);
  155. else
  156. scrollbars[i].widget->SetBarPosition(0);
  157. }
  158. float slider_length = containing_block[1 - i];
  159. float user_scrollbar_margin = scrollbars[i].element->ResolveProperty(SCROLLBAR_MARGIN, slider_length);
  160. float min_scrollbar_margin = GetScrollbarSize(i == VERTICAL ? HORIZONTAL : VERTICAL);
  161. slider_length -= Math::Max(user_scrollbar_margin, min_scrollbar_margin);
  162. scrollbars[i].widget->FormatElements(containing_block, slider_length);
  163. scrollbars[i].widget->SetLineHeight((float) ElementUtilities::GetLineHeight(element));
  164. int variable_axis = i == VERTICAL ? 0 : 1;
  165. Vector2f offset = element->GetBox().GetPosition(Box::PADDING);
  166. 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));
  167. // Add the top or left margin (as appropriate) onto the scrollbar's position.
  168. offset[1 - variable_axis] += scrollbars[i].element->GetBox().GetEdge(Box::MARGIN, i == VERTICAL ? Box::TOP : Box::LEFT);
  169. scrollbars[i].element->SetOffset(offset, element, true);
  170. }
  171. // Format the corner, if it is necessary.
  172. if (scrollbars[0].enabled &&
  173. scrollbars[1].enabled)
  174. {
  175. CreateCorner();
  176. Box corner_box;
  177. corner_box.SetContent(Vector2f(scrollbars[VERTICAL].size, scrollbars[HORIZONTAL].size));
  178. corner->SetBox(corner_box);
  179. corner->SetOffset(containing_block - Vector2f(scrollbars[VERTICAL].size, scrollbars[HORIZONTAL].size), element, true);
  180. corner->SetProperty(VISIBILITY, "visible");
  181. }
  182. else
  183. {
  184. if (corner != NULL)
  185. corner->SetProperty(VISIBILITY, "hidden");
  186. }
  187. }
  188. // Handles the 'onchange' events for the scrollbars.
  189. void ElementScroll::ProcessEvent(Event& event)
  190. {
  191. if (event == "scrollchange")
  192. {
  193. float value = event.GetParameter< float >("value", 0);
  194. if (event.GetTargetElement() == scrollbars[VERTICAL].element)
  195. element->SetScrollTop(value * (element->GetScrollHeight() - element->GetClientHeight()));
  196. else
  197. element->SetScrollLeft(value * (element->GetScrollWidth() - element->GetClientWidth()));
  198. }
  199. }
  200. // Creates one of the scroll component's scrollbar.
  201. bool ElementScroll::CreateScrollbar(Orientation orientation)
  202. {
  203. if (scrollbars[orientation].element != NULL &&
  204. scrollbars[orientation].widget != NULL)
  205. return true;
  206. scrollbars[orientation].element = Factory::InstanceElement(element, "*", orientation == VERTICAL ? "scrollbarvertical" : "scrollbarhorizontal", XMLAttributes());
  207. scrollbars[orientation].element->AddEventListener("scrollchange", this);
  208. scrollbars[orientation].element->SetProperty(CLIP, "1");
  209. scrollbars[orientation].widget = new WidgetSliderScroll(scrollbars[orientation].element);
  210. scrollbars[orientation].widget->Initialise(orientation == VERTICAL ? WidgetSlider::VERTICAL : WidgetSlider::HORIZONTAL);
  211. element->AppendChild(scrollbars[orientation].element, false);
  212. scrollbars[orientation].element->RemoveReference();
  213. return true;
  214. }
  215. // Creates the scrollbar corner.
  216. bool ElementScroll::CreateCorner()
  217. {
  218. if (corner != NULL)
  219. return true;
  220. corner = Factory::InstanceElement(element, "*", "scrollbarcorner", XMLAttributes());
  221. element->AppendChild(corner, false);
  222. corner->RemoveReference();
  223. return true;
  224. }
  225. ElementScroll::Scrollbar::Scrollbar()
  226. {
  227. element = NULL;
  228. widget = NULL;
  229. enabled = false;
  230. size = 0;
  231. }
  232. ElementScroll::Scrollbar::~Scrollbar()
  233. {
  234. if (widget != NULL)
  235. delete widget;
  236. if (element != NULL)
  237. {
  238. if (element->GetParentNode() != NULL)
  239. element->GetParentNode()->RemoveChild(element);
  240. }
  241. }
  242. }
  243. }