ContainerBox.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #pragma once
  2. #include "../../../Include/RmlUi/Core/Box.h"
  3. #include "../../../Include/RmlUi/Core/StyleTypes.h"
  4. #include "../../../Include/RmlUi/Core/Types.h"
  5. #include "LayoutBox.h"
  6. namespace Rml {
  7. /**
  8. Abstraction for layout boxes that can act as a containing block.
  9. Implements functionality for catching overflow, and for handling positioned boxes that the box acts as a containing block for.
  10. */
  11. class ContainerBox : public LayoutBox {
  12. public:
  13. // Enable or disable scrollbars for the element we represent, preparing it for the first round of layouting, according to our properties.
  14. void ResetScrollbars(const Box& box);
  15. // Adds an absolutely positioned element, to be formatted and positioned when closing this container, see 'ClosePositionedElements'.
  16. void AddAbsoluteElement(Element* element, Vector2f static_position, Element* static_relative_offset_parent);
  17. // Adds a relatively positioned element which we act as a containing block for.
  18. void AddRelativeElement(Element* element);
  19. ContainerBox* GetParent() { return parent_container; }
  20. Element* GetElement() { return element; }
  21. bool IsScrollContainer() const;
  22. // Returns true if this box acts as a containing block for absolutely positioned descendants.
  23. bool IsAbsolutePositioningContainingBlock() const { return is_absolute_positioning_containing_block; }
  24. protected:
  25. ContainerBox(Type type, Element* element, ContainerBox* parent_container);
  26. /// Checks if we have a new overflow on an auto-scrolling element. If so, our vertical scrollbar will be enabled and
  27. /// our block boxes will be destroyed. All content will need to be re-formatted.
  28. /// @param[in] content_overflow_size The size of the visible content, relative to our content area.
  29. /// @param[in] box The box built for the element, possibly with a non-determinate height.
  30. /// @param[in] max_height Maximum height of the content area, if any.
  31. /// @returns True if no overflow occured, false if it did.
  32. bool CatchOverflow(const Vector2f content_overflow_size, const Box& box, const float max_height) const;
  33. /// Set the box and scrollable area on our element, possibly catching any overflow.
  34. /// @param[in] content_overflow_size The size of the visible content, relative to our content area.
  35. /// @param[in] box The box to be set on the element.
  36. /// @param[in] max_height Maximum height of the content area, if any.
  37. /// @returns True if no overflow occured, false if it did.
  38. bool SubmitBox(const Vector2f content_overflow_size, const Box& box, const float max_height);
  39. /// Formats, sizes, and positions all absolute elements whose containing block is this, and offsets relative elements.
  40. void ClosePositionedElements();
  41. // Set the element's baseline (proxy for private access to Element).
  42. void SetElementBaseline(float element_baseline);
  43. // Calls Element::OnLayout (proxy for private access to Element).
  44. void SubmitElementLayout();
  45. // The element this box represents, if any.
  46. Element* const element;
  47. private:
  48. struct AbsoluteElement {
  49. Vector2f static_position; // The hypothetical position of the element as if it was placed in normal flow.
  50. Element* static_position_offset_parent; // The element for which the static position is offset from.
  51. };
  52. using AbsoluteElementMap = SmallUnorderedMap<Element*, AbsoluteElement>;
  53. AbsoluteElementMap absolute_elements; // List of absolutely positioned elements that we act as a containing block for.
  54. ElementList relative_elements; // List of relatively positioned elements that we act as a containing block for.
  55. Style::Overflow overflow_x = Style::Overflow::Visible;
  56. Style::Overflow overflow_y = Style::Overflow::Visible;
  57. bool is_absolute_positioning_containing_block = false;
  58. ContainerBox* parent_container = nullptr;
  59. };
  60. /**
  61. The root of a tree of layout boxes, usually represents the root element or viewport.
  62. */
  63. class RootBox final : public ContainerBox {
  64. public:
  65. RootBox(Vector2f containing_block) : ContainerBox(Type::Root, nullptr, nullptr), box(containing_block) {}
  66. RootBox(const Box& box) : ContainerBox(Type::Root, nullptr, nullptr), box(box) {}
  67. const Box* GetIfBox() const override { return &box; }
  68. String DebugDumpTree(int depth) const override;
  69. private:
  70. Box box;
  71. };
  72. /**
  73. A box where flexbox layout occurs.
  74. */
  75. class FlexContainer final : public ContainerBox {
  76. public:
  77. FlexContainer(Element* element, ContainerBox* parent_container);
  78. // Submits the formatted box to the flex container element, and propagates any uncaught overflow to this box.
  79. // @returns True if it succeeds, otherwise false if it needs to be formatted again because scrollbars were enabled.
  80. bool Close(const Vector2f content_overflow_size, const Box& box, float element_baseline);
  81. float GetShrinkToFitWidth() const override;
  82. const Box* GetIfBox() const override { return &box; }
  83. String DebugDumpTree(int depth) const override;
  84. Box& GetBox() { return box; }
  85. private:
  86. Box box;
  87. };
  88. /**
  89. A box where table formatting occurs.
  90. As opposed to a flex container, the table wrapper cannot contain overflow or produce scrollbars.
  91. */
  92. class TableWrapper final : public ContainerBox {
  93. public:
  94. TableWrapper(Element* element, ContainerBox* parent_container);
  95. // Submits the formatted box to the table element, and propagates any uncaught overflow to this box.
  96. void Close(const Vector2f content_overflow_size, const Box& box, float element_baseline);
  97. float GetShrinkToFitWidth() const override;
  98. const Box* GetIfBox() const override { return &box; }
  99. String DebugDumpTree(int depth) const override;
  100. Box& GetBox() { return box; }
  101. private:
  102. Box box;
  103. };
  104. } // namespace Rml