ContainerBox.h 8.8 KB

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