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.
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.
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.
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.
24 #include "RenderWidget.h"
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"
35 #if USE(ACCELERATED_COMPOSITING)
36 #include "RenderLayerBacking.h"
43 static HashMap<const Widget*, RenderWidget*>& widgetRendererMap()
45 static HashMap<const Widget*, RenderWidget*>* staticWidgetRendererMap = new HashMap<const Widget*, RenderWidget*>;
46 return *staticWidgetRendererMap;
49 static size_t widgetHierarchyUpdateSuspendCount;
51 typedef HashMap<RefPtr<Widget>, FrameView*> WidgetToParentMap;
53 static WidgetToParentMap& widgetNewParentMap()
55 DEFINE_STATIC_LOCAL(WidgetToParentMap, map, ());
59 void RenderWidget::suspendWidgetHierarchyUpdates()
61 widgetHierarchyUpdateSuspendCount++;
64 void RenderWidget::resumeWidgetHierarchyUpdates()
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) {
77 currentParent->removeChild(child);
79 newParent->addChild(child);
83 widgetHierarchyUpdateSuspendCount--;
86 static void moveWidgetToParentSoon(Widget* child, FrameView* parent)
88 if (!widgetHierarchyUpdateSuspendCount) {
90 parent->addChild(child);
92 child->removeFromParent();
95 widgetNewParentMap().set(child, parent);
98 RenderWidget::RenderWidget(Node* node)
99 : RenderReplaced(node)
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.
107 view()->addWidget(this);
110 void RenderWidget::destroy()
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>.
118 animation()->cancelAnimations(this);
120 if (RenderView* v = view())
121 v->removeWidget(this);
123 if (m_hasCounterNodeMap)
124 RenderCounter::destroyCounterNodes(this);
126 if (AXObjectCache::accessibilityEnabled()) {
127 document()->axObjectCache()->childrenChanged(this->parent());
128 document()->axObjectCache()->remove(this);
134 // removes from override size map
135 if (hasOverrideSize())
138 if (style() && (style()->height().isPercent() || style()->minHeight().isPercent() || style()->maxHeight().isPercent()))
139 RenderBlock::removePercentHeightDescendant(this);
142 layer()->clearClipRects();
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();
154 RenderWidget::~RenderWidget()
156 ASSERT(m_refCount <= 0);
160 bool RenderWidget::setWidgetGeometry(const IntRect& frame)
162 ASSERT(!widgetHierarchyUpdateSuspendCount);
166 IntRect clipRect = enclosingLayer()->childrenClipRect();
167 bool clipChanged = m_clipRect != clipRect;
168 bool boundsChanged = m_widget->frameRect() != frame;
170 if (!boundsChanged && !clipChanged)
173 m_clipRect = clipRect;
175 RenderWidgetProtector protector(this);
176 RefPtr<Node> protectedNode(node());
177 m_widget->setFrameRect(frame);
179 #if USE(ACCELERATED_COMPOSITING)
180 if (hasLayer() && layer()->isComposited())
181 layer()->backing()->updateAfterWidgetResize();
184 return boundsChanged;
187 void RenderWidget::setWidget(PassRefPtr<Widget> widget)
189 if (widget == m_widget)
193 moveWidgetToParentSoon(m_widget.get(), 0);
194 widgetRendererMap().remove(m_widget.get());
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
205 setWidgetGeometry(absoluteContentBox());
206 if (style()->visibility() != VISIBLE)
211 moveWidgetToParentSoon(m_widget.get(), m_frameView);
215 void RenderWidget::layout()
217 ASSERT(needsLayout());
219 setNeedsLayout(false);
222 void RenderWidget::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
224 RenderReplaced::styleDidChange(diff, oldStyle);
226 if (style()->visibility() != VISIBLE)
233 void RenderWidget::showSubstituteImage(PassRefPtr<Image> prpImage)
235 m_substituteImage = prpImage;
239 void RenderWidget::paint(PaintInfo& paintInfo, int tx, int ty)
241 if (!shouldPaint(paintInfo, tx, ty))
247 if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
248 paintBoxDecorations(paintInfo, tx, ty);
250 if (paintInfo.phase == PaintPhaseMask) {
251 paintMask(paintInfo, tx, ty);
255 if (!m_frameView || paintInfo.phase != PaintPhaseForeground || style()->visibility() != VISIBLE)
259 if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
260 paintCustomHighlight(tx - x(), ty - y(), style()->highlight(), true);
263 if (style()->hasBorderRadius()) {
264 IntRect borderRect = IntRect(tx, ty, width(), height());
266 if (borderRect.isEmpty())
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();
272 IntSize topLeft, topRight, bottomLeft, bottomRight;
273 style()->getBorderRadiiForRect(borderRect, topLeft, topRight, bottomLeft, bottomRight);
275 paintInfo.context->addRoundedRectClip(borderRect, topLeft, topRight, bottomLeft, bottomRight);
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());
284 IntPoint widgetLocation = m_widget->frameRect().location();
285 IntPoint paintLocation(tx + borderLeft() + paddingLeft(), ty + borderTop() + paddingTop());
286 IntRect paintRect = paintInfo.rect;
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);
295 m_widget->paint(paintInfo.context, paintRect);
297 if (!paintOffset.isZero())
298 paintInfo.context->translate(-paintOffset);
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());
311 if (style()->hasBorderRadius())
312 paintInfo.context->restore();
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());
321 void RenderWidget::setOverlapTestResult(bool isOverlapped)
324 ASSERT(m_widget->isFrameView());
325 static_cast<FrameView*>(m_widget.get())->setIsOverlapped(isOverlapped);
328 void RenderWidget::deref(RenderArena *arena)
330 if (--m_refCount <= 0)
331 arenaDelete(arena, this);
334 void RenderWidget::updateWidgetPosition()
336 if (!m_widget || !node()) // Check the node in case destroy() has been called.
339 // FIXME: This doesn't work correctly with transforms.
340 FloatPoint absPos = localToAbsolute();
341 absPos.move(borderLeft() + paddingLeft(), borderTop() + paddingTop());
343 int w = width() - borderAndPaddingWidth();
344 int h = height() - borderAndPaddingHeight();
346 bool boundsChanged = setWidgetGeometry(IntRect(absPos.x(), absPos.y(), w, h));
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())
360 void RenderWidget::widgetPositionsUpdated()
364 m_widget->widgetPositionsUpdated();
367 IntRect RenderWidget::windowClipRect() const
372 return intersection(m_frameView->contentsToWindow(m_clipRect), m_frameView->windowClipRect());
375 void RenderWidget::setSelectionState(SelectionState state)
377 if (selectionState() != state) {
378 RenderReplaced::setSelectionState(state);
380 m_widget->setIsSelected(isSelected());
384 void RenderWidget::clearWidget()
389 RenderWidget* RenderWidget::find(const Widget* widget)
391 return widgetRendererMap().get(widget);
394 bool RenderWidget::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int x, int y, int tx, int ty, HitTestAction action)
396 bool hadResult = result.innerNode();
397 bool inside = RenderReplaced::nodeAtPoint(request, result, x, y, tx, ty, action);
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()));
405 } // namespace WebCore