Fix the issue that Web Audio test case fails on PR3.
[framework/web/webkit-efl.git] / Source / WebCore / rendering / RenderWidget.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  * Copyright (C) 2000 Dirk Mueller (mueller@kde.org)
4  * Copyright (C) 2004, 2006, 2009, 2010 Apple Inc. All rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22
23 #include "config.h"
24 #include "RenderWidget.h"
25
26 #include "AXObjectCache.h"
27 #include "AnimationController.h"
28 #include "Frame.h"
29 #include "GraphicsContext.h"
30 #include "HitTestResult.h"
31 #include "RenderCounter.h"
32 #include "RenderLayer.h"
33 #include "RenderView.h"
34 #include "RenderWidgetProtector.h"
35
36 #if USE(ACCELERATED_COMPOSITING)
37 #include "RenderLayerBacking.h"
38 #endif
39
40 using namespace std;
41
42 namespace WebCore {
43
44 static HashMap<const Widget*, RenderWidget*>& widgetRendererMap()
45 {
46     static HashMap<const Widget*, RenderWidget*>* staticWidgetRendererMap = new HashMap<const Widget*, RenderWidget*>;
47     return *staticWidgetRendererMap;
48 }
49
50 static unsigned widgetHierarchyUpdateSuspendCount;
51
52 typedef HashMap<RefPtr<Widget>, FrameView*> WidgetToParentMap;
53
54 static WidgetToParentMap& widgetNewParentMap()
55 {
56     DEFINE_STATIC_LOCAL(WidgetToParentMap, map, ());
57     return map;
58 }
59
60 void RenderWidget::suspendWidgetHierarchyUpdates()
61 {
62     widgetHierarchyUpdateSuspendCount++;
63 }
64
65 void RenderWidget::resumeWidgetHierarchyUpdates()
66 {
67     ASSERT(widgetHierarchyUpdateSuspendCount);
68     if (widgetHierarchyUpdateSuspendCount == 1) {
69         WidgetToParentMap map = widgetNewParentMap();
70         widgetNewParentMap().clear();
71         WidgetToParentMap::iterator end = map.end();
72         for (WidgetToParentMap::iterator it = map.begin(); it != end; ++it) {
73             Widget* child = it->first.get();
74             ScrollView* currentParent = child->parent();
75             FrameView* newParent = it->second;
76             if (newParent != currentParent) {
77                 if (currentParent)
78                     currentParent->removeChild(child);
79                 if (newParent)
80                     newParent->addChild(child);
81             }
82         }
83     }
84     widgetHierarchyUpdateSuspendCount--;
85 }
86
87 static void moveWidgetToParentSoon(Widget* child, FrameView* parent)
88 {
89     if (!widgetHierarchyUpdateSuspendCount) {
90         if (parent)
91             parent->addChild(child);
92         else
93             child->removeFromParent();
94         return;
95     }
96     widgetNewParentMap().set(child, parent);
97 }
98
99 RenderWidget::RenderWidget(Node* node)
100     : RenderReplaced(node)
101     , m_widget(0)
102     , m_frameView(node->document()->view())
103     // Reference counting is used to prevent the widget from being
104     // destroyed while inside the Widget code, which might not be
105     // able to handle that.
106     , m_refCount(1)
107 {
108     view()->addWidget(this);
109 }
110
111 void RenderWidget::willBeDestroyed()
112 {
113     if (RenderView* v = view())
114         v->removeWidget(this);
115     
116     if (AXObjectCache::accessibilityEnabled()) {
117         document()->axObjectCache()->childrenChanged(this->parent());
118         document()->axObjectCache()->remove(this);
119     }
120
121     setWidget(0);
122
123     RenderReplaced::willBeDestroyed();
124 }
125
126 void RenderWidget::destroy()
127 {
128     willBeDestroyed();
129
130     // Grab the arena from node()->document()->renderArena() before clearing the node pointer.
131     // Clear the node before deref-ing, as this may be deleted when deref is called.
132     RenderArena* arena = renderArena();
133     setNode(0);
134     deref(arena);
135 }
136
137 RenderWidget::~RenderWidget()
138 {
139     ASSERT(m_refCount <= 0);
140     clearWidget();
141 }
142
143 // Widgets are always placed on integer boundaries, so rounding the size is actually
144 // the desired behavior. This function is here because it's otherwise seldom what we
145 // want to do with a LayoutRect.
146 static inline IntRect roundedIntRect(const LayoutRect& rect)
147 {
148     return IntRect(roundedIntPoint(rect.location()), roundedIntSize(rect.size()));
149 }
150
151 bool RenderWidget::setWidgetGeometry(const LayoutRect& frame)
152 {
153     if (!node())
154         return false;
155
156     IntRect clipRect = roundedIntRect(enclosingLayer()->childrenClipRect());
157     bool clipChanged = m_clipRect != clipRect;
158     bool boundsChanged = m_widget->frameRect() != frame;
159
160     if (!boundsChanged && !clipChanged)
161         return false;
162
163     m_clipRect = clipRect;
164
165     RenderWidgetProtector protector(this);
166     RefPtr<Node> protectedNode(node());
167     m_widget->setFrameRect(roundedIntRect(frame));
168     
169 #if USE(ACCELERATED_COMPOSITING)
170     if (hasLayer() && layer()->isComposited())
171         layer()->backing()->updateAfterWidgetResize();
172 #endif
173     
174     return boundsChanged;
175 }
176
177 bool RenderWidget::updateWidgetGeometry()
178 {
179     IntRect contentBox = pixelSnappedIntRect(contentBoxRect());
180     if (!m_widget->transformsAffectFrameRect())
181         return setWidgetGeometry(absoluteContentBox());
182
183     IntRect absoluteContentBox(localToAbsoluteQuad(FloatQuad(contentBox)).boundingBox());
184     if (m_widget->isFrameView()) {
185         contentBox.setLocation(absoluteContentBox.location());
186         return setWidgetGeometry(contentBox);
187     }
188
189     return setWidgetGeometry(absoluteContentBox);
190 }
191
192 void RenderWidget::setWidget(PassRefPtr<Widget> widget)
193 {
194     if (widget == m_widget)
195         return;
196
197     if (m_widget) {
198         moveWidgetToParentSoon(m_widget.get(), 0);
199         widgetRendererMap().remove(m_widget.get());
200         clearWidget();
201     }
202     m_widget = widget;
203     if (m_widget) {
204         widgetRendererMap().add(m_widget.get(), this);
205         // If we've already received a layout, apply the calculated space to the
206         // widget immediately, but we have to have really been fully constructed (with a non-null
207         // style pointer).
208         if (style()) {
209             if (!needsLayout())
210                 updateWidgetGeometry();
211
212             if (style()->visibility() != VISIBLE)
213                 m_widget->hide();
214             else {
215                 m_widget->show();
216                 repaint();
217             }
218         }
219         moveWidgetToParentSoon(m_widget.get(), m_frameView);
220     }
221 }
222
223 void RenderWidget::layout()
224 {
225     ASSERT(needsLayout());
226
227     setNeedsLayout(false);
228 }
229
230 void RenderWidget::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
231 {
232     RenderReplaced::styleDidChange(diff, oldStyle);
233     if (m_widget) {
234         if (style()->visibility() != VISIBLE)
235             m_widget->hide();
236         else
237             m_widget->show();
238     }
239 }
240
241 void RenderWidget::notifyWidget(WidgetNotification notification)
242 {
243     if (m_widget)
244         m_widget->notifyWidget(notification);
245 }
246
247 void RenderWidget::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
248 {
249     if (!shouldPaint(paintInfo, paintOffset))
250         return;
251
252     LayoutPoint adjustedPaintOffset = paintOffset + location();
253
254     if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
255         paintBoxDecorations(paintInfo, adjustedPaintOffset);
256
257     if (paintInfo.phase == PaintPhaseMask) {
258         paintMask(paintInfo, adjustedPaintOffset);
259         return;
260     }
261
262     if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && hasOutline())
263         paintOutline(paintInfo.context, LayoutRect(adjustedPaintOffset, size()));
264
265     if (!m_frameView || paintInfo.phase != PaintPhaseForeground)
266         return;
267
268 #if PLATFORM(MAC)
269     if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
270         paintCustomHighlight(paintOffset, style()->highlight(), true);
271 #endif
272
273     if (style()->hasBorderRadius()) {
274         LayoutRect borderRect = LayoutRect(adjustedPaintOffset, size());
275
276         if (borderRect.isEmpty())
277             return;
278
279         // Push a clip if we have a border radius, since we want to round the foreground content that gets painted.
280         paintInfo.context->save();
281         paintInfo.context->addRoundedRectClip(style()->getRoundedBorderFor(borderRect, view()));
282     }
283
284     if (m_widget) {
285         // Tell the widget to paint now.  This is the only time the widget is allowed
286         // to paint itself.  That way it will composite properly with z-indexed layers.
287         IntPoint widgetLocation = m_widget->frameRect().location();
288         IntPoint paintLocation(roundToInt(adjustedPaintOffset.x() + borderLeft() + paddingLeft()),
289             roundToInt(adjustedPaintOffset.y() + borderTop() + paddingTop()));
290         IntRect paintRect = paintInfo.rect;
291
292         IntSize widgetPaintOffset = paintLocation - widgetLocation;
293         // When painting widgets into compositing layers, tx and ty are relative to the enclosing compositing layer,
294         // not the root. In this case, shift the CTM and adjust the paintRect to be root-relative to fix plug-in drawing.
295         if (!widgetPaintOffset.isZero()) {
296             paintInfo.context->translate(widgetPaintOffset);
297             paintRect.move(-widgetPaintOffset);
298         }
299         m_widget->paint(paintInfo.context, paintRect);
300
301         if (!widgetPaintOffset.isZero())
302             paintInfo.context->translate(-widgetPaintOffset);
303
304         if (m_widget->isFrameView()) {
305             FrameView* frameView = static_cast<FrameView*>(m_widget.get());
306             bool runOverlapTests = !frameView->useSlowRepaintsIfNotOverlapped() || frameView->hasCompositedContentIncludingDescendants();
307             if (paintInfo.overlapTestRequests && runOverlapTests) {
308                 ASSERT(!paintInfo.overlapTestRequests->contains(this));
309                 paintInfo.overlapTestRequests->set(this, m_widget->frameRect());
310             }
311          }
312     }
313
314     if (style()->hasBorderRadius())
315         paintInfo.context->restore();
316
317     // Paint a partially transparent wash over selected widgets.
318     if (isSelected() && !document()->printing()) {
319         // FIXME: selectionRect() is in absolute, not painting coordinates.
320         paintInfo.context->fillRect(pixelSnappedIntRect(selectionRect()), selectionBackgroundColor(), style()->colorSpace());
321     }
322 }
323
324 void RenderWidget::setOverlapTestResult(bool isOverlapped)
325 {
326     ASSERT(m_widget);
327     ASSERT(m_widget->isFrameView());
328     static_cast<FrameView*>(m_widget.get())->setIsOverlapped(isOverlapped);
329 }
330
331 void RenderWidget::deref(RenderArena *arena)
332 {
333     if (--m_refCount <= 0)
334         arenaDelete(arena, this);
335 }
336
337 void RenderWidget::updateWidgetPosition()
338 {
339     if (!m_widget || !node()) // Check the node in case destroy() has been called.
340         return;
341
342     bool boundsChanged = updateWidgetGeometry();
343     
344     // if the frame bounds got changed, or if view needs layout (possibly indicating
345     // content size is wrong) we have to do a layout to set the right widget size
346     if (m_widget && m_widget->isFrameView()) {
347         FrameView* frameView = static_cast<FrameView*>(m_widget.get());
348         // Check the frame's page to make sure that the frame isn't in the process of being destroyed.
349         if ((boundsChanged || frameView->needsLayout()) && frameView->frame()->page())
350             frameView->layout();
351     }
352 }
353
354 void RenderWidget::widgetPositionsUpdated()
355 {
356     if (!m_widget)
357         return;
358     m_widget->widgetPositionsUpdated();
359 }
360
361 IntRect RenderWidget::windowClipRect() const
362 {
363     if (!m_frameView)
364         return IntRect();
365
366     return intersection(m_frameView->contentsToWindow(m_clipRect), m_frameView->windowClipRect());
367 }
368
369 void RenderWidget::setSelectionState(SelectionState state)
370 {
371     // The selection state for our containing block hierarchy is updated by the base class call.
372     RenderReplaced::setSelectionState(state);
373
374     if (m_widget)
375         m_widget->setIsSelected(isSelected());
376 }
377
378 void RenderWidget::clearWidget()
379 {
380     m_widget = 0;
381 }
382
383 RenderWidget* RenderWidget::find(const Widget* widget)
384 {
385     return widgetRendererMap().get(widget);
386 }
387
388 bool RenderWidget::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
389 {
390     bool hadResult = result.innerNode();
391     bool inside = RenderReplaced::nodeAtPoint(request, result, pointInContainer, accumulatedOffset, action);
392
393     // Check to see if we are really over the widget itself (and not just in the border/padding area).
394     if ((inside || result.isRectBasedTest()) && !hadResult && result.innerNode() == node())
395         result.setIsOverWidget(contentBoxRect().contains(result.localPoint()));
396     return inside;
397 }
398
399 CursorDirective RenderWidget::getCursor(const LayoutPoint& point, Cursor& cursor) const
400 {
401     if (widget() && widget()->isPluginViewBase()) {
402         // A plug-in is responsible for setting the cursor when the pointer is over it.
403         return DoNotSetCursor;
404     }
405     return RenderReplaced::getCursor(point, cursor);
406 }
407
408 } // namespace WebCore