ContainerBox.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // The element this box represents, if any.
  72. Element* const element;
  73. private:
  74. struct AbsoluteElement {
  75. Vector2f static_position; // The hypothetical position of the element as if it was placed in normal flow.
  76. Element* static_position_offset_parent; // The element for which the static position is offset from.
  77. };
  78. using AbsoluteElementMap = SmallUnorderedMap<Element*, AbsoluteElement>;
  79. AbsoluteElementMap absolute_elements; // List of absolutely positioned elements that we act as a containing block for.
  80. ElementList relative_elements; // List of relatively positioned elements that we act as a containing block for.
  81. Style::Overflow overflow_x = Style::Overflow::Visible;
  82. Style::Overflow overflow_y = Style::Overflow::Visible;
  83. bool is_absolute_positioning_containing_block = false;
  84. ContainerBox* parent_container = nullptr;
  85. };
  86. /**
  87. The root of a tree of layout boxes, usually represents the root element or viewport.
  88. */
  89. class RootBox final : public ContainerBox {
  90. public:
  91. RootBox(Vector2f containing_block) : ContainerBox(Type::Root, nullptr, nullptr), box(containing_block) {}
  92. RootBox(const Box& box) : ContainerBox(Type::Root, nullptr, nullptr), box(box) {}
  93. const Box* GetIfBox() const override { return &box; }
  94. String DebugDumpTree(int depth) const override;
  95. private:
  96. Box box;
  97. };
  98. /**
  99. A box where flexbox layout occurs.
  100. */
  101. class FlexContainer final : public ContainerBox {
  102. public:
  103. FlexContainer(Element* element, ContainerBox* parent_container);
  104. // Submits the formatted box to the flex container element, and propagates any uncaught overflow to this box.
  105. // @returns True if it succeeds, otherwise false if it needs to be formatted again because scrollbars were enabled.
  106. bool Close(const Vector2f content_overflow_size, const Box& box, float element_baseline);
  107. float GetShrinkToFitWidth() const override;
  108. const Box* GetIfBox() const override { return &box; }
  109. String DebugDumpTree(int depth) const override;
  110. Box& GetBox() { return box; }
  111. private:
  112. Box box;
  113. };
  114. /**
  115. A box where table formatting occurs.
  116. As opposed to a flex container, the table wrapper cannot contain overflow or produce scrollbars.
  117. */
  118. class TableWrapper final : public ContainerBox {
  119. public:
  120. TableWrapper(Element* element, ContainerBox* parent_container);
  121. // Submits the formatted box to the table element, and propagates any uncaught overflow to this box.
  122. void Close(const Vector2f content_overflow_size, const Box& box, float element_baseline);
  123. float GetShrinkToFitWidth() const override;
  124. const Box* GetIfBox() const override { return &box; }
  125. String DebugDumpTree(int depth) const override;
  126. Box& GetBox() { return box; }
  127. private:
  128. Box box;
  129. };
  130. } // namespace Rml
  131. #endif