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 37 namespace WebCore { 38 39 class RenderFlexibleBox : public RenderBlock { 40 public: 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 avoidsFloats() const OVERRIDE FINAL { return true; } 50 virtual bool canCollapseAnonymousBlockChild() const OVERRIDE FINAL { return false; } 51 virtual void layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight = 0) OVERRIDE FINAL; 52 53 virtual int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const OVERRIDE; 54 virtual int firstLineBoxBaseline() const OVERRIDE; 55 virtual int inlineBlockBaseline(LineDirectionMode) const OVERRIDE; 56 57 virtual void paintChildren(PaintInfo&, const LayoutPoint&) OVERRIDE FINAL; 58 59 bool isHorizontalFlow() const; 60 61 protected: 62 virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const OVERRIDE; 63 64 virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle); 65 66 private: 67 enum FlexSign { 68 PositiveFlexibility, 69 NegativeFlexibility, 70 }; 71 72 enum PositionedLayoutMode { 73 FlipForRowReverse, 74 NoFlipForRowReverse, 75 }; 76 77 typedef HashMap<const RenderBox*, LayoutUnit> InflexibleFlexItemSize; 78 typedef Vector<RenderBox*> OrderedFlexItemList; 79 80 struct LineContext; 81 struct Violation; 82 83 // Use an inline capacity of 8, since flexbox containers usually have less than 8 children. 84 typedef Vector<LayoutRect, 8> ChildFrameRects; 85 86 bool hasOrthogonalFlow(RenderBox* child) const; 87 bool isColumnFlow() const; 88 bool isLeftToRightFlow() const; 89 bool isMultiline() const; 90 Length flexBasisForChild(RenderBox* child) const; 91 void setCrossAxisExtent(LayoutUnit); 92 LayoutUnit crossAxisExtentForChild(RenderBox* child) const; 93 LayoutUnit mainAxisExtentForChild(RenderBox* child) const; 94 LayoutUnit crossAxisExtent() const; 95 LayoutUnit mainAxisExtent() const; 96 LayoutUnit crossAxisContentExtent() const; 97 LayoutUnit mainAxisContentExtent(LayoutUnit contentLogicalHeight); 98 LayoutUnit computeMainAxisExtentForChild(RenderBox* child, SizeType, const Length& size); 99 WritingMode transformedWritingMode() const; 100 LayoutUnit flowAwareBorderStart() const; 101 LayoutUnit flowAwareBorderEnd() const; 102 LayoutUnit flowAwareBorderBefore() const; 103 LayoutUnit flowAwareBorderAfter() const; 104 LayoutUnit flowAwarePaddingStart() const; 105 LayoutUnit flowAwarePaddingEnd() const; 106 LayoutUnit flowAwarePaddingBefore() const; 107 LayoutUnit flowAwarePaddingAfter() const; 108 LayoutUnit flowAwareMarginStartForChild(RenderBox* child) const; 109 LayoutUnit flowAwareMarginEndForChild(RenderBox* child) const; 110 LayoutUnit flowAwareMarginBeforeForChild(RenderBox* child) const; 111 LayoutUnit flowAwareMarginAfterForChild(RenderBox* child) const; 112 LayoutUnit crossAxisMarginExtentForChild(RenderBox* child) const; 113 LayoutUnit crossAxisScrollbarExtent() const; 114 LayoutPoint flowAwareLocationForChild(RenderBox* child) const; 115 // FIXME: Supporting layout deltas. 116 void setFlowAwareLocationForChild(RenderBox* child, const LayoutPoint&); 117 void adjustAlignmentForChild(RenderBox* child, LayoutUnit); 118 EAlignItems alignmentForChild(RenderBox* child) const; 119 LayoutUnit mainAxisBorderAndPaddingExtentForChild(RenderBox* child) const; 120 LayoutUnit mainAxisScrollbarExtentForChild(RenderBox* child) const; 121 LayoutUnit preferredMainAxisContentExtentForChild(RenderBox* child, bool hasInfiniteLineLength); 122 123 void layoutFlexItems(bool relayoutChildren, Vector<LineContext>&); 124 LayoutUnit autoMarginOffsetInMainAxis(const OrderedFlexItemList&, LayoutUnit& availableFreeSpace); 125 void updateAutoMarginsInMainAxis(RenderBox* child, LayoutUnit autoMarginOffset); 126 bool hasAutoMarginsInCrossAxis(RenderBox* child) const; 127 bool updateAutoMarginsInCrossAxis(RenderBox* child, LayoutUnit availableAlignmentSpace); 128 void repositionLogicalHeightDependentFlexItems(Vector<LineContext>&); 129 LayoutUnit clientLogicalBottomAfterRepositioning(); 130 void appendChildFrameRects(ChildFrameRects&); 131 void repaintChildrenDuringLayoutIfMoved(const ChildFrameRects&); 132 133 LayoutUnit availableAlignmentSpaceForChild(LayoutUnit lineCrossAxisExtent, RenderBox*); 134 LayoutUnit marginBoxAscentForChild(RenderBox*); 135 136 LayoutUnit computeChildMarginValue(Length margin, RenderView*); 137 void prepareOrderIteratorAndMargins(); 138 LayoutUnit adjustChildSizeForMinAndMax(RenderBox*, LayoutUnit childSize); 139 // The hypothetical main size of an item is the flex base size clamped according to its min and max main size properties 140 bool computeNextFlexLine(OrderedFlexItemList& orderedChildren, LayoutUnit& sumFlexBaseSize, double& totalFlexGrow, double& totalWeightedFlexShrink, LayoutUnit& sumHypotheticalMainSize, bool& hasInfiniteLineLength); 141 142 bool resolveFlexibleLengths(FlexSign, const OrderedFlexItemList&, LayoutUnit& availableFreeSpace, double& totalFlexGrow, double& totalWeightedFlexShrink, InflexibleFlexItemSize&, Vector<LayoutUnit>& childSizes, bool hasInfiniteLineLength); 143 void freezeViolations(const Vector<Violation>&, LayoutUnit& availableFreeSpace, double& totalFlexGrow, double& totalWeightedFlexShrink, InflexibleFlexItemSize&, bool hasInfiniteLineLength); 144 145 void resetAutoMarginsAndLogicalTopInCrossAxis(RenderBox*); 146 bool needToStretchChild(RenderBox*); 147 void setLogicalOverrideSize(RenderBox* child, LayoutUnit childPreferredSize); 148 void prepareChildForPositionedLayout(RenderBox* child, LayoutUnit mainAxisOffset, LayoutUnit crossAxisOffset, PositionedLayoutMode); 149 size_t numberOfInFlowPositionedChildren(const OrderedFlexItemList&) const; 150 void layoutAndPlaceChildren(LayoutUnit& crossAxisOffset, const OrderedFlexItemList&, const Vector<LayoutUnit>& childSizes, LayoutUnit availableFreeSpace, bool relayoutChildren, Vector<LineContext>&); 151 void layoutColumnReverse(const OrderedFlexItemList&, LayoutUnit crossAxisOffset, LayoutUnit availableFreeSpace); 152 void alignFlexLines(Vector<LineContext>&); 153 void alignChildren(const Vector<LineContext>&); 154 void applyStretchAlignmentToChild(RenderBox*, LayoutUnit lineCrossAxisExtent); 155 void flipForRightToLeftColumn(); 156 void flipForWrapReverse(const Vector<LineContext>&, LayoutUnit crossAxisStartEdge); 157 158 mutable OrderIterator m_orderIterator; 159 int m_numberOfInFlowChildrenOnFirstLine; 160 }; 161 162 inline RenderFlexibleBox* toRenderFlexibleBox(RenderObject* object) 163 { 164 ASSERT_WITH_SECURITY_IMPLICATION(!object || object->isFlexibleBox()); 165 return static_cast<RenderFlexibleBox*>(object); 166 } 167 168 inline const RenderFlexibleBox* toRenderFlexibleBox(const RenderObject* object) 169 { 170 ASSERT_WITH_SECURITY_IMPLICATION(!object || object->isFlexibleBox()); 171 return static_cast<const RenderFlexibleBox*>(object); 172 } 173 174 // This will catch anyone doing an unnecessary cast. 175 void toRenderFlexibleBox(const RenderFlexibleBox*); 176 177 } // namespace WebCore 178 179 #endif // RenderFlexibleBox_h 180