1 /* 2 * Copyright (C) 2011 Apple 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 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef RenderGrid_h 27 #define RenderGrid_h 28 29 #include "core/rendering/OrderIterator.h" 30 #include "core/rendering/RenderBlock.h" 31 32 namespace WebCore { 33 34 struct GridCoordinate; 35 struct GridSpan; 36 class GridTrack; 37 38 enum GridTrackSizingDirection { 39 ForColumns, 40 ForRows 41 }; 42 43 class RenderGrid FINAL : public RenderBlock { 44 public: 45 RenderGrid(Element*); 46 virtual ~RenderGrid(); 47 48 virtual const char* renderName() const OVERRIDE; 49 50 virtual void layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight = 0) OVERRIDE; 51 52 virtual bool avoidsFloats() const OVERRIDE { return true; } 53 virtual bool canCollapseAnonymousBlockChild() const OVERRIDE { return false; } 54 55 void dirtyGrid(); 56 57 const Vector<LayoutUnit>& columnPositions() const { return m_columnPositions; } 58 const Vector<LayoutUnit>& rowPositions() const { return m_rowPositions; } 59 60 private: 61 virtual bool isRenderGrid() const OVERRIDE { return true; } 62 virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const OVERRIDE; 63 virtual void computePreferredLogicalWidths() OVERRIDE; 64 65 virtual void addChild(RenderObject* newChild, RenderObject* beforeChild = 0) OVERRIDE; 66 virtual void removeChild(RenderObject*) OVERRIDE; 67 68 virtual void styleDidChange(StyleDifference, const RenderStyle*) OVERRIDE; 69 70 bool explicitGridDidResize(const RenderStyle*) const; 71 bool namedGridLinesDefinitionDidChange(const RenderStyle*) const; 72 73 class GridIterator; 74 struct GridSizingData; 75 void computedUsedBreadthOfGridTracks(GridTrackSizingDirection, GridSizingData&); 76 void computedUsedBreadthOfGridTracks(GridTrackSizingDirection, GridSizingData&, LayoutUnit& availableLogicalSpace); 77 LayoutUnit computeUsedBreadthOfMinLength(GridTrackSizingDirection, const GridLength&) const; 78 LayoutUnit computeUsedBreadthOfMaxLength(GridTrackSizingDirection, const GridLength&, LayoutUnit usedBreadth) const; 79 LayoutUnit computeUsedBreadthOfSpecifiedLength(GridTrackSizingDirection, const Length&) const; 80 void resolveContentBasedTrackSizingFunctions(GridTrackSizingDirection, GridSizingData&, LayoutUnit& availableLogicalSpace); 81 82 void growGrid(GridTrackSizingDirection); 83 void insertItemIntoGrid(RenderBox*, size_t rowTrack, size_t columnTrack); 84 void insertItemIntoGrid(RenderBox*, const GridCoordinate&); 85 void placeItemsOnGrid(); 86 void populateExplicitGridAndOrderIterator(); 87 void placeSpecifiedMajorAxisItemsOnGrid(const Vector<RenderBox*>&); 88 void placeAutoMajorAxisItemsOnGrid(const Vector<RenderBox*>&); 89 void placeAutoMajorAxisItemOnGrid(RenderBox*); 90 GridTrackSizingDirection autoPlacementMajorAxisDirection() const; 91 GridTrackSizingDirection autoPlacementMinorAxisDirection() const; 92 93 void layoutGridItems(); 94 void populateGridPositions(const GridSizingData&); 95 96 virtual bool supportsPartialLayout() const OVERRIDE { return false; } 97 98 typedef LayoutUnit (RenderGrid::* SizingFunction)(RenderBox*, GridTrackSizingDirection, Vector<GridTrack>&); 99 typedef LayoutUnit (GridTrack::* AccumulatorGetter)() const; 100 typedef void (GridTrack::* AccumulatorGrowFunction)(LayoutUnit); 101 typedef bool (GridTrackSize::* FilterFunction)() const; 102 void resolveContentBasedTrackSizingFunctionsForItems(GridTrackSizingDirection, GridSizingData&, RenderBox*, FilterFunction, SizingFunction, AccumulatorGetter, AccumulatorGrowFunction); 103 void distributeSpaceToTracks(Vector<GridTrack*>&, Vector<GridTrack*>* tracksForGrowthAboveMaxBreadth, AccumulatorGetter, AccumulatorGrowFunction, GridSizingData&, LayoutUnit& availableLogicalSpace); 104 105 double computeNormalizedFractionBreadth(Vector<GridTrack>&, GridTrackSizingDirection, LayoutUnit availableLogicalSpace) const; 106 107 const GridTrackSize& gridTrackSize(GridTrackSizingDirection, size_t) const; 108 size_t explicitGridColumnCount() const; 109 size_t explicitGridRowCount() const; 110 size_t explicitGridSizeForSide(GridPositionSide) const; 111 112 LayoutUnit logicalContentHeightForChild(RenderBox*, Vector<GridTrack>&); 113 LayoutUnit minContentForChild(RenderBox*, GridTrackSizingDirection, Vector<GridTrack>& columnTracks); 114 LayoutUnit maxContentForChild(RenderBox*, GridTrackSizingDirection, Vector<GridTrack>& columnTracks); 115 LayoutPoint findChildLogicalPosition(RenderBox*, const GridSizingData&); 116 GridCoordinate cachedGridCoordinate(const RenderBox*) const; 117 118 GridSpan resolveGridPositionsFromAutoPlacementPosition(const RenderBox*, GridTrackSizingDirection, size_t) const; 119 PassOwnPtr<GridSpan> resolveGridPositionsFromStyle(const RenderBox*, GridTrackSizingDirection) const; 120 size_t resolveNamedGridLinePositionFromStyle(const GridPosition&, GridPositionSide) const; 121 size_t resolveGridPositionFromStyle(const GridPosition&, GridPositionSide) const; 122 PassOwnPtr<GridSpan> resolveGridPositionAgainstOppositePosition(size_t resolvedOppositePosition, const GridPosition&, GridPositionSide) const; 123 PassOwnPtr<GridSpan> resolveNamedGridLinePositionAgainstOppositePosition(size_t resolvedOppositePosition, const GridPosition&, GridPositionSide) const; 124 PassOwnPtr<GridSpan> resolveBeforeStartNamedGridLinePositionAgainstOppositePosition(size_t resolvedOppositePosition, const GridPosition&, const Vector<size_t>&) const; 125 PassOwnPtr<GridSpan> resolveAfterEndNamedGridLinePositionAgainstOppositePosition(size_t resolvedOppositePosition, const GridPosition&, const Vector<size_t>&) const; 126 127 LayoutUnit gridAreaBreadthForChild(const RenderBox* child, GridTrackSizingDirection, const Vector<GridTrack>&) const; 128 129 virtual void paintChildren(PaintInfo&, const LayoutPoint&) OVERRIDE FINAL; 130 void paintChildrenSlowCase(PaintInfo&, const LayoutPoint&); 131 132 bool gridIsDirty() const { return m_gridIsDirty; } 133 134 #ifndef NDEBUG 135 bool tracksAreWiderThanMinTrackBreadth(GridTrackSizingDirection, const Vector<GridTrack>&); 136 #endif 137 138 size_t gridColumnCount() const 139 { 140 ASSERT(!gridIsDirty()); 141 return m_grid[0].size(); 142 } 143 size_t gridRowCount() const 144 { 145 ASSERT(!gridIsDirty()); 146 return m_grid.size(); 147 } 148 149 typedef Vector<RenderBox*, 1> GridCell; 150 typedef Vector<Vector<GridCell> > GridRepresentation; 151 GridRepresentation m_grid; 152 bool m_gridIsDirty; 153 Vector<LayoutUnit> m_rowPositions; 154 Vector<LayoutUnit> m_columnPositions; 155 HashMap<const RenderBox*, GridCoordinate> m_gridItemCoordinate; 156 OrderIterator m_orderIterator; 157 bool m_gridItemOverflowGridArea; 158 }; 159 160 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderGrid, isRenderGrid()); 161 162 } // namespace WebCore 163 164 #endif // RenderGrid_h 165