1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef RenderFlexibleBox_h
32#define RenderFlexibleBox_h
33
34#include "RenderBlock.h"
35
36namespace WebCore {
37
38class RenderFlexibleBox : public RenderBlock {
39public:
40    RenderFlexibleBox(Element*);
41    virtual ~RenderFlexibleBox();
42
43    static RenderFlexibleBox* createAnonymous(Document*);
44
45    virtual const char* renderName() const OVERRIDE;
46
47    virtual bool isFlexibleBox() const OVERRIDE { return true; }
48    virtual bool avoidsFloats() const OVERRIDE { return true; }
49    virtual bool canCollapseAnonymousBlockChild() const OVERRIDE { return false; }
50    virtual void layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight = 0) OVERRIDE;
51
52    virtual int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const OVERRIDE;
53    virtual int firstLineBoxBaseline() const OVERRIDE;
54    virtual int inlineBlockBaseline(LineDirectionMode) const OVERRIDE;
55
56    virtual void paintChildren(PaintInfo& forSelf, const LayoutPoint&, PaintInfo& forChild, bool usePrintRect) OVERRIDE;
57
58    bool isHorizontalFlow() const;
59
60protected:
61    virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const OVERRIDE;
62    virtual void computePreferredLogicalWidths() OVERRIDE;
63
64    virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
65
66private:
67    enum FlexSign {
68        PositiveFlexibility,
69        NegativeFlexibility,
70    };
71
72    enum PositionedLayoutMode {
73        FlipForRowReverse,
74        NoFlipForRowReverse,
75    };
76
77    struct OrderHashTraits;
78    typedef HashSet<int, DefaultHash<int>::Hash, OrderHashTraits> OrderHashSet;
79
80    class OrderIterator {
81        WTF_MAKE_NONCOPYABLE(OrderIterator);
82    public:
83        OrderIterator(const RenderFlexibleBox*);
84        void setOrderValues(const OrderHashSet&);
85        RenderBox* currentChild() const { return m_currentChild; }
86        RenderBox* first();
87        RenderBox* next();
88        void reset();
89
90    private:
91        const RenderFlexibleBox* m_flexibleBox;
92        RenderBox* m_currentChild;
93        Vector<int> m_orderValues;
94        Vector<int>::const_iterator m_orderValuesIterator;
95    };
96
97    typedef HashMap<const RenderBox*, LayoutUnit> InflexibleFlexItemSize;
98    typedef Vector<RenderBox*> OrderedFlexItemList;
99
100    struct LineContext;
101    struct Violation;
102
103    // Use an inline capacity of 8, since flexbox containers usually have less than 8 children.
104    typedef Vector<LayoutRect, 8> ChildFrameRects;
105
106    bool hasOrthogonalFlow(RenderBox* child) const;
107    bool isColumnFlow() const;
108    bool isLeftToRightFlow() const;
109    bool isMultiline() const;
110    Length flexBasisForChild(RenderBox* child) const;
111    void setCrossAxisExtent(LayoutUnit);
112    LayoutUnit crossAxisExtentForChild(RenderBox* child) const;
113    LayoutUnit mainAxisExtentForChild(RenderBox* child) const;
114    LayoutUnit crossAxisExtent() const;
115    LayoutUnit mainAxisExtent() const;
116    LayoutUnit crossAxisContentExtent() const;
117    LayoutUnit mainAxisContentExtent(LayoutUnit contentLogicalHeight);
118    LayoutUnit computeMainAxisExtentForChild(RenderBox* child, SizeType, const Length& size);
119    WritingMode transformedWritingMode() const;
120    LayoutUnit flowAwareBorderStart() const;
121    LayoutUnit flowAwareBorderEnd() const;
122    LayoutUnit flowAwareBorderBefore() const;
123    LayoutUnit flowAwareBorderAfter() const;
124    LayoutUnit flowAwarePaddingStart() const;
125    LayoutUnit flowAwarePaddingEnd() const;
126    LayoutUnit flowAwarePaddingBefore() const;
127    LayoutUnit flowAwarePaddingAfter() const;
128    LayoutUnit flowAwareMarginStartForChild(RenderBox* child) const;
129    LayoutUnit flowAwareMarginEndForChild(RenderBox* child) const;
130    LayoutUnit flowAwareMarginBeforeForChild(RenderBox* child) const;
131    LayoutUnit flowAwareMarginAfterForChild(RenderBox* child) const;
132    LayoutUnit crossAxisMarginExtentForChild(RenderBox* child) const;
133    LayoutUnit crossAxisScrollbarExtent() const;
134    LayoutPoint flowAwareLocationForChild(RenderBox* child) const;
135    // FIXME: Supporting layout deltas.
136    void setFlowAwareLocationForChild(RenderBox* child, const LayoutPoint&);
137    void adjustAlignmentForChild(RenderBox* child, LayoutUnit);
138    EAlignItems alignmentForChild(RenderBox* child) const;
139    LayoutUnit mainAxisBorderAndPaddingExtentForChild(RenderBox* child) const;
140    LayoutUnit mainAxisScrollbarExtentForChild(RenderBox* child) const;
141    LayoutUnit preferredMainAxisContentExtentForChild(RenderBox* child, bool hasInfiniteLineLength);
142
143    void layoutFlexItems(bool relayoutChildren, Vector<LineContext>&);
144    LayoutUnit autoMarginOffsetInMainAxis(const OrderedFlexItemList&, LayoutUnit& availableFreeSpace);
145    void updateAutoMarginsInMainAxis(RenderBox* child, LayoutUnit autoMarginOffset);
146    bool hasAutoMarginsInCrossAxis(RenderBox* child) const;
147    bool updateAutoMarginsInCrossAxis(RenderBox* child, LayoutUnit availableAlignmentSpace);
148    void repositionLogicalHeightDependentFlexItems(Vector<LineContext>&);
149    LayoutUnit clientLogicalBottomAfterRepositioning();
150    void appendChildFrameRects(ChildFrameRects&);
151    void repaintChildrenDuringLayoutIfMoved(const ChildFrameRects&);
152
153    LayoutUnit availableAlignmentSpaceForChild(LayoutUnit lineCrossAxisExtent, RenderBox*);
154    LayoutUnit marginBoxAscentForChild(RenderBox*);
155
156    LayoutUnit computeChildMarginValue(Length margin, RenderView*);
157    void computeMainAxisPreferredSizes(OrderHashSet&);
158    LayoutUnit adjustChildSizeForMinAndMax(RenderBox*, LayoutUnit childSize);
159    bool computeNextFlexLine(OrderedFlexItemList& orderedChildren, LayoutUnit& preferredMainAxisExtent, double& totalFlexGrow, double& totalWeightedFlexShrink, LayoutUnit& minMaxAppliedMainAxisExtent, bool& hasInfiniteLineLength);
160
161    bool resolveFlexibleLengths(FlexSign, const OrderedFlexItemList&, LayoutUnit& availableFreeSpace, double& totalFlexGrow, double& totalWeightedFlexShrink, InflexibleFlexItemSize&, Vector<LayoutUnit>& childSizes, bool hasInfiniteLineLength);
162    void freezeViolations(const Vector<Violation>&, LayoutUnit& availableFreeSpace, double& totalFlexGrow, double& totalWeightedFlexShrink, InflexibleFlexItemSize&, bool hasInfiniteLineLength);
163
164    void resetAutoMarginsAndLogicalTopInCrossAxis(RenderBox*);
165    bool needToStretchChild(RenderBox*);
166    void setLogicalOverrideSize(RenderBox* child, LayoutUnit childPreferredSize);
167    void prepareChildForPositionedLayout(RenderBox* child, LayoutUnit mainAxisOffset, LayoutUnit crossAxisOffset, PositionedLayoutMode);
168    size_t numberOfInFlowPositionedChildren(const OrderedFlexItemList&) const;
169    void layoutAndPlaceChildren(LayoutUnit& crossAxisOffset, const OrderedFlexItemList&, const Vector<LayoutUnit>& childSizes, LayoutUnit availableFreeSpace, bool relayoutChildren, Vector<LineContext>&);
170    void layoutColumnReverse(const OrderedFlexItemList&, LayoutUnit crossAxisOffset, LayoutUnit availableFreeSpace);
171    void alignFlexLines(Vector<LineContext>&);
172    void alignChildren(const Vector<LineContext>&);
173    void applyStretchAlignmentToChild(RenderBox*, LayoutUnit lineCrossAxisExtent);
174    void flipForRightToLeftColumn();
175    void flipForWrapReverse(const Vector<LineContext>&, LayoutUnit crossAxisStartEdge);
176
177    mutable OrderIterator m_orderIterator;
178    int m_numberOfInFlowChildrenOnFirstLine;
179};
180
181inline RenderFlexibleBox* toRenderFlexibleBox(RenderObject* object)
182{
183    ASSERT_WITH_SECURITY_IMPLICATION(!object || object->isFlexibleBox());
184    return static_cast<RenderFlexibleBox*>(object);
185}
186
187inline const RenderFlexibleBox* toRenderFlexibleBox(const RenderObject* object)
188{
189    ASSERT_WITH_SECURITY_IMPLICATION(!object || object->isFlexibleBox());
190    return static_cast<const RenderFlexibleBox*>(object);
191}
192
193// This will catch anyone doing an unnecessary cast.
194void toRenderFlexibleBox(const RenderFlexibleBox*);
195
196} // namespace WebCore
197
198#endif // RenderFlexibleBox_h
199