OSDN Git Service

Merge WebKit at r71558: Initial merge by git.
[android-x86/external-webkit.git] / 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 "GraphicsContext.h"
29 #include "HitTestResult.h"
30 #include "RenderCounter.h"
31 #include "RenderLayer.h"
32 #include "RenderView.h"
33 #include "RenderWidgetProtector.h"
34
35 #if USE(ACCELERATED_COMPOSITING)
36 #include "RenderLayerBacking.h"
37 #endif
38
39 using namespace std;
40
41 namespace WebCore {
42
43 static HashMap<const Widget*, RenderWidget*>& widgetRendererMap()
44 {
45     static HashMap<const Widget*, RenderWidget*>* staticWidgetRendererMap = new HashMap<const Widget*, RenderWidget*>;
46     return *staticWidgetRendererMap;
47 }
48
49 static size_t widgetHierarchyUpdateSuspendCount;
50
51 typedef HashMap<RefPtr<Widget>, FrameView*> WidgetToParentMap;
52
53 static WidgetToParentMap& widgetNewParentMap()
54 {
55     DEFINE_STATIC_LOCAL(WidgetToParentMap, map, ());
56     return map;
57 }
58
59 void RenderWidget::suspendWidgetHierarchyUpdates()
60 {
61     widgetHierarchyUpdateSuspendCount++;
62 }
63
64 void RenderWidget::resumeWidgetHierarchyUpdates()
65 {
66     ASSERT(widgetHierarchyUpdateSuspendCount);
67     if (widgetHierarchyUpdateSuspendCount == 1) {
68         WidgetToParentMap map = widgetNewParentMap();
69         widgetNewParentMap().clear();
70         WidgetToParentMap::iterator end = map.end();
71         for (WidgetToParentMap::iterator it = map.begin(); it != end; ++it) {
72             Widget* child = it->first.get();
73             ScrollView* currentParent = child->parent();
74             FrameView* newParent = it->second;
75             if (newParent != currentParent) {
76                 if (currentParent)
77                     currentParent->removeChild(child);
78                 if (newParent)
79                     newParent->addChild(child);
80             }
81         }
82     }
83     widgetHierarchyUpdateSuspendCount--;
84 }
85
86 static void moveWidgetToParentSoon(Widget* child, FrameView* parent)
87 {
88     if (!widgetHierarchyUpdateSuspendCount) {
89         if (parent)
90             parent->addChild(child);
91         else
92             child->removeFromParent();
93         return;
94     }
95     widgetNewParentMap().set(child, parent);
96 }
97
98 RenderWidget::RenderWidget(Node* node)
99     : RenderReplaced(node)
100     , m_widget(0)
101     , m_frameView(node->document()->view())
102     // Reference counting is used to prevent the widget from being
103     // destroyed while inside the Widget code, which might not be
104     // able to handle that.
105     , m_refCount(1)
106 {
107     view()->addWidget(this);
108 }
109
110 void RenderWidget::destroy()
111 {
112     // We can't call the base class's destroy because we don't
113     // want to unconditionally delete ourselves (we're ref-counted).
114     // So the code below includes copied and pasted contents of
115     // both RenderBox::destroy() and RenderObject::destroy().
116     // Fix originally made for <rdar://problem/4228818>.
117
118     animation()->cancelAnimations(this);
119
120     if (RenderView* v = view())
121         v->removeWidget(this);
122
123     if (m_hasCounterNodeMap)
124         RenderCounter::destroyCounterNodes(this);
125     
126     if (AXObjectCache::accessibilityEnabled()) {
127         document()->axObjectCache()->childrenChanged(this->parent());
128         document()->axObjectCache()->remove(this);
129     }
130     remove();
131
132     setWidget(0);
133
134     // removes from override size map
135     if (hasOverrideSize())
136         setOverrideSize(-1);
137
138     if (style() && (style()->height().isPercent() || style()->minHeight().isPercent() || style()->maxHeight().isPercent()))
139         RenderBlock::removePercentHeightDescendant(this);
140
141     if (hasLayer()) {
142         layer()->clearClipRects();
143         setHasLayer(false);
144         destroyLayer();
145     }
146
147     // Grab the arena from node()->document()->renderArena() before clearing the node pointer.
148     // Clear the node before deref-ing, as this may be deleted when deref is called.
149     RenderArena* arena = renderArena();
150     setNode(0);
151     deref(arena);
152 }
153
154 RenderWidget::~RenderWidget()
155 {
156     ASSERT(m_refCount <= 0);
157     clearWidget();
158 }
159
160 bool RenderWidget::setWidgetGeometry(const IntRect& frame)
161 {
162     ASSERT(!widgetHierarchyUpdateSuspendCount);
163     if (!node())
164         return false;
165
166     IntRect clipRect = enclosingLayer()->childrenClipRect();
167     bool clipChanged = m_clipRect != clipRect;
168     bool boundsChanged = m_widget->frameRect() != frame;
169
170     if (!boundsChanged && !clipChanged)
171         return false;
172
173     m_clipRect = clipRect;
174
175     RenderWidgetProtector protector(this);
176     RefPtr<Node> protectedNode(node());
177     m_widget->setFrameRect(frame);
178     
179 #if USE(ACCELERATED_COMPOSITING)
180     if (hasLayer() && layer()->isComposited())
181         layer()->backing()->updateAfterWidgetResize();
182 #endif
183     
184     return boundsChanged;
185 }
186
187 void RenderWidget::setWidget(PassRefPtr<Widget> widget)
188 {
189     if (widget == m_widget)
190         return;
191
192     if (m_widget) {
193         moveWidgetToParentSoon(m_widget.get(), 0);
194         widgetRendererMap().remove(m_widget.get());
195         clearWidget();
196     }
197     m_widget = widget;
198     if (m_widget) {
199         widgetRendererMap().add(m_widget.get(), this);
200         // If we've already received a layout, apply the calculated space to the
201         // widget immediately, but we have to have really been fully constructed (with a non-null
202         // style pointer).
203         if (style()) {
204             if (!needsLayout())
205                 setWidgetGeometry(absoluteContentBox());
206             if (style()->visibility() != VISIBLE)
207                 m_widget->hide();
208             else
209                 m_widget->show();
210         }
211         moveWidgetToParentSoon(m_widget.get(), m_frameView);
212     }
213 }
214
215 void RenderWidget::layout()
216 {
217     ASSERT(needsLayout());
218
219     setNeedsLayout(false);
220 }
221
222 void RenderWidget::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
223 {
224     RenderReplaced::styleDidChange(diff, oldStyle);
225     if (m_widget) {
226         if (style()->visibility() != VISIBLE)
227             m_widget->hide();
228         else
229             m_widget->show();
230     }
231 }
232
233 void RenderWidget::showSubstituteImage(PassRefPtr<Image> prpImage)
234 {
235     m_substituteImage = prpImage;
236     repaint();
237 }
238
239 void RenderWidget::paint(PaintInfo& paintInfo, int tx, int ty)
240 {
241     if (!shouldPaint(paintInfo, tx, ty))
242         return;
243
244     tx += x();
245     ty += y();
246
247     if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
248         paintBoxDecorations(paintInfo, tx, ty);
249
250     if (paintInfo.phase == PaintPhaseMask) {
251         paintMask(paintInfo, tx, ty);
252         return;
253     }
254
255     if (!m_frameView || paintInfo.phase != PaintPhaseForeground || style()->visibility() != VISIBLE)
256         return;
257
258 #if PLATFORM(MAC)
259     if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
260         paintCustomHighlight(tx - x(), ty - y(), style()->highlight(), true);
261 #endif
262
263     if (style()->hasBorderRadius()) {
264         IntRect borderRect = IntRect(tx, ty, width(), height());
265
266         if (borderRect.isEmpty())
267             return;
268
269         // Push a clip if we have a border radius, since we want to round the foreground content that gets painted.
270         paintInfo.context->save();
271         
272         IntSize topLeft, topRight, bottomLeft, bottomRight;
273         style()->getBorderRadiiForRect(borderRect, topLeft, topRight, bottomLeft, bottomRight);
274
275         paintInfo.context->addRoundedRectClip(borderRect, topLeft, topRight, bottomLeft, bottomRight);
276     }
277
278     if (m_widget) {
279         // Tell the widget to paint now.  This is the only time the widget is allowed
280         // to paint itself.  That way it will composite properly with z-indexed layers.
281         if (m_substituteImage)
282             paintInfo.context->drawImage(m_substituteImage.get(), style()->colorSpace(), m_widget->frameRect());
283         else {
284             IntPoint widgetLocation = m_widget->frameRect().location();
285             IntPoint paintLocation(tx + borderLeft() + paddingLeft(), ty + borderTop() + paddingTop());
286             IntRect paintRect = paintInfo.rect;
287
288             IntSize paintOffset = paintLocation - widgetLocation;
289             // When painting widgets into compositing layers, tx and ty are relative to the enclosing compositing layer,
290             // not the root. In this case, shift the CTM and adjust the paintRect to be root-relative to fix plug-in drawing.
291             if (!paintOffset.isZero()) {
292                 paintInfo.context->translate(paintOffset);
293                 paintRect.move(-paintOffset);
294             }
295             m_widget->paint(paintInfo.context, paintRect);
296
297             if (!paintOffset.isZero())
298                 paintInfo.context->translate(-paintOffset);
299         }
300
301         if (m_widget->isFrameView()) {
302             FrameView* frameView = static_cast<FrameView*>(m_widget.get());
303             bool runOverlapTests = !frameView->useSlowRepaintsIfNotOverlapped() || frameView->hasCompositedContentIncludingDescendants();
304             if (paintInfo.overlapTestRequests && runOverlapTests) {
305                 ASSERT(!paintInfo.overlapTestRequests->contains(this));
306                 paintInfo.overlapTestRequests->set(this, m_widget->frameRect());
307             }
308          }
309     }
310
311     if (style()->hasBorderRadius())
312         paintInfo.context->restore();
313
314     // Paint a partially transparent wash over selected widgets.
315     if (isSelected() && !document()->printing()) {
316         // FIXME: selectionRect() is in absolute, not painting coordinates.
317         paintInfo.context->fillRect(selectionRect(), selectionBackgroundColor(), style()->colorSpace());
318     }
319 }
320
321 void RenderWidget::setOverlapTestResult(bool isOverlapped)
322 {
323     ASSERT(m_widget);
324     ASSERT(m_widget->isFrameView());
325     static_cast<FrameView*>(m_widget.get())->setIsOverlapped(isOverlapped);
326 }
327
328 void RenderWidget::deref(RenderArena *arena)
329 {
330     if (--m_refCount <= 0)
331         arenaDelete(arena, this);
332 }
333
334 void RenderWidget::updateWidgetPosition()
335 {
336     if (!m_widget || !node()) // Check the node in case destroy() has been called.
337         return;
338
339     // FIXME: This doesn't work correctly with transforms.
340     FloatPoint absPos = localToAbsolute();
341     absPos.move(borderLeft() + paddingLeft(), borderTop() + paddingTop());
342
343     int w = width() - borderAndPaddingWidth();
344     int h = height() - borderAndPaddingHeight();
345
346     bool boundsChanged = setWidgetGeometry(IntRect(absPos.x(), absPos.y(), w, h));
347
348 #ifndef ANDROID_FLATTEN_IFRAME
349     // if the frame bounds got changed, or if view needs layout (possibly indicating
350     // content size is wrong) we have to do a layout to set the right widget size
351     if (m_widget && m_widget->isFrameView()) {
352         FrameView* frameView = static_cast<FrameView*>(m_widget.get());
353         // Check the frame's page to make sure that the frame isn't in the process of being destroyed.
354         if ((boundsChanged || frameView->needsLayout()) && frameView->frame()->page())
355             frameView->layout();
356     }
357 #endif
358 }
359
360 void RenderWidget::widgetPositionsUpdated()
361 {
362     if (!m_widget)
363         return;
364     m_widget->widgetPositionsUpdated();
365 }
366
367 IntRect RenderWidget::windowClipRect() const
368 {
369     if (!m_frameView)
370         return IntRect();
371
372     return intersection(m_frameView->contentsToWindow(m_clipRect), m_frameView->windowClipRect());
373 }
374
375 void RenderWidget::setSelectionState(SelectionState state)
376 {
377     if (selectionState() != state) {
378         RenderReplaced::setSelectionState(state);
379         if (m_widget)
380             m_widget->setIsSelected(isSelected());
381     }
382 }
383
384 void RenderWidget::clearWidget()
385 {
386     m_widget = 0;
387 }
388
389 RenderWidget* RenderWidget::find(const Widget* widget)
390 {
391     return widgetRendererMap().get(widget);
392 }
393
394 bool RenderWidget::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int x, int y, int tx, int ty, HitTestAction action)
395 {
396     bool hadResult = result.innerNode();
397     bool inside = RenderReplaced::nodeAtPoint(request, result, x, y, tx, ty, action);
398     
399     // Check to see if we are really over the widget itself (and not just in the border/padding area).
400     if ((inside || result.isRectBasedTest()) && !hadResult && result.innerNode() == node())
401         result.setIsOverWidget(contentBoxRect().contains(result.localPoint()));
402     return inside;
403 }
404
405 } // namespace WebCore