ContainerBox.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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, to be formatted and positioned when closing this container, see 'ClosePositionedElements'.
  44. void AddAbsoluteElement(Element* element, Vector2f static_position, Element* static_relative_offset_parent);
  45. // Adds a relatively positioned element which we act as a containing block for.
  46. void AddRelativeElement(Element* element);
  47. ContainerBox* GetParent() { return parent_container; }
  48. Element* GetElement() { return element; }
  49. bool IsScrollContainer() const;
  50. // Returns true if this box acts as a containing block for absolutely positioned descendants.
  51. bool IsAbsolutePositioningContainingBlock() const { return is_absolute_positioning_containing_block; }
  52. protected:
  53. ContainerBox(Type type, Element* element, ContainerBox* parent_container);
  54. /// Checks if we have a new overflow on an auto-scrolling element. If so, our vertical scrollbar will be enabled and
  55. /// our block boxes will be destroyed. All content will need to be re-formatted.
  56. /// @param[in] content_overflow_size The size of the visible content, relative to our content area.
  57. /// @param[in] box The box built for the element, possibly with a non-determinate height.
  58. /// @param[in] max_height Maximum height of the content area, if any.
  59. /// @returns True if no overflow occured, false if it did.
  60. bool CatchOverflow(const Vector2f content_overflow_size, const Box& box, const float max_height) const;
  61. /// Set the box and scrollable area on our element, possibly catching any overflow.
  62. /// @param[in] content_overflow_size The size of the visible content, relative to our content area.
  63. /// @param[in] box The box to be set on the element.
  64. /// @param[in] max_height Maximum height of the content area, if any.
  65. /// @returns True if no overflow occured, false if it did.
  66. bool SubmitBox(const Vector2f content_overflow_size, const Box& box, const float max_height);
  67. /// Formats, sizes, and positions all absolute elements whose containing block is this, and offsets relative elements.
  68. void ClosePositionedElements();
  69. // Set the element's baseline (proxy for private access to Element).
  70. void SetElementBaseline(float element_baseline);
  71. // Calls Element::OnLayout (proxy for private access to Element).
  72. void SubmitElementLayout();
  73. // The element this box represents, if any.
  74. Element* const element;
  75. private:
  76. struct AbsoluteElement {
  77. Vector2f static_position; // The hypothetical position of the element as if it was placed in normal flow.
  78. Element* static_position_offset_parent; // The element for which the static position is offset from.
  79. };
  80. using AbsoluteElementMap = SmallUnorderedMap<Element*, AbsoluteElement>;
  81. AbsoluteElementMap absolute_elements; // List of absolutely positioned elements that we act as a containing block for.
  82. ElementList relative_elements; // List of relatively positioned elements that we act as a containing block for.
  83. Style::Overflow overflow_x = Style::Overflow::Visible;
  84. Style::Overflow overflow_y = Style::Overflow::Visible;
  85. bool is_absolute_positioning_containing_block = false;
  86. ContainerBox* parent_container = nullptr;
  87. };
  88. /**
  89. The root of a tree of layout boxes, usually represents the root element or viewport.
  90. */
  91. class RootBox final : public ContainerBox {
  92. public:
  93. RootBox(Vector2f containing_block) : ContainerBox(Type::Root, nullptr, nullptr), box(containing_block) {}
  94. RootBox(const Box& box) : ContainerBox(Type::Root, nullptr, nullptr), box(box) {}
  95. const Box* GetIfBox() const override { return &box; }
  96. String DebugDumpTree(int depth) const override;
  97. private:
  98. Box box;
  99. };
  100. /**
  101. A box where flexbox layout occurs.
  102. */
  103. class FlexContainer final : public ContainerBox {
  104. public:
  105. FlexContainer(Element* element, ContainerBox* parent_container);
  106. // Submits the formatted box to the flex container element, and propagates any uncaught overflow to this box.
  107. // @returns True if it succeeds, otherwise false if it needs to be formatted again because scrollbars were enabled.
  108. bool Close(const Vector2f content_overflow_size, const Box& box, float element_baseline);
  109. float GetShrinkToFitWidth() const override;
  110. const Box* GetIfBox() const override { return &box; }
  111. String DebugDumpTree(int depth) const override;
  112. Box& GetBox() { return box; }
  113. private:
  114. Box box;
  115. };
  116. /**
  117. A box where table formatting occurs.
  118. As opposed to a flex container, the table wrapper cannot contain overflow or produce scrollbars.
  119. */
  120. class TableWrapper final : public ContainerBox {
  121. public:
  122. TableWrapper(Element* element, ContainerBox* parent_container);
  123. // Submits the formatted box to the table element, and propagates any uncaught overflow to this box.
  124. void Close(const Vector2f content_overflow_size, const Box& box, float element_baseline);
  125. float GetShrinkToFitWidth() const override;
  126. const Box* GetIfBox() const override { return &box; }
  127. String DebugDumpTree(int depth) const override;
  128. Box& GetBox() { return box; }
  129. private:
  130. Box box;
  131. };
  132. } // namespace Rml
  133. #endif