2 * Copyright 2011 Adobe Systems Incorporated. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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
31 #include "RenderRegion.h"
33 #include "CSSStyleSelector.h"
34 #include "GraphicsContext.h"
35 #include "HitTestResult.h"
37 #include "PaintInfo.h"
38 #include "RenderBoxRegionInfo.h"
39 #include "RenderFlowThread.h"
40 #include "RenderView.h"
44 RenderRegion::RenderRegion(Node* node, RenderFlowThread* flowThread)
45 : RenderReplaced(node, IntSize())
46 , m_flowThread(flowThread)
47 , m_parentFlowThread(0)
49 , m_hasCustomRegionStyle(false)
53 RenderRegion::~RenderRegion()
55 deleteAllRenderBoxRegionInfo();
58 LayoutRect RenderRegion::regionOverflowRect() const
60 // FIXME: Would like to just use hasOverflowClip() but we aren't a block yet. When RenderRegion is eliminated and
61 // folded into RenderBlock, switch to hasOverflowClip().
62 bool clipX = style()->overflowX() != OVISIBLE;
63 bool clipY = style()->overflowY() != OVISIBLE;
64 if ((clipX && clipY) || !isValid() || !m_flowThread)
67 LayoutRect flowThreadOverflow = m_flowThread->visualOverflowRect();
69 // Only clip along the flow thread axis.
70 LayoutUnit outlineSize = maximalOutlineSize(PaintPhaseOutline);
72 if (m_flowThread->isHorizontalWritingMode()) {
73 LayoutUnit minY = isFirstRegion() ? (flowThreadOverflow.y() - outlineSize) : regionRect().y();
74 LayoutUnit maxY = isLastRegion() ? max(regionRect().maxY(), flowThreadOverflow.maxY()) + outlineSize : regionRect().maxY();
75 LayoutUnit minX = clipX ? regionRect().x() : (flowThreadOverflow.x() - outlineSize);
76 LayoutUnit maxX = clipX ? regionRect().maxX() : (flowThreadOverflow.maxX() + outlineSize);
77 clipRect = LayoutRect(minX, minY, maxX - minX, maxY - minY);
79 LayoutUnit minX = isFirstRegion() ? (flowThreadOverflow.x() - outlineSize) : regionRect().x();
80 LayoutUnit maxX = isLastRegion() ? max(regionRect().maxX(), flowThreadOverflow.maxX()) + outlineSize : regionRect().maxX();
81 LayoutUnit minY = clipY ? regionRect().y() : (flowThreadOverflow.y() - outlineSize);
82 LayoutUnit maxY = clipY ? regionRect().maxY() : (flowThreadOverflow.maxY() + outlineSize);
83 clipRect = LayoutRect(minX, minY, maxX - minX, maxY - minY);
89 bool RenderRegion::isFirstRegion() const
91 ASSERT(isValid() && m_flowThread);
92 return m_flowThread->firstRegion() == this;
95 bool RenderRegion::isLastRegion() const
97 ASSERT(isValid() && m_flowThread);
98 return m_flowThread->lastRegion() == this;
101 void RenderRegion::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
103 // Delegate painting of content in region to RenderFlowThread.
104 if (!m_flowThread || !isValid())
106 m_flowThread->paintIntoRegion(paintInfo, this, LayoutPoint(paintOffset.x() + borderLeft() + paddingLeft(), paintOffset.y() + borderTop() + paddingTop()));
110 bool RenderRegion::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
115 LayoutPoint adjustedLocation = accumulatedOffset + location();
117 // Check our bounds next. For this purpose always assume that we can only be hit in the
118 // foreground phase (which is true for replaced elements like images).
119 LayoutRect boundsRect(adjustedLocation, size());
120 if (visibleToHitTesting() && action == HitTestForeground && boundsRect.intersects(result.rectForPoint(pointInContainer))) {
121 // Check the contents of the RenderFlowThread.
122 if (m_flowThread && m_flowThread->hitTestRegion(this, request, result, pointInContainer, LayoutPoint(adjustedLocation.x() + borderLeft() + paddingLeft(), adjustedLocation.y() + borderTop() + paddingTop())))
124 updateHitTestResult(result, pointInContainer - toLayoutSize(adjustedLocation));
125 if (!result.addNodeToRectBasedTestResult(node(), pointInContainer, boundsRect))
132 void RenderRegion::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
134 RenderReplaced::styleDidChange(diff, oldStyle);
135 bool customRegionStyle = false;
137 Element* regionElement = static_cast<Element*>(node());
138 customRegionStyle = view()->document()->styleSelector()->checkRegionStyle(regionElement);
140 setHasCustomRegionStyle(customRegionStyle);
143 void RenderRegion::layout()
145 RenderReplaced::layout();
146 if (m_flowThread && isValid()) {
147 if (regionRect().width() != contentWidth() || regionRect().height() != contentHeight())
148 m_flowThread->invalidateRegions();
151 // FIXME: We need to find a way to set up overflow properly. Our flow thread hasn't gotten a layout
152 // yet, so we can't look to it for correct information. It's possible we could wait until after the RenderFlowThread
153 // gets a layout, and then try to propagate overflow information back to the region, and then mark for a second layout.
154 // That second layout would then be able to use the information from the RenderFlowThread to set up overflow.
156 // The big problem though is that overflow needs to be region-specific. We can't simply use the RenderFlowThread's global
157 // overflow values, since then we'd always think any narrow region had huge overflow (all the way to the width of the
158 // RenderFlowThread itself).
160 // We'll need to expand RenderBoxRegionInfo to also hold left and right overflow values.
163 void RenderRegion::attachRegion()
168 // By now the flow thread should already be added to the rendering tree,
169 // so we go up the rendering parents and check that this region is not part of the same
170 // flow that it actually needs to display. It would create a circular reference.
171 RenderObject* parentObject = parent();
172 m_parentFlowThread = 0;
173 for ( ; parentObject; parentObject = parentObject->parent()) {
174 if (parentObject->isRenderFlowThread()) {
175 m_parentFlowThread = toRenderFlowThread(parentObject);
176 // Do not take into account a region that links a flow with itself. The dependency
177 // cannot change, so it is not worth adding it to the list.
178 if (m_flowThread == m_parentFlowThread) {
186 m_flowThread->addRegionToThread(this);
189 void RenderRegion::detachRegion()
192 m_flowThread->removeRegionFromThread(this);
195 RenderBoxRegionInfo* RenderRegion::renderBoxRegionInfo(const RenderBox* box) const
197 if (!m_isValid || !m_flowThread)
199 return m_renderBoxRegionInfo.get(box);
202 RenderBoxRegionInfo* RenderRegion::setRenderBoxRegionInfo(const RenderBox* box, LayoutUnit logicalLeftInset, LayoutUnit logicalRightInset,
203 bool containingBlockChainIsInset)
205 ASSERT(m_isValid && m_flowThread);
206 if (!m_isValid || !m_flowThread)
209 RenderBoxRegionInfo* existingBoxInfo = m_renderBoxRegionInfo.get(box);
210 if (existingBoxInfo) {
211 *existingBoxInfo = RenderBoxRegionInfo(logicalLeftInset, logicalRightInset, containingBlockChainIsInset);
212 return existingBoxInfo;
215 RenderBoxRegionInfo* newBoxInfo = new RenderBoxRegionInfo(logicalLeftInset, logicalRightInset, containingBlockChainIsInset);
216 m_renderBoxRegionInfo.set(box, newBoxInfo);
220 RenderBoxRegionInfo* RenderRegion::takeRenderBoxRegionInfo(const RenderBox* box)
222 return m_renderBoxRegionInfo.take(box);
225 void RenderRegion::removeRenderBoxRegionInfo(const RenderBox* box)
227 delete m_renderBoxRegionInfo.take(box);
230 void RenderRegion::deleteAllRenderBoxRegionInfo()
232 deleteAllValues(m_renderBoxRegionInfo);
233 m_renderBoxRegionInfo.clear();
236 LayoutUnit RenderRegion::offsetFromLogicalTopOfFirstPage() const
238 if (!m_isValid || !m_flowThread)
240 if (m_flowThread->isHorizontalWritingMode())
241 return regionRect().y();
242 return regionRect().x();
245 } // namespace WebCore