ContainerBox.cpp 15 KB

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