8184ecfb74262a54b18ed7a6183861650402be47
[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 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     const Vector<LayoutUnit>& columnPositions() const { return m_columnPositions; }
59     const Vector<LayoutUnit>& rowPositions() const { return m_rowPositions; }
60
61 private:
62     virtual const char* renderName() const override;
63     virtual bool isRenderGrid() const override { return true; }
64     virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const override;
65     virtual void computePreferredLogicalWidths() override;
66
67     class GridIterator;
68     class GridSizingData;
69     enum GridTrackSizingDirection { ForColumns, ForRows };
70     void computeUsedBreadthOfGridTracks(GridTrackSizingDirection, GridSizingData&);
71     void computeUsedBreadthOfGridTracks(GridTrackSizingDirection, GridSizingData&, LayoutUnit& availableLogicalSpace);
72     bool gridElementIsShrinkToFit();
73     LayoutUnit computeUsedBreadthOfMinLength(GridTrackSizingDirection, const GridLength&) const;
74     LayoutUnit computeUsedBreadthOfMaxLength(GridTrackSizingDirection, const GridLength&, LayoutUnit usedBreadth) const;
75     LayoutUnit computeUsedBreadthOfSpecifiedLength(GridTrackSizingDirection, const Length&) const;
76     void resolveContentBasedTrackSizingFunctions(GridTrackSizingDirection, GridSizingData&);
77
78     void growGrid(GridTrackSizingDirection);
79     void insertItemIntoGrid(RenderBox*, size_t rowTrack, size_t columnTrack);
80     void insertItemIntoGrid(RenderBox*, const GridCoordinate&);
81     void placeItemsOnGrid();
82     void populateExplicitGridAndOrderIterator();
83     void placeSpecifiedMajorAxisItemsOnGrid(const Vector<RenderBox*>&);
84     void placeAutoMajorAxisItemsOnGrid(const Vector<RenderBox*>&);
85     void placeAutoMajorAxisItemOnGrid(RenderBox*);
86     GridTrackSizingDirection autoPlacementMajorAxisDirection() const;
87     GridTrackSizingDirection autoPlacementMinorAxisDirection() const;
88
89     void layoutGridItems();
90     void populateGridPositions(const GridSizingData&);
91     void clearGrid();
92
93     typedef LayoutUnit (RenderGrid::* SizingFunction)(RenderBox*, GridTrackSizingDirection, Vector<GridTrack>&);
94     typedef LayoutUnit (GridTrack::* AccumulatorGetter)() const;
95     typedef void (GridTrack::* AccumulatorGrowFunction)(LayoutUnit);
96     typedef bool (GridTrackSize::* FilterFunction)() const;
97     void resolveContentBasedTrackSizingFunctionsForItems(GridTrackSizingDirection, GridSizingData&, RenderBox*, FilterFunction, SizingFunction, AccumulatorGetter, AccumulatorGrowFunction);
98     void distributeSpaceToTracks(Vector<GridTrack*>&, Vector<GridTrack*>* tracksForGrowthAboveMaxBreadth, AccumulatorGetter, AccumulatorGrowFunction, GridSizingData&, LayoutUnit& availableLogicalSpace);
99
100     double computeNormalizedFractionBreadth(Vector<GridTrack>&, const GridSpan& tracksSpan, GridTrackSizingDirection, LayoutUnit availableLogicalSpace) const;
101
102     const GridTrackSize& gridTrackSize(GridTrackSizingDirection, size_t) const;
103     size_t explicitGridColumnCount() const;
104     size_t explicitGridRowCount() const;
105     size_t explicitGridSizeForSide(GridPositionSide) const;
106
107     LayoutUnit logicalContentHeightForChild(RenderBox*, Vector<GridTrack>&);
108     LayoutUnit minContentForChild(RenderBox*, GridTrackSizingDirection, Vector<GridTrack>& columnTracks);
109     LayoutUnit maxContentForChild(RenderBox*, GridTrackSizingDirection, Vector<GridTrack>& columnTracks);
110     LayoutPoint findChildLogicalPosition(RenderBox*, const GridSizingData&);
111     GridCoordinate cachedGridCoordinate(const RenderBox*) const;
112
113     GridSpan resolveGridPositionsFromAutoPlacementPosition(const RenderBox*, GridTrackSizingDirection, size_t) const;
114     void adjustNamedGridItemPosition(GridPosition&, GridPositionSide) const;
115     void adjustGridPositionsFromStyle(GridPosition& initialPosition, GridPosition& finalPosition, GridPositionSide initialPositionSide, GridPositionSide finalPositionSide) const;
116     std::unique_ptr<GridSpan> resolveGridPositionsFromStyle(const RenderBox*, GridTrackSizingDirection) const;
117     size_t resolveNamedGridLinePositionFromStyle(const GridPosition&, GridPositionSide) const;
118     size_t resolveGridPositionFromStyle(const GridPosition&, GridPositionSide) const;
119     std::unique_ptr<GridSpan> resolveGridPositionAgainstOppositePosition(size_t resolvedOppositePosition, const GridPosition&, GridPositionSide) const;
120     std::unique_ptr<GridSpan> resolveNamedGridLinePositionAgainstOppositePosition(size_t resolvedOppositePosition, const GridPosition&, GridPositionSide) const;
121     std::unique_ptr<GridSpan> resolveRowStartColumnStartNamedGridLinePositionAgainstOppositePosition(size_t resolvedOppositePosition, const GridPosition&, const Vector<size_t>&) const;
122     std::unique_ptr<GridSpan> resolveRowEndColumnEndNamedGridLinePositionAgainstOppositePosition(size_t resolvedOppositePosition, const GridPosition&, const Vector<size_t>&) const;
123
124     LayoutUnit gridAreaBreadthForChild(const RenderBox* child, GridTrackSizingDirection, const Vector<GridTrack>&) const;
125
126     virtual void paintChildren(PaintInfo& forSelf, const LayoutPoint& paintOffset, PaintInfo& forChild, bool usePrintRect) override final;
127
128 #ifndef NDEBUG
129     bool tracksAreWiderThanMinTrackBreadth(GridTrackSizingDirection, const Vector<GridTrack>&);
130     bool gridWasPopulated() const { return !m_grid.isEmpty() && !m_grid[0].isEmpty(); }
131 #endif
132
133     size_t gridColumnCount() const
134     {
135         ASSERT(gridWasPopulated());
136         return m_grid[0].size();
137     }
138     size_t gridRowCount() const
139     {
140         ASSERT(gridWasPopulated());
141         return m_grid.size();
142     }
143
144     Vector<Vector<Vector<RenderBox*, 1>>> m_grid;
145     Vector<LayoutUnit> m_columnPositions;
146     Vector<LayoutUnit> m_rowPositions;
147     HashMap<const RenderBox*, GridCoordinate> m_gridItemCoordinate;
148     OrderIterator m_orderIterator;
149 };
150
151 RENDER_OBJECT_TYPE_CASTS(RenderGrid, isRenderGrid())
152
153 } // namespace WebCore
154
155 #endif /* ENABLE(CSS_GRID_LAYOUT) */
156
157 #endif // RenderGrid_h