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 "core/rendering/OrderIterator.h"
35#include "core/rendering/RenderBlock.h"
36
37namespace blink {
38
39class RenderFlexibleBox : public RenderBlock {
40public:
41    RenderFlexibleBox(Element*);
42    virtual ~RenderFlexibleBox();
43
44    static RenderFlexibleBox* createAnonymous(Document*);
45
46    virtual const char* renderName() const OVERRIDE;
47
48    virtual bool isFlexibleBox() const OVERRIDE FINAL { return true; }
49    virtual bool canCollapseAnonymousBlockChild() const OVERRIDE { return false; }
50    virtual void layoutBlock(bool relayoutChildren) OVERRIDE FINAL;
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&, const LayoutPoint&) OVERRIDE FINAL;
57
58    bool isHorizontalFlow() const;
59
60    OrderIterator& orderIterator() { return m_orderIterator; }
61
62protected:
63    virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const OVERRIDE;
64
65    virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) OVERRIDE;
66    virtual void removeChild(RenderObject*) OVERRIDE;
67
68private:
69    enum FlexSign {
70        PositiveFlexibility,
71        NegativeFlexibility,
72    };
73
74    enum PositionedLayoutMode {
75        FlipForRowReverse,
76        NoFlipForRowReverse,
77    };
78
79    typedef HashMap<const RenderBox*, LayoutUnit> InflexibleFlexItemSize;
80    typedef Vector<RenderBox*> OrderedFlexItemList;
81
82    struct LineContext;
83    struct Violation;
84
85    // Use an inline capacity of 8, since flexbox containers usually have less than 8 children.
86    typedef Vector<LayoutRect, 8> ChildFrameRects;
87
88    bool hasOrthogonalFlow(RenderBox& child) const;
89    bool isColumnFlow() const;
90    bool isLeftToRightFlow() const;
91    bool isMultiline() const;
92    Length flexBasisForChild(RenderBox& child) const;
93    LayoutUnit crossAxisExtentForChild(RenderBox& child) const;
94    LayoutUnit crossAxisIntrinsicExtentForChild(RenderBox& child) const;
95    LayoutUnit childIntrinsicHeight(RenderBox& child) const;
96    LayoutUnit childIntrinsicWidth(RenderBox& child) const;
97    LayoutUnit mainAxisExtentForChild(RenderBox& child) const;
98    LayoutUnit crossAxisExtent() const;
99    LayoutUnit mainAxisExtent() const;
100    LayoutUnit crossAxisContentExtent() const;
101    LayoutUnit mainAxisContentExtent(LayoutUnit contentLogicalHeight);
102    LayoutUnit computeMainAxisExtentForChild(RenderBox& child, SizeType, const Length& size);
103    WritingMode transformedWritingMode() const;
104    LayoutUnit flowAwareBorderStart() const;
105    LayoutUnit flowAwareBorderEnd() const;
106    LayoutUnit flowAwareBorderBefore() const;
107    LayoutUnit flowAwareBorderAfter() const;
108    LayoutUnit flowAwarePaddingStart() const;
109    LayoutUnit flowAwarePaddingEnd() const;
110    LayoutUnit flowAwarePaddingBefore() const;
111    LayoutUnit flowAwarePaddingAfter() const;
112    LayoutUnit flowAwareMarginStartForChild(RenderBox& child) const;
113    LayoutUnit flowAwareMarginEndForChild(RenderBox& child) const;
114    LayoutUnit flowAwareMarginBeforeForChild(RenderBox& child) const;
115    LayoutUnit crossAxisMarginExtentForChild(RenderBox& child) const;
116    LayoutUnit crossAxisScrollbarExtent() const;
117    LayoutUnit crossAxisScrollbarExtentForChild(RenderBox& child) const;
118    LayoutPoint flowAwareLocationForChild(RenderBox& child) const;
119    // FIXME: Supporting layout deltas.
120    void setFlowAwareLocationForChild(RenderBox& child, const LayoutPoint&);
121    void adjustAlignmentForChild(RenderBox& child, LayoutUnit);
122    ItemPosition alignmentForChild(RenderBox& child) const;
123    LayoutUnit mainAxisBorderAndPaddingExtentForChild(RenderBox& child) const;
124    LayoutUnit preferredMainAxisContentExtentForChild(RenderBox& child, bool hasInfiniteLineLength, bool relayoutChildren = false);
125    bool childPreferredMainAxisContentExtentRequiresLayout(RenderBox& child, bool hasInfiniteLineLength) const;
126    bool needToStretchChildLogicalHeight(RenderBox& child) const;
127
128    void layoutFlexItems(bool relayoutChildren);
129    LayoutUnit autoMarginOffsetInMainAxis(const OrderedFlexItemList&, LayoutUnit& availableFreeSpace);
130    void updateAutoMarginsInMainAxis(RenderBox& child, LayoutUnit autoMarginOffset);
131    bool hasAutoMarginsInCrossAxis(RenderBox& child) const;
132    bool updateAutoMarginsInCrossAxis(RenderBox& child, LayoutUnit availableAlignmentSpace);
133    void repositionLogicalHeightDependentFlexItems(Vector<LineContext>&);
134    LayoutUnit clientLogicalBottomAfterRepositioning();
135    void appendChildFrameRects(ChildFrameRects&);
136
137    LayoutUnit availableAlignmentSpaceForChild(LayoutUnit lineCrossAxisExtent, RenderBox& child);
138    LayoutUnit availableAlignmentSpaceForChildBeforeStretching(LayoutUnit lineCrossAxisExtent, RenderBox& child);
139    LayoutUnit marginBoxAscentForChild(RenderBox& child);
140
141    LayoutUnit computeChildMarginValue(Length margin);
142    void prepareOrderIteratorAndMargins();
143    LayoutUnit adjustChildSizeForMinAndMax(RenderBox& child, LayoutUnit childSize);
144    // The hypothetical main size of an item is the flex base size clamped according to its min and max main size properties
145    bool computeNextFlexLine(OrderedFlexItemList& orderedChildren, LayoutUnit& sumFlexBaseSize, double& totalFlexGrow, double& totalWeightedFlexShrink, LayoutUnit& sumHypotheticalMainSize, bool& hasInfiniteLineLength, bool relayoutChildren);
146
147    bool resolveFlexibleLengths(FlexSign, const OrderedFlexItemList&, LayoutUnit& availableFreeSpace, double& totalFlexGrow, double& totalWeightedFlexShrink, InflexibleFlexItemSize&, Vector<LayoutUnit, 16>& childSizes, bool hasInfiniteLineLength);
148    void freezeViolations(const Vector<Violation>&, LayoutUnit& availableFreeSpace, double& totalFlexGrow, double& totalWeightedFlexShrink, InflexibleFlexItemSize&, bool hasInfiniteLineLength);
149
150    void resetAutoMarginsAndLogicalTopInCrossAxis(RenderBox& child);
151    void setLogicalOverrideSize(RenderBox& child, LayoutUnit childPreferredSize);
152    void prepareChildForPositionedLayout(RenderBox& child, LayoutUnit mainAxisOffset, LayoutUnit crossAxisOffset, PositionedLayoutMode);
153    size_t numberOfInFlowPositionedChildren(const OrderedFlexItemList&) const;
154    void layoutAndPlaceChildren(LayoutUnit& crossAxisOffset, const OrderedFlexItemList&, const Vector<LayoutUnit, 16>& childSizes, LayoutUnit availableFreeSpace, bool relayoutChildren, Vector<LineContext>&, bool hasInfiniteLineLength);
155    void layoutColumnReverse(const OrderedFlexItemList&, LayoutUnit crossAxisOffset, LayoutUnit availableFreeSpace);
156    void alignFlexLines(Vector<LineContext>&);
157    void alignChildren(const Vector<LineContext>&);
158    void applyStretchAlignmentToChild(RenderBox& child, LayoutUnit lineCrossAxisExtent);
159    void flipForRightToLeftColumn();
160    void flipForWrapReverse(const Vector<LineContext>&, LayoutUnit crossAxisStartEdge);
161
162    // This is used to cache the preferred size for orthogonal flow children so we don't have to relayout to get it
163    HashMap<const RenderObject*, LayoutUnit> m_intrinsicSizeAlongMainAxis;
164
165    mutable OrderIterator m_orderIterator;
166    int m_numberOfInFlowChildrenOnFirstLine;
167};
168
169DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderFlexibleBox, isFlexibleBox());
170
171} // namespace blink
172
173#endif // RenderFlexibleBox_h
174