ElementScroll.cpp 9.2 KB

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