ContainerBox.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #ifndef RMLUI_CORE_LAYOUT_CONTAINERBOX_H
  29. #define RMLUI_CORE_LAYOUT_CONTAINERBOX_H
  30. #include "../../../Include/RmlUi/Core/Box.h"
  31. #include "../../../Include/RmlUi/Core/StyleTypes.h"
  32. #include "../../../Include/RmlUi/Core/Types.h"
  33. #include "LayoutBox.h"
  34. namespace Rml {
  35. struct FormattingMode {
  36. enum class Constraint : uint8_t {
  37. None,
  38. MaxContent,
  39. };
  40. Constraint constraint = Constraint::None;
  41. bool allow_format_independent_cache = true;
  42. };
  43. /**
  44. Abstraction for layout boxes that can act as a containing block.
  45. Implements functionality for catching overflow, and for handling positioned boxes that the box acts as a containing block for.
  46. */
  47. class ContainerBox : public LayoutBox {
  48. public:
  49. /// Enable or disable scrollbars for the element we represent, preparing it for the first round of layouting, according to our properties.
  50. void ResetScrollbars(const Box& box);
  51. /// Adds an absolutely positioned element located within this container. The element is added to the absolute
  52. /// positioning containing block box, and will be formatted and positioned when closing that box, see
  53. /// 'ClosePositionedElements'.
  54. void AddAbsoluteElement(Element* element, Vector2f static_position, Element* static_relative_offset_parent);
  55. /// Adds a relatively positioned element which this container acts as a containing block for.
  56. void AddRelativeElement(Element* element);
  57. Element* GetElement() { return element; }
  58. /// Returns the size of the containing block for a box taking part in this container.
  59. /// @param[in] position The position property of the current box.
  60. /// @return The containing size, possibly indefinite (represented by negative size) along one or both axes.
  61. Vector2f GetContainingBlockSize(Style::Position position) const;
  62. /// Returns true if this container can have scrollbars enabled, as determined by its overflow properties.
  63. bool IsScrollContainer() const;
  64. /// Returns the currently active formatting mode for this container tree.
  65. const FormattingMode& GetFormattingMode() const { return formatting_mode; }
  66. void AssertMatchesParentContainer(ContainerBox* container_box) const
  67. {
  68. RMLUI_ASSERTMSG(container_box == parent_container, "Mismatched parent box.");
  69. (void)container_box;
  70. }
  71. Element* GetAbsolutePositioningContainingBlockElementForDebug() const { return absolute_positioning_containing_block->element; }
  72. protected:
  73. ContainerBox(Type type, Element* element, ContainerBox* parent_container, const FormattingMode& formatting_mode);
  74. /// Checks if we have a new overflow on an auto-scrolling element. If so, our vertical scrollbar will be enabled and
  75. /// our block boxes will be destroyed. All content will need to be re-formatted.
  76. /// @param[in] content_overflow_size The size of the visible content, relative to our content area.
  77. /// @param[in] box The box built for the element, possibly with a non-determinate height.
  78. /// @param[in] max_height Maximum height of the content area, if any.
  79. /// @returns True if no overflow occurred, false if it did.
  80. bool CatchOverflow(const Vector2f content_overflow_size, const Box& box, const float max_height) const;
  81. /// Set the box and scrollable area on our element, possibly catching any overflow.
  82. /// @param[in] content_overflow_size The size of the visible content, relative to our content area.
  83. /// @param[in] box The box to be set on the element.
  84. /// @param[in] max_height Maximum height of the content area, if any.
  85. /// @returns True if no overflow occurred, false if it did.
  86. bool SubmitBox(const Vector2f content_overflow_size, const Box& box, const float max_height);
  87. /// Formats, sizes, and positions all absolute elements whose containing block is this, and offsets relative elements.
  88. void ClosePositionedElements();
  89. // Set the element's baseline (proxy for private access to Element).
  90. void SetElementBaseline(float element_baseline);
  91. int CountAbsolutelyPositionedBoxes() const { return (int)absolute_elements.size(); }
  92. // The element this box represents, if any.
  93. Element* const element;
  94. private:
  95. struct AbsoluteElement {
  96. Vector2f static_position; // The hypothetical position of the element as if it was placed in normal flow.
  97. Element* static_position_offset_parent; // The element for which the static position is offset from.
  98. };
  99. using AbsoluteElementMap = SmallUnorderedMap<Element*, AbsoluteElement>;
  100. AbsoluteElementMap absolute_elements; // List of absolutely positioned elements that we act as a containing block for.
  101. ElementList relative_elements; // List of relatively positioned elements that we act as a containing block for.
  102. Style::Overflow overflow_x = Style::Overflow::Visible;
  103. Style::Overflow overflow_y = Style::Overflow::Visible;
  104. FormattingMode formatting_mode;
  105. ContainerBox* parent_container = nullptr;
  106. // For absolutely positioned boxes we use the first positioned ancestor. We deviate from the CSS specs where they
  107. // use a separate containing block for fixed boxes. In RCSS, we use the same rules on fixed boxes, as this is
  108. // particularly helpful on handles and other widgets that should not scroll with the window. This is a common design
  109. // pattern in target applications for this library.
  110. ContainerBox* absolute_positioning_containing_block = nullptr; // [not null]
  111. };
  112. /**
  113. The root of a tree of layout boxes, usually represents the root element or viewport.
  114. */
  115. class RootBox final : public ContainerBox {
  116. public:
  117. RootBox(const Box& box, FormattingMode formatting_mode) : ContainerBox(Type::Root, nullptr, nullptr, formatting_mode), box(box) {}
  118. RootBox(const Box& box, RootBox* absolute_root) : ContainerBox(Type::Root, nullptr, absolute_root, absolute_root->GetFormattingMode()), box(box)
  119. {}
  120. const Box* GetIfBox() const override { return &box; }
  121. String DebugDumpTree(int depth) const override;
  122. using ContainerBox::CountAbsolutelyPositionedBoxes;
  123. private:
  124. Box box;
  125. };
  126. /**
  127. A box where flexbox layout occurs.
  128. */
  129. class FlexContainer final : public ContainerBox {
  130. public:
  131. FlexContainer(Element* element, ContainerBox* parent_container, const Box& box);
  132. // Submits the formatted box to the flex container element, and propagates any uncaught overflow to this box.
  133. // @returns True if it succeeds, otherwise false if it needs to be formatted again because scrollbars were enabled.
  134. bool Close(const Vector2f content_overflow_size, const Box& box, float element_baseline);
  135. float GetShrinkToFitWidth() const override;
  136. const Box* GetIfBox() const override { return &box; }
  137. String DebugDumpTree(int depth) const override;
  138. Box& GetBox() { return box; }
  139. private:
  140. Box box;
  141. };
  142. /**
  143. A box where table formatting occurs.
  144. As opposed to a flex container, the table wrapper cannot contain overflow or produce scrollbars.
  145. */
  146. class TableWrapper final : public ContainerBox {
  147. public:
  148. TableWrapper(Element* element, ContainerBox* parent_container, const Box& initial_box);
  149. // Submits the formatted box to the table element, and propagates any uncaught overflow to this box.
  150. void Close(const Vector2f content_overflow_size, const Box& box, float element_baseline);
  151. float GetShrinkToFitWidth() const override;
  152. const Box* GetIfBox() const override { return &box; }
  153. String DebugDumpTree(int depth) const override;
  154. Box& GetBox() { return box; }
  155. private:
  156. Box box;
  157. };
  158. /**
  159. A box which is produced after matching the existing layout.
  160. */
  161. class CachedContainer final : public ContainerBox {
  162. public:
  163. CachedContainer(Element* element, ContainerBox* parent_container, const Box& box, Vector2f visible_overflow_size,
  164. Optional<float> baseline_of_last_line) :
  165. ContainerBox(Type::CachedContainer, element, parent_container, parent_container->GetFormattingMode()), box(box),
  166. baseline_of_last_line(baseline_of_last_line)
  167. {
  168. SetVisibleOverflowSize(visible_overflow_size);
  169. }
  170. const Box* GetIfBox() const override { return &box; }
  171. bool GetBaselineOfLastLine(float& out_baseline) const override;
  172. float GetShrinkToFitWidth() const override;
  173. String DebugDumpTree(int depth) const override;
  174. private:
  175. const Box& box;
  176. Optional<float> baseline_of_last_line;
  177. };
  178. } // namespace Rml
  179. #endif