tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / rendering / RenderRegion.cpp
1 /*
2  * Copyright 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 #include "config.h"
31 #include "RenderRegion.h"
32
33 #include "CSSStyleSelector.h"
34 #include "GraphicsContext.h"
35 #include "HitTestResult.h"
36 #include "IntRect.h"
37 #include "PaintInfo.h"
38 #include "RenderBoxRegionInfo.h"
39 #include "RenderFlowThread.h"
40 #include "RenderView.h"
41
42 namespace WebCore {
43
44 RenderRegion::RenderRegion(Node* node, RenderFlowThread* flowThread)
45     : RenderReplaced(node, IntSize())
46     , m_flowThread(flowThread)
47     , m_parentFlowThread(0)
48     , m_isValid(false)
49     , m_hasCustomRegionStyle(false)
50 {
51 }
52
53 RenderRegion::~RenderRegion()
54 {
55     deleteAllRenderBoxRegionInfo();
56 }
57
58 LayoutRect RenderRegion::regionOverflowRect() const
59 {
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)
65         return regionRect();
66
67     LayoutRect flowThreadOverflow = m_flowThread->visualOverflowRect();
68
69     // Only clip along the flow thread axis.
70     LayoutUnit outlineSize = maximalOutlineSize(PaintPhaseOutline);
71     LayoutRect clipRect;
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);
78     } else {
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);
84     }
85
86     return clipRect;
87 }
88
89 bool RenderRegion::isFirstRegion() const
90 {
91     ASSERT(isValid() && m_flowThread);
92     return m_flowThread->firstRegion() == this;
93 }
94
95 bool RenderRegion::isLastRegion() const
96 {
97     ASSERT(isValid() && m_flowThread);
98     return m_flowThread->lastRegion() == this;
99 }
100
101 void RenderRegion::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
102 {
103     // Delegate painting of content in region to RenderFlowThread.
104     if (!m_flowThread || !isValid())
105         return;
106     m_flowThread->paintIntoRegion(paintInfo, this, LayoutPoint(paintOffset.x() + borderLeft() + paddingLeft(), paintOffset.y() + borderTop() + paddingTop()));
107 }
108
109 // Hit Testing
110 bool RenderRegion::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
111 {
112     if (!isValid())
113         return false;
114
115     LayoutPoint adjustedLocation = accumulatedOffset + location();
116
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())))
123             return true;
124         updateHitTestResult(result, pointInContainer - toLayoutSize(adjustedLocation));
125         if (!result.addNodeToRectBasedTestResult(node(), pointInContainer, boundsRect))
126             return true;
127     }
128
129     return false;
130 }
131
132 void RenderRegion::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
133 {
134     RenderReplaced::styleDidChange(diff, oldStyle);
135     bool customRegionStyle = false;
136     if (node()) {
137         Element* regionElement = static_cast<Element*>(node());
138         customRegionStyle = view()->document()->styleSelector()->checkRegionStyle(regionElement);
139     }
140     setHasCustomRegionStyle(customRegionStyle);
141 }
142
143 void RenderRegion::layout()
144 {
145     RenderReplaced::layout();
146     if (m_flowThread && isValid()) {
147         if (regionRect().width() != contentWidth() || regionRect().height() != contentHeight())
148             m_flowThread->invalidateRegions();
149     }
150     
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.
155     //
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).
159     //
160     // We'll need to expand RenderBoxRegionInfo to also hold left and right overflow values.
161 }
162
163 void RenderRegion::attachRegion()
164 {
165     if (!m_flowThread)
166         return;
167
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) {
179                 m_flowThread = 0;
180                 return;
181             }
182             break;
183         }
184     }
185
186     m_flowThread->addRegionToThread(this);
187 }
188
189 void RenderRegion::detachRegion()
190 {
191     if (m_flowThread)
192         m_flowThread->removeRegionFromThread(this);
193 }
194
195 RenderBoxRegionInfo* RenderRegion::renderBoxRegionInfo(const RenderBox* box) const
196 {
197     if (!m_isValid || !m_flowThread)
198         return 0;
199     return m_renderBoxRegionInfo.get(box);
200 }
201
202 RenderBoxRegionInfo* RenderRegion::setRenderBoxRegionInfo(const RenderBox* box, LayoutUnit logicalLeftInset, LayoutUnit logicalRightInset,
203     bool containingBlockChainIsInset)
204 {
205     ASSERT(m_isValid && m_flowThread);
206     if (!m_isValid || !m_flowThread)
207         return 0;
208
209     RenderBoxRegionInfo* existingBoxInfo = m_renderBoxRegionInfo.get(box);
210     if (existingBoxInfo) {
211         *existingBoxInfo = RenderBoxRegionInfo(logicalLeftInset, logicalRightInset, containingBlockChainIsInset);
212         return existingBoxInfo;
213     }
214     
215     RenderBoxRegionInfo* newBoxInfo = new RenderBoxRegionInfo(logicalLeftInset, logicalRightInset, containingBlockChainIsInset);
216     m_renderBoxRegionInfo.set(box, newBoxInfo);
217     return newBoxInfo;
218 }
219
220 RenderBoxRegionInfo* RenderRegion::takeRenderBoxRegionInfo(const RenderBox* box)
221 {
222     return m_renderBoxRegionInfo.take(box);
223 }
224
225 void RenderRegion::removeRenderBoxRegionInfo(const RenderBox* box)
226 {
227     delete m_renderBoxRegionInfo.take(box);
228 }
229
230 void RenderRegion::deleteAllRenderBoxRegionInfo()
231 {
232     deleteAllValues(m_renderBoxRegionInfo);
233     m_renderBoxRegionInfo.clear();
234 }
235
236 LayoutUnit RenderRegion::offsetFromLogicalTopOfFirstPage() const
237 {
238     if (!m_isValid || !m_flowThread)
239         return 0;
240     if (m_flowThread->isHorizontalWritingMode())
241         return regionRect().y();
242     return regionRect().x();
243 }
244
245 } // namespace WebCore