ContainerBox.cpp 13 KB

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