LayoutBlockBox.h

00001 /*
00002  * This source file is part of libRocket, the HTML/CSS Interface Middleware
00003  *
00004  * For the latest information, see http://www.librocket.com
00005  *
00006  * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy
00009  * of this software and associated documentation files (the "Software"), to deal
00010  * in the Software without restriction, including without limitation the rights
00011  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the Software is
00013  * furnished to do so, subject to the following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included in
00016  * all copies or substantial portions of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024  * THE SOFTWARE.
00025  *
00026  */
00027 
00028 #ifndef ROCKETCORELAYOUTBLOCKBOX_H
00029 #define ROCKETCORELAYOUTBLOCKBOX_H
00030 
00031 #include "LayoutLineBox.h"
00032 #include <Rocket/Core/Box.h>
00033 #include <Rocket/Core/Types.h>
00034 
00035 namespace Rocket {
00036 namespace Core {
00037 
00038 class LayoutBlockBoxSpace;
00039 class LayoutEngine;
00040 
00045 class LayoutBlockBox
00046 {
00047 public:
00048         enum FormattingContext
00049         {
00050                 BLOCK,
00051                 INLINE
00052         };
00053 
00054         enum CloseResult
00055         {
00056                 OK,
00057                 LAYOUT_SELF,
00058                 LAYOUT_PARENT
00059         };
00060 
00065         LayoutBlockBox(LayoutEngine* layout_engine, LayoutBlockBox* parent, Element* element);
00069         LayoutBlockBox(LayoutEngine* layout_engine, LayoutBlockBox* parent);
00071         ~LayoutBlockBox();
00072 
00075         CloseResult Close();
00076 
00080         bool CloseBlockBox(LayoutBlockBox* child);
00087         LayoutInlineBox* CloseLineBox(LayoutLineBox* child, LayoutInlineBox* overflow, LayoutInlineBox* overflow_chain);
00088 
00093         LayoutBlockBox* AddBlockElement(Element* element);
00098         LayoutInlineBox* AddInlineElement(Element* element, const Box& box);
00100         void AddBreak();
00101 
00103         bool AddFloatElement(Element* element);
00104 
00109         void AddAbsoluteElement(Element* element);
00111         void CloseAbsoluteElements();
00112 
00118         void PositionBox(Vector2f& box_position, float top_margin = 0, int clear_property = 0) const;
00124         void PositionBlockBox(Vector2f& box_position, const Box& box, int clear_property) const;
00130         void PositionLineBox(Vector2f& box_position, float& box_width, bool& wrap_content, const Vector2f& dimensions) const;
00131 
00134         Element* GetElement() const;
00135 
00138         LayoutBlockBox* GetParent() const;
00139 
00142         const Vector2f& GetPosition() const;
00143 
00146         LayoutBlockBox* GetOffsetParent() const;
00149         LayoutBlockBox* GetOffsetRoot() const;
00150 
00153         Box& GetBox();
00156         const Box& GetBox() const;
00157 
00158         void* operator new(size_t size);
00159         void operator delete(void* chunk);
00160 
00161 private:
00162         struct AbsoluteElement
00163         {
00164                 Element* element;
00165                 Vector2f position;
00166         };
00167 
00168         // Closes our last block box, if it is an open inline block box.
00169         CloseResult CloseInlineBlockBox();
00170 
00171         // Positions a floating element within this block box.
00172         void PositionFloat(Element* element, float offset = 0);
00173 
00174         // Checks if we have a new vertical overflow on an auto-scrolling element. If so, our vertical scrollbar will
00175         // be enabled and our block boxes will be destroyed. All content will need to re-formatted. Returns true if no
00176         // overflow occured, false if it did.
00177         bool CatchVerticalOverflow(float cursor = -1);
00178 
00179         typedef std::vector< AbsoluteElement > AbsoluteElementList;
00180         typedef std::vector< LayoutBlockBox* > BlockBoxList;
00181         typedef std::vector< LayoutLineBox* > LineBoxList;
00182 
00183         // The object managing our space, as occupied by floating elements of this box and our ancestors.
00184         LayoutBlockBoxSpace* space;
00185 
00186         // The box's layout engine.
00187         LayoutEngine* layout_engine;
00188         // The element this box represents. This will be NULL for boxes rendering in an inline context.
00189         Element* element;
00190 
00191         // The element we'll be computing our offset relative to during layout.
00192         LayoutBlockBox* offset_root;
00193         // The element this block box's children are to be offset from.
00194         LayoutBlockBox* offset_parent;
00195 
00196         // The box's block parent. This will be NULL for the root of the box tree.
00197         LayoutBlockBox* parent;
00198 
00199         // The context of the box's context; either block or inline.
00200         FormattingContext context;
00201 
00202         // The block box's position.
00203         Vector2f position;
00204         // The block box's size.
00205         Box box;
00206         float min_height;
00207         float max_height;
00208         // 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.
00209         bool wrap_content;
00210 
00211         // The vertical position of the next block box to be added to this box, relative to the top of this box.
00212         float box_cursor;
00213 
00214         // Used by block contexts only; stores the list of block boxes under this box.
00215         BlockBoxList block_boxes;
00216         // Used by block contexts only; stores any elements that are to be absolutely positioned within this block box.
00217         AbsoluteElementList absolute_elements;
00218         // Used by block contexts only; stores an inline element hierarchy that was interrupted by a child block box.
00219         // The hierarchy will be resumed in an inline-context box once the intervening block box is completed.
00220         LayoutInlineBox* interrupted_chain;
00221         // Used by block contexts only; stores the value of the overflow property for the element.
00222         int overflow_x_property;
00223         int overflow_y_property;
00224         // Used by block contexts only; if true, we've enabled our vertical scrollbar.
00225         bool vertical_overflow;
00226 
00227         // Used by inline contexts only; stores the list of line boxes flowing inline content.
00228         LineBoxList line_boxes;
00229         // Used by inline contexts only; stores any floating elements that are waiting for a line break to be positioned.
00230         ElementList float_elements;
00231 };
00232 
00233 }
00234 }
00235 
00236 #endif