1/*
2 * Copyright (C) 2017 Igalia S.L.
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 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#pragma once
27
28#include "GridPositionsResolver.h"
29#include "OrderIterator.h"
30#include <wtf/HashMap.h>
31#include <wtf/ListHashSet.h>
32#include <wtf/WeakPtr.h>
33
34namespace WebCore {
35
36typedef Vector<WeakPtr<RenderBox>, 1> GridCell;
37typedef Vector<Vector<GridCell>> GridAsMatrix;
38typedef ListHashSet<size_t> OrderedTrackIndexSet;
39
40class GridArea;
41class GridPositionsResolver;
42class RenderGrid;
43
44class Grid final {
45public:
46 explicit Grid(RenderGrid&);
47
48 unsigned numTracks(GridTrackSizingDirection) const;
49
50 void ensureGridSize(unsigned maximumRowSize, unsigned maximumColumnSize);
51 void insert(RenderBox&, const GridArea&);
52
53 // Note that each in flow child of a grid container becomes a grid item. This means that
54 // this method will return false for a grid container with only out of flow children.
55 bool hasGridItems() const { return !m_gridItemArea.isEmpty(); }
56
57 GridArea gridItemArea(const RenderBox& item) const;
58 void setGridItemArea(const RenderBox& item, GridArea);
59
60 GridSpan gridItemSpan(const RenderBox&, GridTrackSizingDirection) const;
61
62 const GridCell& cell(unsigned row, unsigned column) const { return m_grid[row][column]; }
63
64 int smallestTrackStart(GridTrackSizingDirection) const;
65 void setSmallestTracksStart(int rowStart, int columnStart);
66
67 unsigned autoRepeatTracks(GridTrackSizingDirection) const;
68 void setAutoRepeatTracks(unsigned autoRepeatRows, unsigned autoRepeatColumns);
69
70 void setAutoRepeatEmptyColumns(std::unique_ptr<OrderedTrackIndexSet>);
71 void setAutoRepeatEmptyRows(std::unique_ptr<OrderedTrackIndexSet>);
72
73 unsigned autoRepeatEmptyTracksCount(GridTrackSizingDirection) const;
74 bool hasAutoRepeatEmptyTracks(GridTrackSizingDirection) const;
75 bool isEmptyAutoRepeatTrack(GridTrackSizingDirection, unsigned) const;
76
77 OrderedTrackIndexSet* autoRepeatEmptyTracks(GridTrackSizingDirection) const;
78
79 OrderIterator& orderIterator() { return m_orderIterator; }
80
81 void setNeedsItemsPlacement(bool);
82 bool needsItemsPlacement() const { return m_needsItemsPlacement; };
83
84private:
85 friend class GridIterator;
86
87 OrderIterator m_orderIterator;
88
89 int m_smallestColumnStart { 0 };
90 int m_smallestRowStart { 0 };
91
92 unsigned m_autoRepeatColumns { 0 };
93 unsigned m_autoRepeatRows { 0 };
94
95 bool m_needsItemsPlacement { true };
96
97 GridAsMatrix m_grid;
98
99 HashMap<const RenderBox*, GridArea> m_gridItemArea;
100 HashMap<const RenderBox*, size_t> m_gridItemsIndexesMap;
101
102 std::unique_ptr<OrderedTrackIndexSet> m_autoRepeatEmptyColumns;
103 std::unique_ptr<OrderedTrackIndexSet> m_autoRepeatEmptyRows;
104};
105
106class GridIterator {
107 WTF_MAKE_NONCOPYABLE(GridIterator);
108public:
109 // |direction| is the direction that is fixed to |fixedTrackIndex| so e.g
110 // GridIterator(m_grid, ForColumns, 1) will walk over the rows of the 2nd column.
111 GridIterator(const Grid&, GridTrackSizingDirection, unsigned fixedTrackIndex, unsigned varyingTrackIndex = 0);
112
113 RenderBox* nextGridItem();
114 bool isEmptyAreaEnough(unsigned rowSpan, unsigned columnSpan) const;
115 std::unique_ptr<GridArea> nextEmptyGridArea(unsigned fixedTrackSpan, unsigned varyingTrackSpan);
116
117private:
118 const GridAsMatrix& m_grid;
119 GridTrackSizingDirection m_direction;
120 unsigned m_rowIndex;
121 unsigned m_columnIndex;
122 unsigned m_childIndex;
123};
124
125} // namespace WebCore
126