ElementScroll.cpp 8.7 KB

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