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