BlockContainer.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_BLOCKCONTAINER_H
  29. #define RMLUI_CORE_LAYOUT_BLOCKCONTAINER_H
  30. #include "../../../Include/RmlUi/Core/Types.h"
  31. #include "ContainerBox.h"
  32. namespace Rml {
  33. class FloatedBoxSpace;
  34. class LineBox;
  35. class InlineBox;
  36. class InlineContainer;
  37. struct InlineBoxHandle {
  38. InlineBox* inline_box;
  39. };
  40. /**
  41. A container for block-level boxes.
  42. Takes part in a block formatting context. Block-level boxes are stacked vertically as they are added. Inline-level
  43. elements create a new inline container if one is not already open. The inline container itself acts as an anonymous
  44. block-level box in this block container.
  45. Positions are relative to the root of the block formatting context this box takes part in, unless otherwise noted.
  46. However, elements themselves are offset relative to their containing block. We act as the containing block for
  47. static and relative children, and possibly for absolutely positioned descendants.
  48. */
  49. class BlockContainer final : public ContainerBox {
  50. public:
  51. /// Creates a new block box for rendering a block element.
  52. /// @param[in] parent The parent of this block box. This will be nullptr for the root element.
  53. /// @param[in] element The element this block box is laying out.
  54. /// @param[in] box The box used for this block box.
  55. /// @param[in] min_height The minimum height of the content box.
  56. /// @param[in] max_height The maximum height of the content box.
  57. BlockContainer(ContainerBox* parent_container, FloatedBoxSpace* space, Element* element, const Box& box, float min_height, float max_height);
  58. ~BlockContainer();
  59. /// Closes the box. This will determine this container's height if it was unspecified.
  60. /// @param[in] parent_block_container Our parent which will be sized to contain this box, or nullptr for the root of the block formatting context.
  61. /// @return False if the block box caused an automatic vertical scrollbar to appear, forcing a reformat of the current block formatting context.
  62. bool Close(BlockContainer* parent_block_container);
  63. /// Creates and opens a new block box, and adds it as a child of this one.
  64. /// @param[in] element The new block element.
  65. /// @param[in] box The dimensions of the new box, whose content height may be indefinite (-1).
  66. /// @param[in] min_height The minimum height of the content box.
  67. /// @param[in] max_height The maximum height of the content box.
  68. /// @return The block box representing the element. Once the element's children have been positioned, Close() must be called on it.
  69. BlockContainer* OpenBlockBox(Element* element, const Box& box, float min_height, float max_height);
  70. /// Adds a block-level box whose contents have already been formatted in an independent formatting context.
  71. /// @param[in] block_level_box The box to add as a new child of this.
  72. /// @param[in] element The element represented by the new box.
  73. /// @param[in] box The dimensions of the new box.
  74. /// @return A pointer to the added block-level box.
  75. LayoutBox* AddBlockLevelBox(UniquePtr<LayoutBox> block_level_box, Element* element, const Box& box);
  76. // Adds an element to this block container to be handled as a floating element.
  77. void AddFloatElement(Element* element, Vector2f visible_overflow_size);
  78. /// Adds a new inline-level node to this block container.
  79. /// @param[in] node The new inline node.
  80. /// @param[in] box The box defining the node's bounds.
  81. /// @return A handle for the inline box, which must later be submitted to 'CloseInlineNode()'.
  82. /// @note Adds a new inline container to this box if needed, which starts a new inline formatting context.
  83. InlineBoxHandle AddInlineNode(Node* node, const Box& box);
  84. /// Closes a previously added inline node. This must be called after all its children have been added.
  85. /// @param[in] handle A handle previously returned from 'AddInlineNode()'.
  86. void CloseInlineNode(InlineBoxHandle handle);
  87. // Adds a line-break to this block box.
  88. void AddBreak();
  89. // Estimates the static position of a hypothetical next element to be placed.
  90. Vector2f GetOpenStaticPosition(Style::Display display) const;
  91. /// Returns the position of a new child box to be placed here, relative to the block formatting context space.
  92. Vector2f NextBoxPosition() const;
  93. /// Returns the position of a new child box to be placed here. Collapses adjacent margins and optionally clears floats.
  94. /// @param[in] child_box The dimensions of the new box.
  95. /// @param[in] clear_property Specifies any floated boxes to be cleared (vertically skipped).
  96. /// @return The next position in the block formatting context space.
  97. Vector2f NextBoxPosition(const Box& child_box, Style::Clear clear_property) const;
  98. // Places all queued floating elements.
  99. void PlaceQueuedFloats(float vertical_position);
  100. // Resets this box, so that it can be formatted again.
  101. void ResetContents();
  102. Vector2f GetPosition() const;
  103. Element* GetElement() const;
  104. const FloatedBoxSpace* GetBlockBoxSpace() const;
  105. Box& GetBox();
  106. const Box& GetBox() const;
  107. // -- Inherited from LayoutBox --
  108. const Box* GetIfBox() const override;
  109. float GetShrinkToFitWidth() const override;
  110. bool GetBaselineOfLastLine(float& out_baseline) const override;
  111. private:
  112. InlineContainer* EnsureOpenInlineContainer();
  113. InlineContainer* GetOpenInlineContainer();
  114. const InlineContainer* GetOpenInlineContainer() const;
  115. const LayoutBox* GetOpenLayoutBox() const;
  116. /// Increment our cursor and content size, to enclose a sized block-level child box.
  117. /// @param[in] child The child box.
  118. /// @param[in] child_position The border position of the child, relative to the current block formatting context.
  119. /// @param[in] child_size The border size of the child.
  120. /// @param[in] child_margin_bottom The bottom margin width of the child.
  121. /// @return False if this caused an automatic vertical scrollbar to appear, forcing a reformat of the current block formatting context.
  122. bool EncloseChildBox(LayoutBox* child, Vector2f child_position, float child_height, float child_margin_bottom);
  123. // Closes the inline container if there is one open. Returns false if our formatting context needs to be reformatted.
  124. bool CloseOpenInlineContainer();
  125. // Ensures that the interrupted line box is empty, otherwise it indicates an internal error.
  126. void EnsureEmptyInterruptedLineBox();
  127. // Positions a floating element within this block box.
  128. void PlaceFloat(Element* element, float vertical_position, Vector2f visible_overflow_size);
  129. // Debug dump layout tree.
  130. String DebugDumpTree(int depth) const override;
  131. using LayoutBoxList = Vector<UniquePtr<LayoutBox>>;
  132. struct QueuedFloat {
  133. Element* element;
  134. Vector2f visible_overflow_size;
  135. };
  136. using QueuedFloatList = Vector<QueuedFloat>;
  137. // Position of this box, relative to the border area of the root of our block formatting context.
  138. Vector2f position;
  139. Box box;
  140. float min_height = 0.f;
  141. float max_height = -1.f;
  142. // The vertical position of the next block box to be added to this box, relative to our box's top content edge.
  143. float box_cursor = 0.f;
  144. // Stores the floated boxes in the current block formatting context, if we are the root of the formatting context.
  145. UniquePtr<FloatedBoxSpace> root_space;
  146. // Pointer to the floated box space of the current block formatting context. [not-null]
  147. FloatedBoxSpace* space;
  148. // List of block-level boxes contained in this box.
  149. LayoutBoxList child_boxes;
  150. // Stores floated elements that are waiting for a line break to be positioned.
  151. QueuedFloatList queued_float_elements;
  152. // Stores the unplaced part of a line box that was split by a block-level box.
  153. UniquePtr<LineBox> interrupted_line_box;
  154. // The inner content size (excluding any padding/border/margins).
  155. // This is extended as child block boxes are closed, or from external formatting contexts.
  156. Vector2f inner_content_size;
  157. };
  158. } // namespace Rml
  159. #endif