2032abd3afb78f5aeda99c872b7e4c2bd82a78d0
[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 "GridPositionsResolver.h"
33 #include "OrderIterator.h"
34 #include "RenderBlock.h"
35
36 namespace WebCore {
37
38 class GridArea;
39 class GridSpan;
40 class GridTrack;
41 class GridItemWithSpan;
42
43 struct ContentAlignmentData;
44
45 enum GridAxisPosition {GridAxisStart, GridAxisEnd, GridAxisCenter};
46
47 class RenderGrid final : public RenderBlock {
48 public:
49     RenderGrid(Element&, Ref<RenderStyle>&&);
50     virtual ~RenderGrid();
51
52     Element& element() const { return downcast<Element>(nodeForNonAnonymous()); }
53
54     void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
55     void layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight = 0) override;
56
57     bool avoidsFloats() const override { return true; }
58     bool canDropAnonymousBlockChild() const override { return false; }
59
60     const Vector<LayoutUnit>& columnPositions() const { return m_columnPositions; }
61     const Vector<LayoutUnit>& rowPositions() const { return m_rowPositions; }
62
63     LayoutUnit guttersSize(GridTrackSizingDirection, size_t span) const;
64
65 private:
66     const char* renderName() const override;
67     bool isRenderGrid() const override { return true; }
68     void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const override;
69
70     Optional<LayoutUnit> computeIntrinsicLogicalContentHeightUsing(Length logicalHeightLength, Optional<LayoutUnit> intrinsicContentHeight, LayoutUnit borderAndPadding) const override;
71
72     class GridIterator;
73     class GridSizingData;
74     void computeUsedBreadthOfGridTracks(GridTrackSizingDirection, GridSizingData&, LayoutUnit& baseSizesWithoutMaximization, LayoutUnit& growthLimitsWithoutMaximization);
75     LayoutUnit computeUsedBreadthOfMinLength(const GridLength&, LayoutUnit maxSize) const;
76     LayoutUnit computeUsedBreadthOfMaxLength(const GridLength&, LayoutUnit usedBreadth, LayoutUnit maxSize) const;
77     void resolveContentBasedTrackSizingFunctions(GridTrackSizingDirection, GridSizingData&);
78
79     void ensureGridSize(unsigned maximumRowSize, unsigned maximumColumnSize);
80     void insertItemIntoGrid(RenderBox&, const GridArea&);
81     void placeItemsOnGrid();
82     void populateExplicitGridAndOrderIterator();
83     std::unique_ptr<GridArea> createEmptyGridAreaAtSpecifiedPositionsOutsideGrid(const RenderBox&, GridTrackSizingDirection, const GridSpan&) const;
84     void placeSpecifiedMajorAxisItemsOnGrid(const Vector<RenderBox*>&);
85     void placeAutoMajorAxisItemsOnGrid(const Vector<RenderBox*>&);
86     typedef std::pair<unsigned, unsigned> AutoPlacementCursor;
87     void placeAutoMajorAxisItemOnGrid(RenderBox&, AutoPlacementCursor&);
88     GridTrackSizingDirection autoPlacementMajorAxisDirection() const;
89     GridTrackSizingDirection autoPlacementMinorAxisDirection() const;
90
91     void prepareChildForPositionedLayout(RenderBox&);
92     void layoutPositionedObject(RenderBox&, bool relayoutChildren, bool fixedPositionObjectsOnly) override;
93     void offsetAndBreadthForPositionedChild(const RenderBox&, GridTrackSizingDirection, LayoutUnit& offset, LayoutUnit& breadth);
94
95     void computeIntrinsicLogicalHeight(GridSizingData&);
96     LayoutUnit computeTrackBasedLogicalHeight(const GridSizingData&) const;
97     void computeTrackSizesForDirection(GridTrackSizingDirection, GridSizingData&, LayoutUnit freeSpace);
98
99     void layoutGridItems(GridSizingData&);
100     void populateGridPositions(GridSizingData&);
101     void clearGrid();
102
103     enum TrackSizeRestriction {
104         AllowInfinity,
105         ForbidInfinity,
106     };
107     enum TrackSizeComputationPhase {
108         ResolveIntrinsicMinimums,
109         ResolveContentBasedMinimums,
110         ResolveMaxContentMinimums,
111         ResolveIntrinsicMaximums,
112         ResolveMaxContentMaximums,
113         MaximizeTracks,
114     };
115     static const LayoutUnit& trackSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, GridTrack&, TrackSizeRestriction);
116     static bool shouldProcessTrackForTrackSizeComputationPhase(TrackSizeComputationPhase, const GridTrackSize&);
117     static bool trackShouldGrowBeyondGrowthLimitsForTrackSizeComputationPhase(TrackSizeComputationPhase, const GridTrackSize&);
118     static void markAsInfinitelyGrowableForTrackSizeComputationPhase(TrackSizeComputationPhase, GridTrack&);
119     static void updateTrackSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, GridTrack&);
120     LayoutUnit currentItemSizeForTrackSizeComputationPhase(TrackSizeComputationPhase, RenderBox&, GridTrackSizingDirection, GridSizingData&);
121
122     typedef struct GridItemsSpanGroupRange GridItemsSpanGroupRange;
123     void resolveContentBasedTrackSizingFunctionsForNonSpanningItems(GridTrackSizingDirection, const GridSpan&, RenderBox& gridItem, GridTrack&, GridSizingData&);
124     template <TrackSizeComputationPhase> void resolveContentBasedTrackSizingFunctionsForItems(GridTrackSizingDirection, GridSizingData&, const GridItemsSpanGroupRange&);
125     template <TrackSizeComputationPhase> void distributeSpaceToTracks(Vector<GridTrack*>&, const Vector<GridTrack*>* growBeyondGrowthLimitsTracks, LayoutUnit& availableLogicalSpace);
126
127     typedef HashSet<unsigned, DefaultHash<unsigned>::Hash, WTF::UnsignedWithZeroKeyHashTraits<unsigned>> TrackIndexSet;
128     double computeFlexFactorUnitSize(const Vector<GridTrack>&, GridTrackSizingDirection, double flexFactorSum, LayoutUnit leftOverSpace, const Vector<unsigned, 8>& flexibleTracksIndexes, std::unique_ptr<TrackIndexSet> tracksToTreatAsInflexible = nullptr) const;
129     double findFlexFactorUnitSize(const Vector<GridTrack>&, const GridSpan&, GridTrackSizingDirection, LayoutUnit spaceToFill) const;
130
131     GridTrackSize gridTrackSize(GridTrackSizingDirection, unsigned) const;
132
133     LayoutUnit logicalContentHeightForChild(RenderBox&, GridSizingData&);
134     LayoutUnit minSizeForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&);
135     LayoutUnit minContentForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&);
136     LayoutUnit maxContentForChild(RenderBox&, GridTrackSizingDirection, GridSizingData&);
137     GridAxisPosition columnAxisPositionForChild(const RenderBox&) const;
138     GridAxisPosition rowAxisPositionForChild(const RenderBox&) const;
139     LayoutUnit columnAxisOffsetForChild(const RenderBox&) const;
140     LayoutUnit rowAxisOffsetForChild(const RenderBox&) const;
141     ContentAlignmentData computeContentPositionAndDistributionOffset(GridTrackSizingDirection, const LayoutUnit& availableFreeSpace, unsigned numberOfGridTracks) const;
142     LayoutPoint findChildLogicalPosition(const RenderBox&) const;
143     GridArea cachedGridArea(const RenderBox&) const;
144     GridSpan cachedGridSpan(const RenderBox&, GridTrackSizingDirection) const;
145
146
147     LayoutUnit gridAreaBreadthForChild(const RenderBox& child, GridTrackSizingDirection, const Vector<GridTrack>&) const;
148     LayoutUnit gridAreaBreadthForChildIncludingAlignmentOffsets(const RenderBox&, GridTrackSizingDirection, const GridSizingData&) const;
149
150     void applyStretchAlignmentToTracksIfNeeded(GridTrackSizingDirection, GridSizingData&);
151
152     void paintChildren(PaintInfo& forSelf, const LayoutPoint& paintOffset, PaintInfo& forChild, bool usePrintRect) override;
153     bool needToStretchChildLogicalHeight(const RenderBox&) const;
154     LayoutUnit marginLogicalHeightForChild(const RenderBox&) const;
155     LayoutUnit computeMarginLogicalHeightForChild(const RenderBox&) const;
156     LayoutUnit availableAlignmentSpaceForChildBeforeStretching(LayoutUnit gridAreaBreadthForChild, const RenderBox&) const;
157     void applyStretchAlignmentToChildIfNeeded(RenderBox&);
158     bool hasAutoMarginsInColumnAxis(const RenderBox&) const;
159     bool hasAutoMarginsInRowAxis(const RenderBox&) const;
160     void resetAutoMarginsAndLogicalTopInColumnAxis(RenderBox& child);
161     void updateAutoMarginsInColumnAxisIfNeeded(RenderBox&);
162     void updateAutoMarginsInRowAxisIfNeeded(RenderBox&);
163
164 #ifndef NDEBUG
165     bool tracksAreWiderThanMinTrackBreadth(GridTrackSizingDirection, GridSizingData&);
166 #endif
167
168     bool gridWasPopulated() const { return !m_grid.isEmpty() && !m_grid[0].isEmpty(); }
169
170     bool spanningItemCrossesFlexibleSizedTracks(const GridSpan&, GridTrackSizingDirection) const;
171
172     unsigned gridColumnCount() const
173     {
174         ASSERT(gridWasPopulated());
175         return m_grid[0].size();
176     }
177     unsigned gridRowCount() const
178     {
179         ASSERT(gridWasPopulated());
180         return m_grid.size();
181     }
182
183     bool hasDefiniteLogicalSize(GridTrackSizingDirection) const;
184     LayoutUnit translateRTLCoordinate(LayoutUnit) const;
185
186     Vector<Vector<Vector<RenderBox*, 1>>> m_grid;
187     Vector<LayoutUnit> m_columnPositions;
188     Vector<LayoutUnit> m_rowPositions;
189     LayoutUnit m_offsetBetweenColumns;
190     LayoutUnit m_offsetBetweenRows;
191     HashMap<const RenderBox*, GridArea> m_gridItemArea;
192     OrderIterator m_orderIterator;
193
194     Optional<LayoutUnit> m_minContentHeight;
195     Optional<LayoutUnit> m_maxContentHeight;
196
197     int m_smallestColumnStart;
198     int m_smallestRowStart;
199 };
200
201 } // namespace WebCore
202
203 SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderGrid, isRenderGrid())
204
205 #endif /* ENABLE(CSS_GRID_LAYOUT) */
206
207 #endif // RenderGrid_h