LayoutBlockBox.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCORELAYOUTBLOCKBOX_H
  28. #define ROCKETCORELAYOUTBLOCKBOX_H
  29. #include "LayoutLineBox.h"
  30. #include "../../Include/Rocket/Core/Box.h"
  31. #include "../../Include/Rocket/Core/Types.h"
  32. namespace Rocket {
  33. namespace Core {
  34. class LayoutBlockBoxSpace;
  35. class LayoutEngine;
  36. /**
  37. @author Peter Curry
  38. */
  39. class LayoutBlockBox
  40. {
  41. public:
  42. enum FormattingContext
  43. {
  44. BLOCK,
  45. INLINE
  46. };
  47. enum CloseResult
  48. {
  49. OK,
  50. LAYOUT_SELF,
  51. LAYOUT_PARENT
  52. };
  53. /// Creates a new block box for rendering a block element.
  54. /// @param layout_engine[in] The layout engine that created this block box.
  55. /// @param parent[in] The parent of this block box. This will be NULL for the root element.
  56. /// @param element[in] The element this block box is laying out.
  57. LayoutBlockBox(LayoutEngine* layout_engine, LayoutBlockBox* parent, Element* element);
  58. /// Creates a new block box in an inline context.
  59. /// @param layout_engine[in] The layout engine that created this block box.
  60. /// @param parent[in] The parent of this block box.
  61. LayoutBlockBox(LayoutEngine* layout_engine, LayoutBlockBox* parent);
  62. /// Releases the block box.
  63. ~LayoutBlockBox();
  64. /// Closes the box. This will determine the element's height (if it was unspecified).
  65. /// @return The result of the close; this may request a reformat of this element or our parent.
  66. CloseResult Close();
  67. /// Called by a closing block box child. Increments the cursor.
  68. /// @param child[in] The closing child block box.
  69. /// @return False if the block box caused an automatic vertical scrollbar to appear, forcing an entire reformat of the block box.
  70. bool CloseBlockBox(LayoutBlockBox* child);
  71. /// Called by a closing line box child. Increments the cursor, and creates a new line box to fit the overflow
  72. /// (if any).
  73. /// @param child[in] The closing child line box.
  74. /// @param overflow[in] The overflow from the closing line box. May be NULL if there was no overflow.
  75. /// @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).
  76. /// @return If the line box had overflow, this will be the last inline box created by the overflow.
  77. LayoutInlineBox* CloseLineBox(LayoutLineBox* child, LayoutInlineBox* overflow, LayoutInlineBox* overflow_chain);
  78. /// Adds a new block element to this block-context box.
  79. /// @param element[in] The new block element.
  80. /// @param placed[in] True if the element is to be placed, false otherwise.
  81. /// @return The block box representing the element. Once the element's children have been positioned, Close() must be called on it.
  82. LayoutBlockBox* AddBlockElement(Element* element);
  83. /// Adds a new inline element to this inline-context box.
  84. /// @param element[in] The new inline element.
  85. /// @param box[in] The box defining the element's bounds.
  86. /// @return The inline box representing the element. Once the element's children have been positioned, Close() must be called on it.
  87. LayoutInlineBox* AddInlineElement(Element* element, const Box& box);
  88. /// Adds a line-break to this block box.
  89. void AddBreak();
  90. /// Adds an element to this block box to be handled as a floating element.
  91. bool AddFloatElement(Element* element);
  92. /// Adds an element to this block box to be handled as an absolutely-positioned element. This element will be
  93. /// laid out, sized and positioned appropriately once this box is finished. This should only be called on boxes
  94. /// rendering in a block-context.
  95. /// @param element[in] The element to be positioned absolutely within this block box.
  96. void AddAbsoluteElement(Element* element);
  97. /// Formats, sizes, and positions all absolute elements in this block.
  98. void CloseAbsoluteElements();
  99. /// Returns the offset from the top-left corner of this box's offset element the next child box will be
  100. /// positioned at.
  101. /// @param[out] box_position The box cursor position.
  102. /// @param[in] top_margin The top margin of the box. This will be collapsed as appropriate against other block boxes.
  103. /// @param[in] clear_property The value of the underlying element's clear property.
  104. void PositionBox(Vector2f& box_position, float top_margin = 0, int clear_property = 0) const;
  105. /// Returns the offset from the top-left corner of this box's offset element the next child block box, of the
  106. /// given dimensions, will be positioned at. This will include the margins on the new block box.
  107. /// @param[out] box_position The block box cursor position.
  108. /// @param[in] box The dimensions of the new box.
  109. /// @param[in] clear_property The value of the underlying element's clear property.
  110. void PositionBlockBox(Vector2f& box_position, const Box& box, int clear_property) const;
  111. /// Returns the offset from the top-left corner of this box for the next line.
  112. /// @param box_position[out] The line box position.
  113. /// @param box_width[out] The available width for the line box.
  114. /// @param wrap_content[out] Set to true if the line box should grow to fit inline boxes, false if it should wrap them.
  115. /// @param dimensions[in] The minimum dimensions of the line.
  116. void PositionLineBox(Vector2f& box_position, float& box_width, bool& wrap_content, const Vector2f& dimensions) const;
  117. /// Returns the block box's element.
  118. /// @return The block box's element.
  119. Element* GetElement() const;
  120. /// Returns the block box's parent.
  121. /// @return The block box's parent.
  122. LayoutBlockBox* GetParent() const;
  123. /// Returns the position of the block box, relative to its parent's content area.
  124. /// @return The relative position of the block box.
  125. const Vector2f& GetPosition() const;
  126. /// Returns the block box against which all positions of boxes in the hierarchy are set relative to.
  127. /// @return This box's offset parent.
  128. LayoutBlockBox* GetOffsetParent() const;
  129. /// Returns the block box against which all positions of boxes in the hierarchy are calculated relative to.
  130. /// @return This box's offset root.
  131. LayoutBlockBox* GetOffsetRoot() const;
  132. /// Returns the block box's dimension box.
  133. /// @return The block box's dimension box.
  134. Box& GetBox();
  135. /// Returns the block box's dimension box.
  136. /// @return The block box's dimension box.
  137. const Box& GetBox() const;
  138. void* operator new(size_t size);
  139. void operator delete(void* chunk);
  140. private:
  141. struct AbsoluteElement
  142. {
  143. Element* element;
  144. Vector2f position;
  145. };
  146. // Closes our last block box, if it is an open inline block box.
  147. CloseResult CloseInlineBlockBox();
  148. // Positions a floating element within this block box.
  149. void PositionFloat(Element* element, float offset = 0);
  150. // Checks if we have a new vertical overflow on an auto-scrolling element. If so, our vertical scrollbar will
  151. // be enabled and our block boxes will be destroyed. All content will need to re-formatted. Returns true if no
  152. // overflow occured, false if it did.
  153. bool CatchVerticalOverflow(float cursor = -1);
  154. typedef std::vector< AbsoluteElement > AbsoluteElementList;
  155. typedef std::vector< LayoutBlockBox* > BlockBoxList;
  156. typedef std::vector< LayoutLineBox* > LineBoxList;
  157. // The object managing our space, as occupied by floating elements of this box and our ancestors.
  158. LayoutBlockBoxSpace* space;
  159. // The box's layout engine.
  160. LayoutEngine* layout_engine;
  161. // The element this box represents. This will be NULL for boxes rendering in an inline context.
  162. Element* element;
  163. // The element we'll be computing our offset relative to during layout.
  164. LayoutBlockBox* offset_root;
  165. // The element this block box's children are to be offset from.
  166. LayoutBlockBox* offset_parent;
  167. // The box's block parent. This will be NULL for the root of the box tree.
  168. LayoutBlockBox* parent;
  169. // The context of the box's context; either block or inline.
  170. FormattingContext context;
  171. // The block box's position.
  172. Vector2f position;
  173. // The block box's size.
  174. Box box;
  175. float min_height;
  176. float max_height;
  177. // 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.
  178. bool wrap_content;
  179. // The vertical position of the next block box to be added to this box, relative to the top of this box.
  180. float box_cursor;
  181. // Used by block contexts only; stores the list of block boxes under this box.
  182. BlockBoxList block_boxes;
  183. // Used by block contexts only; stores any elements that are to be absolutely positioned within this block box.
  184. AbsoluteElementList absolute_elements;
  185. // Used by block contexts only; stores an inline element hierarchy that was interrupted by a child block box.
  186. // The hierarchy will be resumed in an inline-context box once the intervening block box is completed.
  187. LayoutInlineBox* interrupted_chain;
  188. // Used by block contexts only; stores the value of the overflow property for the element.
  189. int overflow_x_property;
  190. int overflow_y_property;
  191. // Used by block contexts only; if true, we've enabled our vertical scrollbar.
  192. bool vertical_overflow;
  193. // Used by inline contexts only; stores the list of line boxes flowing inline content.
  194. LineBoxList line_boxes;
  195. // Used by inline contexts only; stores any floating elements that are waiting for a line break to be positioned.
  196. ElementList float_elements;
  197. };
  198. }
  199. }
  200. #endif