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"
29 #include "GraphicsContext.h"
30 #include "HitTestResult.h"
31 #include "RenderCounter.h"
32 #include "RenderLayer.h"
33 #include "RenderView.h"
34 #include "RenderWidgetProtector.h"
36 #if USE(ACCELERATED_COMPOSITING)
37 #include "RenderLayerBacking.h"
44 static HashMap<const Widget*, RenderWidget*>& widgetRendererMap()
46 static HashMap<const Widget*, RenderWidget*>* staticWidgetRendererMap = new HashMap<const Widget*, RenderWidget*>;
47 return *staticWidgetRendererMap;
50 static unsigned widgetHierarchyUpdateSuspendCount;
52 typedef HashMap<RefPtr<Widget>, FrameView*> WidgetToParentMap;
54 static WidgetToParentMap& widgetNewParentMap()
56 DEFINE_STATIC_LOCAL(WidgetToParentMap, map, ());
60 void RenderWidget::suspendWidgetHierarchyUpdates()
62 widgetHierarchyUpdateSuspendCount++;
65 void RenderWidget::resumeWidgetHierarchyUpdates()
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) {
78 currentParent->removeChild(child);
80 newParent->addChild(child);
84 widgetHierarchyUpdateSuspendCount--;
87 static void moveWidgetToParentSoon(Widget* child, FrameView* parent)
89 if (!widgetHierarchyUpdateSuspendCount) {
91 parent->addChild(child);
93 child->removeFromParent();
96 widgetNewParentMap().set(child, parent);
99 RenderWidget::RenderWidget(Node* node)
100 : RenderReplaced(node)
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.
108 view()->addWidget(this);
111 void RenderWidget::willBeDestroyed()
113 if (RenderView* v = view())
114 v->removeWidget(this);
116 if (AXObjectCache::accessibilityEnabled()) {
117 document()->axObjectCache()->childrenChanged(this->parent());
118 document()->axObjectCache()->remove(this);
123 RenderReplaced::willBeDestroyed();
126 void RenderWidget::destroy()
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();
137 RenderWidget::~RenderWidget()
139 ASSERT(m_refCount <= 0);
143 bool RenderWidget::setWidgetGeometry(const IntRect& frame)
148 IntRect clipRect = enclosingLayer()->childrenClipRect();
149 bool clipChanged = m_clipRect != clipRect;
150 bool boundsChanged = m_widget->frameRect() != frame;
152 if (!boundsChanged && !clipChanged)
155 m_clipRect = clipRect;
157 RenderWidgetProtector protector(this);
158 RefPtr<Node> protectedNode(node());
159 m_widget->setFrameRect(frame);
161 #if USE(ACCELERATED_COMPOSITING)
162 if (hasLayer() && layer()->isComposited())
163 layer()->backing()->updateAfterWidgetResize();
166 return boundsChanged;
169 bool RenderWidget::updateWidgetGeometry()
171 IntRect contentBox = contentBoxRect();
172 if (!m_widget->transformsAffectFrameRect())
173 return setWidgetGeometry(absoluteContentBox());
175 IntRect absoluteContentBox = IntRect(localToAbsoluteQuad(FloatQuad(contentBox)).boundingBox());
176 if (m_widget->isFrameView()) {
177 contentBox.setLocation(absoluteContentBox.location());
178 return setWidgetGeometry(contentBox);
181 return setWidgetGeometry(absoluteContentBox);
184 void RenderWidget::setWidget(PassRefPtr<Widget> widget)
186 if (widget == m_widget)
190 moveWidgetToParentSoon(m_widget.get(), 0);
191 widgetRendererMap().remove(m_widget.get());
196 widgetRendererMap().add(m_widget.get(), this);
197 // If we've already received a layout, apply the calculated space to the
198 // widget immediately, but we have to have really been fully constructed (with a non-null
202 updateWidgetGeometry();
204 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::notifyWidget(WidgetNotification notification)
236 m_widget->notifyWidget(notification);
239 void RenderWidget::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
241 if (!shouldPaint(paintInfo, paintOffset))
244 LayoutPoint adjustedPaintOffset = paintOffset + location();
246 if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
247 paintBoxDecorations(paintInfo, adjustedPaintOffset);
249 if (paintInfo.phase == PaintPhaseMask) {
250 paintMask(paintInfo, adjustedPaintOffset);
254 if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && hasOutline())
255 paintOutline(paintInfo.context, LayoutRect(adjustedPaintOffset, size()));
257 if (!m_frameView || paintInfo.phase != PaintPhaseForeground)
261 if (style()->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
262 paintCustomHighlight(paintOffset, style()->highlight(), true);
265 if (style()->hasBorderRadius()) {
266 LayoutRect borderRect = LayoutRect(adjustedPaintOffset, size());
268 if (borderRect.isEmpty())
271 // Push a clip if we have a border radius, since we want to round the foreground content that gets painted.
272 paintInfo.context->save();
273 paintInfo.context->addRoundedRectClip(style()->getRoundedBorderFor(borderRect));
277 // Tell the widget to paint now. This is the only time the widget is allowed
278 // to paint itself. That way it will composite properly with z-indexed layers.
279 LayoutPoint widgetLocation = m_widget->frameRect().location();
280 LayoutPoint paintLocation(adjustedPaintOffset.x() + borderLeft() + paddingLeft(), adjustedPaintOffset.y() + borderTop() + paddingTop());
281 LayoutRect paintRect = paintInfo.rect;
283 LayoutSize widgetPaintOffset = paintLocation - widgetLocation;
284 // When painting widgets into compositing layers, tx and ty are relative to the enclosing compositing layer,
285 // not the root. In this case, shift the CTM and adjust the paintRect to be root-relative to fix plug-in drawing.
286 if (!widgetPaintOffset.isZero()) {
287 paintInfo.context->translate(widgetPaintOffset);
288 paintRect.move(-widgetPaintOffset);
290 m_widget->paint(paintInfo.context, paintRect);
292 if (!widgetPaintOffset.isZero())
293 paintInfo.context->translate(-widgetPaintOffset);
295 if (m_widget->isFrameView()) {
296 FrameView* frameView = static_cast<FrameView*>(m_widget.get());
297 bool runOverlapTests = !frameView->useSlowRepaintsIfNotOverlapped() || frameView->hasCompositedContentIncludingDescendants();
298 if (paintInfo.overlapTestRequests && runOverlapTests) {
299 ASSERT(!paintInfo.overlapTestRequests->contains(this));
300 paintInfo.overlapTestRequests->set(this, m_widget->frameRect());
305 if (style()->hasBorderRadius())
306 paintInfo.context->restore();
308 // Paint a partially transparent wash over selected widgets.
309 if (isSelected() && !document()->printing()) {
310 // FIXME: selectionRect() is in absolute, not painting coordinates.
311 paintInfo.context->fillRect(selectionRect(), selectionBackgroundColor(), style()->colorSpace());
315 void RenderWidget::setOverlapTestResult(bool isOverlapped)
318 ASSERT(m_widget->isFrameView());
319 static_cast<FrameView*>(m_widget.get())->setIsOverlapped(isOverlapped);
322 void RenderWidget::deref(RenderArena *arena)
324 if (--m_refCount <= 0)
325 arenaDelete(arena, this);
328 void RenderWidget::updateWidgetPosition()
330 if (!m_widget || !node()) // Check the node in case destroy() has been called.
333 bool boundsChanged = updateWidgetGeometry();
335 // if the frame bounds got changed, or if view needs layout (possibly indicating
336 // content size is wrong) we have to do a layout to set the right widget size
337 if (m_widget && m_widget->isFrameView()) {
338 FrameView* frameView = static_cast<FrameView*>(m_widget.get());
339 // Check the frame's page to make sure that the frame isn't in the process of being destroyed.
340 if ((boundsChanged || frameView->needsLayout()) && frameView->frame()->page())
345 void RenderWidget::widgetPositionsUpdated()
349 m_widget->widgetPositionsUpdated();
352 IntRect RenderWidget::windowClipRect() const
357 return intersection(m_frameView->contentsToWindow(m_clipRect), m_frameView->windowClipRect());
360 void RenderWidget::setSelectionState(SelectionState state)
362 if (selectionState() != state) {
363 RenderReplaced::setSelectionState(state);
365 m_widget->setIsSelected(isSelected());
369 void RenderWidget::clearWidget()
374 RenderWidget* RenderWidget::find(const Widget* widget)
376 return widgetRendererMap().get(widget);
379 bool RenderWidget::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
381 bool hadResult = result.innerNode();
382 bool inside = RenderReplaced::nodeAtPoint(request, result, pointInContainer, accumulatedOffset, action);
384 // Check to see if we are really over the widget itself (and not just in the border/padding area).
385 if ((inside || result.isRectBasedTest()) && !hadResult && result.innerNode() == node())
386 result.setIsOverWidget(contentBoxRect().contains(result.localPoint()));
390 CursorDirective RenderWidget::getCursor(const LayoutPoint& point, Cursor& cursor) const
392 if (widget() && widget()->isPluginViewBase()) {
393 // A plug-in is responsible for setting the cursor when the pointer is over it.
394 return DoNotSetCursor;
396 return RenderReplaced::getCursor(point, cursor);
399 } // namespace WebCore