2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "core/layout/LayoutBlock.h"
30 #include "core/layout/OrderIterator.h"
31 #include "core/style/GridResolvedPosition.h"
35 struct ContentAlignmentData
;
36 struct GridCoordinate
;
40 enum TrackSizeComputationPhase
{
41 ResolveIntrinsicMinimums
,
42 ResolveContentBasedMinimums
,
43 ResolveMaxContentMinimums
,
44 ResolveIntrinsicMaximums
,
45 ResolveMaxContentMaximums
,
48 enum GridAxisPosition
{GridAxisStart
, GridAxisEnd
, GridAxisCenter
};
50 class LayoutGrid final
: public LayoutBlock
{
52 explicit LayoutGrid(Element
*);
53 ~LayoutGrid() override
;
55 const char* name() const override
{ return "LayoutGrid"; }
57 void layoutBlock(bool relayoutChildren
) override
;
59 bool canCollapseAnonymousBlockChild() const override
{ return false; }
63 const Vector
<LayoutUnit
>& columnPositions() const
65 ASSERT(!m_gridIsDirty
);
66 return m_columnPositions
;
69 const Vector
<LayoutUnit
>& rowPositions() const
71 ASSERT(!m_gridIsDirty
);
72 return m_rowPositions
;
75 typedef Vector
<LayoutBox
*, 1> GridCell
;
76 const GridCell
& gridCell(int row
, int column
)
78 ASSERT_WITH_SECURITY_IMPLICATION(!m_gridIsDirty
);
79 return m_grid
[row
][column
];
82 const Vector
<LayoutBox
*>& itemsOverflowingGridArea()
84 ASSERT_WITH_SECURITY_IMPLICATION(!m_gridIsDirty
);
85 return m_gridItemsOverflowingGridArea
;
88 int paintIndexForGridItem(const LayoutBox
* layoutBox
)
90 ASSERT_WITH_SECURITY_IMPLICATION(!m_gridIsDirty
);
91 return m_gridItemsIndexesMap
.get(layoutBox
);
95 bool isOfType(LayoutObjectType type
) const override
{ return type
== LayoutObjectLayoutGrid
|| LayoutBlock::isOfType(type
); }
96 void computeIntrinsicLogicalWidths(LayoutUnit
& minLogicalWidth
, LayoutUnit
& maxLogicalWidth
) const override
;
98 void addChild(LayoutObject
* newChild
, LayoutObject
* beforeChild
= nullptr) override
;
99 void removeChild(LayoutObject
*) override
;
101 void styleDidChange(StyleDifference
, const ComputedStyle
*) override
;
103 bool explicitGridDidResize(const ComputedStyle
&) const;
104 bool namedGridLinesDefinitionDidChange(const ComputedStyle
&) const;
107 struct GridSizingData
;
108 bool gridElementIsShrinkToFit();
109 void computeUsedBreadthOfGridTracks(GridTrackSizingDirection
, GridSizingData
&, LayoutUnit
& availableLogicalSpace
);
110 LayoutUnit
computeUsedBreadthOfMinLength(GridTrackSizingDirection
, const GridLength
&) const;
111 LayoutUnit
computeUsedBreadthOfMaxLength(GridTrackSizingDirection
, const GridLength
&, LayoutUnit usedBreadth
) const;
112 LayoutUnit
computeUsedBreadthOfSpecifiedLength(GridTrackSizingDirection
, const Length
&) const;
113 void resolveContentBasedTrackSizingFunctions(GridTrackSizingDirection
, GridSizingData
&);
115 void ensureGridSize(size_t maximumRowIndex
, size_t maximumColumnIndex
);
116 void insertItemIntoGrid(LayoutBox
&, const GridCoordinate
&);
117 void placeItemsOnGrid();
118 void populateExplicitGridAndOrderIterator();
119 PassOwnPtr
<GridCoordinate
> createEmptyGridAreaAtSpecifiedPositionsOutsideGrid(const LayoutBox
&, GridTrackSizingDirection
, const GridSpan
& specifiedPositions
) const;
120 void placeSpecifiedMajorAxisItemsOnGrid(const Vector
<LayoutBox
*>&);
121 void placeAutoMajorAxisItemsOnGrid(const Vector
<LayoutBox
*>&);
122 void placeAutoMajorAxisItemOnGrid(LayoutBox
&, std::pair
<size_t, size_t>& autoPlacementCursor
);
123 GridTrackSizingDirection
autoPlacementMajorAxisDirection() const;
124 GridTrackSizingDirection
autoPlacementMinorAxisDirection() const;
126 void layoutGridItems();
127 void prepareChildForPositionedLayout(LayoutBox
&);
128 void layoutPositionedObjects(bool relayoutChildren
, PositionedLayoutBehavior
= DefaultLayout
);
129 void offsetAndBreadthForPositionedChild(const LayoutBox
&, GridTrackSizingDirection
, bool startIsAuto
, bool endIsAuto
, LayoutUnit
& offset
, LayoutUnit
& breadth
);
130 void populateGridPositions(GridSizingData
&, LayoutUnit availableSpaceForColumns
, LayoutUnit availableSpaceForRows
);
132 typedef struct GridItemsSpanGroupRange GridItemsSpanGroupRange
;
133 LayoutUnit
currentItemSizeForTrackSizeComputationPhase(TrackSizeComputationPhase
, LayoutBox
&, GridTrackSizingDirection
, Vector
<GridTrack
>& columnTracks
);
134 void resolveContentBasedTrackSizingFunctionsForNonSpanningItems(GridTrackSizingDirection
, const GridCoordinate
&, LayoutBox
& gridItem
, GridTrack
&, Vector
<GridTrack
>& columnTracks
);
135 template <TrackSizeComputationPhase
> void resolveContentBasedTrackSizingFunctionsForItems(GridTrackSizingDirection
, GridSizingData
&, const GridItemsSpanGroupRange
&);
136 template <TrackSizeComputationPhase
> void distributeSpaceToTracks(Vector
<GridTrack
*>&, const Vector
<GridTrack
*>* growBeyondGrowthLimitsTracks
, GridSizingData
&, LayoutUnit
& availableLogicalSpace
);
138 double computeNormalizedFractionBreadth(Vector
<GridTrack
>&, const GridSpan
& tracksSpan
, GridTrackSizingDirection
, LayoutUnit availableLogicalSpace
) const;
140 GridTrackSize
gridTrackSize(GridTrackSizingDirection
, size_t) const;
142 LayoutUnit
logicalHeightForChild(LayoutBox
&, Vector
<GridTrack
>&);
143 LayoutUnit
minSizeForChild(LayoutBox
&, GridTrackSizingDirection
, Vector
<GridTrack
>& columnTracks
);
144 LayoutUnit
minContentForChild(LayoutBox
&, GridTrackSizingDirection
, Vector
<GridTrack
>& columnTracks
);
145 LayoutUnit
maxContentForChild(LayoutBox
&, GridTrackSizingDirection
, Vector
<GridTrack
>& columnTracks
);
146 GridAxisPosition
columnAxisPositionForChild(const LayoutBox
&) const;
147 GridAxisPosition
rowAxisPositionForChild(const LayoutBox
&) const;
148 LayoutUnit
rowAxisOffsetForChild(const LayoutBox
&) const;
149 LayoutUnit
columnAxisOffsetForChild(const LayoutBox
&) const;
150 ContentAlignmentData
computeContentPositionAndDistributionOffset(GridTrackSizingDirection
, LayoutUnit availableFreeSpace
, unsigned numberOfGridTracks
) const;
151 LayoutPoint
findChildLogicalPosition(const LayoutBox
&, GridSizingData
&) const;
152 GridCoordinate
cachedGridCoordinate(const LayoutBox
&) const;
154 LayoutUnit
gridAreaBreadthForChild(const LayoutBox
& child
, GridTrackSizingDirection
, const Vector
<GridTrack
>&) const;
155 LayoutUnit
gridAreaBreadthForChildIncludingAlignmentOffsets(const LayoutBox
&, GridTrackSizingDirection
, const GridSizingData
&) const;
157 void applyStretchAlignmentToTracksIfNeeded(GridTrackSizingDirection
, GridSizingData
&, LayoutUnit availableSpace
);
159 void paintChildren(const PaintInfo
&, const LayoutPoint
&) override
;
161 bool needToStretchChildLogicalHeight(const LayoutBox
&) const;
162 LayoutUnit
childIntrinsicHeight(const LayoutBox
&) const;
163 LayoutUnit
childIntrinsicWidth(const LayoutBox
&) const;
164 LayoutUnit
intrinsicLogicalHeightForChild(const LayoutBox
&) const;
165 LayoutUnit
marginLogicalHeightForChild(const LayoutBox
&) const;
166 LayoutUnit
computeMarginLogicalHeightForChild(const LayoutBox
&) const;
167 LayoutUnit
availableAlignmentSpaceForChildBeforeStretching(LayoutUnit gridAreaBreadthForChild
, const LayoutBox
&) const;
168 void applyStretchAlignmentToChildIfNeeded(LayoutBox
&);
171 bool tracksAreWiderThanMinTrackBreadth(GridTrackSizingDirection
, const Vector
<GridTrack
>&);
174 size_t gridItemSpan(const LayoutBox
&, GridTrackSizingDirection
);
175 bool spanningItemCrossesFlexibleSizedTracks(const GridCoordinate
&, GridTrackSizingDirection
) const;
177 size_t gridColumnCount() const
179 ASSERT(!m_gridIsDirty
);
180 return m_grid
[0].size();
182 size_t gridRowCount() const
184 ASSERT(!m_gridIsDirty
);
185 return m_grid
.size();
188 bool hasDefiniteLogicalSize(GridTrackSizingDirection
) const;
190 typedef Vector
<Vector
<GridCell
>> GridRepresentation
;
191 GridRepresentation m_grid
;
193 Vector
<LayoutUnit
> m_rowPositions
;
194 Vector
<LayoutUnit
> m_columnPositions
;
195 HashMap
<const LayoutBox
*, GridCoordinate
> m_gridItemCoordinate
;
196 OrderIterator m_orderIterator
;
197 Vector
<LayoutBox
*> m_gridItemsOverflowingGridArea
;
198 HashMap
<const LayoutBox
*, size_t> m_gridItemsIndexesMap
;
201 DEFINE_LAYOUT_OBJECT_TYPE_CASTS(LayoutGrid
, isLayoutGrid());
205 #endif // LayoutGrid_h