ContainerBox.h 9.4 KB

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