ContainerBox.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 "ContainerBox.h"
  29. #include "../../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../../Include/RmlUi/Core/Element.h"
  31. #include "../../../Include/RmlUi/Core/ElementScroll.h"
  32. #include "../../../Include/RmlUi/Core/Profiling.h"
  33. #include "FormattingContext.h"
  34. #include "LayoutDetails.h"
  35. #include <algorithm>
  36. #include <cmath>
  37. namespace Rml {
  38. void ContainerBox::ResetScrollbars(const Box& box)
  39. {
  40. RMLUI_ASSERT(element);
  41. if (overflow_x == Style::Overflow::Scroll)
  42. element->GetElementScroll()->EnableScrollbar(ElementScroll::HORIZONTAL, box.GetSizeAcross(Box::HORIZONTAL, Box::PADDING));
  43. else
  44. element->GetElementScroll()->DisableScrollbar(ElementScroll::HORIZONTAL);
  45. if (overflow_y == Style::Overflow::Scroll)
  46. element->GetElementScroll()->EnableScrollbar(ElementScroll::VERTICAL, box.GetSizeAcross(Box::HORIZONTAL, Box::PADDING));
  47. else
  48. element->GetElementScroll()->DisableScrollbar(ElementScroll::VERTICAL);
  49. }
  50. void ContainerBox::AddAbsoluteElement(Element* element, Vector2f static_position, Element* static_relative_offset_parent)
  51. {
  52. // We may possibly be adding the same element from a previous layout iteration. If so, this ensures it is updated with the latest static position.
  53. absolute_elements[element] = AbsoluteElement{static_position, static_relative_offset_parent};
  54. }
  55. void ContainerBox::AddRelativeElement(Element* element)
  56. {
  57. // The same relative element may be added multiple times during repeated layout iterations, avoid any duplicates.
  58. if (std::find(relative_elements.begin(), relative_elements.end(), element) == relative_elements.end())
  59. relative_elements.push_back(element);
  60. }
  61. void ContainerBox::ClosePositionedElements()
  62. {
  63. // Any relatively positioned elements that we act as containing block for may need to be have their positions
  64. // updated to reflect changes to the size of this block box. Update relative offsets before handling absolute
  65. // elements, as this may affect the resolved static position of the absolute elements.
  66. for (Element* child : relative_elements)
  67. child->UpdateOffset();
  68. relative_elements.clear();
  69. while (!absolute_elements.empty())
  70. {
  71. // New absolute elements may be added to this box during formatting below. To avoid invalidated iterators and
  72. // references, move the list to a local copy to iterate over, and repeat if new elements are added.
  73. AbsoluteElementMap absolute_elements_iterate = std::move(absolute_elements);
  74. absolute_elements.clear();
  75. for (const auto& absolute_element_pair : absolute_elements_iterate)
  76. {
  77. Element* absolute_element = absolute_element_pair.first;
  78. const Vector2f static_position = absolute_element_pair.second.static_position;
  79. Element* static_position_offset_parent = absolute_element_pair.second.static_position_offset_parent;
  80. // Find the static position relative to this containing block. First, calculate the offset from ourself to
  81. // the static position's offset parent. Assumes (1) that this container box is part of the containing block
  82. // chain of the static position offset parent, and (2) that all offsets in this chain has been set already.
  83. Vector2f relative_position;
  84. for (Element* ancestor = static_position_offset_parent; ancestor && ancestor != element; ancestor = ancestor->GetOffsetParent())
  85. relative_position += ancestor->GetRelativeOffset(Box::BORDER);
  86. // Now simply add the result to the stored static position to get the static position in our local space.
  87. Vector2f offset = relative_position + static_position;
  88. // Lay out the element.
  89. FormattingContext::FormatIndependent(this, absolute_element, nullptr, FormattingContextType::Block);
  90. // Now that the element's box has been built, we can offset the position we determined was appropriate for
  91. // it by the element's margin. This is necessary because the coordinate system for the box begins at the
  92. // border, not the margin.
  93. offset.x += absolute_element->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  94. offset.y += absolute_element->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  95. // Set the offset of the element; the element itself will take care of any RCSS-defined positional offsets.
  96. absolute_element->SetOffset(offset, element);
  97. }
  98. }
  99. }
  100. void ContainerBox::SetElementBaseline(float element_baseline)
  101. {
  102. element->SetBaseline(element_baseline);
  103. }
  104. void ContainerBox::SubmitElementLayout()
  105. {
  106. element->OnLayout();
  107. }
  108. ContainerBox::ContainerBox(Type type, Element* element, ContainerBox* parent_container) :
  109. LayoutBox(type), element(element), parent_container(parent_container)
  110. {
  111. if (element)
  112. {
  113. const auto& computed = element->GetComputedValues();
  114. overflow_x = computed.overflow_x();
  115. overflow_y = computed.overflow_y();
  116. position_property = computed.position();
  117. has_local_transform_or_perspective = (computed.has_local_transform() || computed.has_local_perspective());
  118. }
  119. }
  120. bool ContainerBox::CatchOverflow(const Vector2f content_overflow_size, const Box& box, const float max_height) const
  121. {
  122. if (!IsScrollContainer())
  123. return true;
  124. const Vector2f padding_bottom_right = {box.GetEdge(Box::PADDING, Box::RIGHT), box.GetEdge(Box::PADDING, Box::BOTTOM)};
  125. const float padding_width = box.GetSizeAcross(Box::HORIZONTAL, Box::PADDING);
  126. Vector2f available_space = box.GetSize();
  127. if (available_space.y < 0.f)
  128. available_space.y = max_height;
  129. if (available_space.y < 0.f)
  130. available_space.y = HUGE_VALF;
  131. RMLUI_ASSERT(available_space.x >= 0.f && available_space.y >= 0.f);
  132. // Allow overflow onto the padding area.
  133. available_space += padding_bottom_right;
  134. ElementScroll* element_scroll = element->GetElementScroll();
  135. bool scrollbar_size_changed = false;
  136. // @performance If we have auto-height sizing and the horizontal scrollbar is enabled, then we can in principle
  137. // simply add the scrollbar size to the height instead of formatting the element all over again.
  138. if (overflow_x == Style::Overflow::Auto && content_overflow_size.x > available_space.x + 0.5f)
  139. {
  140. if (element_scroll->GetScrollbarSize(ElementScroll::HORIZONTAL) == 0.f)
  141. {
  142. element_scroll->EnableScrollbar(ElementScroll::HORIZONTAL, padding_width);
  143. const float new_size = element_scroll->GetScrollbarSize(ElementScroll::HORIZONTAL);
  144. scrollbar_size_changed = (new_size != 0.f);
  145. available_space.y -= new_size;
  146. }
  147. }
  148. // If we're auto-scrolling and our height is fixed, we have to check if this box has exceeded our client height.
  149. if (overflow_y == Style::Overflow::Auto && content_overflow_size.y > available_space.y + 0.5f)
  150. {
  151. if (element_scroll->GetScrollbarSize(ElementScroll::VERTICAL) == 0.f)
  152. {
  153. element_scroll->EnableScrollbar(ElementScroll::VERTICAL, padding_width);
  154. const float new_size = element_scroll->GetScrollbarSize(ElementScroll::VERTICAL);
  155. scrollbar_size_changed |= (new_size != 0.f);
  156. }
  157. }
  158. return !scrollbar_size_changed;
  159. }
  160. bool ContainerBox::SubmitBox(const Vector2f content_overflow_size, const Box& box, const float max_height)
  161. {
  162. Vector2f visible_overflow_size;
  163. // Set the computed box on the element.
  164. if (element)
  165. {
  166. // Calculate the dimensions of the box's scrollable overflow rectangle. This is the union of the tightest-
  167. // fitting box around all of the internal elements, and this element's padding box. We really only care about
  168. // overflow on the bottom-right sides, as these are the only ones allowed to be scrolled to in CSS.
  169. //
  170. // If we are a scroll container (use any other value than 'overflow: visible'), then any overflow outside our
  171. // padding box should be caught here. Otherwise, our overflow should be included in the overflow calculations of
  172. // our nearest scroll container ancestor.
  173. // If our content is larger than our padding box, we can add scrollbars if we're set to auto-scrollbars. If
  174. // we're set to always use scrollbars, then the scrollbars have already been enabled.
  175. if (!CatchOverflow(content_overflow_size, box, max_height))
  176. return false;
  177. const Vector2f padding_top_left = {box.GetEdge(Box::PADDING, Box::LEFT), box.GetEdge(Box::PADDING, Box::TOP)};
  178. const Vector2f padding_bottom_right = {box.GetEdge(Box::PADDING, Box::RIGHT), box.GetEdge(Box::PADDING, Box::BOTTOM)};
  179. const Vector2f padding_size = box.GetSize() + padding_top_left + padding_bottom_right;
  180. const bool is_scroll_container = IsScrollContainer();
  181. const Vector2f scrollbar_size = {
  182. is_scroll_container ? element->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL) : 0.f,
  183. is_scroll_container ? element->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL) : 0.f,
  184. };
  185. // Scrollable overflow is the set of things extending our padding area, for which scrolling could be provided.
  186. const Vector2f scrollable_overflow_size = Math::Max(padding_size - scrollbar_size, padding_top_left + content_overflow_size);
  187. element->SetBox(box);
  188. element->SetScrollableOverflowRectangle(scrollable_overflow_size);
  189. const Vector2f border_size = padding_size + box.GetFrameSize(Box::BORDER);
  190. // Set the visible overflow size so that ancestors can catch any overflow produced by us. That is, hiding it or
  191. // providing a scrolling mechanism. If this box is a scroll container, we catch our own overflow here; then,
  192. // just use the normal margin box as that will effectively remove the overflow from our ancestor's perspective.
  193. if (is_scroll_container)
  194. {
  195. visible_overflow_size = border_size;
  196. // Format any scrollbars in case they were enabled on this element.
  197. element->GetElementScroll()->FormatScrollbars();
  198. }
  199. else
  200. {
  201. const Vector2f border_top_left = {box.GetEdge(Box::BORDER, Box::LEFT), box.GetEdge(Box::BORDER, Box::TOP)};
  202. visible_overflow_size = Math::Max(border_size, content_overflow_size + border_top_left + padding_top_left);
  203. }
  204. }
  205. SetVisibleOverflowSize(visible_overflow_size);
  206. return true;
  207. }
  208. String RootBox::DebugDumpTree(int depth) const
  209. {
  210. return String(depth * 2, ' ') + "RootBox";
  211. }
  212. FlexContainer::FlexContainer(Element* element, ContainerBox* parent_container) : ContainerBox(Type::FlexContainer, element, parent_container)
  213. {
  214. RMLUI_ASSERT(element);
  215. }
  216. bool FlexContainer::Close(const Vector2f content_overflow_size, const Box& box, float element_baseline)
  217. {
  218. if (!SubmitBox(content_overflow_size, box, -1.f))
  219. return false;
  220. ClosePositionedElements();
  221. SubmitElementLayout();
  222. SetElementBaseline(element_baseline);
  223. return true;
  224. }
  225. String FlexContainer::DebugDumpTree(int depth) const
  226. {
  227. return String(depth * 2, ' ') + "FlexContainer" + " | " + LayoutDetails::GetDebugElementName(element);
  228. }
  229. TableWrapper::TableWrapper(Element* element, ContainerBox* parent_container) : ContainerBox(Type::TableWrapper, element, parent_container)
  230. {
  231. RMLUI_ASSERT(element);
  232. }
  233. void TableWrapper::Close(const Vector2f content_overflow_size, const Box& box, float element_baseline)
  234. {
  235. bool result = SubmitBox(content_overflow_size, box, -1.f);
  236. // Since the table wrapper cannot generate scrollbars, this should always pass.
  237. RMLUI_ASSERT(result);
  238. (void)result;
  239. ClosePositionedElements();
  240. SubmitElementLayout();
  241. SetElementBaseline(element_baseline);
  242. }
  243. String TableWrapper::DebugDumpTree(int depth) const
  244. {
  245. return String(depth * 2, ' ') + "TableWrapper" + " | " + LayoutDetails::GetDebugElementName(element);
  246. }
  247. } // namespace Rml