ContainerBox.cpp 16 KB

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