LayoutBlockBox.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 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 RMLUICORELAYOUTBLOCKBOX_H
  29. #define RMLUICORELAYOUTBLOCKBOX_H
  30. #include "LayoutLineBox.h"
  31. #include "../../Include/RmlUi/Core/Box.h"
  32. #include "../../Include/RmlUi/Core/Types.h"
  33. namespace Rml {
  34. namespace Core {
  35. class LayoutBlockBoxSpace;
  36. class LayoutEngine;
  37. /**
  38. @author Peter Curry
  39. */
  40. class LayoutBlockBox
  41. {
  42. public:
  43. enum FormattingContext
  44. {
  45. BLOCK,
  46. INLINE
  47. };
  48. enum CloseResult
  49. {
  50. OK,
  51. LAYOUT_SELF,
  52. LAYOUT_PARENT
  53. };
  54. /// Creates a new block box for rendering a block element.
  55. /// @param layout_engine[in] The layout engine that created this block box.
  56. /// @param parent[in] The parent of this block box. This will be nullptr for the root element.
  57. /// @param element[in] The element this block box is laying out.
  58. LayoutBlockBox(LayoutEngine* layout_engine, LayoutBlockBox* parent, Element* element);
  59. /// Creates a new block box in an inline context.
  60. /// @param layout_engine[in] The layout engine that created this block box.
  61. /// @param parent[in] The parent of this block box.
  62. LayoutBlockBox(LayoutEngine* layout_engine, LayoutBlockBox* parent);
  63. /// Releases the block box.
  64. ~LayoutBlockBox();
  65. /// Closes the box. This will determine the element's height (if it was unspecified).
  66. /// @return The result of the close; this may request a reformat of this element or our parent.
  67. CloseResult Close();
  68. /// Called by a closing block box child. Increments the cursor.
  69. /// @param child[in] The closing child block box.
  70. /// @return False if the block box caused an automatic vertical scrollbar to appear, forcing an entire reformat of the block box.
  71. bool CloseBlockBox(LayoutBlockBox* child);
  72. /// Called by a closing line box child. Increments the cursor, and creates a new line box to fit the overflow
  73. /// (if any).
  74. /// @param child[in] The closing child line box.
  75. /// @param overflow[in] The overflow from the closing line box. May be nullptr if there was no overflow.
  76. /// @param overflow_chain[in] The end of the chained hierarchy to be spilled over to the new line, as the parent to the overflow box (if one exists).
  77. /// @return If the line box had overflow, this will be the last inline box created by the overflow.
  78. LayoutInlineBox* CloseLineBox(LayoutLineBox* child, LayoutInlineBox* overflow, LayoutInlineBox* overflow_chain);
  79. /// Adds a new block element to this block-context box.
  80. /// @param element[in] The new block element.
  81. /// @param placed[in] True if the element is to be placed, false otherwise.
  82. /// @return The block box representing the element. Once the element's children have been positioned, Close() must be called on it.
  83. LayoutBlockBox* AddBlockElement(Element* element);
  84. /// Adds a new inline element to this inline-context box.
  85. /// @param element[in] The new inline element.
  86. /// @param box[in] The box defining the element's bounds.
  87. /// @return The inline box representing the element. Once the element's children have been positioned, Close() must be called on it.
  88. LayoutInlineBox* AddInlineElement(Element* element, const Box& box);
  89. /// Adds a line-break to this block box.
  90. void AddBreak();
  91. /// Adds an element to this block box to be handled as a floating element.
  92. bool AddFloatElement(Element* element);
  93. /// Adds an element to this block box to be handled as an absolutely-positioned element. This element will be
  94. /// laid out, sized and positioned appropriately once this box is finished. This should only be called on boxes
  95. /// rendering in a block-context.
  96. /// @param element[in] The element to be positioned absolutely within this block box.
  97. void AddAbsoluteElement(Element* element);
  98. /// Formats, sizes, and positions all absolute elements in this block.
  99. void CloseAbsoluteElements();
  100. /// Returns the offset from the top-left corner of this box's offset element the next child box will be
  101. /// positioned at.
  102. /// @param[out] box_position The box cursor position.
  103. /// @param[in] top_margin The top margin of the box. This will be collapsed as appropriate against other block boxes.
  104. /// @param[in] clear_property The value of the underlying element's clear property.
  105. void PositionBox(Vector2f& box_position, float top_margin = 0, Style::Clear clear_property = Style::Clear::None) const;
  106. /// Returns the offset from the top-left corner of this box's offset element the next child block box, of the
  107. /// given dimensions, will be positioned at. This will include the margins on the new block box.
  108. /// @param[out] box_position The block box cursor position.
  109. /// @param[in] box The dimensions of the new box.
  110. /// @param[in] clear_property The value of the underlying element's clear property.
  111. void PositionBlockBox(Vector2f& box_position, const Box& box, Style::Clear clear_property) const;
  112. /// Returns the offset from the top-left corner of this box for the next line.
  113. /// @param box_position[out] The line box position.
  114. /// @param box_width[out] The available width for the line box.
  115. /// @param wrap_content[out] Set to true if the line box should grow to fit inline boxes, false if it should wrap them.
  116. /// @param dimensions[in] The minimum dimensions of the line.
  117. void PositionLineBox(Vector2f& box_position, float& box_width, bool& wrap_content, const Vector2f& dimensions) const;
  118. float InternalContentWidth() const;
  119. /// Returns the block box's element.
  120. /// @return The block box's element.
  121. Element* GetElement() const;
  122. /// Returns the block box's parent.
  123. /// @return The block box's parent.
  124. LayoutBlockBox* GetParent() const;
  125. /// Returns the position of the block box, relative to its parent's content area.
  126. /// @return The relative position of the block box.
  127. const Vector2f& GetPosition() const;
  128. /// Returns the block box against which all positions of boxes in the hierarchy are set relative to.
  129. /// @return This box's offset parent.
  130. LayoutBlockBox* GetOffsetParent() const;
  131. /// Returns the block box against which all positions of boxes in the hierarchy are calculated relative to.
  132. /// @return This box's offset root.
  133. LayoutBlockBox* GetOffsetRoot() const;
  134. /// Returns the block box's dimension box.
  135. /// @return The block box's dimension box.
  136. Box& GetBox();
  137. /// Returns the block box's dimension box.
  138. /// @return The block box's dimension box.
  139. const Box& GetBox() const;
  140. void* operator new(size_t size);
  141. void operator delete(void* chunk);
  142. private:
  143. struct AbsoluteElement
  144. {
  145. Element* element;
  146. Vector2f position;
  147. };
  148. // Closes our last block box, if it is an open inline block box.
  149. CloseResult CloseInlineBlockBox();
  150. // Positions a floating element within this block box.
  151. void PositionFloat(Element* element, float offset = 0);
  152. // Checks if we have a new vertical overflow on an auto-scrolling element. If so, our vertical scrollbar will
  153. // be enabled and our block boxes will be destroyed. All content will need to re-formatted. Returns true if no
  154. // overflow occured, false if it did.
  155. bool CatchVerticalOverflow(float cursor = -1);
  156. typedef std::vector< AbsoluteElement > AbsoluteElementList;
  157. typedef std::vector< LayoutBlockBox* > BlockBoxList;
  158. typedef std::vector< LayoutLineBox* > LineBoxList;
  159. // The object managing our space, as occupied by floating elements of this box and our ancestors.
  160. LayoutBlockBoxSpace* space;
  161. // The box's layout engine.
  162. LayoutEngine* layout_engine;
  163. // The element this box represents. This will be nullptr for boxes rendering in an inline context.
  164. Element* element;
  165. // The element we'll be computing our offset relative to during layout.
  166. LayoutBlockBox* offset_root;
  167. // The element this block box's children are to be offset from.
  168. LayoutBlockBox* offset_parent;
  169. // The box's block parent. This will be nullptr for the root of the box tree.
  170. LayoutBlockBox* parent;
  171. // The context of the box's context; either block or inline.
  172. FormattingContext context;
  173. // The block box's position.
  174. Vector2f position;
  175. // The block box's size.
  176. Box box;
  177. float min_height;
  178. float max_height;
  179. // Used by inline contexts only; set to true if the block box's line boxes should stretch to fit their inline content instead of wrapping.
  180. bool wrap_content;
  181. // The vertical position of the next block box to be added to this box, relative to the top of this box.
  182. float box_cursor;
  183. // Used by block contexts only; stores the list of block boxes under this box.
  184. BlockBoxList block_boxes;
  185. // Used by block contexts only; stores any elements that are to be absolutely positioned within this block box.
  186. AbsoluteElementList absolute_elements;
  187. // Used by block contexts only; stores an inline element hierarchy that was interrupted by a child block box.
  188. // The hierarchy will be resumed in an inline-context box once the intervening block box is completed.
  189. LayoutInlineBox* interrupted_chain;
  190. // Used by block contexts only; stores the value of the overflow property for the element.
  191. Style::Overflow overflow_x_property;
  192. Style::Overflow overflow_y_property;
  193. // Used by block contexts only; if true, we've enabled our vertical scrollbar.
  194. bool vertical_overflow;
  195. // Used by inline contexts only; stores the list of line boxes flowing inline content.
  196. LineBoxList line_boxes;
  197. // Used by inline contexts only; stores any floating elements that are waiting for a line break to be positioned.
  198. ElementList float_elements;
  199. };
  200. }
  201. }
  202. #endif