1/*
2 * Copyright (C) 2011 Adobe Systems Incorporated. All rights reserved.
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 *
8 * 1. Redistributions of source code must retain the above
9 *    copyright notice, this list of conditions and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 *    copyright notice, this list of conditions and the following
13 *    disclaimer in the documentation and/or other materials
14 *    provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef RenderRegion_h
31#define RenderRegion_h
32
33#include "RenderBlockFlow.h"
34#include "StyleInheritedData.h"
35#include "VisiblePosition.h"
36#include <memory>
37
38namespace WebCore {
39
40struct LayerFragment;
41typedef Vector<LayerFragment, 1> LayerFragments;
42
43class Element;
44class RenderBox;
45class RenderBoxRegionInfo;
46class RenderFlowThread;
47class RenderNamedFlowThread;
48
49class RenderRegion : public RenderBlockFlow {
50public:
51    virtual bool isRenderRegion() const override final { return true; }
52
53    virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
54
55    void setFlowThreadPortionRect(const LayoutRect& rect) { m_flowThreadPortionRect = rect; }
56    LayoutRect flowThreadPortionRect() const { return m_flowThreadPortionRect; }
57    LayoutRect flowThreadPortionOverflowRect();
58
59    LayoutPoint flowThreadPortionLocation() const;
60
61    virtual void attachRegion();
62    virtual void detachRegion();
63
64    RenderNamedFlowThread* parentNamedFlowThread() const { return m_parentNamedFlowThread; }
65    RenderFlowThread* flowThread() const { return m_flowThread; }
66
67    // Valid regions do not create circular dependencies with other flows.
68    bool isValid() const { return m_isValid; }
69    void setIsValid(bool valid) { m_isValid = valid; }
70
71    RenderBoxRegionInfo* renderBoxRegionInfo(const RenderBox*) const;
72    RenderBoxRegionInfo* setRenderBoxRegionInfo(const RenderBox*, LayoutUnit logicalLeftInset, LayoutUnit logicalRightInset,
73        bool containingBlockChainIsInset);
74    std::unique_ptr<RenderBoxRegionInfo> takeRenderBoxRegionInfo(const RenderBox*);
75    void removeRenderBoxRegionInfo(const RenderBox*);
76
77    void deleteAllRenderBoxRegionInfo();
78
79    bool isFirstRegion() const;
80    bool isLastRegion() const;
81    virtual bool shouldClipFlowThreadContent() const;
82
83    // These methods represent the width and height of a "page" and for a RenderRegion they are just the
84    // content width and content height of a region. For RenderRegionSets, however, they will be the width and
85    // height of a single column or page in the set.
86    virtual LayoutUnit pageLogicalWidth() const;
87    virtual LayoutUnit pageLogicalHeight() const;
88
89    LayoutUnit logicalTopOfFlowThreadContentRect(const LayoutRect&) const;
90    LayoutUnit logicalBottomOfFlowThreadContentRect(const LayoutRect&) const;
91    LayoutUnit logicalTopForFlowThreadContent() const { return logicalTopOfFlowThreadContentRect(flowThreadPortionRect()); };
92    LayoutUnit logicalBottomForFlowThreadContent() const { return logicalBottomOfFlowThreadContentRect(flowThreadPortionRect()); };
93
94    // This method represents the logical height of the entire flow thread portion used by the region or set.
95    // For RenderRegions it matches logicalPaginationHeight(), but for sets it is the height of all the pages
96    // or columns added together.
97    virtual LayoutUnit logicalHeightOfAllFlowThreadContent() const;
98
99    // The top of the nearest page inside the region. For RenderRegions, this is just the logical top of the
100    // flow thread portion we contain. For sets, we have to figure out the top of the nearest column or
101    // page.
102    virtual LayoutUnit pageLogicalTopForOffset(LayoutUnit offset) const;
103
104    // Whether or not this region is a set.
105    virtual bool isRenderRegionSet() const { return false; }
106
107    virtual void repaintFlowThreadContent(const LayoutRect& repaintRect);
108
109    virtual void collectLayerFragments(LayerFragments&, const LayoutRect&, const LayoutRect&) { }
110
111    virtual void adjustRegionBoundsFromFlowThreadPortionRect(const LayoutPoint& layerOffset, LayoutRect& regionBounds); // layerOffset is needed for multi-column.
112
113    void addLayoutOverflowForBox(const RenderBox*, const LayoutRect&);
114    void addVisualOverflowForBox(const RenderBox*, const LayoutRect&);
115    LayoutRect layoutOverflowRectForBox(const RenderBox*);
116    LayoutRect visualOverflowRectForBox(const RenderBoxModelObject*);
117    LayoutRect layoutOverflowRectForBoxForPropagation(const RenderBox*);
118    LayoutRect visualOverflowRectForBoxForPropagation(const RenderBoxModelObject*);
119
120    LayoutRect rectFlowPortionForBox(const RenderBox*, const LayoutRect&) const;
121
122    void setRegionObjectsRegionStyle();
123    void restoreRegionObjectsOriginalStyle();
124
125    virtual bool canHaveChildren() const override { return false; }
126    virtual bool canHaveGeneratedChildren() const override { return true; }
127    virtual VisiblePosition positionForPoint(const LayoutPoint&, const RenderRegion*) override;
128
129    virtual bool hasAutoLogicalHeight() const { return false; }
130
131    virtual void absoluteQuadsForBoxInRegion(Vector<FloatQuad>&, bool*, const RenderBox*, float, float) { }
132
133protected:
134    RenderRegion(Element&, PassRef<RenderStyle>, RenderFlowThread*);
135    RenderRegion(Document&, PassRef<RenderStyle>, RenderFlowThread*);
136
137    void ensureOverflowForBox(const RenderBox*, RefPtr<RenderOverflow>&, bool);
138
139    virtual void computePreferredLogicalWidths() override;
140    virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const override;
141
142    enum OverflowType {
143        LayoutOverflow = 0,
144        VisualOverflow
145    };
146
147    LayoutRect overflowRectForFlowThreadPortion(const LayoutRect& flowThreadPortionRect, bool isFirstPortion, bool isLastPortion, OverflowType);
148    void repaintFlowThreadContentRectangle(const LayoutRect& repaintRect, const LayoutRect& flowThreadPortionRect, const LayoutPoint& regionLocation, const LayoutRect* flowThreadPortionClipRect = 0);
149
150    void computeOverflowFromFlowThread();
151
152private:
153    virtual const char* renderName() const { return "RenderRegion"; }
154
155    virtual void insertedIntoTree() override;
156    virtual void willBeRemovedFromTree() override;
157
158    virtual void installFlowThread();
159
160    LayoutPoint mapRegionPointIntoFlowThreadCoordinates(const LayoutPoint&);
161
162protected:
163    RenderFlowThread* m_flowThread;
164
165private:
166    // If this RenderRegion is displayed as part of another named flow,
167    // we need to create a dependency tree, so that layout of the
168    // regions is always done before the regions themselves.
169    RenderNamedFlowThread* m_parentNamedFlowThread;
170    LayoutRect m_flowThreadPortionRect;
171
172    // This map holds unique information about a block that is split across regions.
173    // A RenderBoxRegionInfo* tells us about any layout information for a RenderBox that
174    // is unique to the region. For now it just holds logical width information for RenderBlocks, but eventually
175    // it will also hold a custom style for any box (for region styling).
176    typedef HashMap<const RenderBox*, std::unique_ptr<RenderBoxRegionInfo>> RenderBoxRegionInfoMap;
177    RenderBoxRegionInfoMap m_renderBoxRegionInfo;
178
179    bool m_isValid : 1;
180};
181
182RENDER_OBJECT_TYPE_CASTS(RenderRegion, isRenderRegion())
183
184class CurrentRenderRegionMaintainer {
185    WTF_MAKE_NONCOPYABLE(CurrentRenderRegionMaintainer);
186public:
187    CurrentRenderRegionMaintainer(RenderRegion&);
188    ~CurrentRenderRegionMaintainer();
189
190    RenderRegion& region() const { return m_region; }
191private:
192    RenderRegion& m_region;
193};
194
195} // namespace WebCore
196
197#endif // RenderRegion_h
198