ContainerBox.cpp 13 KB

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